]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/testdir.cpp
[Details mode] Allow to fill the column size of directories with actual size
[dolphin.git] / src / tests / testdir.cpp
1 /*****************************************************************************
2 * Copyright (C) 2010-2011 by Frank Reininghaus (frank78ac@googlemail.com) *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 *****************************************************************************/
19
20 #include "testdir.h"
21
22 #ifdef Q_OS_UNIX
23 #include <utime.h>
24 #else
25 #include <sys/utime.h>
26 #endif
27
28 TestDir::TestDir(const QString& directoryPrefix) :
29 QTemporaryDir(directoryPrefix)
30 {
31 }
32
33 TestDir::~TestDir()
34 {
35 }
36
37 QUrl TestDir::url() const
38 {
39 return QUrl::fromLocalFile(path());
40 }
41
42 /** The following function is taken from kdelibs/kio/tests/kiotesthelper.h, copyright (C) 2006 by David Faure */
43 static void setTimeStamp(const QString& path, const QDateTime& mtime)
44 {
45 #ifdef Q_OS_UNIX
46 struct utimbuf utbuf;
47 utbuf.actime = mtime.toTime_t();
48 utbuf.modtime = utbuf.actime;
49 utime(QFile::encodeName(path), &utbuf);
50 #elif defined(Q_OS_WIN)
51 struct _utimbuf utbuf;
52 utbuf.actime = mtime.toTime_t();
53 utbuf.modtime = utbuf.actime;
54 _wutime(reinterpret_cast<const wchar_t *>(path.utf16()), &utbuf);
55 #endif
56 }
57
58 void TestDir::createFile(const QString& path, const QByteArray& data, const QDateTime& time)
59 {
60 QString absolutePath = path;
61 makePathAbsoluteAndCreateParents(absolutePath);
62
63 QFile f(absolutePath);
64 f.open(QIODevice::WriteOnly);
65 f.write(data);
66 f.close();
67
68 if (time.isValid()) {
69 setTimeStamp(absolutePath, time);
70 }
71
72 Q_ASSERT(QFile::exists(absolutePath));
73 }
74
75 void TestDir::createFiles(const QStringList& files)
76 {
77 foreach (const QString& path, files) {
78 createFile(path);
79 }
80 }
81
82 void TestDir::createDir(const QString& path, const QDateTime& time)
83 {
84 QString absolutePath = path;
85 makePathAbsoluteAndCreateParents(absolutePath);
86 QDir(TestDir::path()).mkdir(absolutePath);
87
88 if (time.isValid()) {
89 setTimeStamp(absolutePath, time);
90 }
91
92 Q_ASSERT(QFile::exists(absolutePath));
93 }
94
95 void TestDir::removeFiles(const QStringList& files)
96 {
97 foreach (const QString& path, files) {
98 removeFile(path);
99 }
100 }
101
102 void TestDir::removeFile(const QString& path)
103 {
104 QString absolutePath = path;
105 QFileInfo fileInfo(absolutePath);
106 if (!fileInfo.isAbsolute()) {
107 absolutePath = TestDir::path() + QLatin1Char('/') + path;
108 }
109 QFile::remove(absolutePath);
110 }
111
112 void TestDir::makePathAbsoluteAndCreateParents(QString& path)
113 {
114 QFileInfo fileInfo(path);
115 if (!fileInfo.isAbsolute()) {
116 path = TestDir::path() + QLatin1Char('/') + path;
117 fileInfo.setFile(path);
118 }
119
120 const QDir dir = fileInfo.dir();
121 if (!dir.exists()) {
122 createDir(dir.absolutePath());
123 }
124
125 Q_ASSERT(dir.exists());
126 }