1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.cpp
3 // Purpose: wxCurlSizeQueryThread
4 // Author: Francesco Montorsi
5 // Created: 2005/04/23
6 // RCS-ID: $Id: utils.cpp 1240 2010-03-10 23:54:25Z frm $
7 // Copyright: (c) 2007 Francesco Montorsi
8 // Licence: wxWidgets licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 // includes
20 #ifndef WX_PRECOMP
21 #include <wx/log.h>
22 #endif
23
24 #ifdef __WXMSW__
25 #include <wx/msw/msvcrt.h> // useful to catch memory leaks when compiling under MSVC
26 #endif
27
28 #include "wx/curl/utils.h"
29 #include "wx/curl/http.h"
30 #include "wx/curl/ftp.h"
31
32
DEFINE_EVENT_TYPE(wxCURL_SIZE_QUERY_EVENT)33 DEFINE_EVENT_TYPE(wxCURL_SIZE_QUERY_EVENT)
34
35
36
37 // -----------------------------------
38 // wxCurlSizeQueryOutput
39 // -----------------------------------
40
41 extern "C"
42 {
43 int wxcurl_size_query_progress_func(void* ptr, double rDlTotal, double WXUNUSED(rDlNow),
44 double WXUNUSED(rUlTotal), double WXUNUSED(rUlNow))
45 {
46 unsigned long *p = (unsigned long *)ptr;
47 if (p) *p = (unsigned long)rDlTotal;
48
49 // a non-zero value means to abort the transfer
50 // and that's what we want as we don't want to download the
51 // entire file but just get the rDlTotal info from the server
52 return 1;
53 }
54 }
55
56 // ---------------------
57 // wxCurlSizeQueryThread
58 // ---------------------
59
Entry()60 void *wxCurlSizeQueryThread::Entry()
61 {
62 wxLogDebug(wxS("wxSizeCacherThread::Entry - caching file sizes"));
63
64 wxMemoryOutputStream os;
65 bool allok = true;
66 wxCurlHTTP http;
67 wxCurlFTP ftp;
68
69 m_urlSize.Clear();
70 for (size_t i=0; i<m_urls.GetCount() && !TestDestroy(); i++)
71 {
72 unsigned long sz;
73
74 wxCurlProtocol prot = GetProtocolFromURL(m_urls[i]);
75 switch (prot)
76 {
77 case wxCP_HTTP:
78 {
79 http.OverrideProgressCallback(wxcurl_size_query_progress_func, &sz);
80 allok &= http.Get(os, m_urls[i]);
81 }
82 break;
83
84 case wxCP_FTP:
85 {
86 ftp.OverrideProgressCallback(wxcurl_size_query_progress_func, &sz);
87 allok &= ftp.Get(os, m_urls[i]);
88 }
89 break;
90
91 default:
92 sz = (unsigned long)-1;
93 wxFAIL;
94 }
95
96 m_urlSize.Add(sz);
97
98 // send the event
99 wxCurlSizeEvent ev(GetId(), m_urls[i], sz);
100 wxPostEvent(GetEvtHandler(), ev);
101 }
102
103 wxLogDebug(wxS("wxSizeCacherThread::Entry - caching of file sizes completed"));
104 return (void *)allok;
105 }
106
OnExit()107 void wxCurlSizeQueryThread::OnExit()
108 {/*
109 // we'll use wxPostEvent to post this event since this is the
110 // only thread-safe way to post events !
111 wxCommandEvent updatevent(wxEVT_COMMAND_CACHESIZE_COMPLETE);
112
113 // the event handler must delete the wxArrayLong which we pass to it in the event !
114 updatevent.SetClientData(m_urlSizes);
115 if (m_pHandler)
116 wxPostEvent(m_pHandler, updatevent);*/
117 }
118
119