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