]> cloud.milkyroute.net Git - dolphin.git/blob - src/versioncontrol/fileviewsvnplugin.cpp
Use KFileMetaDataWidget from kdelibs. Still open: Provide dialog which wraps KFileMet...
[dolphin.git] / src / versioncontrol / fileviewsvnplugin.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program 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 *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "fileviewsvnplugin.h"
21
22 #include <kaction.h>
23 #include <kdemacros.h>
24 #include <kdialog.h>
25 #include <kfileitem.h>
26 #include <kicon.h>
27 #include <klocale.h>
28 #include <krun.h>
29 #include <kshell.h>
30 #include <kvbox.h>
31 #include <QDir>
32 #include <QLabel>
33 #include <QProcess>
34 #include <QString>
35 #include <QStringList>
36 #include <QTextEdit>
37 #include <QTextStream>
38
39 #include <KPluginFactory>
40 #include <KPluginLoader>
41 K_PLUGIN_FACTORY(FileViewSvnPluginFactory, registerPlugin<FileViewSvnPlugin>();)
42 K_EXPORT_PLUGIN(FileViewSvnPluginFactory("fileviewsvnplugin"))
43
44 FileViewSvnPlugin::FileViewSvnPlugin(QObject* parent, const QList<QVariant>& args) :
45 KVersionControlPlugin(parent),
46 m_pendingOperation(false),
47 m_versionInfoHash(),
48 m_versionInfoKeys(),
49 m_updateAction(0),
50 m_showLocalChangesAction(0),
51 m_commitAction(0),
52 m_addAction(0),
53 m_removeAction(0),
54 m_command(),
55 m_errorMsg(),
56 m_operationCompletedMsg(),
57 m_contextDir(),
58 m_contextItems(),
59 m_tempFile()
60 {
61 Q_UNUSED(args);
62
63 m_updateAction = new KAction(this);
64 m_updateAction->setIcon(KIcon("view-refresh"));
65 m_updateAction->setText(i18nc("@item:inmenu", "SVN Update"));
66 connect(m_updateAction, SIGNAL(triggered()),
67 this, SLOT(updateFiles()));
68
69 m_showLocalChangesAction = new KAction(this);
70 m_showLocalChangesAction->setIcon(KIcon("view-split-left-right"));
71 m_showLocalChangesAction->setText(i18nc("@item:inmenu", "Show Local SVN Changes"));
72 connect(m_showLocalChangesAction, SIGNAL(triggered()),
73 this, SLOT(showLocalChanges()));
74
75 m_commitAction = new KAction(this);
76 m_commitAction->setText(i18nc("@item:inmenu", "SVN Commit..."));
77 connect(m_commitAction, SIGNAL(triggered()),
78 this, SLOT(commitFiles()));
79
80 m_addAction = new KAction(this);
81 m_addAction->setIcon(KIcon("list-add"));
82 m_addAction->setText(i18nc("@item:inmenu", "SVN Add"));
83 connect(m_addAction, SIGNAL(triggered()),
84 this, SLOT(addFiles()));
85
86 m_removeAction = new KAction(this);
87 m_removeAction->setIcon(KIcon("list-remove"));
88 m_removeAction->setText(i18nc("@item:inmenu", "SVN Delete"));
89 connect(m_removeAction, SIGNAL(triggered()),
90 this, SLOT(removeFiles()));
91 }
92
93 FileViewSvnPlugin::~FileViewSvnPlugin()
94 {
95 }
96
97 QString FileViewSvnPlugin::fileName() const
98 {
99 return ".svn";
100 }
101
102 bool FileViewSvnPlugin::beginRetrieval(const QString& directory)
103 {
104 Q_ASSERT(directory.endsWith('/'));
105
106 // clear all entries for this directory
107 QMutableHashIterator<QString, VersionState> it(m_versionInfoHash);
108 while (it.hasNext()) {
109 it.next();
110 if (it.key().startsWith(directory)) {
111 it.remove();
112 }
113 }
114
115 QStringList arguments;
116 arguments << "status" << "--show-updates" << directory;
117
118 QProcess process;
119 process.start("svn", arguments);
120 while (process.waitForReadyRead()) {
121 char buffer[1024];
122 while (process.readLine(buffer, sizeof(buffer)) > 0) {
123 VersionState state = NormalVersion;
124 QString filePath(buffer);
125
126 switch (buffer[0]) {
127 case '?': state = UnversionedVersion; break;
128 case 'M': state = LocallyModifiedVersion; break;
129 case 'A': state = AddedVersion; break;
130 case 'D': state = RemovedVersion; break;
131 case 'C': state = ConflictingVersion; break;
132 default:
133 if (filePath.contains('*')) {
134 state = UpdateRequiredVersion;
135 }
136 break;
137 }
138
139 int pos = filePath.indexOf('/');
140 const int length = filePath.length() - pos - 1;
141 filePath = filePath.mid(pos, length);
142 if (!filePath.isEmpty()) {
143 m_versionInfoHash.insert(filePath, state);
144 }
145 }
146 }
147
148 m_versionInfoKeys = m_versionInfoHash.keys();
149 return true;
150 }
151
152 void FileViewSvnPlugin::endRetrieval()
153 {
154 }
155
156 KVersionControlPlugin::VersionState FileViewSvnPlugin::versionState(const KFileItem& item)
157 {
158 const QString itemUrl = item.localPath();
159 if (m_versionInfoHash.contains(itemUrl)) {
160 return m_versionInfoHash.value(itemUrl);
161 }
162
163 if (!item.isDir()) {
164 // files that have not been listed by 'svn status' (= m_versionInfoHash)
165 // are under version control per definition
166 return NormalVersion;
167 }
168
169 // The item is a directory. Check whether an item listed by 'svn status' (= m_versionInfoHash)
170 // is part of this directory. In this case a local modification should be indicated in the
171 // directory already.
172 foreach (const QString& key, m_versionInfoKeys) {
173 if (key.startsWith(itemUrl)) {
174 const VersionState state = m_versionInfoHash.value(key);
175 if (state == LocallyModifiedVersion) {
176 return LocallyModifiedVersion;
177 }
178 }
179 }
180
181 return NormalVersion;
182 }
183
184 QList<QAction*> FileViewSvnPlugin::contextMenuActions(const KFileItemList& items)
185 {
186 Q_ASSERT(!items.isEmpty());
187 foreach (const KFileItem& item, items) {
188 m_contextItems.append(item);
189 }
190 m_contextDir.clear();
191
192 // iterate all items and check the version state to know which
193 // actions can be enabled
194 const int itemsCount = items.count();
195 int versionedCount = 0;
196 int editingCount = 0;
197 foreach (const KFileItem& item, items) {
198 const VersionState state = versionState(item);
199 if (state != UnversionedVersion) {
200 ++versionedCount;
201 }
202
203 switch (state) {
204 case LocallyModifiedVersion:
205 case ConflictingVersion:
206 ++editingCount;
207 break;
208 default:
209 break;
210 }
211 }
212 m_commitAction->setEnabled(editingCount > 0);
213 m_addAction->setEnabled(versionedCount == 0);
214 m_removeAction->setEnabled(versionedCount == itemsCount);
215
216 QList<QAction*> actions;
217 actions.append(m_updateAction);
218 actions.append(m_commitAction);
219 actions.append(m_addAction);
220 actions.append(m_removeAction);
221 return actions;
222 }
223
224 QList<QAction*> FileViewSvnPlugin::contextMenuActions(const QString& directory)
225 {
226 const bool enabled = !m_pendingOperation;
227 if (enabled) {
228 m_contextDir = directory;
229 }
230
231 // Only enable the SVN actions if no SVN commands are
232 // executed currently (see slotOperationCompleted() and
233 // startSvnCommandProcess()).
234 m_updateAction->setEnabled(enabled);
235 m_showLocalChangesAction->setEnabled(enabled);
236 m_commitAction->setEnabled(enabled);
237
238 QList<QAction*> actions;
239 actions.append(m_updateAction);
240 actions.append(m_showLocalChangesAction);
241 actions.append(m_commitAction);
242 return actions;
243 }
244
245 void FileViewSvnPlugin::updateFiles()
246 {
247 execSvnCommand("update",
248 i18nc("@info:status", "Updating SVN repository..."),
249 i18nc("@info:status", "Update of SVN repository failed."),
250 i18nc("@info:status", "Updated SVN repository."));
251 }
252
253 void FileViewSvnPlugin::showLocalChanges()
254 {
255 Q_ASSERT(!m_contextDir.isEmpty());
256 Q_ASSERT(m_contextItems.isEmpty());
257
258 const QString command = "mkfifo /tmp/fifo; svn diff " +
259 KShell::quoteArg(m_contextDir) +
260 " > /tmp/fifo & kompare /tmp/fifo; rm /tmp/fifo";
261 KRun::runCommand(command, 0);
262 }
263
264 void FileViewSvnPlugin::commitFiles()
265 {
266 KDialog dialog(0, Qt::Dialog);
267
268 KVBox* box = new KVBox(&dialog);
269 new QLabel(i18nc("@label", "Description:"), box);
270 QTextEdit* editor = new QTextEdit(box);
271
272 dialog.setMainWidget(box);
273 dialog.setCaption(i18nc("@title:window", "SVN Commit"));
274 dialog.setButtons(KDialog::Ok | KDialog::Cancel);
275 dialog.setDefaultButton(KDialog::Ok);
276 dialog.setButtonText(KDialog::Ok, i18nc("@action:button", "Commit"));
277
278 KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
279 "SvnCommitDialog");
280 dialog.restoreDialogSize(dialogConfig);
281
282 if (dialog.exec() == QDialog::Accepted) {
283 // Write the commit description into a temporary file, so
284 // that it can be read by the command "svn commit -F". The temporary
285 // file must stay alive until slotOperationCompleted() is invoked and will
286 // be destroyed when the version plugin is destructed.
287 if (!m_tempFile.open()) {
288 emit errorMessage(i18nc("@info:status", "Commit of SVN changes failed."));
289 return;
290 }
291
292 QTextStream out(&m_tempFile);
293 const QString fileName = m_tempFile.fileName();
294 out << editor->toPlainText();
295 m_tempFile.close();
296
297 execSvnCommand("commit -F " + KShell::quoteArg(fileName),
298 i18nc("@info:status", "Committing SVN changes..."),
299 i18nc("@info:status", "Commit of SVN changes failed."),
300 i18nc("@info:status", "Committed SVN changes."));
301 }
302
303 dialog.saveDialogSize(dialogConfig, KConfigBase::Persistent);
304 }
305
306 void FileViewSvnPlugin::addFiles()
307 {
308 execSvnCommand("add",
309 i18nc("@info:status", "Adding files to SVN repository..."),
310 i18nc("@info:status", "Adding of files to SVN repository failed."),
311 i18nc("@info:status", "Added files to SVN repository."));
312 }
313
314 void FileViewSvnPlugin::removeFiles()
315 {
316 execSvnCommand("remove",
317 i18nc("@info:status", "Removing files from SVN repository..."),
318 i18nc("@info:status", "Removing of files from SVN repository failed."),
319 i18nc("@info:status", "Removed files from SVN repository."));
320 }
321
322 void FileViewSvnPlugin::slotOperationCompleted(int exitCode, QProcess::ExitStatus exitStatus)
323 {
324 m_pendingOperation = false;
325
326 if ((exitStatus != QProcess::NormalExit) || (exitCode != 0)) {
327 emit errorMessage(m_errorMsg);
328 } else if (m_contextItems.isEmpty()) {
329 emit operationCompletedMessage(m_operationCompletedMsg);
330 emit versionStatesChanged();
331 } else {
332 startSvnCommandProcess();
333 }
334 }
335
336 void FileViewSvnPlugin::slotOperationError()
337 {
338 // don't do any operation on other items anymore
339 m_contextItems.clear();
340 m_pendingOperation = false;
341
342 emit errorMessage(m_errorMsg);
343 }
344
345 void FileViewSvnPlugin::execSvnCommand(const QString& svnCommand,
346 const QString& infoMsg,
347 const QString& errorMsg,
348 const QString& operationCompletedMsg)
349 {
350 emit infoMessage(infoMsg);
351
352 m_command = svnCommand;
353 m_errorMsg = errorMsg;
354 m_operationCompletedMsg = operationCompletedMsg;
355
356 startSvnCommandProcess();
357 }
358
359 void FileViewSvnPlugin::startSvnCommandProcess()
360 {
361 m_pendingOperation = true;
362
363 QProcess* process = new QProcess(this);
364 connect(process, SIGNAL(finished(int, QProcess::ExitStatus)),
365 this, SLOT(slotOperationCompleted(int, QProcess::ExitStatus)));
366 connect(process, SIGNAL(error(QProcess::ProcessError)),
367 this, SLOT(slotOperationError()));
368
369 const QString program = "svn " + m_command + ' ';
370 if (!m_contextDir.isEmpty()) {
371 process->start(program + KShell::quoteArg(m_contextDir));
372 m_contextDir.clear();
373 } else {
374 const KFileItem item = m_contextItems.takeLast();
375 process->start(program + KShell::quoteArg(item.localPath()));
376 // the remaining items of m_contextItems will be executed
377 // after the process has finished (see slotOperationFinished())
378 }
379 }