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