# HG changeset patch # User sheepluva # Date 1319109024 -7200 # Node ID c780b8cf4d75c2d06976075a65fd7a096535b907 # Parent cf034cc88e39233716a9f730904216b82801feee introduce HWDataManager util for transparent access to [user-]data files diff -r cf034cc88e39 -r c780b8cf4d75 QTfrontend/ui/page/pagetraining.cpp --- a/QTfrontend/ui/page/pagetraining.cpp Thu Oct 20 05:52:11 2011 +0200 +++ b/QTfrontend/ui/page/pagetraining.cpp Thu Oct 20 13:10:24 2011 +0200 @@ -27,8 +27,11 @@ #include #include +#include "hwconsts.h" + +#include "HWDataManager.h" + #include "pagetraining.h" -#include "hwconsts.h" QLayout * PageTraining::bodyLayoutDefinition() { @@ -120,41 +123,40 @@ if (loc.isEmpty()) loc = QLocale::system().name(); - QString infoFile = - datadir->absolutePath() + "/Locale/missions_" + loc + ".txt"; + QFile * infoFile = HWDataManager::instance().findFileForRead(QString( + "Locale/missions_" + loc + ".txt")); // if file is non-existant try with language only - if (!QFile::exists(infoFile)) - infoFile = datadir->absolutePath() + "/Locale/missions_" + - loc.replace(QRegExp("_.*$"),"") + ".txt"; + if (!infoFile->exists()) + { + delete infoFile; + infoFile = HWDataManager::instance().findFileForRead(QString( + "Locale/missions_" + loc.replace(QRegExp("_.*$"),"") + ".txt")); + } // fallback if file for current locale is non-existant - if (!QFile::exists(infoFile)) - infoFile = datadir->absolutePath() + "/Locale/missions_en.txt"; + if (!infoFile->exists()) + { + delete infoFile; + infoFile = HWDataManager::instance().findFileForRead(QString( + "Locale/missions_en.txt")); + } + // preload mission info for current locale - m_info = new QSettings(infoFile, QSettings::IniFormat, this); + m_info = + new QSettings(infoFile->fileName(), QSettings::IniFormat, this); -// TODO -> this should be done in a tool "DataDir" class - QDir tmpdir; - tmpdir.cd(cfgdir->absolutePath()); - tmpdir.cd("Data/Missions/Training"); - QStringList missionList = scriptList(tmpdir); - missionList.sort(); + // we don't need infoFile anymore + delete infoFile; - tmpdir.cd(datadir->absolutePath()); - tmpdir.cd("Missions/Training"); - QStringList defaultList = scriptList(tmpdir); - defaultList.sort(); + QStringList missionList = + HWDataManager::instance().entryList(QString("Missions/Training"), + QDir::Files, + QStringList("*.lua") + ).replaceInStrings(QRegExp("\\.lua$"), ""); - // add non-duplicate default scripts to the list - foreach (const QString & mission, defaultList) - { - if (!missionList.contains(mission)) - missionList.append(mission); - } - - // add default scripts that have names different from detected user scripts + // scripts to lost - TODO: model? foreach (const QString & mission, missionList) { QListWidgetItem * item = new QListWidgetItem(mission); @@ -180,13 +182,6 @@ lstMissions->setCurrentRow(0); } -QStringList PageTraining::scriptList(const QDir & scriptDir) const -{ - QDir dir = scriptDir; - dir.setFilter(QDir::Files); - return dir.entryList(QStringList("*.lua")).replaceInStrings(QRegExp("^(.*)\\.lua"), "\\1"); -} - void PageTraining::startSelected() { diff -r cf034cc88e39 -r c780b8cf4d75 QTfrontend/util/HWDataManager.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/QTfrontend/util/HWDataManager.cpp Thu Oct 20 13:10:24 2011 +0200 @@ -0,0 +1,89 @@ +/* + * Hedgewars, a free turn based strategy game + * Copyright (c) 2006-2007 Igor Ulyanov + * Copyright (c) 2007-2011 Andrey Korotaev + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#include + +#include "hwconsts.h" + +#include "HWDataManager.h" + + +HWDataManager::HWDataManager() +{ + userData = new QDir(cfgdir->absolutePath()); + if (!userData->cd("Data")) + userData = NULL; + + defaultData = new QDir(datadir->absolutePath()); +} + + +HWDataManager & HWDataManager::instance() +{ + static HWDataManager instance; + return instance; +} + + +QStringList HWDataManager::entryList( + const QString & subDirectory, + QDir::Filters filters, + const QStringList & nameFilters + ) const +{ + QStringList result; + + if (userData != NULL) + { + QDir tmpDir(*userData); + if (tmpDir.cd(subDirectory)) + result.append(tmpDir.entryList(nameFilters, filters)); + } + + QDir tmpDir(*defaultData); + if (tmpDir.cd(subDirectory)) + result.append(tmpDir.entryList(nameFilters, filters)); + + result.sort(); + result.removeDuplicates(); + + return result; +} + + +QFile * HWDataManager::findFileForRead( + const QString & relativeDataFilePath) const +{ + QFile * file = + new QFile(userData->absolutePath()+"/"+relativeDataFilePath); + if (!file->exists()) + { + delete file; + file = new QFile(defaultData->absolutePath()+"/"+relativeDataFilePath); + } + return file; +} + + +QFile * HWDataManager::findFileForWrite( + const QString & relativeDataFilePath) const +{ + return new QFile(userData->absolutePath()+"/"+relativeDataFilePath); +} + diff -r cf034cc88e39 -r c780b8cf4d75 QTfrontend/util/HWDataManager.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/QTfrontend/util/HWDataManager.h Thu Oct 20 13:10:24 2011 +0200 @@ -0,0 +1,87 @@ +/* + * Hedgewars, a free turn based strategy game + * Copyright (c) 2006-2007 Igor Ulyanov + * Copyright (c) 2007-2011 Andrey Korotaev + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#ifndef HEDGEWARS_HWDATAMANAGER_H +#define HEDGEWARS_HWDATAMANAGER_H + +#include +#include + +#include + +class QDir; +class QFile; +class QStringList; + +/** + * Offers access to the data files of hedgewars. + * Note: singleton pattern + * @author sheepluva + * @since 0.9.17 + */ +class HWDataManager +{ +public: + /** + * Returns a pointer to the singleton instance of this class. + * @return pointer to the instance. + */ + static HWDataManager & instance(); + + /** + * Returns a pointer to the singleton instance of this class. + * @param subDirectory sub-directory to search. + * @param filters filters for entry type. + * @param namefilters filters by name patterns. + * @return a list of matches in the subDirectory of data directory. + */ + QStringList entryList(const QString & subDirectory, + QDir::Filters filters = QDir::NoFilter, + const QStringList & nameFilters = QStringList() + ) const; + + /** + * Creates a QFile for the desired data path and returns a pointer to it. + * Use this method if you want to read an existing data file; + * @param relativeDataFilePath path to the data file. + * @return respective QFile pointer, the actual file may actually not exist. + */ + QFile * findFileForRead(const QString & relativeDataFilePath) const; + + + /** + * Creates a QFile for the desired data path and returns a pointer to it. + * Use this method if you want to create or write into a data file. + * @param relativeDataFilePath path to the data file. + * @return respective QFile pointer. + */ + QFile * findFileForWrite(const QString & relativeDataFilePath) const; + + +private: + /** + * Singleton class constructor. + */ + HWDataManager(); + + QDir * defaultData; + QDir * userData; +}; + +#endif // HEDGEWARS_HWDATAMANAGER_H diff -r cf034cc88e39 -r c780b8cf4d75 project_files/hedgewars.pro --- a/project_files/hedgewars.pro Thu Oct 20 05:52:11 2011 +0200 +++ b/project_files/hedgewars.pro Thu Oct 20 13:10:24 2011 +0200 @@ -72,6 +72,7 @@ ../QTfrontend/ui/widget/mapContainer.h \ ../QTfrontend/ui/widget/HistoryLineEdit.h \ ../QTfrontend/ui/widget/SmartLineEdit.h \ + ../QTfrontend/ui/util/HWDataManager.h \ ../QTfrontend/net/netregister.h \ ../QTfrontend/net/netserver.h \ ../QTfrontend/net/netudpwidget.h \ @@ -146,6 +147,7 @@ ../QTfrontend/ui/widget/mapContainer.cpp \ ../QTfrontend/ui/widget/HistoryLineEdit.cpp \ ../QTfrontend/ui/widget/SmartLineEdit.cpp \ + ../QTfrontend/ui/util/HWDataManager.cpp \ ../QTfrontend/net/tcpBase.cpp \ ../QTfrontend/net/netregister.cpp \ ../QTfrontend/net/proto.cpp \