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