]> cygwin.com Git - cygwin-apps/setup.git/blob - localdir.cc
* source.cc: Add support for both -D and -L on command line
[cygwin-apps/setup.git] / localdir.cc
1 /*
2 * Copyright (c) 2000, 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 Andrej Borsenkow <Andrej.Borsenkow@mow.siemens.ru>
13 * based on work and suggestions of DJ Delorie
14 *
15 */
16
17 /* The purpose of this file is to ask the user where they want the
18 * locally downloaded package files to be cached
19 */
20
21 #if 0
22 static const char *cvsid =
23 "\n%%% $Id$\n";
24 #endif
25
26 #include "localdir.h"
27
28 #include "LogSingleton.h"
29 #include "LogFile.h"
30 #include "win32.h"
31
32 #include <shlobj.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36
37 #include "dialog.h"
38 #include "resource.h"
39 #include "state.h"
40 #include "msg.h"
41 #include "mount.h"
42 #include "io_stream.h"
43
44 #include "UserSettings.h"
45
46 #include "getopt++/StringOption.h"
47
48 #include "threebar.h"
49 extern ThreeBarProgressPage Progress;
50 extern LogFile * theLog;
51
52 static StringOption LocalDirOption ("", 'l', "local-package-dir", "Local package directory", false);
53
54 static LocalDirSetting localDir;
55
56 static ControlAdjuster::ControlInfo LocaldirControlsInfo[] = {
57 { IDC_LOCALDIR_GRP, CP_STRETCH, CP_TOP },
58 { IDC_LOCAL_DIR, CP_STRETCH, CP_TOP },
59 { IDC_LOCAL_DIR_BROWSE, CP_RIGHT, CP_TOP },
60 {0, CP_LEFT, CP_TOP}
61 };
62
63 void
64 LocalDirSetting::load(){
65 static int inited = 0;
66 if (inited)
67 return;
68 io_stream *f = UserSettings::Instance().settingFileForLoad("last-cache");
69 if (f)
70 {
71 char localdir[1000];
72 char *fg_ret = f->gets (localdir, 1000);
73 delete f;
74 if (fg_ret)
75 local_dir = std::string (localdir);
76 }
77 if (((std::string)LocalDirOption).size())
78 local_dir = ((std::string)LocalDirOption);
79 inited = 1;
80 }
81
82 void
83 LocalDirSetting::save()
84 {
85 io_stream *f = UserSettings::Instance().settingFileForSave("last-cache");
86 if (f)
87 {
88 f->write (local_dir.c_str(), local_dir.size());
89 delete f;
90 }
91 if (source == IDC_SOURCE_DOWNLOAD || !get_root_dir ().size())
92 {
93 theLog->clearFiles();
94 theLog->setFile (LOG_BABBLE, local_dir + "/setup.log.full", false);
95 theLog->setFile (0, local_dir + "/setup.log", true);
96 }
97 else
98 {
99 theLog->clearFiles();
100 theLog->setFile (LOG_BABBLE, cygpath ("/var/log/setup.log.full"), false);
101 theLog->setFile (0, cygpath ("/var/log/setup.log"), true);
102 }
103 }
104
105 static void
106 check_if_enable_next (HWND h)
107 {
108 EnableWindow (GetDlgItem (h, IDOK), local_dir.size() != 0);
109 }
110
111 static void
112 load_dialog (HWND h)
113 {
114 eset (h, IDC_LOCAL_DIR, local_dir);
115 check_if_enable_next (h);
116 }
117
118 static void
119 save_dialog (HWND h)
120 {
121 local_dir = egetString (h, IDC_LOCAL_DIR);
122 }
123
124
125 static int CALLBACK
126 browse_cb (HWND h, UINT msg, LPARAM lp, LPARAM data)
127 {
128 switch (msg)
129 {
130 case BFFM_INITIALIZED:
131 if (local_dir.size())
132 SendMessage (h, BFFM_SETSELECTION, TRUE, (LPARAM) local_dir.c_str());
133 break;
134 }
135 return 0;
136 }
137
138 static void
139 browse (HWND h)
140 {
141 BROWSEINFO bi;
142 CHAR name[MAX_PATH];
143 LPITEMIDLIST pidl;
144 memset (&bi, 0, sizeof (bi));
145 bi.hwndOwner = h;
146 bi.pszDisplayName = name;
147 bi.lpszTitle = "Select download directory";
148 bi.ulFlags = BIF_RETURNONLYFSDIRS;
149 bi.lpfn = browse_cb;
150 pidl = SHBrowseForFolder (&bi);
151 if (pidl)
152 {
153 if (SHGetPathFromIDList (pidl, name))
154 eset (h, IDC_LOCAL_DIR, name);
155 }
156 }
157
158 static BOOL
159 dialog_cmd (HWND h, int id, HWND hwndctl, UINT code)
160 {
161 switch (id)
162 {
163
164 case IDC_LOCAL_DIR:
165 save_dialog (h);
166 check_if_enable_next (h);
167 break;
168
169 case IDC_LOCAL_DIR_BROWSE:
170 browse (h);
171 break;
172 }
173 return 0;
174 }
175
176 LocalDirPage::LocalDirPage ()
177 {
178 sizeProcessor.AddControlInfo (LocaldirControlsInfo);
179 }
180
181 bool
182 LocalDirPage::Create ()
183 {
184 return PropertyPage::Create (NULL, dialog_cmd, IDD_LOCAL_DIR);
185 }
186
187 void
188 LocalDirPage::OnActivate ()
189 {
190 load_dialog (GetHWND ());
191 }
192
193 long
194 LocalDirPage::OnNext ()
195 {
196 HWND h = GetHWND ();
197
198 save_dialog (h);
199 localDir.save ();
200 log (LOG_PLAIN) << "Selected local directory: " << local_dir << endLog;
201
202 bool trySetCurDir = true;
203 while (trySetCurDir)
204 {
205 trySetCurDir = false;
206 if (SetCurrentDirectoryA (local_dir.c_str()))
207 {
208 if (source == IDC_SOURCE_CWD)
209 {
210 if (do_fromcwd (GetInstance (), GetHWND ()))
211 {
212 Progress.SetActivateTask (WM_APP_START_SETUP_INI_DOWNLOAD);
213 return IDD_INSTATUS;
214 }
215 return IDD_CHOOSE;
216 }
217 }
218 else
219 {
220 DWORD err = GetLastError ();
221 char msgText[1000];
222 LoadString (hinstance, IDS_ERR_CHDIR, msgText, sizeof (msgText));
223 char* buf;
224 char msg[1000];
225 if (FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM |
226 FORMAT_MESSAGE_ALLOCATE_BUFFER, 0, err, LANG_NEUTRAL,
227 (LPSTR)&buf, 0, 0) != 0)
228 {
229 snprintf (msg, sizeof (msg), msgText, local_dir.c_str(),
230 buf, err);
231 }
232 else
233 snprintf (msg, sizeof (msg), msgText, local_dir.c_str(),
234 "(unknown error)", err);
235 log (LOG_PLAIN) << msg << endLog;
236 int ret = MessageBox (h, msg, 0, MB_ICONEXCLAMATION |
237 MB_ABORTRETRYIGNORE);
238
239 if ((ret == IDABORT) || (ret == IDCANCEL))
240 return -1;
241 else
242 trySetCurDir = (ret == IDRETRY);
243 }
244 }
245
246 return 0;
247 }
248
249 long
250 LocalDirPage::OnBack ()
251 {
252 save_dialog (GetHWND ());
253 return 0;
254 }
This page took 0.045742 seconds and 5 git commands to generate.