Changeset View
Changeset View
Standalone View
Standalone View
plugins/phabricator/phabricatorplugin.cpp
- This file was added.
1 | /* This file is part of KDevelop | ||||
---|---|---|---|---|---|
2 | Copyright 2017 Friedrich W. H. Kossebau <kossebau@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Library General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library 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 GNU | ||||
12 | Library General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "phabricatorplugin.h" | ||||
21 | | ||||
22 | #include "phabricatorhookupconfiguration.h" | ||||
23 | #include "phabricatorhookupconfigpage.h" | ||||
24 | | ||||
25 | #include <interfaces/iproject.h> | ||||
26 | #include <vcs/interfaces/ibranchingversioncontrol.h> | ||||
27 | #include <util/path.h> | ||||
28 | #include <vcs/vcsrevision.h> | ||||
29 | | ||||
30 | #include <KTextEditor/Cursor> | ||||
31 | #include <KConfigGroup> | ||||
32 | #include <KPluginFactory> | ||||
33 | | ||||
34 | #include <QIcon> | ||||
35 | | ||||
36 | using namespace KDevelop; | ||||
37 | | ||||
38 | K_PLUGIN_FACTORY_WITH_JSON(PhabricatorPluginFactory, "phabricatorplugin.json", | ||||
39 | registerPlugin<PhabricatorPlugin>();) | ||||
40 | | ||||
41 | | ||||
42 | namespace { | ||||
43 | | ||||
44 | namespace Strings { | ||||
45 | inline QString phabricatorGroupKey() { return QStringLiteral("Phabricator Settings"); } | ||||
46 | inline QString nameKey() { return QStringLiteral("Name"); } | ||||
47 | inline QString baseUrlKey() { return QStringLiteral("BaseUrl"); } | ||||
48 | inline QString repositoryNameKey() { return QStringLiteral("RepositoryName"); } | ||||
49 | inline QString repositoryIdKey() { return QStringLiteral("RepositoryId"); } | ||||
50 | } | ||||
51 | | ||||
52 | } | ||||
53 | | ||||
54 | | ||||
55 | PhabricatorPlugin::PhabricatorPlugin(QObject *parent, const QVariantList& args) | ||||
56 | : KDevelop::IPlugin(QStringLiteral("kdevphabricator"), parent) | ||||
57 | { | ||||
58 | Q_UNUSED(args); | ||||
59 | | ||||
60 | } | ||||
61 | | ||||
62 | PhabricatorPlugin::~PhabricatorPlugin() = default; | ||||
63 | | ||||
64 | | ||||
65 | QString PhabricatorPlugin::projectServiceId() const | ||||
66 | { | ||||
67 | return QStringLiteral("phabricator"); | ||||
68 | } | ||||
69 | | ||||
70 | QString PhabricatorPlugin::projectServiceDisplayName() const | ||||
71 | { | ||||
72 | return QStringLiteral("Phabricator"); | ||||
73 | } | ||||
74 | | ||||
75 | QIcon PhabricatorPlugin::projectServiceIcon() const | ||||
76 | { | ||||
77 | return QIcon::fromTheme(QStringLiteral("phabricator")); | ||||
78 | } | ||||
79 | | ||||
80 | QList<IProjectServiceHookupConfiguration*> PhabricatorPlugin::loadProjectServiceHookupConfigurations(IProject* project) const | ||||
81 | { | ||||
82 | QList<IProjectServiceHookupConfiguration*> result; | ||||
83 | | ||||
84 | KConfigGroup cfg(project->projectConfiguration(), Strings::phabricatorGroupKey()); | ||||
85 | | ||||
86 | for (const auto& groupName : cfg.groupList()) { | ||||
87 | KConfigGroup configurationGroup(&cfg, groupName); | ||||
88 | | ||||
89 | const QString name = configurationGroup.readEntry(Strings::nameKey(), QString()); | ||||
90 | auto phabricatorConfiguration = new PhabricatorHookupConfiguration(groupName, name); | ||||
91 | const QUrl baseUrl = QUrl::fromUserInput(configurationGroup.readEntry(Strings::baseUrlKey(), QString())); | ||||
92 | phabricatorConfiguration->setBaseUrl(baseUrl); | ||||
93 | const QString repositoryName = configurationGroup.readEntry(Strings::repositoryNameKey(), QString()); | ||||
94 | phabricatorConfiguration->setRepositoryName(repositoryName); | ||||
95 | const QString repositoryId = configurationGroup.readEntry(Strings::repositoryIdKey(), QString()); | ||||
96 | phabricatorConfiguration->setRepositoryId(repositoryId); | ||||
97 | | ||||
98 | result << phabricatorConfiguration; | ||||
99 | } | ||||
100 | | ||||
101 | return result; | ||||
102 | } | ||||
103 | | ||||
104 | void PhabricatorPlugin::saveProjectServiceHookupConfigurations(const QList<IProjectServiceHookupConfiguration*>& projectServiceHookupConfigurations, | ||||
105 | IProject* project) | ||||
106 | { | ||||
107 | QStringList configurationNames; | ||||
108 | for (auto configuration : projectServiceHookupConfigurations) { | ||||
109 | configurationNames.append(configuration->id()); | ||||
110 | } | ||||
111 | | ||||
112 | KConfigGroup cfg(project->projectConfiguration(), Strings::phabricatorGroupKey()); | ||||
113 | | ||||
114 | // clean up no longer existing configurations | ||||
115 | for (const auto& groupName : cfg.groupList()) { | ||||
116 | if (!configurationNames.contains(groupName)) { | ||||
117 | cfg.deleteGroup(groupName); | ||||
118 | } | ||||
119 | } | ||||
120 | | ||||
121 | // store current ones | ||||
122 | for (auto configuration : projectServiceHookupConfigurations) { | ||||
123 | auto phabricatorConfiguration = dynamic_cast<PhabricatorHookupConfiguration*>(configuration); | ||||
124 | Q_ASSERT(phabricatorConfiguration); | ||||
125 | if (!phabricatorConfiguration) { | ||||
126 | continue; | ||||
127 | } | ||||
128 | KConfigGroup configurationGroup(&cfg, configuration->id()); | ||||
129 | configurationGroup.deleteGroup(); | ||||
130 | | ||||
131 | configurationGroup.writeEntry(Strings::nameKey(), phabricatorConfiguration->displayName()); | ||||
132 | configurationGroup.writeEntry(Strings::baseUrlKey(), phabricatorConfiguration->baseUrl().toString()); | ||||
133 | configurationGroup.writeEntry(Strings::repositoryNameKey(), phabricatorConfiguration->repositoryName()); | ||||
134 | configurationGroup.writeEntry(Strings::repositoryIdKey(), phabricatorConfiguration->repositoryId()); | ||||
135 | } | ||||
136 | | ||||
137 | cfg.sync(); | ||||
138 | } | ||||
139 | | ||||
140 | IProjectServiceHookupConfiguration* PhabricatorPlugin::createProjectServiceHookupConfiguration(IProject* project) const | ||||
141 | { | ||||
142 | Q_UNUSED(project); | ||||
143 | return new PhabricatorHookupConfiguration(projectServiceId(), projectServiceDisplayName()); | ||||
144 | } | ||||
145 | | ||||
146 | ProjectServiceHookupConfigPage* PhabricatorPlugin::createProjectServiceHookupConfigPage(QWidget* parent) const | ||||
147 | { | ||||
148 | return new PhabricatorHookupConfigPage(parent); | ||||
149 | } | ||||
150 | | ||||
151 | IProjectServiceUrlMapper::ProjectServiceUrlMapperCapabilities PhabricatorPlugin::projectServiceUrlMapperCapabilities() const | ||||
152 | { | ||||
153 | return TextSelectionProjectUrlMapping | VcsRevisionProjectUrlMapping; | ||||
154 | } | ||||
155 | | ||||
156 | QVector<ProjectServiceUrlData> PhabricatorPlugin::projectServiceUrlsFromTextSelection(const QUrl& url, const KTextEditor::Cursor& cursor, IProject* project) const | ||||
157 | { | ||||
158 | const QString relativeFilePath = project->path().relativePath(Path(url)); | ||||
159 | if (relativeFilePath.isEmpty() || relativeFilePath.startsWith(QLatin1String("../"))) { | ||||
160 | return {}; | ||||
161 | } | ||||
162 | | ||||
163 | QVector<ProjectServiceUrlData> result; | ||||
164 | const auto configurations = loadProjectServiceHookupConfigurations(project); | ||||
165 | for (auto configuration : configurations) { | ||||
166 | const auto phabricatorConfiguration = dynamic_cast<PhabricatorHookupConfiguration*>(configuration); | ||||
167 | | ||||
168 | const QString repositoryName = phabricatorConfiguration->repositoryName(); | ||||
169 | QUrl url = phabricatorConfiguration->baseUrl(); | ||||
170 | if (!url.isValid()) { | ||||
171 | continue; | ||||
172 | } | ||||
173 | | ||||
174 | // TODO: get the remote tracked branch of the matching remote repo here instead | ||||
175 | const QString branch = m_projectVcsTracker.currentBranchName(project); | ||||
176 | if (branch.isEmpty()) { | ||||
177 | continue; | ||||
178 | } | ||||
179 | | ||||
180 | // TODO: find out what url encoding branch and relativeFilePath need | ||||
181 | const QString path = url.path() + QStringLiteral("/source/") + repositoryName + QLatin1String("/browse/") + branch + QLatin1Char('/') + relativeFilePath + QLatin1String("$") + QString::number(cursor.line()+1); | ||||
182 | url.setPath(path); | ||||
183 | | ||||
184 | ProjectServiceUrlData data; | ||||
185 | data.url = url; | ||||
186 | data.name = phabricatorConfiguration->displayName(); | ||||
187 | data.icon = projectServiceIcon(); | ||||
188 | result << data; | ||||
189 | } | ||||
190 | | ||||
191 | return result; | ||||
192 | } | ||||
193 | | ||||
194 | QVector<ProjectServiceUrlData> | ||||
195 | PhabricatorPlugin::projectServiceUrlsFromVcsRevision(const VcsRevision& revision, IProject* project) const | ||||
196 | { | ||||
197 | QVector<ProjectServiceUrlData> result; | ||||
198 | const QString revisionHash = revision.revisionValue().toString(); | ||||
199 | if (revisionHash.isEmpty()) { | ||||
200 | return result; | ||||
201 | } | ||||
202 | | ||||
203 | const auto configurations = loadProjectServiceHookupConfigurations(project); | ||||
204 | for (auto configuration : configurations) { | ||||
205 | const auto phabricatorConfiguration = dynamic_cast<PhabricatorHookupConfiguration*>(configuration); | ||||
206 | | ||||
207 | const QString repositoryId = phabricatorConfiguration->repositoryId(); | ||||
208 | QUrl url = phabricatorConfiguration->baseUrl(); | ||||
209 | if (!url.isValid()) { | ||||
210 | continue; | ||||
211 | } | ||||
212 | | ||||
213 | // TODO: find out what url encoding branch and relativeFilePath need | ||||
214 | const QString path = url.path() + QLatin1Char('/') + repositoryId + QLatin1Char(':') + revisionHash; | ||||
215 | url.setPath(path); | ||||
216 | | ||||
217 | ProjectServiceUrlData data; | ||||
218 | data.url = url; | ||||
219 | data.name = phabricatorConfiguration->displayName(); | ||||
220 | data.icon = projectServiceIcon(); | ||||
221 | result << data; | ||||
222 | } | ||||
223 | | ||||
224 | return result; | ||||
225 | } | ||||
226 | | ||||
227 | // needed for QObject class created from K_PLUGIN_FACTORY_WITH_JSON | ||||
228 | #include "phabricatorplugin.moc" |