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