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
);
49 DBListType(const DatabaseDatabase
&dbdb
)
58 std::string m_path
; // /path/to/config/dir without trailing slash
59 std::string m_filename
; // /path/to/config/dir/filename
61 std::string m_last_error
;
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
70 bool m_autoSelectAll
; // if true, automatically select all
71 // databases on every backup
75 void BuildDefaultPath();
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
);
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(); }
111 bool PromptBackupLabel() const { return m_promptBackupLabel
; }
112 bool AutoSelectAll() const { return m_autoSelectAll
; }
118 /// Saves current device's config, overwriting or creating a
122 /// Compares a given databasedatabase from a real device with the
123 /// current config. If not yet configured, initialize with valid
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);
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
152 typedef std::map
<std::string
, std::string
> keymap_type
;
155 std::string m_path
; // /path/to/.barry without trailing slash
156 std::string m_filename
; // /path/to/.barry/config
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
;
166 keymap_type m_keymap
;
169 void BuildFilename();
174 /// Loads the global config file.
175 /// Throws ConfigFileError on error, but it is not an error
176 /// if the config does not exist.
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
);
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
; }
205 /// Save the current global config, overwriting or creating as needed
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
);