File: ProfileManager.h

package info (click to toggle)
freespace2-launcher-wxlauncher 0.11.0%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 2,372 kB
  • ctags: 1,443
  • sloc: cpp: 13,446; python: 797; makefile: 13; sh: 12
file content (199 lines) | stat: -rw-r--r-- 7,422 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
Copyright (C) 2009-2010 wxLauncher Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#ifndef PROFILEMANAGER_H
#define PROFILEMANAGER_H

#include <wx/wx.h>
#include <wx/event.h>
#include <wx/fileconf.h>
#include <wx/filename.h>

#include "apis/EventHandlers.h"

WX_DECLARE_STRING_HASH_MAP( wxFileConfig*, ProfileMap );

/** event is generated anytime the number of profiles in the manager change. */
LAUNCHER_DECLARE_EVENT_TYPE(EVT_PROFILE_CHANGE);
/** Event is generated anytime the currently selected profile is changed. */
LAUNCHER_DECLARE_EVENT_TYPE(EVT_CURRENT_PROFILE_CHANGED);

/** Stores data about downloaded news. */
struct NewsData {
	NewsData() { } // required for wxHashMap, unfortunately
	NewsData(const wxString& theNews, const wxDateTime& lastDownloadNews);
	bool IsValid() const { return (!theNews.IsEmpty()) && lastDownloadNews.IsValid(); }
	wxString theNews;
	wxDateTime lastDownloadNews;
};

/** Maps a news source by name to the locally stored data on it. */
WX_DECLARE_STRING_HASH_MAP(NewsData, NewsMap);

class ProMan {
public:
	enum Flags
	{
		None = 0,
		NoUpdateLastProfile = 1 << 0,
		ProManFlagsMax
	};
	static bool Initialize(Flags flags = None);
	static bool DeInitialize();
	static ProMan* GetProfileManager();

	virtual ~ProMan();
	wxArrayString GetAllProfileNames();
	wxString GetCurrentName();

	bool GlobalExists(const wxChar* strName) const;
	bool GlobalExists(wxString& strName) const;
	
	bool GlobalRead(const wxString& key, bool* b) const;
	bool GlobalRead(const wxString& key, bool* b, bool defaultVal, bool writeBackIfAbsent = false);
	bool GlobalRead(const wxString& key, wxString* str) const;
	bool GlobalRead(const wxString& key, wxString* str, const wxString& defaultVal, bool writeBackIfAbsent = false);
	bool GlobalRead(const wxString& key, long* l, long defaultVal, bool writeBackIfAbsent = false);
	
	bool GlobalWrite(const wxString& key, const wxString& value);
	bool GlobalWrite(const wxString& key, const wxChar* value);
	bool GlobalWrite(const wxString& key, long value);
	bool GlobalWrite(const wxString& key, bool value);
	
	bool ProfileExists(const wxChar* strName) const;
	bool ProfileExists(const wxString& strName) const;
	
	bool ProfileRead(const wxString& key, bool* b) const;
	bool ProfileRead(const wxString& key, bool* b, bool defaultVal, bool writeBackIfAbsent = false);
	bool ProfileRead(const wxString& key, wxString* str) const;
	bool ProfileRead(const wxString& key, wxString* str, const wxString& defaultVal, bool writeBackIfAbsent = false);
	bool ProfileRead(const wxString& key, long* l) const;
	bool ProfileRead(const wxString& key, long* l, long defaultVal, bool writeBackIfAbsent = false);
	
	bool ProfileWrite(const wxString& key, const wxString& value);
	bool ProfileWrite(const wxString& key, const wxChar* value);
	bool ProfileWrite(const wxString& key, long value);
	bool ProfileWrite(const wxString& key, bool value);
	
	bool ProfileDeleteEntry(const wxString& key, bool bDeleteGroupIfEmpty = true);
	
	/** Returns NULL if not found in global profile (or found but invalid). */
	const NewsData* NewsRead(const wxString& newsSource) const;
	void NewsWrite(const wxString& newsSource, const NewsData& data);
	
	enum SaveDialogContext {
		ON_PROFILE_SWITCH,
		ON_PROFILE_CREATE,
		ON_EXIT
	};

	static const wxString GetSaveDialogCaptionText(SaveDialogContext context,
		const wxString& profileName);
	static const wxString GetSaveDialogMessageText(SaveDialogContext context,
		const wxString& profileName);

	bool CreateProfile(const wxString& newProfileName, const wxString& cloneFromProfileName = wxEmptyString);
	bool CreateProfile(const wxString& newProfileName, const wxFileName& sourceFile);
	bool CreateProfile(const wxString& newProfileName, const wxFileConfig *sourceConfig);
	bool DeleteProfile(wxString name);
	bool DoesProfileExist(wxString name);
	bool SwitchTo(wxString name);
	void SaveCurrentProfile(bool quiet = false);
	void RevertCurrentProfile();
	bool HasUnsavedChanges();
	inline bool NeedToPromptToSave() { return (!this->isAutoSaving) && this->HasUnsavedChanges(); }
	void SetAutoSave(bool value) { this->isAutoSaving = value; }

	void AddEventHandler(wxEvtHandler *handler);
	void RemoveEventHandler(wxEvtHandler *handler);

	enum RegistryCodes {
		InvalidParameters = -1,
		NoError = 0,
		AccessDenied,

		FileIsNotOK,
		InputFileDoesNotExist,
		SupportNotCompiledIn,
		UnknownError,
	};

	RegistryCodes PushCurrentProfile(); //!< push current profile into registry

	static const wxString& DEFAULT_PROFILE_NAME;
private:
	static ProMan* proman;
	static bool isInitialized;
	static Flags flags;
	wxFileConfig* currentProfile;
	wxString currentProfileName;
	
	bool CreateNewProfile(wxString newName);
	static wxString GenerateNewProfileFileName();

	static RegistryCodes PushProfile(wxFileConfig *cfg); //!< push profile into registry
	static RegistryCodes PullProfile(wxFileConfig *cfg); //!< pull profile from registry

	static void CopyConfig(const wxConfigBase& src, wxConfigBase &dest, const bool includeMainGroup = true, const wxString path = _T("/"));
	static void CopyConfigEntry(const wxConfigBase& src, wxConfigBase& dest, const wxString path, const wxString entry);

	static void ClearConfig(wxConfigBase& cfg);
	
	static bool AreConfigsEqual(wxConfigBase& cfg1, wxConfigBase& cfg2);
	static bool IsConfigSubset(wxConfigBase& cfg1, wxConfigBase& cfg2, const wxString path = _T("/"));
	static bool AreEntriesEqual(const wxConfigBase& cfg1, const wxConfigBase& cfg2, const wxString path, const wxString entry);

	static void LogConfigContents(wxConfigBase& cfg, const wxString path = _T("/"), const bool includeWxWindows = false);
	static void TestConfigFunctions(wxConfigBase& src);
	
	ProMan();
	void SaveProfilesBeforeExiting();
	
	NewsMap newsMap;
	void LoadNewsMapFromGlobalProfile();
	void SaveNewsMapToGlobalProfile();

	ProfileMap profiles; //!< The profiles. Indexed by Name;
	wxFileConfig* globalProfile;  //!< Global profile settings, like language, or proxy
	wxString privateCopyFilename; //!< Name of file used for private copy
	wxFileConfig* privateCopy; //!< Private copy, used in determining whether current profile has unsaved changes
	void ResetPrivateCopy();
	bool isAutoSaving; //!< Are we auto saving the profiles?
	void GenerateChangeEvent();
	void GenerateCurrentProfileChangedEvent();

	EventHandlers eventHandlers;
};

// These operators should be refactored into a common
// macro rather than copied for the next enum flag
inline ProMan::Flags operator|(ProMan::Flags a, ProMan::Flags b)
{
	return static_cast<ProMan::Flags>(
		static_cast<int>(a) | static_cast<int>(b)
		);
}

inline ProMan::Flags operator&(ProMan::Flags a, ProMan::Flags b)
{
	return static_cast<ProMan::Flags>(
		static_cast<int>(a) & static_cast<int>(b)
		);
}

#endif