QTfrontend/util/HWDataManager.cpp
changeset 6930 d187ea93fc4f
parent 6929 c305d31642ac
child 6931 86c951cd0f3f
equal deleted inserted replaced
6929:c305d31642ac 6930:d187ea93fc4f
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com>
       
     4  * Copyright (c) 2007-2012 Andrey Korotaev <unC0Rr@gmail.com>
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License as published by
       
     8  * the Free Software Foundation; version 2 of the License
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    18  */
       
    19 
       
    20 /**
       
    21  * @file
       
    22  * @brief HWDataManager class implementation
       
    23  */
       
    24 
       
    25 #include <QMap>
       
    26 #include <QStringList>
       
    27 
       
    28 #include <QFileInfo>
       
    29 
       
    30 #include "hwconsts.h"
       
    31 
       
    32 #include "HWDataManager.h"
       
    33 
       
    34 
       
    35 HWDataManager::HWDataManager()
       
    36 {
       
    37     userData = new QDir(cfgdir->absolutePath());
       
    38     if (!userData->cd("Data"))
       
    39         userData = NULL;
       
    40 
       
    41     defaultData = new QDir(datadir->absolutePath());
       
    42 }
       
    43 
       
    44 
       
    45 HWDataManager & HWDataManager::instance()
       
    46 {
       
    47     static HWDataManager instance;
       
    48     return instance;
       
    49 }
       
    50 
       
    51 
       
    52 QStringList HWDataManager::entryList(
       
    53     const QString & subDirectory,
       
    54     QDir::Filters filters,
       
    55     const QStringList & nameFilters
       
    56 ) const
       
    57 {
       
    58     QStringList result;
       
    59 
       
    60     if (userData != NULL)
       
    61     {
       
    62         QDir tmpDir(*userData);
       
    63         if (tmpDir.cd(subDirectory))
       
    64             result.append(tmpDir.entryList(nameFilters, filters));
       
    65     }
       
    66 
       
    67     QDir tmpDir(*defaultData);
       
    68     if (tmpDir.cd(subDirectory))
       
    69         result.append(tmpDir.entryList(nameFilters, filters));
       
    70 
       
    71     result.removeDuplicates();
       
    72 
       
    73     // sort case-insensitive
       
    74     QMap<QString, QString> sortedFileNames;
       
    75     foreach ( QString fn, result)
       
    76     {
       
    77         sortedFileNames.insert(fn.toLower(), fn);
       
    78     }
       
    79     result = sortedFileNames.values();
       
    80 
       
    81     return result;
       
    82 }
       
    83 
       
    84 
       
    85 QString HWDataManager::findFileForRead(
       
    86     const QString & relativeDataFilePath) const
       
    87 {
       
    88     QString path;
       
    89 
       
    90     if (userData != NULL)
       
    91         path = userData->absolutePath()+"/"+relativeDataFilePath;
       
    92 
       
    93     if ((!path.isEmpty()) && (!QFile::exists(path)))
       
    94         path = defaultData->absolutePath()+"/"+relativeDataFilePath;
       
    95 
       
    96     return path;
       
    97 }
       
    98 
       
    99 
       
   100 QString HWDataManager::findFileForWrite(
       
   101     const QString & relativeDataFilePath) const
       
   102 {
       
   103     if (userData != NULL)
       
   104     {
       
   105         QString path = userData->absolutePath()+"/"+relativeDataFilePath;
       
   106 
       
   107         // create folders if needed
       
   108         QDir tmp;
       
   109         tmp.mkpath(QFileInfo(path).absolutePath());
       
   110 
       
   111         return path;
       
   112     }
       
   113 
       
   114 
       
   115     return "";
       
   116 }
       
   117