1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
---|---|
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "stylejsonfile.h" |
6 | #include "textedittabwidget/textedit.h" |
7 | #include "common/common.h" |
8 | |
9 | #include <QJsonArray> |
10 | #include <QJsonObject> |
11 | #include <QJsonDocument> |
12 | #include <QMutex> |
13 | |
14 | namespace { |
15 | static QMutex mutex; |
16 | } |
17 | |
18 | class StyleJsonFilePrivate |
19 | { |
20 | friend class StyleJsonFile; |
21 | TextEdit *edit; |
22 | QHash<QString, QJsonObject> userObjects{}; |
23 | QString languageID; |
24 | QJsonObject themeObj; |
25 | bool load(const QString &languageID); |
26 | }; |
27 | |
28 | StyleJsonFile::StyleJsonFile(TextEdit *edit) |
29 | : QObject (edit) |
30 | , d(new StyleJsonFilePrivate()) |
31 | { |
32 | d->edit = edit; |
33 | } |
34 | |
35 | TextEdit *StyleJsonFile::edit() |
36 | { |
37 | return d->edit; |
38 | } |
39 | |
40 | StyleJsonFile::~StyleJsonFile() |
41 | { |
42 | if (d) { |
43 | delete d; |
44 | } |
45 | } |
46 | |
47 | bool StyleJsonFile::setLanguage(const QString &languageID) |
48 | { |
49 | bool result = false; |
50 | result = d->load(languageID); |
51 | |
52 | QMutexLocker locker(&::mutex); |
53 | if (result) { |
54 | d->languageID = languageID; |
55 | } |
56 | return result; |
57 | } |
58 | |
59 | QStringList StyleJsonFile::themes() const |
60 | { |
61 | QMutexLocker locker(&::mutex); |
62 | |
63 | if (d->languageID.isEmpty()) |
64 | return {}; |
65 | |
66 | return d->userObjects[d->languageID].keys(); |
67 | } |
68 | |
69 | bool StyleJsonFile::setTheme(const QString &theme) |
70 | { |
71 | QMutexLocker locker(&::mutex); |
72 | |
73 | if (d->languageID.isEmpty()) { |
74 | qCritical() << "Failed, Language is no setting, " |
75 | "call setTheme after need to call setLanguage"; |
76 | return false; |
77 | } |
78 | |
79 | QJsonObject fileObj= d->userObjects.value(d->languageID); |
80 | if (!fileObj.keys().contains(theme)) |
81 | return false; |
82 | |
83 | d->themeObj = fileObj.value(theme).toObject(); |
84 | |
85 | if (d->themeObj.isEmpty()) |
86 | return false; |
87 | |
88 | return true; |
89 | } |
90 | |
91 | QJsonValue StyleJsonFile::value(const QString &Key) const |
92 | { |
93 | QMutexLocker locker(&::mutex); |
94 | return d->themeObj.value(Key); |
95 | } |
96 | |
97 | bool StyleJsonFilePrivate::load(const QString &languageID) |
98 | { |
99 | QMutexLocker locker(&::mutex); |
100 | |
101 | support_file::EditorStyle::initialize(languageID); |
102 | |
103 | if (!userObjects[languageID].isEmpty()) |
104 | return true; |
105 | |
106 | QString languageStyleFilePath = support_file::EditorStyle::userPath(languageID); |
107 | QFile file(languageStyleFilePath); |
108 | |
109 | if (!file.exists()) { |
110 | return false; |
111 | } |
112 | |
113 | QJsonObject obj; |
114 | if (file.open(QFile::ReadOnly)){ |
115 | QJsonParseError error; |
116 | auto jsonDoc = QJsonDocument::fromJson(file.readAll(), &error); |
117 | if (error.error != QJsonParseError::NoError) { |
118 | qCritical() << error.errorString(); |
119 | } |
120 | obj = jsonDoc.object(); |
121 | userObjects[languageID] = obj; |
122 | file.close(); |
123 | } |
124 | return true; |
125 | } |
126 |