1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#ifndef PROJECTSERVICE_H
6#define PROJECTSERVICE_H
7
8#include "projectgenerator.h"
9#include "projectinfo.h"
10#include "projectviewinterface.h"
11
12#include <QTabWidget>
13
14namespace dpfservice {
15
16struct Target {
17 QString name;
18 QString srcPath;
19 QString targetID;
20
21 QString buildCommand;
22 QStringList buildArguments;
23 QString buildTarget;
24
25 QString workingDir;
26 QString output;
27
28 // TODO(mozart):tempory
29 bool enableEnv;
30
31 bool operator==(const Target &other) const
32 {
33 if (name == other.name
34 && srcPath == other.srcPath)
35 return true;
36
37 return false;
38 }
39};
40using Targets = QVector<Target>;
41
42enum TargetType {
43 kUnknown,
44 kBuildTarget,
45 kRebuildTarget,
46 kCleanTarget,
47 kActiveExecTarget
48};
49
50class ProjectService final : public dpf::PluginService,
51 dpf::AutoServiceRegister<ProjectService>,
52 dpf::QtClassFactory<ProjectGenerator>,
53 dpf::QtClassManager<ProjectGenerator>
54{
55 Q_OBJECT
56 Q_DISABLE_COPY(ProjectService)
57 typedef dpf::QtClassManager<ProjectGenerator> GeneratorProManager;
58 typedef dpf::QtClassFactory<ProjectGenerator> GeneratorProFactory;
59public:
60 static QString name()
61 {
62 return "org.deepin.service.ProjectService";
63 }
64
65 explicit ProjectService(QObject *parent = nullptr)
66 : dpf::PluginService (parent)
67 {
68
69 }
70
71 /*!
72 * \brief supportGenerator 獲取支持的工程名稱
73 * \return
74 */
75 template<class T>
76 QStringList supportGeneratorName()
77 {
78 if (std::is_same<ProjectGenerator, T>())
79 return GeneratorProFactory::createKeys();
80 else {
81 qCritical() << "must ProjectGenerator, "
82 << "not support " << typeid (T).name();
83 abort();
84 }
85 }
86
87 /*!
88 * \brief implGenerator 導入工程文件生成器對象 T = Project::Generator類泛化對象
89 * \param name 生成器對象名稱(唯一鍵)
90 * \param errorString 錯誤信息
91 */
92 template<class T>
93 bool implGenerator(const QString &name, QString *errorString = nullptr)
94 {
95 if (std::is_base_of<ProjectGenerator, T>())
96 return GeneratorProFactory::regClass<T>(name, errorString);
97 else {
98 qCritical() << "must base class ProjectGenerator, "
99 << "not support " << typeid (T).name();
100 abort();
101 }
102 }
103
104 /*!
105 * \brief createGenerator
106 * \param name 名稱
107 * \param errorString 錯誤信息
108 * \return 生成器對象實例
109 */
110 template<class T>
111 T *createGenerator(const QString &name, QString *errorString = nullptr)
112 {
113 if (std::is_base_of<ProjectGenerator, T>()) {
114 Generator *generator = GeneratorProManager::value(name);
115 if (!generator) {
116 generator = GeneratorProFactory::create(name, errorString);
117 if (generator)
118 GeneratorProManager::append(name, dynamic_cast<ProjectGenerator*>(generator));
119 }
120 return dynamic_cast<T*>(generator);
121 } else {
122 qCritical() << "must base class or ProjectGenerator, "
123 << "not support "<< typeid (T).name();
124 abort();
125 }
126 }
127
128 /*!
129 * \brief name
130 */
131 template<class T>
132 QString name(T* value) const{
133 return QtClassManager<T>::key(value);
134 }
135
136 template<class T>
137 QList<T*> values() const {
138 return QtClassManager<T>::values();
139 }
140
141 /*!
142 * \brief getAllProjectInfo
143 */
144 DPF_INTERFACE(QList<dpfservice::ProjectInfo>, getAllProjectInfo, void);
145
146 /*!
147 * \brief getProjectInfo
148 * \param kitName workspace
149 */
150 DPF_INTERFACE(dpfservice::ProjectInfo, getProjectInfo, const QString &kitName, const QString &workspace);
151
152 /**
153 * @brief getActiveProjectInfo
154 */
155 DPF_INTERFACE(dpfservice::ProjectInfo, getActiveProjectInfo);
156
157 /*!
158 * \brief projectView 工程视图接口对象
159 */
160 ProjectViewInterface projectView;
161
162 /*!
163 * \brief getActiveTarget
164 * \param TargetType
165 * \return Target
166 */
167 DPF_INTERFACE(Target, getActiveTarget, TargetType);
168
169Q_SIGNALS:
170 /*!
171 * \brief projectConfigureDone
172 */
173 void projectConfigureDone(const QString &buildDirectory);
174};
175
176/* MainWindow codeediter workspace title,
177 * use in window service swtich workspace
178 */
179inline const QString MWCWT_PROJECTS {QTabWidget::tr("Projects")};
180
181} //namespace dpfservice
182
183#endif // PROJECTSERVICE_H
184