Kitlist  1.1.0
yamlconfig.cpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of Kitlist, a program to maintain a simple list
4  of items and assign items to one or more categories.
5 
6  Copyright (C) 2008-2021 Frank Dean
7 
8  Kitlist is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  Kitlist is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Kitlist. If not, see <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #include "yamlconfig.hpp"
24 #include <config.h>
25 
26 #ifndef GCONF
27 #include <glibmm/miscutils.h>
28 #include <fstream>
29 #include <iostream>
30 #include <yaml-cpp/yaml.h>
31 
32 
34 const std::string CONFIG_FILENAME = "/kitlist";
35 
37 const std::string PAGE_TITLE_CONFIG_KEY = "Printed page title";
38 
40 const std::string CURRENT_FILENAME_CONFIG_KEY = "current filename";
41 
43 const std::string RECENT_FILES_CONFIG_KEY = "file history list";
44 
46 const std::string MAX_RECENT_FILES_CONFIG_KEY = "max recent files";
47 
49 const std::string DEBUG_LOG_FILENAME_CONFIG_KEY = "debug_log_filename";
50 
52  m_max_recent_files(DEFAULT_MAX_RECENT_FILES),
53  m_current_filename(""),
54  m_page_title(""),
55  m_debug_log_filename("/tmp/kitlist.log") {
57 };
58 
61  // Try getting the value from XDG config file
62  try {
63  YAML::Node config = YAML::LoadFile(get_config_filename().c_str());
64  try {
65  m_current_filename = config[CURRENT_FILENAME_CONFIG_KEY].as<std::string>();
66  } catch (std::exception) {
67  g_info("Current filename not found in config");
68  }
69  try {
70  YAML::Node files = config[RECENT_FILES_CONFIG_KEY];
71  for (YAML::const_iterator it = files.begin(); it != files.end(); ++it) {
72  Glib::ustring filename = it->as<std::string>();
73  m_mru_file_history.push_back(filename);
74  }
75  } catch (std::exception) {
76  g_info("MRU file list not found in config");
77  }
78  try {
79  m_page_title = config[PAGE_TITLE_CONFIG_KEY].as<std::string>();
80  } catch (std::exception) {
81  g_info("Page title not found in config");
83  }
84  try {
86  } catch (std::exception) {
87  g_info("Max recent filename history count not found in config");
88  }
89  try {
90  m_debug_log_filename = config[DEBUG_LOG_FILENAME_CONFIG_KEY].as<std::string>();
91  } catch (std::exception) {
92  g_info("Debug log filename not found in config");
93  }
94  } catch (std::exception ex) {
95  g_info("Error loading config file");
97  // m_yaml_config[PAGE_TITLE_CONFIG_KEY] = m_page_title.c_str();
98  }
99 }
100 
105  YAML::Node config;
106  if (m_current_filename.empty()) {
107  config.remove(CURRENT_FILENAME_CONFIG_KEY);
108  } else {
110  }
111  if (m_page_title.empty()) {
112  config.remove(PAGE_TITLE_CONFIG_KEY);
113  } else {
114  config[PAGE_TITLE_CONFIG_KEY] = m_page_title.c_str();
115  }
118  for (std::deque<Glib::ustring>::const_iterator it = m_mru_file_history.begin(); it != m_mru_file_history.end(); ++it) {
119  config[RECENT_FILES_CONFIG_KEY].push_back(it->c_str());
120  }
121  std::ofstream fout (get_config_filename().c_str());
122  fout << config;
123 }
124 
130  std::ostringstream os;
131  os << Glib::get_user_config_dir() << CONFIG_FILENAME;
132  return os.str();
133 }
134 
135 
141 void YamlConfig::add_recent_filename(Glib::ustring filename) {
142  // Remove any existing entry with the same filename
143  for (auto it = m_mru_file_history.begin(); it != m_mru_file_history.end(); ) {
144  if (*it == filename) {
145  it = m_mru_file_history.erase(it);
146  } else {
147  ++it;
148  }
149  }
150  m_mru_file_history.push_front(filename);
151  while (m_mru_file_history.size() > m_max_recent_files) {
152  m_mru_file_history.pop_back();
153  }
154 };
155 
156 #endif // GCONF
Glib::ustring m_debug_log_filename
The name of the log file to write debug information to.
Definition: yamlconfig.hpp:72
Glib::ustring get_config_filename()
Definition: yamlconfig.cpp:129
Glib::ustring m_current_filename
The current filename.
Definition: yamlconfig.hpp:65
void add_recent_filename(Glib::ustring filename)
Adds a filename to the list of recently used filenames.
Definition: yamlconfig.cpp:141
std::deque< Glib::ustring > m_mru_file_history
The history list of most recently used filenames.
Definition: yamlconfig.hpp:80
gint m_max_recent_files
The list of recently used files for the recent files menu.
Definition: yamlconfig.hpp:59
const std::string DEBUG_LOG_FILENAME_CONFIG_KEY
GConf entry for the page title.
Definition: yamlconfig.cpp:49
const std::string DEFAULT_PAGE_TITLE
The default page title for printing.
Definition: yamlconfig.hpp:32
const std::string CONFIG_FILENAME
The filename used to store the application&#39;s configuration.
Definition: yamlconfig.cpp:34
const std::string CURRENT_FILENAME_CONFIG_KEY
The key used to store the value of the current filename in the configuration file.
Definition: yamlconfig.cpp:40
const std::string MAX_RECENT_FILES_CONFIG_KEY
The key for the maximum number of files to maintain in the recent files menu.
Definition: yamlconfig.cpp:46
const std::string RECENT_FILES_CONFIG_KEY
The key used to store the value of the current filename in the configuration file.
Definition: yamlconfig.cpp:43
const std::string PAGE_TITLE_CONFIG_KEY
The key used to store the value of the page title in the configuration file.
Definition: yamlconfig.cpp:37
const gint DEFAULT_MAX_RECENT_FILES
The maximum number of recent files to maintain.
Definition: yamlconfig.hpp:35
void load()
Loads the configuration file.
Definition: yamlconfig.cpp:60
Glib::ustring m_page_title
The title to use when printing a page or creating a PDF.
Definition: yamlconfig.hpp:75
void save()
Saves the current configuration.
Definition: yamlconfig.cpp:104

Copyright 2008-2021 Frank Dean