1
/***************************************************************************
2
cfilemanager.cpp - description
4
begin : Sat Aug 03 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
***************************************************************************/
29
#include <dclib/dcos.h>
30
#include <dclib/core/cbytearray.h>
31
#include <dclib/cconfig.h>
32
#include <dclib/core/cstring.h>
33
#include <dclib/core/cstringlist.h>
34
#include <dclib/core/cdir.h>
35
#include <dclib/core/cfile.h>
36
#include <dclib/cdownloadmanager.h>
37
#include <dclib/dclib.h>
38
#include <dclib/core/cmanager.h>
39
#include <dclib/core/cbz.h>
40
#include <dclib/core/che3.h>
41
#include <dclib/csearchindex.h>
42
#include <dclib/hash/mhash_tiger.h>
44
#include "cfilemanager.h"
46
class CFileType : public CObject {
51
virtual ~CFileType() {};
54
eFileTypes m_eFileType;
58
typedef struct filetype {
63
struct filetype FileType[] = {
75
{ "ACE", eftARCHIVE },
76
{ "BZ2", eftARCHIVE },
77
{ "CAB", eftARCHIVE },
78
{ "EX_", eftARCHIVE },
80
{ "LZH", eftARCHIVE },
81
{ "RAR", eftARCHIVE },
82
{ "RPM", eftARCHIVE },
83
{ "TAR", eftARCHIVE },
84
{ "TGZ", eftARCHIVE },
85
{ "ZIP", eftARCHIVE },
86
{ "ZOO", eftARCHIVE },
90
{ "CFG", eftDOCUMENT },
91
{ "CPP", eftDOCUMENT },
92
{ "DIZ", eftDOCUMENT },
93
{ "DOC", eftDOCUMENT },
95
{ "HLP", eftDOCUMENT },
96
{ "HTM", eftDOCUMENT },
97
{ "HTML", eftDOCUMENT },
98
{ "INI", eftDOCUMENT },
99
{ "INF", eftDOCUMENT },
100
{ "LOG", eftDOCUMENT },
101
{ "PHP", eftDOCUMENT },
102
{ "PS", eftDOCUMENT },
103
{ "PDF", eftDOCUMENT },
104
{ "SHTML", eftDOCUMENT },
105
{ "TXT", eftDOCUMENT },
106
{ "RFT", eftDOCUMENT },
107
{ "XML", eftDOCUMENT },
110
{ "BAT", eftAPPLICATION },
111
{ "CGI", eftAPPLICATION },
112
{ "COM", eftAPPLICATION },
113
{ "DLL", eftAPPLICATION },
114
{ "EXE", eftAPPLICATION },
115
{ "HQX", eftAPPLICATION },
116
{ "JS", eftAPPLICATION },
117
{ "SO", eftAPPLICATION },
118
{ "SYS", eftAPPLICATION },
119
{ "VXD", eftAPPLICATION },
122
{ "3DS", eftPICTURE },
123
{ "A11", eftPICTURE },
124
{ "ACB", eftPICTURE },
125
{ "ADC", eftPICTURE },
126
{ "ADI", eftPICTURE },
127
{ "AFI", eftPICTURE },
128
{ "AI", eftPICTURE },
129
{ "AIS", eftPICTURE },
130
{ "ANS", eftPICTURE },
131
{ "ART", eftPICTURE },
132
{ "B8", eftPICTURE },
133
{ "BMP", eftPICTURE },
134
{ "CBM", eftPICTURE },
135
{ "GIF", eftPICTURE },
136
{ "ICO", eftPICTURE },
137
{ "JPEG", eftPICTURE },
138
{ "JPG", eftPICTURE },
139
{ "PIC", eftPICTURE },
140
{ "PNG", eftPICTURE },
141
{ "PCX", eftPICTURE },
142
{ "TGA", eftPICTURE },
143
{ "TIF", eftPICTURE },
151
{ "MOVIE", eftVIDEO },
154
{ "MPEG", eftVIDEO },
160
{ "DIVX", eftVIDEO },
167
CFileManager::CFileManager()
169
m_pFileTypeList = new CStringList();
171
m_nFileBaseIndex = 0;
172
m_pSearchIndex = new CSearchIndex();
173
m_pShareList = new CShareList();
175
m_pFileManagerInfo = new CFileManagerInfo();
176
m_pFileManagerInfo->m_bCreateShareList = FALSE;
177
m_pFileManagerInfo->m_bCreateSearchIndex = FALSE;
178
m_pFileManagerInfo->m_nShareListPercent = 100;
182
if ( m_pShareList->Load() == FALSE )
188
if ( m_pSearchIndex->LoadIndex() == FALSE )
192
else if ( CConfig::Instance()->GetAutoRecreateShareList() )
194
if ( CalcShareSize() != m_pShareList->GetShareSize() )
201
if ( CConfig::Instance()->GetRecreateShareListTime() != 0 )
203
m_tCreateShareListTimeout = time(0)+CConfig::Instance()->GetRecreateShareListTime()*60*60;
207
m_tCreateShareListTimeout = 0;
211
m_pFileManagerCallback = new CCallback<CFileManager>( this, &CFileManager::FileManagerCallback );
212
CManager::Instance()->Add( m_pFileManagerCallback );
218
CFileManager::~CFileManager()
226
if ( m_pFileManagerCallback )
228
CManager::Instance()->Remove(m_pFileManagerCallback);
229
delete m_pFileManagerCallback;
232
if ( m_pFileNameList )
234
delete m_pFileNameList;
238
if ( m_pFileManagerInfo )
240
delete m_pFileManagerInfo;
241
m_pFileManagerInfo = 0;
244
if ( m_pFileTypeList )
246
delete m_pFileTypeList;
256
if ( m_pSearchIndex )
258
delete m_pSearchIndex;
266
int CFileManager::GetShareBuffer( eShareBufferType type, CByteArray * sharebuffer )
268
return m_pShareList->GetShareBuffer( type, sharebuffer );
272
unsigned long CFileManager::GetShareBufferSize( eShareBufferType type )
274
return m_pShareList->GetShareBufferSize(type);
278
ulonglong CFileManager::GetShareSize()
281
return m_pShareList->GetShareSize();
287
void CFileManager::InitFileTypeList()
290
CFileType * filetype;
292
for(i=0;FileType[i].sExt != 0;i++)
294
filetype = new CFileType();
295
filetype->m_eFileType = FileType[i].eFileType;
297
m_pFileTypeList->Add( FileType[i].sExt, filetype );
301
/** return the filetype */
302
eFileTypes CFileManager::GetFileType( CString file )
305
eFileTypes res=eftUNKNOWN;
306
CFileType * filetype;
309
ext = CDir::Extension(file);
313
if ( m_pFileTypeList->Get( ext.ToUpper(), (CObject*&)filetype ) == 0 )
315
res = filetype->m_eFileType;
318
if ( res == eftUNKNOWN )
319
printf("unknown extension: %s\n",ext.Data());
327
CStringList * CFileManager::Search( CString s )
329
if ( !m_pFileManagerInfo ||
331
(m_pFileManagerInfo->m_bCreateSearchIndex == TRUE) ||
332
(m_pFileManagerInfo->m_bCreateShareList == TRUE) )
337
return m_pSearchIndex->Search(s);
341
bool CFileManager::GetFileBaseObject( CString id, struct filebaseobject * fbo, CString & filename )
343
if ( !m_pFileManagerInfo ||
345
(m_pFileManagerInfo->m_bCreateSearchIndex == TRUE) ||
346
(m_pFileManagerInfo->m_bCreateShareList == TRUE) )
351
return m_pSearchIndex->GetFileBaseObject(id,fbo,filename);
355
bool CFileManager::CreateShareList()
361
if ( (m_pFileManagerInfo->m_bCreateSearchIndex == FALSE) &&
362
(m_pFileManagerInfo->m_bCreateShareList == FALSE) )
364
m_SharedFolders.Clear();
370
m_pSearchIndex->Reset();
372
if ( CConfig::Instance()->GetSharedFolders(&m_SharedFolders) != 0 )
374
if ( m_pFileNameList )
375
delete m_pFileNameList;
376
m_pFileNameList = new CStringList(25);
378
m_pFileManagerInfo->m_nShareListPercent = 0;
379
m_pFileManagerInfo->m_bCreateShareList = TRUE;
381
if ( CDownloadManager::Instance() )
382
CDownloadManager::Instance()->SendFileManagerInfo(m_pFileManagerInfo);
388
m_pShareList->SetShareBuffer( "", 0 );
390
m_pSearchIndex->SaveIndex();
405
void CFileManager::CreateSearchIndex()
407
if ( (m_pFileManagerInfo->m_bCreateSearchIndex == FALSE) &&
408
(m_pFileManagerInfo->m_bCreateShareList == FALSE) )
410
m_nFileBaseIndex = 0;
412
m_pSearchIndex->ResetIndex();
414
m_pFileManagerInfo->m_nSearchIndexPercent = 0;
415
m_pFileManagerInfo->m_bCreateSearchIndex = TRUE;
422
bool CFileManager::CreateHashList()
428
void CFileManager::ThreadCreateSearchIndex()
436
if ( (s = m_pSearchIndex->GetFileName(m_nFileBaseIndex)) != "" )
440
m_pSearchIndex->AddIndex(s,m_nFileBaseIndex);
442
// convert special chars
443
s = s.Replace(' ',"/");
444
s = s.Replace('.',"/");
445
s = s.Replace('-',"/");
446
s = s.Replace('_',"/");
447
s = s.Replace('(',"/");
448
s = s.Replace(')',"/");
449
s = s.Replace('!',"/");
451
s = CDir().ConvertSeparators(s);
453
while( (i=s.Find(DIRSEPARATOR)) != -1 )
456
s = s.Mid(i+1,s.Length()-i-1);
461
if ( s1.Length() < 3 )
464
m_pSearchIndex->AddIndex(s1,m_nFileBaseIndex);
471
m_pSearchIndex->InitIndex();
473
//delete m_pSearchArray;
474
//m_pSearchArray = 0;
476
printf("ready create index\n");
478
m_pSearchIndex->SaveIndex();
480
m_pFileManagerInfo->m_nSearchIndexPercent = 100;
481
m_pFileManagerInfo->m_bCreateSearchIndex = FALSE;
483
if ( CDownloadManager::Instance() )
484
CDownloadManager::Instance()->SendFileManagerInfo(m_pFileManagerInfo);
492
if ( m_pSearchIndex->IndexCount() > 0 )
494
percent = (m_nFileBaseIndex*100.0)/m_pSearchIndex->IndexCount();
496
if ( m_pFileManagerInfo->m_nSearchIndexPercent != percent )
498
m_pFileManagerInfo->m_nSearchIndexPercent = percent;
500
if ( CDownloadManager::Instance() )
501
CDownloadManager::Instance()->SendFileManagerInfo(m_pFileManagerInfo);
507
void CFileManager::ThreadCreateShareList()
512
if ( (m_pShareFolder=m_SharedFolders.Next(m_pShareFolder)) != 0 )
515
s = m_pShareFolder->m_sPath;
518
if ( dir.cd(s.Data()) == TRUE )
525
s = s.Left( s.Length()-s1.Length() );
528
ThreadCreateShareList( 0, "", "", 100/m_SharedFolders.Count() );
532
printf("Can't change dir: '%s'\n",s.Data());
537
m_pFileManagerInfo->m_nShareListPercent = 100;
538
printf("ready create sharelist\n");
540
m_pShareList->SetShareBuffer( m_sShareBuffer, m_nShareSize );
542
if ( m_pFileNameList )
544
delete m_pFileNameList;
548
m_pFileManagerInfo->m_bCreateShareList = FALSE;
550
if ( CDownloadManager::Instance() )
551
CDownloadManager::Instance()->SendFileManagerInfo(m_pFileManagerInfo);
555
if ( CDownloadManager::Instance() )
556
CDownloadManager::Instance()->SendFileManagerInfo(m_pFileManagerInfo);
561
void CFileManager::ThreadCreateShareList( int depth, CString curr, CString rel, double percent )
564
CList<CFileInfo> list;
570
path = dir.SimplePath(m_pShareFolder->m_sPath+DIRSEPARATOR+curr+DIRSEPARATOR+rel);
577
// printf("%s\n",curr.Data());
579
// create depth string
580
for(x=depth;x>0;x--) d+="\x9";
584
m_sShareBuffer += m_pShareFolder->m_sAlias + "\xd\xa";
586
m_sShareBuffer += d + rel + "\xd\xa";
590
if ( dir.ReadEntrys( CDir::Files, &list ) )
594
while( (fileinfo=list.Next(fileinfo)) != 0 )
596
if ( fileinfo->name != "" )
598
if ( fileinfo->m_bSymlink )
599
s = dir.ReadLink(path+DIRSEPARATOR+fileinfo->name);
601
s = dir.SimplePath(path+DIRSEPARATOR+fileinfo->name);
605
if ( m_pFileNameList->Get(s,(CObject*&)ts) != 0 )
607
if ( dclibVerbose() > 1 )
608
printf("CreateShareList: Add file: '%s/%s'\n",path.Data(),fileinfo->name.Data());fflush(stdout);
610
m_pFileNameList->Add(s,new CString());
611
m_nShareSize += fileinfo->size;
612
m_sShareBuffer += d + fileinfo->name + "|" + CString().setNum(fileinfo->size) + "\xd\xa";
616
// add file to the file manager ...
617
m_pSearchIndex->AddIndex( fileinfo, dir.SimplePath(m_pShareFolder->m_sAlias+DIRSEPARATOR+curr+DIRSEPARATOR+rel), GetFileType(fileinfo->name) );
623
if ( dir.ReadEntrys( CDir::Dirs, &list ) )
625
if ( list.Count() > 0 )
626
percent /= list.Count();
628
m_pFileManagerInfo->m_nShareListPercent += percent;
632
while( (fileinfo=list.Next(fileinfo)) != 0 )
634
if ( (fileinfo->name != ".") && (fileinfo->name != "..") )
636
if ( fileinfo->m_bSymlink )
637
s = dir.ReadLink(path+DIRSEPARATOR+fileinfo->name);
639
s = dir.SimplePath(path+DIRSEPARATOR+fileinfo->name);
643
if ( m_pFileNameList->Get(s,(CObject*&)ts) != 0 )
645
if ( dclibVerbose() > 0 )
646
printf("CreateShareList: Add path: '%s/%s'\n",path.Data(),fileinfo->name.Data());fflush(stdout);
648
m_pFileNameList->Add(s,new CString());
649
ThreadCreateShareList(z,curr+DIRSEPARATOR+rel,fileinfo->name,percent);
652
m_pFileManagerInfo->m_nShareListPercent += percent;
657
m_pFileManagerInfo->m_nShareListPercent += percent;
659
if ( CDownloadManager::Instance() )
660
CDownloadManager::Instance()->SendFileManagerInfo(m_pFileManagerInfo);
666
ulonglong CFileManager::CalcShareSize()
675
if ( CConfig::Instance()->GetSharedFolders(&m_SharedFolders) == 0 )
677
printf("No share folderfound !");
681
while ( (m_pShareFolder=m_SharedFolders.Next(m_pShareFolder)) != 0 )
684
s = m_pShareFolder->m_sPath;
687
if ( dir.cd(s.Data()) == TRUE )
694
s = s.Left( s.Length()-s1.Length() );
697
iSSize += CalcShareSize( 0, s, s1, "" );
701
printf("Can't change to dir: '%s'\n",s.Data());
710
ulonglong CFileManager::CalcShareSize( int depth, CString base, CString curr, CString relpath )
712
ulonglong lShareSize;
714
CList<CFileInfo> list;
726
rpath = relpath+DIRSEPARATOR+curr;
731
path = base+DIRSEPARATOR+curr;
738
// create depth string
741
for(x=depth;x>0;x--) d+="\x9";
747
if ( dir.ReadEntrys( CDir::Files, &list ) )
750
while( (fileinfo=list.Next(fileinfo)) != 0 )
752
if ( fileinfo->name != "" )
754
if ( fileinfo->m_bSymlink )
755
s = dir.ReadLink(path+DIRSEPARATOR+fileinfo->name);
757
s = dir.SimplePath(path+DIRSEPARATOR+fileinfo->name);
761
lShareSize += fileinfo->size;
768
if ( dir.ReadEntrys( CDir::Dirs, &list ) )
771
while( (fileinfo=list.Next(fileinfo)) != 0 )
773
if ( (fileinfo->name != ".") && (fileinfo->name != "..") )
775
if ( fileinfo->m_bSymlink )
776
s = dir.ReadLink(path+DIRSEPARATOR+fileinfo->name);
778
s = dir.SimplePath(path+DIRSEPARATOR+fileinfo->name);
782
lShareSize += CalcShareSize(z,path,fileinfo->name,rpath);
791
void CFileManager::ThreadCreateHashList()
796
void CFileManager::Thread( CObject * )
802
if ( m_pFileManagerInfo->m_bCreateShareList == TRUE )
804
ThreadCreateShareList();
806
else if ( m_pFileManagerInfo->m_bCreateSearchIndex == TRUE )
808
ThreadCreateSearchIndex();
819
int CFileManager::FileManagerCallback( CObject *, CObject * )
821
if ( CConfig::Instance()->GetRecreateShareListTime() != 0 )
823
if ( m_tCreateShareListTimeout != 0 )
825
if ( time(0) > m_tCreateShareListTimeout )
829
m_tCreateShareListTimeout = time(0)+CConfig::Instance()->GetRecreateShareListTime()*60*60;
834
m_tCreateShareListTimeout = time(0)+CConfig::Instance()->GetRecreateShareListTime()*60*60;
839
m_tCreateShareListTimeout = 0;