QTfrontend/util/HWDataManager.cpp
changeset 6159 c780b8cf4d75
child 6160 863d3edf5690
equal deleted inserted replaced
6158:cf034cc88e39 6159:c780b8cf4d75
       
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com>
       
     4  * Copyright (c) 2007-2011 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 #include <QStringList>
       
    21 
       
    22 #include "hwconsts.h"
       
    23 
       
    24 #include "HWDataManager.h"
       
    25 
       
    26 
       
    27 HWDataManager::HWDataManager()
       
    28 {
       
    29     userData = new QDir(cfgdir->absolutePath());
       
    30     if (!userData->cd("Data"))
       
    31         userData = NULL;
       
    32 
       
    33     defaultData = new QDir(datadir->absolutePath());
       
    34 }
       
    35 
       
    36 
       
    37 HWDataManager & HWDataManager::instance()
       
    38 {
       
    39     static HWDataManager instance;
       
    40     return instance;
       
    41 }
       
    42 
       
    43 
       
    44 QStringList HWDataManager::entryList(
       
    45                     const QString & subDirectory,
       
    46                     QDir::Filters filters,
       
    47                     const QStringList & nameFilters
       
    48                     ) const
       
    49 {
       
    50     QStringList result;
       
    51 
       
    52     if (userData != NULL)
       
    53     {
       
    54         QDir tmpDir(*userData);
       
    55         if (tmpDir.cd(subDirectory))
       
    56             result.append(tmpDir.entryList(nameFilters, filters));
       
    57     }
       
    58 
       
    59     QDir tmpDir(*defaultData);
       
    60     if (tmpDir.cd(subDirectory))
       
    61         result.append(tmpDir.entryList(nameFilters, filters));
       
    62 
       
    63     result.sort();
       
    64     result.removeDuplicates();
       
    65 
       
    66     return result;
       
    67 }
       
    68 
       
    69 
       
    70 QFile * HWDataManager::findFileForRead(
       
    71                                 const QString & relativeDataFilePath) const
       
    72 {
       
    73     QFile * file =
       
    74             new QFile(userData->absolutePath()+"/"+relativeDataFilePath);
       
    75     if (!file->exists())
       
    76     {
       
    77         delete file;
       
    78         file = new QFile(defaultData->absolutePath()+"/"+relativeDataFilePath);
       
    79     }
       
    80     return file;
       
    81 }
       
    82 
       
    83 
       
    84 QFile * HWDataManager::findFileForWrite(
       
    85                                 const QString & relativeDataFilePath) const
       
    86 {
       
    87     return new QFile(userData->absolutePath()+"/"+relativeDataFilePath);
       
    88 }
       
    89