1/*
2 * Copyright (C) 2007 Staikos Computing Services Inc.
3 * Copyright (C) 2007 Holger Hans Peter Freyther
4 * Copyright (C) 2008 Apple, Inc. All rights reserved.
5 * Copyright (C) 2008 Collabora, Ltd. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1.  Redistributions of source code must retain the above copyright
12 *     notice, this list of conditions and the following disclaimer.
13 * 2.  Redistributions in binary form must reproduce the above copyright
14 *     notice, this list of conditions and the following disclaimer in the
15 *     documentation and/or other materials provided with the distribution.
16 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
17 *     its contributors may be used to endorse or promote products derived
18 *     from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "config.h"
33#include "FileSystem.h"
34
35#include "CString.h"
36#include "PlatformString.h"
37
38#include <QDateTime>
39#include <QFile>
40#include <QTemporaryFile>
41#include <QFileInfo>
42#include <QDateTime>
43#include <QDir>
44
45namespace WebCore {
46
47bool fileExists(const String& path)
48{
49    return QFile::exists(path);
50}
51
52
53bool deleteFile(const String& path)
54{
55    return QFile::remove(path);
56}
57
58bool deleteEmptyDirectory(const String& path)
59{
60    return QDir::root().rmdir(path);
61}
62
63bool getFileSize(const String& path, long long& result)
64{
65    QFileInfo info(path);
66    result = info.size();
67    return info.exists();
68}
69
70bool getFileModificationTime(const String& path, time_t& result)
71{
72    QFileInfo info(path);
73    result = info.lastModified().toTime_t();
74    return info.exists();
75}
76
77bool makeAllDirectories(const String& path)
78{
79    return QDir::root().mkpath(path);
80}
81
82String pathByAppendingComponent(const String& path, const String& component)
83{
84    return QDir::toNativeSeparators(QDir(path).filePath(component));
85}
86
87String homeDirectoryPath()
88{
89    return QDir::homePath();
90}
91
92String pathGetFileName(const String& path)
93{
94    return QFileInfo(path).fileName();
95}
96
97String directoryName(const String& path)
98{
99    return String(QFileInfo(path).absolutePath());
100}
101
102Vector<String> listDirectory(const String& path, const String& filter)
103{
104    Vector<String> entries;
105
106    QStringList nameFilters;
107    if (!filter.isEmpty())
108        nameFilters.append(filter);
109    QFileInfoList fileInfoList = QDir(path).entryInfoList(nameFilters, QDir::AllEntries | QDir::NoDotAndDotDot);
110    foreach (const QFileInfo fileInfo, fileInfoList) {
111        String entry = String(fileInfo.canonicalFilePath());
112        entries.append(entry);
113    }
114
115    return entries;
116}
117
118CString openTemporaryFile(const char* prefix, PlatformFileHandle& handle)
119{
120    QTemporaryFile* tempFile = new QTemporaryFile(QLatin1String(prefix));
121    tempFile->setAutoRemove(false);
122    QFile* temp = tempFile;
123    if (temp->open(QIODevice::ReadWrite)) {
124        handle = temp;
125        return String(temp->fileName()).utf8();
126    }
127    handle = invalidPlatformFileHandle;
128    return CString();
129}
130
131void closeFile(PlatformFileHandle& handle)
132{
133    if (handle) {
134        handle->close();
135        delete handle;
136    }
137}
138
139int writeToFile(PlatformFileHandle handle, const char* data, int length)
140{
141    if (handle && handle->exists() && handle->isWritable())
142        return handle->write(data, length);
143
144    return 0;
145}
146
147bool unloadModule(PlatformModule module)
148{
149#if defined(Q_WS_MAC)
150    CFRelease(module);
151    return true;
152
153#elif defined(Q_OS_WIN)
154    return ::FreeLibrary(module);
155
156#else
157    if (module->unload()) {
158        delete module;
159        return true;
160    }
161
162    return false;
163#endif
164}
165
166}
167
168// vim: ts=4 sw=4 et
169