]>
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 | ||
a0b4c98b | 45 | #include "wx/dynlib.h" |
2690830e | 46 | #include "wx/dialup.h" |
a0b4c98b | 47 | |
2e4df4bf VZ |
48 | DEFINE_EVENT_TYPE(wxEVT_DIALUP_CONNECTED) |
49 | DEFINE_EVENT_TYPE(wxEVT_DIALUP_DISCONNECTED) | |
50 | ||
d8c72298 | 51 | // Doesn't yet compile under VC++ 4, BC++, Watcom C++: no wininet.h |
7ba4fbeb | 52 | #if !defined(__BORLANDC__) && \ |
ce20b5d7 | 53 | (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION(0, 5)) && \ |
7ba4fbeb VZ |
54 | !defined(__GNUWIN32_OLD__) && \ |
55 | !defined(__WATCOMC__) && \ | |
56 | (!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 | ||
4f89dbc4 RL |
225 | // the handle of rasapi32.dll when it's loaded |
226 | wxPluginManager m_dllRas; | |
227 | ||
2690830e VZ |
228 | // the hidden window we use for passing messages between threads |
229 | static HWND ms_hwndRas; | |
230 | ||
a0b4c98b VZ |
231 | // the handle of the connection we initiated or 0 if none |
232 | static HRASCONN ms_hRasConnection; | |
233 | ||
4f89dbc4 RL |
234 | // FIXME: There is probably no reason these really need to |
235 | // be static anymore since the dll refcounting is | |
236 | // handled by wxPluginManager now. Whether or not | |
237 | // we still _want_ them to be static is another | |
238 | // issue entirely.. | |
a0b4c98b VZ |
239 | |
240 | // the pointers to RAS functions | |
241 | static RASDIAL ms_pfnRasDial; | |
242 | static RASENUMCONNECTIONS ms_pfnRasEnumConnections; | |
243 | static RASENUMENTRIES ms_pfnRasEnumEntries; | |
244 | static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus; | |
245 | static RASGETERRORSTRING ms_pfnRasGetErrorString; | |
246 | static RASHANGUP ms_pfnRasHangUp; | |
247 | static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo; | |
248 | static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry; | |
249 | static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry; | |
250 | static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams; | |
251 | static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams; | |
252 | static RASENUMDEVICES ms_pfnRasEnumDevices; | |
253 | static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo; | |
254 | static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties; | |
255 | static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties; | |
256 | static RASRENAMEENTRY ms_pfnRasRenameEntry; | |
257 | static RASDELETEENTRY ms_pfnRasDeleteEntry; | |
258 | static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName; | |
259 | ||
260 | // this function is not supported by Win95 | |
261 | static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification; | |
262 | ||
263 | // if this flag is different from -1, it overrides IsOnline() | |
264 | static int ms_userSpecifiedOnlineStatus; | |
265 | ||
266 | // this flag tells us if we're online | |
267 | static int ms_isConnected; | |
268 | ||
2690830e VZ |
269 | // this flag is the result of the call to IsAlwaysOnline() (-1 if not |
270 | // called yet) | |
271 | static int ms_isAlwaysOnline; | |
272 | ||
a0b4c98b VZ |
273 | // this flag tells us whether a call to RasDial() is in progress |
274 | static wxDialUpManagerMSW *ms_dialer; | |
275 | }; | |
276 | ||
277 | // ---------------------------------------------------------------------------- | |
278 | // private functions | |
279 | // ---------------------------------------------------------------------------- | |
280 | ||
281 | static LRESULT WINAPI wxRasStatusWindowProc(HWND hWnd, UINT message, | |
282 | WPARAM wParam, LPARAM lParam); | |
283 | ||
284 | static DWORD wxRasMonitorThread(wxRasThreadData *data); | |
285 | ||
286 | static void WINAPI wxRasDialFunc(UINT unMsg, | |
287 | RASCONNSTATE rasconnstate, | |
288 | DWORD dwError); | |
289 | ||
290 | // ============================================================================ | |
291 | // implementation | |
292 | // ============================================================================ | |
293 | ||
294 | // ---------------------------------------------------------------------------- | |
295 | // init the static variables | |
296 | // ---------------------------------------------------------------------------- | |
297 | ||
298 | HRASCONN wxDialUpManagerMSW::ms_hRasConnection = 0; | |
299 | ||
2690830e VZ |
300 | HWND wxDialUpManagerMSW::ms_hwndRas = 0; |
301 | ||
a0b4c98b VZ |
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) | |
4f89dbc4 | 344 | , m_dllRas(_T("RASAPI32")) |
a0b4c98b VZ |
345 | { |
346 | // initialize our data | |
347 | m_hThread = 0; | |
348 | ||
4f89dbc4 RL |
349 | if ( !m_dllRas.IsLoaded() ) |
350 | { | |
351 | wxLogError(_("Dial up functions are unavailable because the remote access service (RAS) is not installed on this machine. Please install it.")); | |
352 | } | |
1c25d245 | 353 | else if ( !ms_pfnRasDial ) |
a0b4c98b | 354 | { |
4f89dbc4 RL |
355 | // resolve the functions we need |
356 | ||
357 | // this will contain the name of the function we failed to resolve | |
358 | // if any at the end | |
359 | const char *funcName = NULL; | |
360 | ||
361 | // get the function from rasapi32.dll and abort if it's not found | |
362 | #define RESOLVE_RAS_FUNCTION(type, name) \ | |
363 | ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(_T(#name)) \ | |
364 | + gs_funcSuffix); \ | |
365 | if ( !ms_pfn##name ) \ | |
366 | { \ | |
367 | funcName = #name; \ | |
368 | goto exit; \ | |
369 | } | |
370 | ||
371 | // a variant of above macro which doesn't abort if the function is | |
372 | // not found in the DLL | |
373 | #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \ | |
374 | ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(_T(#name)) \ | |
375 | + gs_funcSuffix); | |
376 | ||
377 | RESOLVE_RAS_FUNCTION(RASDIAL, RasDial); | |
378 | RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS, RasEnumConnections); | |
379 | RESOLVE_RAS_FUNCTION(RASENUMENTRIES, RasEnumEntries); | |
380 | RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS, RasGetConnectStatus); | |
381 | RESOLVE_RAS_FUNCTION(RASGETERRORSTRING, RasGetErrorString); | |
382 | RESOLVE_RAS_FUNCTION(RASHANGUP, RasHangUp); | |
383 | RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS, RasGetEntryDialParams); | |
384 | ||
385 | // suppress error messages about missing (non essential) functions | |
a0b4c98b | 386 | { |
4f89dbc4 RL |
387 | wxLogNull noLog; |
388 | ||
389 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO, RasGetProjectionInfo); | |
390 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY, RasCreatePhonebookEntry); | |
391 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY, RasEditPhonebookEntry); | |
392 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS, RasSetEntryDialParams); | |
393 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES, RasGetEntryProperties); | |
394 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES, RasSetEntryProperties); | |
395 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY, RasRenameEntry); | |
396 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY, RasDeleteEntry); | |
397 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME, RasValidateEntryName); | |
398 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO, RasGetCountryInfo); | |
399 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES, RasEnumDevices); | |
400 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION, RasConnectionNotification); | |
a0b4c98b | 401 | } |
a0b4c98b | 402 | |
4f89dbc4 RL |
403 | // keep your preprocessor name space clean |
404 | #undef RESOLVE_RAS_FUNCTION | |
405 | #undef RESOLVE_OPTIONAL_RAS_FUNCTION | |
a0b4c98b VZ |
406 | |
407 | exit: | |
4f89dbc4 RL |
408 | if ( funcName ) |
409 | { | |
410 | static const wxChar *msg = wxTRANSLATE( | |
f6bcfd97 BP |
411 | "The version of remote access service (RAS) installed on this machine is too\ |
412 | old, please upgrade (the following required function is missing: %s)." | |
4f89dbc4 | 413 | ); |
a0b4c98b | 414 | |
4f89dbc4 RL |
415 | wxLogError(wxGetTranslation(msg), funcName); |
416 | m_dllRas.Unload(); | |
417 | return; | |
a0b4c98b VZ |
418 | } |
419 | } | |
420 | ||
421 | // enable auto check by default | |
422 | EnableAutoCheckOnlineStatus(0); | |
423 | } | |
424 | ||
425 | wxDialUpManagerMSW::~wxDialUpManagerMSW() | |
426 | { | |
427 | CleanUpThreadData(); | |
a0b4c98b VZ |
428 | } |
429 | ||
430 | // ---------------------------------------------------------------------------- | |
431 | // helper functions | |
432 | // ---------------------------------------------------------------------------- | |
433 | ||
434 | wxString wxDialUpManagerMSW::GetErrorString(DWORD error) | |
435 | { | |
436 | wxChar buffer[512]; // this should be more than enough according to MS docs | |
437 | DWORD dwRet = ms_pfnRasGetErrorString(error, buffer, WXSIZEOF(buffer)); | |
438 | switch ( dwRet ) | |
439 | { | |
440 | case ERROR_INVALID_PARAMETER: | |
441 | // this was a standard Win32 error probably | |
442 | return wxString(wxSysErrorMsg(error)); | |
443 | ||
444 | default: | |
445 | { | |
f6bcfd97 BP |
446 | wxLogSysError(dwRet, |
447 | _("Failed to retrieve text of RAS error message")); | |
a0b4c98b VZ |
448 | |
449 | wxString msg; | |
450 | msg.Printf(_("unknown error (error code %08x)."), error); | |
451 | return msg; | |
452 | } | |
453 | ||
454 | case 0: | |
455 | // we want the error message to start from a lower case letter | |
456 | buffer[0] = wxTolower(buffer[0]); | |
457 | ||
458 | return wxString(buffer); | |
459 | } | |
460 | } | |
461 | ||
462 | HRASCONN wxDialUpManagerMSW::FindActiveConnection() | |
463 | { | |
464 | // enumerate connections | |
465 | DWORD cbBuf = sizeof(RASCONN); | |
466 | LPRASCONN lpRasConn = (LPRASCONN)malloc(cbBuf); | |
467 | if ( !lpRasConn ) | |
468 | { | |
469 | // out of memory | |
470 | return 0; | |
471 | } | |
472 | ||
473 | lpRasConn->dwSize = sizeof(RASCONN); | |
474 | ||
475 | DWORD nConnections = 0; | |
476 | DWORD dwRet = ERROR_BUFFER_TOO_SMALL; | |
477 | ||
478 | while ( dwRet == ERROR_BUFFER_TOO_SMALL ) | |
479 | { | |
480 | dwRet = ms_pfnRasEnumConnections(lpRasConn, &cbBuf, &nConnections); | |
481 | ||
482 | if ( dwRet == ERROR_BUFFER_TOO_SMALL ) | |
483 | { | |
484 | LPRASCONN lpRasConnOld = lpRasConn; | |
485 | lpRasConn = (LPRASCONN)realloc(lpRasConn, cbBuf); | |
486 | if ( !lpRasConn ) | |
487 | { | |
488 | // out of memory | |
489 | free(lpRasConnOld); | |
490 | ||
491 | return 0; | |
492 | } | |
493 | } | |
494 | else if ( dwRet == 0 ) | |
495 | { | |
496 | // ok, success | |
497 | break; | |
498 | } | |
499 | else | |
500 | { | |
501 | // an error occured | |
502 | wxLogError(_("Cannot find active dialup connection: %s"), | |
5b09e9c2 | 503 | GetErrorString(dwRet).c_str()); |
a0b4c98b VZ |
504 | return 0; |
505 | } | |
506 | } | |
507 | ||
508 | HRASCONN hrasconn; | |
509 | ||
510 | switch ( nConnections ) | |
511 | { | |
512 | case 0: | |
513 | // no connections | |
514 | hrasconn = 0; | |
515 | break; | |
516 | ||
517 | default: | |
518 | // more than 1 connection - we don't know what to do with this | |
519 | // case, so give a warning but continue (taking the first | |
520 | // connection) - the warning is really needed because this function | |
521 | // is used, for example, to select the connection to hang up and so | |
522 | // we may hang up the wrong connection here... | |
f6bcfd97 | 523 | wxLogWarning(_("Several active dialup connections found, choosing one randomly.")); |
a0b4c98b VZ |
524 | // fall through |
525 | ||
526 | case 1: | |
527 | // exactly 1 connection, great | |
528 | hrasconn = lpRasConn->hrasconn; | |
529 | } | |
530 | ||
531 | free(lpRasConn); | |
532 | ||
533 | return hrasconn; | |
534 | } | |
535 | ||
536 | void wxDialUpManagerMSW::CleanUpThreadData() | |
537 | { | |
538 | if ( m_hThread ) | |
539 | { | |
540 | if ( !SetEvent(m_data.hEventQuit) ) | |
541 | { | |
fcec6429 | 542 | wxLogLastError(_T("SetEvent(RasThreadQuit)")); |
a0b4c98b VZ |
543 | } |
544 | ||
545 | CloseHandle(m_hThread); | |
546 | ||
547 | m_hThread = 0; | |
548 | } | |
549 | ||
550 | if ( m_data.hWnd ) | |
551 | { | |
552 | DestroyWindow(m_data.hWnd); | |
553 | ||
554 | m_data.hWnd = 0; | |
555 | } | |
556 | ||
557 | if ( m_data.hEventQuit ) | |
558 | { | |
559 | CloseHandle(m_data.hEventQuit); | |
560 | ||
561 | m_data.hEventQuit = 0; | |
562 | } | |
563 | ||
564 | if ( m_data.hEventRas ) | |
565 | { | |
566 | CloseHandle(m_data.hEventRas); | |
567 | ||
568 | m_data.hEventRas = 0; | |
569 | } | |
570 | } | |
571 | ||
572 | // ---------------------------------------------------------------------------- | |
573 | // connection status | |
574 | // ---------------------------------------------------------------------------- | |
575 | ||
576 | void wxDialUpManagerMSW::CheckRasStatus() | |
577 | { | |
578 | // use int, not bool to compare with -1 | |
579 | int isConnected = FindActiveConnection() != 0; | |
580 | if ( isConnected != ms_isConnected ) | |
581 | { | |
582 | if ( ms_isConnected != -1 ) | |
583 | { | |
584 | // notify the program | |
585 | NotifyApp(isConnected != 0); | |
586 | } | |
587 | // else: it's the first time we're called, just update the flag | |
588 | ||
589 | ms_isConnected = isConnected; | |
590 | } | |
591 | } | |
592 | ||
593 | void wxDialUpManagerMSW::NotifyApp(bool connected, bool fromOurselves) const | |
594 | { | |
595 | wxDialUpEvent event(connected, fromOurselves); | |
596 | (void)wxTheApp->ProcessEvent(event); | |
597 | } | |
598 | ||
599 | // this function is called whenever the status of any RAS connection on this | |
600 | // machine changes by RAS itself | |
601 | void wxDialUpManagerMSW::OnConnectStatusChange() | |
602 | { | |
603 | // we know that status changed, but we don't know whether we're connected | |
604 | // or not - so find it out | |
605 | CheckRasStatus(); | |
606 | } | |
607 | ||
608 | // this function is called by our callback which we give to RasDial() when | |
609 | // calling it asynchronously | |
610 | void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate, | |
611 | DWORD dwError) | |
612 | { | |
613 | if ( !GetDialer() ) | |
614 | { | |
615 | // this probably means that CancelDialing() was called and we get | |
616 | // "disconnected" notification | |
617 | return; | |
618 | } | |
619 | ||
620 | // we're only interested in 2 events: connected and disconnected | |
621 | if ( dwError ) | |
622 | { | |
623 | wxLogError(_("Failed to establish dialup connection: %s"), | |
5b09e9c2 | 624 | GetErrorString(dwError).c_str()); |
a0b4c98b VZ |
625 | |
626 | // we should still call RasHangUp() if we got a non 0 connection | |
627 | if ( ms_hRasConnection ) | |
628 | { | |
629 | ms_pfnRasHangUp(ms_hRasConnection); | |
630 | ms_hRasConnection = 0; | |
631 | } | |
632 | ||
633 | ms_dialer = NULL; | |
634 | ||
635 | NotifyApp(FALSE /* !connected */, TRUE /* we dialed ourselves */); | |
636 | } | |
637 | else if ( rasconnstate == RASCS_Connected ) | |
638 | { | |
639 | ms_isConnected = TRUE; | |
640 | ms_dialer = NULL; | |
641 | ||
642 | NotifyApp(TRUE /* connected */, TRUE /* we dialed ourselves */); | |
643 | } | |
644 | } | |
645 | ||
646 | // ---------------------------------------------------------------------------- | |
647 | // implementation of wxDialUpManager functions | |
648 | // ---------------------------------------------------------------------------- | |
649 | ||
650 | bool wxDialUpManagerMSW::IsOk() const | |
651 | { | |
4f89dbc4 | 652 | return m_dllRas.IsLoaded(); |
a0b4c98b VZ |
653 | } |
654 | ||
2690830e VZ |
655 | size_t wxDialUpManagerMSW::GetISPNames(wxArrayString& names) const |
656 | { | |
657 | // fetch the entries | |
658 | DWORD size = sizeof(RASENTRYNAME); | |
659 | RASENTRYNAME *rasEntries = (RASENTRYNAME *)malloc(size); | |
660 | rasEntries->dwSize = sizeof(RASENTRYNAME); | |
661 | ||
662 | DWORD nEntries; | |
663 | DWORD dwRet; | |
664 | do | |
665 | { | |
666 | dwRet = ms_pfnRasEnumEntries | |
667 | ( | |
668 | NULL, // reserved | |
669 | NULL, // default phone book (or all) | |
670 | rasEntries, // [out] buffer for the entries | |
671 | &size, // [in/out] size of the buffer | |
672 | &nEntries // [out] number of entries fetched | |
673 | ); | |
674 | ||
675 | if ( dwRet == ERROR_BUFFER_TOO_SMALL ) | |
676 | { | |
677 | // reallocate the buffer | |
678 | rasEntries = (RASENTRYNAME *)realloc(rasEntries, size); | |
679 | } | |
680 | else if ( dwRet != 0 ) | |
681 | { | |
682 | // some other error - abort | |
5b09e9c2 VZ |
683 | wxLogError(_("Failed to get ISP names: %s"), |
684 | GetErrorString(dwRet).c_str()); | |
2690830e VZ |
685 | |
686 | free(rasEntries); | |
687 | ||
688 | return 0u; | |
689 | } | |
690 | } | |
691 | while ( dwRet != 0 ); | |
692 | ||
693 | // process them | |
694 | names.Empty(); | |
695 | for ( size_t n = 0; n < (size_t)nEntries; n++ ) | |
696 | { | |
697 | names.Add(rasEntries[n].szEntryName); | |
698 | } | |
699 | ||
700 | free(rasEntries); | |
701 | ||
702 | // return the number of entries | |
703 | return names.GetCount(); | |
704 | } | |
705 | ||
a0b4c98b VZ |
706 | bool wxDialUpManagerMSW::Dial(const wxString& nameOfISP, |
707 | const wxString& username, | |
708 | const wxString& password, | |
709 | bool async) | |
710 | { | |
2690830e | 711 | // check preconditions |
223d09f6 | 712 | wxCHECK_MSG( IsOk(), FALSE, wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
713 | |
714 | if ( ms_hRasConnection ) | |
715 | { | |
223d09f6 | 716 | wxFAIL_MSG(wxT("there is already an active connection")); |
a0b4c98b VZ |
717 | |
718 | return TRUE; | |
719 | } | |
720 | ||
2690830e VZ |
721 | // get the default ISP if none given |
722 | wxString entryName(nameOfISP); | |
723 | if ( !entryName ) | |
724 | { | |
725 | wxArrayString names; | |
726 | size_t count = GetISPNames(names); | |
727 | switch ( count ) | |
728 | { | |
729 | case 0: | |
730 | // no known ISPs, abort | |
731 | wxLogError(_("Failed to connect: no ISP to dial.")); | |
732 | ||
733 | return FALSE; | |
734 | ||
735 | case 1: | |
736 | // only one ISP, choose it | |
737 | entryName = names[0u]; | |
738 | break; | |
739 | ||
740 | default: | |
741 | // several ISPs, let the user choose | |
742 | { | |
743 | wxString *strings = new wxString[count]; | |
744 | for ( size_t i = 0; i < count; i++ ) | |
745 | { | |
746 | strings[i] = names[i]; | |
747 | } | |
748 | ||
749 | entryName = wxGetSingleChoice | |
750 | ( | |
751 | _("Choose ISP to dial"), | |
f6bcfd97 | 752 | _("Please choose which ISP do you want to connect to"), |
2690830e VZ |
753 | count, |
754 | strings | |
755 | ); | |
756 | ||
757 | delete [] strings; | |
758 | ||
759 | if ( !entryName ) | |
760 | { | |
761 | // cancelled by user | |
762 | return FALSE; | |
763 | } | |
764 | } | |
765 | } | |
766 | } | |
767 | ||
a0b4c98b VZ |
768 | RASDIALPARAMS rasDialParams; |
769 | rasDialParams.dwSize = sizeof(rasDialParams); | |
f6bcfd97 | 770 | wxStrncpy(rasDialParams.szEntryName, entryName, RAS_MaxEntryName); |
2690830e VZ |
771 | |
772 | // do we have the username and password? | |
773 | if ( !username || !password ) | |
774 | { | |
775 | BOOL gotPassword; | |
776 | DWORD dwRet = ms_pfnRasGetEntryDialParams | |
777 | ( | |
778 | NULL, // default phonebook | |
779 | &rasDialParams, // [in/out] the params of this entry | |
780 | &gotPassword // [out] did we get password? | |
781 | ); | |
782 | ||
783 | if ( dwRet != 0 ) | |
784 | { | |
785 | wxLogError(_("Failed to connect: missing username/password.")); | |
786 | ||
787 | return FALSE; | |
788 | } | |
789 | } | |
4f89dbc4 RL |
790 | else |
791 | { | |
792 | wxStrncpy(rasDialParams.szUserName, username, UNLEN); | |
793 | wxStrncpy(rasDialParams.szPassword, password, PWLEN); | |
794 | } | |
2690830e | 795 | |
4f89dbc4 | 796 | // default values for other fields |
a0b4c98b VZ |
797 | rasDialParams.szPhoneNumber[0] = '\0'; |
798 | rasDialParams.szCallbackNumber[0] = '\0'; | |
799 | rasDialParams.szCallbackNumber[0] = '\0'; | |
800 | ||
a0b4c98b VZ |
801 | rasDialParams.szDomain[0] = '*'; |
802 | rasDialParams.szDomain[1] = '\0'; | |
803 | ||
804 | // apparently, this is not really necessary - passing NULL instead of the | |
805 | // phone book has the same effect | |
806 | #if 0 | |
807 | wxString phoneBook; | |
808 | if ( wxGetOsVersion() == wxWINDOWS_NT ) | |
809 | { | |
810 | // first get the length | |
811 | UINT nLen = ::GetSystemDirectory(NULL, 0); | |
812 | nLen++; | |
813 | ||
814 | if ( !::GetSystemDirectory(phoneBook.GetWriteBuf(nLen), nLen) ) | |
815 | { | |
816 | wxLogSysError(_("Cannot find the location of address book file")); | |
817 | } | |
818 | ||
819 | phoneBook.UngetWriteBuf(); | |
820 | ||
821 | // this is the default phone book | |
822 | phoneBook << "\\ras\\rasphone.pbk"; | |
823 | } | |
824 | #endif // 0 | |
825 | ||
826 | // TODO may be we should disable auto check while async dialing is in | |
827 | // progress? | |
828 | ||
829 | ms_dialer = this; | |
830 | ||
831 | DWORD dwRet = ms_pfnRasDial | |
832 | ( | |
1c25d245 VZ |
833 | NULL, // no extended features |
834 | NULL, // default phone book file (NT only) | |
a0b4c98b | 835 | &rasDialParams, |
1c25d245 VZ |
836 | 0, // use callback for notifications |
837 | async ? (void *)wxRasDialFunc // cast needed for gcc 3.1 | |
838 | : 0, // no notifications, sync operation | |
a0b4c98b VZ |
839 | &ms_hRasConnection |
840 | ); | |
841 | ||
842 | if ( dwRet != 0 ) | |
843 | { | |
bf9b6266 | 844 | // can't pass a wxWCharBuffer through ( ... ) |
a0b4c98b | 845 | wxLogError(_("Failed to %s dialup connection: %s"), |
5438a566 | 846 | wxString(async ? _("initiate") : _("establish")).c_str(), |
5b09e9c2 | 847 | GetErrorString(dwRet).c_str()); |
a0b4c98b VZ |
848 | |
849 | // we should still call RasHangUp() if we got a non 0 connection | |
850 | if ( ms_hRasConnection ) | |
851 | { | |
852 | ms_pfnRasHangUp(ms_hRasConnection); | |
853 | ms_hRasConnection = 0; | |
854 | } | |
855 | ||
856 | ms_dialer = NULL; | |
857 | ||
858 | return FALSE; | |
859 | } | |
860 | ||
861 | // for async dialing, we're not yet connected | |
862 | if ( !async ) | |
863 | { | |
864 | ms_isConnected = TRUE; | |
865 | } | |
866 | ||
867 | return TRUE; | |
868 | } | |
869 | ||
870 | bool wxDialUpManagerMSW::IsDialing() const | |
871 | { | |
872 | return GetDialer() != NULL; | |
873 | } | |
874 | ||
875 | bool wxDialUpManagerMSW::CancelDialing() | |
876 | { | |
877 | if ( !GetDialer() ) | |
878 | { | |
879 | // silently ignore | |
880 | return FALSE; | |
881 | } | |
882 | ||
223d09f6 | 883 | wxASSERT_MSG( ms_hRasConnection, wxT("dialing but no connection?") ); |
a0b4c98b VZ |
884 | |
885 | ms_dialer = NULL; | |
886 | ||
887 | return HangUp(); | |
888 | } | |
889 | ||
890 | bool wxDialUpManagerMSW::HangUp() | |
891 | { | |
223d09f6 | 892 | wxCHECK_MSG( IsOk(), FALSE, wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
893 | |
894 | // we may terminate either the connection we initiated or another one which | |
895 | // is active now | |
896 | HRASCONN hRasConn; | |
897 | if ( ms_hRasConnection ) | |
898 | { | |
899 | hRasConn = ms_hRasConnection; | |
900 | ||
901 | ms_hRasConnection = 0; | |
902 | } | |
903 | else | |
904 | { | |
905 | hRasConn = FindActiveConnection(); | |
906 | } | |
907 | ||
908 | if ( !hRasConn ) | |
909 | { | |
910 | wxLogError(_("Cannot hang up - no active dialup connection.")); | |
911 | ||
912 | return FALSE; | |
913 | } | |
914 | ||
915 | DWORD dwRet = ms_pfnRasHangUp(hRasConn); | |
916 | if ( dwRet != 0 ) | |
917 | { | |
918 | wxLogError(_("Failed to terminate the dialup connection: %s"), | |
5b09e9c2 | 919 | GetErrorString(dwRet).c_str()); |
a0b4c98b VZ |
920 | } |
921 | ||
922 | ms_isConnected = FALSE; | |
923 | ||
924 | return TRUE; | |
925 | } | |
926 | ||
2690830e VZ |
927 | bool wxDialUpManagerMSW::IsAlwaysOnline() const |
928 | { | |
929 | // we cache the result (presumably this won't change while the program is | |
930 | // running!) | |
931 | if ( ms_isAlwaysOnline != -1 ) | |
932 | { | |
933 | return ms_isAlwaysOnline != 0; | |
934 | } | |
935 | ||
936 | // try to use WinInet function first | |
4f89dbc4 RL |
937 | |
938 | // NB: we could probably use wxDynamicLibrary here just as well, | |
939 | // but we allow multiple instances of wxDialUpManagerMSW so | |
940 | // we might as well use the ref counted version here too. | |
941 | ||
942 | wxPluginManager hDll(_T("WININET")); | |
943 | if ( hDll.IsLoaded() ) | |
2690830e | 944 | { |
5a56be28 | 945 | typedef BOOL (WINAPI *INTERNETGETCONNECTEDSTATE)(LPDWORD, DWORD); |
2690830e VZ |
946 | INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState; |
947 | ||
948 | #define RESOLVE_FUNCTION(type, name) \ | |
4f89dbc4 | 949 | pfn##name = (type)hDll.GetSymbol(_T(#name)) |
2690830e VZ |
950 | |
951 | RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE, InternetGetConnectedState); | |
952 | ||
953 | if ( pfnInternetGetConnectedState ) | |
954 | { | |
955 | DWORD flags = 0; | |
956 | if ( pfnInternetGetConnectedState(&flags, 0 /* reserved */) ) | |
957 | { | |
958 | // there is some connection to the net, see of which type | |
4ccf704a VZ |
959 | ms_isAlwaysOnline = ((flags & INTERNET_CONNECTION_LAN) != 0) |
960 | || ((flags & INTERNET_CONNECTION_PROXY) != 0); | |
2690830e VZ |
961 | } |
962 | else | |
963 | { | |
964 | // no Internet connection at all | |
965 | ms_isAlwaysOnline = FALSE; | |
966 | } | |
967 | } | |
2690830e VZ |
968 | } |
969 | ||
970 | // did we succeed with WinInet? if not, try something else | |
971 | if ( ms_isAlwaysOnline == -1 ) | |
972 | { | |
973 | if ( !IsOnline() ) | |
974 | { | |
975 | // definitely no permanent connection because we are not connected | |
976 | // now | |
977 | ms_isAlwaysOnline = FALSE; | |
978 | } | |
979 | else | |
980 | { | |
981 | // of course, having a modem doesn't prevent us from having a | |
982 | // permanent connection as well, but we have to guess somehow and | |
983 | // it's probably more common that a system connected via a modem | |
984 | // doesn't have any other net access, so: | |
985 | ms_isAlwaysOnline = FALSE; | |
986 | } | |
987 | } | |
988 | ||
223d09f6 | 989 | wxASSERT_MSG( ms_isAlwaysOnline != -1, wxT("logic error") ); |
2690830e VZ |
990 | |
991 | return ms_isAlwaysOnline != 0; | |
992 | } | |
993 | ||
a0b4c98b VZ |
994 | bool wxDialUpManagerMSW::IsOnline() const |
995 | { | |
223d09f6 | 996 | wxCHECK_MSG( IsOk(), FALSE, wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
997 | |
998 | if ( ms_userSpecifiedOnlineStatus != -1 ) | |
999 | { | |
1000 | // user specified flag overrides our logic | |
1001 | return ms_userSpecifiedOnlineStatus != 0; | |
1002 | } | |
1003 | else | |
1004 | { | |
1005 | // return TRUE if there is at least one active connection | |
1006 | return FindActiveConnection() != 0; | |
1007 | } | |
1008 | } | |
1009 | ||
1010 | void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline) | |
1011 | { | |
223d09f6 | 1012 | wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1013 | |
1014 | ms_userSpecifiedOnlineStatus = isOnline; | |
1015 | } | |
1016 | ||
1017 | bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds) | |
1018 | { | |
223d09f6 | 1019 | wxCHECK_MSG( IsOk(), FALSE, wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1020 | |
1021 | bool ok = ms_pfnRasConnectionNotification != 0; | |
1022 | ||
1023 | if ( ok ) | |
1024 | { | |
1025 | // we're running under NT 4.0, Windows 98 or later and can use | |
1026 | // RasConnectionNotification() to be notified by a secondary thread | |
1027 | ||
1028 | // first, see if we don't have this thread already running | |
1029 | if ( m_hThread != 0 ) | |
1030 | { | |
1031 | DWORD dwSuspendCount = 2; | |
1032 | while ( dwSuspendCount > 1 ) | |
1033 | { | |
1034 | dwSuspendCount = ResumeThread(m_hThread); | |
1035 | if ( dwSuspendCount == (DWORD)-1 ) | |
1036 | { | |
f6bcfd97 | 1037 | wxLogLastError(wxT("ResumeThread(RasThread)")); |
a0b4c98b VZ |
1038 | |
1039 | ok = FALSE; | |
1040 | } | |
1041 | } | |
1042 | ||
1043 | if ( ok ) | |
1044 | { | |
1045 | return TRUE; | |
1046 | } | |
1047 | } | |
1048 | } | |
1049 | ||
1050 | // create all the stuff we need to be notified about RAS connection | |
1051 | // status change | |
1052 | ||
1053 | if ( ok ) | |
1054 | { | |
1055 | // first create an event to wait on | |
1056 | m_data.hEventRas = CreateEvent | |
1057 | ( | |
1058 | NULL, // security attribute (default) | |
1059 | FALSE, // manual reset (not) | |
1060 | FALSE, // initial state (not signaled) | |
1061 | NULL // name (no) | |
1062 | ); | |
1063 | if ( !m_data.hEventRas ) | |
1064 | { | |
f6bcfd97 | 1065 | wxLogLastError(wxT("CreateEvent(RasStatus)")); |
a0b4c98b VZ |
1066 | |
1067 | ok = FALSE; | |
1068 | } | |
1069 | } | |
1070 | ||
1071 | if ( ok ) | |
1072 | { | |
1073 | // create the event we use to quit the thread | |
1074 | m_data.hEventQuit = CreateEvent(NULL, FALSE, FALSE, NULL); | |
1075 | if ( !m_data.hEventQuit ) | |
1076 | { | |
f6bcfd97 | 1077 | wxLogLastError(wxT("CreateEvent(RasThreadQuit)")); |
a0b4c98b VZ |
1078 | |
1079 | CleanUpThreadData(); | |
1080 | ||
1081 | ok = FALSE; | |
1082 | } | |
1083 | } | |
1084 | ||
2690830e | 1085 | if ( ok && !ms_hwndRas ) |
a0b4c98b VZ |
1086 | { |
1087 | // create a hidden window to receive notification about connections | |
1088 | // status change | |
03baf031 VZ |
1089 | extern const wxChar *wxCanvasClassName; |
1090 | ms_hwndRas = ::CreateWindow(wxCanvasClassName, NULL, | |
2690830e VZ |
1091 | 0, 0, 0, 0, |
1092 | 0, NULL, | |
1093 | (HMENU)NULL, wxGetInstance(), 0); | |
1094 | if ( !ms_hwndRas ) | |
a0b4c98b | 1095 | { |
f6bcfd97 | 1096 | wxLogLastError(wxT("CreateWindow(RasHiddenWindow)")); |
a0b4c98b VZ |
1097 | |
1098 | CleanUpThreadData(); | |
1099 | ||
1100 | ok = FALSE; | |
1101 | } | |
1102 | ||
1103 | // and subclass it | |
1104 | FARPROC windowProc = MakeProcInstance | |
1105 | ( | |
1106 | (FARPROC)wxRasStatusWindowProc, | |
1107 | wxGetInstance() | |
1108 | ); | |
1109 | ||
2690830e | 1110 | ::SetWindowLong(ms_hwndRas, GWL_WNDPROC, (LONG) windowProc); |
a0b4c98b VZ |
1111 | } |
1112 | ||
2690830e VZ |
1113 | m_data.hWnd = ms_hwndRas; |
1114 | ||
a0b4c98b VZ |
1115 | if ( ok ) |
1116 | { | |
1117 | // start the secondary thread | |
1118 | m_data.dialUpManager = this; | |
1119 | ||
1120 | DWORD tid; | |
1121 | m_hThread = CreateThread | |
1122 | ( | |
1123 | NULL, | |
1124 | 0, | |
1125 | (LPTHREAD_START_ROUTINE)wxRasMonitorThread, | |
1126 | (void *)&m_data, | |
1127 | 0, | |
1128 | &tid | |
1129 | ); | |
1130 | ||
1131 | if ( !m_hThread ) | |
1132 | { | |
f6bcfd97 | 1133 | wxLogLastError(wxT("CreateThread(RasStatusThread)")); |
a0b4c98b VZ |
1134 | |
1135 | CleanUpThreadData(); | |
1136 | } | |
1137 | } | |
1138 | ||
1139 | if ( ok ) | |
1140 | { | |
1141 | // start receiving RAS notifications | |
1142 | DWORD dwRet = ms_pfnRasConnectionNotification | |
1143 | ( | |
1144 | (HRASCONN)INVALID_HANDLE_VALUE, | |
1145 | m_data.hEventRas, | |
1146 | 3 /* RASCN_Connection | RASCN_Disconnection */ | |
1147 | ); | |
1148 | ||
1149 | if ( dwRet != 0 ) | |
1150 | { | |
223d09f6 | 1151 | wxLogDebug(wxT("RasConnectionNotification() failed: %s"), |
5b09e9c2 | 1152 | GetErrorString(dwRet).c_str()); |
a0b4c98b VZ |
1153 | |
1154 | CleanUpThreadData(); | |
1155 | } | |
1156 | else | |
1157 | { | |
1158 | return TRUE; | |
1159 | } | |
1160 | } | |
1161 | ||
1162 | // we're running under Windows 95 and have to poll ourselves | |
1163 | // (or, alternatively, the code above for NT/98 failed) | |
1164 | m_timerStatusPolling.Stop(); | |
1165 | if ( nSeconds == 0 ) | |
1166 | { | |
1167 | // default value | |
1168 | nSeconds = 60; | |
1169 | } | |
1170 | m_timerStatusPolling.Start(nSeconds * 1000); | |
1171 | ||
1172 | return TRUE; | |
1173 | } | |
1174 | ||
1175 | void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus() | |
1176 | { | |
223d09f6 | 1177 | wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1178 | |
1179 | if ( m_hThread ) | |
1180 | { | |
1181 | // we have running secondary thread, it's just enough to suspend it | |
1182 | if ( SuspendThread(m_hThread) == (DWORD)-1 ) | |
1183 | { | |
f6bcfd97 | 1184 | wxLogLastError(wxT("SuspendThread(RasThread)")); |
a0b4c98b VZ |
1185 | } |
1186 | } | |
1187 | else | |
1188 | { | |
1189 | // even simpler - just stop the timer | |
1190 | m_timerStatusPolling.Stop(); | |
1191 | } | |
1192 | } | |
1193 | ||
1194 | // ---------------------------------------------------------------------------- | |
1195 | // stubs which don't do anything in MSW version | |
1196 | // ---------------------------------------------------------------------------- | |
1197 | ||
1198 | void wxDialUpManagerMSW::SetWellKnownHost(const wxString& WXUNUSED(hostname), | |
1199 | int WXUNUSED(port)) | |
1200 | { | |
223d09f6 | 1201 | wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1202 | |
1203 | // nothing to do - we don't use this | |
1204 | } | |
1205 | ||
1206 | void wxDialUpManagerMSW::SetConnectCommand(const wxString& WXUNUSED(dial), | |
1207 | const wxString& WXUNUSED(hangup)) | |
1208 | { | |
223d09f6 | 1209 | wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1210 | |
1211 | // nothing to do - we don't use this | |
1212 | } | |
1213 | ||
1214 | // ---------------------------------------------------------------------------- | |
1215 | // callbacks | |
1216 | // ---------------------------------------------------------------------------- | |
1217 | ||
1218 | static DWORD wxRasMonitorThread(wxRasThreadData *data) | |
1219 | { | |
1220 | HANDLE handles[2]; | |
1221 | handles[0] = data->hEventRas; | |
1222 | handles[1] = data->hEventQuit; | |
1223 | ||
1224 | bool cont = TRUE; | |
1225 | while ( cont ) | |
1226 | { | |
1227 | DWORD dwRet = WaitForMultipleObjects(2, handles, FALSE, INFINITE); | |
1228 | ||
1229 | switch ( dwRet ) | |
1230 | { | |
1231 | case WAIT_OBJECT_0: | |
1232 | // RAS connection status changed | |
1233 | SendMessage(data->hWnd, wxWM_RAS_STATUS_CHANGED, | |
1234 | 0, (LPARAM)data); | |
1235 | break; | |
1236 | ||
1237 | case WAIT_OBJECT_0 + 1: | |
1238 | cont = FALSE; | |
1239 | break; | |
1240 | ||
1241 | case WAIT_FAILED: | |
f6bcfd97 | 1242 | wxLogLastError(wxT("WaitForMultipleObjects(RasMonitor)")); |
a0b4c98b VZ |
1243 | break; |
1244 | } | |
1245 | } | |
1246 | ||
1247 | return 0; | |
1248 | } | |
1249 | ||
1250 | static LRESULT APIENTRY wxRasStatusWindowProc(HWND hWnd, UINT message, | |
1251 | WPARAM wParam, LPARAM lParam) | |
1252 | { | |
1253 | if ( message == wxWM_RAS_STATUS_CHANGED ) | |
1254 | { | |
1255 | wxRasThreadData *data = (wxRasThreadData *)lParam; | |
1256 | data->dialUpManager->OnConnectStatusChange(); | |
1257 | } | |
2690830e VZ |
1258 | else if ( message == wxWM_RAS_DIALING_PROGRESS ) |
1259 | { | |
1260 | wxDialUpManagerMSW *dialUpManager = wxDialUpManagerMSW::GetDialer(); | |
1261 | ||
1262 | dialUpManager->OnDialProgress((RASCONNSTATE)wParam, lParam); | |
1263 | } | |
a0b4c98b VZ |
1264 | |
1265 | return 0; | |
1266 | } | |
1267 | ||
1268 | static void WINAPI wxRasDialFunc(UINT unMsg, | |
1269 | RASCONNSTATE rasconnstate, | |
1270 | DWORD dwError) | |
1271 | { | |
1272 | wxDialUpManagerMSW *dialUpManager = wxDialUpManagerMSW::GetDialer(); | |
1273 | ||
223d09f6 | 1274 | wxCHECK_RET( dialUpManager, wxT("who started to dial then?") ); |
a0b4c98b | 1275 | |
2690830e VZ |
1276 | SendMessage(dialUpManager->GetRasWindow(), wxWM_RAS_DIALING_PROGRESS, |
1277 | rasconnstate, dwError); | |
a0b4c98b VZ |
1278 | } |
1279 | ||
1c25d245 VZ |
1280 | #endif // __BORLANDC__ |
1281 | ||
a0b4c98b | 1282 | #endif // wxUSE_DIALUP_MANAGER |
4f89dbc4 | 1283 |