Debian Codegraph @

tools/dci/main.cpp

raw
1 // SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include <QCoreApplication>
6 #include <QCommandLineParser>
7 #include <QCommandLineOption>
8 #include <QDir>
9 #include <QFileInfo>
10 #include <QFile>
11 #include <QDebug>
12
13 #include <unistd.h>
14
15 #include "dci/ddcifile.h"
16
17 static QString getSymLinkTarget(const QString &file, const QString &originPath) {
18 char target[512] = {0};
19 const auto ret = readlink(file.toLocal8Bit().constData(), target, 511);
20 if (ret <= 0)
21 return QString();
22 QString tar = QByteArray(target, ret);
23 QString path = originPath;
24 if (originPath.endsWith('/'))
25 path = path.left(path.length() - 1);
26 if (tar.startsWith(path)) {
27 tar = tar.remove(0, path.length());
28 }
29 return tar;
30 }
31
32 static bool copyFilesToDci(DDciFile *dci, const QString &targetDir, const QString &sourceDir, const QString &originPath) {
33 QDir dir(sourceDir);
34 for (const auto &info : dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot)) {
35 const QString &newFile = QDir(targetDir).filePath(info.fileName());
36 if (info.isDir()) {
37 if (!dci->mkdir(newFile))
38 return false;
39 if (!copyFilesToDci(dci, newFile, info.absoluteFilePath(), originPath))
40 return false;
41 } else if (info.isSymLink()) {
42 if (!dci->link(getSymLinkTarget(info.absoluteFilePath(), originPath), newFile))
43 return false;
44 } else if (info.isFile()) {
45 QFile file(info.absoluteFilePath());
46 if (!file.open(QIODevice::ReadOnly))
47 return false;
48 if (!dci->writeFile(newFile, file.readAll()))
49 return false;
50 }
51 }
52
53 return true;
54 }
55
56 QString cleanPath(const QString &path) {
57 return path.size() < 2 || !path.endsWith(QDir::separator()) ? path : path.chopped(1);
58 }
59
60 bool createTo(const QString &sourceDir, const QString &targetDir) {
61 QFileInfo info(cleanPath(sourceDir));
62 if (!info.isDir())
63 return false;
64
65 const QString &iconName = info.fileName();
66 if (iconName.isEmpty()) {
67 printf("The icon name is not correctly resolved in the source path: \"%s\".\n", qPrintable(sourceDir));
68 return false;
69 }
70
71 const auto newFile = QDir(targetDir).filePath(iconName + ".dci");
72 if (QFile::exists(newFile)) {
73 printf("The path \"%s\" already exists.\n", qPrintable(newFile));
74 return false;
75 }
76
77 DDciFile dci;
78 if (!copyFilesToDci(&dci, "/", sourceDir, sourceDir))
79 return false;
80
81 return dci.writeToFile(newFile);
82 }
83
84 static bool copyFilesFromDci(const DDciFile *dci, const QString &targetDir, const QString &sourceDir, QMap<QString, QString> &pathMap) {
85 QDir target(targetDir);
86 for (const QString &file : dci->list(sourceDir)) {
87 const QString &newFileName = QFileInfo(file).fileName();
88 const QString &newFilePath = target.filePath(newFileName);
89 pathMap.insert(file, newFilePath);
90 const auto &type = dci->type(file);
91 if (type == DDciFile::Directory) {
92 if (!target.mkdir(newFileName))
93 return false;
94 if (!copyFilesFromDci(dci, newFilePath, file, pathMap))
95 return false;
96 } else if (type == DDciFile::File) {
97 QFile newFile(newFilePath);
98 if (!newFile.open(QIODevice::WriteOnly))
99 return false;
100 const auto &data = dci->dataRef(file);
101 if (newFile.write(data) != data.size())
102 return false;
103 } else if (type == DDciFile::Symlink) {
104 // link the real source later
105 } else {
106 return false;
107 }
108 }
109
110 return true;
111 }
112
113 bool exportTo(const QString &dciFile, const QString &targetDir) {
114 QFileInfo info(dciFile);
115 if (!info.isFile() || info.suffix() != "dci")
116 return false;
117 const QString &newDir = QDir(targetDir).filePath(info.baseName());
118 if (QDir(newDir).exists())
119 return false;
120 if (!QDir::current().mkdir(newDir))
121 return false;
122
123 DDciFile dci(dciFile);
124 if (!dci.isValid())
125 return false;
126
127 QMap<QString, QString> pathMap;
128 if (!copyFilesFromDci(&dci, QDir(newDir).absolutePath(), "/", pathMap))
129 return false;
130
131 // link to real source
132 for (auto it = pathMap.begin(); it != pathMap.end(); it++) {
133 if (dci.type(it.key()) == DDciFile::Symlink) {
134 const QString &realSource = it.value();
135 const QDir &dirInDci(QFileInfo(it.key()).path());
136 const QDir &dirRealSrc(QFileInfo(realSource).path());
137 const QString &target = dci.symlinkTarget(it.key(), true);
138 const QString &absoluteTarget = QDir::cleanPath(dirInDci.absoluteFilePath(target));
139 const QString &realTarget = pathMap.value(absoluteTarget);
140
141 // link to relative path(e.g. xx.webp -> ../../normal.light/x/xx.webp)
142 if (!QFile::link(dirRealSrc.relativeFilePath(realTarget), realSource)) {
143 qErrnoWarning(strerror(errno));
144 return false;
145 }
146 }
147 }
148 return true;
149 }
150
151 #define SPACE_CHAR " "
152 #define BEGINE_CHAR "├── "
153 #define MIDDLE_CHAR "│ "
154 #define END_CHAR "└── "
155 #define ARROW_CHAR " -> "
156
157 static inline QString pathName(const QString &path)
158 {
159 QDir dir(path);
160 return dir.dirName().isEmpty() ? path : dir.dirName();
161 }
162
163 void print(DDciFile &dci, const QString &dir = QString("/"), QString prefix = QString())
164 {
165 const auto &fileList = dci.list(dir);
166 QString dirName = pathName(dir);
167
168 printf("%s\n", qPrintable(prefix + dirName));
169
170 prefix.replace(END_CHAR, SPACE_CHAR);
171 prefix.replace(BEGINE_CHAR, MIDDLE_CHAR);
172
173 for (const auto &file : fileList) {
174 QString newPrefix = prefix;
175 newPrefix.append(file == fileList.last() ? END_CHAR : BEGINE_CHAR);
176 if (dci.type(file) == DDciFile::Directory) {
177 print(dci, file, newPrefix);
178 } else {
179 QString fileName = pathName(file);
180 QString symlinkTarget;
181 if (dci.type(file) == DDciFile::Symlink) {
182 symlinkTarget.append(ARROW_CHAR).append(dci.symlinkTarget(file));
183 }
184 printf("%s\n", qPrintable(newPrefix + fileName + symlinkTarget));
185 }
186 }
187 }
188
189 bool tree(const QString &dciFile)
190 {
191 QFileInfo info(dciFile);
192 DDciFile dci(dciFile);
193 if (!dci.isValid())
194 return false;
195
196 print(dci, "/");
197
198 return true;
199 }
200
201 int main(int argc, char *argv[])
202 {
203 QCoreApplication a(argc, argv);
204
205 a.setOrganizationName("zccrs");
206 a.setApplicationName("dci");
207 a.setApplicationVersion("0.0.1");
208
209 QCommandLineParser commandParser;
210 commandParser.setApplicationDescription("DCI tool is a command tool that automatically packs and unpacks DCI directories.\n"
211 "If you have created an icon directory according to the correct DCI directory specification,\n"
212 "you can easily make the DCI icons with this tool.\n"
213 "The commands of DCI tools can be expressed as follows:\n"
214 "\t dci --create [target file path] [source directory path]\n"
215 "\t dci --export [target directory path] [source file path]\n"
216 "\t dci --tree [target file path]\n"
217 "For example, the tool is used in the following ways: \n"
218 "\t dci --create ~/Desktop ~/Desktop/action_add\n"
219 "\t dci --export ~/Desktop ~/Desktop/action_add.dci\n"
220 "\t dci --tree ~/Desktop/action_add.dci\n");
221
222 auto options = QList<QCommandLineOption>
223 {
224 QCommandLineOption("create", "Create the new dci files by the directorys", "targetDirectiry"),
225 QCommandLineOption("export", "Export the dci files to the directorys", "targetDirectory"),
226 QCommandLineOption("tree", "tree view the dci file", "targetDciFile"),
227 };
228 commandParser.addOptions(options);
229 commandParser.addPositionalArgument("sources", "The directorys of create or the dci files of export",
230 "[dir1 dir2...]/[file1 file2...]");
231 commandParser.addHelpOption();
232 commandParser.addVersionOption();
233 commandParser.process(a);
234
235 if (commandParser.isSet(options.at(0))) {
236 for (const QString &dir : commandParser.positionalArguments()) {
237 if (!createTo(dir, commandParser.value(options.at(0)))) {
238 printf("Failed on create dci file for \"%s\"\n", qPrintable(dir));
239 }
240 }
241 } else if (commandParser.isSet(options.at(1))) {
242 for (const QString &dci : commandParser.positionalArguments()) {
243 if (!exportTo(dci, commandParser.value(options.at(1)))) {
244 printf("Failed on export the \"%s\" dci file\n", qPrintable(dci));
245 }
246 }
247 } else if (commandParser.isSet(options.at(2))) {
248 const QString &dci = commandParser.value(options.at(2));
249 if (!tree(dci)) {
250 printf("Failed on view the \"%s\" dci file\n", qPrintable(dci));
251 }
252 } else {
253 commandParser.showHelp(-1);
254 }
255
256 return 0;
257 }