]> cygwin.com Git - cygwin-apps/setup.git/blob - netio.cc
* Makefile.am: Treat libgetopt++ as full-fledged SUBDIRS.
[cygwin-apps/setup.git] / netio.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 coordinate the various access
17 methods known to setup. To add a new method, create a pair of
18 nio-*.[ch] files and add the logic to NetIO::open here */
19
20 #if 0
21 static const char *cvsid =
22 "\n%%% $Id$\n";
23 #endif
24
25 #include "netio.h"
26
27 #include "LogSingleton.h"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "resource.h"
34 #include "state.h"
35 #include "msg.h"
36 #include "nio-file.h"
37 #include "nio-ie5.h"
38 #include "nio-http.h"
39 #include "nio-ftp.h"
40 #include "dialog.h"
41
42 int NetIO::net_method;
43 char *NetIO::net_proxy_host;
44 int NetIO::net_proxy_port;
45
46 char *NetIO::net_user;
47 char *NetIO::net_passwd;
48 char *NetIO::net_proxy_user;
49 char *NetIO::net_proxy_passwd;
50 char *NetIO::net_ftp_user;
51 char *NetIO::net_ftp_passwd;
52
53 NetIO::NetIO (char const *Purl)
54 {
55 set_url (Purl);
56 }
57
58 NetIO::~NetIO ()
59 {
60 if (url)
61 delete[] url;
62 if (proto)
63 delete[] proto;
64 if (host)
65 delete[] host;
66 if (path)
67 delete[] path;
68 }
69
70 void
71 NetIO::set_url (char const *Purl)
72 {
73 char *bp, *ep, c;
74
75 file_size = 0;
76 url = new char[strlen (Purl) + 1];
77 strcpy (url, Purl);
78 proto = 0;
79 host = 0;
80 port = 0;
81 path = 0;
82
83 bp = url;
84 ep = strstr (bp, "://");
85 if (!ep)
86 {
87 path = strdup (url);
88 return;
89 }
90
91 *ep = 0;
92 proto = new char [strlen (bp)+1];
93 strcpy (proto, bp);
94 *ep = ':';
95 bp = ep + 3;
96
97 ep = bp + strcspn (bp, ":/");
98 c = *ep;
99 *ep = 0;
100 host = new char [strlen (bp) + 1];
101 strcpy (host, bp);
102 *ep = c;
103
104 if (*ep == ':')
105 {
106 port = atoi (ep + 1);
107 ep = strchr (ep, '/');
108 }
109
110 if (*ep)
111 {
112 path = new char [strlen (ep)+1];
113 strcpy (path, ep);
114 }
115 }
116
117 int
118 NetIO::ok ()
119 {
120 return 0;
121 }
122
123 int
124 NetIO::read (char *buf, int nbytes)
125 {
126 return 0;
127 }
128
129 NetIO *
130 NetIO::open (char const *url)
131 {
132 NetIO *rv = 0;
133 enum
134 { http, ftp, file }
135 proto;
136 if (strncmp (url, "http://", 7) == 0)
137 proto = http;
138 else if (strncmp (url, "ftp://", 6) == 0)
139 proto = ftp;
140 else
141 proto = file;
142
143 if (proto == file)
144 rv = new NetIO_File (url);
145 else if (net_method == IDC_NET_IE5)
146 rv = new NetIO_IE5 (url);
147 else if (net_method == IDC_NET_PROXY)
148 rv = new NetIO_HTTP (url);
149 else if (net_method == IDC_NET_DIRECT)
150 {
151 switch (proto)
152 {
153 case http:
154 rv = new NetIO_HTTP (url);
155 break;
156 case ftp:
157 rv = new NetIO_FTP (url);
158 break;
159 case file:
160 rv = new NetIO_File (url);
161 break;
162 }
163 }
164
165 if (!rv->ok ())
166 {
167 delete rv;
168 return 0;
169 }
170
171 return rv;
172 }
173
174
175 static char **user, **passwd;
176 static int loading = 0;
177
178 static void
179 check_if_enable_ok (HWND h)
180 {
181 int e = 0;
182 if (*user)
183 e = 1;
184 EnableWindow (GetDlgItem (h, IDOK), e);
185 }
186
187 static void
188 load_dialog (HWND h)
189 {
190 loading = 1;
191 eset (h, IDC_NET_USER, *user);
192 eset (h, IDC_NET_PASSWD, *passwd);
193 check_if_enable_ok (h);
194 loading = 0;
195 }
196
197 static void
198 save_dialog (HWND h)
199 {
200 *user = eget (h, IDC_NET_USER, *user);
201 *passwd = eget (h, IDC_NET_PASSWD, *passwd);
202 if (! *passwd) {
203 *passwd = new char[1];
204 passwd[0] = '\0';
205 }
206 }
207
208 static BOOL
209 auth_cmd (HWND h, int id, HWND hwndctl, UINT code)
210 {
211 switch (id)
212 {
213
214 case IDC_NET_USER:
215 case IDC_NET_PASSWD:
216 if (code == EN_CHANGE && !loading)
217 {
218 save_dialog (h);
219 check_if_enable_ok (h);
220 }
221 break;
222
223 case IDOK:
224 save_dialog (h);
225 EndDialog (h, 0);
226 break;
227
228 case IDCANCEL:
229 EndDialog (h, 1);
230 LogSingleton::GetInstance().exit (1);
231 break;
232 }
233 return 0;
234 }
235
236 static BOOL CALLBACK
237 auth_proc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
238 {
239 switch (message)
240 {
241 case WM_INITDIALOG:
242 load_dialog (h);
243 return FALSE;
244 case WM_COMMAND:
245 auth_cmd (h, LOWORD(wParam), (HWND)lParam, HIWORD(wParam));
246 return 0;
247 }
248 return FALSE;
249 }
250
251 static int
252 auth_common (HINSTANCE h, int id, HWND owner)
253 {
254 return DialogBox (h, MAKEINTRESOURCE (id), owner, auth_proc);
255 }
256
257 int
258 NetIO::get_auth (HWND owner)
259 {
260 user = &net_user;
261 passwd = &net_passwd;
262 return auth_common (hinstance, IDD_NET_AUTH, owner);
263 }
264
265 int
266 NetIO::get_proxy_auth (HWND owner)
267 {
268 user = &net_proxy_user;
269 passwd = &net_proxy_passwd;
270 return auth_common (hinstance, IDD_PROXY_AUTH, owner);
271 }
272
273 int
274 NetIO::get_ftp_auth (HWND owner)
275 {
276 if (net_ftp_user)
277 {
278 delete[] net_ftp_user;
279 net_ftp_user = NULL;
280 }
281 if (net_ftp_passwd)
282 {
283 delete[] net_ftp_passwd;
284 net_ftp_passwd = NULL;
285 }
286 if (!ftp_auth)
287 return IDCANCEL;
288 user = &net_ftp_user;
289 passwd = &net_ftp_passwd;
290 return auth_common (hinstance, IDD_FTP_AUTH, owner);
291 }
This page took 0.047153 seconds and 5 git commands to generate.