]>
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
),
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
106 QMutableHashIterator
<QString
, VersionState
> it(m_versionInfoHash
);
107 while (it
.hasNext()) {
109 if (it
.key().startsWith(directory
)) {
114 QStringList arguments
;
115 arguments
<< "status" << "--show-updates" << directory
;
118 process
.start("svn", arguments
);
119 while (process
.waitForReadyRead()) {
121 while (process
.readLine(buffer
, sizeof(buffer
)) > 0) {
122 VersionState state
= NormalVersion
;
123 QString
filePath(buffer
);
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;
132 if (filePath
.contains('*')) {
133 state
= UpdateRequiredVersion
;
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
);
147 m_versionInfoKeys
= m_versionInfoHash
.keys();
151 void FileViewSvnPlugin::endRetrieval()
155 KVersionControlPlugin::VersionState
FileViewSvnPlugin::versionState(const KFileItem
& item
)
157 const QString itemUrl
= item
.localPath();
158 if (m_versionInfoHash
.contains(itemUrl
)) {
159 return m_versionInfoHash
.value(itemUrl
);
163 // files that have not been listed by 'svn status' (= m_versionInfoHash)
164 // are under version control per definition
165 return NormalVersion
;
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
;
180 return NormalVersion
;
183 QList
<QAction
*> FileViewSvnPlugin::contextMenuActions(const KFileItemList
& items
)
185 Q_ASSERT(!items
.isEmpty());
186 foreach (const KFileItem
& item
, items
) {
187 m_contextItems
.append(item
);
189 m_contextDir
.clear();
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
) {
203 case LocallyModifiedVersion
:
204 case ConflictingVersion
:
211 m_commitAction
->setEnabled(editingCount
> 0);
212 m_addAction
->setEnabled(versionedCount
== 0);
213 m_removeAction
->setEnabled(versionedCount
== itemsCount
);
215 QList
<QAction
*> actions
;
216 actions
.append(m_updateAction
);
217 actions
.append(m_commitAction
);
218 actions
.append(m_addAction
);
219 actions
.append(m_removeAction
);
223 QList
<QAction
*> FileViewSvnPlugin::contextMenuActions(const QString
& directory
)
225 const bool enabled
= m_contextItems
.isEmpty();
227 m_contextDir
= directory
;
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
);
237 QList
<QAction
*> actions
;
238 actions
.append(m_updateAction
);
239 actions
.append(m_showLocalChangesAction
);
240 actions
.append(m_commitAction
);
244 void FileViewSvnPlugin::updateFiles()
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."));
252 void FileViewSvnPlugin::showLocalChanges()
254 Q_ASSERT(!m_contextDir
.isEmpty());
255 Q_ASSERT(m_contextItems
.isEmpty());
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);
263 void FileViewSvnPlugin::commitFiles()
265 KDialog
dialog(0, Qt::Dialog
);
267 KVBox
* box
= new KVBox(&dialog
);
268 new QLabel(i18nc("@label", "Description:"), box
);
269 QTextEdit
* editor
= new QTextEdit(box
);
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"));
277 KConfigGroup
dialogConfig(KSharedConfig::openConfig("dolphinrc"),
279 dialog
.restoreDialogSize(dialogConfig
);
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."));
291 QTextStream
out(&m_tempFile
);
292 const QString fileName
= m_tempFile
.fileName();
293 out
<< editor
->toPlainText();
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."));
302 dialog
.saveDialogSize(dialogConfig
, KConfigBase::Persistent
);
305 void FileViewSvnPlugin::addFiles()
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."));
313 void FileViewSvnPlugin::removeFiles()
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."));
321 void FileViewSvnPlugin::slotOperationCompleted(int exitCode
, QProcess::ExitStatus exitStatus
)
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();
329 startSvnCommandProcess();
333 void FileViewSvnPlugin::slotOperationError()
335 emit
errorMessage(m_errorMsg
);
337 // don't do any operation on other items anymore
338 m_contextItems
.clear();
341 void FileViewSvnPlugin::execSvnCommand(const QString
& svnCommand
,
342 const QString
& infoMsg
,
343 const QString
& errorMsg
,
344 const QString
& operationCompletedMsg
)
346 emit
infoMessage(infoMsg
);
348 m_command
= svnCommand
;
349 m_errorMsg
= errorMsg
;
350 m_operationCompletedMsg
= operationCompletedMsg
;
352 startSvnCommandProcess();
355 void FileViewSvnPlugin::startSvnCommandProcess()
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()));
363 const QString program
= "svn " + m_command
+ ' ';
364 if (!m_contextDir
.isEmpty()) {
365 process
->start(program
+ KShell::quoteArg(m_contextDir
));
366 m_contextDir
.clear();
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())