source: vendor/synergy/1.3.1/cmd/launcher/LaunchUtil.cpp

Last change on this file was 2749, checked in by bird, 19 years ago

synergy v1.3.1 sources (zip).

File size: 5.1 KB
Line 
1/*
2 * synergy -- mouse and keyboard sharing utility
3 * Copyright (C) 2002 Chris Schoeneman
4 *
5 * This package is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * found in the file COPYING that should have accompanied this file.
8 *
9 * This package 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
15#include "CConfig.h"
16#include "LaunchUtil.h"
17#include "CMSWindowsUtil.h"
18#include "CArch.h"
19#include "resource.h"
20#include "stdfstream.h"
21
22size_t s_showingDialog = 0;
23
24CString
25getString(DWORD id)
26{
27 return CMSWindowsUtil::getString(s_instance, id);
28}
29
30CString
31getErrorString(DWORD error)
32{
33 return CMSWindowsUtil::getErrorString(s_instance, error, IDS_ERROR);
34}
35
36void
37showError(HWND hwnd, const CString& msg)
38{
39 CString title = getString(IDS_ERROR);
40 ++s_showingDialog;
41 MessageBox(hwnd, msg.c_str(), title.c_str(), MB_OK | MB_APPLMODAL);
42 --s_showingDialog;
43}
44
45void
46askOkay(HWND hwnd, const CString& title, const CString& msg)
47{
48 ++s_showingDialog;
49 MessageBox(hwnd, msg.c_str(), title.c_str(), MB_OK | MB_APPLMODAL);
50 --s_showingDialog;
51}
52
53bool
54askVerify(HWND hwnd, const CString& msg)
55{
56 CString title = getString(IDS_VERIFY);
57 ++s_showingDialog;
58 int result = MessageBox(hwnd, msg.c_str(),
59 title.c_str(), MB_OKCANCEL | MB_APPLMODAL);
60 --s_showingDialog;
61 return (result == IDOK);
62}
63
64bool
65isShowingDialog()
66{
67 return (s_showingDialog != 0);
68}
69
70void
71setWindowText(HWND hwnd, const CString& msg)
72{
73 SendMessage(hwnd, WM_SETTEXT, 0, (LPARAM)msg.c_str());
74}
75
76CString
77getWindowText(HWND hwnd)
78{
79 LRESULT size = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0);
80 char* buffer = new char[size + 1];
81 SendMessage(hwnd, WM_GETTEXT, size + 1, (LPARAM)buffer);
82 buffer[size] = '\0';
83 CString result(buffer);
84 delete[] buffer;
85 return result;
86}
87
88HWND
89getItem(HWND hwnd, int id)
90{
91 return GetDlgItem(hwnd, id);
92}
93
94void
95enableItem(HWND hwnd, int id, bool enabled)
96{
97 EnableWindow(GetDlgItem(hwnd, id), enabled);
98}
99
100void
101setItemChecked(HWND hwnd, bool checked)
102{
103 SendMessage(hwnd, BM_SETCHECK, checked ? BST_CHECKED : BST_UNCHECKED, 0);
104}
105
106bool
107isItemChecked(HWND hwnd)
108{
109 return (SendMessage(hwnd, BM_GETCHECK, 0, 0) == BST_CHECKED);
110}
111
112CString
113getAppPath(const CString& appName)
114{
115 // prepare path to app
116 char myPathname[MAX_PATH];
117 GetModuleFileName(s_instance, myPathname, MAX_PATH);
118 const char* myBasename = ARCH->getBasename(myPathname);
119 CString appPath = CString(myPathname, myBasename - myPathname);
120 appPath += appName;
121 return appPath;
122}
123
124static
125void
126getFileTime(const CString& path, time_t& t)
127{
128 struct _stat s;
129 if (_stat(path.c_str(), &s) != -1) {
130 t = s.st_mtime;
131 }
132}
133
134bool
135isConfigNewer(time_t& oldTime, bool userConfig)
136{
137 time_t newTime = oldTime;
138 if (userConfig) {
139 CString path = ARCH->getUserDirectory();
140 if (!path.empty()) {
141 path = ARCH->concatPath(path, CONFIG_NAME);
142 getFileTime(path, newTime);
143 }
144 }
145 else {
146 CString path = ARCH->getSystemDirectory();
147 if (!path.empty()) {
148 path = ARCH->concatPath(path, CONFIG_NAME);
149 getFileTime(path, newTime);
150 }
151 }
152 bool result = (newTime > oldTime);
153 oldTime = newTime;
154 return result;
155}
156
157static
158bool
159loadConfig(const CString& pathname, CConfig& config)
160{
161 try {
162 std::ifstream stream(pathname.c_str());
163 if (stream) {
164 stream >> config;
165 return true;
166 }
167 }
168 catch (...) {
169 // ignore
170 }
171 return false;
172}
173
174bool
175loadConfig(CConfig& config, time_t& t, bool& userConfig)
176{
177 // load configuration
178 bool configLoaded = false;
179 CString path = ARCH->getUserDirectory();
180 if (!path.empty()) {
181 // try loading the user's configuration
182 path = ARCH->concatPath(path, CONFIG_NAME);
183 if (loadConfig(path, config)) {
184 configLoaded = true;
185 userConfig = true;
186 getFileTime(path, t);
187 }
188 else {
189 // try the system-wide config file
190 path = ARCH->getSystemDirectory();
191 if (!path.empty()) {
192 path = ARCH->concatPath(path, CONFIG_NAME);
193 if (loadConfig(path, config)) {
194 configLoaded = true;
195 userConfig = false;
196 getFileTime(path, t);
197 }
198 }
199 }
200 }
201 return configLoaded;
202}
203
204static
205bool
206saveConfig(const CString& pathname, const CConfig& config)
207{
208 try {
209 std::ofstream stream(pathname.c_str());
210 if (stream) {
211 stream << config;
212 return !!stream;
213 }
214 }
215 catch (...) {
216 // ignore
217 }
218 return false;
219}
220
221bool
222saveConfig(const CConfig& config, bool sysOnly, time_t& t)
223{
224 // try saving the user's configuration
225 if (!sysOnly) {
226 CString path = ARCH->getUserDirectory();
227 if (!path.empty()) {
228 path = ARCH->concatPath(path, CONFIG_NAME);
229 if (saveConfig(path, config)) {
230 getFileTime(path, t);
231 return true;
232 }
233 }
234 }
235
236 // try the system-wide config file
237 else {
238 CString path = ARCH->getSystemDirectory();
239 if (!path.empty()) {
240 path = ARCH->concatPath(path, CONFIG_NAME);
241 if (saveConfig(path, config)) {
242 getFileTime(path, t);
243 return true;
244 }
245 }
246 }
247
248 return false;
249}
250
251const TCHAR* const*
252getSettingsPath()
253{
254 static const TCHAR* s_keyNames[] = {
255 TEXT("Software"),
256 TEXT("Synergy"),
257 TEXT("Synergy"),
258 NULL
259 };
260 return s_keyNames;
261}
Note: See TracBrowser for help on using the repository browser.