File: modulefactory.cpp

package info (click to toggle)
ukui-control-center 3.22.1.29-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,420 kB
  • sloc: cpp: 77,963; xml: 2,408; sh: 30; makefile: 4
file content (292 lines) | stat: -rw-r--r-- 10,549 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright (C) 2023, KylinSoft Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
**/
#include "modulefactory.h"
#include <QDir>
#include <QSettings>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include <QByteArray>
#include <QJsonParseError>
#include <QJsonObject>
#include "interface.h"

int ModulesFactory::totalModule;
QList<int> ModulesFactory::moduleTypeList;
QList<QPair<QString, QString>> ModulesFactory::pluginToModuleNameList;
QVector<ModuleInfo *> ModulesFactory::moduleInfoVec;
QMap<int, QMap<QString, QObject *>> ModulesFactory::moduleTypeToPluginMap;

const QString MODULEPATH = "/usr/share/ukui-control-center/data/ukui-control-center-config.json";

ModulesFactory::ModulesFactory() {
    clear();
}

ModulesFactory::~ModulesFactory() {
    clear();
}

void ModulesFactory::clear() {
    totalModule = 0;
    moduleInfoVec.clear();
    moduleTypeList.clear();
    moduleTypeToPluginMap.clear();
    pluginToModuleNameList.clear();
}

int ModulesFactory::size() {
    return totalModule;
}

void ModulesFactory::loadConfig() {
    QFile file(MODULEPATH);
    moduleInfoVec.clear();
    if (!file.exists()) {
        qDebug() << "file not exist:" << MODULEPATH;
        return;
    }
    file.open(QIODevice::ReadOnly);
    QByteArray readBy = file.readAll();
    QJsonParseError error;
    QJsonDocument moduleDoc = QJsonDocument::fromJson(readBy, &error);
    if (error.error != QJsonParseError::NoError) {
        qDebug() << "ModuleFactory parseJson failed:" << error.errorString();
        return;
    }
    QJsonObject modulesObj = moduleDoc.object();
    if (!modulesObj.contains("ukcc")) {
        qDebug() << "ModuleFactory has not ukcc item";
        return;
    }
    QJsonArray moduleArr = modulesObj.value("ukcc").toArray();
    totalModule = moduleArr.size();
    for (int order = 0; order < totalModule; order++) {
        QJsonObject moduleObj = moduleArr[order].toObject();
        QString moduleName = moduleObj["name"].toString().toLower();
        QString localIconName = moduleObj["local_icon_name"].toString();
        QString themeIconName = moduleObj["theme_icon_name"].toString();
        int moduleType = moduleObj["module_type"].toInt();
        QJsonObject localeObj = moduleObj.value("name_locale").toObject();
        int moduleOrder = order;
        ModuleInfo* curModuleInfo = new ModuleInfo(moduleName, moduleOrder, moduleType, themeIconName, localIconName);
        if (curModuleInfo == nullptr) {
            continue;
        }
        int totalLocale = localeObj.size();
        for (int localeIdx = 0; localeIdx < totalLocale; ++localeIdx) {
            QString key = localeObj.keys().at(localeIdx);
            QString value = localeObj[key].toString();
            curModuleInfo->appendNameLocale(key, value);
        }
        QJsonArray pluginArr = moduleObj.value("childnode").toArray();
        int totalPlugin = pluginArr.size();
        for (int pluginIdx = 0; pluginIdx < totalPlugin; ++pluginIdx) {
            QJsonObject pluginObj = pluginArr[pluginIdx].toObject();
            if (!pluginObj.keys().contains("name")) {
                continue;
            }
            QString pluginname = pluginObj["name"].toString().toLower();
            curModuleInfo->appendPluginNameList(pluginIdx, pluginname);
        }
        moduleInfoVec.append(curModuleInfo);
    }
    std::sort(moduleInfoVec.begin(), moduleInfoVec.end(),
              [&](const ModuleInfo* lModuleInfo, const ModuleInfo* rModuleInfo) {
        if (lModuleInfo->moduleOrder != rModuleInfo->moduleOrder) {
            return lModuleInfo->moduleOrder < rModuleInfo->moduleOrder;
        }
        return false;
    });
    foreach (ModuleInfo* moduleInfo, moduleInfoVec) {
        QMap<QString, QObject*> pluginsMaps;
        int moduleType = moduleInfo->moduleType;
        moduleTypeToPluginMap.insert(moduleType, pluginsMaps);
        qDebug() << "modulename: " <<moduleInfo->moduleName << moduleInfo->moduleOrder;
    }
    totalModule = moduleInfoVec.size();
    file.close();
}

void ModulesFactory::loadPluginInfo(QObject* pluginObj) {
    if (!pluginObj) {
        return;
    }
    CommonInterface* pluginInstance = qobject_cast<CommonInterface*>(pluginObj);
    if (!pluginInstance) {
        return;
    }
    QString name = pluginInstance->name().toLower();
    QString i18nName = pluginInstance->plugini18nName();
    int moduleType = pluginInstance->pluginTypes();
    bool isShow = pluginInstance->isShowOnHomePage();
    bool isEnable = pluginInstance->isEnable();
    ModuleInfo* moduleInfo = getModuleInfoByType(moduleType);
    if (moduleInfo == nullptr) {
        return;
    }
    QList<QString>& pluginName = moduleInfo->pluginNameList;
    QList<PluginInfo>& pluginInfoList = moduleInfo->pluginInfoList;

    // 查找当前加载的插件序号i
    int pluginIdx = 0;
    for (; pluginIdx < pluginName.size(); pluginIdx++) {
        if (name.compare(pluginName.at(pluginIdx), Qt::CaseInsensitive) == 0) {
            break;
        }
    }
    PluginInfo pluginInfo;
    pluginInfo.oriNameString = pluginInstance->name();
    pluginInfo.nameString = name;
    pluginInfo.namei18nString = i18nName;
    pluginInfo.type = moduleType;
    pluginInfo.mainShow = isShow;
    pluginInfo.isEnable = isEnable;
    QPair<QString, QString> data;
    data.first = pluginInfo.nameString;
    data.second = moduleInfo->moduleName;
    pluginToModuleNameList.append(data);
    if (!moduleTypeList.contains(pluginInfo.type)) {
        moduleTypeList.append(pluginInfo.type);
    }
    int pluginInfoListSize = pluginInfoList.size();
    if (pluginInfoListSize == 0) {
        pluginInfoList.append(pluginInfo);
    } else {
        bool isInsert = false;
        // 遍历原有list,对比pluginInfo的序号与list中每个元素序号的关系
        for (int preItemIndex = pluginInfoListSize - 1; preItemIndex >= 0; preItemIndex--) {
            int nItemIndex = 0;
            for (nItemIndex = 0; nItemIndex < pluginName.count(); nItemIndex++) {
                if (pluginInfoList[preItemIndex].nameString.toLower().compare(pluginName.at(nItemIndex).toLower(), Qt::CaseInsensitive) == 0) {
                    break;
                }
            }
            // 原有list元素序号小于新的module序号,将module插入该元素之后
            if (nItemIndex <= pluginIdx) {
                pluginInfoList.insert(preItemIndex + 1, pluginInfo);
                isInsert = true;
                break;
            }
        }
        if (!isInsert) {
            pluginInfoList.insert(0, pluginInfo);
        }
    }
    moduleTypeToPluginMap[moduleType].insert(i18nName, pluginObj);
}

QString ModulesFactory::getModuleNamebyName(QString pluginName) {
    QString moduleName = "";
    for (auto it : pluginToModuleNameList) {
        if (it.first.toLower() == pluginName.toLower()) {
            moduleName = it.second;
            break;
        }
    }
    return moduleName;
}

PluginInfo* ModulesFactory::getPluginInfoByName(QString pluginName) {
    for (int moduleIdx = 0; moduleIdx < totalModule; moduleIdx++) {
        for (int pluginIdx = 0; pluginIdx < moduleInfoVec.at(moduleIdx)->pluginInfoList.size(); pluginIdx++) {
            if (!moduleInfoVec.at(moduleIdx)->pluginInfoList[pluginIdx].nameString.compare(pluginName, Qt::CaseInsensitive)) {
                return &moduleInfoVec.at(moduleIdx)->pluginInfoList[pluginIdx];
            }
        }
    }
    return nullptr;
}

QObject* ModulesFactory::getPluginObjectByName(QString pluginil8Name) {
    for (int idx = 0; idx < moduleTypeToPluginMap.size(); ++idx) {
        if (moduleTypeToPluginMap[idx].contains(pluginil8Name)) {
            return moduleTypeToPluginMap[idx][pluginil8Name];
        }
    }
    return nullptr;
}

QObject* ModulesFactory::getPluginObjectByType(int moduleType, QString pluginil8Name) {
    if (moduleTypeToPluginMap.contains(moduleType)) {
        return nullptr;
    }
    QMap<QString, QObject*> curPluginMap = moduleTypeToPluginMap[moduleType];
    if (!curPluginMap.empty() && curPluginMap.keys().contains(pluginil8Name)) {
        return curPluginMap.value(pluginil8Name);
    }
    return nullptr;
}

ModuleInfo* ModulesFactory::getModuleInfoByIndex(int index) {
    if (!checkModuleIndex(index)) {
        return nullptr;
    }
    return moduleInfoVec[index];
}

ModuleInfo* ModulesFactory::getModuleInfoByType(int moduleType) {
    for (int moduleIdx = 0; moduleIdx < totalModule; moduleIdx++) {
        int curModuleType = moduleInfoVec[moduleIdx]->moduleType;
        if (moduleType != curModuleType) {
            continue;
        }
        return moduleInfoVec[moduleIdx];
    }
    return nullptr;
}

bool ModulesFactory::checkPluginExist(QString pluginil8Name) {
    for (int idx = 0; idx < moduleTypeToPluginMap.size(); ++idx) {
        if (moduleTypeToPluginMap[idx].contains(pluginil8Name)) {
            return true;
        }
    }
    return false;
}

bool ModulesFactory::checkModuleIndex(int index) {
    int moduleSize = moduleInfoVec.size();
    if (index >= 0 && index < moduleSize) {
        return true;
    }
    return false;
}

bool ModulesFactory::checkModuleType(int moduleType) {
    return moduleTypeList.contains(moduleType);
}

void ModulesFactory::pluginDelete() {
    QMap<QString, QObject*> moduleMap;
    for (auto plugin : moduleTypeList) {
        if (!moduleTypeToPluginMap.contains(plugin)) {
            continue;
        }
        moduleMap = moduleTypeToPluginMap[plugin];
        QMap<QString, QObject*>::const_iterator it;
        for (it = moduleMap.constBegin(); it != moduleMap.constEnd(); ++it)  {
            CommonInterface* pluginInstance = qobject_cast<CommonInterface*>(it.value());
            if (pluginInstance) {
                qInfo() << "~exit && delete-plugin:" << pluginInstance->name();
                delete pluginInstance;
                pluginInstance = nullptr;
            }
        }
    }
}