]>
Commit | Line | Data |
---|---|---|
23c9e63c | 1 | /* |
85553593 | 2 | * Copyright (c) 2000, 2001, Red Hat, Inc. |
23c9e63c DD |
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 | ||
b24c88b3 RC |
20 | #if 0 |
21 | static const char *cvsid = | |
22 | "\n%%% $Id$\n"; | |
23 | #endif | |
8507f105 | 24 | |
a77b6167 MB |
25 | #include "netio.h" |
26 | ||
27 | #include "LogSingleton.h" | |
28 | ||
23c9e63c DD |
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" | |
23c9e63c DD |
36 | #include "nio-file.h" |
37 | #include "nio-ie5.h" | |
4a83b7b0 DD |
38 | #include "nio-http.h" |
39 | #include "nio-ftp.h" | |
4e8ff53f | 40 | #include "dialog.h" |
23c9e63c | 41 | |
071f7af3 MB |
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 | ||
f5d45c3b | 53 | NetIO::NetIO (char const *Purl) |
4a83b7b0 DD |
54 | { |
55 | set_url (Purl); | |
56 | } | |
57 | ||
58 | NetIO::~NetIO () | |
59 | { | |
60 | if (url) | |
5e0464a1 | 61 | delete[] url; |
4a83b7b0 | 62 | if (proto) |
5e0464a1 | 63 | delete[] proto; |
4a83b7b0 | 64 | if (host) |
5e0464a1 | 65 | delete[] host; |
4a83b7b0 | 66 | if (path) |
5e0464a1 | 67 | delete[] path; |
4a83b7b0 DD |
68 | } |
69 | ||
70 | void | |
341988b9 | 71 | NetIO::set_url (char const *Purl) |
23c9e63c DD |
72 | { |
73 | char *bp, *ep, c; | |
74 | ||
75 | file_size = 0; | |
5e0464a1 RC |
76 | url = new char[strlen (Purl) + 1]; |
77 | strcpy (url, Purl); | |
23c9e63c DD |
78 | proto = 0; |
79 | host = 0; | |
80 | port = 0; | |
81 | path = 0; | |
82 | ||
83 | bp = url; | |
84 | ep = strstr (bp, "://"); | |
85 | if (!ep) | |
86 | { | |
dbfe3c19 | 87 | path = strdup (url); |
23c9e63c DD |
88 | return; |
89 | } | |
90 | ||
91 | *ep = 0; | |
5e0464a1 RC |
92 | proto = new char [strlen (bp)+1]; |
93 | strcpy (proto, bp); | |
23c9e63c | 94 | *ep = ':'; |
b24c88b3 | 95 | bp = ep + 3; |
23c9e63c | 96 | |
4a83b7b0 | 97 | ep = bp + strcspn (bp, ":/"); |
23c9e63c DD |
98 | c = *ep; |
99 | *ep = 0; | |
5e0464a1 RC |
100 | host = new char [strlen (bp) + 1]; |
101 | strcpy (host, bp); | |
23c9e63c DD |
102 | *ep = c; |
103 | ||
104 | if (*ep == ':') | |
105 | { | |
b24c88b3 | 106 | port = atoi (ep + 1); |
23c9e63c DD |
107 | ep = strchr (ep, '/'); |
108 | } | |
109 | ||
110 | if (*ep) | |
5e0464a1 RC |
111 | { |
112 | path = new char [strlen (ep)+1]; | |
113 | strcpy (path, ep); | |
114 | } | |
23c9e63c DD |
115 | } |
116 | ||
23c9e63c DD |
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 * | |
f5d45c3b | 130 | NetIO::open (char const *url) |
23c9e63c DD |
131 | { |
132 | NetIO *rv = 0; | |
b24c88b3 RC |
133 | enum |
134 | { http, ftp, file } | |
135 | proto; | |
23c9e63c DD |
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); | |
23c9e63c | 147 | else if (net_method == IDC_NET_PROXY) |
f5d45c3b | 148 | rv = new NetIO_HTTP (url); |
4a83b7b0 DD |
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: | |
f5d45c3b | 157 | rv = new NetIO_FTP (url); |
4a83b7b0 | 158 | break; |
b24c88b3 RC |
159 | case file: |
160 | rv = new NetIO_File (url); | |
161 | break; | |
4a83b7b0 DD |
162 | } |
163 | } | |
23c9e63c DD |
164 | |
165 | if (!rv->ok ()) | |
166 | { | |
167 | delete rv; | |
168 | return 0; | |
169 | } | |
170 | ||
171 | return rv; | |
172 | } | |
4e8ff53f DD |
173 | |
174 | ||
175 | static char **user, **passwd; | |
348860fa | 176 | static int loading = 0; |
4e8ff53f DD |
177 | |
178 | static void | |
179 | check_if_enable_ok (HWND h) | |
180 | { | |
181 | int e = 0; | |
ed6137e5 | 182 | if (*user) |
4e8ff53f DD |
183 | e = 1; |
184 | EnableWindow (GetDlgItem (h, IDOK), e); | |
185 | } | |
186 | ||
187 | static void | |
188 | load_dialog (HWND h) | |
189 | { | |
348860fa | 190 | loading = 1; |
4e8ff53f DD |
191 | eset (h, IDC_NET_USER, *user); |
192 | eset (h, IDC_NET_PASSWD, *passwd); | |
193 | check_if_enable_ok (h); | |
348860fa | 194 | loading = 0; |
4e8ff53f DD |
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); | |
ed6137e5 MB |
202 | if (! *passwd) { |
203 | *passwd = new char[1]; | |
204 | passwd[0] = '\0'; | |
205 | } | |
4e8ff53f DD |
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: | |
348860fa DD |
216 | if (code == EN_CHANGE && !loading) |
217 | { | |
218 | save_dialog (h); | |
219 | check_if_enable_ok (h); | |
220 | } | |
4e8ff53f DD |
221 | break; |
222 | ||
223 | case IDOK: | |
224 | save_dialog (h); | |
225 | EndDialog (h, 0); | |
226 | break; | |
227 | ||
228 | case IDCANCEL: | |
229 | EndDialog (h, 1); | |
9f4a0c62 | 230 | LogSingleton::GetInstance().exit (1); |
4e8ff53f DD |
231 | break; |
232 | } | |
b24c88b3 | 233 | return 0; |
4e8ff53f DD |
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: | |
4875ac88 MB |
245 | auth_cmd (h, LOWORD(wParam), (HWND)lParam, HIWORD(wParam)); |
246 | return 0; | |
4e8ff53f DD |
247 | } |
248 | return FALSE; | |
249 | } | |
250 | ||
251 | static int | |
ab57ceaa | 252 | auth_common (HINSTANCE h, int id, HWND owner) |
4e8ff53f | 253 | { |
ab57ceaa | 254 | return DialogBox (h, MAKEINTRESOURCE (id), owner, auth_proc); |
4e8ff53f DD |
255 | } |
256 | ||
257 | int | |
ab57ceaa | 258 | NetIO::get_auth (HWND owner) |
4e8ff53f DD |
259 | { |
260 | user = &net_user; | |
261 | passwd = &net_passwd; | |
ab57ceaa | 262 | return auth_common (hinstance, IDD_NET_AUTH, owner); |
4e8ff53f DD |
263 | } |
264 | ||
265 | int | |
ab57ceaa | 266 | NetIO::get_proxy_auth (HWND owner) |
4e8ff53f DD |
267 | { |
268 | user = &net_proxy_user; | |
269 | passwd = &net_proxy_passwd; | |
ab57ceaa | 270 | return auth_common (hinstance, IDD_PROXY_AUTH, owner); |
4e8ff53f | 271 | } |
85553593 CV |
272 | |
273 | int | |
ab57ceaa | 274 | NetIO::get_ftp_auth (HWND owner) |
85553593 CV |
275 | { |
276 | if (net_ftp_user) | |
277 | { | |
5e0464a1 | 278 | delete[] net_ftp_user; |
85553593 CV |
279 | net_ftp_user = NULL; |
280 | } | |
281 | if (net_ftp_passwd) | |
282 | { | |
5e0464a1 | 283 | delete[] net_ftp_passwd; |
85553593 CV |
284 | net_ftp_passwd = NULL; |
285 | } | |
286 | if (!ftp_auth) | |
287 | return IDCANCEL; | |
288 | user = &net_ftp_user; | |
289 | passwd = &net_ftp_passwd; | |
ab57ceaa | 290 | return auth_common (hinstance, IDD_FTP_AUTH, owner); |
85553593 | 291 | } |