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