]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/testdir.cpp
Improves 79ccd0202d106a9c16b79a20cd79831a60dc5393 by allowing "/" in file names.
[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 /** The following function is taken from kdelibs/kio/tests/kiotesthelper.h, copyright (C) 2006 by David Faure */
31
32 static void setTimeStamp(const QString& path, const QDateTime& mtime)
33 {
34 #ifdef Q_OS_UNIX
35 struct utimbuf utbuf;
36 utbuf.actime = mtime.toTime_t();
37 utbuf.modtime = utbuf.actime;
38 utime(QFile::encodeName(path), &utbuf);
39 #elif defined(Q_OS_WIN)
40 struct _utimbuf utbuf;
41 utbuf.actime = mtime.toTime_t();
42 utbuf.modtime = utbuf.actime;
43 _wutime(reinterpret_cast<const wchar_t *>(path.utf16()), &utbuf);
44 #endif
45 }
46
47 void TestDir::createFile(const QString& path, const QByteArray& data, const QDateTime& time)
48 {
49 QString absolutePath = path;
50 makePathAbsoluteAndCreateParents(absolutePath);
51
52 QFile f(absolutePath);
53 f.open(QIODevice::WriteOnly);
54 f.write(data);
55 f.close();
56
57 if (time.isValid()) {
58 setTimeStamp(absolutePath, time);
59 }
60
61 Q_ASSERT(QFile::exists(absolutePath));
62 }
63
64 void TestDir::createFiles(const QStringList& files)
65 {
66 foreach(const QString& path, files) {
67 createFile(path);
68 }
69 }
70
71 void TestDir::createDir(const QString& path, const QDateTime& time)
72 {
73 QString absolutePath = path;
74 makePathAbsoluteAndCreateParents(absolutePath);
75 QDir(name()).mkdir(absolutePath);
76
77 if (time.isValid()) {
78 setTimeStamp(absolutePath, time);
79 }
80
81 Q_ASSERT(QFile::exists(absolutePath));
82 }
83
84 void TestDir::makePathAbsoluteAndCreateParents(QString& path)
85 {
86 QFileInfo fileInfo(path);
87 if (!fileInfo.isAbsolute()) {
88 path = name() + path;
89 fileInfo.setFile(path);
90 }
91
92 const QDir dir = fileInfo.dir();
93 if (!dir.exists()) {
94 createDir(dir.absolutePath());
95 }
96
97 Q_ASSERT(dir.exists());
98 }