QTfrontend/util/FileEngine.cpp
author unc0rr
Thu, 22 Nov 2012 01:19:16 +0400
branchflibqtfrontend
changeset 8092 08960209db8c
parent 8085 6c059add1560
child 8098 4efee370e2de
permissions -rw-r--r--
Try to use map preview facilities: - HWGame and HWRecorder are completely broken - HWMap should work, but doesn't
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     1
/* borrowed from https://github.com/skhaz/qt-physfs-wrapper
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     2
 * TODO: add copyright header, determine license
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     3
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     4
8052
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents: 7955
diff changeset
     5
#include "hwpacksmounter.h"
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     6
#include "FileEngine.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     7
7770
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
     8
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
     9
const QString FileEngineHandler::scheme = "physfs:/";
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
    10
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    11
FileEngine::FileEngine(const QString& filename)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    12
: _handler(NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    13
, _flags(0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    14
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    15
    setFileName(filename);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    16
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    17
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    18
FileEngine::~FileEngine()
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    19
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    20
    close();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    21
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    22
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    23
bool FileEngine::open(QIODevice::OpenMode openMode)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    24
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    25
    close();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    26
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    27
    if (openMode & QIODevice::WriteOnly) {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    28
        _handler = PHYSFS_openWrite(_filename.toUtf8().constData());
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    29
        _flags = QAbstractFileEngine::WriteOwnerPerm | QAbstractFileEngine::WriteUserPerm | QAbstractFileEngine::FileType;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    30
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    31
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    32
    else if (openMode & QIODevice::ReadOnly) {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    33
        _handler = PHYSFS_openRead(_filename.toUtf8().constData());
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    34
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    35
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    36
    else if (openMode & QIODevice::Append) {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    37
        _handler = PHYSFS_openAppend(_filename.toUtf8().constData());
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    38
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    39
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    40
    else {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    41
        qWarning("Bad file open mode: %d", (int)openMode);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    42
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    43
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    44
    if (!_handler) {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    45
        qWarning("Failed to open %s, reason: %s", _filename.toUtf8().constData(), PHYSFS_getLastError());
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    46
        return false;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    47
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    48
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    49
    return true;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    50
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    51
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    52
bool FileEngine::close()
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    53
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    54
    if (isOpened()) {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    55
        int result = PHYSFS_close(_handler);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    56
        _handler = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    57
        return result != 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    58
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    59
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    60
    return true;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    61
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    62
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    63
bool FileEngine::flush()
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    64
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    65
    return PHYSFS_flush(_handler) != 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    66
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    67
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    68
qint64 FileEngine::size() const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    69
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    70
    return _size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    71
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    72
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    73
qint64 FileEngine::pos() const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    74
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    75
    return PHYSFS_tell(_handler);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    76
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    77
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    78
bool FileEngine::seek(qint64 pos)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    79
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    80
    return PHYSFS_seek(_handler, pos) != 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    81
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    82
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    83
bool FileEngine::isSequential() const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    84
{
7770
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
    85
    return false;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    86
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    87
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    88
bool FileEngine::remove()
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    89
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    90
    return PHYSFS_delete(_filename.toUtf8().constData()) != 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    91
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    92
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    93
bool FileEngine::mkdir(const QString &dirName, bool createParentDirectories) const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    94
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    95
    Q_UNUSED(createParentDirectories);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    96
    return PHYSFS_mkdir(dirName.toUtf8().constData()) != 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    97
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    98
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    99
bool FileEngine::rmdir(const QString &dirName, bool recurseParentDirectories) const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   100
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   101
    Q_UNUSED(recurseParentDirectories);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   102
    return PHYSFS_delete(dirName.toUtf8().constData()) != 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   103
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   104
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   105
bool FileEngine::caseSensitive() const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   106
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   107
    return true;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   108
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   109
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   110
bool FileEngine::isRelativePath() const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   111
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   112
    return true;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   113
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   114
7770
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   115
QAbstractFileEngineIterator * FileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames)
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   116
{
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   117
    return new FileEngineIterator(filters, filterNames, entryList(filters, filterNames));
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   118
}
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   119
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   120
QStringList FileEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   121
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   122
    Q_UNUSED(filters);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   123
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   124
    QString file;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   125
    QStringList result;
7770
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   126
    char **files = PHYSFS_enumerateFiles(_filename.toUtf8().constData());
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   127
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   128
    for (char **i = files; *i != NULL; i++) {
7770
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   129
        file = QString::fromUtf8(*i);
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   130
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   131
        if (filterNames.isEmpty() || QDir::match(filterNames, file)) {
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   132
            result << file;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   133
        }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   134
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   135
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   136
    PHYSFS_freeList(files);
7770
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   137
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   138
    return result;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   139
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   140
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   141
QAbstractFileEngine::FileFlags FileEngine::fileFlags(FileFlags type) const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   142
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   143
    return type & _flags;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   144
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   145
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   146
QString FileEngine::fileName(FileName file) const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   147
{
8085
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   148
    switch(file)
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   149
    {
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   150
        case QAbstractFileEngine::AbsolutePathName:
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   151
        {
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   152
            QString s(PHYSFS_getWriteDir());
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   153
            return s;
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   154
        }
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   155
        case QAbstractFileEngine::BaseName:
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   156
        {
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   157
            int l = _filename.lastIndexOf('/');
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   158
            QString s = _filename.mid(l + 1);
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   159
            return s;
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   160
        }
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   161
        case QAbstractFileEngine::DefaultName:
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   162
        case QAbstractFileEngine::AbsoluteName:
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   163
        default:
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   164
        {
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   165
            QString s = "physfs:/" + _filename;
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   166
            return s;
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   167
        }
6c059add1560 Take care of parameter passed to FileEngine::fileName. Fixes problem with weird paths on windows.
unc0rr
parents: 8052
diff changeset
   168
    }
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   169
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   170
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   171
QDateTime FileEngine::fileTime(FileTime time) const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   172
{
7955
85b3970b402a - Load teams via physfs
unc0rr
parents: 7931
diff changeset
   173
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   174
    switch (time)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   175
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   176
        case QAbstractFileEngine::ModificationTime:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   177
        default:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   178
            return _datetime;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   179
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   180
    };
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   181
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   182
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   183
void FileEngine::setFileName(const QString &file)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   184
{
7770
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   185
    if(file.startsWith(FileEngineHandler::scheme))
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   186
        _filename = file.mid(FileEngineHandler::scheme.size());
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   187
    else
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   188
        _filename = file;
7931
5a27ed7f17b7 Wee, now frontend is able to load translation file
unc0rr
parents: 7770
diff changeset
   189
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   190
    PHYSFS_Stat stat;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   191
    if (PHYSFS_stat(_filename.toUtf8().constData(), &stat) != 0) {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   192
        _size = stat.filesize;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   193
        _datetime = QDateTime::fromTime_t(stat.modtime);
7955
85b3970b402a - Load teams via physfs
unc0rr
parents: 7931
diff changeset
   194
//        _flags |= QAbstractFileEngine::WriteUserPerm;
7931
5a27ed7f17b7 Wee, now frontend is able to load translation file
unc0rr
parents: 7770
diff changeset
   195
        _flags |= QAbstractFileEngine::ReadUserPerm;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   196
        _flags |= QAbstractFileEngine::ExistsFlag;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   197
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   198
        switch (stat.filetype)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   199
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   200
            case PHYSFS_FILETYPE_REGULAR:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   201
                _flags |= QAbstractFileEngine::FileType;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   202
                break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   203
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   204
            case PHYSFS_FILETYPE_DIRECTORY:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   205
                _flags |= QAbstractFileEngine::DirectoryType;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   206
                break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   207
            case PHYSFS_FILETYPE_SYMLINK:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   208
                _flags |= QAbstractFileEngine::LinkType;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   209
                break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   210
            default: ;
7955
85b3970b402a - Load teams via physfs
unc0rr
parents: 7931
diff changeset
   211
        }
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   212
    }
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   213
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   214
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   215
bool FileEngine::atEnd() const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   216
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   217
    return PHYSFS_eof(_handler) != 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   218
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   219
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   220
qint64 FileEngine::read(char *data, qint64 maxlen)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   221
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   222
    return PHYSFS_readBytes(_handler, data, maxlen);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   223
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   224
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   225
qint64 FileEngine::write(const char *data, qint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   226
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   227
    return PHYSFS_writeBytes(_handler, data, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   228
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   229
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   230
bool FileEngine::isOpened() const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   231
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   232
    return _handler != NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   233
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   234
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   235
QFile::FileError FileEngine::error() const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   236
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   237
    return QFile::UnspecifiedError;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   238
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   239
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   240
QString FileEngine::errorString() const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   241
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   242
    return PHYSFS_getLastError();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   243
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   244
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   245
bool FileEngine::supportsExtension(Extension extension) const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   246
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   247
    return extension == QAbstractFileEngine::AtEndExtension;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   248
}
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   249
7770
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   250
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   251
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   252
FileEngineHandler::FileEngineHandler(char *argv0)
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   253
{
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   254
    PHYSFS_init(argv0);
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   255
}
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   256
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   257
FileEngineHandler::~FileEngineHandler()
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   258
{
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   259
    PHYSFS_deinit();
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   260
}
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   261
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   262
QAbstractFileEngine* FileEngineHandler::create(const QString &filename) const
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   263
{
7770
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   264
    if (filename.startsWith(scheme))
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   265
        return new FileEngine(filename.mid(scheme.size()));
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   266
    else
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   267
        return NULL;
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   268
}
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   269
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   270
void FileEngineHandler::mount(const QString &path)
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   271
{
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   272
    PHYSFS_mount(path.toUtf8().constData(), NULL, 1);
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   273
}
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   274
7955
85b3970b402a - Load teams via physfs
unc0rr
parents: 7931
diff changeset
   275
void FileEngineHandler::mount(const QString & path, const QString & mountPoint)
85b3970b402a - Load teams via physfs
unc0rr
parents: 7931
diff changeset
   276
{
85b3970b402a - Load teams via physfs
unc0rr
parents: 7931
diff changeset
   277
    PHYSFS_mount(path.toUtf8().constData(), mountPoint.toUtf8().constData(), 1);
85b3970b402a - Load teams via physfs
unc0rr
parents: 7931
diff changeset
   278
}
85b3970b402a - Load teams via physfs
unc0rr
parents: 7931
diff changeset
   279
7770
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   280
void FileEngineHandler::setWriteDir(const QString &path)
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   281
{
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   282
    PHYSFS_setWriteDir(path.toUtf8().constData());
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   283
}
7770
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   284
8052
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents: 7955
diff changeset
   285
void FileEngineHandler::mountPacks()
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents: 7955
diff changeset
   286
{
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents: 7955
diff changeset
   287
    hedgewarsMountPackages();
845b5ae03841 Mount .hwt files found in Data folder
unc0rr
parents: 7955
diff changeset
   288
}
7770
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   289
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   290
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   291
FileEngineIterator::FileEngineIterator(QDir::Filters filters, const QStringList &nameFilters, const QStringList &entries)
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   292
    : QAbstractFileEngineIterator(filters, nameFilters)
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   293
{
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   294
    m_entries = entries;
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   295
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   296
    /* heck.. docs are unclear on this
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   297
     * QDirIterator puts iterator before first entry
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   298
     * but QAbstractFileEngineIterator example puts iterator on first entry
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   299
     * though QDirIterator approach seems to be the right one
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   300
     */
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   301
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   302
    m_index = -1;
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   303
}
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   304
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   305
bool FileEngineIterator::hasNext() const
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   306
{
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   307
    return m_index < m_entries.size() - 1;
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   308
}
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   309
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   310
QString FileEngineIterator::next()
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   311
{
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   312
   if (!hasNext())
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   313
       return QString();
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   314
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   315
   ++m_index;
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   316
   return currentFilePath();
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   317
}
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   318
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   319
QString FileEngineIterator::currentFileName() const
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   320
{
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   321
    return m_entries.at(m_index);
ff3442338882 Many bugfixes and features to FileEngine
unc0rr
parents: 7768
diff changeset
   322
}