source: trunk/gsinstaller/gsProgman.cpp@ 1767

Last change on this file since 1767 was 1672, checked in by cs025, 24 years ago

Fixed problem with Windows9x; only does administrator behaviour for
installing program groups when running under WindowsNT family.

  • Property svn:keywords set to Author Date Id Revision
File size: 11.8 KB
RevLine 
[1536]1#include "gsProgman.h"
2
3#include <stdio.h>
4
5// include shell for explorer-based OS'es
6#include <shlobj.h>
7// include dde for old (win 3.1/winNT 3.5) OS'es
8#include <ddeml.h>
9
10#include "FilePath.h"
11
12extern HINSTANCE app_instance;
13
14/**
15 * A private function for Dde callbacks; in practice it needs to be here,
16 * but does nothing whatsoever
17 */
[1543]18HDDEDATA CALLBACK DdeCallBack(UINT uType, // transaction type
19 UINT uFmt, // clipboard data format
20 HCONV hconv, // handle to the conversation
21 HSZ hsz1, // handle to a string
22 HSZ hsz2, // handle to a string
23 HDDEDATA hdata, // handle to a global memory object
24 DWORD dwData1, // transaction-specific data
25 DWORD dwData2 // transaction-specific data
26 )
27{
28 return NULL;
[1536]29}
30
31 /**
32 * A private little function to do DDE activity
33 */
34bool DDEShellCommand(DWORD instance, char *command)
[1543]35{
36 HSZ serviceName;
37 HCONV conversation;
38 int commandLength;
39 HDDEDATA ddeData;
40 DWORD result;
41 bool reply = false;
[1536]42
[1543]43 // create DDE string from service request
44 serviceName = DdeCreateStringHandle(instance, "PROGMAN", CP_WINANSI);
45 if (serviceName == NULL)
46 {
47 /*char buffer[20];
[1536]48 sprintf(buffer, "%d %d", DdeGetLastError(instance), instance);
[1543]49 MessageBox(0, buffer, "GSInstall", MB_OK);*/
50 return FALSE;
51 }
[1536]52
53 // strike up a conversation with the DDE service
54 if ((conversation = DdeConnect(instance, serviceName, serviceName, NULL)) != NULL)
[1543]55 {
56 commandLength = lstrlen(command);
[1536]57
[1543]58 // send the message via DDE
59 // MessageBox(0, command, "Executing", MB_OK);
60 ddeData = DdeClientTransaction((LPBYTE) command, commandLength + 1,
61 conversation, NULL, CF_TEXT, XTYP_EXECUTE,
62 1000, &result);
63 // if our reply isn't NULL, it succeeded
64 reply = (ddeData != NULL);
65 if (reply == false)
66 { /*
67 char buffer[20];
68
69 sprintf(buffer, "%d", DdeGetLastError(instance));
70 MessageBox(0, buffer, "DDE Error", MB_OK);*/
71 }
72
73 // disconnect the conversation
74 DdeDisconnect(conversation);
[1536]75 }
76 else
[1543]77 {
78 MessageBox(0, "DDE Connect failed", "GSInstall", MB_OK);
79 }
80
[1536]81 // free DDE string
82 DdeFreeStringHandle(instance, serviceName);
83 return reply;
84}
85
86gsProgramManager::gsProgramManager(installManager &manager)
[1543]87 : installAgent(manager)
88{
89 this->platform = new gsPlatform();
90 this->connected = false;
[1536]91}
92
93void gsProgramManager::logAction(string actionName, string group)
[1543]94{
95 unInstallCommand command(actionName);
96 command.addParameter(group);
97 manager->storeCommand(command);
[1536]98}
99
100void gsProgramManager::logAction(string actionName, string group, string item, string parameter)
[1543]101{
102 unInstallCommand command(actionName);
[1536]103 command.addParameter(group);
[1543]104 command.addParameter(item);
[1536]105 command.addParameter(parameter);
106 manager->storeCommand(command);
107}
108
109bool gsProgramManager::undoAction(string actionName, stringArray &params)
[1543]110{
111 if (actionName == "ProgManCreateGroup")
112 {
113 this->removeProgramGroup(params[0]);
114 return true;
115 }
[1536]116 else if (actionName == "ProgManAddItem")
[1543]117 {
118 this->removeIcon(params[0], params[1]);
119 return true;
120 }
[1536]121 return false;
122}
123
124bool gsProgramManager::connect()
[1543]125{
126 HRESULT response;
127
128 if (this->platform->isExplorerShell())
129 {
130 // Initialise the shell connection
131 response = CoInitialize(NULL);
132 if (response < 0)
133 {
134 return false;
135 }
[1536]136 }
137 else
[1543]138 {
139 FARPROC procPtr;
140
141 procPtr = MakeProcInstance((FARPROC) DdeCallBack, app_instance);
142
143 instance = 0L;
144 if (DdeInitialize(&instance, (PFNCALLBACK) procPtr, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
145 {
146 MessageBox(0, "DDE failed to initialise", "GSInstall", MB_OK);
147 return false;
148 }
[1536]149 }
[1543]150 this->connected = true;
[1536]151 return true;
152}
153
154bool gsProgramManager::disconnect()
[1543]155{
156 if (this->connected)
157 {
158 if (this->platform->isExplorerShell())
159 {
160 CoUninitialize();
161 }
162 else
163 {
164 DdeUninitialize(instance);
165 }
[1536]166 }
[1543]167 this->connected = false;
[1536]168 return true;
169}
170
171bool gsProgramManager::addProgramGroup(string groupName)
[1543]172{
173 //CreateGroup(groupName);
174 char buffer[256];
[1536]175
176 if (this->platform->isExplorerShell())
[1580]177 {
178 // add using shell controls
179 LPITEMIDLIST pidl;
180
[1672]181 if (this->platform->isWindowsNT() && this->platform->isUserAdministrator())
[1580]182 {
[1547]183#if defined (__GNUC__)
184 // this is a hack as windows port of gcc doesn't have CSIDL_COMMON_PROGRAMS defined
[1580]185 if (SHGetSpecialFolderLocation(0, CSIDL_PROGRAMS, &pidl) != NOERROR)
[1547]186#else
[1580]187 if (SHGetSpecialFolderLocation(0, CSIDL_COMMON_PROGRAMS, &pidl) != NOERROR)
[1547]188#endif
[1580]189 {
[1543]190 return false;
191 }
[1580]192 }
193 else
[1672]194 { if (SHGetSpecialFolderLocation(0, CSIDL_PROGRAMS, &pidl) != NOERROR)
[1543]195 {
196 return false;
197 }
[1580]198 }
199 SHGetPathFromIDList(pidl, buffer);
[1536]200
[1580]201 // Create the folder as required
202 FilePath *path = new FilePath(buffer, groupName);
203 if (!CreateDirectory((char *) path->cString(), NULL))
204 {
205 // it probably existed already - if so skip it!
206 DWORD reason = GetLastError();
207 if (reason == ERROR_ALREADY_EXISTS)
208 { return true;
209 }
210 else
211 { return false;
212 }
213 }
214 delete path;
215 }
[1536]216 else
[1543]217 {
218 // add using program manager DDE
219 sprintf(buffer, "[CreateGroup (%s)]", groupName.c_str());
220 if (!DDEShellCommand(this->instance, buffer))
221 {
222 // flag error by returning false
223 return false;
224 }
[1536]225 }
226
227 this->logAction("ProgManCreateGroup", groupName);
[1543]228 return true;
[1536]229}
230
[1543]231bool gsProgramManager::addIcon(string groupName, // group of the icon
232 string iconName, // name of the icon
233 string iconDestination, // destination file of the icon
[1657]234 string arguments, // any arguments to go along with iconDestination
[1543]235 string description) // the textual description
[1657]236 // of the icon (not usually displayed)
[1543]237{
238 bool reply = false;
239 char buffer[MAX_PATH];
240
[1536]241 if (this->platform->isExplorerShell())
[1543]242 {
243 // create item using the shell
244 LPITEMIDLIST pidl;
245 HRESULT hres;
246 IShellLink* psl;
247 FilePath *fullPath;
[1536]248
[1543]249 if (this->platform->isUserAdministrator())
250 {
[1547]251#if defined (__GNUC__)
252 // this is a hack as windows port of gcc doesn't have CSIDL_COMMON_PROGRAMS defined
253 if (SHGetSpecialFolderLocation(0, CSIDL_PROGRAMS, &pidl) != NOERROR)
254#else
[1543]255 if (SHGetSpecialFolderLocation(0, CSIDL_COMMON_PROGRAMS, &pidl) != NOERROR)
[1547]256#endif
257
[1543]258 {
259 return false;
260 }
261 }
262 else
263 {
264 if (SHGetSpecialFolderLocation(0, CSIDL_PROGRAMS, &pidl) != NOERROR)
265 {
266 return false;
267 }
268 }
[1536]269
[1543]270 SHGetPathFromIDList(pidl, buffer);
271 fullPath = new FilePath(4, buffer, groupName.c_str(), iconName.c_str(), "!.lnk");
272
273 // check if the shortcut already exists then don't create this again
274 if (fullPath->exists())
275 {
276 delete fullPath;
277 return true;
278 }
279
280 // Get a pointer to the IShellLink interface.
281 hres = CoCreateInstance(CLSID_ShellLink, NULL,
282 CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) &psl);
283 if (SUCCEEDED(hres))
284 {
285 IPersistFile* ppf;
[1536]286
[1543]287 // Set the path to the shortcut target, and add the
288 // description.
[1536]289 psl->SetPath(iconDestination.c_str());
[1657]290 psl->SetArguments(arguments.c_str());
[1543]291 psl->SetDescription(description.c_str());
292
293 // Query IShellLink for the IPersistFile interface for saving the
[1536]294 // shortcut in persistent storage.
[1543]295 hres = psl->QueryInterface(IID_IPersistFile, (void **) &ppf);
296
297 if (SUCCEEDED(hres))
298 {
299 WCHAR wsz[MAX_PATH];
300
301 // Ensure that the string is ANSI.
302 MultiByteToWideChar(CP_ACP, 0, fullPath->cString(), -1, wsz, MAX_PATH);
[1536]303
304 // store the link in the "persistent" file
[1580]305 reply = (ppf->Save(wsz, TRUE) != E_FAIL);
306
307 // release the persistent file handle
308 ppf->Release();
[1536]309 }
310 psl->Release();
[1543]311 }
312 delete fullPath;
313 }
[1536]314 else
[1580]315 {
[1543]316 // Back to the dark ages with win 3.1!
317
318 // ensure that the requisite group is active; the add icon command will
319 // use the active group.
320 sprintf(buffer, "[ShowGroup(%s,5)]", groupName.c_str());
321 if (!DDEShellCommand(this->instance, buffer))
322 {
323 // TODO: articulate/determine possible errors; expand error handling
324 return false;
325 }
326
327 // and now add the item itself to the active (given) group
328 sprintf(buffer, "[AddItem(%s,%s)]", iconDestination.c_str(), iconName.c_str());
329 if (!DDEShellCommand(this->instance, buffer))
330 {
331 // TODO: again work out what the problems could be
332 return false;
333 }
334 reply = true;
[1536]335 }
336
[1543]337 if (reply)
338 {
339 this->logAction("ProgManAddItem", groupName, iconName, iconDestination);
[1536]340 }
341 return reply;
342}
343
344bool gsProgramManager::removeProgramGroup(string groupName)
[1543]345{
346 //CreateGroup(groupName);
347 char buffer[256];
348
[1536]349 if (this->platform->isExplorerShell())
[1543]350 {
351 // add using shell controls
352 LPITEMIDLIST pidl;
353
354 if (this->platform->isUserAdministrator())
355 {
[1547]356#if defined (__GNUC__)
357 // this is a hack as windows port of gcc doesn't have CSIDL_COMMON_PROGRAMS defined
358 if (SHGetSpecialFolderLocation(0, CSIDL_PROGRAMS, &pidl) != NOERROR)
359#else
[1543]360 if (SHGetSpecialFolderLocation(0, CSIDL_COMMON_PROGRAMS, &pidl) != NOERROR)
[1547]361#endif
[1543]362 {
363 return false;
364 }
365 }
366 else
367 {
368 if (SHGetSpecialFolderLocation(0, CSIDL_PROGRAMS, &pidl) != NOERROR)
369 {
370 return false;
371 }
372 }
373 SHGetPathFromIDList(pidl, buffer);
374
375 // Create the folder as required
376 FilePath *path = new FilePath(buffer, groupName);
377 if (!RemoveDirectory((char *) path->cString()))
378 {
379 // TODO: find cause of failure to remove
380 DWORD reason = GetLastError();
381 if (reason != 0)
382 {
383 return false;
384 }
385 }
[1541]386 }
[1536]387 else
[1543]388 {
389 // add using program manager DDE
390 sprintf(buffer, "[DeleteGroup (%s)]", groupName.c_str());
391 if (!DDEShellCommand(this->instance, buffer))
392 {
393 // flag error by returning false
394 return false;
395 }
[1536]396 }
[1543]397 return true;
[1536]398}
399
[1543]400bool gsProgramManager::removeIcon(string groupName, // group of the icon
401 string iconName) // name of the icon
402{
403 bool reply = false;
404 char buffer[MAX_PATH];
[1536]405
406 if (this->platform->isExplorerShell())
[1580]407 {
408 // create item using the shell
409 LPITEMIDLIST pidl;
410 // HRESULT hres;
411 // IShellLink * psl;
412 FilePath * fullPath;
[1536]413
[1580]414 // if the user is adminstrator, take the folder from the common programs folder
415 // (in the "All Users" profile)
416 if (this->platform->isUserAdministrator())
417 {
[1547]418#if defined (__GNUC__)
[1580]419 // this is a hack as windows port of gcc doesn't have CSIDL_COMMON_PROGRAMS defined
420 if (SHGetSpecialFolderLocation(0, CSIDL_PROGRAMS, &pidl) != NOERROR)
[1547]421#else
[1580]422 if (SHGetSpecialFolderLocation(0, CSIDL_COMMON_PROGRAMS, &pidl) != NOERROR)
[1547]423#endif
[1543]424 {
425 return false;
426 }
[1580]427 }
428 // otherwise get it from the user's own programs folder in their profile
429 else
430 {
431 if (SHGetSpecialFolderLocation(0, CSIDL_PROGRAMS, &pidl) != NOERROR)
432 {
[1543]433 return false;
434 }
[1580]435 }
[1536]436
[1580]437 // get path of folder from shell
438 SHGetPathFromIDList(pidl, buffer);
439
440 // delete file
441 fullPath = new FilePath(4, buffer, groupName.c_str(), iconName.c_str(), "!.lnk");
442 if (!DeleteFile(fullPath->cString()))
443 { reply = false;
444 }
445 delete fullPath;
446 }
[1536]447 else
[1580]448 {
449 // Back to the dark ages with win 3.1!
[1536]450
[1580]451 // ensure that the requisite group is active; the add icon command will
452 // use the active group.
453 sprintf(buffer, "[ShowGroup(%s,5)]", groupName.c_str());
454 if (!DDEShellCommand(this->instance, buffer))
455 {
456 // TODO: articulate/determine possible errors; expand error handling
457 return false;
458 }
[1536]459
[1580]460 // and now add the item itself to the active (given) group
461 sprintf(buffer, "[DeleteItem(%s)]", iconName.c_str());
462 if (!DDEShellCommand(this->instance, buffer))
463 {
464 // TODO: again work out what the problems could be
465 return false;
466 }
467 }
[1536]468 return reply;
469}
Note: See TracBrowser for help on using the repository browser.