File indexing completed on 2024-05-12 04:28:29

0001 /*
0002  *  SPDX-License-Identifier: GPL-3.0-or-later
0003  */
0004 
0005 #include <QFile>
0006 #include <QDir>
0007 #include <QString>
0008 #include <QStringList>
0009 #include <QByteArray>
0010 #include <QList>
0011 #include <QDebug>
0012 #include <QCommandLineParser>
0013 #include <QCoreApplication>
0014 
0015 QStringList findIconPaths(const QString &baseName, const QDir startDir)
0016 {
0017     QStringList filters;
0018     filters << baseName + ".svg" << baseName + ".png";
0019 
0020     QFileInfoList icons = startDir.entryInfoList(filters, QDir::Files);
0021     QStringList results;
0022     Q_FOREACH (const QFileInfo &icon, icons) {
0023         results << icon.absoluteFilePath();
0024     }
0025 
0026     QStringList subdirs = startDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
0027     Q_FOREACH (const QString &subdir, subdirs) {
0028         results << findIconPaths(baseName, QDir(startDir.absolutePath() + "/" + subdir));
0029     }
0030     return results;
0031 
0032 }
0033 
0034 int main(int argc, char *argv[])
0035 {
0036 
0037     QCoreApplication app(argc, argv);
0038     app.setApplicationName("CreateIcons");
0039     QCommandLineParser parser;
0040     parser.setApplicationDescription("Copy all specified icons from a given theme and create a qrc file.\n\n"
0041                                      "If the word 'dark' exists in the theme directory name, the icons will be"
0042                                      "classified as dark, otherwise as light."
0043                                      );
0044     parser.addHelpOption();
0045     QCommandLineOption targetDirectoryOption(QStringList() << "p" << "pics",
0046                                              "Copy all icons into <directory>.",
0047                                              "directory");
0048     parser.addOption(targetDirectoryOption);
0049     parser.addPositionalArgument("iconsdir", "location of the icon theme");
0050 
0051     parser.process(app);
0052 
0053     const QStringList themes = parser.positionalArguments();
0054     QString dir = parser.value(targetDirectoryOption);
0055     if (dir.isEmpty()) {
0056         dir = ".";
0057     }
0058 
0059     QFile f(":/icons");
0060     f.open(QFile::ReadOnly);
0061     QByteArray ba = f.readAll();
0062     QList<QByteArray> iconNames = ba.split('\n');
0063     qDebug() << iconNames.size() << "icons will be retrieved";
0064 
0065     QString qrc("<!DOCTYPE RCC>\n"
0066                 "<RCC version=\"1.0\">\n"
0067                 "\t<qresource>\n");
0068 
0069     Q_FOREACH (const QByteArray &iconName, iconNames) {
0070         if (!iconName.isEmpty()) {
0071             Q_FOREACH (const QString &theme, themes) {
0072                 QStringList iconPaths = findIconPaths(QString::fromLatin1(iconName), QDir(theme));
0073                 if (iconPaths.isEmpty()) {
0074                     qDebug() << "Could not find" << iconName << "in theme" << theme;
0075                 }
0076                 else {
0077                     bool dark = theme.contains("dark");
0078                     Q_FOREACH (const QString &iconPath, iconPaths) {
0079                         QFileInfo fi(iconPath);
0080                         // Check whether the path contains a size
0081                         QStringList parts = fi.absolutePath().split('/');
0082                         QString newIconPath = dir + "/";
0083 
0084                         bool ok;
0085                         parts.last().toInt(&ok);
0086                         if (ok) {
0087                             newIconPath = newIconPath + parts.last() + "_";
0088                         }
0089                         if (dark) {
0090                             newIconPath = newIconPath + "dark_";
0091                         }
0092                         else {
0093                             newIconPath = newIconPath + "light_";
0094                         }
0095                         newIconPath = newIconPath + fi.fileName();
0096                         QFile::copy(iconPath, newIconPath);
0097 
0098                         qrc += "\t\t<file>" + QFileInfo(newIconPath).fileName() + "</file>\n";
0099                     }
0100                 }
0101             }
0102         }
0103     }
0104 
0105     qrc += "\t</qresource>\n"
0106            "</RCC>";
0107 
0108     QFile qrcf(dir + "/icons.qrc");
0109     qrcf.open(QFile::WriteOnly);
0110     qrcf.write(qrc.toLatin1());
0111     qrcf.close();
0112 }