| 1 |
/* |
| 2 |
Copyright (C) 2009-2010 wxLauncher Team |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License |
| 6 |
as published by the Free Software Foundation; either version 2 |
| 7 |
of the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, write to the Free Software |
| 16 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef PROFILEMANAGER_H |
| 20 |
#define PROFILEMANAGER_H |
| 21 |
|
| 22 |
#include <wx/wx.h> |
| 23 |
#include <wx/event.h> |
| 24 |
#include <wx/fileconf.h> |
| 25 |
#include <wx/filename.h> |
| 26 |
|
| 27 |
#include "apis/EventHandlers.h" |
| 28 |
|
| 29 |
WX_DECLARE_STRING_HASH_MAP( wxFileConfig*, ProfileMap ); |
| 30 |
|
| 31 |
/** event is generated anytime the number of profiles in the manager change. */ |
| 32 |
LAUNCHER_DECLARE_EVENT_TYPE(EVT_PROFILE_CHANGE); |
| 33 |
/** Event is generated anytime the currently selected profile is changed. */ |
| 34 |
LAUNCHER_DECLARE_EVENT_TYPE(EVT_CURRENT_PROFILE_CHANGED); |
| 35 |
|
| 36 |
/** Stores data about downloaded news. */ |
| 37 |
struct NewsData { |
| 38 |
NewsData() { } // required for wxHashMap, unfortunately |
| 39 |
NewsData(const wxString& theNews, const wxDateTime& lastDownloadNews); |
| 40 |
bool IsValid() const { return (!theNews.IsEmpty()) && lastDownloadNews.IsValid(); } |
| 41 |
wxString theNews; |
| 42 |
wxDateTime lastDownloadNews; |
| 43 |
}; |
| 44 |
|
| 45 |
/** Maps a news source by name to the locally stored data on it. */ |
| 46 |
WX_DECLARE_STRING_HASH_MAP(NewsData, NewsMap); |
| 47 |
|
| 48 |
class ProMan { |
| 49 |
public: |
| 50 |
enum Flags |
| 51 |
{ |
| 52 |
None = 0, |
| 53 |
NoUpdateLastProfile = 1 << 0, |
| 54 |
ProManFlagsMax |
| 55 |
}; |
| 56 |
static bool Initialize(Flags flags = None); |
| 57 |
static bool DeInitialize(); |
| 58 |
static bool IsInitialized() { return isInitialized; } |
| 59 |
static ProMan* GetProfileManager(); |
| 60 |
|
| 61 |
virtual ~ProMan(); |
| 62 |
wxArrayString GetAllProfileNames(); |
| 63 |
wxString GetCurrentName(); |
| 64 |
|
| 65 |
bool GlobalExists(const wxChar* strName) const; |
| 66 |
bool GlobalExists(wxString& strName) const; |
| 67 |
|
| 68 |
bool GlobalRead(const wxString& key, bool* b) const; |
| 69 |
bool GlobalRead(const wxString& key, bool* b, bool defaultVal, bool writeBackIfAbsent = false); |
| 70 |
bool GlobalRead(const wxString& key, wxString* str) const; |
| 71 |
bool GlobalRead(const wxString& key, wxString* str, const wxString& defaultVal, bool writeBackIfAbsent = false); |
| 72 |
bool GlobalRead(const wxString& key, long* l, long defaultVal, bool writeBackIfAbsent = false); |
| 73 |
|
| 74 |
bool GlobalWrite(const wxString& key, const wxString& value); |
| 75 |
bool GlobalWrite(const wxString& key, const wxChar* value); |
| 76 |
bool GlobalWrite(const wxString& key, long value); |
| 77 |
bool GlobalWrite(const wxString& key, bool value); |
| 78 |
|
| 79 |
bool ProfileExists(const wxChar* strName) const; |
| 80 |
bool ProfileExists(const wxString& strName) const; |
| 81 |
|
| 82 |
bool ProfileRead(const wxString& key, bool* b) const; |
| 83 |
bool ProfileRead(const wxString& key, bool* b, bool defaultVal, bool writeBackIfAbsent = false); |
| 84 |
bool ProfileRead(const wxString& key, wxString* str) const; |
| 85 |
bool ProfileRead(const wxString& key, wxString* str, const wxString& defaultVal, bool writeBackIfAbsent = false); |
| 86 |
bool ProfileRead(const wxString& key, long* l) const; |
| 87 |
bool ProfileRead(const wxString& key, long* l, long defaultVal, bool writeBackIfAbsent = false); |
| 88 |
|
| 89 |
bool ProfileWrite(const wxString& key, const wxString& value); |
| 90 |
bool ProfileWrite(const wxString& key, const wxChar* value); |
| 91 |
bool ProfileWrite(const wxString& key, long value); |
| 92 |
bool ProfileWrite(const wxString& key, bool value); |
| 93 |
|
| 94 |
bool ProfileDeleteEntry(const wxString& key, bool bDeleteGroupIfEmpty = true); |
| 95 |
|
| 96 |
/** Returns NULL if not found in global profile (or found but invalid). */ |
| 97 |
const NewsData* NewsRead(const wxString& newsSource) const; |
| 98 |
void NewsWrite(const wxString& newsSource, const NewsData& data); |
| 99 |
|
| 100 |
enum SaveDialogContext { |
| 101 |
ON_PROFILE_SWITCH, |
| 102 |
ON_PROFILE_CREATE, |
| 103 |
ON_EXIT |
| 104 |
}; |
| 105 |
|
| 106 |
static const wxString GetSaveDialogCaptionText(SaveDialogContext context, |
| 107 |
const wxString& profileName); |
| 108 |
static const wxString GetSaveDialogMessageText(SaveDialogContext context, |
| 109 |
const wxString& profileName); |
| 110 |
|
| 111 |
bool CreateProfile(const wxString& newProfileName, const wxString& cloneFromProfileName = wxEmptyString); |
| 112 |
bool CreateProfile(const wxString& newProfileName, const wxFileName& sourceFile); |
| 113 |
bool CreateProfile(const wxString& newProfileName, const wxFileConfig *sourceConfig); |
| 114 |
bool DeleteProfile(wxString name); |
| 115 |
bool DoesProfileExist(wxString name); |
| 116 |
bool SwitchTo(wxString name); |
| 117 |
void SaveCurrentProfile(bool quiet = false); |
| 118 |
void RevertCurrentProfile(); |
| 119 |
bool HasUnsavedChanges(); |
| 120 |
inline bool NeedToPromptToSave() { return (!this->isAutoSaving) && this->HasUnsavedChanges(); } |
| 121 |
void SetAutoSave(bool value) { this->isAutoSaving = value; } |
| 122 |
|
| 123 |
void AddEventHandler(wxEvtHandler *handler); |
| 124 |
void RemoveEventHandler(wxEvtHandler *handler); |
| 125 |
|
| 126 |
enum RegistryCodes { |
| 127 |
InvalidParameters = -1, |
| 128 |
NoError = 0, |
| 129 |
AccessDenied, |
| 130 |
|
| 131 |
FileIsNotOK, |
| 132 |
InputFileDoesNotExist, |
| 133 |
SupportNotCompiledIn, |
| 134 |
UnknownError, |
| 135 |
}; |
| 136 |
|
| 137 |
RegistryCodes PushCurrentProfile(); //!< push current profile into registry |
| 138 |
|
| 139 |
static const wxString& DEFAULT_PROFILE_NAME; |
| 140 |
private: |
| 141 |
static ProMan* proman; |
| 142 |
static bool isInitialized; |
| 143 |
static Flags flags; |
| 144 |
wxFileConfig* currentProfile; |
| 145 |
wxString currentProfileName; |
| 146 |
|
| 147 |
bool CreateNewProfile(wxString newName); |
| 148 |
static wxString GenerateNewProfileFileName(); |
| 149 |
|
| 150 |
static RegistryCodes PushProfile(wxFileConfig *cfg); //!< push profile into registry |
| 151 |
static RegistryCodes PullProfile(wxFileConfig *cfg); //!< pull profile from registry |
| 152 |
|
| 153 |
static void CopyConfig(const wxConfigBase& src, wxConfigBase &dest, const bool includeMainGroup = true, const wxString path = _T("/")); |
| 154 |
static void CopyConfigEntry(const wxConfigBase& src, wxConfigBase& dest, const wxString path, const wxString entry); |
| 155 |
|
| 156 |
static void ClearConfig(wxConfigBase& cfg); |
| 157 |
|
| 158 |
static bool AreConfigsEqual(wxConfigBase& cfg1, wxConfigBase& cfg2); |
| 159 |
static bool IsConfigSubset(wxConfigBase& cfg1, wxConfigBase& cfg2, const wxString path = _T("/")); |
| 160 |
static bool AreEntriesEqual(const wxConfigBase& cfg1, const wxConfigBase& cfg2, const wxString path, const wxString entry); |
| 161 |
|
| 162 |
static void LogConfigContents(wxConfigBase& cfg, const wxString path = _T("/"), const bool includeWxWindows = false); |
| 163 |
static void TestConfigFunctions(wxConfigBase& src); |
| 164 |
|
| 165 |
ProMan(); |
| 166 |
void SaveProfilesBeforeExiting(); |
| 167 |
|
| 168 |
NewsMap newsMap; |
| 169 |
void LoadNewsMapFromGlobalProfile(); |
| 170 |
void SaveNewsMapToGlobalProfile(); |
| 171 |
|
| 172 |
ProfileMap profiles; //!< The profiles. Indexed by Name; |
| 173 |
wxFileConfig* globalProfile; //!< Global profile settings, like language, or proxy |
| 174 |
wxString privateCopyFilename; //!< Name of file used for private copy |
| 175 |
wxFileConfig* privateCopy; //!< Private copy, used in determining whether current profile has unsaved changes |
| 176 |
void ResetPrivateCopy(); |
| 177 |
bool isAutoSaving; //!< Are we auto saving the profiles? |
| 178 |
void GenerateChangeEvent(); |
| 179 |
void GenerateCurrentProfileChangedEvent(); |
| 180 |
|
| 181 |
EventHandlers eventHandlers; |
| 182 |
}; |
| 183 |
|
| 184 |
// These operators should be refactored into a common |
| 185 |
// macro rather than copied for the next enum flag |
| 186 |
inline ProMan::Flags operator|(ProMan::Flags a, ProMan::Flags b) |
| 187 |
{ |
| 188 |
return static_cast<ProMan::Flags>( |
| 189 |
static_cast<int>(a) | static_cast<int>(b) |
| 190 |
); |
| 191 |
} |
| 192 |
|
| 193 |
inline ProMan::Flags operator&(ProMan::Flags a, ProMan::Flags b) |
| 194 |
{ |
| 195 |
return static_cast<ProMan::Flags>( |
| 196 |
static_cast<int>(a) & static_cast<int>(b) |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
#endif |