]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/testdir.cpp
port Dolphin from KUrl to QUrl
[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 #include <QDir>
23
24 #ifdef Q_OS_UNIX
25 #include <utime.h>
26 #else
27 #include <sys/utime.h>
28 #endif
29
30 TestDir::TestDir(const QString& directoryPrefix) :
31 KTempDir(directoryPrefix)
32 {
33 }
34
35 TestDir::~TestDir()
36 {
37 }
38
39 QUrl TestDir::url() const
40 {
41 return QUrl::fromLocalFile(name());
42 }
43
44 /** The following function is taken from kdelibs/kio/tests/kiotesthelper.h, copyright (C) 2006 by David Faure */
45 static void setTimeStamp(const QString& path, const QDateTime& mtime)
46 {
47 #ifdef Q_OS_UNIX
48 struct utimbuf utbuf;
49 utbuf.actime = mtime.toTime_t();
50 utbuf.modtime = utbuf.actime;
51 utime(QFile::encodeName(path), &utbuf);
52 #elif defined(Q_OS_WIN)
53 struct _utimbuf utbuf;
54 utbuf.actime = mtime.toTime_t();
55 utbuf.modtime = utbuf.actime;
56 _wutime(reinterpret_cast<const wchar_t *>(path.utf16()), &utbuf);
57 #endif
58 }
59
60 void TestDir::createFile(const QString& path, const QByteArray& data, const QDateTime& time)
61 {
62 QString absolutePath = path;
63 makePathAbsoluteAndCreateParents(absolutePath);
64
65 QFile f(absolutePath);
66 f.open(QIODevice::WriteOnly);
67 f.write(data);
68 f.close();
69
70 if (time.isValid()) {
71 setTimeStamp(absolutePath, time);
72 }
73
74 Q_ASSERT(QFile::exists(absolutePath));
75 }
76
77 void TestDir::createFiles(const QStringList& files)
78 {
79 foreach (const QString& path, files) {
80 createFile(path);
81 }
82 }
83
84 void TestDir::createDir(const QString& path, const QDateTime& time)
85 {
86 QString absolutePath = path;
87 makePathAbsoluteAndCreateParents(absolutePath);
88 QDir(name()).mkdir(absolutePath);
89
90 if (time.isValid()) {
91 setTimeStamp(absolutePath, time);
92 }
93
94 Q_ASSERT(QFile::exists(absolutePath));
95 }
96
97 void TestDir::removeFile(const QString& path)
98 {
99 QString absolutePath = path;
100 QFileInfo fileInfo(absolutePath);
101 if (!fileInfo.isAbsolute()) {
102 absolutePath = name() + path;
103 }
104 QFile::remove(absolutePath);
105 }
106
107 void TestDir::makePathAbsoluteAndCreateParents(QString& path)
108 {
109 QFileInfo fileInfo(path);
110 if (!fileInfo.isAbsolute()) {
111 path = name() + path;
112 fileInfo.setFile(path);
113 }
114
115 const QDir dir = fileInfo.dir();
116 if (!dir.exists()) {
117 createDir(dir.absolutePath());
118 }
119
120 Q_ASSERT(dir.exists());
121 }