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