Updated ChangeLog and whitespace
[barry.git] / src / configfile.h
blob6ef22144014ad37e62c7394a6d9e82ce8e9c97b3
1 ///
2 /// \file configfile.h
3 /// Barry configuraion class, for one device PIN
4 ///
6 /*
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__
25 #include "dll.h"
26 #include "record.h"
27 #include "pin.h"
28 #include <string>
30 namespace Barry {
32 class BXEXPORT ConfigFile
34 public:
35 class BXEXPORT DBListType : public std::vector<std::string>
37 public:
38 bool IsSelected(const std::string &dbname) const;
41 private:
42 // meta data
43 Barry::Pin m_pin;
44 std::string m_path; // /path/to/config/dir without trailing slash
45 std::string m_filename; // /path/to/config/dir/filename
46 bool m_loaded;
47 std::string m_last_error;
49 // configuration data
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
55 // backup file with
57 protected:
58 void BuildFilename();
59 void BuildDefaultPath();
60 void Clear();
61 void Load();
63 public:
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);
76 ~ConfigFile();
79 // data access
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; }
98 // operations
101 /// Saves current device's config, overwriting or creating a
102 /// config file
103 bool Save();
105 /// Compares a given databasedatabase from a real device with the
106 /// current config. If not yet configured, initialize with valid
107 /// defaults.
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);
123 // Utility functions
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
133 private:
134 typedef std::map<std::string, std::string> keymap_type;
136 // meta data
137 std::string m_path; // /path/to/.barry without trailing slash
138 std::string m_filename; // /path/to/.barry/config
139 bool m_loaded;
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;
147 // set/get key data
148 keymap_type m_keymap;
150 protected:
151 void BuildFilename();
152 void Clear();
153 void Load();
155 public:
156 /// Loads the global config file.
157 /// Throws ConfigFileError on error, but it is not an error
158 /// if the config does not exist.
159 GlobalConfigFile();
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);
167 ~GlobalConfigFile();
170 // data access
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; }
183 // operations
186 /// Save the current global config, overwriting or creating as needed
187 bool Save();
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);
200 } // namespace Barry
202 #endif