~ubuntu-branches/ubuntu/trusty/bibletime/trusty

« back to all changes in this revision

Viewing changes to src/util/directoryutil.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Marsden
  • Date: 2009-11-18 17:30:00 UTC
  • mfrom: (1.3.4 upstream) (5.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20091118173000-endkhjz5qai88tvr
Tags: 2.4-1
* New upstream version 2.4
* debian/control: 
  - Replace incorrect bibletime-data Depends on lib4qt-gui
    with bibletime Depends on libqtgui4 (>= 4.4.0). (Closes: #556209).
  - Add Build-depends: on zlib1g-dev and libcurl4-gnutls-dev 
    (Closes: #556805).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*********
2
 
*
3
 
* This file is part of BibleTime's source code, http://www.bibletime.info/.
4
 
*
5
 
* Copyright 1999-2008 by the BibleTime developers.
6
 
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
7
 
*
8
 
**********/
9
 
 
10
 
#include "directoryutil.h"
11
 
 
12
 
//Qt includes
13
 
#include <QDir>
14
 
#include <QFile>
15
 
#include <QFileInfo>
16
 
#include <QFileInfoList>
17
 
#include <QDebug>
18
 
#include <QCoreApplication>
19
 
#include <QLocale>
20
 
 
21
 
namespace util {
22
 
 
23
 
namespace filesystem {
24
 
 
25
 
void DirectoryUtil::removeRecursive(const QString dir) {
26
 
        //Check for validity of argument
27
 
        if (dir.isEmpty()) return;
28
 
        QDir d(dir);
29
 
        if (!d.exists()) return;
30
 
 
31
 
        //remove all files in this dir
32
 
        d.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
33
 
        const QFileInfoList fileList = d.entryInfoList();
34
 
        for (QFileInfoList::const_iterator it_file = fileList.begin(); it_file != fileList.end(); it_file++)
35
 
        {
36
 
                d.remove( it_file->fileName() );
37
 
        }
38
 
        
39
 
        //remove all subdirs recursively
40
 
        d.setFilter( QDir::Dirs | QDir::NoSymLinks );
41
 
        const QFileInfoList dirList = d.entryInfoList();
42
 
        for (QFileInfoList::const_iterator it_dir = dirList.begin(); it_dir != dirList.end(); it_dir++)
43
 
        {
44
 
                if ( !it_dir->isDir() || it_dir->fileName() == "." || it_dir->fileName() == ".." ) {
45
 
                        continue;
46
 
                }
47
 
                removeRecursive( it_dir->absoluteFilePath() );
48
 
        }
49
 
        d.rmdir(dir);
50
 
}
51
 
 
52
 
/** Returns the size of the directory including the size of all it's files and it's subdirs.
53
 
 */
54
 
unsigned long DirectoryUtil::getDirSizeRecursive(const QString dir) {
55
 
        //Check for validity of argument
56
 
        QDir d(dir);
57
 
        if (!d.exists()) return 0;      
58
 
 
59
 
        unsigned long size = 0;
60
 
        
61
 
        //First get the size of all files int this folder
62
 
        d.setFilter(QDir::Files);
63
 
        const QFileInfoList infoList = d.entryInfoList();
64
 
        for (QFileInfoList::const_iterator it = infoList.begin(); it != infoList.end(); it++)
65
 
        {
66
 
                size += it->size();
67
 
        }
68
 
 
69
 
        //Then add the sizes of all subdirectories
70
 
        d.setFilter(QDir::Dirs);
71
 
        const QFileInfoList dirInfoList = d.entryInfoList();
72
 
        for (QFileInfoList::const_iterator it_dir = dirInfoList.begin(); it_dir != dirInfoList.end(); it_dir++)
73
 
        {
74
 
                if ( !it_dir->isDir() || it_dir->fileName() == "." || it_dir->fileName() == ".." ) {
75
 
                        continue;
76
 
                }
77
 
                size += getDirSizeRecursive( it_dir->absoluteFilePath() );
78
 
        }
79
 
        return size;
80
 
}
81
 
 
82
 
/**Recursively copies a directory, overwriting existing files*/
83
 
void DirectoryUtil::copyRecursive(QString src, QString dest){
84
 
        QDir srcDir(src);
85
 
        QDir destDir(dest);
86
 
        //Copy files
87
 
        QStringList files = srcDir.entryList(QDir::Files);
88
 
        for (QStringList::iterator it = files.begin(); it != files.end(); ++it){
89
 
                QFile currFile(src + "/" + *it);
90
 
                QString newFileLoc = dest + "/" + *it;
91
 
                QFile newFile(newFileLoc);
92
 
                newFile.remove();
93
 
                currFile.copy(newFileLoc);
94
 
        }
95
 
        QStringList dirs = srcDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
96
 
        for (QStringList::iterator it = dirs.begin(); it != dirs.end(); ++it){
97
 
                QString temp = *it;
98
 
                if (!destDir.cd(*it)){
99
 
                        destDir.mkdir(*it);
100
 
                }
101
 
                copyRecursive(src + "/" + *it, dest + "/" + *it);
102
 
        }
103
 
}
104
 
 
105
 
static QDir cachedIconDir;
106
 
static QDir cachedJavascriptDir;
107
 
static QDir cachedLicenseDir;
108
 
static QDir cachedPicsDir;
109
 
static QDir cachedLocaleDir;
110
 
static QDir cachedHandbookDir;
111
 
static QDir cachedHowtoDir;
112
 
static QDir cachedDisplayTemplatesDir;
113
 
static QDir cachedUserDisplayTemplatesDir;
114
 
static QDir cachedUserBaseDir;
115
 
static QDir cachedUserHomeDir;
116
 
static QDir cachedUserSessionsDir;
117
 
static QDir cachedUserCacheDir;
118
 
static QDir cachedUserIndexDir;
119
 
 
120
 
static bool dirCacheInitialized = false;
121
 
 
122
 
void DirectoryUtil::initDirectoryCache(void)
123
 
{
124
 
        QDir wDir( QCoreApplication::applicationDirPath() );
125
 
        wDir.makeAbsolute();
126
 
        
127
 
        if (!wDir.cdUp()) //installation prefix
128
 
        {
129
 
                qWarning() << "Cannot cd up from directory " << QCoreApplication::applicationDirPath();
130
 
                throw;
131
 
        }
132
 
        
133
 
        cachedIconDir = wDir; //icon dir
134
 
        if (!cachedIconDir.cd("share/bibletime/icons") || !cachedIconDir.isReadable())
135
 
        {
136
 
                qWarning() << "Cannot find icon directory relative to" << QCoreApplication::applicationDirPath();
137
 
                throw;
138
 
        }
139
 
 
140
 
        cachedJavascriptDir = wDir; 
141
 
        if (!cachedJavascriptDir.cd("share/bibletime/javascript") || !cachedJavascriptDir.isReadable())
142
 
        {
143
 
                qWarning() << "Cannot find javascript directory relative to" << QCoreApplication::applicationDirPath();
144
 
                throw;
145
 
        }
146
 
 
147
 
        cachedLicenseDir = wDir; 
148
 
        if (!cachedLicenseDir.cd("share/bibletime/license") || !cachedLicenseDir.isReadable())
149
 
        {
150
 
                qWarning() << "Cannot find license directory relative to" << QCoreApplication::applicationDirPath();
151
 
                throw;
152
 
        }
153
 
 
154
 
        cachedPicsDir = wDir; //icon dir
155
 
        if (!cachedPicsDir.cd("share/bibletime/pics") || !cachedPicsDir.isReadable())
156
 
        {
157
 
                qWarning() << "Cannot find icon directory relative to" << QCoreApplication::applicationDirPath();
158
 
                throw;
159
 
        }
160
 
 
161
 
        cachedLocaleDir = wDir;
162
 
        if (!cachedLocaleDir.cd("share/bibletime/locale")) {
163
 
                qWarning() << "Cannot find locale directory relative to" << QCoreApplication::applicationDirPath();
164
 
                throw;
165
 
        }
166
 
        
167
 
        QString localeName = QLocale::system().name();
168
 
        QString langCode = localeName.section('_', 0, 0);
169
 
        
170
 
        cachedHandbookDir = wDir;
171
 
        if (!cachedHandbookDir.cd(QString("share/bibletime/docs/handbook/") + localeName)) {
172
 
                if (!cachedHandbookDir.cd(QString("share/bibletime/docs/handbook/") + langCode)) {
173
 
                        if (!cachedHandbookDir.cd("share/bibletime/docs/handbook/en/")) {
174
 
                                qWarning() << "Cannot find handbook directory relative to" << QCoreApplication::applicationDirPath();
175
 
                                throw;
176
 
                        }
177
 
                }
178
 
        }
179
 
 
180
 
        cachedHowtoDir = wDir;
181
 
        if (!cachedHowtoDir.cd(QString("share/bibletime/docs/howto/") + localeName)) {
182
 
                if (!cachedHowtoDir.cd(QString("share/bibletime/docs/howto/") + langCode)) {
183
 
                        if (!cachedHowtoDir.cd("share/bibletime/docs/howto/en/")) {
184
 
                                qWarning() << "Cannot find handbook directory relative to" << QCoreApplication::applicationDirPath();
185
 
                                throw;
186
 
                        }
187
 
                }
188
 
        }
189
 
 
190
 
        cachedDisplayTemplatesDir = wDir; //display templates dir
191
 
        if (!cachedDisplayTemplatesDir.cd("share/bibletime/display-templates/")) {
192
 
                qWarning() << "Cannot find display template directory relative to" << QCoreApplication::applicationDirPath();
193
 
                throw;
194
 
        }
195
 
 
196
 
        cachedUserHomeDir = QDir::home();
197
 
 
198
 
        cachedUserBaseDir = QDir::home();
199
 
        if (!cachedUserBaseDir.cd(".bibletime")){
200
 
                bool success = cachedUserBaseDir.mkdir(".bibletime") && cachedUserBaseDir.cd(".bibletime");
201
 
                if (!success){
202
 
                        qWarning() << "Could not create user setting directory.";
203
 
                        throw;
204
 
                }
205
 
        }
206
 
 
207
 
        cachedUserSessionsDir = cachedUserBaseDir;
208
 
        if (!cachedUserSessionsDir.cd("sessions")){
209
 
                bool success = cachedUserSessionsDir.mkdir("sessions") && cachedUserSessionsDir.cd("sessions");
210
 
                if (!success){
211
 
                        qWarning() << "Could not create user sessions directory.";
212
 
                        throw;
213
 
                }
214
 
        }
215
 
 
216
 
        cachedUserCacheDir = cachedUserBaseDir;
217
 
        if (!cachedUserCacheDir.cd("cache")){
218
 
                bool success = cachedUserCacheDir.mkdir("cache") && cachedUserCacheDir.cd("cache");
219
 
                if (!success){
220
 
                        qWarning() << "Could not create user cache directory.";
221
 
                        throw;
222
 
                }
223
 
        }
224
 
 
225
 
        cachedUserIndexDir = cachedUserBaseDir;
226
 
        if (!cachedUserIndexDir.cd("indices")){
227
 
                bool success = cachedUserIndexDir.mkdir("indices") && cachedUserIndexDir.cd("indices");
228
 
                if (!success){
229
 
                        qWarning() << "Could not create user indices directory.";
230
 
                }
231
 
        }
232
 
        
233
 
        cachedUserDisplayTemplatesDir = cachedUserBaseDir;
234
 
        if (!cachedUserDisplayTemplatesDir.cd("display-templates")){
235
 
                bool success = cachedUserDisplayTemplatesDir.mkdir("display-templates") && cachedUserDisplayTemplatesDir.cd("display-templates");
236
 
                if (!success){
237
 
                        qWarning() << "Could not create user display templates directory.";
238
 
                }
239
 
        }
240
 
 
241
 
        dirCacheInitialized = true;
242
 
}
243
 
 
244
 
QDir DirectoryUtil::getIconDir(void)
245
 
246
 
        if (!dirCacheInitialized) initDirectoryCache();
247
 
        return cachedIconDir; 
248
 
}
249
 
 
250
 
QDir DirectoryUtil::getJavascriptDir(void)
251
 
252
 
        if (!dirCacheInitialized) initDirectoryCache();
253
 
        return cachedJavascriptDir; 
254
 
}
255
 
 
256
 
QDir DirectoryUtil::getLicenseDir(void)
257
 
258
 
        if (!dirCacheInitialized) initDirectoryCache();
259
 
        return cachedLicenseDir; 
260
 
}
261
 
 
262
 
QIcon DirectoryUtil::getIcon(const QString& name)
263
 
{
264
 
        static QMap<QString, QIcon> iconCache;
265
 
        //error if trying to use name directly...
266
 
        QString name2(name);
267
 
        QString plainName = name2.remove(".svg", Qt::CaseInsensitive);
268
 
        if (iconCache.contains(plainName)) {
269
 
                return iconCache.value(plainName);
270
 
        }
271
 
 
272
 
        QString iconDir = getIconDir().canonicalPath();
273
 
        QString iconFileName = iconDir + "/" + plainName + ".svg";
274
 
        if (QFile(iconFileName).exists())
275
 
        {
276
 
                QIcon ic = QIcon(iconFileName);
277
 
                iconCache.insert(plainName, ic);
278
 
                return ic;
279
 
        }
280
 
        else {
281
 
                iconFileName = iconDir + "/" + plainName + ".png";
282
 
                if (QFile(iconFileName).exists()) {
283
 
                        QIcon ic = QIcon(iconFileName);
284
 
                        iconCache.insert(plainName, ic);
285
 
                        return ic;
286
 
                } else {
287
 
                        qWarning() << "Cannot find icon file" << iconFileName << ", using default icon.";
288
 
                        iconFileName = iconDir + "/" + "/default.svg";
289
 
                        if (QFile(iconFileName).exists()) {
290
 
                                return QIcon(iconDir + "/default.svg");
291
 
                        } else {
292
 
                                return QIcon(iconDir + "default.png");
293
 
                        }
294
 
                }
295
 
        }
296
 
}
297
 
 
298
 
QDir DirectoryUtil::getPicsDir(void)
299
 
300
 
        if (!dirCacheInitialized) initDirectoryCache();
301
 
        return cachedPicsDir; 
302
 
}
303
 
 
304
 
QDir DirectoryUtil::getLocaleDir(void)
305
 
{
306
 
        if (!dirCacheInitialized) initDirectoryCache();
307
 
        return cachedLocaleDir;
308
 
}
309
 
 
310
 
QDir DirectoryUtil::getHandbookDir(void)
311
 
{
312
 
        if (!dirCacheInitialized) initDirectoryCache();
313
 
        return cachedHandbookDir;
314
 
}
315
 
 
316
 
QDir DirectoryUtil::getHowtoDir(void)
317
 
{
318
 
        if (!dirCacheInitialized) initDirectoryCache();
319
 
        return cachedHowtoDir;
320
 
}
321
 
 
322
 
QDir DirectoryUtil::getDisplayTemplatesDir(void)
323
 
{
324
 
        if (!dirCacheInitialized) initDirectoryCache();
325
 
        return cachedDisplayTemplatesDir;
326
 
}
327
 
 
328
 
QDir DirectoryUtil::getUserBaseDir(void)
329
 
{
330
 
        if (!dirCacheInitialized) initDirectoryCache();
331
 
        return cachedUserBaseDir;
332
 
}
333
 
 
334
 
QDir DirectoryUtil::getUserHomeDir(void)
335
 
{
336
 
        if (!dirCacheInitialized) initDirectoryCache();
337
 
        return cachedUserHomeDir;
338
 
}
339
 
 
340
 
QDir DirectoryUtil::getUserSessionsDir(void)
341
 
{
342
 
        if (!dirCacheInitialized) initDirectoryCache();
343
 
        return cachedUserSessionsDir;
344
 
}
345
 
 
346
 
QDir DirectoryUtil::getUserCacheDir(void)
347
 
{
348
 
        if (!dirCacheInitialized) initDirectoryCache();
349
 
        return cachedUserCacheDir;
350
 
}
351
 
 
352
 
QDir DirectoryUtil::getUserIndexDir(void)
353
 
{
354
 
        if (!dirCacheInitialized) initDirectoryCache();
355
 
        return cachedUserIndexDir;
356
 
}
357
 
 
358
 
QDir DirectoryUtil::getUserDisplayTemplatesDir(void)
359
 
{
360
 
        if (!dirCacheInitialized) initDirectoryCache();
361
 
        return cachedUserDisplayTemplatesDir;
362
 
}
363
 
 
364
 
 
365
 
} //end of namespace util::filesystem
366
 
 
367
 
} //end of namespace util
368