3 /// Barry configuraion class, for one device PIN
7 Copyright (C) 2007-2012, 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__
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
41 class BXEXPORT DBListType
: public std::vector
<std::string
>
44 bool IsSelected(const std::string
&dbname
) const;
46 DBListType
& operator=(const DatabaseDatabase
&dbdb
);
52 std::string m_path
; // /path/to/config/dir without trailing slash
53 std::string m_filename
; // /path/to/config/dir/filename
55 std::string m_last_error
;
58 DBListType m_backupList
;
59 DBListType m_restoreList
;
60 std::string m_deviceName
;
61 bool m_promptBackupLabel
; // if true, prompt the user on every
62 // backup for a string to label the
64 bool m_autoSelectAll
; // if true, automatically select all
65 // databases on every backup
69 void BuildDefaultPath();
74 /// Loads config file for the given pin, and ends up in an
75 /// unenlightened state. Throws ConfigFileError on error,
76 /// but it is not an error if the config does not exist.
77 /// Never use this if you have a DatabaseDatabase object!
78 /// This ctor is only for temporary loading of config data.
79 explicit ConfigFile(Barry::Pin pin
);
81 /// Opens and loads config file for given pin, and calls Enlighten
82 /// Throws ConfigFileError on error. Should never fail unless
83 /// passed a bad pin, or if unable to get current user info.
84 ConfigFile(Barry::Pin pin
, const Barry::DatabaseDatabase
&db
);
92 const std::string
& get_last_error() const { return m_last_error
; }
93 bool IsConfigLoaded() const { return m_loaded
; }
95 const std::string
& GetPath() const { return m_path
; }
96 // const std::string& GetDeviceConfigFilename() const { return m_device_filename; }
99 // per-Device Configuration
101 const DBListType
& GetBackupList() const { return m_backupList
; }
102 const DBListType
& GetRestoreList() const { return m_restoreList
; }
103 const std::string
& GetDeviceName() const { return m_deviceName
; }
104 bool HasDeviceName() const { return m_deviceName
.size(); }
105 bool PromptBackupLabel() const { return m_promptBackupLabel
; }
106 bool AutoSelectAll() const { return m_autoSelectAll
; }
112 /// Saves current device's config, overwriting or creating a
116 /// Compares a given databasedatabase from a real device with the
117 /// current config. If not yet configured, initialize with valid
119 void Enlighten(const Barry::DatabaseDatabase
&db
);
122 // per-Device Configuration setting
125 /// Sets list with new config
126 void SetBackupList(const DBListType
&list
);
127 void SetRestoreList(const DBListType
&list
);
129 void SetDeviceName(const std::string
&name
);
130 void SetBackupPath(const std::string
&path
);
131 void SetPromptBackupLabel(bool prompt
= true);
132 void SetAutoSelectAll(bool asa
= true);
138 /// Checks that the path in path exists, and if not, creates it.
139 /// Returns false if unable to create path, true if ok.
140 static bool CheckPath(const std::string
&path
, std::string
*perr
= 0);
143 class BXEXPORT GlobalConfigFile
146 typedef std::map
<std::string
, std::string
> keymap_type
;
149 std::string m_path
; // /path/to/.barry without trailing slash
150 std::string m_filename
; // /path/to/.barry/config
152 std::string m_last_error
;
153 std::string m_appname
;
155 // global configuration data
156 Barry::Pin m_lastDevice
; // the last selected device in a GUI
157 bool m_verboseLogging
;
160 keymap_type m_keymap
;
163 void BuildFilename();
168 /// Loads the global config file.
169 /// Throws ConfigFileError on error, but it is not an error
170 /// if the config does not exist.
173 /// Loads the global config file, as well as any application-specific
174 /// keywords and variables. Use this if you wish to make use of
175 /// SetKey() and GetKey(), otherwise these functions will throw
176 /// and exception since they don't have an application name.
177 GlobalConfigFile(const std::string
&appname
);
185 const std::string
& get_last_error() const { return m_last_error
; }
186 bool IsConfigLoaded() const { return m_loaded
; }
187 const std::string
& GetPath() const { return m_path
; }
190 // Global Configuration data access
192 Barry::Pin
GetLastDevice() const { return m_lastDevice
; }
193 bool VerboseLogging() const { return m_verboseLogging
; }
199 /// Save the current global config, overwriting or creating as needed
202 /// Throws std::logic_error if not constructed with an appname
203 void SetKey(const std::string
&key
, const std::string
&value
);
205 /// Returns value for key, and returns default_value if key does not
206 /// exist in the config file.
207 /// Throws std::logic_error if not constructed with an appname
208 std::string
GetKey(const std::string
&key
,
209 const std::string
&default_value
= "") const;
212 // Global Configuration setting
214 void SetLastDevice(const Barry::Pin
&pin
);
215 void SetVerboseLogging(bool verbose
= true);
218 BXEXPORT
std::ostream
& operator<< (std::ostream
&os
, const ConfigFile::DBListType
&list
);