]>
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),
49 m_showLocalChangesAction(0),
55 m_operationCompletedMsg(),
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()));
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()));
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()));
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()));
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()));
92 FileViewSvnPlugin::~FileViewSvnPlugin()
96 QString
FileViewSvnPlugin::fileName() const
101 bool FileViewSvnPlugin::beginRetrieval(const QString
& directory
)
103 Q_ASSERT(directory
.endsWith('/'));
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()) {
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 // 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
);
157 void FileViewSvnPlugin::endRetrieval()
161 KVersionControlPlugin::VersionState
FileViewSvnPlugin::versionState(const KFileItem
& item
)
163 const QString itemUrl
= item
.localPath();
164 if (m_versionInfoHash
.contains(itemUrl
)) {
165 return m_versionInfoHash
.value(itemUrl
);
169 // files that have not been listed by 'svn status' (= m_versionInfoHash)
170 // are under version control per definition
171 return NormalVersion
;
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
;
188 return NormalVersion
;
191 QList
<QAction
*> FileViewSvnPlugin::contextMenuActions(const KFileItemList
& items
)
193 Q_ASSERT(!items
.isEmpty());
194 foreach (const KFileItem
& item
, items
) {
195 m_contextItems
.append(item
);
197 m_contextDir
.clear();
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
) {
211 case LocallyModifiedVersion
:
212 case ConflictingVersion
:
219 m_commitAction
->setEnabled(editingCount
> 0);
220 m_addAction
->setEnabled(versionedCount
== 0);
221 m_removeAction
->setEnabled(versionedCount
== itemsCount
);
223 QList
<QAction
*> actions
;
224 actions
.append(m_updateAction
);
225 actions
.append(m_commitAction
);
226 actions
.append(m_addAction
);
227 actions
.append(m_removeAction
);
231 QList
<QAction
*> FileViewSvnPlugin::contextMenuActions(const QString
& directory
)
233 const bool enabled
= !m_pendingOperation
;
235 m_contextDir
= directory
;
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
);
245 QList
<QAction
*> actions
;
246 actions
.append(m_updateAction
);
247 actions
.append(m_showLocalChangesAction
);
248 actions
.append(m_commitAction
);
252 void FileViewSvnPlugin::updateFiles()
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."));
260 void FileViewSvnPlugin::showLocalChanges()
262 Q_ASSERT(!m_contextDir
.isEmpty());
263 Q_ASSERT(m_contextItems
.isEmpty());
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);
271 void FileViewSvnPlugin::commitFiles()
273 KDialog
dialog(0, Qt::Dialog
);
275 KVBox
* box
= new KVBox(&dialog
);
276 new QLabel(i18nc("@label", "Description:"), box
);
277 QTextEdit
* editor
= new QTextEdit(box
);
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"));
285 KConfigGroup
dialogConfig(KSharedConfig::openConfig("dolphinrc"),
287 dialog
.restoreDialogSize(dialogConfig
);
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."));
299 QTextStream
out(&m_tempFile
);
300 const QString fileName
= m_tempFile
.fileName();
301 out
<< editor
->toPlainText();
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."));
310 dialog
.saveDialogSize(dialogConfig
, KConfigBase::Persistent
);
313 void FileViewSvnPlugin::addFiles()
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."));
321 void FileViewSvnPlugin::removeFiles()
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."));
329 void FileViewSvnPlugin::slotOperationCompleted(int exitCode
, QProcess::ExitStatus exitStatus
)
331 m_pendingOperation
= false;
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();
339 startSvnCommandProcess();
343 void FileViewSvnPlugin::slotOperationError()
345 // don't do any operation on other items anymore
346 m_contextItems
.clear();
347 m_pendingOperation
= false;
349 emit
errorMessage(m_errorMsg
);
352 void FileViewSvnPlugin::execSvnCommand(const QString
& svnCommand
,
353 const QString
& infoMsg
,
354 const QString
& errorMsg
,
355 const QString
& operationCompletedMsg
)
357 emit
infoMessage(infoMsg
);
359 m_command
= svnCommand
;
360 m_errorMsg
= errorMsg
;
361 m_operationCompletedMsg
= operationCompletedMsg
;
363 startSvnCommandProcess();
366 void FileViewSvnPlugin::startSvnCommandProcess()
368 m_pendingOperation
= true;
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()));
376 const QString program
= "svn " + m_command
+ ' ';
377 if (!m_contextDir
.isEmpty()) {
378 process
->start(program
+ KShell::quoteArg(m_contextDir
));
379 m_contextDir
.clear();
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())