1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
---|---|
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "editorstyle.h" |
6 | #include "util/custompaths.h" |
7 | |
8 | #include <QDir> |
9 | #include <QJsonDocument> |
10 | |
11 | namespace support_file { |
12 | namespace documents { |
13 | inline static QHash<QString, QJsonDocument> editorStyleGlobal; |
14 | inline static QHash<QString, QJsonDocument> editorStyleUser; |
15 | bool editorStyleIsLoaded(const QString &languageID); |
16 | } // namespace documents |
17 | |
18 | bool documents::editorStyleIsLoaded(const QString &languageID) |
19 | { |
20 | if (languageID.isEmpty()) |
21 | return false; |
22 | |
23 | return !editorStyleGlobal.value(languageID).isEmpty() |
24 | && !editorStyleUser.value(languageID).isEmpty(); |
25 | } |
26 | |
27 | QString EditorStyle::globalPath(const QString &languageID) |
28 | { |
29 | auto result = CustomPaths::endSeparator(CustomPaths::global(CustomPaths::Configures)); |
30 | return result + QString("editorstyle_%0.support").arg(languageID); |
31 | } |
32 | |
33 | QString EditorStyle::userPath(const QString &languageID) |
34 | { |
35 | auto result = CustomPaths::endSeparator(CustomPaths::user(CustomPaths::Configures)); |
36 | QDir dir(result); |
37 | if (!dir.exists()) { |
38 | QStringList list = result.split(QDir::separator()); |
39 | list.removeFirst(); |
40 | QDir dir(QDir::separator()); |
41 | for (auto &val : list) { |
42 | if (dir.cd(val)) { |
43 | continue; |
44 | } else { |
45 | dir.mkdir(val); |
46 | } |
47 | } |
48 | if (!CustomPaths::installed()) { |
49 | auto tempConfigFile = CustomPaths::endSeparator(result + "temp"); |
50 | QDir dir(tempConfigFile); |
51 | if (!dir.exists()) |
52 | QDir(result).mkdir("temp"); |
53 | result = tempConfigFile; |
54 | } |
55 | } |
56 | return result + QString("editorstyle_%0.support").arg(languageID); |
57 | } |
58 | |
59 | void EditorStyle::initialize(const QString &languageID) |
60 | { |
61 | auto user = userPath(languageID); |
62 | auto global = globalPath(languageID); |
63 | |
64 | if (!CustomPaths::installed()) { |
65 | QFile::remove(user); |
66 | } |
67 | |
68 | if (!QFileInfo(user).exists()) { |
69 | QFile::copy(global, user); |
70 | } |
71 | } |
72 | |
73 | bool EditorStyle::recovery(const QString &languageID) |
74 | { |
75 | bool result = false; |
76 | auto user = userPath(languageID); |
77 | result = QFile::remove(user); |
78 | initialize(languageID); |
79 | return result; |
80 | } |
81 | |
82 | } // namespace support_file |
83 |