File indexing completed on 2024-05-12 16:46:36

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() {
0039 }
0040 
0041 void DataFileRegistry::addDataLocation(const QString& dir_, bool prepend_) {
0042   if(dir_.isEmpty()) {
0043     return;
0044   }
0045 
0046   QFileInfo info(dir_);
0047   if(!info.exists()) {
0048     return;
0049   }
0050 
0051   QString dataDir;
0052   if(info.isDir()) {
0053     dataDir = dir_;
0054   } else {
0055     dataDir = info.canonicalPath();
0056   }
0057   dataDir += QLatin1Char('/');
0058 
0059   if(!m_dataLocations.contains(dataDir)) {
0060     if(prepend_) {
0061       m_dataLocations.prepend(dataDir);
0062     } else {
0063       m_dataLocations.append(dataDir);
0064     }
0065   }
0066 }
0067 
0068 QString DataFileRegistry::locate(const QString& fileName_) {
0069   // account for absolute paths
0070   if(QFileInfo(fileName_).isAbsolute()) {
0071     return fileName_;
0072   }
0073   foreach(const QString& dataDir, m_dataLocations) {
0074     if(QFileInfo::exists(dataDir + fileName_)) {
0075       return dataDir + fileName_;
0076     }
0077   }
0078   return QStandardPaths::locate(QStandardPaths::AppDataLocation, fileName_);
0079 }