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

« back to all changes in this revision

Viewing changes to dclib/cfilemanager.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
                           cfilemanager.cpp  -  description
 
3
                             -------------------
 
4
    begin                : Sat Aug 03 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
#ifdef HAVE_CONFIG_H
 
19
#include <config.h>
 
20
#endif
 
21
 
 
22
#ifndef WIN32
 
23
#include <unistd.h>
 
24
#include <stdlib.h>
 
25
#endif
 
26
 
 
27
#include <string.h>
 
28
 
 
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>
 
43
 
 
44
#include "cfilemanager.h"
 
45
 
 
46
class CFileType : public CObject {
 
47
public:
 
48
        /** */
 
49
        CFileType() {};
 
50
        /** */
 
51
        virtual ~CFileType() {};
 
52
 
 
53
        /** */
 
54
        eFileTypes m_eFileType;
 
55
};
 
56
 
 
57
/** filetype list */
 
58
typedef struct filetype {
 
59
        const char* sExt;
 
60
        eFileTypes eFileType;
 
61
} filetype;
 
62
 
 
63
struct filetype FileType[] = {
 
64
        // MP3 2
 
65
        { "AIFF", eftMP3 },
 
66
        { "AU",   eftMP3 },
 
67
        { "MID",  eftMP3 },
 
68
        { "MP2",  eftMP3 },
 
69
        { "MP3",  eftMP3 },
 
70
        { "OGG",  eftMP3 },
 
71
        { "RA",   eftMP3 },
 
72
        { "WAV",  eftMP3 },
 
73
 
 
74
        // ARCHIVE 3
 
75
        { "ACE", eftARCHIVE },
 
76
        { "BZ2", eftARCHIVE },
 
77
        { "CAB", eftARCHIVE },
 
78
        { "EX_", eftARCHIVE },
 
79
        { "GZ",  eftARCHIVE },
 
80
        { "LZH", eftARCHIVE },
 
81
        { "RAR", eftARCHIVE },
 
82
        { "RPM", eftARCHIVE },
 
83
        { "TAR", eftARCHIVE },
 
84
        { "TGZ", eftARCHIVE },
 
85
        { "ZIP", eftARCHIVE },
 
86
        { "ZOO", eftARCHIVE },
 
87
        { "Z",   eftARCHIVE },
 
88
 
 
89
        // DOCUMENT 4
 
90
        { "CFG",   eftDOCUMENT },
 
91
        { "CPP",   eftDOCUMENT },
 
92
        { "DIZ",   eftDOCUMENT },
 
93
        { "DOC",   eftDOCUMENT },
 
94
        { "H",     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 },
 
108
 
 
109
        // APPL 5
 
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 },
 
120
 
 
121
        // PICTURE 6
 
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 },
 
144
 
 
145
        // VIDEO 7
 
146
        { "AVI",   eftVIDEO },
 
147
        { "ASF",   eftVIDEO },
 
148
        { "ASX",   eftVIDEO },
 
149
        { "DAT",   eftVIDEO },
 
150
        { "MOV",   eftVIDEO },
 
151
        { "MOVIE", eftVIDEO },
 
152
        { "MPG",   eftVIDEO },
 
153
        { "MP4",   eftVIDEO },
 
154
        { "MPEG",  eftVIDEO },
 
155
        { "OGM",   eftVIDEO },
 
156
        { "QT",    eftVIDEO },
 
157
        { "RM",    eftVIDEO },
 
158
        { "VIV",   eftVIDEO },
 
159
        { "WMV",   eftVIDEO },
 
160
        { "DIVX",  eftVIDEO },
 
161
 
 
162
        // END
 
163
        { 0, eftALL }
 
164
};
 
165
 
 
166
/** */
 
167
CFileManager::CFileManager()
 
168
{
 
169
        m_pFileTypeList   = new CStringList();
 
170
        m_pFileNameList   = 0;
 
171
        m_nFileBaseIndex  = 0;
 
172
        m_pSearchIndex    = new CSearchIndex();
 
173
        m_pShareList      = new CShareList();
 
174
 
 
175
        m_pFileManagerInfo = new CFileManagerInfo();
 
176
        m_pFileManagerInfo->m_bCreateShareList   = FALSE;
 
177
        m_pFileManagerInfo->m_bCreateSearchIndex = FALSE;
 
178
        m_pFileManagerInfo->m_nShareListPercent  = 100;
 
179
 
 
180
        InitFileTypeList();
 
181
 
 
182
        if ( m_pShareList->Load() == FALSE )
 
183
        {
 
184
                CreateShareList();
 
185
        }
 
186
        else
 
187
        {
 
188
                if ( m_pSearchIndex->LoadIndex() == FALSE )
 
189
                {
 
190
                        CreateShareList();
 
191
                }
 
192
                else if ( CConfig::Instance()->GetAutoRecreateShareList() )
 
193
                {
 
194
                        if ( CalcShareSize() != m_pShareList->GetShareSize() )
 
195
                        {
 
196
                                CreateShareList();
 
197
                        }
 
198
                }
 
199
        }
 
200
 
 
201
        if ( CConfig::Instance()->GetRecreateShareListTime() != 0 )
 
202
        {
 
203
                m_tCreateShareListTimeout = time(0)+CConfig::Instance()->GetRecreateShareListTime()*60*60;
 
204
        }
 
205
        else
 
206
        {
 
207
                m_tCreateShareListTimeout = 0;
 
208
        }
 
209
 
 
210
        // init timer
 
211
        m_pFileManagerCallback = new CCallback<CFileManager>( this, &CFileManager::FileManagerCallback );
 
212
        CManager::Instance()->Add( m_pFileManagerCallback );
 
213
 
 
214
        SetInstance(this);
 
215
}
 
216
 
 
217
/** */
 
218
CFileManager::~CFileManager()
 
219
{
 
220
        Stop();
 
221
 
 
222
        SetInstance(0);
 
223
 
 
224
        Lock();
 
225
 
 
226
        if ( m_pFileManagerCallback )
 
227
        {
 
228
                CManager::Instance()->Remove(m_pFileManagerCallback);
 
229
                delete m_pFileManagerCallback;
 
230
        }
 
231
 
 
232
        if ( m_pFileNameList )
 
233
        {
 
234
                delete m_pFileNameList;
 
235
                m_pFileNameList = 0;
 
236
        }
 
237
 
 
238
        if ( m_pFileManagerInfo )
 
239
        {
 
240
                delete m_pFileManagerInfo;
 
241
                m_pFileManagerInfo = 0;
 
242
        }
 
243
 
 
244
        if ( m_pFileTypeList )
 
245
        {
 
246
                delete m_pFileTypeList;
 
247
                m_pFileTypeList = 0;
 
248
        }
 
249
 
 
250
        if ( m_pShareList )
 
251
        {
 
252
                delete m_pShareList;
 
253
                m_pShareList = 0;
 
254
        }
 
255
 
 
256
        if ( m_pSearchIndex )
 
257
        {
 
258
                delete m_pSearchIndex;
 
259
                m_pSearchIndex = 0;
 
260
        }
 
261
 
 
262
        UnLock();
 
263
}
 
264
 
 
265
/** */
 
266
int CFileManager::GetShareBuffer( eShareBufferType type, CByteArray * sharebuffer )
 
267
{
 
268
        return m_pShareList->GetShareBuffer( type, sharebuffer );
 
269
}
 
270
 
 
271
/** */
 
272
unsigned long CFileManager::GetShareBufferSize( eShareBufferType type )
 
273
{
 
274
        return m_pShareList->GetShareBufferSize(type);
 
275
}
 
276
 
 
277
/** */
 
278
ulonglong CFileManager::GetShareSize()
 
279
{
 
280
        if ( m_pShareList )
 
281
                return m_pShareList->GetShareSize();
 
282
        else
 
283
                return 0;
 
284
}
 
285
 
 
286
/** */
 
287
void CFileManager::InitFileTypeList()
 
288
{
 
289
        int i;
 
290
        CFileType * filetype;
 
291
 
 
292
        for(i=0;FileType[i].sExt != 0;i++)
 
293
        {
 
294
                filetype = new CFileType();
 
295
                filetype->m_eFileType = FileType[i].eFileType;
 
296
 
 
297
                m_pFileTypeList->Add( FileType[i].sExt, filetype );
 
298
        }
 
299
}
 
300
 
 
301
/** return the filetype */
 
302
eFileTypes CFileManager::GetFileType( CString file )
 
303
{
 
304
        CString ext;
 
305
        eFileTypes res=eftUNKNOWN;
 
306
        CFileType * filetype;
 
307
 
 
308
        // set the filetype
 
309
        ext = CDir::Extension(file);
 
310
 
 
311
        if ( ext != "" )
 
312
        {
 
313
                if ( m_pFileTypeList->Get( ext.ToUpper(), (CObject*&)filetype ) == 0 )
 
314
                {
 
315
                        res = filetype->m_eFileType;
 
316
                }
 
317
#if 0
 
318
                if ( res == eftUNKNOWN )
 
319
                        printf("unknown extension: %s\n",ext.Data());
 
320
#endif
 
321
        }
 
322
 
 
323
        return res;
 
324
}
 
325
 
 
326
/** */
 
327
CStringList * CFileManager::Search( CString s )
 
328
{
 
329
        if ( !m_pFileManagerInfo ||
 
330
             !m_pSearchIndex ||
 
331
             (m_pFileManagerInfo->m_bCreateSearchIndex == TRUE) ||
 
332
             (m_pFileManagerInfo->m_bCreateShareList == TRUE) )
 
333
        {
 
334
                return 0;
 
335
        }
 
336
 
 
337
        return m_pSearchIndex->Search(s);
 
338
}
 
339
 
 
340
/** */
 
341
bool CFileManager::GetFileBaseObject( CString id, struct filebaseobject * fbo, CString & filename )
 
342
{
 
343
        if ( !m_pFileManagerInfo ||
 
344
             !m_pSearchIndex ||
 
345
             (m_pFileManagerInfo->m_bCreateSearchIndex == TRUE) ||
 
346
             (m_pFileManagerInfo->m_bCreateShareList == TRUE) )
 
347
        {
 
348
                return FALSE;
 
349
        }
 
350
 
 
351
        return m_pSearchIndex->GetFileBaseObject(id,fbo,filename);
 
352
}
 
353
 
 
354
/** */
 
355
bool CFileManager::CreateShareList()
 
356
{
 
357
        bool err = FALSE;
 
358
 
 
359
        Lock();
 
360
 
 
361
        if ( (m_pFileManagerInfo->m_bCreateSearchIndex == FALSE) &&
 
362
             (m_pFileManagerInfo->m_bCreateShareList == FALSE) )
 
363
        {
 
364
                m_SharedFolders.Clear();
 
365
 
 
366
                m_pShareFolder = 0;
 
367
                m_nShareSize   = 0;
 
368
                m_sShareBuffer = "";
 
369
 
 
370
                m_pSearchIndex->Reset();
 
371
 
 
372
                if ( CConfig::Instance()->GetSharedFolders(&m_SharedFolders) != 0 )
 
373
                {
 
374
                        if ( m_pFileNameList )
 
375
                                delete m_pFileNameList;
 
376
                        m_pFileNameList  = new CStringList(25);
 
377
 
 
378
                        m_pFileManagerInfo->m_nShareListPercent = 0;
 
379
                        m_pFileManagerInfo->m_bCreateShareList  = TRUE;
 
380
 
 
381
                        if ( CDownloadManager::Instance() )
 
382
                                CDownloadManager::Instance()->SendFileManagerInfo(m_pFileManagerInfo);
 
383
 
 
384
                        err = TRUE;
 
385
                }
 
386
                else
 
387
                {
 
388
                        m_pShareList->SetShareBuffer( "", 0 );
 
389
 
 
390
                        m_pSearchIndex->SaveIndex();
 
391
                }
 
392
        }
 
393
 
 
394
        if ( err == TRUE )
 
395
        {       
 
396
                Start();
 
397
        }
 
398
 
 
399
        UnLock();
 
400
 
 
401
        return err;
 
402
}
 
403
 
 
404
/** */
 
405
void CFileManager::CreateSearchIndex()
 
406
{
 
407
        if ( (m_pFileManagerInfo->m_bCreateSearchIndex == FALSE) &&
 
408
             (m_pFileManagerInfo->m_bCreateShareList == FALSE) )
 
409
        {
 
410
                m_nFileBaseIndex = 0;
 
411
 
 
412
                m_pSearchIndex->ResetIndex();
 
413
 
 
414
                m_pFileManagerInfo->m_nSearchIndexPercent = 0;
 
415
                m_pFileManagerInfo->m_bCreateSearchIndex  = TRUE;
 
416
 
 
417
                Start();
 
418
        }
 
419
}
 
420
 
 
421
/** */
 
422
bool CFileManager::CreateHashList()
 
423
{
 
424
        return FALSE;
 
425
}
 
426
 
 
427
/** */
 
428
void CFileManager::ThreadCreateSearchIndex()
 
429
{
 
430
        int i,k;
 
431
        CString s,s1,id;
 
432
        double percent;
 
433
 
 
434
        for(k=0;k<100;k++)
 
435
        {
 
436
                if ( (s = m_pSearchIndex->GetFileName(m_nFileBaseIndex)) != "" )
 
437
                {
 
438
                        s = s.ToUpper();
 
439
 
 
440
                        m_pSearchIndex->AddIndex(s,m_nFileBaseIndex);
 
441
 
 
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('!',"/");
 
450
 
 
451
                        s = CDir().ConvertSeparators(s);
 
452
 
 
453
                        while( (i=s.Find(DIRSEPARATOR)) != -1 )
 
454
                        {
 
455
                                s1 = s.Left(i);
 
456
                                s  = s.Mid(i+1,s.Length()-i-1);
 
457
 
 
458
                                i++;
 
459
 
 
460
                                // max length is 3
 
461
                                if ( s1.Length() < 3 )
 
462
                                        continue;
 
463
 
 
464
                                m_pSearchIndex->AddIndex(s1,m_nFileBaseIndex);
 
465
                        }
 
466
 
 
467
                        m_nFileBaseIndex++;
 
468
                }
 
469
                else
 
470
                {
 
471
                        m_pSearchIndex->InitIndex();
 
472
 
 
473
                        //delete m_pSearchArray;
 
474
                        //m_pSearchArray = 0;
 
475
 
 
476
                        printf("ready create index\n");
 
477
 
 
478
                        m_pSearchIndex->SaveIndex();
 
479
 
 
480
                        m_pFileManagerInfo->m_nSearchIndexPercent = 100;
 
481
                        m_pFileManagerInfo->m_bCreateSearchIndex  = FALSE;
 
482
 
 
483
                        if ( CDownloadManager::Instance() )
 
484
                                CDownloadManager::Instance()->SendFileManagerInfo(m_pFileManagerInfo);
 
485
 
 
486
                        Stop();
 
487
 
 
488
                        return;
 
489
                }
 
490
        }
 
491
 
 
492
        if ( m_pSearchIndex->IndexCount() > 0 )
 
493
        {
 
494
                percent = (m_nFileBaseIndex*100.0)/m_pSearchIndex->IndexCount();
 
495
 
 
496
                if ( m_pFileManagerInfo->m_nSearchIndexPercent != percent )
 
497
                {
 
498
                        m_pFileManagerInfo->m_nSearchIndexPercent = percent;
 
499
 
 
500
                        if ( CDownloadManager::Instance() )
 
501
                                CDownloadManager::Instance()->SendFileManagerInfo(m_pFileManagerInfo);
 
502
                }
 
503
        }
 
504
}
 
505
 
 
506
/** */
 
507
void CFileManager::ThreadCreateShareList()
 
508
{
 
509
        CString s,s1;
 
510
        CDir dir;
 
511
 
 
512
        if ( (m_pShareFolder=m_SharedFolders.Next(m_pShareFolder)) != 0 )
 
513
        {
 
514
                // directory
 
515
                s = m_pShareFolder->m_sPath;
 
516
 
 
517
                // is valid ?
 
518
                if ( dir.cd(s.Data()) == TRUE )
 
519
                {
 
520
                        s  = dir.Path();
 
521
                        s1 = dir.DirName();
 
522
 
 
523
                        if ( s1 != "" )
 
524
                        {
 
525
                                s = s.Left( s.Length()-s1.Length() );
 
526
                        }
 
527
 
 
528
                        ThreadCreateShareList( 0, "", "", 100/m_SharedFolders.Count() );
 
529
                }
 
530
                else
 
531
                {
 
532
                        printf("Can't change dir: '%s'\n",s.Data());
 
533
                }
 
534
        }
 
535
        else
 
536
        {
 
537
                m_pFileManagerInfo->m_nShareListPercent = 100;
 
538
                printf("ready create sharelist\n");
 
539
 
 
540
                m_pShareList->SetShareBuffer( m_sShareBuffer, m_nShareSize );
 
541
 
 
542
                if ( m_pFileNameList )
 
543
                {
 
544
                        delete m_pFileNameList;
 
545
                        m_pFileNameList = 0;
 
546
                }
 
547
 
 
548
                m_pFileManagerInfo->m_bCreateShareList = FALSE;
 
549
 
 
550
                if ( CDownloadManager::Instance() )
 
551
                        CDownloadManager::Instance()->SendFileManagerInfo(m_pFileManagerInfo);
 
552
 
 
553
                CreateSearchIndex();
 
554
 
 
555
                if ( CDownloadManager::Instance() )
 
556
                        CDownloadManager::Instance()->SendFileManagerInfo(m_pFileManagerInfo);
 
557
        }
 
558
}
 
559
 
 
560
/** */
 
561
void CFileManager::ThreadCreateShareList( int depth, CString curr, CString rel, double percent )
 
562
{
 
563
        CDir dir;
 
564
        CList<CFileInfo> list;
 
565
        CString d,s,*ts=0;
 
566
        CString path;
 
567
        CFileInfo *fileinfo;
 
568
        int x,z;
 
569
 
 
570
        path = dir.SimplePath(m_pShareFolder->m_sPath+DIRSEPARATOR+curr+DIRSEPARATOR+rel);
 
571
 
 
572
        dir.SetPath(path);
 
573
 
 
574
        d = "";
 
575
        z = depth;
 
576
 
 
577
//      printf("%s\n",curr.Data());
 
578
 
 
579
        // create depth string
 
580
        for(x=depth;x>0;x--) d+="\x9";
 
581
        z++;
 
582
        // use alias
 
583
        if ( depth == 0 )
 
584
                m_sShareBuffer += m_pShareFolder->m_sAlias + "\xd\xa";
 
585
        else
 
586
                m_sShareBuffer += d + rel + "\xd\xa";
 
587
        d += "\x9";
 
588
 
 
589
        // get all files
 
590
        if ( dir.ReadEntrys( CDir::Files, &list ) )
 
591
        {
 
592
                // add files
 
593
                fileinfo = 0;
 
594
                while( (fileinfo=list.Next(fileinfo)) != 0 )
 
595
                {
 
596
                        if ( fileinfo->name != "" )
 
597
                        {
 
598
                                if ( fileinfo->m_bSymlink )
 
599
                                        s = dir.ReadLink(path+DIRSEPARATOR+fileinfo->name);
 
600
                                else
 
601
                                        s = dir.SimplePath(path+DIRSEPARATOR+fileinfo->name);
 
602
 
 
603
                                if ( s != "" )
 
604
                                {
 
605
                                        if ( m_pFileNameList->Get(s,(CObject*&)ts) != 0 )
 
606
                                        {
 
607
                                                if ( dclibVerbose() > 1 )
 
608
                                                        printf("CreateShareList: Add file: '%s/%s'\n",path.Data(),fileinfo->name.Data());fflush(stdout);
 
609
 
 
610
                                                m_pFileNameList->Add(s,new CString());
 
611
                                                m_nShareSize   += fileinfo->size;
 
612
                                                m_sShareBuffer += d + fileinfo->name + "|" + CString().setNum(fileinfo->size) + "\xd\xa";
 
613
                                        }
 
614
                                }
 
615
 
 
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) );
 
618
                        }
 
619
                }
 
620
        }
 
621
 
 
622
        // get all dirs
 
623
        if ( dir.ReadEntrys( CDir::Dirs, &list ) )
 
624
        {
 
625
                if ( list.Count() > 0 )
 
626
                        percent /= list.Count();
 
627
                else
 
628
                        m_pFileManagerInfo->m_nShareListPercent += percent;
 
629
 
 
630
                // add dir
 
631
                fileinfo = 0;
 
632
                while( (fileinfo=list.Next(fileinfo)) != 0 )
 
633
                {
 
634
                        if ( (fileinfo->name != ".") && (fileinfo->name != "..") )
 
635
                        {
 
636
                                if ( fileinfo->m_bSymlink )
 
637
                                        s = dir.ReadLink(path+DIRSEPARATOR+fileinfo->name);
 
638
                                else
 
639
                                        s = dir.SimplePath(path+DIRSEPARATOR+fileinfo->name);
 
640
 
 
641
                                if ( s != "" )
 
642
                                {
 
643
                                        if ( m_pFileNameList->Get(s,(CObject*&)ts) != 0 )
 
644
                                        {
 
645
                                                if ( dclibVerbose() > 0 )
 
646
                                                        printf("CreateShareList: Add path: '%s/%s'\n",path.Data(),fileinfo->name.Data());fflush(stdout);
 
647
 
 
648
                                                m_pFileNameList->Add(s,new CString());
 
649
                                                ThreadCreateShareList(z,curr+DIRSEPARATOR+rel,fileinfo->name,percent);
 
650
                                        }
 
651
                                        else
 
652
                                                m_pFileManagerInfo->m_nShareListPercent += percent;
 
653
 
 
654
                                }
 
655
                        }
 
656
                        else
 
657
                                m_pFileManagerInfo->m_nShareListPercent += percent;
 
658
 
 
659
                        if ( CDownloadManager::Instance() )
 
660
                                CDownloadManager::Instance()->SendFileManagerInfo(m_pFileManagerInfo);
 
661
                }
 
662
        }
 
663
}
 
664
 
 
665
/** */
 
666
ulonglong CFileManager::CalcShareSize()
 
667
{
 
668
        CString s,s1;
 
669
        CDir dir;
 
670
        ulonglong iSSize;
 
671
 
 
672
        iSSize = 0;
 
673
        m_pShareFolder=0;
 
674
 
 
675
        if ( CConfig::Instance()->GetSharedFolders(&m_SharedFolders) == 0 )
 
676
        {
 
677
                printf("No share folderfound !");
 
678
                return 0;
 
679
        }
 
680
 
 
681
        while ( (m_pShareFolder=m_SharedFolders.Next(m_pShareFolder)) != 0 )
 
682
        {
 
683
                // directory
 
684
                s = m_pShareFolder->m_sPath;
 
685
 
 
686
                // is valid ?
 
687
                if ( dir.cd(s.Data()) == TRUE )
 
688
                {
 
689
                        s  = dir.Path();
 
690
                        s1 = dir.DirName();
 
691
 
 
692
                        if ( s1 != "" )
 
693
                        {
 
694
                                s = s.Left( s.Length()-s1.Length() );
 
695
                        }
 
696
 
 
697
                        iSSize += CalcShareSize( 0, s, s1, "" );
 
698
                }
 
699
                else
 
700
                {
 
701
                        printf("Can't change to dir: '%s'\n",s.Data());
 
702
                }
 
703
        }
 
704
 
 
705
        return iSSize;
 
706
}
 
707
 
 
708
 
 
709
/** */
 
710
ulonglong CFileManager::CalcShareSize( int depth, CString base, CString curr, CString relpath )
 
711
{
 
712
        ulonglong lShareSize;
 
713
        CDir dir;
 
714
        CList<CFileInfo> list;
 
715
        CString d,s;
 
716
        CString path;
 
717
        CString rpath;
 
718
        CFileInfo *fileinfo;
 
719
        int x,z;
 
720
 
 
721
        lShareSize = 0;
 
722
 
 
723
        if ( relpath == "" )
 
724
                rpath = curr;
 
725
        else
 
726
                rpath = relpath+DIRSEPARATOR+curr;
 
727
 
 
728
        if (base=="")
 
729
                path = curr;
 
730
        else
 
731
                path = base+DIRSEPARATOR+curr;
 
732
 
 
733
        dir.SetPath(path);
 
734
 
 
735
        d = "";
 
736
        z = depth;
 
737
 
 
738
        // create depth string
 
739
        if ( curr != "" )
 
740
        {
 
741
                for(x=depth;x>0;x--) d+="\x9";
 
742
                z++;
 
743
                d += "\x9";
 
744
        }
 
745
 
 
746
        // get all files
 
747
        if ( dir.ReadEntrys( CDir::Files, &list ) )
 
748
        {
 
749
                fileinfo = 0;
 
750
                while( (fileinfo=list.Next(fileinfo)) != 0 )
 
751
                {
 
752
                        if ( fileinfo->name != "" )
 
753
                        {
 
754
                                if ( fileinfo->m_bSymlink )
 
755
                                        s = dir.ReadLink(path+DIRSEPARATOR+fileinfo->name);
 
756
                                else
 
757
                                        s = dir.SimplePath(path+DIRSEPARATOR+fileinfo->name);
 
758
 
 
759
                                if ( s != "" )
 
760
                                {
 
761
                                        lShareSize += fileinfo->size;
 
762
                                }
 
763
                        }
 
764
                }
 
765
        }
 
766
 
 
767
        // get all dirs
 
768
        if ( dir.ReadEntrys( CDir::Dirs, &list ) )
 
769
        {
 
770
                fileinfo = 0;
 
771
                while( (fileinfo=list.Next(fileinfo)) != 0 )
 
772
                {
 
773
                        if ( (fileinfo->name != ".") && (fileinfo->name != "..") )
 
774
                        {
 
775
                                if ( fileinfo->m_bSymlink )
 
776
                                        s = dir.ReadLink(path+DIRSEPARATOR+fileinfo->name);
 
777
                                else
 
778
                                        s = dir.SimplePath(path+DIRSEPARATOR+fileinfo->name);
 
779
 
 
780
                                if ( s != "" )
 
781
                                {
 
782
                                        lShareSize += CalcShareSize(z,path,fileinfo->name,rpath);
 
783
                                }
 
784
                        }
 
785
                }
 
786
        }
 
787
        return lShareSize;
 
788
}
 
789
 
 
790
/** */
 
791
void CFileManager::ThreadCreateHashList()
 
792
{
 
793
}
 
794
 
 
795
/** */
 
796
void CFileManager::Thread( CObject * )
 
797
{
 
798
        int wait = 50;
 
799
 
 
800
        Lock();
 
801
 
 
802
        if ( m_pFileManagerInfo->m_bCreateShareList == TRUE )
 
803
        {
 
804
                ThreadCreateShareList();
 
805
        }
 
806
        else if ( m_pFileManagerInfo->m_bCreateSearchIndex == TRUE )
 
807
        {
 
808
                ThreadCreateSearchIndex();
 
809
 
 
810
                wait = 0;
 
811
        }
 
812
 
 
813
        UnLock();
 
814
 
 
815
        NanoSleep(wait);
 
816
}
 
817
 
 
818
/** */
 
819
int CFileManager::FileManagerCallback( CObject *, CObject * )
 
820
{
 
821
        if ( CConfig::Instance()->GetRecreateShareListTime() != 0 )
 
822
        {
 
823
                if ( m_tCreateShareListTimeout != 0 )
 
824
                {
 
825
                        if ( time(0) > m_tCreateShareListTimeout )
 
826
                        {
 
827
                                CreateShareList();
 
828
 
 
829
                                m_tCreateShareListTimeout = time(0)+CConfig::Instance()->GetRecreateShareListTime()*60*60;
 
830
                        }
 
831
                }
 
832
                else
 
833
                {
 
834
                        m_tCreateShareListTimeout = time(0)+CConfig::Instance()->GetRecreateShareListTime()*60*60;
 
835
                }
 
836
        }
 
837
        else
 
838
        {
 
839
                m_tCreateShareListTimeout = 0;
 
840
        }
 
841
 
 
842
        return 0;
 
843
}