1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/***************************************************************************
sq_threaddirlister.cpp - description
-------------------
begin : Feb 10 2007
copyright : (C) 2007 by Baryshev Dmitry
email : ksquirrel.iv@gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <tqfile.h>
#include <tqdir.h>
#include <tqobject.h>
#include <tqfileinfo.h>
#include <tqdatetime.h>
#include <tqapplication.h>
#include <kconfig.h>
#include <kglobal.h>
#include "sq_threaddirlister.h"
SQ_ThreadDirLister::SQ_ThreadDirLister(TQObject *o)
: TQThread(), obj(o)
{
cache = new KConfig("ksquirrel-tree-cache");
dir = 0;
}
SQ_ThreadDirLister::~SQ_ThreadDirLister()
{
// sync() & delete
delete cache;
}
void SQ_ThreadDirLister::run()
{
KURL url;
TQString path, name, filepath;
dirent *file;
int count_files, count_dirs;
TQDateTime dt, dt_def;
TQFileInfo fi;
bool b_read;
static const TQString &dot = KGlobal::staticQString(".");
static const TQString &dotdot = KGlobal::staticQString("..");
while(true)
{
waitMutex();
count_files = todo.count();
unlock();
if(!count_files) break;
waitMutex();
url = todo.first();
unlock();
path = url.path();
count_files = count_dirs = 0;
b_read = true;
fi.setFile(path);
if(cache->hasGroup(path))
{
cache->setGroup(path);
dt = cache->readDateTimeEntry("modified", &dt_def);
// compare cached time and real time
if(dt.isValid() && fi.lastModified() == dt)
{
count_files = cache->readNumEntry("count_files", 0);
count_dirs = cache->readNumEntry("count_dirs", 0);
b_read = false;
}
}
if(b_read)
{
dir = opendir(TQFile::encodeName(path));
dt = fi.lastModified(); // save directory last modified time
if(dir)
{
while((file = readdir(dir)))
{
name = TQFile::decodeName(file->d_name);
if(name != dot && name != dotdot)
{
filepath = path + TQDir::separator() + name;
fi.setFile(filepath);
if(fi.isDir())
count_dirs++;
else
count_files++;
}
}
closeDir();
}
else // opendir() failed, this value won't be cached
b_read = false;
}
waitMutex();
todo.pop_front();
unlock();
// cache only new values
if(b_read)
{
cache->setGroup(path);
cache->writeEntry("modified", dt);
cache->writeEntry("count_files", count_files);
cache->writeEntry("count_dirs", count_dirs);
}
TQApplication::postEvent(obj, new SQ_ItemsEvent(url, count_files, count_dirs));
}// while
}
void SQ_ThreadDirLister::closeDir()
{
if(dir)
{
closedir(dir);
dir = 0;
}
}
|