File indexing completed on 2024-05-12 05:10:15

0001 /***************************************************************************
0002     Copyright (C) 2015 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "datafileregistry.h"
0026 #include "../tellico_debug.h"
0027 
0028 #include <QFileInfo>
0029 #include <QStandardPaths>
0030 
0031 using Tellico::DataFileRegistry;
0032 
0033 Tellico::DataFileRegistry* DataFileRegistry::self() {
0034   static DataFileRegistry registry;
0035   return &registry;
0036 }
0037 
0038 DataFileRegistry::DataFileRegistry() = default;
0039 
0040 void DataFileRegistry::addDataLocation(const QString& dir_, bool prepend_) {
0041   if(dir_.isEmpty()) {
0042     return;
0043   }
0044 
0045   QFileInfo info(dir_);
0046   if(!info.exists()) {
0047     return;
0048   }
0049 
0050   QString dataDir;
0051   if(info.isDir()) {
0052     dataDir = dir_;
0053   } else {
0054     dataDir = info.canonicalPath();
0055   }
0056   dataDir += QLatin1Char('/');
0057 
0058   if(!m_dataLocations.contains(dataDir)) {
0059     if(prepend_) {
0060       m_dataLocations.prepend(dataDir);
0061     } else {
0062       m_dataLocations.append(dataDir);
0063     }
0064   }
0065 }
0066 
0067 QString DataFileRegistry::locate(const QString& fileName_) {
0068   // account for absolute paths
0069   if(QFileInfo(fileName_).isAbsolute()) {
0070     return fileName_;
0071   }
0072   foreach(const QString& dataDir, m_dataLocations) {
0073     if(QFileInfo::exists(dataDir + fileName_)) {
0074       return dataDir + fileName_;
0075     }
0076   }
0077   return QStandardPaths::locate(QStandardPaths::AppDataLocation, fileName_);
0078 }