Changeset View
Changeset View
Standalone View
Standalone View
plugins/gitlab/gitlabplugin.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 "gitlabplugin.h" | ||||
21 | | ||||
22 | #include "gitlabhookupconfiguration.h" | ||||
23 | #include "gitlabhookupconfigpage.h" | ||||
24 | | ||||
25 | #include <interfaces/iproject.h> | ||||
26 | #include <vcs/interfaces/ibranchingversioncontrol.h> | ||||
27 | #include <util/path.h> | ||||
28 | | ||||
29 | #include <KTextEditor/Cursor> | ||||
30 | #include <KConfigGroup> | ||||
31 | #include <KPluginFactory> | ||||
32 | | ||||
33 | #include <QIcon> | ||||
34 | | ||||
35 | using namespace KDevelop; | ||||
36 | | ||||
37 | K_PLUGIN_FACTORY_WITH_JSON(GitLabPluginFactory, "gitlabplugin.json", | ||||
38 | registerPlugin<GitLabPlugin>();) | ||||
39 | | ||||
40 | | ||||
41 | namespace { | ||||
42 | | ||||
43 | namespace Strings { | ||||
44 | inline QString gitlabGroupKey() { return QStringLiteral("GitLab Settings"); } | ||||
45 | inline QString nameKey() { return QStringLiteral("Name"); } | ||||
46 | inline QString baseUrlKey() { return QStringLiteral("BaseUrl"); } | ||||
47 | inline QString repositoryNameKey() { return QStringLiteral("RepositoryName"); } | ||||
48 | } | ||||
49 | | ||||
50 | } | ||||
51 | | ||||
52 | | ||||
53 | GitLabPlugin::GitLabPlugin(QObject *parent, const QVariantList& args) | ||||
54 | : KDevelop::IPlugin(QStringLiteral("kdevgitlab"), parent) | ||||
55 | { | ||||
56 | Q_UNUSED(args); | ||||
57 | | ||||
58 | } | ||||
59 | | ||||
60 | GitLabPlugin::~GitLabPlugin() = default; | ||||
61 | | ||||
62 | | ||||
63 | QString GitLabPlugin::projectServiceId() const | ||||
64 | { | ||||
65 | return QStringLiteral("gitlab"); | ||||
66 | } | ||||
67 | | ||||
68 | QString GitLabPlugin::projectServiceDisplayName() const | ||||
69 | { | ||||
70 | return QStringLiteral("GitLab"); | ||||
71 | } | ||||
72 | | ||||
73 | QIcon GitLabPlugin::projectServiceIcon() const | ||||
74 | { | ||||
75 | return QIcon::fromTheme(QStringLiteral("gitlab")); | ||||
76 | } | ||||
77 | | ||||
78 | QList<IProjectServiceHookupConfiguration*> GitLabPlugin::loadProjectServiceHookupConfigurations(IProject* project) const | ||||
79 | { | ||||
80 | QList<IProjectServiceHookupConfiguration*> result; | ||||
81 | | ||||
82 | KConfigGroup cfg(project->projectConfiguration(), Strings::gitlabGroupKey()); | ||||
83 | | ||||
84 | for (const auto& groupName : cfg.groupList()) { | ||||
85 | KConfigGroup configurationGroup(&cfg, groupName); | ||||
86 | | ||||
87 | const QString name = configurationGroup.readEntry(Strings::nameKey(), QString()); | ||||
88 | auto gitlabConfiguration = new GitLabHookupConfiguration(groupName, name); | ||||
89 | const QUrl baseUrl = QUrl::fromUserInput(configurationGroup.readEntry(Strings::baseUrlKey(), QString())); | ||||
90 | gitlabConfiguration->setBaseUrl(baseUrl); | ||||
91 | | ||||
92 | result << gitlabConfiguration; | ||||
93 | } | ||||
94 | | ||||
95 | return result; | ||||
96 | } | ||||
97 | | ||||
98 | void GitLabPlugin::saveProjectServiceHookupConfigurations(const QList<IProjectServiceHookupConfiguration*>& projectServiceHookupConfigurations, | ||||
99 | IProject* project) | ||||
100 | { | ||||
101 | QStringList configurationNames; | ||||
102 | for (auto configuration : projectServiceHookupConfigurations) { | ||||
103 | configurationNames.append(configuration->id()); | ||||
104 | } | ||||
105 | | ||||
106 | KConfigGroup cfg(project->projectConfiguration(), Strings::gitlabGroupKey()); | ||||
107 | | ||||
108 | // clean up no longer existing configurations | ||||
109 | for (const auto& groupName : cfg.groupList()) { | ||||
110 | if (!configurationNames.contains(groupName)) { | ||||
111 | cfg.deleteGroup(groupName); | ||||
112 | } | ||||
113 | } | ||||
114 | | ||||
115 | // store current ones | ||||
116 | for (auto configuration : projectServiceHookupConfigurations) { | ||||
117 | auto gitlabConfiguration = dynamic_cast<GitLabHookupConfiguration*>(configuration); | ||||
118 | Q_ASSERT(gitlabConfiguration); | ||||
119 | if (!gitlabConfiguration) { | ||||
120 | continue; | ||||
121 | } | ||||
122 | KConfigGroup configurationGroup(&cfg, configuration->id()); | ||||
123 | configurationGroup.deleteGroup(); | ||||
124 | | ||||
125 | configurationGroup.writeEntry(Strings::nameKey(), gitlabConfiguration->displayName()); | ||||
126 | configurationGroup.writeEntry(Strings::baseUrlKey(), gitlabConfiguration->baseUrl().toString()); | ||||
127 | } | ||||
128 | | ||||
129 | cfg.sync(); | ||||
130 | } | ||||
131 | | ||||
132 | IProjectServiceHookupConfiguration* GitLabPlugin::createProjectServiceHookupConfiguration(IProject* project) const | ||||
133 | { | ||||
134 | Q_UNUSED(project); | ||||
135 | return new GitLabHookupConfiguration(projectServiceId(), projectServiceDisplayName()); | ||||
136 | } | ||||
137 | | ||||
138 | ProjectServiceHookupConfigPage* GitLabPlugin::createProjectServiceHookupConfigPage(QWidget* parent) const | ||||
139 | { | ||||
140 | return new GitLabHookupConfigPage(parent); | ||||
141 | } | ||||
142 | | ||||
143 | IProjectServiceUrlMapper::ProjectServiceUrlMapperCapabilities GitLabPlugin::projectServiceUrlMapperCapabilities() const | ||||
144 | { | ||||
145 | return TextSelectionProjectUrlMapping; | ||||
146 | } | ||||
147 | | ||||
148 | QVector<ProjectServiceUrlData> GitLabPlugin::projectServiceUrlsFromTextSelection(const QUrl& url, const KTextEditor::Cursor& cursor, IProject* project) const | ||||
149 | { | ||||
150 | const QString relativeFilePath = project->path().relativePath(Path(url)); | ||||
151 | if (relativeFilePath.isEmpty() || relativeFilePath.startsWith(QLatin1String("../"))) { | ||||
152 | return {}; | ||||
153 | } | ||||
154 | | ||||
155 | QVector<ProjectServiceUrlData> result; | ||||
156 | const auto configurations = loadProjectServiceHookupConfigurations(project); | ||||
157 | for (auto configuration : configurations) { | ||||
158 | const auto gitlabConfiguration = dynamic_cast<GitLabHookupConfiguration*>(configuration); | ||||
159 | | ||||
160 | QUrl url = gitlabConfiguration->baseUrl(); | ||||
161 | if (!url.isValid()) { | ||||
162 | continue; | ||||
163 | } | ||||
164 | | ||||
165 | // TODO: get the remote tracked branch of the matching remote repo here instead | ||||
166 | const QString branch = m_projectVcsTracker.currentBranchName(project); | ||||
167 | if (branch.isEmpty()) { | ||||
168 | continue; | ||||
169 | } | ||||
170 | | ||||
171 | // TODO: find out what url encoding branch and relativeFilePath need | ||||
172 | const QString path = url.path() + QLatin1String("/blob/") + branch + QLatin1Char('/') + relativeFilePath; | ||||
173 | url.setPath(path); | ||||
174 | url.setFragment(QLatin1Char('L') + QString::number(cursor.line()+1)); | ||||
175 | | ||||
176 | ProjectServiceUrlData data; | ||||
177 | data.url = url; | ||||
178 | data.name = gitlabConfiguration->displayName(); | ||||
179 | data.icon = projectServiceIcon(); | ||||
180 | result << data; | ||||
181 | } | ||||
182 | | ||||
183 | return result; | ||||
184 | } | ||||
185 | | ||||
186 | // needed for QObject class created from K_PLUGIN_FACTORY_WITH_JSON | ||||
187 | #include "gitlabplugin.moc" |