3 /// Barry configuraion class, for one device PIN
7 Copyright (C) 2007-2010, 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__
32 class BXEXPORT ConfigFile
35 class BXEXPORT DBListType
: public std::vector
<std::string
>
38 bool IsSelected(const std::string
&dbname
) const;
44 std::string m_path
; // /path/to/config/dir without trailing slash
45 std::string m_filename
; // /path/to/config/dir/filename
47 std::string m_last_error
;
50 DBListType m_backupList
;
51 DBListType m_restoreList
;
52 std::string m_deviceName
;
53 bool m_promptBackupLabel
; // if true, prompt the user on every
54 // backup for a string to label the
59 void BuildDefaultPath();
64 /// Loads config file for the given pin, and ends up in an
65 /// unenlightened state. Throws ConfigFileError on error,
66 /// but it is not an error if the config does not exist.
67 /// Never use this if you have a DatabaseDatabase object!
68 /// This ctor is only for temporary loading of config data.
69 explicit ConfigFile(Barry::Pin pin
);
71 /// Opens and loads config file for given pin, and calls Enlighten
72 /// Throws ConfigFileError on error. Should never fail unless
73 /// passed a bad pin, or if unable to get current user info.
74 ConfigFile(Barry::Pin pin
, const Barry::DatabaseDatabase
&db
);
82 const std::string
& get_last_error() const { return m_last_error
; }
83 bool IsConfigLoaded() const { return m_loaded
; }
85 const std::string
& GetPath() const { return m_path
; }
86 // const std::string& GetDeviceConfigFilename() const { return m_device_filename; }
89 // per-Device Configuration
91 const DBListType
& GetBackupList() const { return m_backupList
; }
92 const DBListType
& GetRestoreList() const { return m_restoreList
; }
93 const std::string
& GetDeviceName() const { return m_deviceName
; }
94 bool HasDeviceName() const { return m_deviceName
.size(); }
95 bool PromptBackupLabel() const { return m_promptBackupLabel
; }
101 /// Saves current device's config, overwriting or creating a
105 /// Compares a given databasedatabase from a real device with the
106 /// current config. If not yet configured, initialize with valid
108 void Enlighten(const Barry::DatabaseDatabase
&db
);
111 // per-Device Configuration setting
114 /// Sets list with new config
115 void SetBackupList(const DBListType
&list
);
116 void SetRestoreList(const DBListType
&list
);
118 void SetDeviceName(const std::string
&name
);
119 void SetBackupPath(const std::string
&path
);
120 void SetPromptBackupLabel(bool prompt
= true);
126 /// Checks that the path in path exists, and if not, creates it.
127 /// Returns false if unable to create path, true if ok.
128 static bool CheckPath(const std::string
&path
, std::string
*perr
= 0);
131 class BXEXPORT GlobalConfigFile
134 typedef std::map
<std::string
, std::string
> keymap_type
;
137 std::string m_path
; // /path/to/.barry without trailing slash
138 std::string m_filename
; // /path/to/.barry/config
140 std::string m_last_error
;
141 std::string m_appname
;
143 // global configuration data
144 Barry::Pin m_lastDevice
; // the last selected device in a GUI
145 bool m_verboseLogging
;
148 keymap_type m_keymap
;
151 void BuildFilename();
156 /// Loads the global config file.
157 /// Throws ConfigFileError on error, but it is not an error
158 /// if the config does not exist.
161 /// Loads the global config file, as well as any application-specific
162 /// keywords and variables. Use this if you wish to make use of
163 /// SetKey() and GetKey(), otherwise these functions will throw
164 /// and exception since they don't have an application name.
165 GlobalConfigFile(const std::string
&appname
);
173 const std::string
& get_last_error() const { return m_last_error
; }
174 bool IsConfigLoaded() const { return m_loaded
; }
177 // Global Configuration data access
179 Barry::Pin
GetLastDevice() const { return m_lastDevice
; }
180 bool VerboseLogging() const { return m_verboseLogging
; }
186 /// Save the current global config, overwriting or creating as needed
189 /// Throws std::logic_error if not constructed with an appname
190 void SetKey(const std::string
&key
, const std::string
&value
);
191 std::string
GetKey(const std::string
&key
) const;
194 // Global Configuration setting
196 void SetLastDevice(const Barry::Pin
&pin
);
197 void SetVerboseLogging(bool verbose
= true);