]> cygwin.com Git - cygwin-apps/setup.git/blob - desktop.cc
2002-04-29 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / desktop.cc
1 /*
2 * Copyright (c) 2000, 2001 Red Hat, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by DJ Delorie <dj@cygnus.com>
13 *
14 */
15
16 /* The purpose of this file is to manage all the desktop setup, such
17 as start menu, batch files, desktop icons, and shortcuts. Note
18 that unlike other do_* functions, this one is called directly from
19 install.cc */
20
21 #if 0
22 static const char *cvsid =
23 "\n%%% $Id$\n";
24 #endif
25
26 #include "win32.h"
27 #include <shlobj.h>
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32
33 #include "resource.h"
34 #include "ini.h"
35 #include "msg.h"
36 #include "state.h"
37 #include "dialog.h"
38 #include "version.h"
39 #include "mount.h"
40
41 #include "port.h"
42
43 #include "mklink2.h"
44
45 #include "package_db.h"
46 #include "package_meta.h"
47 #include "package_version.h"
48
49 #include "filemanip.h"
50 #include "String++.h"
51 #include "io_stream.h"
52
53 #include "desktop.h"
54 #include "getopt++/BoolOption.h"
55
56 static BoolOption NoShortcutsOption (false, 'n', "no-shortcuts", "Disable creation of desktop and start menu shortcuts");
57
58 static OSVERSIONINFO verinfo;
59
60 /* Lines starting with '@' are conditionals - include 'N' for NT,
61 '5' for Win95, '8' for Win98, '*' for all, like this:
62 echo foo
63 @N8
64 echo NT or 98
65 @*
66 */
67
68 static const char *etc_profile[] = {
69 "PATH=\"/usr/local/bin:/usr/bin:/bin:$PATH\"",
70 "",
71 "USER=\"`id -un`\"",
72 "",
73 "# Set up USER's home directory",
74 "if [ -z \"$HOME\" ]; then",
75 " HOME=\"/home/$USER\"",
76 "fi",
77 "",
78 "if [ ! -d \"$HOME\" ]; then",
79 " mkdir -p \"$HOME\"",
80 "fi",
81 "",
82 "export HOME USER",
83 "",
84 "for i in /etc/profile.d/*.sh ; do",
85 " if [ -f $i ]; then",
86 " . $i",
87 " fi",
88 "done",
89 "",
90 "export MAKE_MODE=unix",
91 "export PS1='\\[\\033]0;\\w\\007",
92 "\\033[32m\\]\\u@\\h \\[\\033[33m\\w\\033[0m\\]",
93 "$ '",
94 "",
95 "cd \"$HOME\"",
96 0
97 };
98
99 #define COMMAND9XARGS String("/E:4096 /c")
100 #define COMMAND9XEXE String("\\command.com")
101
102 static String batname;
103 static String iconname;
104
105 static void
106 make_link (String const &linkpath, String const &title, String const &target)
107 {
108 String fname = linkpath + "/" + title + ".lnk";
109
110 if (_access (fname.cstr_oneuse(), 0) == 0)
111 return; /* already exists */
112
113 msg ("make_link %s, %s, %s\n",
114 fname.cstr_oneuse(), title.cstr_oneuse(), target.cstr_oneuse());
115
116 io_stream::mkpath_p (PATH_TO_FILE, String ("file://") + fname);
117
118 String exepath;
119
120 String argbuf;
121 /* If we are running Win9x, build a command line. */
122 if (verinfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
123 {
124 exepath = target;
125 argbuf = " ";
126 // sprintf (argbuf, " ");
127 }
128 else
129 {
130 char windir[MAX_PATH];
131
132 GetWindowsDirectory (windir, sizeof (windir));
133 exepath = String(windir) + COMMAND9XEXE;
134 argbuf = COMMAND9XARGS + " " + target;
135 // sprintf (argbuf, "%s %s", COMMAND9XARGS, target.cstr_oneuse());
136 }
137
138 msg ("make_link_2 (%s, %s, %s, %s)",
139 exepath.cstr_oneuse(), argbuf.cstr_oneuse(),
140 iconname.cstr_oneuse(), fname.cstr_oneuse());
141 make_link_2 (exepath.cstr_oneuse(), argbuf.cstr_oneuse(),
142 iconname.cstr_oneuse(), fname.cstr_oneuse());
143 }
144
145 static void
146 start_menu (String const &title, String const &target)
147 {
148 LPITEMIDLIST id;
149 int issystem = (root_scope == IDC_ROOT_SYSTEM) ? 1 : 0;
150 SHGetSpecialFolderLocation (NULL,
151 issystem ? CSIDL_COMMON_PROGRAMS :
152 CSIDL_PROGRAMS, &id);
153 char _path[_MAX_PATH];
154 SHGetPathFromIDList (id, _path);
155 String path(_path);
156 // Win95 does not use common programs unless multiple users for Win95 is enabled
157 msg ("Program directory for program link: %s", path.cstr_oneuse());
158 if (path.size() == 0)
159 {
160 SHGetSpecialFolderLocation (NULL, CSIDL_PROGRAMS, &id);
161 SHGetPathFromIDList (id, _path);
162 path = String(_path);
163 msg ("Program directory for program link changed to: %s",
164 path.cstr_oneuse());
165 }
166 // end of Win95 addition
167 path += "/Cygwin";
168 make_link (path, title, target);
169 }
170
171 static void
172 desktop_icon (String const &title, String const &target)
173 {
174 char path[_MAX_PATH];
175 LPITEMIDLIST id;
176 int issystem = (root_scope == IDC_ROOT_SYSTEM) ? 1 : 0;
177 //SHGetSpecialFolderLocation (NULL, issystem ? CSIDL_DESKTOP : CSIDL_COMMON_DESKTOPDIRECTORY, &id);
178 SHGetSpecialFolderLocation (NULL,
179 issystem ? CSIDL_COMMON_DESKTOPDIRECTORY :
180 CSIDL_DESKTOPDIRECTORY, &id);
181 SHGetPathFromIDList (id, path);
182 // following lines added because it appears Win95 does not use common programs
183 // unless it comes into play when multiple users for Win95 is enabled
184 msg ("Desktop directory for desktop link: %s", path);
185 if (strlen (path) == 0)
186 {
187 SHGetSpecialFolderLocation (NULL, CSIDL_DESKTOPDIRECTORY, &id);
188 SHGetPathFromIDList (id, path);
189 msg ("Desktop directory for deskop link changed to: %s", path);
190 }
191 // end of Win95 addition
192 make_link (path, title, target);
193 }
194
195 static void
196 make_cygwin_bat ()
197 {
198 batname = backslash (cygpath ("/cygwin.bat"));
199
200 /* if the batch file exists, don't overwrite it */
201 if (_access (batname.cstr_oneuse(), 0) == 0)
202 return;
203
204 FILE *bat = fopen (batname.cstr_oneuse(), "wt");
205 if (!bat)
206 return;
207
208 fprintf (bat, "@echo off\n\n");
209
210 fprintf (bat, "%.2s\n", get_root_dir ().cstr_oneuse());
211 fprintf (bat, "chdir %s\n\n",
212 backslash (get_root_dir () + "/bin").cstr_oneuse());
213
214 fprintf (bat, "bash --login -i\n");
215
216 fclose (bat);
217 }
218
219 static void
220 make_etc_profile ()
221 {
222 String fname = cygpath ("/etc/profile");
223
224 /* if the file exists, don't overwrite it */
225 if (_access (fname.cstr_oneuse(), 0) == 0)
226 return;
227
228 char os;
229 switch (verinfo.dwPlatformId)
230 {
231 case VER_PLATFORM_WIN32_NT:
232 os = 'N';
233 break;
234 case VER_PLATFORM_WIN32_WINDOWS:
235 if (verinfo.dwMinorVersion == 0)
236 os = '5';
237 else
238 os = '8';
239 break;
240 default:
241 os = '?';
242 break;
243 }
244 msg ("os is %c", os);
245
246 FILE *p = fopen (fname.cstr_oneuse(), "wb");
247 if (!p)
248 return;
249
250 int i, allow = 1;
251 for (i = 0; etc_profile[i]; i++)
252 {
253 if (etc_profile[i][0] == '@')
254 {
255 allow = 0;
256 msg ("profile: %s", etc_profile[i]);
257 for (const char *cp = etc_profile[i] + 1; *cp; cp++)
258 if (*cp == os || *cp == '*')
259 allow = 1;
260 msg ("allow is %d\n", allow);
261 }
262 else if (allow)
263 fprintf (p, "%s\n", etc_profile[i]);
264 }
265
266 fclose (p);
267 }
268
269 static int
270 uexists (const char *path)
271 {
272 String f = cygpath (path);
273 int a = _access (f.cstr_oneuse(), 0);
274 if (a == 0)
275 return 1;
276 return 0;
277 }
278
279 static void
280 make_passwd_group ()
281 {
282 String fname = cygpath ("/etc/postinstall/passwd-grp.bat");
283 io_stream::mkpath_p (PATH_TO_FILE, String("file://") + fname);
284
285 if ((uexists ("/etc/passwd") || uexists ("/etc/passwd.lnk"))
286 && (uexists ("/etc/group") || uexists ("/etc/group.lnk")))
287 return;
288
289 if (verinfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
290 {
291 packagedb db;
292 packagemeta *pkg = db.packages.getbykey ("cygwin");
293 if (pkg && pkg->installed)
294 {
295 /* mkpasswd and mkgroup are not working on 9x/ME up to 1.1.5-4 */
296 String border_version = canonicalize_version ("1.1.5-4");
297 String inst_version =
298 canonicalize_version (pkg->installed->Canonical_version ());
299 if (inst_version.compare(border_version) <= 0)
300 return;
301 }
302 }
303
304 FILE *p = fopen (fname.cstr_oneuse(), "wt");
305 if (!p)
306 return;
307 if (!(uexists ("/etc/passwd") || uexists ("/etc/passwd.lnk")))
308 fprintf (p, "bin\\mkpasswd -l > etc\\passwd\n");
309 if (!(uexists ("/etc/group") || uexists ("/etc/group.lnk")))
310 fprintf (p, "bin\\mkgroup -l > etc\\group\n");
311 fclose (p);
312 }
313
314 static void
315 save_icon ()
316 {
317 iconname = backslash (cygpath ("/cygwin.ico"));
318
319 HRSRC rsrc = FindResource (NULL, "CYGWIN.ICON", "FILE");
320 if (rsrc == NULL)
321 {
322 fatal ("FindResource failed");
323 }
324 HGLOBAL res = LoadResource (NULL, rsrc);
325 char *data = (char *) LockResource (res);
326 int len = SizeofResource (NULL, rsrc);
327
328 FILE *f = fopen (iconname.cstr_oneuse(), "wb");
329 if (f)
330 {
331 fwrite (data, 1, len, f);
332 fclose (f);
333 }
334 }
335
336 static void
337 do_desktop_setup ()
338 {
339 save_icon ();
340
341 make_cygwin_bat ();
342 make_etc_profile ();
343 make_passwd_group ();
344
345 if (root_menu)
346 {
347 start_menu ("Cygwin Bash Shell", batname);
348 }
349
350 if (root_desktop)
351 {
352 desktop_icon ("Cygwin", batname);
353 }
354 }
355
356 static int da[] = { IDC_ROOT_DESKTOP, 0 };
357 static int ma[] = { IDC_ROOT_MENU, 0 };
358
359 static void
360 check_if_enable_next (HWND h)
361 {
362 EnableWindow (GetDlgItem (h, IDOK), 1);
363 }
364
365 static void
366 load_dialog (HWND h)
367 {
368 rbset (h, da, root_desktop);
369 rbset (h, ma, root_menu);
370 check_if_enable_next (h);
371 }
372
373 static int
374 check_desktop (String const title, String const target)
375 {
376 char path[_MAX_PATH];
377 LPITEMIDLIST id;
378 int issystem = (root_scope == IDC_ROOT_SYSTEM) ? 1 : 0;
379 SHGetSpecialFolderLocation (NULL,
380 issystem ? CSIDL_COMMON_DESKTOPDIRECTORY :
381 CSIDL_DESKTOPDIRECTORY, &id);
382 SHGetPathFromIDList (id, path);
383 // following lines added because it appears Win95 does not use common programs
384 // unless it comes into play when multiple users for Win95 is enabled
385 msg ("Desktop directory for desktop link: %s", path);
386 if (strlen (path) == 0)
387 {
388 SHGetSpecialFolderLocation (NULL, CSIDL_DESKTOPDIRECTORY, &id);
389 SHGetPathFromIDList (id, path);
390 msg ("Desktop directory for deskop link changed to: %s", path);
391 }
392 // end of Win95 addition
393 String fname = String (path) + "/" + title + ".lnk";
394
395 if (_access (fname.cstr_oneuse(), 0) == 0)
396 return 0; /* already exists */
397
398 fname = String (path) + "/" + title + ".pif"; /* check for a pif as well */
399
400 if (_access (fname.cstr_oneuse(), 0) == 0)
401 return 0; /* already exists */
402
403 return IDC_ROOT_DESKTOP;
404 }
405
406 static int
407 check_startmenu (String const title, String const target)
408 {
409 char path[_MAX_PATH];
410 LPITEMIDLIST id;
411 int issystem = (root_scope == IDC_ROOT_SYSTEM) ? 1 : 0;
412 SHGetSpecialFolderLocation (NULL,
413 issystem ? CSIDL_COMMON_PROGRAMS :
414 CSIDL_PROGRAMS, &id);
415 SHGetPathFromIDList (id, path);
416 // following lines added because it appears Win95 does not use common programs
417 // unless it comes into play when multiple users for Win95 is enabled
418 msg ("Program directory for program link: %s", path);
419 if (strlen (path) == 0)
420 {
421 SHGetSpecialFolderLocation (NULL, CSIDL_PROGRAMS, &id);
422 SHGetPathFromIDList (id, path);
423 msg ("Program directory for program link changed to: %s", path);
424 }
425 // end of Win95 addition
426 strcat (path, "/Cygwin");
427 String fname = String (path) + "/" + title + ".lnk";
428
429 if (_access (fname.cstr_oneuse(), 0) == 0)
430 return 0; /* already exists */
431
432 fname = String (path) + "/" + title + ".pif"; /* check for a pif as well */
433
434 if (_access (fname.cstr_oneuse(), 0) == 0)
435 return 0; /* already exists */
436
437 return IDC_ROOT_MENU;
438 }
439
440 static void
441 save_dialog (HWND h)
442 {
443 root_desktop = rbget (h, da);
444 root_menu = rbget (h, ma);
445 }
446
447 static BOOL
448 dialog_cmd (HWND h, int id, HWND hwndctl, UINT code)
449 {
450 switch (id)
451 {
452
453 case IDC_ROOT_DESKTOP:
454 case IDC_ROOT_MENU:
455 save_dialog (h);
456 check_if_enable_next (h);
457 break;
458 }
459 return 0;
460 }
461
462 bool
463 DesktopSetupPage::Create ()
464 {
465 return PropertyPage::Create (NULL, dialog_cmd, IDD_DESKTOP);
466 }
467
468 void
469 DesktopSetupPage::OnInit ()
470 {
471 // FIXME: This CoInitialize() feels like it could be moved to startup in main.cc.
472 CoInitialize (NULL);
473 verinfo.dwOSVersionInfoSize = sizeof (verinfo);
474 GetVersionEx (&verinfo);
475 root_desktop =
476 check_desktop ("Cygwin", backslash (cygpath ("/cygwin.bat")));
477 root_menu =
478 check_startmenu ("Cygwin Bash Shell",
479 backslash (cygpath ("/cygwin.bat")));
480 if (NoShortcutsOption)
481 {
482 root_desktop=false;
483 root_menu=false;
484 OnFinish();
485 }
486 else
487 load_dialog (GetHWND ());
488 }
489
490 long
491 DesktopSetupPage::OnBack ()
492 {
493 HWND h = GetHWND ();
494 save_dialog (h);
495 NEXT (IDD_CHOOSE);
496 return IDD_CHOOSER;
497 }
498
499 bool
500 DesktopSetupPage::OnFinish ()
501 {
502 HWND h = GetHWND ();
503 save_dialog (h);
504 do_desktop_setup ();
505 NEXT (IDD_S_POSTINSTALL);
506 do_postinstall (GetInstance (), h);
507
508 return true;
509 }
This page took 0.059125 seconds and 5 git commands to generate.