# 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-2011, 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
    00030 namespace Barry {
    00031
    00032 class BXEXPORT ConfigFile
    00033 {
    00034 public:
    00035         class BXEXPORT DBListType : public std::vector<std::string>
    00036         {
    00037         public:
    00038                 bool IsSelected(const std::string &dbname) const;
    00039         };
    00040
    00041 private:
    00042         // meta data
    00043         Barry::Pin m_pin;
    00044         std::string m_path;             // /path/to/config/dir  without trailing slash
    00045         std::string m_filename;         // /path/to/config/dir/filename
    00046         bool m_loaded;
    00047         std::string m_last_error;
    00048
    00049         // configuration data
    00050         DBListType m_backupList;
    00051         DBListType m_restoreList;
    00052         std::string m_deviceName;
    00053         bool m_promptBackupLabel;       // if true, prompt the user on every
    00054                                         // backup for a string to label the
    00055                                         // backup file with
    00056
    00057 protected:
    00058         void BuildFilename();
    00059         void BuildDefaultPath();
    00060         void Clear();
    00061         void Load();
    00062
    00063 public:
    00064         /// Loads config file for the given pin, and ends up in an
    00065         /// unenlightened state.  Throws ConfigFileError on error,
    00066         /// but it is not an error if the config does not exist.
    00067         /// Never use this if you have a DatabaseDatabase object!
    00068         /// This ctor is only for temporary loading of config data.
    00069         explicit ConfigFile(Barry::Pin pin);
    00070
    00071         /// Opens and loads config file for given pin, and calls Enlighten
    00072         /// Throws ConfigFileError on error.  Should never fail unless
    00073         /// passed a bad pin, or if unable to get current user info.
    00074         ConfigFile(Barry::Pin pin, const Barry::DatabaseDatabase &db);
    00075
    00076         ~ConfigFile();
    00077
    00078         //
    00079         // data access
    00080         //
    00081
    00082         const std::string& get_last_error() const { return m_last_error; }
    00083         bool IsConfigLoaded() const { return m_loaded; }
    00084
    00085         const std::string& GetPath() const { return m_path; }
    00086 //      const std::string& GetDeviceConfigFilename() const { return m_device_filename; }
    00087
    00088         //
    00089         // per-Device Configuration
    00090         //
    00091         const DBListType& GetBackupList() const { return m_backupList; }
    00092         const DBListType& GetRestoreList() const { return m_restoreList; }
    00093         const std::string& GetDeviceName() const { return m_deviceName; }
    00094         bool HasDeviceName() const { return m_deviceName.size(); }
    00095         bool PromptBackupLabel() const { return m_promptBackupLabel; }
    00096
    00097         //
    00098         // operations
    00099         //
    00100
    00101         /// Saves current device's config, overwriting or creating a
    00102         /// config file
    00103         bool Save();
    00104
    00105         /// Compares a given databasedatabase from a real device with the
    00106         /// current config.  If not yet configured, initialize with valid
    00107         /// defaults.
    00108         void Enlighten(const Barry::DatabaseDatabase &db);
    00109
    00110         //
    00111         // per-Device Configuration setting
    00112         //
    00113
    00114         /// Sets list with new config
    00115         void SetBackupList(const DBListType &list);
    00116         void SetRestoreList(const DBListType &list);
    00117
    00118         void SetDeviceName(const std::string &name);
    00119         void SetBackupPath(const std::string &path);
    00120         void SetPromptBackupLabel(bool prompt = true);
    00121
    00122         //
    00123         // Utility functions
    00124         //
    00125
    00126         /// Checks that the path in path exists, and if not, creates it.
    00127         /// Returns false if unable to create path, true if ok.
    00128         static bool CheckPath(const std::string &path, std::string *perr = 0);
    00129 };
    00130
    00131 class BXEXPORT GlobalConfigFile
    00132 {
    00133 private:
    00134         typedef std::map<std::string, std::string>              keymap_type;
    00135
    00136         // meta data
    00137         std::string m_path;     // /path/to/.barry   without trailing slash
    00138         std::string m_filename; // /path/to/.barry/config
    00139         bool m_loaded;
    00140         std::string m_last_error;
    00141         std::string m_appname;
    00142
    00143         // global configuration data
    00144         Barry::Pin m_lastDevice;        // the last selected device in a GUI
    00145         bool m_verboseLogging;
    00146
    00147         // set/get key data
    00148         keymap_type m_keymap;
    00149
    00150 protected:
    00151         void BuildFilename();
    00152         void Clear();
    00153         void Load();
    00154
    00155 public:
    00156         /// Loads the global config file.
    00157         /// Throws ConfigFileError on error, but it is not an error
    00158         /// if the config does not exist.
    00159         GlobalConfigFile();
    00160
    00161         /// Loads the global config file, as well as any application-specific
    00162         /// keywords and variables.  Use this if you wish to make use of
    00163         /// SetKey() and GetKey(), otherwise these functions will throw
    00164         /// and exception since they don't have an application name.
    00165         GlobalConfigFile(const std::string &appname);
    00166
    00167         ~GlobalConfigFile();
    00168
    00169         //
    00170         // data access
    00171         //
    00172
    00173         const std::string& get_last_error() const { return m_last_error; }
    00174         bool IsConfigLoaded() const { return m_loaded; }
    00175
    00176         //
    00177         // Global Configuration data access
    00178         //
    00179         Barry::Pin GetLastDevice() const { return m_lastDevice; }
    00180         bool VerboseLogging() const { return m_verboseLogging; }
    00181
    00182         //
    00183         // operations
    00184         //
    00185
    00186         /// Save the current global config, overwriting or creating as needed
    00187         bool Save();
    00188
    00189         /// Throws std::logic_error if not constructed with an appname
    00190         void SetKey(const std::string &key, const std::string &value);
    00191         std::string GetKey(const std::string &key) const;
    00192
    00193         //
    00194         // Global Configuration setting
    00195         //
    00196         void SetLastDevice(const Barry::Pin &pin);
    00197         void SetVerboseLogging(bool verbose = true);
    00198 };
    00199
    00200 } // namespace Barry
    00201
    00202 #endif
    00203