1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "common/common.h"
6
7#include <framework/framework.h>
8#include <framework/lifecycle/pluginsetting.h>
9#include <QApplication>
10#include <QStyleFactory>
11
12static const char *const IID = "org.deepin.plugin.unioncode";
13static const char *const CORE_PLUGIN = "plugin-core";
14static const char *const CORE_NAME = "libplugin-core.so";
15static bool loadPlugins()
16{
17 dpfCheckTimeBegin();
18
19 auto &&lifeCycle = dpfInstance.lifeCycle();
20
21 // set plugin iid from qt style
22 lifeCycle.setPluginIID(IID);
23
24 dpf::PluginSetting *setting = new dpf::PluginSetting;
25 lifeCycle.setSettings(setting);
26
27 QString pluginsPath = CustomPaths::global(CustomPaths::Plugins);
28 qInfo() << QString("run application in %0").arg(pluginsPath);
29 lifeCycle.setPluginPaths({pluginsPath});
30
31 qInfo() << "Depend library paths:" << QApplication::libraryPaths();
32 qInfo() << "Load plugin paths: " << dpf::LifeCycle::pluginPaths();
33
34 // read all plugins in setting paths
35 if (!lifeCycle.readPlugins()) {
36 qCritical() << "Failed, not found any plugin!";
37 return false;
38 }
39
40 auto corePlugin = lifeCycle.pluginMetaObj(CORE_PLUGIN);
41 if (corePlugin.isNull() || !corePlugin->fileName().contains(CORE_NAME)) {
42 qCritical() << "Failed, not found core plugin!";
43 return false;
44 }
45
46 if (!lifeCycle.loadPlugin(corePlugin)) {
47 qCritical() << "Failed, Load core plugin";
48 return false;
49 }
50
51 // load plugins without core
52 if (!lifeCycle.loadPlugins()) {
53 qCritical() << "Failed, Load other plugin error!";
54 return false;
55 }
56
57 dpfCheckTimeEnd();
58
59 return true;
60}
61
62void installTranslator(QApplication &a)
63{
64 QTranslator *translator = new QTranslator();
65
66 auto result = CustomPaths::endSeparator(CustomPaths::global(CustomPaths::Translations));
67 QFile file(CustomPaths::user(CustomPaths::Flags::Configures)
68 + QDir::separator() + QString("chooselanguage.support"));
69
70 if (!file.exists()) {
71 if (file.open(QFile::ReadWrite)) {
72 QLocale locale;
73 QString fileName = locale.name() + ".qm";
74 file.write(fileName.toUtf8());
75 file.close();
76 }
77 }
78 if (file.open(QFile::ReadOnly)) {
79 QTextStream txtInput(&file);
80 QString language = txtInput.readLine();
81 file.close();
82 translator->load(result + language);
83 }
84 a.installTranslator(translator);
85}
86
87void voidMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
88{
89 Q_UNUSED(type);
90 Q_UNUSED(context);
91 Q_UNUSED(msg);
92 // not ouput qt log when in command mode.
93}
94
95int main(int argc, char *argv[])
96{
97 QApplication a(argc, argv);
98 QApplication::setStyle(QStyleFactory::create("Fusion"));
99
100 CommandParser::instance().process();
101
102 // TODO(Any): put to command processor
103 if (CommandParser::instance().isBuildModel()) {
104 CommandParser::instance().setModel(CommandParser::CommandLine);
105 qInstallMessageHandler(voidMessageOutput);
106 if (!loadPlugins()) {
107 qCritical() << "Failed, Load plugins!";
108 abort();
109 }
110 commandLine.build();
111 return 0;
112 }
113
114 installTranslator(a);
115 dpfInstance.initialize();
116 if (!loadPlugins()) {
117 qCritical() << "Failed, Load plugins!";
118 abort();
119 }
120
121 QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
122 QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
123
124 return a.exec();
125}
126