configfile.h

Go to the documentation of this file.
00001 ///
00002 /// \file       configfile.h
00003 ///             Barry configuraion class, for one device PIN
00004 ///
00005 
00006 /*
00007     Copyright (C) 2007-2012, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #ifndef __BARRY_CONFIGFILE_H__
00023 #define __BARRY_CONFIGFILE_H__
00024 
00025 #include "dll.h"
00026 #include "record.h"
00027 #include "pin.h"
00028 #include <string>
00029 #include <iosfwd>
00030 
00031 namespace Barry {
00032 
00033 /// Creates a tar.gz filename using PIN + date + time + label.
00034 /// Does not include any path, just returns a new filename.
00035 BXEXPORT std::string MakeBackupFilename(const Barry::Pin &pin,
00036                                         const std::string &label);
00037 
00038 class BXEXPORT ConfigFile
00039 {
00040 public:
00041         class BXEXPORT DBListType : public std::vector<std::string>
00042         {
00043         public:
00044                 bool IsSelected(const std::string &dbname) const;
00045 
00046                 DBListType& operator=(const DatabaseDatabase &dbdb);
00047 
00048                 DBListType() {}
00049                 DBListType(const DatabaseDatabase &dbdb)
00050                 {
00051                         operator=(dbdb);
00052                 }
00053         };
00054 
00055 private:
00056         // meta data
00057         Barry::Pin m_pin;
00058         std::string m_path;             // /path/to/config/dir  without trailing slash
00059         std::string m_filename;         // /path/to/config/dir/filename
00060         bool m_loaded;
00061         std::string m_last_error;
00062 
00063         // configuration data
00064         DBListType m_backupList;
00065         DBListType m_restoreList;
00066         std::string m_deviceName;
00067         bool m_promptBackupLabel;       // if true, prompt the user on every
00068                                         // backup for a string to label the
00069                                         // backup file with
00070         bool m_autoSelectAll;           // if true, automatically select all
00071                                         // databases on every backup
00072 
00073 protected:
00074         void BuildFilename();
00075         void BuildDefaultPath();
00076         void Clear();
00077         void Load();
00078 
00079 public:
00080         /// Loads config file for the given pin, and ends up in an
00081         /// unenlightened state.  Throws ConfigFileError on error,
00082         /// but it is not an error if the config does not exist.
00083         /// Never use this if you have a DatabaseDatabase object!
00084         /// This ctor is only for temporary loading of config data.
00085         explicit ConfigFile(Barry::Pin pin);
00086 
00087         /// Opens and loads config file for given pin, and calls Enlighten
00088         /// Throws ConfigFileError on error.  Should never fail unless
00089         /// passed a bad pin, or if unable to get current user info.
00090         ConfigFile(Barry::Pin pin, const Barry::DatabaseDatabase &db);
00091 
00092         ~ConfigFile();
00093 
00094         //
00095         // data access
00096         //
00097 
00098         const std::string& get_last_error() const { return m_last_error; }
00099         bool IsConfigLoaded() const { return m_loaded; }
00100 
00101         const std::string& GetPath() const { return m_path; }
00102 //      const std::string& GetDeviceConfigFilename() const { return m_device_filename; }
00103 
00104         //
00105         // per-Device Configuration
00106         //
00107         const DBListType& GetBackupList() const { return m_backupList; }
00108         const DBListType& GetRestoreList() const { return m_restoreList; }
00109         const std::string& GetDeviceName() const { return m_deviceName; }
00110         bool HasDeviceName() const { return m_deviceName.size(); }
00111         bool PromptBackupLabel() const { return m_promptBackupLabel; }
00112         bool AutoSelectAll() const { return m_autoSelectAll; }
00113 
00114         //
00115         // operations
00116         //
00117 
00118         /// Saves current device's config, overwriting or creating a
00119         /// config file
00120         bool Save();
00121 
00122         /// Compares a given databasedatabase from a real device with the
00123         /// current config.  If not yet configured, initialize with valid
00124         /// defaults.
00125         void Enlighten(const Barry::DatabaseDatabase &db);
00126 
00127         //
00128         // per-Device Configuration setting
00129         //
00130 
00131         /// Sets list with new config
00132         void SetBackupList(const DBListType &list);
00133         void SetRestoreList(const DBListType &list);
00134 
00135         void SetDeviceName(const std::string &name);
00136         void SetBackupPath(const std::string &path);
00137         void SetPromptBackupLabel(bool prompt = true);
00138         void SetAutoSelectAll(bool asa = true);
00139 
00140         //
00141         // Utility functions
00142         //
00143 
00144         /// Checks that the path in path exists, and if not, creates it.
00145         /// Returns false if unable to create path, true if ok.
00146         static bool CheckPath(const std::string &path, std::string *perr = 0);
00147 };
00148 
00149 class BXEXPORT GlobalConfigFile
00150 {
00151 private:
00152         typedef std::map<std::string, std::string>              keymap_type;
00153 
00154         // meta data
00155         std::string m_path;     // /path/to/.barry   without trailing slash
00156         std::string m_filename; // /path/to/.barry/config
00157         bool m_loaded;
00158         std::string m_last_error;
00159         std::string m_appname;
00160 
00161         // global configuration data
00162         Barry::Pin m_lastDevice;        // the last selected device in a GUI
00163         bool m_verboseLogging;
00164 
00165         // set/get key data
00166         keymap_type m_keymap;
00167 
00168 protected:
00169         void BuildFilename();
00170         void Clear();
00171         void Load();
00172 
00173 public:
00174         /// Loads the global config file.
00175         /// Throws ConfigFileError on error, but it is not an error
00176         /// if the config does not exist.
00177         GlobalConfigFile();
00178 
00179         /// Loads the global config file, as well as any application-specific
00180         /// keywords and variables.  Use this if you wish to make use of
00181         /// SetKey() and GetKey(), otherwise these functions will throw
00182         /// and exception since they don't have an application name.
00183         GlobalConfigFile(const std::string &appname);
00184 
00185         ~GlobalConfigFile();
00186 
00187         //
00188         // data access
00189         //
00190 
00191         const std::string& get_last_error() const { return m_last_error; }
00192         bool IsConfigLoaded() const { return m_loaded; }
00193         const std::string& GetPath() const { return m_path; }
00194 
00195         //
00196         // Global Configuration data access
00197         //
00198         Barry::Pin GetLastDevice() const { return m_lastDevice; }
00199         bool VerboseLogging() const { return m_verboseLogging; }
00200 
00201         //
00202         // operations
00203         //
00204 
00205         /// Save the current global config, overwriting or creating as needed
00206         bool Save();
00207 
00208         /// Throws std::logic_error if not constructed with an appname
00209         void SetKey(const std::string &key, const std::string &value);
00210 
00211         /// Returns value for key, and returns default_value if key does not
00212         /// exist in the config file.
00213         /// Throws std::logic_error if not constructed with an appname
00214         std::string GetKey(const std::string &key,
00215                 const std::string &default_value = "") const;
00216 
00217         //
00218         // Global Configuration setting
00219         //
00220         void SetLastDevice(const Barry::Pin &pin);
00221         void SetVerboseLogging(bool verbose = true);
00222 };
00223 
00224 BXEXPORT std::ostream& operator<< (std::ostream &os, const ConfigFile::DBListType &list);
00225 
00226 } // namespace Barry
00227 
00228 #endif
00229