]> cloud.milkyroute.net Git - dolphin.git/blob - src/versioncontrol/fileviewsvnplugin.cpp
Remove the redundant m_versionInfoKeys member, just iterate the hash table instead
[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_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 including the entries
106 // for sub directories
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 // Only values with a different state as 'NormalVersion'
140 // are added to the hash table. If a value is not in the
141 // hash table, it is automatically defined as 'NormalVersion'
142 // (see FileViewSvnPlugin::versionState()).
143 if (state != NormalVersion) {
144 int pos = filePath.indexOf('/');
145 const int length = filePath.length() - pos - 1;
146 filePath = filePath.mid(pos, length);
147 if (!filePath.isEmpty()) {
148 m_versionInfoHash.insert(filePath, state);
149 }
150 }
151 }
152 }
153
154 return true;
155 }
156
157 void FileViewSvnPlugin::endRetrieval()
158 {
159 }
160
161 KVersionControlPlugin::VersionState FileViewSvnPlugin::versionState(const KFileItem& item)
162 {
163 const QString itemUrl = item.localPath();
164 if (m_versionInfoHash.contains(itemUrl)) {
165 return m_versionInfoHash.value(itemUrl);
166 }
167
168 if (!item.isDir()) {
169 // files that have not been listed by 'svn status' (= m_versionInfoHash)
170 // are under version control per definition
171 return NormalVersion;
172 }
173
174 // The item is a directory. Check whether an item listed by 'svn status' (= m_versionInfoHash)
175 // is part of this directory. In this case a local modification should be indicated in the
176 // directory already.
177 QHash<QString, VersionState>::const_iterator it = m_versionInfoHash.constBegin();
178 while (it != m_versionInfoHash.constEnd()) {
179 if (it.key().startsWith(itemUrl)) {
180 const VersionState state = m_versionInfoHash.value(it.key());
181 if (state == LocallyModifiedVersion) {
182 return LocallyModifiedVersion;
183 }
184 }
185 ++it;
186 }
187
188 return NormalVersion;
189 }
190
191 QList<QAction*> FileViewSvnPlugin::contextMenuActions(const KFileItemList& items)
192 {
193 Q_ASSERT(!items.isEmpty());
194 foreach (const KFileItem& item, items) {
195 m_contextItems.append(item);
196 }
197 m_contextDir.clear();
198
199 // iterate all items and check the version state to know which
200 // actions can be enabled
201 const int itemsCount = items.count();
202 int versionedCount = 0;
203 int editingCount = 0;
204 foreach (const KFileItem& item, items) {
205 const VersionState state = versionState(item);
206 if (state != UnversionedVersion) {
207 ++versionedCount;
208 }
209
210 switch (state) {
211 case LocallyModifiedVersion:
212 case ConflictingVersion:
213 ++editingCount;
214 break;
215 default:
216 break;
217 }
218 }
219 m_commitAction->setEnabled(editingCount > 0);
220 m_addAction->setEnabled(versionedCount == 0);
221 m_removeAction->setEnabled(versionedCount == itemsCount);
222
223 QList<QAction*> actions;
224 actions.append(m_updateAction);
225 actions.append(m_commitAction);
226 actions.append(m_addAction);
227 actions.append(m_removeAction);
228 return actions;
229 }
230
231 QList<QAction*> FileViewSvnPlugin::contextMenuActions(const QString& directory)
232 {
233 const bool enabled = !m_pendingOperation;
234 if (enabled) {
235 m_contextDir = directory;
236 }
237
238 // Only enable the SVN actions if no SVN commands are
239 // executed currently (see slotOperationCompleted() and
240 // startSvnCommandProcess()).
241 m_updateAction->setEnabled(enabled);
242 m_showLocalChangesAction->setEnabled(enabled);
243 m_commitAction->setEnabled(enabled);
244
245 QList<QAction*> actions;
246 actions.append(m_updateAction);
247 actions.append(m_showLocalChangesAction);
248 actions.append(m_commitAction);
249 return actions;
250 }
251
252 void FileViewSvnPlugin::updateFiles()
253 {
254 execSvnCommand("update",
255 i18nc("@info:status", "Updating SVN repository..."),
256 i18nc("@info:status", "Update of SVN repository failed."),
257 i18nc("@info:status", "Updated SVN repository."));
258 }
259
260 void FileViewSvnPlugin::showLocalChanges()
261 {
262 Q_ASSERT(!m_contextDir.isEmpty());
263 Q_ASSERT(m_contextItems.isEmpty());
264
265 const QString command = "mkfifo /tmp/fifo; svn diff " +
266 KShell::quoteArg(m_contextDir) +
267 " > /tmp/fifo & kompare /tmp/fifo; rm /tmp/fifo";
268 KRun::runCommand(command, 0);
269 }
270
271 void FileViewSvnPlugin::commitFiles()
272 {
273 KDialog dialog(0, Qt::Dialog);
274
275 KVBox* box = new KVBox(&dialog);
276 new QLabel(i18nc("@label", "Description:"), box);
277 QTextEdit* editor = new QTextEdit(box);
278
279 dialog.setMainWidget(box);
280 dialog.setCaption(i18nc("@title:window", "SVN Commit"));
281 dialog.setButtons(KDialog::Ok | KDialog::Cancel);
282 dialog.setDefaultButton(KDialog::Ok);
283 dialog.setButtonText(KDialog::Ok, i18nc("@action:button", "Commit"));
284
285 KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
286 "SvnCommitDialog");
287 dialog.restoreDialogSize(dialogConfig);
288
289 if (dialog.exec() == QDialog::Accepted) {
290 // Write the commit description into a temporary file, so
291 // that it can be read by the command "svn commit -F". The temporary
292 // file must stay alive until slotOperationCompleted() is invoked and will
293 // be destroyed when the version plugin is destructed.
294 if (!m_tempFile.open()) {
295 emit errorMessage(i18nc("@info:status", "Commit of SVN changes failed."));
296 return;
297 }
298
299 QTextStream out(&m_tempFile);
300 const QString fileName = m_tempFile.fileName();
301 out << editor->toPlainText();
302 m_tempFile.close();
303
304 execSvnCommand("commit -F " + KShell::quoteArg(fileName),
305 i18nc("@info:status", "Committing SVN changes..."),
306 i18nc("@info:status", "Commit of SVN changes failed."),
307 i18nc("@info:status", "Committed SVN changes."));
308 }
309
310 dialog.saveDialogSize(dialogConfig, KConfigBase::Persistent);
311 }
312
313 void FileViewSvnPlugin::addFiles()
314 {
315 execSvnCommand("add",
316 i18nc("@info:status", "Adding files to SVN repository..."),
317 i18nc("@info:status", "Adding of files to SVN repository failed."),
318 i18nc("@info:status", "Added files to SVN repository."));
319 }
320
321 void FileViewSvnPlugin::removeFiles()
322 {
323 execSvnCommand("remove",
324 i18nc("@info:status", "Removing files from SVN repository..."),
325 i18nc("@info:status", "Removing of files from SVN repository failed."),
326 i18nc("@info:status", "Removed files from SVN repository."));
327 }
328
329 void FileViewSvnPlugin::slotOperationCompleted(int exitCode, QProcess::ExitStatus exitStatus)
330 {
331 m_pendingOperation = false;
332
333 if ((exitStatus != QProcess::NormalExit) || (exitCode != 0)) {
334 emit errorMessage(m_errorMsg);
335 } else if (m_contextItems.isEmpty()) {
336 emit operationCompletedMessage(m_operationCompletedMsg);
337 emit versionStatesChanged();
338 } else {
339 startSvnCommandProcess();
340 }
341 }
342
343 void FileViewSvnPlugin::slotOperationError()
344 {
345 // don't do any operation on other items anymore
346 m_contextItems.clear();
347 m_pendingOperation = false;
348
349 emit errorMessage(m_errorMsg);
350 }
351
352 void FileViewSvnPlugin::execSvnCommand(const QString& svnCommand,
353 const QString& infoMsg,
354 const QString& errorMsg,
355 const QString& operationCompletedMsg)
356 {
357 emit infoMessage(infoMsg);
358
359 m_command = svnCommand;
360 m_errorMsg = errorMsg;
361 m_operationCompletedMsg = operationCompletedMsg;
362
363 startSvnCommandProcess();
364 }
365
366 void FileViewSvnPlugin::startSvnCommandProcess()
367 {
368 m_pendingOperation = true;
369
370 QProcess* process = new QProcess(this);
371 connect(process, SIGNAL(finished(int, QProcess::ExitStatus)),
372 this, SLOT(slotOperationCompleted(int, QProcess::ExitStatus)));
373 connect(process, SIGNAL(error(QProcess::ProcessError)),
374 this, SLOT(slotOperationError()));
375
376 const QString program = "svn " + m_command + ' ';
377 if (!m_contextDir.isEmpty()) {
378 process->start(program + KShell::quoteArg(m_contextDir));
379 m_contextDir.clear();
380 } else {
381 const KFileItem item = m_contextItems.takeLast();
382 process->start(program + KShell::quoteArg(item.localPath()));
383 // the remaining items of m_contextItems will be executed
384 // after the process has finished (see slotOperationFinished())
385 }
386 }