1
/***************************************************************************
2
chublistmanager.cpp - description
4
begin : Don Mai 16 2002
5
copyright : (C) 2002-2003 by Mathias K�ster
6
email : mathen@users.berlios.de
7
***************************************************************************/
9
/***************************************************************************
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. *
16
***************************************************************************/
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>
34
#include "chublistmanager.h"
37
CHubListManager::CHubListManager()
39
m_pCallback = new CCallback<CHubListManager>( this, &CHubListManager::Callback );
40
CManager::Instance()->Add( m_pCallback );
42
if ( CConfig::Instance()->GetReloadHubListTime() != 0 )
44
m_nReloadHubListTimeout = time(0) + CConfig::Instance()->GetReloadHubListTime()*60*60;
48
m_nReloadHubListTimeout = 0;
53
m_pHubListUrlList = 0;
57
m_bGetHubListDone = FALSE;
63
CHubListManager::~CHubListManager()
69
CManager::Instance()->Remove( m_pCallback );
78
/** thread callbackfunction */
79
int CHubListManager::Callback( CObject *, CObject * )
83
// check reload hublist timeout
84
if ( CConfig::Instance() )
86
if ( CConfig::Instance()->GetReloadHubListTime() != 0 )
88
if ( m_nReloadHubListTimeout != 0 )
90
if ( time(0) >= m_nReloadHubListTimeout )
94
m_nReloadHubListTimeout = time(0) + CConfig::Instance()->GetReloadHubListTime()*60*60;
97
else // change from config
99
m_nReloadHubListTimeout = time(0) + CConfig::Instance()->GetReloadHubListTime()*60*60;
104
m_nReloadHubListTimeout = 0;
108
// cleanup all objects
109
if ( m_bGetHubListDone == TRUE )
117
if ( m_pHubListUrlList )
119
delete m_pHubListUrlList;
120
m_pHubListUrlList = 0;
123
if ( m_pHubListData )
125
delete m_pHubListData;
131
DCMessageGetHubList * msg = new DCMessageGetHubList();
135
if ( DC_CallBack(msg) == -1 )
140
m_bGetHubListDone = FALSE;
148
/** http callback function */
149
int CHubListManager::HttpCallBack( CObject *, CObject * object )
154
switch(((CDCMessage*)object)->m_eType)
156
case DC_MESSAGE_CONNECTION_STATE:
158
CMessageConnectionState *msg = (CMessageConnectionState*)object;
160
if ( msg->m_eState == estDISCONNECTED )
162
if ( (m_pHttp->GetHttpError() == 200) && (m_pHttp->GetData(&in) == TRUE) )
164
if ( m_pHttp->GetUrl().Find(".bz2") != -1 )
166
if ( bz2.Decompress( &in, &out ) == TRUE )
168
if ( m_pHttp->GetUrl().Find(".xml.") != -1 )
170
printf("XML Hublist not supported\n");
174
m_pHubListData->Append(out.Data(),out.Size());
175
m_pHubListData->Append("\xD\xA",2);
180
printf("bz2 decompress failed\n");
185
m_pHubListData->Append(in.Data(),in.Size());
186
m_pHubListData->Append("\xD\xA",2);
191
if ( m_pHttp->GetHttpError() == 302 )
193
m_pHttp->GetUrl(m_pHttp->GetLocation());
195
else if ( NextHubListUrl() == FALSE )
197
// parse public hub list in own thread
198
CCallback<CHubListManager> * c = new CCallback<CHubListManager>( this, &CHubListManager::ParsePublicHubList );
199
m_Thread.SetThreadCallBackFunction(c);
207
case DC_MESSAGE_TRANSFER:
209
if ( DC_CallBack(object) != -1 )
232
bool CHubListManager::GetPublicHubList()
238
// get hublist allready running
242
m_pHubListUrlList = new CList<DCConfigHubListUrl>();
244
// get all hublist urls
245
CConfig::Instance()->GetHubListUrlList(m_pHubListUrlList);
247
// check hublist url count
248
if ( m_pHubListUrlList->Count() == 0 )
250
delete m_pHubListUrlList;
251
m_pHubListUrlList = 0;
258
m_pHubListData = new CByteArray();
259
m_pHttp = new CHttp();
260
m_pHttp->SetCallBackFunction( new CCallback<CHubListManager>( this, &CHubListManager::HttpCallBack ) );
262
res = NextHubListUrl();
266
m_bGetHubListDone = TRUE;
270
DCMessageGetHubList * msg = new DCMessageGetHubList();
274
if ( DC_CallBack(msg) == -1 )
284
int CHubListManager::ParsePublicHubList( CObject * , CObject * )
291
if ( m_pHubListData->Size() == 0 )
294
m_Thread.SetThreadCallBackFunction(0);
295
m_bGetHubListDone = TRUE;
300
s.Set((const char*)m_pHubListData->Data(),m_pHubListData->Size());
302
while( (i = s.Find(0x0d,i)) != -1 )
304
line = s.Mid(i1,i-i1);
306
if ( !line.IsEmpty() )
308
s1 = line.Section( '|', 0, 0 );
309
s2 = line.Section( '|', 1, 1 );
310
s3 = line.Section( '|', 2, 2 );
311
s4 = line.Section( '|', 3, 3 );
313
// replace all spaces
314
s2 = s2.Replace(" ","");
316
CConfig::Instance()->AddPublicHub( s1, s2, s3, s4 );
324
if ( CConfig::Instance()->GetHubListStoreLocal() == TRUE )
326
CConfig::Instance()->SaveDCHub();
330
m_Thread.SetThreadCallBackFunction(0);
331
m_bGetHubListDone = TRUE;
337
bool CHubListManager::NextHubListUrl()
341
while( (m_pHubListUrl=m_pHubListUrlList->Next(m_pHubListUrl)) != 0 )
343
if ( m_pHubListUrl->bEnabled == TRUE )
345
m_pHttp->GetUrl(m_pHubListUrl->sUrl);