]>
cloud.milkyroute.net Git - dolphin.git/blob - src/tests/testdir.cpp
1 /*****************************************************************************
2 * Copyright (C) 2010-2011 by Frank Reininghaus (frank78ac@googlemail.com) *
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. *
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. *
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 *****************************************************************************/
25 #include <sys/utime.h>
28 TestDir::TestDir(const QString
& directoryPrefix
) :
29 QTemporaryDir(directoryPrefix
)
37 QUrl
TestDir::url() const
39 return QUrl::fromLocalFile(path());
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
)
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
);
58 void TestDir::createFile(const QString
& path
, const QByteArray
& data
, const QDateTime
& time
)
60 QString absolutePath
= path
;
61 makePathAbsoluteAndCreateParents(absolutePath
);
63 QFile
f(absolutePath
);
64 f
.open(QIODevice::WriteOnly
);
69 setTimeStamp(absolutePath
, time
);
72 Q_ASSERT(QFile::exists(absolutePath
));
75 void TestDir::createFiles(const QStringList
& files
)
77 foreach (const QString
& path
, files
) {
82 void TestDir::createDir(const QString
& path
, const QDateTime
& time
)
84 QString absolutePath
= path
;
85 makePathAbsoluteAndCreateParents(absolutePath
);
86 QDir(TestDir::path()).mkdir(absolutePath
);
89 setTimeStamp(absolutePath
, time
);
92 Q_ASSERT(QFile::exists(absolutePath
));
95 void TestDir::removeFiles(const QStringList
& files
)
97 foreach (const QString
& path
, files
) {
102 void TestDir::removeFile(const QString
& path
)
104 QString absolutePath
= path
;
105 QFileInfo
fileInfo(absolutePath
);
106 if (!fileInfo
.isAbsolute()) {
107 absolutePath
= TestDir::path() + QLatin1Char('/') + path
;
109 QFile::remove(absolutePath
);
112 void TestDir::makePathAbsoluteAndCreateParents(QString
& path
)
114 QFileInfo
fileInfo(path
);
115 if (!fileInfo
.isAbsolute()) {
116 path
= TestDir::path() + QLatin1Char('/') + path
;
117 fileInfo
.setFile(path
);
120 const QDir dir
= fileInfo
.dir();
122 createDir(dir
.absolutePath());
125 Q_ASSERT(dir
.exists());