diff --git a/src/interface/desktop/common/mainwindow.cpp b/src/interface/desktop/common/mainwindow.cpp index 7824ca8..3a1873c 100644 --- a/src/interface/desktop/common/mainwindow.cpp +++ b/src/interface/desktop/common/mainwindow.cpp @@ -39,16 +39,11 @@ int main(int argc, char** argv) QApplication app(argc, argv); - if (not QFileInfo("./data/.paysages_data").isReadable()) - { - QMessageBox::critical(NULL, QObject::tr("Paysages 3D - Data error"), QObject::tr("Application data were not found. Please ensure the software is run from its original directory.")); - return 1; - } - QTranslator qtTranslator; QTranslator myTranslator; - if (myTranslator.load("paysages_" + QLocale::system().name(), "./data/i18n") || myTranslator.load("paysages_" + QLocale::system().name(), "/usr/share/paysages3d/i18n")) + QString i18ndir = QString::fromStdString(DataFile::findDir("i18n")); + if (not i18ndir.isEmpty() and myTranslator.load("paysages_" + QLocale::system().name(), i18ndir)) { app.installTranslator(&myTranslator); diff --git a/src/interface/desktop/tools.h b/src/interface/desktop/tools.h index 147e95c..9d4b59f 100644 --- a/src/interface/desktop/tools.h +++ b/src/interface/desktop/tools.h @@ -3,7 +3,7 @@ #include #include -#include +#include "DataFile.h" #include "Color.h" @@ -15,15 +15,7 @@ static inline QColor colorToQColor(Color color) static inline QString getDataPath(QString path) { - QFile datafile(QDir("/usr/share/paysages3d/").absoluteFilePath(path)); - if (datafile.exists()) - { - return datafile.fileName(); - } - else - { - return QDir("./data").absoluteFilePath(path); - } + return QString::fromStdString(DataFile::findFile(path.toStdString())); } QString getHumanMemory(qint64 memused); diff --git a/src/system/CacheFile.cpp b/src/system/CacheFile.cpp index 270b656..aef984f 100644 --- a/src/system/CacheFile.cpp +++ b/src/system/CacheFile.cpp @@ -1,23 +1,16 @@ #include "CacheFile.h" -CacheFile::CacheFile(const char* module, const char* ext, const char* tag1, int tag2, int tag3, int tag4, int tag5, int tag6) -{ - datapath = (char*)malloc(sizeof(char) * 501); - filepath = (char*)malloc(sizeof(char) * 501); +#include +#include "DataFile.h" - snprintf(datapath, 500, "./data/cache/%s-%s-%d-%d-%d-%d-%d.%s", module, tag1, tag2, tag3, tag4, tag5, tag6, ext); - snprintf(filepath, 500, "./cache/%s-%s-%d-%d-%d-%d-%d.%s", module, tag1, tag2, tag3, tag4, tag5, tag6, ext); -} - -CacheFile::~CacheFile() +CacheFile::CacheFile(const std::string &module, const std::string &ext, const std::string &tag1, int tag2, int tag3, int tag4, int tag5, int tag6) { - free(datapath); - free(filepath); + filepath = QString("cache/%1-%2-%3-%4-%5-%6-%7.%8").arg(QString::fromStdString(module)).arg(QString::fromStdString(tag1)).arg(tag2).arg(tag3).arg(tag4).arg(tag5).arg(tag6).arg(QString::fromStdString(ext)).toStdString(); } bool CacheFile::isReadable() { - FILE* f = fopen(filepath, "rb"); + FILE* f = fopen(filepath.c_str(), "rb"); if (f) { fclose(f); @@ -25,22 +18,30 @@ bool CacheFile::isReadable() } else { - FILE* f = fopen(datapath, "rb"); - if (f) + std::string datapath = DataFile::findFile(filepath); + if (datapath.empty()) { - fclose(f); - return true; + return false; } else { - return false; + FILE* f = fopen(datapath.c_str(), "rb"); + if (f) + { + fclose(f); + return true; + } + else + { + return false; + } } } } bool CacheFile::isWritable() { - FILE* f = fopen("./cache/.test", "wb"); + FILE* f = fopen("cache/.test", "wb"); if (f) { fclose(f); @@ -52,16 +53,15 @@ bool CacheFile::isWritable() } } -const char* CacheFile::getPath() +std::string CacheFile::getPath() { - FILE* f = fopen(datapath, "rb"); - if (f) - { - fclose(f); - return datapath; - } - else + std::string datapath = DataFile::findFile(filepath); + if (datapath.empty()) { return filepath; } + else + { + return datapath; + } } diff --git a/src/system/CacheFile.h b/src/system/CacheFile.h index 64c39d7..4e5e9c1 100644 --- a/src/system/CacheFile.h +++ b/src/system/CacheFile.h @@ -9,16 +9,14 @@ namespace system { class SYSTEMSHARED_EXPORT CacheFile { public: - CacheFile(const char* module, const char* ext, const char* tag1, int tag2, int tag3, int tag4, int tag5, int tag6); - ~CacheFile(); + CacheFile(const std::string &module, const std::string &ext, const std::string &tag1, int tag2, int tag3, int tag4, int tag5, int tag6); bool isReadable(); bool isWritable(); - const char* getPath(); + std::string getPath(); private: - char* datapath; - char* filepath; + std::string filepath; }; } diff --git a/src/system/DataFile.cpp b/src/system/DataFile.cpp new file mode 100644 index 0000000..b748c96 --- /dev/null +++ b/src/system/DataFile.cpp @@ -0,0 +1,72 @@ +#include "DataFile.h" + +#include "Logs.h" +#include + +std::string DataFile::findFile(const std::string &relpath) +{ + QDir dir(QString::fromStdString(dataDir)); + if (dir.exists(QString::fromStdString(relpath))) + { + return dir.absoluteFilePath(QString::fromStdString(relpath)).toStdString(); + } + else + { + return ""; + } +} + +std::string DataFile::findDir(const std::string &relpath) +{ + return findFile(relpath); +} + +bool DataFile::tryDataDir(const QDir &dir) +{ + logDebug("[System] Try data dir %s", dir.absolutePath().toLocal8Bit().data()); + return dir.exists("data/.paysages_data"); +} + +std::string DataFile::locateDataDir() +{ + QDir dir = QDir::current(); + int i = 0; + + // TODO /usr/share/paysages3d/ + + while (i++ < 100 and not dir.isRoot()) + { + if (tryDataDir(dir)) + { + return dir.absolutePath().toStdString(); + } + + QDir dir2(dir.absoluteFilePath("paysages3d")); + if (tryDataDir(dir2)) + { + return dir2.absolutePath().toStdString(); + } + + dir = QDir(QDir(dir.absoluteFilePath("..")).canonicalPath()); + } + + return ""; +} + +std::string DataFile::initDataDir() +{ + std::string parent = locateDataDir(); + if (parent.empty()) + { + logWarning("[System] Data files not found"); + return parent; + } + else + { + std::string result = QDir(QString::fromStdString(parent)).absoluteFilePath("data").toStdString(); + logDebug("[System] Data files found : %s", result.c_str()); + return result; + } +} + +std::string DataFile::dataDir = DataFile::initDataDir(); diff --git a/src/system/DataFile.h b/src/system/DataFile.h new file mode 100644 index 0000000..f66e4e4 --- /dev/null +++ b/src/system/DataFile.h @@ -0,0 +1,41 @@ +#ifndef DATAFILE_H +#define DATAFILE_H + +#include "system_global.h" + +class QDir; + +namespace paysages { +namespace system { + +/** + * Locator of data files. + */ +class SYSTEMSHARED_EXPORT DataFile +{ +public: + /** + * Find a data file. + * + * Return the absolute data path, or an empty string if not found. + */ + static std::string findFile(const std::string &relpath); + + /** + * Find a data directory. + * + * Return the absolute data path, or an empty string if not found. + */ + static std::string findDir(const std::string &relpath); + +private: + static bool tryDataDir(const QDir &dir); + static std::string locateDataDir(); + static std::string initDataDir(); + static std::string dataDir; +}; + +} +} + +#endif // DATAFILE_H diff --git a/src/system/Logs.h b/src/system/Logs.h index 17d084c..f952299 100644 --- a/src/system/Logs.h +++ b/src/system/Logs.h @@ -3,6 +3,7 @@ #include "system_global.h" +#define logDebug qDebug #define logWarning qWarning #define logError qCritical diff --git a/src/system/system.pro b/src/system/system.pro index 0c1de8c..ca6f1aa 100644 --- a/src/system/system.pro +++ b/src/system/system.pro @@ -27,7 +27,8 @@ SOURCES += \ ParallelPool.cpp \ ParallelWorker.cpp \ Semaphore.cpp \ - FileSystem.cpp + FileSystem.cpp \ + DataFile.cpp HEADERS += \ system_global.h \ @@ -44,7 +45,8 @@ HEADERS += \ ParallelPool.h \ ParallelWorker.h \ Semaphore.h \ - FileSystem.h + FileSystem.h \ + DataFile.h unix:!symbian { maemo5 {