File indexing completed on 2024-05-12 16:46:38
0001 /*************************************************************************** 0002 Copyright (C) 2003-2009 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 "tellico_utils.h" 0026 #include "string_utils.h" 0027 #include "../tellico_debug.h" 0028 0029 #include <KIO/FileCopyJob> 0030 0031 #include <QIcon> 0032 #include <QStandardPaths> 0033 #include <QDir> 0034 #include <QPixmap> 0035 #include <QDateTime> 0036 #include <QUrl> 0037 #include <QSet> 0038 0039 QStringList Tellico::findAllSubDirs(const QString& dir_) { 0040 if(dir_.isEmpty()) { 0041 return QStringList(); 0042 } 0043 0044 // TODO: build in symlink checking, for now, prohibit 0045 QDir dir(dir_, QString(), QDir::Name | QDir::IgnoreCase, QDir::Dirs | QDir::Readable | QDir::NoSymLinks); 0046 0047 QStringList allSubdirs; // the whole list 0048 0049 // find immediate sub directories 0050 const QStringList subdirs = dir.entryList(); 0051 for(QStringList::ConstIterator subdir = subdirs.begin(); subdir != subdirs.end(); ++subdir) { 0052 if((*subdir).isEmpty() || *subdir == QLatin1String(".") || *subdir == QLatin1String("..")) { 0053 continue; 0054 } 0055 QString absSubdir = dir.absoluteFilePath(*subdir); 0056 allSubdirs += findAllSubDirs(absSubdir); 0057 allSubdirs += absSubdir; 0058 } 0059 return allSubdirs; 0060 } 0061 0062 QStringList Tellico::locateAllFiles(const QString& fileName_) { 0063 QStringList files; 0064 QSet<QString> uniqueNames; 0065 0066 QString dirPart = fileName_.section(QLatin1Char('/'), 0, -2); 0067 QString filePart = fileName_.section(QLatin1Char('/'), -1); 0068 0069 const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, dirPart, QStandardPaths::LocateDirectory); 0070 foreach(const QString& dir, dirs) { 0071 const QStringList fileNames = QDir(dir).entryList(QStringList() << filePart); 0072 foreach(const QString& file, fileNames) { 0073 if(!uniqueNames.contains(file)) { 0074 files.append(dir + QLatin1Char('/') + file); 0075 uniqueNames += file; 0076 } 0077 } 0078 } 0079 return files; 0080 } 0081 0082 QString Tellico::installationDir() { 0083 // look for a file that gets installed to know the installation directory 0084 static QString appdir; 0085 if(appdir.isEmpty()) { 0086 appdir = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("tellico/pics/tellico.png")); 0087 // remove the file name string. Important to keep trailing slash 0088 appdir.chop(QStringLiteral("pics/tellico.png").length()); 0089 // myDebug() << "InstallationDir:" << appdir; 0090 } 0091 return appdir; 0092 } 0093 0094 QString Tellico::saveLocation(const QString& dir_) { 0095 QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QDir::separator() + dir_; 0096 QDir dir; 0097 bool success = dir.mkpath(path); 0098 if(!success) { 0099 myWarning() << "Failed to make path:" << path; 0100 } 0101 return path; 0102 } 0103 0104 const QPixmap& Tellico::pixmap(const QString& value_) { 0105 static QHash<int, QPixmap*> pixmaps; 0106 if(pixmaps.isEmpty()) { 0107 pixmaps.insert(-1, new QPixmap()); 0108 } 0109 bool ok; 0110 int n = Tellico::toUInt(value_, &ok); 0111 if(!ok || n < 1 || n > 10) { 0112 return *pixmaps[-1]; 0113 } 0114 if(pixmaps[n]) { 0115 return *pixmaps[n]; 0116 } 0117 0118 QString picName = QStringLiteral(":/icons/stars%1").arg(n); 0119 QPixmap* pix = new QPixmap(QIcon(picName).pixmap(QSize(n*16, 16))); 0120 pixmaps.insert(n, pix); 0121 return *pix; 0122 } 0123 0124 bool Tellico::checkCommonXSLFile() { 0125 // look for a file that gets installed to know the installation directory 0126 // need to check timestamps 0127 QString userDataDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); 0128 QString userCommonFile = userDataDir + QDir::separator() + QLatin1String("tellico-common.xsl"); 0129 if(QFile::exists(userCommonFile)) { 0130 // check timestamps 0131 // pics/tellico.png is not likely to be in a user directory 0132 QString installDir = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("pics/tellico.png")); 0133 installDir = QFileInfo(installDir).absolutePath(); 0134 QString installCommonFile = installDir + QDir::separator() + QLatin1String("tellico-common.xsl"); 0135 if(userCommonFile == installCommonFile) { 0136 myWarning() << "install location is same as user location"; 0137 } 0138 QFileInfo installInfo(installCommonFile); 0139 QFileInfo userInfo(userCommonFile); 0140 if(installInfo.lastModified() > userInfo.lastModified()) { 0141 // the installed file has been modified more recently than the user's 0142 // remove user's tellico-common.xsl file so it gets copied again 0143 // myLog() << "removing" << userCommonFile; 0144 // myLog() << "copying" << installCommonFile; 0145 QFile::remove(userCommonFile); 0146 } else { 0147 return true; 0148 } 0149 } 0150 QUrl src = QUrl::fromLocalFile(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("tellico-common.xsl"))); 0151 if(src.isEmpty()) { 0152 myDebug() << "checkCommonXSLFile() - could not locate tellico-common.xsl"; 0153 return false; 0154 } 0155 QUrl dest = QUrl::fromLocalFile(userCommonFile); 0156 return KIO::file_copy(src, dest)->exec(); 0157 }