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