00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "CConfig.h"
00016 #include "LaunchUtil.h"
00017 #include "CMSWindowsUtil.h"
00018 #include "CArch.h"
00019 #include "resource.h"
00020 #include "stdfstream.h"
00021
00022 size_t s_showingDialog = 0;
00023
00024 CString
00025 getString(DWORD id)
00026 {
00027 return CMSWindowsUtil::getString(s_instance, id);
00028 }
00029
00030 CString
00031 getErrorString(DWORD error)
00032 {
00033 return CMSWindowsUtil::getErrorString(s_instance, error, IDS_ERROR);
00034 }
00035
00036 void
00037 showError(HWND hwnd, const CString& msg)
00038 {
00039 CString title = getString(IDS_ERROR);
00040 ++s_showingDialog;
00041 MessageBox(hwnd, msg.c_str(), title.c_str(), MB_OK | MB_APPLMODAL);
00042 --s_showingDialog;
00043 }
00044
00045 void
00046 askOkay(HWND hwnd, const CString& title, const CString& msg)
00047 {
00048 ++s_showingDialog;
00049 MessageBox(hwnd, msg.c_str(), title.c_str(), MB_OK | MB_APPLMODAL);
00050 --s_showingDialog;
00051 }
00052
00053 bool
00054 askVerify(HWND hwnd, const CString& msg)
00055 {
00056 CString title = getString(IDS_VERIFY);
00057 ++s_showingDialog;
00058 int result = MessageBox(hwnd, msg.c_str(),
00059 title.c_str(), MB_OKCANCEL | MB_APPLMODAL);
00060 --s_showingDialog;
00061 return (result == IDOK);
00062 }
00063
00064 bool
00065 isShowingDialog()
00066 {
00067 return (s_showingDialog != 0);
00068 }
00069
00070 void
00071 setWindowText(HWND hwnd, const CString& msg)
00072 {
00073 SendMessage(hwnd, WM_SETTEXT, 0, (LPARAM)msg.c_str());
00074 }
00075
00076 CString
00077 getWindowText(HWND hwnd)
00078 {
00079 LRESULT size = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0);
00080 char* buffer = new char[size + 1];
00081 SendMessage(hwnd, WM_GETTEXT, size + 1, (LPARAM)buffer);
00082 buffer[size] = '\0';
00083 CString result(buffer);
00084 delete[] buffer;
00085 return result;
00086 }
00087
00088 HWND
00089 getItem(HWND hwnd, int id)
00090 {
00091 return GetDlgItem(hwnd, id);
00092 }
00093
00094 void
00095 enableItem(HWND hwnd, int id, bool enabled)
00096 {
00097 EnableWindow(GetDlgItem(hwnd, id), enabled);
00098 }
00099
00100 void
00101 setItemChecked(HWND hwnd, bool checked)
00102 {
00103 SendMessage(hwnd, BM_SETCHECK, checked ? BST_CHECKED : BST_UNCHECKED, 0);
00104 }
00105
00106 bool
00107 isItemChecked(HWND hwnd)
00108 {
00109 return (SendMessage(hwnd, BM_GETCHECK, 0, 0) == BST_CHECKED);
00110 }
00111
00112 CString
00113 getAppPath(const CString& appName)
00114 {
00115
00116 char myPathname[MAX_PATH];
00117 GetModuleFileName(s_instance, myPathname, MAX_PATH);
00118 const char* myBasename = ARCH->getBasename(myPathname);
00119 CString appPath = CString(myPathname, myBasename - myPathname);
00120 appPath += appName;
00121 return appPath;
00122 }
00123
00124 static
00125 void
00126 getFileTime(const CString& path, time_t& t)
00127 {
00128 struct _stat s;
00129 if (_stat(path.c_str(), &s) != -1) {
00130 t = s.st_mtime;
00131 }
00132 }
00133
00134 bool
00135 isConfigNewer(time_t& oldTime, bool userConfig)
00136 {
00137 time_t newTime = oldTime;
00138 if (userConfig) {
00139 CString path = ARCH->getUserDirectory();
00140 if (!path.empty()) {
00141 path = ARCH->concatPath(path, CONFIG_NAME);
00142 getFileTime(path, newTime);
00143 }
00144 }
00145 else {
00146 CString path = ARCH->getSystemDirectory();
00147 if (!path.empty()) {
00148 path = ARCH->concatPath(path, CONFIG_NAME);
00149 getFileTime(path, newTime);
00150 }
00151 }
00152 bool result = (newTime > oldTime);
00153 oldTime = newTime;
00154 return result;
00155 }
00156
00157 static
00158 bool
00159 loadConfig(const CString& pathname, CConfig& config)
00160 {
00161 try {
00162 std::ifstream stream(pathname.c_str());
00163 if (stream) {
00164 stream >> config;
00165 return true;
00166 }
00167 }
00168 catch (...) {
00169
00170 }
00171 return false;
00172 }
00173
00174 bool
00175 loadConfig(CConfig& config, time_t& t, bool& userConfig)
00176 {
00177
00178 bool configLoaded = false;
00179 CString path = ARCH->getUserDirectory();
00180 if (!path.empty()) {
00181
00182 path = ARCH->concatPath(path, CONFIG_NAME);
00183 if (loadConfig(path, config)) {
00184 configLoaded = true;
00185 userConfig = true;
00186 getFileTime(path, t);
00187 }
00188 else {
00189
00190 path = ARCH->getSystemDirectory();
00191 if (!path.empty()) {
00192 path = ARCH->concatPath(path, CONFIG_NAME);
00193 if (loadConfig(path, config)) {
00194 configLoaded = true;
00195 userConfig = false;
00196 getFileTime(path, t);
00197 }
00198 }
00199 }
00200 }
00201 return configLoaded;
00202 }
00203
00204 static
00205 bool
00206 saveConfig(const CString& pathname, const CConfig& config)
00207 {
00208 try {
00209 std::ofstream stream(pathname.c_str());
00210 if (stream) {
00211 stream << config;
00212 return !!stream;
00213 }
00214 }
00215 catch (...) {
00216
00217 }
00218 return false;
00219 }
00220
00221 bool
00222 saveConfig(const CConfig& config, bool sysOnly, time_t& t)
00223 {
00224
00225 if (!sysOnly) {
00226 CString path = ARCH->getUserDirectory();
00227 if (!path.empty()) {
00228 path = ARCH->concatPath(path, CONFIG_NAME);
00229 if (saveConfig(path, config)) {
00230 getFileTime(path, t);
00231 return true;
00232 }
00233 }
00234 }
00235
00236
00237 else {
00238 CString path = ARCH->getSystemDirectory();
00239 if (!path.empty()) {
00240 path = ARCH->concatPath(path, CONFIG_NAME);
00241 if (saveConfig(path, config)) {
00242 getFileTime(path, t);
00243 return true;
00244 }
00245 }
00246 }
00247
00248 return false;
00249 }
00250
00251 const TCHAR* const*
00252 getSettingsPath()
00253 {
00254 static const TCHAR* s_keyNames[] = {
00255 TEXT("Software"),
00256 TEXT("Synergy"),
00257 TEXT("Synergy"),
00258 NULL
00259 };
00260 return s_keyNames;
00261 }