QTfrontend/util/DataManager.cpp
author sheepluva
Fri, 27 Apr 2012 11:47:37 +0200
changeset 6938 217ed62e872c
parent 6937 7f77fa908a4e
child 6948 7271ce89950f
permissions -rw-r--r--
MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
     1
/*
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
     2
 * Hedgewars, a free turn based strategy game
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
     3
 * Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com>
6700
e04da46ee43c the most important commit of the year
koda
parents: 6616
diff changeset
     4
 * Copyright (c) 2007-2012 Andrey Korotaev <unC0Rr@gmail.com>
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
     5
 *
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
     8
 * the Free Software Foundation; version 2 of the License
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
     9
 *
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    13
 * GNU General Public License for more details.
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    14
 *
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    18
 */
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    19
6168
6f301dac12ff fix an include; fix/add comments
sheepluva
parents: 6167
diff changeset
    20
/**
6170
2b1748161278 fix multiplayer page layout, doc/comment fixes
sheepluva
parents: 6168
diff changeset
    21
 * @file
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6700
diff changeset
    22
 * @brief DataManager class implementation
6168
6f301dac12ff fix an include; fix/add comments
sheepluva
parents: 6167
diff changeset
    23
 */
6f301dac12ff fix an include; fix/add comments
sheepluva
parents: 6167
diff changeset
    24
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    25
#include <QMap>
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    26
#include <QStringList>
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    27
6199
582e85254110 on file save: create parent directories if needed
sheepluva
parents: 6196
diff changeset
    28
#include <QFileInfo>
582e85254110 on file save: create parent directories if needed
sheepluva
parents: 6196
diff changeset
    29
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    30
#include "hwconsts.h"
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    31
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6700
diff changeset
    32
#include "DataManager.h"
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    33
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    34
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6700
diff changeset
    35
DataManager::DataManager()
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    36
{
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
    37
    m_userData = new QDir(cfgdir->absolutePath());
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
    38
    if (!m_userData->cd("Data"))
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
    39
        m_userData = NULL;
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    40
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
    41
    m_defaultData = new QDir(datadir->absolutePath());
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
    42
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents: 6937
diff changeset
    43
    m_mapModel = NULL;
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
    44
    m_themeModel = NULL;
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    45
}
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    46
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    47
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6700
diff changeset
    48
DataManager & DataManager::instance()
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    49
{
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6700
diff changeset
    50
    static DataManager instance;
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    51
    return instance;
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    52
}
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    53
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    54
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6700
diff changeset
    55
QStringList DataManager::entryList(
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6199
diff changeset
    56
    const QString & subDirectory,
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6199
diff changeset
    57
    QDir::Filters filters,
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6199
diff changeset
    58
    const QStringList & nameFilters
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6199
diff changeset
    59
) const
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    60
{
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    61
    QStringList result;
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    62
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
    63
    if (m_userData != NULL)
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    64
    {
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
    65
        QDir tmpDir(*m_userData);
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    66
        if (tmpDir.cd(subDirectory))
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    67
            result.append(tmpDir.entryList(nameFilters, filters));
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    68
    }
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    69
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
    70
    QDir tmpDir(*m_defaultData);
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    71
    if (tmpDir.cd(subDirectory))
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    72
        result.append(tmpDir.entryList(nameFilters, filters));
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    73
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    74
    result.removeDuplicates();
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    75
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    76
    // sort case-insensitive
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    77
    QMap<QString, QString> sortedFileNames;
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    78
    foreach ( QString fn, result)
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    79
    {
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    80
        sortedFileNames.insert(fn.toLower(), fn);
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    81
    }
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    82
    result = sortedFileNames.values();
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    83
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    84
    return result;
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    85
}
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    86
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    87
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6700
diff changeset
    88
QString DataManager::findFileForRead(
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6199
diff changeset
    89
    const QString & relativeDataFilePath) const
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    90
{
6196
c16e84558f71 minor cleanups, mostly in own code
sheepluva
parents: 6170
diff changeset
    91
    QString path;
6167
728cabee2c9f simplified some stuff I wrote
sheepluva
parents: 6160
diff changeset
    92
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
    93
    if (m_userData != NULL)
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
    94
        path = m_userData->absolutePath()+"/"+relativeDataFilePath;
6196
c16e84558f71 minor cleanups, mostly in own code
sheepluva
parents: 6170
diff changeset
    95
c16e84558f71 minor cleanups, mostly in own code
sheepluva
parents: 6170
diff changeset
    96
    if ((!path.isEmpty()) && (!QFile::exists(path)))
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
    97
        path = m_defaultData->absolutePath()+"/"+relativeDataFilePath;
6167
728cabee2c9f simplified some stuff I wrote
sheepluva
parents: 6160
diff changeset
    98
728cabee2c9f simplified some stuff I wrote
sheepluva
parents: 6160
diff changeset
    99
    return path;
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
   100
}
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
   101
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
   102
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6700
diff changeset
   103
QString DataManager::findFileForWrite(
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6199
diff changeset
   104
    const QString & relativeDataFilePath) const
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
   105
{
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   106
    if (m_userData != NULL)
6199
582e85254110 on file save: create parent directories if needed
sheepluva
parents: 6196
diff changeset
   107
    {
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   108
        QString path = m_userData->absolutePath()+"/"+relativeDataFilePath;
6199
582e85254110 on file save: create parent directories if needed
sheepluva
parents: 6196
diff changeset
   109
582e85254110 on file save: create parent directories if needed
sheepluva
parents: 6196
diff changeset
   110
        // create folders if needed
582e85254110 on file save: create parent directories if needed
sheepluva
parents: 6196
diff changeset
   111
        QDir tmp;
582e85254110 on file save: create parent directories if needed
sheepluva
parents: 6196
diff changeset
   112
        tmp.mkpath(QFileInfo(path).absolutePath());
582e85254110 on file save: create parent directories if needed
sheepluva
parents: 6196
diff changeset
   113
582e85254110 on file save: create parent directories if needed
sheepluva
parents: 6196
diff changeset
   114
        return path;
582e85254110 on file save: create parent directories if needed
sheepluva
parents: 6196
diff changeset
   115
    }
6196
c16e84558f71 minor cleanups, mostly in own code
sheepluva
parents: 6170
diff changeset
   116
c16e84558f71 minor cleanups, mostly in own code
sheepluva
parents: 6170
diff changeset
   117
c16e84558f71 minor cleanups, mostly in own code
sheepluva
parents: 6170
diff changeset
   118
    return "";
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
   119
}
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
   120
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents: 6937
diff changeset
   121
MapModel * DataManager::mapModel()
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents: 6937
diff changeset
   122
{
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents: 6937
diff changeset
   123
    if (m_mapModel == NULL) {
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents: 6937
diff changeset
   124
        m_mapModel = new MapModel();
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents: 6937
diff changeset
   125
        m_mapModel->loadMaps();
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents: 6937
diff changeset
   126
    }
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents: 6937
diff changeset
   127
    return m_mapModel;
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents: 6937
diff changeset
   128
}
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents: 6937
diff changeset
   129
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   130
ThemeModel * DataManager::themeModel()
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   131
{
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   132
    if (m_themeModel == NULL) {
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   133
        m_themeModel = new ThemeModel();
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   134
        m_themeModel->loadThemes();
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   135
    }
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   136
    return m_themeModel;
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   137
}
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   138
6931
86c951cd0f3f make DataManager a QObject with reload() slot that emits signal updated()
sheepluva
parents: 6930
diff changeset
   139
void DataManager::reload()
86c951cd0f3f make DataManager a QObject with reload() slot that emits signal updated()
sheepluva
parents: 6930
diff changeset
   140
{
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents: 6937
diff changeset
   141
    m_mapModel->loadMaps();
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   142
    m_themeModel->loadThemes();
6931
86c951cd0f3f make DataManager a QObject with reload() slot that emits signal updated()
sheepluva
parents: 6930
diff changeset
   143
    emit updated();
86c951cd0f3f make DataManager a QObject with reload() slot that emits signal updated()
sheepluva
parents: 6930
diff changeset
   144
}