1 | /* |
2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) |
3 | * |
4 | * This program is free software: you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation, either version 3 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program 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 |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
16 | */ |
17 | #include "toolsmanager.h" |
18 | |
19 | #include <QFile> |
20 | #include <QJsonArray> |
21 | #include <QJsonDocument> |
22 | #include <QJsonObject> |
23 | #include <QMessageBox> |
24 | #include "settings.h" |
25 | #include "systemconsts.h" |
26 | |
27 | ToolsManager::ToolsManager(QObject *parent) : QObject(parent) |
28 | { |
29 | |
30 | } |
31 | |
32 | void ToolsManager::load() |
33 | { |
34 | //if config file not exists, copy it from data |
35 | QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + DEV_TOOLS_FILE; |
36 | if (!fileExists(filename)) { |
37 | mTools.clear(); |
38 | PToolItem item = std::make_shared<ToolItem>(); |
39 | item->title = tr("Remove Compiled" ); |
40 | #ifdef Q_OS_WIN |
41 | item->program = "del" ; |
42 | #else |
43 | item->program = "rm" ; |
44 | #endif |
45 | item->workingDirectory = "<SOURCEPATH>" ; |
46 | item->parameters = "<EXENAME>" ; |
47 | item->pauseAfterExit = false; |
48 | mTools.append(item); |
49 | return; |
50 | } |
51 | //read config file |
52 | QFile file(filename); |
53 | if (!file.open(QFile::ReadOnly)) { |
54 | QMessageBox::critical(nullptr, |
55 | tr("Read tools config failed" ), |
56 | tr("Can't open tools config file '%1' for read." ) |
57 | .arg(filename)); |
58 | return; |
59 | } |
60 | |
61 | QByteArray json = file.readAll(); |
62 | QJsonParseError error; |
63 | QJsonDocument doc = QJsonDocument::fromJson(json,&error); |
64 | if (error.error != QJsonParseError::NoError) { |
65 | QMessageBox::critical(nullptr, |
66 | tr("Read tools config failed" ), |
67 | tr("Read tools config file '%1' failed:%2" ) |
68 | .arg(filename,error.errorString())); |
69 | return; |
70 | } |
71 | mTools.clear(); |
72 | QJsonArray array = doc.array(); |
73 | foreach (const QJsonValue& value,array) { |
74 | QJsonObject object = value.toObject(); |
75 | PToolItem item = std::make_shared<ToolItem>(); |
76 | item->title = object["title" ].toString(); |
77 | if (item->title.isEmpty()) |
78 | continue; |
79 | item->program = object["program" ].toString(); |
80 | item->workingDirectory = object["workingDirectory" ].toString(); |
81 | item->parameters = object["parameters" ].toString(); |
82 | item->pauseAfterExit = object["pauseAfterExit" ].toBool(); |
83 | mTools.append(item); |
84 | } |
85 | } |
86 | |
87 | void ToolsManager::save() |
88 | { |
89 | QString filename = includeTrailingPathDelimiter(pSettings->dirs().config()) + DEV_TOOLS_FILE; |
90 | QFile file(filename); |
91 | if (!file.open(QFile::WriteOnly | QFile::Truncate)) { |
92 | QMessageBox::critical(nullptr, |
93 | tr("Save tools config failed" ), |
94 | tr("Can't open tools config file '%1' for write." ) |
95 | .arg(filename)); |
96 | return; |
97 | } |
98 | QJsonArray array; |
99 | foreach (const PToolItem& tool,mTools) { |
100 | QJsonObject object; |
101 | object["title" ]=tool->title; |
102 | object["program" ]=tool->program; |
103 | object["workingDirectory" ] = tool->workingDirectory; |
104 | object["parameters" ]=tool->parameters; |
105 | object["pauseAfterExit" ]=tool->pauseAfterExit; |
106 | array.append(object); |
107 | } |
108 | QJsonDocument doc; |
109 | doc.setArray(array); |
110 | if (file.write(doc.toJson())<0) { |
111 | QMessageBox::critical(nullptr, |
112 | tr("Save tools config failed" ), |
113 | tr("Write to tools config file '%1' failed." ) |
114 | .arg(filename)); |
115 | return; |
116 | } |
117 | |
118 | } |
119 | |
120 | const QList<PToolItem> &ToolsManager::tools() const |
121 | { |
122 | return mTools; |
123 | } |
124 | |
125 | PToolItem ToolsManager::findTool(const QString &title) |
126 | { |
127 | for (int i=0;i<mTools.count();i++) { |
128 | PToolItem item = mTools[i]; |
129 | if (title == item->title) { |
130 | return item; |
131 | } |
132 | } |
133 | return PToolItem(); |
134 | } |
135 | |
136 | void ToolsManager::setTools(const QList<PToolItem> &newTools) |
137 | { |
138 | mTools = newTools; |
139 | } |
140 | |