diff options
Diffstat (limited to 'icons/breeze/qrcAlias.cpp')
-rw-r--r-- | icons/breeze/qrcAlias.cpp | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/icons/breeze/qrcAlias.cpp b/icons/breeze/qrcAlias.cpp new file mode 100644 index 00000000..409ba61f --- /dev/null +++ b/icons/breeze/qrcAlias.cpp | |||
@@ -0,0 +1,119 @@ | |||
1 | /* This file is part of the KDE libraries | ||
2 | * Copyright (C) 2016 Kåre Särs <kare.sars@iki.fi> | ||
3 | * | ||
4 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Library General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2 of the License, or (at your option) any later version. | ||
8 | * | ||
9 | * This library is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | * Library General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU Library General Public License | ||
15 | * along with this library; see the file COPYING.LIB. If not, write to | ||
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
17 | * Boston, MA 02110-1301, USA. | ||
18 | */ | ||
19 | #include <QCoreApplication> | ||
20 | #include <QFile> | ||
21 | #include <QFileInfo> | ||
22 | #include <QString> | ||
23 | #include <QRegularExpression> | ||
24 | #include <QDebug> | ||
25 | #include <QCommandLineParser> | ||
26 | |||
27 | QString link(const QString &path, const QString &fileName) | ||
28 | { | ||
29 | QFile in(path + QLatin1Char('/') + fileName); | ||
30 | if (!in.open(QIODevice::ReadOnly)) { | ||
31 | qDebug() << "failed to read" << path << fileName << in.fileName(); | ||
32 | return QString(); | ||
33 | } | ||
34 | |||
35 | QString firstLine = in.readLine(); | ||
36 | if (firstLine.isEmpty()) { | ||
37 | qDebug() << in.fileName() << "line could not be read..."; | ||
38 | return QString(); | ||
39 | } | ||
40 | QRegularExpression fNameReg(QStringLiteral("(.*\\.(?:svg|png|gif|ico))")); | ||
41 | QRegularExpressionMatch match = fNameReg.match(firstLine); | ||
42 | if (!match.hasMatch()) { | ||
43 | return QString(); | ||
44 | } | ||
45 | |||
46 | QFileInfo linkInfo(path + QLatin1Char('/') + match.captured(1)); | ||
47 | QString aliasLink = link(linkInfo.path(), linkInfo.fileName()); | ||
48 | if (!aliasLink.isEmpty()) { | ||
49 | //qDebug() << fileName << "=" << match.captured(1) << "=" << aliasLink; | ||
50 | return aliasLink; | ||
51 | } | ||
52 | |||
53 | return path + QLatin1Char('/') + match.captured(1); | ||
54 | } | ||
55 | |||
56 | int parseFile(const QString &infile, const QString &outfile) | ||
57 | { | ||
58 | QFile in(infile); | ||
59 | QFile out(outfile); | ||
60 | QRegularExpression imageReg(QStringLiteral("<file>(.*\\.(?:svg|png|gif|ico))</file>")); | ||
61 | |||
62 | if (!in.open(QIODevice::ReadOnly)) { | ||
63 | qDebug() << "Failed to open" << infile; | ||
64 | return -1; | ||
65 | } | ||
66 | if (!out.open(QIODevice::WriteOnly)) { | ||
67 | qDebug() << "Failed to create" << outfile; | ||
68 | return -2; | ||
69 | } | ||
70 | |||
71 | while (in.bytesAvailable()) { | ||
72 | QString line = QString::fromLocal8Bit(in.readLine()); | ||
73 | QRegularExpressionMatch match = imageReg.match(line); | ||
74 | if (!match.hasMatch()) { | ||
75 | //qDebug() << "No Match: " << line; | ||
76 | out.write(qPrintable(line)); | ||
77 | continue; | ||
78 | } | ||
79 | |||
80 | QFileInfo info(match.captured(1)); | ||
81 | |||
82 | QString aliasLink = link(info.path(), info.fileName()); | ||
83 | if (aliasLink.isEmpty()) { | ||
84 | //qDebug() << "No alias: " << line; | ||
85 | out.write(qPrintable(line)); | ||
86 | continue; | ||
87 | } | ||
88 | |||
89 | QString newLine = QStringLiteral("<file alias=\"%1\">%2</file>\n").arg(match.captured(1), aliasLink); | ||
90 | //qDebug() << newLine; | ||
91 | out.write(qPrintable(newLine)); | ||
92 | } | ||
93 | return 0; | ||
94 | } | ||
95 | |||
96 | int main(int argc, char *argv[]) | ||
97 | { | ||
98 | QCoreApplication app(argc, argv); | ||
99 | |||
100 | QCommandLineParser parser; | ||
101 | |||
102 | QCommandLineOption inOption(QStringList() << QLatin1String("i") << QLatin1String("infile"), QStringLiteral("Input qrc file"), QStringLiteral("infile")); | ||
103 | QCommandLineOption outOption(QStringList() << QLatin1String("o") << QLatin1String("outfile"), QStringLiteral("Output qrc file"), QStringLiteral("outfile")); | ||
104 | parser.setApplicationDescription( | ||
105 | QLatin1String("On Windows git handles symbolic links by converting them " | ||
106 | "to text files containing the links to the actual file. This application " | ||
107 | "takes a .qrc file as input and outputs a .qrc file with the symbolic " | ||
108 | "links converted to qrc-aliases.")); | ||
109 | parser.addHelpOption(); | ||
110 | parser.addVersionOption(); | ||
111 | parser.addOption(inOption); | ||
112 | parser.addOption(outOption); | ||
113 | parser.process(app); | ||
114 | |||
115 | const QString inName = parser.value(inOption); | ||
116 | const QString outName = parser.value(outOption); | ||
117 | |||
118 | return parseFile(inName, outName); | ||
119 | } | ||