Add new option --no-write-registry
[cygwin-setup.git] / netio.cc
blob631532a3a53d5db83086be5e6851572d72c8741d
1 /*
2 * Copyright (c) 2000, 2001, Red Hat, Inc.
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.
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
12 * Written by DJ Delorie <dj@cygnus.com>
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 */
20 #include "netio.h"
22 #include "LogFile.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include <shlwapi.h>
30 #include "resource.h"
31 #include "state.h"
32 #include "msg.h"
33 #include "nio-ie5.h"
34 #include "dialog.h"
36 int NetIO::net_method;
37 char *NetIO::net_proxy_host;
38 int NetIO::net_proxy_port;
40 char *NetIO::net_user;
41 char *NetIO::net_passwd;
42 char *NetIO::net_proxy_user;
43 char *NetIO::net_proxy_passwd;
44 char *NetIO::net_ftp_user;
45 char *NetIO::net_ftp_passwd;
47 int
48 NetIO::ok ()
50 return 0;
53 int
54 NetIO::read (char *buf, int nbytes)
56 return 0;
59 NetIO *
60 NetIO::open (char const *url, bool cachable)
62 NetIO *rv = 0;
63 std::string file_url;
65 enum
66 { http, https, ftp, ftps, file }
67 proto;
68 if (strncmp (url, "http://", 7) == 0)
69 proto = http;
70 else if (strncmp (url, "https://", 8) == 0)
71 proto = https;
72 else if (strncmp (url, "ftp://", 6) == 0)
73 proto = ftp;
74 else if (strncmp (url, "ftps://", 7) == 0)
75 proto = ftps;
76 else if (strncmp (url, "file://", 7) == 0)
78 proto = file;
80 // WinInet expects a 'legacy' file:// URL
81 // (i.e. a windows path with "file://" prepended)
82 // https://blogs.msdn.microsoft.com/freeassociations/2005/05/19/the-bizarre-and-unhappy-story-of-file-urls/
83 char path[MAX_PATH];
84 DWORD len = MAX_PATH;
85 if (S_OK == PathCreateFromUrl(url, path, &len, 0))
87 file_url = std::string("file://") + path;
88 url = file_url.c_str();
91 else
92 // treat everything else as a windows path
94 proto = file;
95 file_url = std::string("file://") + url;
96 url = file_url.c_str();
99 rv = new NetIO_IE5 (url, proto == file ? false : cachable);
101 if (rv && !rv->ok ())
103 delete rv;
104 return 0;
107 return rv;
111 static char **user, **passwd;
112 static int loading = 0;
114 static void
115 check_if_enable_ok (HWND h)
117 int e = 0;
118 if (*user)
119 e = 1;
120 EnableWindow (GetDlgItem (h, IDOK), e);
123 static void
124 load_dialog (HWND h)
126 loading = 1;
127 eset (h, IDC_NET_USER, *user);
128 eset (h, IDC_NET_PASSWD, *passwd);
129 check_if_enable_ok (h);
130 loading = 0;
133 static void
134 save_dialog (HWND h)
136 *user = eget (h, IDC_NET_USER, *user);
137 *passwd = eget (h, IDC_NET_PASSWD, *passwd);
138 if (! *passwd) {
139 *passwd = new char[1];
140 (*passwd)[0] = '\0';
144 static BOOL
145 auth_cmd (HWND h, int id, HWND hwndctl, UINT code)
147 switch (id)
150 case IDC_NET_USER:
151 case IDC_NET_PASSWD:
152 if (code == EN_CHANGE && !loading)
154 save_dialog (h);
155 check_if_enable_ok (h);
157 break;
159 case IDOK:
160 save_dialog (h);
161 EndDialog (h, 0);
162 break;
164 case IDCANCEL:
165 EndDialog (h, 1);
166 Logger ().exit (1);
167 break;
169 return 0;
172 static INT_PTR CALLBACK
173 auth_proc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
175 switch (message)
177 case WM_INITDIALOG:
178 load_dialog (h);
179 return FALSE;
180 case WM_COMMAND:
181 auth_cmd (h, LOWORD(wParam), (HWND)lParam, HIWORD(wParam));
182 return 0;
184 return FALSE;
187 static int
188 auth_common (HINSTANCE h, int id, HWND owner)
190 return DialogBox (h, MAKEINTRESOURCE (id), owner, auth_proc);
194 NetIO::get_auth (HWND owner)
196 user = &net_user;
197 passwd = &net_passwd;
198 return auth_common (hinstance, IDD_NET_AUTH, owner);
202 NetIO::get_proxy_auth (HWND owner)
204 user = &net_proxy_user;
205 passwd = &net_proxy_passwd;
206 return auth_common (hinstance, IDD_PROXY_AUTH, owner);
210 NetIO::get_ftp_auth (HWND owner)
212 if (net_ftp_user)
214 delete[] net_ftp_user;
215 net_ftp_user = NULL;
217 if (net_ftp_passwd)
219 delete[] net_ftp_passwd;
220 net_ftp_passwd = NULL;
222 user = &net_ftp_user;
223 passwd = &net_ftp_passwd;
224 return auth_common (hinstance, IDD_FTP_AUTH, owner);
227 const char *
228 NetIO::net_method_name ()
230 switch (net_method)
232 case IDC_NET_PRECONFIG:
233 return "Preconfig";
234 case IDC_NET_DIRECT:
235 return "Direct";
236 case IDC_NET_PROXY:
237 return "Proxy";
238 default:
239 return "Unknown";