~ubuntu-branches/debian/sid/libdc0/sid

« back to all changes in this revision

Viewing changes to dclib/chublistmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Pasi Savilaakso
  • Date: 2004-06-05 23:01:37 UTC
  • Revision ID: james.westby@ubuntu.com-20040605230137-2ty5g6rcfyguk4et
Tags: upstream-0.3.2
Import upstream version 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          chublistmanager.cpp  -  description
 
3
                             -------------------
 
4
    begin                : Don Mai 16 2002
 
5
    copyright            : (C) 2002-2003 by Mathias K�ster
 
6
    email                : mathen@users.berlios.de
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
11
 *   This program is free software; you can redistribute it and/or modify  *
 
12
 *   it under the terms of the GNU General Public License as published by  *
 
13
 *   the Free Software Foundation; either version 2 of the License, or     *
 
14
 *   (at your option) any later version.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
 
 
18
#include <dclib/cclient.h>
 
19
#include <dclib/cconfig.h>
 
20
#include <dclib/core/cmanager.h>
 
21
#include <dclib/cdownloadmanager.h>
 
22
#include <dclib/chubsearch.h>
 
23
#include <dclib/dcobject.h>
 
24
#include <dclib/core/cbytearray.h>
 
25
#include <dclib/chttp.h>
 
26
#include <dclib/core/cbz.h>
 
27
#include <dclib/cfilemanager.h>
 
28
 
 
29
#ifndef WIN32
 
30
#include <unistd.h>
 
31
#include <stdlib.h>
 
32
#endif
 
33
 
 
34
#include "chublistmanager.h"
 
35
 
 
36
/** */
 
37
CHubListManager::CHubListManager()
 
38
{
 
39
        m_pCallback = new CCallback<CHubListManager>( this, &CHubListManager::Callback );
 
40
        CManager::Instance()->Add( m_pCallback );
 
41
 
 
42
        if ( CConfig::Instance()->GetReloadHubListTime() != 0 )
 
43
        {
 
44
                m_nReloadHubListTimeout = time(0) + CConfig::Instance()->GetReloadHubListTime()*60*60;
 
45
        }
 
46
        else
 
47
        {
 
48
                m_nReloadHubListTimeout = 0;
 
49
        }
 
50
 
 
51
        // get hublist stuff
 
52
        m_pHttp           = 0;
 
53
        m_pHubListUrlList = 0;
 
54
        m_pHubListUrl     = 0;
 
55
        m_pHubListData    = 0;
 
56
 
 
57
        m_bGetHubListDone = FALSE;
 
58
 
 
59
        SetInstance(this);
 
60
}
 
61
 
 
62
/** */
 
63
CHubListManager::~CHubListManager()
 
64
{
 
65
        m_Thread.Stop();
 
66
 
 
67
        SetInstance(0);
 
68
 
 
69
        CManager::Instance()->Remove( m_pCallback );
 
70
 
 
71
        if (m_pCallback)
 
72
        {
 
73
                delete m_pCallback;
 
74
                m_pCallback = 0;
 
75
        }
 
76
}
 
77
 
 
78
/** thread callbackfunction */
 
79
int CHubListManager::Callback( CObject *, CObject * )
 
80
{
 
81
        m_Thread.Lock();
 
82
 
 
83
        // check reload hublist timeout
 
84
        if ( CConfig::Instance() )
 
85
        {
 
86
                if ( CConfig::Instance()->GetReloadHubListTime() != 0 )
 
87
                {
 
88
                        if ( m_nReloadHubListTimeout != 0 )
 
89
                        {
 
90
                                if ( time(0) >= m_nReloadHubListTimeout )
 
91
                                {
 
92
                                        GetPublicHubList();
 
93
 
 
94
                                        m_nReloadHubListTimeout = time(0) + CConfig::Instance()->GetReloadHubListTime()*60*60;
 
95
                                }
 
96
                        }
 
97
                        else // change from config
 
98
                        {
 
99
                                m_nReloadHubListTimeout = time(0) + CConfig::Instance()->GetReloadHubListTime()*60*60;
 
100
                        }
 
101
                }
 
102
                else
 
103
                {
 
104
                        m_nReloadHubListTimeout = 0;
 
105
                }
 
106
        }
 
107
 
 
108
        // cleanup all objects
 
109
        if ( m_bGetHubListDone == TRUE )
 
110
        {
 
111
                if ( m_pHttp )
 
112
                {
 
113
                        delete m_pHttp;
 
114
                        m_pHttp = 0;
 
115
                }
 
116
 
 
117
                if ( m_pHubListUrlList )
 
118
                {
 
119
                        delete m_pHubListUrlList;
 
120
                        m_pHubListUrlList = 0;
 
121
                }
 
122
 
 
123
                if ( m_pHubListData )
 
124
                {
 
125
                        delete m_pHubListData;
 
126
                        m_pHubListData = 0;
 
127
                }
 
128
 
 
129
                m_pHubListUrl = 0;
 
130
 
 
131
                DCMessageGetHubList * msg = new DCMessageGetHubList();
 
132
 
 
133
                msg->m_bRun = FALSE;
 
134
 
 
135
                if ( DC_CallBack(msg) == -1 )
 
136
                {
 
137
                        delete msg;
 
138
                }
 
139
 
 
140
                m_bGetHubListDone = FALSE;
 
141
        }
 
142
 
 
143
        m_Thread.UnLock();
 
144
 
 
145
        return 0;
 
146
}
 
147
 
 
148
/** http callback function */
 
149
int CHubListManager::HttpCallBack( CObject *, CObject * object )
 
150
{
 
151
        CByteArray in,out;
 
152
        CBZ bz2;
 
153
 
 
154
        switch(((CDCMessage*)object)->m_eType)
 
155
        {
 
156
                case DC_MESSAGE_CONNECTION_STATE:
 
157
                {
 
158
                        CMessageConnectionState *msg = (CMessageConnectionState*)object;
 
159
 
 
160
                        if ( msg->m_eState == estDISCONNECTED )
 
161
                        {
 
162
                                if ( (m_pHttp->GetHttpError() == 200) && (m_pHttp->GetData(&in) == TRUE) )
 
163
                                {
 
164
                                        if ( m_pHttp->GetUrl().Find(".bz2") != -1 )
 
165
                                        {
 
166
                                                if ( bz2.Decompress( &in, &out ) == TRUE )
 
167
                                                {
 
168
                                                        if ( m_pHttp->GetUrl().Find(".xml.") != -1 )
 
169
                                                        {
 
170
                                                                printf("XML Hublist not supported\n");
 
171
                                                        }
 
172
                                                        else
 
173
                                                        {
 
174
                                                                m_pHubListData->Append(out.Data(),out.Size());
 
175
                                                                m_pHubListData->Append("\xD\xA",2);
 
176
                                                        }
 
177
                                                }
 
178
                                                else
 
179
                                                {
 
180
                                                        printf("bz2 decompress failed\n");
 
181
                                                }
 
182
                                        }
 
183
                                        else
 
184
                                        {
 
185
                                                m_pHubListData->Append(in.Data(),in.Size());
 
186
                                                m_pHubListData->Append("\xD\xA",2);
 
187
                                        }
 
188
                                }
 
189
 
 
190
                                // redirect
 
191
                                if ( m_pHttp->GetHttpError() == 302 )
 
192
                                {
 
193
                                        m_pHttp->GetUrl(m_pHttp->GetLocation());
 
194
                                }
 
195
                                else if ( NextHubListUrl() == FALSE )
 
196
                                {
 
197
                                        // parse public hub list in own thread
 
198
                                        CCallback<CHubListManager> * c = new CCallback<CHubListManager>( this, &CHubListManager::ParsePublicHubList );
 
199
                                        m_Thread.SetThreadCallBackFunction(c);
 
200
                                        m_Thread.Start();
 
201
                                }
 
202
                        }
 
203
 
 
204
                        break;
 
205
                }
 
206
 
 
207
                case DC_MESSAGE_TRANSFER:
 
208
                {
 
209
                        if ( DC_CallBack(object) != -1 )
 
210
                        {
 
211
                                object = 0;
 
212
                        }
 
213
 
 
214
                        break;
 
215
                }
 
216
 
 
217
                default:
 
218
                {
 
219
                        break;
 
220
                }
 
221
        }
 
222
 
 
223
        if ( object )
 
224
        {
 
225
                delete object;
 
226
        }
 
227
 
 
228
        return 0;
 
229
}
 
230
 
 
231
/** */
 
232
bool CHubListManager::GetPublicHubList()
 
233
{
 
234
        bool res = FALSE;
 
235
 
 
236
        if ( m_pHttp != 0 )
 
237
        {
 
238
                // get hublist allready running
 
239
                return res;
 
240
        }
 
241
 
 
242
        m_pHubListUrlList = new CList<DCConfigHubListUrl>();
 
243
 
 
244
        // get all hublist urls
 
245
        CConfig::Instance()->GetHubListUrlList(m_pHubListUrlList);
 
246
 
 
247
        // check hublist url count
 
248
        if ( m_pHubListUrlList->Count() == 0 )
 
249
        {
 
250
                delete m_pHubListUrlList;
 
251
                m_pHubListUrlList = 0;
 
252
                return res;
 
253
        }
 
254
 
 
255
        // init first url
 
256
        m_pHubListUrl = 0;
 
257
 
 
258
        m_pHubListData = new CByteArray();
 
259
        m_pHttp = new CHttp();
 
260
        m_pHttp->SetCallBackFunction( new CCallback<CHubListManager>( this, &CHubListManager::HttpCallBack ) );
 
261
 
 
262
        res = NextHubListUrl();
 
263
 
 
264
        if ( !res )
 
265
        {
 
266
                m_bGetHubListDone = TRUE;
 
267
        }
 
268
        else
 
269
        {
 
270
                DCMessageGetHubList * msg = new DCMessageGetHubList();
 
271
 
 
272
                msg->m_bRun = TRUE;
 
273
 
 
274
                if ( DC_CallBack(msg) == -1 )
 
275
                {
 
276
                        delete msg;
 
277
                }
 
278
        }
 
279
 
 
280
        return res;
 
281
}
 
282
 
 
283
/** */
 
284
int CHubListManager::ParsePublicHubList( CObject * , CObject * )
 
285
{
 
286
        CString line;
 
287
        CString s;
 
288
        CString s1,s2,s3,s4;
 
289
        long i=0,i1=0;
 
290
 
 
291
        if ( m_pHubListData->Size() == 0 )
 
292
        {
 
293
                m_Thread.Stop();
 
294
                m_Thread.SetThreadCallBackFunction(0);
 
295
                m_bGetHubListDone = TRUE;
 
296
 
 
297
                return 0;
 
298
        }
 
299
 
 
300
        s.Set((const char*)m_pHubListData->Data(),m_pHubListData->Size());
 
301
 
 
302
        while( (i = s.Find(0x0d,i)) != -1 )
 
303
        {
 
304
                line = s.Mid(i1,i-i1);
 
305
 
 
306
                if ( !line.IsEmpty() )
 
307
                {
 
308
                        s1 = line.Section( '|', 0, 0 );
 
309
                        s2 = line.Section( '|', 1, 1 );
 
310
                        s3 = line.Section( '|', 2, 2 );
 
311
                        s4 = line.Section( '|', 3, 3 );
 
312
 
 
313
                        // replace all spaces
 
314
                        s2 = s2.Replace(" ","");
 
315
 
 
316
                        CConfig::Instance()->AddPublicHub( s1, s2, s3, s4 );
 
317
                }
 
318
 
 
319
                i1 = i+2;
 
320
                i += 2;
 
321
        }
 
322
 
 
323
        // store the list
 
324
        if ( CConfig::Instance()->GetHubListStoreLocal() == TRUE )
 
325
        {
 
326
                CConfig::Instance()->SaveDCHub();
 
327
        }
 
328
 
 
329
        m_Thread.Stop();
 
330
        m_Thread.SetThreadCallBackFunction(0);
 
331
        m_bGetHubListDone = TRUE;
 
332
 
 
333
        return 0;
 
334
}
 
335
 
 
336
/** */
 
337
bool CHubListManager::NextHubListUrl()
 
338
{
 
339
        bool res = FALSE;
 
340
 
 
341
        while( (m_pHubListUrl=m_pHubListUrlList->Next(m_pHubListUrl)) != 0 )
 
342
        {
 
343
                if ( m_pHubListUrl->bEnabled == TRUE )
 
344
                {
 
345
                        m_pHttp->GetUrl(m_pHubListUrl->sUrl);
 
346
 
 
347
                        res = TRUE;
 
348
                        break;
 
349
                }
 
350
        }
 
351
 
 
352
        return res;
 
353
}