1 /**********************************************************************
2 
3   Audacity: A Digital Audio Editor
4 
5   FileConfig.h
6 
7   Leland Lucius
8 
9 **********************************************************************/
10 
11 #ifndef __AUDACITY_WIDGETS_FILECONFIG__
12 #define __AUDACITY_WIDGETS_FILECONFIG__
13 
14 #include <memory>
15 
16 #include <wx/defs.h>
17 #include <wx/fileconf.h>
18 #include "Identifier.h"
19 
20 class PREFERENCES_API FileConfig : public wxConfigBase
21 {
22 public:
23    FileConfig(const wxString& appName = wxEmptyString,
24               const wxString& vendorName = wxEmptyString,
25               const wxString& localFilename = wxEmptyString,
26               const wxString& globalFilename = wxEmptyString,
27               long style = wxCONFIG_USE_LOCAL_FILE | wxCONFIG_USE_GLOBAL_FILE,
28               const wxMBConv& conv = wxConvAuto());
29    void Init();
30    virtual ~FileConfig();
31 
32    virtual void SetPath(const wxString& strPath) wxOVERRIDE;
33    virtual const wxString& GetPath() const wxOVERRIDE;
34    virtual bool GetFirstGroup(wxString& str, long& lIndex) const wxOVERRIDE;
35    virtual bool GetNextGroup(wxString& str, long& lIndex) const wxOVERRIDE;
36    virtual bool GetFirstEntry(wxString& str, long& lIndex) const wxOVERRIDE;
37    virtual bool GetNextEntry(wxString& str, long& lIndex) const wxOVERRIDE;
38    virtual size_t GetNumberOfEntries(bool bRecursive = false) const wxOVERRIDE;
39    virtual size_t GetNumberOfGroups(bool bRecursive = false) const wxOVERRIDE;
40    virtual bool HasGroup(const wxString& strName) const wxOVERRIDE;
41    virtual bool HasEntry(const wxString& strName) const wxOVERRIDE;
42    virtual bool Flush(bool bCurrentOnly = false) wxOVERRIDE;
43    virtual bool RenameEntry(const wxString& oldName, const wxString& newName) wxOVERRIDE;
44    virtual bool RenameGroup(const wxString& oldName, const wxString& newName) wxOVERRIDE;
45    virtual bool DeleteEntry(const wxString& key, bool bDeleteGroupIfEmpty = true) wxOVERRIDE;
46    virtual bool DeleteGroup(const wxString& key) wxOVERRIDE;
47    virtual bool DeleteAll() wxOVERRIDE;
48 
49    // Set and Get values of the version major/minor/micro keys in audacity.cfg when Audacity first opens
SetVersionKeysInit(int major,int minor,int micro)50    void SetVersionKeysInit( int major, int minor, int micro)
51    {
52       mVersionMajorKeyInit = major;
53       mVersionMinorKeyInit = minor;
54       mVersionMicroKeyInit = micro;
55    }
GetVersionKeysInit(int & major,int & minor,int & micro)56    void GetVersionKeysInit( int& major, int& minor, int& micro) const
57    {
58       major = mVersionMajorKeyInit;
59       minor = mVersionMinorKeyInit;
60       micro = mVersionMicroKeyInit;
61    }
62 
63 protected:
64    virtual bool DoReadString(const wxString& key, wxString *pStr) const wxOVERRIDE;
65    virtual bool DoReadLong(const wxString& key, long *pl) const wxOVERRIDE;
66 #if wxUSE_BASE64
67    virtual bool DoReadBinary(const wxString& key, wxMemoryBuffer* buf) const wxOVERRIDE;
68 #endif // wxUSE_BASE64
69 
70    virtual bool DoWriteString(const wxString& key, const wxString& szValue) wxOVERRIDE;
71    virtual bool DoWriteLong(const wxString& key, long lValue) wxOVERRIDE;
72 #if wxUSE_BASE64
73    virtual bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf) wxOVERRIDE;
74 #endif // wxUSE_BASE64
75 
76 protected:
77    //! Override to notify the user of error conditions involving writability of config files
78    virtual void Warn() = 0;
79 
GetFilePath()80    const FilePath &GetFilePath() const { return mLocalFilename; }
81 
82 private:
83    const wxString mAppName;
84    const wxString mVendorName;
85    const wxString mLocalFilename;
86    const wxString mGlobalFilename;
87    const long mStyle;
88    const wxMBConv & mConv;
89 
90    std::unique_ptr<wxFileConfig> mConfig;
91 
92    // values of the version major/minor/micro keys in audacity.cfg
93    // when Audacity first opens
94    int mVersionMajorKeyInit{};
95    int mVersionMinorKeyInit{};
96    int mVersionMicroKeyInit{};
97 
98    bool mDirty;
99 };
100 
101 #endif
102 
103