menu: added new Keywords tag to .desktop files
[barry.git] / src / configfile.h
blob70fce39497d5d404f986df5fffd49d89a87de673
1 ///
2 /// \file configfile.h
3 /// Barry configuraion class, for one device PIN
4 ///
6 /*
7 Copyright (C) 2007-2013, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRY_CONFIGFILE_H__
23 #define __BARRY_CONFIGFILE_H__
25 #include "dll.h"
26 #include "record.h"
27 #include "pin.h"
28 #include <string>
29 #include <iosfwd>
31 namespace Barry {
33 /// Creates a tar.gz filename using PIN + date + time + label.
34 /// Does not include any path, just returns a new filename.
35 BXEXPORT std::string MakeBackupFilename(const Barry::Pin &pin,
36 const std::string &label);
38 class BXEXPORT ConfigFile
40 public:
41 class BXEXPORT DBListType : public std::vector<std::string>
43 public:
44 bool IsSelected(const std::string &dbname) const;
46 DBListType& operator=(const DatabaseDatabase &dbdb);
48 DBListType() {}
49 DBListType(const DatabaseDatabase &dbdb)
51 operator=(dbdb);
55 private:
56 // meta data
57 Barry::Pin m_pin;
58 std::string m_path; // /path/to/config/dir without trailing slash
59 std::string m_filename; // /path/to/config/dir/filename
60 bool m_loaded;
61 std::string m_last_error;
63 // configuration data
64 DBListType m_backupList;
65 DBListType m_restoreList;
66 std::string m_deviceName;
67 bool m_promptBackupLabel; // if true, prompt the user on every
68 // backup for a string to label the
69 // backup file with
70 bool m_autoSelectAll; // if true, automatically select all
71 // databases on every backup
73 protected:
74 void BuildFilename();
75 void BuildDefaultPath();
76 void Clear();
77 void Load();
79 public:
80 /// Loads config file for the given pin, and ends up in an
81 /// unenlightened state. Throws ConfigFileError on error,
82 /// but it is not an error if the config does not exist.
83 /// Never use this if you have a DatabaseDatabase object!
84 /// This ctor is only for temporary loading of config data.
85 explicit ConfigFile(Barry::Pin pin);
87 /// Opens and loads config file for given pin, and calls Enlighten
88 /// Throws ConfigFileError on error. Should never fail unless
89 /// passed a bad pin, or if unable to get current user info.
90 ConfigFile(Barry::Pin pin, const Barry::DatabaseDatabase &db);
92 ~ConfigFile();
95 // data access
98 const std::string& get_last_error() const { return m_last_error; }
99 bool IsConfigLoaded() const { return m_loaded; }
101 const std::string& GetPath() const { return m_path; }
102 // const std::string& GetDeviceConfigFilename() const { return m_device_filename; }
105 // per-Device Configuration
107 const DBListType& GetBackupList() const { return m_backupList; }
108 const DBListType& GetRestoreList() const { return m_restoreList; }
109 const std::string& GetDeviceName() const { return m_deviceName; }
110 bool HasDeviceName() const { return m_deviceName.size() != 0; }
111 bool PromptBackupLabel() const { return m_promptBackupLabel; }
112 bool AutoSelectAll() const { return m_autoSelectAll; }
115 // operations
118 /// Saves current device's config, overwriting or creating a
119 /// config file
120 bool Save();
122 /// Compares a given databasedatabase from a real device with the
123 /// current config. If not yet configured, initialize with valid
124 /// defaults.
125 void Enlighten(const Barry::DatabaseDatabase &db);
128 // per-Device Configuration setting
131 /// Sets list with new config
132 void SetBackupList(const DBListType &list);
133 void SetRestoreList(const DBListType &list);
135 void SetDeviceName(const std::string &name);
136 void SetBackupPath(const std::string &path);
137 void SetPromptBackupLabel(bool prompt = true);
138 void SetAutoSelectAll(bool asa = true);
141 // Utility functions
144 /// Checks that the path in path exists, and if not, creates it.
145 /// Returns false if unable to create path, true if ok.
146 static bool CheckPath(const std::string &path, std::string *perr = 0);
149 class BXEXPORT GlobalConfigFile
151 private:
152 typedef std::map<std::string, std::string> keymap_type;
154 // meta data
155 std::string m_path; // /path/to/.barry without trailing slash
156 std::string m_filename; // /path/to/.barry/config
157 bool m_loaded;
158 std::string m_last_error;
159 std::string m_appname;
161 // global configuration data
162 Barry::Pin m_lastDevice; // the last selected device in a GUI
163 bool m_verboseLogging;
165 // set/get key data
166 keymap_type m_keymap;
168 protected:
169 void BuildFilename();
170 void Clear();
171 void Load();
173 public:
174 /// Loads the global config file.
175 /// Throws ConfigFileError on error, but it is not an error
176 /// if the config does not exist.
177 GlobalConfigFile();
179 /// Loads the global config file, as well as any application-specific
180 /// keywords and variables. Use this if you wish to make use of
181 /// SetKey() and GetKey(), otherwise these functions will throw
182 /// and exception since they don't have an application name.
183 GlobalConfigFile(const std::string &appname);
185 ~GlobalConfigFile();
188 // data access
191 const std::string& get_last_error() const { return m_last_error; }
192 bool IsConfigLoaded() const { return m_loaded; }
193 const std::string& GetPath() const { return m_path; }
196 // Global Configuration data access
198 Barry::Pin GetLastDevice() const { return m_lastDevice; }
199 bool VerboseLogging() const { return m_verboseLogging; }
202 // operations
205 /// Save the current global config, overwriting or creating as needed
206 bool Save();
208 /// Throws std::logic_error if not constructed with an appname
209 void SetKey(const std::string &key, const std::string &value);
211 /// Returns value for key, and returns default_value if key does not
212 /// exist in the config file.
213 /// Throws std::logic_error if not constructed with an appname
214 std::string GetKey(const std::string &key,
215 const std::string &default_value = "") const;
218 // Global Configuration setting
220 void SetLastDevice(const Barry::Pin &pin);
221 void SetVerboseLogging(bool verbose = true);
224 BXEXPORT std::ostream& operator<< (std::ostream &os, const ConfigFile::DBListType &list);
226 } // namespace Barry
228 #endif