Rubber-stamped by Brady Eidson.
[webbrowser.git] / WebCore / platform / qt / FileSystemQt.cpp
blob4093fad4ee2160f553b3d7e8d113f0f8aed499bd
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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
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.
32 #include "config.h"
33 #include "FileSystem.h"
35 #include "CString.h"
36 #include "PlatformString.h"
38 #include <QDateTime>
39 #include <QFile>
40 #include <QTemporaryFile>
41 #include <QFileInfo>
42 #include <QDateTime>
43 #include <QDir>
45 namespace WebCore {
47 bool fileExists(const String& path)
49 return QFile::exists(path);
53 bool deleteFile(const String& path)
55 return QFile::remove(path);
58 bool deleteEmptyDirectory(const String& path)
60 return QDir::root().rmdir(path);
63 bool getFileSize(const String& path, long long& result)
65 QFileInfo info(path);
66 result = info.size();
67 return info.exists();
70 bool getFileModificationTime(const String& path, time_t& result)
72 QFileInfo info(path);
73 result = info.lastModified().toTime_t();
74 return info.exists();
77 bool makeAllDirectories(const String& path)
79 return QDir::root().mkpath(path);
82 String pathByAppendingComponent(const String& path, const String& component)
84 return QDir::toNativeSeparators(QDir(path).filePath(component));
87 String homeDirectoryPath()
89 return QDir::homePath();
92 String pathGetFileName(const String& path)
94 return QFileInfo(path).fileName();
97 String directoryName(const String& path)
99 return String(QFileInfo(path).absolutePath());
102 Vector<String> listDirectory(const String& path, const String& filter)
104 Vector<String> entries;
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);
115 return entries;
118 CString openTemporaryFile(const char* prefix, PlatformFileHandle& handle)
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();
127 handle = invalidPlatformFileHandle;
128 return CString();
131 void closeFile(PlatformFileHandle& handle)
133 if (handle) {
134 handle->close();
135 delete handle;
139 int writeToFile(PlatformFileHandle handle, const char* data, int length)
141 if (handle && handle->exists() && handle->isWritable())
142 return handle->write(data, length);
144 return 0;
147 bool unloadModule(PlatformModule module)
149 #if defined(Q_WS_MAC)
150 CFRelease(module);
151 return true;
153 #elif defined(Q_OS_WIN)
154 return ::FreeLibrary(module);
156 #else
157 if (module->unload()) {
158 delete module;
159 return true;
162 return false;
163 #endif
168 // vim: ts=4 sw=4 et