| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "ninjaprojectgenerator.h" |
| 6 | #include "ninjaasynparse.h" |
| 7 | |
| 8 | #include "common/dialog/propertiesdialog.h" |
| 9 | #include "services/window/windowservice.h" |
| 10 | #include "services/builder/builderservice.h" |
| 11 | #include "services/option/optionmanager.h" |
| 12 | |
| 13 | #include <QtConcurrent> |
| 14 | #include <QtXml> |
| 15 | #include <QFileIconProvider> |
| 16 | |
| 17 | class NinjaProjectGeneratorPrivate |
| 18 | { |
| 19 | friend class NinjaProjectGenerator; |
| 20 | QStandardItem* configureRootItem {nullptr}; |
| 21 | QMenu * {nullptr}; |
| 22 | QProcess * {nullptr}; |
| 23 | QHash<QStandardItem*, NinjaAsynParse*> projectParses {}; |
| 24 | }; |
| 25 | |
| 26 | NinjaProjectGenerator::NinjaProjectGenerator() |
| 27 | : d(new NinjaProjectGeneratorPrivate()) |
| 28 | { |
| 29 | qRegisterMetaType<QList<QStandardItem*>>("QList<QStandardItem*>" ); |
| 30 | using namespace dpfservice; |
| 31 | auto &ctx = dpfInstance.serviceContext(); |
| 32 | ProjectService *projectService = ctx.service<ProjectService>(ProjectService::name()); |
| 33 | if (!projectService) { |
| 34 | qCritical() << "Failed, not found service : projectService" ; |
| 35 | abort(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | NinjaProjectGenerator::~NinjaProjectGenerator() |
| 40 | { |
| 41 | qInfo() << __FUNCTION__; |
| 42 | |
| 43 | if (d) |
| 44 | delete d; |
| 45 | } |
| 46 | |
| 47 | QStringList NinjaProjectGenerator::supportLanguages() |
| 48 | { |
| 49 | return {dpfservice::MWMFA_CXX}; |
| 50 | } |
| 51 | |
| 52 | QStringList NinjaProjectGenerator::supportFileNames() |
| 53 | { |
| 54 | return {"build.ninja" }; |
| 55 | } |
| 56 | |
| 57 | QDialog *NinjaProjectGenerator::configureWidget(const QString &language, |
| 58 | const QString &projectPath) |
| 59 | { |
| 60 | using namespace dpfservice; |
| 61 | |
| 62 | ProjectInfo info; |
| 63 | info.setLanguage(language); |
| 64 | info.setKitName(NinjaProjectGenerator::toolKitName()); |
| 65 | info.setWorkspaceFolder(projectPath); |
| 66 | |
| 67 | configure(info); |
| 68 | |
| 69 | return nullptr; |
| 70 | } |
| 71 | |
| 72 | bool NinjaProjectGenerator::configure(const dpfservice::ProjectInfo &info) |
| 73 | { |
| 74 | dpfservice::ProjectGenerator::configure(info); |
| 75 | |
| 76 | auto root = createRootItem(info); |
| 77 | using namespace dpfservice; |
| 78 | auto &ctx = dpfInstance.serviceContext(); |
| 79 | ProjectService *projectService = ctx.service<ProjectService>(ProjectService::name()); |
| 80 | if (projectService && root) { |
| 81 | projectService->projectView.addRootItem(root); |
| 82 | projectService->projectView.expandedDepth(root, 1); |
| 83 | } |
| 84 | |
| 85 | dpfservice::ProjectGenerator::configure(info); |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | QStandardItem *NinjaProjectGenerator::createRootItem(const dpfservice::ProjectInfo &info) |
| 91 | { |
| 92 | using namespace dpfservice; |
| 93 | QStandardItem * rootItem = ProjectGenerator::createRootItem(info); |
| 94 | dpfservice::ProjectInfo::set(rootItem, info); |
| 95 | d->projectParses[rootItem] = new NinjaAsynParse(); |
| 96 | QObject::connect(d->projectParses[rootItem], &NinjaAsynParse::itemsModified, |
| 97 | this, &NinjaProjectGenerator::doProjectChildsModified); |
| 98 | QMetaObject::invokeMethod(d->projectParses[rootItem], "parseProject" , |
| 99 | Q_ARG(const dpfservice::ProjectInfo &, info)); |
| 100 | return rootItem; |
| 101 | } |
| 102 | |
| 103 | void NinjaProjectGenerator::removeRootItem(QStandardItem *root) |
| 104 | { |
| 105 | if (!root) |
| 106 | return; |
| 107 | auto parser = d->projectParses[root]; |
| 108 | |
| 109 | while (root->hasChildren()) { |
| 110 | root->takeRow(0); |
| 111 | } |
| 112 | d->projectParses.remove(root); |
| 113 | |
| 114 | delete root; |
| 115 | |
| 116 | if (parser) |
| 117 | delete parser; |
| 118 | } |
| 119 | |
| 120 | QMenu *NinjaProjectGenerator::createItemMenu(const QStandardItem *item) |
| 121 | { |
| 122 | Q_UNUSED(item) |
| 123 | return nullptr; |
| 124 | } |
| 125 | |
| 126 | void NinjaProjectGenerator::doProjectChildsModified(const QList<QStandardItem *> &items) |
| 127 | { |
| 128 | auto rootItem = d->projectParses.key(qobject_cast<NinjaAsynParse*>(sender())); |
| 129 | if (rootItem) { |
| 130 | while (rootItem->hasChildren()) { |
| 131 | rootItem->takeRow(0); |
| 132 | } |
| 133 | rootItem->appendRows(items); |
| 134 | } |
| 135 | } |
| 136 | |