]>
cloud.milkyroute.net Git - dolphin.git/blob - src/versioncontrol/fileviewsvnplugin.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 ***************************************************************************/
20 #include "fileviewsvnplugin.h"
23 #include <kdemacros.h>
25 #include <kfileitem.h>
35 #include <QStringList>
37 #include <QTextStream>
39 #include <KPluginFactory>
40 #include <KPluginLoader>
41 K_PLUGIN_FACTORY(FileViewSvnPluginFactory
, registerPlugin
<FileViewSvnPlugin
>();)
42 K_EXPORT_PLUGIN(FileViewSvnPluginFactory("fileviewsvnplugin"))
44 FileViewSvnPlugin::FileViewSvnPlugin(QObject
* parent
, const QList
<QVariant
>& args
) :
45 KVersionControlPlugin(parent
),
46 m_pendingOperation(false),
50 m_showLocalChangesAction(0),
56 m_operationCompletedMsg(),
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()));
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()));
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()));
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()));
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()));
93 FileViewSvnPlugin::~FileViewSvnPlugin()
97 QString
FileViewSvnPlugin::fileName() const
102 bool FileViewSvnPlugin::beginRetrieval(const QString
& directory
)
104 Q_ASSERT(directory
.endsWith('/'));
106 // clear all entries for this directory
107 QMutableHashIterator
<QString
, VersionState
> it(m_versionInfoHash
);
108 while (it
.hasNext()) {
110 if (it
.key().startsWith(directory
)) {
115 QStringList arguments
;
116 arguments
<< "status" << "--show-updates" << directory
;
119 process
.start("svn", arguments
);
120 while (process
.waitForReadyRead()) {
122 while (process
.readLine(buffer
, sizeof(buffer
)) > 0) {
123 VersionState state
= NormalVersion
;
124 QString
filePath(buffer
);
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;
133 if (filePath
.contains('*')) {
134 state
= UpdateRequiredVersion
;
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
);
148 m_versionInfoKeys
= m_versionInfoHash
.keys();
152 void FileViewSvnPlugin::endRetrieval()
156 KVersionControlPlugin::VersionState
FileViewSvnPlugin::versionState(const KFileItem
& item
)
158 const QString itemUrl
= item
.localPath();
159 if (m_versionInfoHash
.contains(itemUrl
)) {
160 return m_versionInfoHash
.value(itemUrl
);
164 // files that have not been listed by 'svn status' (= m_versionInfoHash)
165 // are under version control per definition
166 return NormalVersion
;
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
;
181 return NormalVersion
;
184 QList
<QAction
*> FileViewSvnPlugin::contextMenuActions(const KFileItemList
& items
)
186 Q_ASSERT(!items
.isEmpty());
187 foreach (const KFileItem
& item
, items
) {
188 m_contextItems
.append(item
);
190 m_contextDir
.clear();
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
) {
204 case LocallyModifiedVersion
:
205 case ConflictingVersion
:
212 m_commitAction
->setEnabled(editingCount
> 0);
213 m_addAction
->setEnabled(versionedCount
== 0);
214 m_removeAction
->setEnabled(versionedCount
== itemsCount
);
216 QList
<QAction
*> actions
;
217 actions
.append(m_updateAction
);
218 actions
.append(m_commitAction
);
219 actions
.append(m_addAction
);
220 actions
.append(m_removeAction
);
224 QList
<QAction
*> FileViewSvnPlugin::contextMenuActions(const QString
& directory
)
226 const bool enabled
= !m_pendingOperation
;
228 m_contextDir
= directory
;
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
);
238 QList
<QAction
*> actions
;
239 actions
.append(m_updateAction
);
240 actions
.append(m_showLocalChangesAction
);
241 actions
.append(m_commitAction
);
245 void FileViewSvnPlugin::updateFiles()
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."));
253 void FileViewSvnPlugin::showLocalChanges()
255 Q_ASSERT(!m_contextDir
.isEmpty());
256 Q_ASSERT(m_contextItems
.isEmpty());
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);
264 void FileViewSvnPlugin::commitFiles()
266 KDialog
dialog(0, Qt::Dialog
);
268 KVBox
* box
= new KVBox(&dialog
);
269 new QLabel(i18nc("@label", "Description:"), box
);
270 QTextEdit
* editor
= new QTextEdit(box
);
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"));
278 KConfigGroup
dialogConfig(KSharedConfig::openConfig("dolphinrc"),
280 dialog
.restoreDialogSize(dialogConfig
);
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."));
292 QTextStream
out(&m_tempFile
);
293 const QString fileName
= m_tempFile
.fileName();
294 out
<< editor
->toPlainText();
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."));
303 dialog
.saveDialogSize(dialogConfig
, KConfigBase::Persistent
);
306 void FileViewSvnPlugin::addFiles()
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."));
314 void FileViewSvnPlugin::removeFiles()
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."));
322 void FileViewSvnPlugin::slotOperationCompleted(int exitCode
, QProcess::ExitStatus exitStatus
)
324 m_pendingOperation
= false;
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();
332 startSvnCommandProcess();
336 void FileViewSvnPlugin::slotOperationError()
338 // don't do any operation on other items anymore
339 m_contextItems
.clear();
340 m_pendingOperation
= false;
342 emit
errorMessage(m_errorMsg
);
345 void FileViewSvnPlugin::execSvnCommand(const QString
& svnCommand
,
346 const QString
& infoMsg
,
347 const QString
& errorMsg
,
348 const QString
& operationCompletedMsg
)
350 emit
infoMessage(infoMsg
);
352 m_command
= svnCommand
;
353 m_errorMsg
= errorMsg
;
354 m_operationCompletedMsg
= operationCompletedMsg
;
356 startSvnCommandProcess();
359 void FileViewSvnPlugin::startSvnCommandProcess()
361 m_pendingOperation
= true;
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()));
369 const QString program
= "svn " + m_command
+ ' ';
370 if (!m_contextDir
.isEmpty()) {
371 process
->start(program
+ KShell::quoteArg(m_contextDir
));
372 m_contextDir
.clear();
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())