]>
Commit | Line | Data |
---|---|---|
a0b4c98b VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/dialup.cpp | |
3 | // Purpose: MSW implementation of network/dialup classes and functions | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 07.07.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Vadim Zeitlin | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // for compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | // these functions require Win32 | |
28 | #if defined(__WIN16__) && wxUSE_DIALUP_MANAGER | |
29 | #undef wxUSE_DIALUP_MANAGER | |
30 | #define wxUSE_DIALUP_MANAGER 0 | |
31 | #endif // wxUSE_DIALUP_MANAGER && Win16 | |
32 | ||
33 | #if wxUSE_DIALUP_MANAGER | |
34 | ||
35 | #ifndef WX_PRECOMP | |
36 | #include "wx/log.h" | |
d1f4970d VZ |
37 | #include "wx/intl.h" |
38 | #include "wx/event.h" | |
a0b4c98b VZ |
39 | #endif |
40 | ||
fcec6429 VZ |
41 | #include "wx/timer.h" |
42 | #include "wx/app.h" | |
43 | #include "wx/generic/choicdgg.h" | |
44 | ||
ff0ea71c GT |
45 | #include "wx/msw/private.h" // must be before #include "dynlib.h" |
46 | ||
7d672752 VZ |
47 | #if !wxUSE_DYNLIB_CLASS |
48 | #error You need wxUSE_DYNLIB_CLASS to be 1 to compile dialup.cpp. | |
c25a510b JS |
49 | #endif |
50 | ||
a0b4c98b VZ |
51 | #include "wx/dynlib.h" |
52 | ||
2690830e | 53 | #include "wx/dialup.h" |
a0b4c98b | 54 | |
658fb8e6 JS |
55 | // Doesn't yet compile under VC++ 4, BC++, mingw, Watcom C++: no wininet.h |
56 | #if !defined(__BORLANDC__) && !defined(__GNUWIN32_OLD__) && !defined(__GNUWIN32__) && !defined(__WATCOMC__) && ! (defined(__VISUALC__) && (__VISUALC__ < 1020)) | |
106f0395 | 57 | |
a0b4c98b VZ |
58 | #include <ras.h> |
59 | #include <raserror.h> | |
60 | ||
2690830e VZ |
61 | #include <wininet.h> |
62 | ||
a2327a9f JS |
63 | // Not in VC++ 5 |
64 | #ifndef INTERNET_CONNECTION_LAN | |
65 | #define INTERNET_CONNECTION_LAN 2 | |
66 | #endif | |
67 | #ifndef INTERNET_CONNECTION_PROXY | |
68 | #define INTERNET_CONNECTION_PROXY 4 | |
69 | #endif | |
70 | ||
a0b4c98b VZ |
71 | // ---------------------------------------------------------------------------- |
72 | // constants | |
73 | // ---------------------------------------------------------------------------- | |
74 | ||
75 | // this message is sent by the secondary thread when RAS status changes | |
76 | #define wxWM_RAS_STATUS_CHANGED (WM_USER + 10010) | |
2690830e | 77 | #define wxWM_RAS_DIALING_PROGRESS (WM_USER + 10011) |
a0b4c98b VZ |
78 | |
79 | // ---------------------------------------------------------------------------- | |
80 | // types | |
81 | // ---------------------------------------------------------------------------- | |
82 | ||
83 | // the signatures of RAS functions: all this is quite heavy, but we must do it | |
84 | // to allow running wxWin programs on machine which don't have RAS installed | |
85 | // (this does exist) - if we link with rasapi32.lib, the program will fail on | |
86 | // startup because of the missing DLL... | |
87 | ||
88 | #ifndef UNICODE | |
2690830e VZ |
89 | typedef DWORD (APIENTRY * RASDIAL)( LPRASDIALEXTENSIONS, LPCSTR, LPRASDIALPARAMSA, DWORD, LPVOID, LPHRASCONN ); |
90 | typedef DWORD (APIENTRY * RASENUMCONNECTIONS)( LPRASCONNA, LPDWORD, LPDWORD ); | |
91 | typedef DWORD (APIENTRY * RASENUMENTRIES)( LPCSTR, LPCSTR, LPRASENTRYNAMEA, LPDWORD, LPDWORD ); | |
92 | typedef DWORD (APIENTRY * RASGETCONNECTSTATUS)( HRASCONN, LPRASCONNSTATUSA ); | |
93 | typedef DWORD (APIENTRY * RASGETERRORSTRING)( UINT, LPSTR, DWORD ); | |
94 | typedef DWORD (APIENTRY * RASHANGUP)( HRASCONN ); | |
95 | typedef DWORD (APIENTRY * RASGETPROJECTIONINFO)( HRASCONN, RASPROJECTION, LPVOID, LPDWORD ); | |
96 | typedef DWORD (APIENTRY * RASCREATEPHONEBOOKENTRY)( HWND, LPCSTR ); | |
97 | typedef DWORD (APIENTRY * RASEDITPHONEBOOKENTRY)( HWND, LPCSTR, LPCSTR ); | |
98 | typedef DWORD (APIENTRY * RASSETENTRYDIALPARAMS)( LPCSTR, LPRASDIALPARAMSA, BOOL ); | |
99 | typedef DWORD (APIENTRY * RASGETENTRYDIALPARAMS)( LPCSTR, LPRASDIALPARAMSA, LPBOOL ); | |
100 | typedef DWORD (APIENTRY * RASENUMDEVICES)( LPRASDEVINFOA, LPDWORD, LPDWORD ); | |
101 | typedef DWORD (APIENTRY * RASGETCOUNTRYINFO)( LPRASCTRYINFOA, LPDWORD ); | |
102 | typedef DWORD (APIENTRY * RASGETENTRYPROPERTIES)( LPCSTR, LPCSTR, LPRASENTRYA, LPDWORD, LPBYTE, LPDWORD ); | |
103 | typedef DWORD (APIENTRY * RASSETENTRYPROPERTIES)( LPCSTR, LPCSTR, LPRASENTRYA, DWORD, LPBYTE, DWORD ); | |
104 | typedef DWORD (APIENTRY * RASRENAMEENTRY)( LPCSTR, LPCSTR, LPCSTR ); | |
105 | typedef DWORD (APIENTRY * RASDELETEENTRY)( LPCSTR, LPCSTR ); | |
106 | typedef DWORD (APIENTRY * RASVALIDATEENTRYNAME)( LPCSTR, LPCSTR ); | |
107 | typedef DWORD (APIENTRY * RASCONNECTIONNOTIFICATION)( HRASCONN, HANDLE, DWORD ); | |
a0b4c98b | 108 | |
f6bcfd97 | 109 | static const wxChar gs_funcSuffix = _T('A'); |
a0b4c98b | 110 | #else // Unicode |
2690830e VZ |
111 | typedef DWORD (APIENTRY * RASDIAL)( LPRASDIALEXTENSIONS, LPCWSTR, LPRASDIALPARAMSW, DWORD, LPVOID, LPHRASCONN ); |
112 | typedef DWORD (APIENTRY * RASENUMCONNECTIONS)( LPRASCONNW, LPDWORD, LPDWORD ); | |
113 | typedef DWORD (APIENTRY * RASENUMENTRIES)( LPCWSTR, LPCWSTR, LPRASENTRYNAMEW, LPDWORD, LPDWORD ); | |
114 | typedef DWORD (APIENTRY * RASGETCONNECTSTATUS)( HRASCONN, LPRASCONNSTATUSW ); | |
115 | typedef DWORD (APIENTRY * RASGETERRORSTRING)( UINT, LPWSTR, DWORD ); | |
116 | typedef DWORD (APIENTRY * RASHANGUP)( HRASCONN ); | |
117 | typedef DWORD (APIENTRY * RASGETPROJECTIONINFO)( HRASCONN, RASPROJECTION, LPVOID, LPDWORD ); | |
118 | typedef DWORD (APIENTRY * RASCREATEPHONEBOOKENTRY)( HWND, LPCWSTR ); | |
119 | typedef DWORD (APIENTRY * RASEDITPHONEBOOKENTRY)( HWND, LPCWSTR, LPCWSTR ); | |
120 | typedef DWORD (APIENTRY * RASSETENTRYDIALPARAMS)( LPCWSTR, LPRASDIALPARAMSW, BOOL ); | |
121 | typedef DWORD (APIENTRY * RASGETENTRYDIALPARAMS)( LPCWSTR, LPRASDIALPARAMSW, LPBOOL ); | |
122 | typedef DWORD (APIENTRY * RASENUMDEVICES)( LPRASDEVINFOW, LPDWORD, LPDWORD ); | |
123 | typedef DWORD (APIENTRY * RASGETCOUNTRYINFO)( LPRASCTRYINFOW, LPDWORD ); | |
124 | typedef DWORD (APIENTRY * RASGETENTRYPROPERTIES)( LPCWSTR, LPCWSTR, LPRASENTRYW, LPDWORD, LPBYTE, LPDWORD ); | |
125 | typedef DWORD (APIENTRY * RASSETENTRYPROPERTIES)( LPCWSTR, LPCWSTR, LPRASENTRYW, DWORD, LPBYTE, DWORD ); | |
126 | typedef DWORD (APIENTRY * RASRENAMEENTRY)( LPCWSTR, LPCWSTR, LPCWSTR ); | |
127 | typedef DWORD (APIENTRY * RASDELETEENTRY)( LPCWSTR, LPCWSTR ); | |
128 | typedef DWORD (APIENTRY * RASVALIDATEENTRYNAME)( LPCWSTR, LPCWSTR ); | |
129 | typedef DWORD (APIENTRY * RASCONNECTIONNOTIFICATION)( HRASCONN, HANDLE, DWORD ); | |
a0b4c98b | 130 | |
f6bcfd97 | 131 | static const wxChar gs_funcSuffix = _T('W'); |
a0b4c98b VZ |
132 | #endif // ASCII/Unicode |
133 | ||
134 | // structure passed to the secondary thread | |
3b415ba4 | 135 | struct WXDLLEXPORT wxRasThreadData |
a0b4c98b VZ |
136 | { |
137 | wxRasThreadData() | |
138 | { | |
139 | hWnd = 0; | |
140 | hEventRas = hEventQuit = INVALID_HANDLE_VALUE; | |
141 | dialUpManager = NULL; | |
142 | } | |
143 | ||
144 | HWND hWnd; // window to send notifications to | |
145 | HANDLE hEventRas, // event which RAS signals when status changes | |
146 | hEventQuit; // event which we signal when we terminate | |
147 | ||
148 | class WXDLLEXPORT wxDialUpManagerMSW *dialUpManager; // the owner | |
149 | }; | |
150 | ||
151 | // ---------------------------------------------------------------------------- | |
152 | // wxDialUpManager class for MSW | |
153 | // ---------------------------------------------------------------------------- | |
154 | ||
155 | class WXDLLEXPORT wxDialUpManagerMSW : public wxDialUpManager | |
156 | { | |
157 | public: | |
158 | // ctor & dtor | |
159 | wxDialUpManagerMSW(); | |
160 | virtual ~wxDialUpManagerMSW(); | |
161 | ||
162 | // implement base class pure virtuals | |
163 | virtual bool IsOk() const; | |
2690830e | 164 | virtual size_t GetISPNames(wxArrayString& names) const; |
a0b4c98b VZ |
165 | virtual bool Dial(const wxString& nameOfISP, |
166 | const wxString& username, | |
167 | const wxString& password, | |
168 | bool async); | |
169 | virtual bool IsDialing() const; | |
170 | virtual bool CancelDialing(); | |
171 | virtual bool HangUp(); | |
2690830e | 172 | virtual bool IsAlwaysOnline() const; |
a0b4c98b VZ |
173 | virtual bool IsOnline() const; |
174 | virtual void SetOnlineStatus(bool isOnline = TRUE); | |
175 | virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds); | |
176 | virtual void DisableAutoCheckOnlineStatus(); | |
177 | virtual void SetWellKnownHost(const wxString& hostname, int port); | |
178 | virtual void SetConnectCommand(const wxString& commandDial, | |
179 | const wxString& commandHangup); | |
180 | ||
181 | // for RasTimer | |
182 | void CheckRasStatus(); | |
183 | ||
184 | // for wxRasStatusWindowProc | |
185 | void OnConnectStatusChange(); | |
2690830e | 186 | void OnDialProgress(RASCONNSTATE rasconnstate, DWORD dwError); |
a0b4c98b VZ |
187 | |
188 | // for wxRasDialFunc | |
2690830e | 189 | static HWND GetRasWindow() { return ms_hwndRas; } |
a0b4c98b VZ |
190 | static wxDialUpManagerMSW *GetDialer() { return ms_dialer; } |
191 | ||
192 | private: | |
193 | // return the error string for the given RAS error code | |
194 | static wxString GetErrorString(DWORD error); | |
195 | ||
196 | // find the (first) handle of the active connection | |
197 | static HRASCONN FindActiveConnection(); | |
198 | ||
199 | // notify the application about status change | |
200 | void NotifyApp(bool connected, bool fromOurselves = FALSE) const; | |
201 | ||
202 | // destroy the thread data and the thread itself | |
203 | void CleanUpThreadData(); | |
204 | ||
205 | // timer used for polling RAS status | |
3b415ba4 | 206 | class WXDLLEXPORT RasTimer : public wxTimer |
a0b4c98b VZ |
207 | { |
208 | public: | |
209 | RasTimer(wxDialUpManagerMSW *dialUpManager) | |
210 | { m_dialUpManager = dialUpManager; } | |
211 | ||
212 | virtual void Notify() { m_dialUpManager->CheckRasStatus(); } | |
213 | ||
214 | private: | |
215 | wxDialUpManagerMSW *m_dialUpManager; | |
216 | } m_timerStatusPolling; | |
217 | ||
218 | // thread handle for the thread sitting on connection change event | |
219 | HANDLE m_hThread; | |
220 | ||
221 | // data used by this thread and our hidden window to send messages between | |
222 | // each other | |
223 | wxRasThreadData m_data; | |
224 | ||
2690830e VZ |
225 | // the hidden window we use for passing messages between threads |
226 | static HWND ms_hwndRas; | |
227 | ||
a0b4c98b VZ |
228 | // the handle of the connection we initiated or 0 if none |
229 | static HRASCONN ms_hRasConnection; | |
230 | ||
231 | // the use count of rasapi32.dll | |
232 | static int ms_nDllCount; | |
233 | ||
234 | // the handle of rasapi32.dll when it's loaded | |
235 | static wxDllType ms_dllRas; | |
236 | ||
237 | // the pointers to RAS functions | |
238 | static RASDIAL ms_pfnRasDial; | |
239 | static RASENUMCONNECTIONS ms_pfnRasEnumConnections; | |
240 | static RASENUMENTRIES ms_pfnRasEnumEntries; | |
241 | static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus; | |
242 | static RASGETERRORSTRING ms_pfnRasGetErrorString; | |
243 | static RASHANGUP ms_pfnRasHangUp; | |
244 | static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo; | |
245 | static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry; | |
246 | static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry; | |
247 | static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams; | |
248 | static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams; | |
249 | static RASENUMDEVICES ms_pfnRasEnumDevices; | |
250 | static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo; | |
251 | static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties; | |
252 | static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties; | |
253 | static RASRENAMEENTRY ms_pfnRasRenameEntry; | |
254 | static RASDELETEENTRY ms_pfnRasDeleteEntry; | |
255 | static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName; | |
256 | ||
257 | // this function is not supported by Win95 | |
258 | static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification; | |
259 | ||
260 | // if this flag is different from -1, it overrides IsOnline() | |
261 | static int ms_userSpecifiedOnlineStatus; | |
262 | ||
263 | // this flag tells us if we're online | |
264 | static int ms_isConnected; | |
265 | ||
2690830e VZ |
266 | // this flag is the result of the call to IsAlwaysOnline() (-1 if not |
267 | // called yet) | |
268 | static int ms_isAlwaysOnline; | |
269 | ||
a0b4c98b VZ |
270 | // this flag tells us whether a call to RasDial() is in progress |
271 | static wxDialUpManagerMSW *ms_dialer; | |
272 | }; | |
273 | ||
274 | // ---------------------------------------------------------------------------- | |
275 | // private functions | |
276 | // ---------------------------------------------------------------------------- | |
277 | ||
278 | static LRESULT WINAPI wxRasStatusWindowProc(HWND hWnd, UINT message, | |
279 | WPARAM wParam, LPARAM lParam); | |
280 | ||
281 | static DWORD wxRasMonitorThread(wxRasThreadData *data); | |
282 | ||
283 | static void WINAPI wxRasDialFunc(UINT unMsg, | |
284 | RASCONNSTATE rasconnstate, | |
285 | DWORD dwError); | |
286 | ||
287 | // ============================================================================ | |
288 | // implementation | |
289 | // ============================================================================ | |
290 | ||
291 | // ---------------------------------------------------------------------------- | |
292 | // init the static variables | |
293 | // ---------------------------------------------------------------------------- | |
294 | ||
295 | HRASCONN wxDialUpManagerMSW::ms_hRasConnection = 0; | |
296 | ||
2690830e VZ |
297 | HWND wxDialUpManagerMSW::ms_hwndRas = 0; |
298 | ||
a0b4c98b VZ |
299 | int wxDialUpManagerMSW::ms_nDllCount = 0; |
300 | wxDllType wxDialUpManagerMSW::ms_dllRas = 0; | |
301 | ||
302 | RASDIAL wxDialUpManagerMSW::ms_pfnRasDial = 0; | |
303 | RASENUMCONNECTIONS wxDialUpManagerMSW::ms_pfnRasEnumConnections = 0; | |
304 | RASENUMENTRIES wxDialUpManagerMSW::ms_pfnRasEnumEntries = 0; | |
305 | RASGETCONNECTSTATUS wxDialUpManagerMSW::ms_pfnRasGetConnectStatus = 0; | |
306 | RASGETERRORSTRING wxDialUpManagerMSW::ms_pfnRasGetErrorString = 0; | |
307 | RASHANGUP wxDialUpManagerMSW::ms_pfnRasHangUp = 0; | |
308 | RASGETPROJECTIONINFO wxDialUpManagerMSW::ms_pfnRasGetProjectionInfo = 0; | |
309 | RASCREATEPHONEBOOKENTRY wxDialUpManagerMSW::ms_pfnRasCreatePhonebookEntry = 0; | |
310 | RASEDITPHONEBOOKENTRY wxDialUpManagerMSW::ms_pfnRasEditPhonebookEntry = 0; | |
311 | RASSETENTRYDIALPARAMS wxDialUpManagerMSW::ms_pfnRasSetEntryDialParams = 0; | |
312 | RASGETENTRYDIALPARAMS wxDialUpManagerMSW::ms_pfnRasGetEntryDialParams = 0; | |
313 | RASENUMDEVICES wxDialUpManagerMSW::ms_pfnRasEnumDevices = 0; | |
314 | RASGETCOUNTRYINFO wxDialUpManagerMSW::ms_pfnRasGetCountryInfo = 0; | |
315 | RASGETENTRYPROPERTIES wxDialUpManagerMSW::ms_pfnRasGetEntryProperties = 0; | |
316 | RASSETENTRYPROPERTIES wxDialUpManagerMSW::ms_pfnRasSetEntryProperties = 0; | |
317 | RASRENAMEENTRY wxDialUpManagerMSW::ms_pfnRasRenameEntry = 0; | |
318 | RASDELETEENTRY wxDialUpManagerMSW::ms_pfnRasDeleteEntry = 0; | |
319 | RASVALIDATEENTRYNAME wxDialUpManagerMSW::ms_pfnRasValidateEntryName = 0; | |
320 | RASCONNECTIONNOTIFICATION wxDialUpManagerMSW::ms_pfnRasConnectionNotification = 0; | |
321 | ||
322 | int wxDialUpManagerMSW::ms_userSpecifiedOnlineStatus = -1; | |
323 | int wxDialUpManagerMSW::ms_isConnected = -1; | |
2690830e | 324 | int wxDialUpManagerMSW::ms_isAlwaysOnline = -1; |
a0b4c98b VZ |
325 | wxDialUpManagerMSW *wxDialUpManagerMSW::ms_dialer = NULL; |
326 | ||
327 | // ---------------------------------------------------------------------------- | |
328 | // ctor and dtor: the dynamic linking happens here | |
329 | // ---------------------------------------------------------------------------- | |
330 | ||
331 | // the static creator function is implemented here | |
332 | wxDialUpManager *wxDialUpManager::Create() | |
333 | { | |
334 | return new wxDialUpManagerMSW; | |
335 | } | |
336 | ||
337 | #ifdef __VISUALC__ | |
338 | // warning about "'this' : used in base member initializer list" - so what? | |
339 | #pragma warning(disable:4355) | |
340 | #endif // VC++ | |
341 | ||
342 | wxDialUpManagerMSW::wxDialUpManagerMSW() | |
343 | : m_timerStatusPolling(this) | |
344 | { | |
345 | // initialize our data | |
346 | m_hThread = 0; | |
347 | ||
348 | if ( !ms_nDllCount++ ) | |
349 | { | |
350 | // load the RAS library | |
351 | ms_dllRas = wxDllLoader::LoadLibrary("RASAPI32"); | |
352 | if ( !ms_dllRas ) | |
353 | { | |
f6bcfd97 | 354 | wxLogError(_("Dial up functions are unavailable because the remote access service (RAS) is not installed on this machine. Please install it.")); |
a0b4c98b VZ |
355 | } |
356 | else | |
357 | { | |
358 | // resolve the functions we need | |
359 | ||
360 | // this will contain the name of the function we failed to resolve | |
361 | // if any at the end | |
362 | const char *funcName = NULL; | |
363 | ||
364 | // get the function from rasapi32.dll and abort if it's not found | |
365 | #define RESOLVE_RAS_FUNCTION(type, name) \ | |
366 | ms_pfn##name = (type)wxDllLoader::GetSymbol(ms_dllRas, \ | |
2690830e | 367 | wxString(_T(#name)) + gs_funcSuffix); \ |
a0b4c98b VZ |
368 | if ( !ms_pfn##name ) \ |
369 | { \ | |
370 | funcName = #name; \ | |
371 | goto exit; \ | |
372 | } | |
373 | ||
374 | // a variant of above macro which doesn't abort if the function is | |
375 | // not found in the DLL | |
376 | #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \ | |
377 | ms_pfn##name = (type)wxDllLoader::GetSymbol(ms_dllRas, \ | |
2690830e | 378 | wxString(_T(#name)) + gs_funcSuffix); |
a0b4c98b VZ |
379 | |
380 | RESOLVE_RAS_FUNCTION(RASDIAL, RasDial); | |
381 | RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS, RasEnumConnections); | |
382 | RESOLVE_RAS_FUNCTION(RASENUMENTRIES, RasEnumEntries); | |
383 | RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS, RasGetConnectStatus); | |
384 | RESOLVE_RAS_FUNCTION(RASGETERRORSTRING, RasGetErrorString); | |
385 | RESOLVE_RAS_FUNCTION(RASHANGUP, RasHangUp); | |
2690830e | 386 | RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS, RasGetEntryDialParams); |
a0b4c98b VZ |
387 | |
388 | // suppress wxDllLoader messages about missing (non essential) | |
389 | // functions | |
390 | { | |
391 | wxLogNull noLog; | |
392 | ||
393 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO, RasGetProjectionInfo); | |
394 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY, RasCreatePhonebookEntry); | |
395 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY, RasEditPhonebookEntry); | |
396 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS, RasSetEntryDialParams); | |
a0b4c98b VZ |
397 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES, RasGetEntryProperties); |
398 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES, RasSetEntryProperties); | |
399 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY, RasRenameEntry); | |
400 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY, RasDeleteEntry); | |
401 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME, RasValidateEntryName); | |
402 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO, RasGetCountryInfo); | |
403 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES, RasEnumDevices); | |
404 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION, RasConnectionNotification); | |
405 | } | |
406 | ||
407 | // keep your preprocessor name space clean | |
408 | #undef RESOLVE_RAS_FUNCTION | |
409 | #undef RESOLVE_OPTIONAL_RAS_FUNCTION | |
410 | ||
411 | exit: | |
412 | if ( funcName ) | |
413 | { | |
f6bcfd97 BP |
414 | static const wxChar *msg = wxTRANSLATE( |
415 | "The version of remote access service (RAS) installed on this machine is too\ | |
416 | old, please upgrade (the following required function is missing: %s)." | |
417 | ); | |
418 | ||
419 | wxLogError(wxGetTranslation(msg), funcName); | |
a0b4c98b VZ |
420 | |
421 | wxDllLoader::UnloadLibrary(ms_dllRas); | |
422 | ms_dllRas = 0; | |
423 | ms_nDllCount = 0; | |
424 | ||
425 | return; | |
426 | } | |
427 | } | |
428 | } | |
429 | ||
430 | // enable auto check by default | |
431 | EnableAutoCheckOnlineStatus(0); | |
432 | } | |
433 | ||
434 | wxDialUpManagerMSW::~wxDialUpManagerMSW() | |
435 | { | |
436 | CleanUpThreadData(); | |
437 | ||
438 | if ( !--ms_nDllCount ) | |
439 | { | |
440 | // unload the RAS library | |
441 | wxDllLoader::UnloadLibrary(ms_dllRas); | |
442 | ms_dllRas = 0; | |
443 | } | |
444 | } | |
445 | ||
446 | // ---------------------------------------------------------------------------- | |
447 | // helper functions | |
448 | // ---------------------------------------------------------------------------- | |
449 | ||
450 | wxString wxDialUpManagerMSW::GetErrorString(DWORD error) | |
451 | { | |
452 | wxChar buffer[512]; // this should be more than enough according to MS docs | |
453 | DWORD dwRet = ms_pfnRasGetErrorString(error, buffer, WXSIZEOF(buffer)); | |
454 | switch ( dwRet ) | |
455 | { | |
456 | case ERROR_INVALID_PARAMETER: | |
457 | // this was a standard Win32 error probably | |
458 | return wxString(wxSysErrorMsg(error)); | |
459 | ||
460 | default: | |
461 | { | |
f6bcfd97 BP |
462 | wxLogSysError(dwRet, |
463 | _("Failed to retrieve text of RAS error message")); | |
a0b4c98b VZ |
464 | |
465 | wxString msg; | |
466 | msg.Printf(_("unknown error (error code %08x)."), error); | |
467 | return msg; | |
468 | } | |
469 | ||
470 | case 0: | |
471 | // we want the error message to start from a lower case letter | |
472 | buffer[0] = wxTolower(buffer[0]); | |
473 | ||
474 | return wxString(buffer); | |
475 | } | |
476 | } | |
477 | ||
478 | HRASCONN wxDialUpManagerMSW::FindActiveConnection() | |
479 | { | |
480 | // enumerate connections | |
481 | DWORD cbBuf = sizeof(RASCONN); | |
482 | LPRASCONN lpRasConn = (LPRASCONN)malloc(cbBuf); | |
483 | if ( !lpRasConn ) | |
484 | { | |
485 | // out of memory | |
486 | return 0; | |
487 | } | |
488 | ||
489 | lpRasConn->dwSize = sizeof(RASCONN); | |
490 | ||
491 | DWORD nConnections = 0; | |
492 | DWORD dwRet = ERROR_BUFFER_TOO_SMALL; | |
493 | ||
494 | while ( dwRet == ERROR_BUFFER_TOO_SMALL ) | |
495 | { | |
496 | dwRet = ms_pfnRasEnumConnections(lpRasConn, &cbBuf, &nConnections); | |
497 | ||
498 | if ( dwRet == ERROR_BUFFER_TOO_SMALL ) | |
499 | { | |
500 | LPRASCONN lpRasConnOld = lpRasConn; | |
501 | lpRasConn = (LPRASCONN)realloc(lpRasConn, cbBuf); | |
502 | if ( !lpRasConn ) | |
503 | { | |
504 | // out of memory | |
505 | free(lpRasConnOld); | |
506 | ||
507 | return 0; | |
508 | } | |
509 | } | |
510 | else if ( dwRet == 0 ) | |
511 | { | |
512 | // ok, success | |
513 | break; | |
514 | } | |
515 | else | |
516 | { | |
517 | // an error occured | |
518 | wxLogError(_("Cannot find active dialup connection: %s"), | |
519 | GetErrorString(dwRet)); | |
520 | return 0; | |
521 | } | |
522 | } | |
523 | ||
524 | HRASCONN hrasconn; | |
525 | ||
526 | switch ( nConnections ) | |
527 | { | |
528 | case 0: | |
529 | // no connections | |
530 | hrasconn = 0; | |
531 | break; | |
532 | ||
533 | default: | |
534 | // more than 1 connection - we don't know what to do with this | |
535 | // case, so give a warning but continue (taking the first | |
536 | // connection) - the warning is really needed because this function | |
537 | // is used, for example, to select the connection to hang up and so | |
538 | // we may hang up the wrong connection here... | |
f6bcfd97 | 539 | wxLogWarning(_("Several active dialup connections found, choosing one randomly.")); |
a0b4c98b VZ |
540 | // fall through |
541 | ||
542 | case 1: | |
543 | // exactly 1 connection, great | |
544 | hrasconn = lpRasConn->hrasconn; | |
545 | } | |
546 | ||
547 | free(lpRasConn); | |
548 | ||
549 | return hrasconn; | |
550 | } | |
551 | ||
552 | void wxDialUpManagerMSW::CleanUpThreadData() | |
553 | { | |
554 | if ( m_hThread ) | |
555 | { | |
556 | if ( !SetEvent(m_data.hEventQuit) ) | |
557 | { | |
fcec6429 | 558 | wxLogLastError(_T("SetEvent(RasThreadQuit)")); |
a0b4c98b VZ |
559 | } |
560 | ||
561 | CloseHandle(m_hThread); | |
562 | ||
563 | m_hThread = 0; | |
564 | } | |
565 | ||
566 | if ( m_data.hWnd ) | |
567 | { | |
568 | DestroyWindow(m_data.hWnd); | |
569 | ||
570 | m_data.hWnd = 0; | |
571 | } | |
572 | ||
573 | if ( m_data.hEventQuit ) | |
574 | { | |
575 | CloseHandle(m_data.hEventQuit); | |
576 | ||
577 | m_data.hEventQuit = 0; | |
578 | } | |
579 | ||
580 | if ( m_data.hEventRas ) | |
581 | { | |
582 | CloseHandle(m_data.hEventRas); | |
583 | ||
584 | m_data.hEventRas = 0; | |
585 | } | |
586 | } | |
587 | ||
588 | // ---------------------------------------------------------------------------- | |
589 | // connection status | |
590 | // ---------------------------------------------------------------------------- | |
591 | ||
592 | void wxDialUpManagerMSW::CheckRasStatus() | |
593 | { | |
594 | // use int, not bool to compare with -1 | |
595 | int isConnected = FindActiveConnection() != 0; | |
596 | if ( isConnected != ms_isConnected ) | |
597 | { | |
598 | if ( ms_isConnected != -1 ) | |
599 | { | |
600 | // notify the program | |
601 | NotifyApp(isConnected != 0); | |
602 | } | |
603 | // else: it's the first time we're called, just update the flag | |
604 | ||
605 | ms_isConnected = isConnected; | |
606 | } | |
607 | } | |
608 | ||
609 | void wxDialUpManagerMSW::NotifyApp(bool connected, bool fromOurselves) const | |
610 | { | |
611 | wxDialUpEvent event(connected, fromOurselves); | |
612 | (void)wxTheApp->ProcessEvent(event); | |
613 | } | |
614 | ||
615 | // this function is called whenever the status of any RAS connection on this | |
616 | // machine changes by RAS itself | |
617 | void wxDialUpManagerMSW::OnConnectStatusChange() | |
618 | { | |
619 | // we know that status changed, but we don't know whether we're connected | |
620 | // or not - so find it out | |
621 | CheckRasStatus(); | |
622 | } | |
623 | ||
624 | // this function is called by our callback which we give to RasDial() when | |
625 | // calling it asynchronously | |
626 | void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate, | |
627 | DWORD dwError) | |
628 | { | |
629 | if ( !GetDialer() ) | |
630 | { | |
631 | // this probably means that CancelDialing() was called and we get | |
632 | // "disconnected" notification | |
633 | return; | |
634 | } | |
635 | ||
636 | // we're only interested in 2 events: connected and disconnected | |
637 | if ( dwError ) | |
638 | { | |
639 | wxLogError(_("Failed to establish dialup connection: %s"), | |
640 | GetErrorString(dwError)); | |
641 | ||
642 | // we should still call RasHangUp() if we got a non 0 connection | |
643 | if ( ms_hRasConnection ) | |
644 | { | |
645 | ms_pfnRasHangUp(ms_hRasConnection); | |
646 | ms_hRasConnection = 0; | |
647 | } | |
648 | ||
649 | ms_dialer = NULL; | |
650 | ||
651 | NotifyApp(FALSE /* !connected */, TRUE /* we dialed ourselves */); | |
652 | } | |
653 | else if ( rasconnstate == RASCS_Connected ) | |
654 | { | |
655 | ms_isConnected = TRUE; | |
656 | ms_dialer = NULL; | |
657 | ||
658 | NotifyApp(TRUE /* connected */, TRUE /* we dialed ourselves */); | |
659 | } | |
660 | } | |
661 | ||
662 | // ---------------------------------------------------------------------------- | |
663 | // implementation of wxDialUpManager functions | |
664 | // ---------------------------------------------------------------------------- | |
665 | ||
666 | bool wxDialUpManagerMSW::IsOk() const | |
667 | { | |
668 | return ms_dllRas != 0; | |
669 | } | |
670 | ||
2690830e VZ |
671 | size_t wxDialUpManagerMSW::GetISPNames(wxArrayString& names) const |
672 | { | |
673 | // fetch the entries | |
674 | DWORD size = sizeof(RASENTRYNAME); | |
675 | RASENTRYNAME *rasEntries = (RASENTRYNAME *)malloc(size); | |
676 | rasEntries->dwSize = sizeof(RASENTRYNAME); | |
677 | ||
678 | DWORD nEntries; | |
679 | DWORD dwRet; | |
680 | do | |
681 | { | |
682 | dwRet = ms_pfnRasEnumEntries | |
683 | ( | |
684 | NULL, // reserved | |
685 | NULL, // default phone book (or all) | |
686 | rasEntries, // [out] buffer for the entries | |
687 | &size, // [in/out] size of the buffer | |
688 | &nEntries // [out] number of entries fetched | |
689 | ); | |
690 | ||
691 | if ( dwRet == ERROR_BUFFER_TOO_SMALL ) | |
692 | { | |
693 | // reallocate the buffer | |
694 | rasEntries = (RASENTRYNAME *)realloc(rasEntries, size); | |
695 | } | |
696 | else if ( dwRet != 0 ) | |
697 | { | |
698 | // some other error - abort | |
699 | wxLogError(_("Failed to get ISP names: %s"), GetErrorString(dwRet)); | |
700 | ||
701 | free(rasEntries); | |
702 | ||
703 | return 0u; | |
704 | } | |
705 | } | |
706 | while ( dwRet != 0 ); | |
707 | ||
708 | // process them | |
709 | names.Empty(); | |
710 | for ( size_t n = 0; n < (size_t)nEntries; n++ ) | |
711 | { | |
712 | names.Add(rasEntries[n].szEntryName); | |
713 | } | |
714 | ||
715 | free(rasEntries); | |
716 | ||
717 | // return the number of entries | |
718 | return names.GetCount(); | |
719 | } | |
720 | ||
a0b4c98b VZ |
721 | bool wxDialUpManagerMSW::Dial(const wxString& nameOfISP, |
722 | const wxString& username, | |
723 | const wxString& password, | |
724 | bool async) | |
725 | { | |
2690830e | 726 | // check preconditions |
223d09f6 | 727 | wxCHECK_MSG( IsOk(), FALSE, wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
728 | |
729 | if ( ms_hRasConnection ) | |
730 | { | |
223d09f6 | 731 | wxFAIL_MSG(wxT("there is already an active connection")); |
a0b4c98b VZ |
732 | |
733 | return TRUE; | |
734 | } | |
735 | ||
2690830e VZ |
736 | // get the default ISP if none given |
737 | wxString entryName(nameOfISP); | |
738 | if ( !entryName ) | |
739 | { | |
740 | wxArrayString names; | |
741 | size_t count = GetISPNames(names); | |
742 | switch ( count ) | |
743 | { | |
744 | case 0: | |
745 | // no known ISPs, abort | |
746 | wxLogError(_("Failed to connect: no ISP to dial.")); | |
747 | ||
748 | return FALSE; | |
749 | ||
750 | case 1: | |
751 | // only one ISP, choose it | |
752 | entryName = names[0u]; | |
753 | break; | |
754 | ||
755 | default: | |
756 | // several ISPs, let the user choose | |
757 | { | |
758 | wxString *strings = new wxString[count]; | |
759 | for ( size_t i = 0; i < count; i++ ) | |
760 | { | |
761 | strings[i] = names[i]; | |
762 | } | |
763 | ||
764 | entryName = wxGetSingleChoice | |
765 | ( | |
766 | _("Choose ISP to dial"), | |
f6bcfd97 | 767 | _("Please choose which ISP do you want to connect to"), |
2690830e VZ |
768 | count, |
769 | strings | |
770 | ); | |
771 | ||
772 | delete [] strings; | |
773 | ||
774 | if ( !entryName ) | |
775 | { | |
776 | // cancelled by user | |
777 | return FALSE; | |
778 | } | |
779 | } | |
780 | } | |
781 | } | |
782 | ||
a0b4c98b VZ |
783 | RASDIALPARAMS rasDialParams; |
784 | rasDialParams.dwSize = sizeof(rasDialParams); | |
f6bcfd97 | 785 | wxStrncpy(rasDialParams.szEntryName, entryName, RAS_MaxEntryName); |
2690830e VZ |
786 | |
787 | // do we have the username and password? | |
788 | if ( !username || !password ) | |
789 | { | |
790 | BOOL gotPassword; | |
791 | DWORD dwRet = ms_pfnRasGetEntryDialParams | |
792 | ( | |
793 | NULL, // default phonebook | |
794 | &rasDialParams, // [in/out] the params of this entry | |
795 | &gotPassword // [out] did we get password? | |
796 | ); | |
797 | ||
798 | if ( dwRet != 0 ) | |
799 | { | |
800 | wxLogError(_("Failed to connect: missing username/password.")); | |
801 | ||
802 | return FALSE; | |
803 | } | |
804 | } | |
805 | else | |
806 | { | |
f6bcfd97 BP |
807 | wxStrncpy(rasDialParams.szUserName, username, UNLEN); |
808 | wxStrncpy(rasDialParams.szPassword, password, PWLEN); | |
2690830e VZ |
809 | } |
810 | ||
811 | // default values for other fields | |
a0b4c98b VZ |
812 | rasDialParams.szPhoneNumber[0] = '\0'; |
813 | rasDialParams.szCallbackNumber[0] = '\0'; | |
814 | rasDialParams.szCallbackNumber[0] = '\0'; | |
815 | ||
a0b4c98b VZ |
816 | rasDialParams.szDomain[0] = '*'; |
817 | rasDialParams.szDomain[1] = '\0'; | |
818 | ||
819 | // apparently, this is not really necessary - passing NULL instead of the | |
820 | // phone book has the same effect | |
821 | #if 0 | |
822 | wxString phoneBook; | |
823 | if ( wxGetOsVersion() == wxWINDOWS_NT ) | |
824 | { | |
825 | // first get the length | |
826 | UINT nLen = ::GetSystemDirectory(NULL, 0); | |
827 | nLen++; | |
828 | ||
829 | if ( !::GetSystemDirectory(phoneBook.GetWriteBuf(nLen), nLen) ) | |
830 | { | |
831 | wxLogSysError(_("Cannot find the location of address book file")); | |
832 | } | |
833 | ||
834 | phoneBook.UngetWriteBuf(); | |
835 | ||
836 | // this is the default phone book | |
837 | phoneBook << "\\ras\\rasphone.pbk"; | |
838 | } | |
839 | #endif // 0 | |
840 | ||
841 | // TODO may be we should disable auto check while async dialing is in | |
842 | // progress? | |
843 | ||
844 | ms_dialer = this; | |
845 | ||
846 | DWORD dwRet = ms_pfnRasDial | |
847 | ( | |
848 | (LPRASDIALEXTENSIONS)NULL, // no extended features | |
849 | NULL, // default phone book file (NT only) | |
850 | &rasDialParams, | |
851 | 0, // use callback for notifications | |
852 | async ? wxRasDialFunc // the callback | |
853 | : 0, // no notifications - sync operation | |
854 | &ms_hRasConnection | |
855 | ); | |
856 | ||
857 | if ( dwRet != 0 ) | |
858 | { | |
859 | wxLogError(_("Failed to %s dialup connection: %s"), | |
860 | async ? _("initiate") : _("establish"), | |
861 | GetErrorString(dwRet)); | |
862 | ||
863 | // we should still call RasHangUp() if we got a non 0 connection | |
864 | if ( ms_hRasConnection ) | |
865 | { | |
866 | ms_pfnRasHangUp(ms_hRasConnection); | |
867 | ms_hRasConnection = 0; | |
868 | } | |
869 | ||
870 | ms_dialer = NULL; | |
871 | ||
872 | return FALSE; | |
873 | } | |
874 | ||
875 | // for async dialing, we're not yet connected | |
876 | if ( !async ) | |
877 | { | |
878 | ms_isConnected = TRUE; | |
879 | } | |
880 | ||
881 | return TRUE; | |
882 | } | |
883 | ||
884 | bool wxDialUpManagerMSW::IsDialing() const | |
885 | { | |
886 | return GetDialer() != NULL; | |
887 | } | |
888 | ||
889 | bool wxDialUpManagerMSW::CancelDialing() | |
890 | { | |
891 | if ( !GetDialer() ) | |
892 | { | |
893 | // silently ignore | |
894 | return FALSE; | |
895 | } | |
896 | ||
223d09f6 | 897 | wxASSERT_MSG( ms_hRasConnection, wxT("dialing but no connection?") ); |
a0b4c98b VZ |
898 | |
899 | ms_dialer = NULL; | |
900 | ||
901 | return HangUp(); | |
902 | } | |
903 | ||
904 | bool wxDialUpManagerMSW::HangUp() | |
905 | { | |
223d09f6 | 906 | wxCHECK_MSG( IsOk(), FALSE, wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
907 | |
908 | // we may terminate either the connection we initiated or another one which | |
909 | // is active now | |
910 | HRASCONN hRasConn; | |
911 | if ( ms_hRasConnection ) | |
912 | { | |
913 | hRasConn = ms_hRasConnection; | |
914 | ||
915 | ms_hRasConnection = 0; | |
916 | } | |
917 | else | |
918 | { | |
919 | hRasConn = FindActiveConnection(); | |
920 | } | |
921 | ||
922 | if ( !hRasConn ) | |
923 | { | |
924 | wxLogError(_("Cannot hang up - no active dialup connection.")); | |
925 | ||
926 | return FALSE; | |
927 | } | |
928 | ||
929 | DWORD dwRet = ms_pfnRasHangUp(hRasConn); | |
930 | if ( dwRet != 0 ) | |
931 | { | |
932 | wxLogError(_("Failed to terminate the dialup connection: %s"), | |
933 | GetErrorString(dwRet)); | |
934 | } | |
935 | ||
936 | ms_isConnected = FALSE; | |
937 | ||
938 | return TRUE; | |
939 | } | |
940 | ||
2690830e VZ |
941 | bool wxDialUpManagerMSW::IsAlwaysOnline() const |
942 | { | |
943 | // we cache the result (presumably this won't change while the program is | |
944 | // running!) | |
945 | if ( ms_isAlwaysOnline != -1 ) | |
946 | { | |
947 | return ms_isAlwaysOnline != 0; | |
948 | } | |
949 | ||
950 | // try to use WinInet function first | |
951 | bool ok; | |
952 | wxDllType hDll = wxDllLoader::LoadLibrary(_T("WININET"), &ok); | |
953 | if ( ok ) | |
954 | { | |
955 | typedef BOOL (*INTERNETGETCONNECTEDSTATE)(LPDWORD, DWORD); | |
956 | INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState; | |
957 | ||
958 | #define RESOLVE_FUNCTION(type, name) \ | |
959 | pfn##name = (type)wxDllLoader::GetSymbol(hDll, _T(#name)) | |
960 | ||
961 | RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE, InternetGetConnectedState); | |
962 | ||
963 | if ( pfnInternetGetConnectedState ) | |
964 | { | |
965 | DWORD flags = 0; | |
966 | if ( pfnInternetGetConnectedState(&flags, 0 /* reserved */) ) | |
967 | { | |
968 | // there is some connection to the net, see of which type | |
969 | ms_isAlwaysOnline = (flags & INTERNET_CONNECTION_LAN != 0) || | |
970 | (flags & INTERNET_CONNECTION_PROXY != 0); | |
2690830e VZ |
971 | } |
972 | else | |
973 | { | |
974 | // no Internet connection at all | |
975 | ms_isAlwaysOnline = FALSE; | |
976 | } | |
977 | } | |
978 | ||
979 | wxDllLoader::UnloadLibrary(hDll); | |
980 | } | |
981 | ||
982 | // did we succeed with WinInet? if not, try something else | |
983 | if ( ms_isAlwaysOnline == -1 ) | |
984 | { | |
985 | if ( !IsOnline() ) | |
986 | { | |
987 | // definitely no permanent connection because we are not connected | |
988 | // now | |
989 | ms_isAlwaysOnline = FALSE; | |
990 | } | |
991 | else | |
992 | { | |
993 | // of course, having a modem doesn't prevent us from having a | |
994 | // permanent connection as well, but we have to guess somehow and | |
995 | // it's probably more common that a system connected via a modem | |
996 | // doesn't have any other net access, so: | |
997 | ms_isAlwaysOnline = FALSE; | |
998 | } | |
999 | } | |
1000 | ||
223d09f6 | 1001 | wxASSERT_MSG( ms_isAlwaysOnline != -1, wxT("logic error") ); |
2690830e VZ |
1002 | |
1003 | return ms_isAlwaysOnline != 0; | |
1004 | } | |
1005 | ||
a0b4c98b VZ |
1006 | bool wxDialUpManagerMSW::IsOnline() const |
1007 | { | |
223d09f6 | 1008 | wxCHECK_MSG( IsOk(), FALSE, wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1009 | |
1010 | if ( ms_userSpecifiedOnlineStatus != -1 ) | |
1011 | { | |
1012 | // user specified flag overrides our logic | |
1013 | return ms_userSpecifiedOnlineStatus != 0; | |
1014 | } | |
1015 | else | |
1016 | { | |
1017 | // return TRUE if there is at least one active connection | |
1018 | return FindActiveConnection() != 0; | |
1019 | } | |
1020 | } | |
1021 | ||
1022 | void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline) | |
1023 | { | |
223d09f6 | 1024 | wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1025 | |
1026 | ms_userSpecifiedOnlineStatus = isOnline; | |
1027 | } | |
1028 | ||
1029 | bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds) | |
1030 | { | |
223d09f6 | 1031 | wxCHECK_MSG( IsOk(), FALSE, wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1032 | |
1033 | bool ok = ms_pfnRasConnectionNotification != 0; | |
1034 | ||
1035 | if ( ok ) | |
1036 | { | |
1037 | // we're running under NT 4.0, Windows 98 or later and can use | |
1038 | // RasConnectionNotification() to be notified by a secondary thread | |
1039 | ||
1040 | // first, see if we don't have this thread already running | |
1041 | if ( m_hThread != 0 ) | |
1042 | { | |
1043 | DWORD dwSuspendCount = 2; | |
1044 | while ( dwSuspendCount > 1 ) | |
1045 | { | |
1046 | dwSuspendCount = ResumeThread(m_hThread); | |
1047 | if ( dwSuspendCount == (DWORD)-1 ) | |
1048 | { | |
f6bcfd97 | 1049 | wxLogLastError(wxT("ResumeThread(RasThread)")); |
a0b4c98b VZ |
1050 | |
1051 | ok = FALSE; | |
1052 | } | |
1053 | } | |
1054 | ||
1055 | if ( ok ) | |
1056 | { | |
1057 | return TRUE; | |
1058 | } | |
1059 | } | |
1060 | } | |
1061 | ||
1062 | // create all the stuff we need to be notified about RAS connection | |
1063 | // status change | |
1064 | ||
1065 | if ( ok ) | |
1066 | { | |
1067 | // first create an event to wait on | |
1068 | m_data.hEventRas = CreateEvent | |
1069 | ( | |
1070 | NULL, // security attribute (default) | |
1071 | FALSE, // manual reset (not) | |
1072 | FALSE, // initial state (not signaled) | |
1073 | NULL // name (no) | |
1074 | ); | |
1075 | if ( !m_data.hEventRas ) | |
1076 | { | |
f6bcfd97 | 1077 | wxLogLastError(wxT("CreateEvent(RasStatus)")); |
a0b4c98b VZ |
1078 | |
1079 | ok = FALSE; | |
1080 | } | |
1081 | } | |
1082 | ||
1083 | if ( ok ) | |
1084 | { | |
1085 | // create the event we use to quit the thread | |
1086 | m_data.hEventQuit = CreateEvent(NULL, FALSE, FALSE, NULL); | |
1087 | if ( !m_data.hEventQuit ) | |
1088 | { | |
f6bcfd97 | 1089 | wxLogLastError(wxT("CreateEvent(RasThreadQuit)")); |
a0b4c98b VZ |
1090 | |
1091 | CleanUpThreadData(); | |
1092 | ||
1093 | ok = FALSE; | |
1094 | } | |
1095 | } | |
1096 | ||
2690830e | 1097 | if ( ok && !ms_hwndRas ) |
a0b4c98b VZ |
1098 | { |
1099 | // create a hidden window to receive notification about connections | |
1100 | // status change | |
8673da0d | 1101 | extern const wxChar *wxPanelClassName; |
2690830e VZ |
1102 | ms_hwndRas = ::CreateWindow(wxPanelClassName, NULL, |
1103 | 0, 0, 0, 0, | |
1104 | 0, NULL, | |
1105 | (HMENU)NULL, wxGetInstance(), 0); | |
1106 | if ( !ms_hwndRas ) | |
a0b4c98b | 1107 | { |
f6bcfd97 | 1108 | wxLogLastError(wxT("CreateWindow(RasHiddenWindow)")); |
a0b4c98b VZ |
1109 | |
1110 | CleanUpThreadData(); | |
1111 | ||
1112 | ok = FALSE; | |
1113 | } | |
1114 | ||
1115 | // and subclass it | |
1116 | FARPROC windowProc = MakeProcInstance | |
1117 | ( | |
1118 | (FARPROC)wxRasStatusWindowProc, | |
1119 | wxGetInstance() | |
1120 | ); | |
1121 | ||
2690830e | 1122 | ::SetWindowLong(ms_hwndRas, GWL_WNDPROC, (LONG) windowProc); |
a0b4c98b VZ |
1123 | } |
1124 | ||
2690830e VZ |
1125 | m_data.hWnd = ms_hwndRas; |
1126 | ||
a0b4c98b VZ |
1127 | if ( ok ) |
1128 | { | |
1129 | // start the secondary thread | |
1130 | m_data.dialUpManager = this; | |
1131 | ||
1132 | DWORD tid; | |
1133 | m_hThread = CreateThread | |
1134 | ( | |
1135 | NULL, | |
1136 | 0, | |
1137 | (LPTHREAD_START_ROUTINE)wxRasMonitorThread, | |
1138 | (void *)&m_data, | |
1139 | 0, | |
1140 | &tid | |
1141 | ); | |
1142 | ||
1143 | if ( !m_hThread ) | |
1144 | { | |
f6bcfd97 | 1145 | wxLogLastError(wxT("CreateThread(RasStatusThread)")); |
a0b4c98b VZ |
1146 | |
1147 | CleanUpThreadData(); | |
1148 | } | |
1149 | } | |
1150 | ||
1151 | if ( ok ) | |
1152 | { | |
1153 | // start receiving RAS notifications | |
1154 | DWORD dwRet = ms_pfnRasConnectionNotification | |
1155 | ( | |
1156 | (HRASCONN)INVALID_HANDLE_VALUE, | |
1157 | m_data.hEventRas, | |
1158 | 3 /* RASCN_Connection | RASCN_Disconnection */ | |
1159 | ); | |
1160 | ||
1161 | if ( dwRet != 0 ) | |
1162 | { | |
223d09f6 | 1163 | wxLogDebug(wxT("RasConnectionNotification() failed: %s"), |
a0b4c98b VZ |
1164 | GetErrorString(dwRet)); |
1165 | ||
1166 | CleanUpThreadData(); | |
1167 | } | |
1168 | else | |
1169 | { | |
1170 | return TRUE; | |
1171 | } | |
1172 | } | |
1173 | ||
1174 | // we're running under Windows 95 and have to poll ourselves | |
1175 | // (or, alternatively, the code above for NT/98 failed) | |
1176 | m_timerStatusPolling.Stop(); | |
1177 | if ( nSeconds == 0 ) | |
1178 | { | |
1179 | // default value | |
1180 | nSeconds = 60; | |
1181 | } | |
1182 | m_timerStatusPolling.Start(nSeconds * 1000); | |
1183 | ||
1184 | return TRUE; | |
1185 | } | |
1186 | ||
1187 | void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus() | |
1188 | { | |
223d09f6 | 1189 | wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1190 | |
1191 | if ( m_hThread ) | |
1192 | { | |
1193 | // we have running secondary thread, it's just enough to suspend it | |
1194 | if ( SuspendThread(m_hThread) == (DWORD)-1 ) | |
1195 | { | |
f6bcfd97 | 1196 | wxLogLastError(wxT("SuspendThread(RasThread)")); |
a0b4c98b VZ |
1197 | } |
1198 | } | |
1199 | else | |
1200 | { | |
1201 | // even simpler - just stop the timer | |
1202 | m_timerStatusPolling.Stop(); | |
1203 | } | |
1204 | } | |
1205 | ||
1206 | // ---------------------------------------------------------------------------- | |
1207 | // stubs which don't do anything in MSW version | |
1208 | // ---------------------------------------------------------------------------- | |
1209 | ||
1210 | void wxDialUpManagerMSW::SetWellKnownHost(const wxString& WXUNUSED(hostname), | |
1211 | int WXUNUSED(port)) | |
1212 | { | |
223d09f6 | 1213 | wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1214 | |
1215 | // nothing to do - we don't use this | |
1216 | } | |
1217 | ||
1218 | void wxDialUpManagerMSW::SetConnectCommand(const wxString& WXUNUSED(dial), | |
1219 | const wxString& WXUNUSED(hangup)) | |
1220 | { | |
223d09f6 | 1221 | wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1222 | |
1223 | // nothing to do - we don't use this | |
1224 | } | |
1225 | ||
1226 | // ---------------------------------------------------------------------------- | |
1227 | // callbacks | |
1228 | // ---------------------------------------------------------------------------- | |
1229 | ||
1230 | static DWORD wxRasMonitorThread(wxRasThreadData *data) | |
1231 | { | |
1232 | HANDLE handles[2]; | |
1233 | handles[0] = data->hEventRas; | |
1234 | handles[1] = data->hEventQuit; | |
1235 | ||
1236 | bool cont = TRUE; | |
1237 | while ( cont ) | |
1238 | { | |
1239 | DWORD dwRet = WaitForMultipleObjects(2, handles, FALSE, INFINITE); | |
1240 | ||
1241 | switch ( dwRet ) | |
1242 | { | |
1243 | case WAIT_OBJECT_0: | |
1244 | // RAS connection status changed | |
1245 | SendMessage(data->hWnd, wxWM_RAS_STATUS_CHANGED, | |
1246 | 0, (LPARAM)data); | |
1247 | break; | |
1248 | ||
1249 | case WAIT_OBJECT_0 + 1: | |
1250 | cont = FALSE; | |
1251 | break; | |
1252 | ||
1253 | case WAIT_FAILED: | |
f6bcfd97 | 1254 | wxLogLastError(wxT("WaitForMultipleObjects(RasMonitor)")); |
a0b4c98b VZ |
1255 | break; |
1256 | } | |
1257 | } | |
1258 | ||
1259 | return 0; | |
1260 | } | |
1261 | ||
1262 | static LRESULT APIENTRY wxRasStatusWindowProc(HWND hWnd, UINT message, | |
1263 | WPARAM wParam, LPARAM lParam) | |
1264 | { | |
1265 | if ( message == wxWM_RAS_STATUS_CHANGED ) | |
1266 | { | |
1267 | wxRasThreadData *data = (wxRasThreadData *)lParam; | |
1268 | data->dialUpManager->OnConnectStatusChange(); | |
1269 | } | |
2690830e VZ |
1270 | else if ( message == wxWM_RAS_DIALING_PROGRESS ) |
1271 | { | |
1272 | wxDialUpManagerMSW *dialUpManager = wxDialUpManagerMSW::GetDialer(); | |
1273 | ||
1274 | dialUpManager->OnDialProgress((RASCONNSTATE)wParam, lParam); | |
1275 | } | |
a0b4c98b VZ |
1276 | |
1277 | return 0; | |
1278 | } | |
1279 | ||
1280 | static void WINAPI wxRasDialFunc(UINT unMsg, | |
1281 | RASCONNSTATE rasconnstate, | |
1282 | DWORD dwError) | |
1283 | { | |
1284 | wxDialUpManagerMSW *dialUpManager = wxDialUpManagerMSW::GetDialer(); | |
1285 | ||
223d09f6 | 1286 | wxCHECK_RET( dialUpManager, wxT("who started to dial then?") ); |
a0b4c98b | 1287 | |
2690830e VZ |
1288 | SendMessage(dialUpManager->GetRasWindow(), wxWM_RAS_DIALING_PROGRESS, |
1289 | rasconnstate, dwError); | |
a0b4c98b VZ |
1290 | } |
1291 | ||
106f0395 JS |
1292 | #endif |
1293 | // __BORLANDC__ | |
a0b4c98b | 1294 | #endif // wxUSE_DIALUP_MANAGER |