]>
cloud.milkyroute.net Git - dolphin.git/blob - src/revisioncontrolplugin.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 "revisioncontrolplugin.h"
22 RevisionControlPlugin::RevisionControlPlugin()
26 RevisionControlPlugin::~RevisionControlPlugin()
30 #include "revisioncontrolplugin.moc"
32 // ----------------------------------------------------------------------------
36 #include <kfileitem.h>
46 #include <QStringList>
48 #include <QTextStream>
50 SubversionPlugin::SubversionPlugin() :
54 m_showLocalChangesAction(0),
60 m_operationCompletedMsg(),
64 m_updateAction
= new KAction(this);
65 m_updateAction
->setIcon(KIcon("view-refresh"));
66 m_updateAction
->setText(i18nc("@item:inmenu", "SVN Update"));
67 connect(m_updateAction
, SIGNAL(triggered()),
68 this, SLOT(updateFiles()));
70 m_showLocalChangesAction
= new KAction(this);
71 m_showLocalChangesAction
->setIcon(KIcon("view-split-left-right"));
72 m_showLocalChangesAction
->setText(i18nc("@item:inmenu", "Show Local SVN Changes"));
73 connect(m_showLocalChangesAction
, SIGNAL(triggered()),
74 this, SLOT(showLocalChanges()));
76 m_commitAction
= new KAction(this);
77 m_commitAction
->setText(i18nc("@item:inmenu", "SVN Commit..."));
78 connect(m_commitAction
, SIGNAL(triggered()),
79 this, SLOT(commitFiles()));
81 m_addAction
= new KAction(this);
82 m_addAction
->setIcon(KIcon("list-add"));
83 m_addAction
->setText(i18nc("@item:inmenu", "SVN Add"));
84 connect(m_addAction
, SIGNAL(triggered()),
85 this, SLOT(addFiles()));
87 m_removeAction
= new KAction(this);
88 m_removeAction
->setIcon(KIcon("list-remove"));
89 m_removeAction
->setText(i18nc("@item:inmenu", "SVN Delete"));
90 connect(m_removeAction
, SIGNAL(triggered()),
91 this, SLOT(removeFiles()));
94 SubversionPlugin::~SubversionPlugin()
98 QString
SubversionPlugin::fileName() const
103 bool SubversionPlugin::beginRetrieval(const QString
& directory
)
105 Q_ASSERT(directory
.endsWith('/'));
107 QStringList arguments
;
108 arguments
<< "status" << "--show-updates" << directory
;
111 process
.start("svn", arguments
);
112 while (process
.waitForReadyRead()) {
114 while (process
.readLine(buffer
, sizeof(buffer
)) > 0) {
115 RevisionState state
= NormalRevision
;
116 QString
filePath(buffer
);
119 case '?': state
= UnversionedRevision
; break;
120 case 'M': state
= LocallyModifiedRevision
; break;
121 case 'A': state
= AddedRevision
; break;
122 case 'D': state
= RemovedRevision
; break;
123 case 'C': state
= ConflictingRevision
; break;
125 if (filePath
.contains('*')) {
126 state
= UpdateRequiredRevision
;
131 int pos
= filePath
.indexOf('/');
132 const int length
= filePath
.length() - pos
- 1;
133 filePath
= filePath
.mid(pos
, length
);
134 if (!filePath
.isEmpty()) {
135 m_revisionInfoHash
.insert(filePath
, state
);
140 m_revisionInfoKeys
= m_revisionInfoHash
.keys();
144 void SubversionPlugin::endRetrieval()
148 RevisionControlPlugin::RevisionState
SubversionPlugin::revisionState(const KFileItem
& item
)
150 const QString itemUrl
= item
.localPath();
151 if (m_revisionInfoHash
.contains(itemUrl
)) {
152 return m_revisionInfoHash
.value(itemUrl
);
156 // files that have not been listed by 'svn status' (= m_revisionInfoHash)
157 // are under revision control per definition
158 return NormalRevision
;
161 // The item is a directory. Check whether an item listed by 'svn status' (= m_revisionInfoHash)
162 // is part of this directory. In this case a local modification should be indicated in the
163 // directory already.
164 foreach (const QString
& key
, m_revisionInfoKeys
) {
165 if (key
.startsWith(itemUrl
)) {
166 const RevisionState state
= m_revisionInfoHash
.value(key
);
167 if (state
== LocallyModifiedRevision
) {
168 return LocallyModifiedRevision
;
173 return NormalRevision
;
176 QList
<QAction
*> SubversionPlugin::contextMenuActions(const KFileItemList
& items
)
178 Q_ASSERT(!items
.isEmpty());
179 foreach (const KFileItem
& item
, items
) {
180 m_contextItems
.append(item
);
182 m_contextDir
.clear();
184 // iterate all items and check the revision state to know which
185 // actions can be enabled
186 const int itemsCount
= items
.count();
187 int revisionedCount
= 0;
188 int editingCount
= 0;
189 foreach (const KFileItem
& item
, items
) {
190 const RevisionState state
= revisionState(item
);
191 if (state
!= UnversionedRevision
) {
196 case LocallyModifiedRevision
:
197 case ConflictingRevision
:
204 m_commitAction
->setEnabled(editingCount
> 0);
205 m_addAction
->setEnabled(revisionedCount
== 0);
206 m_removeAction
->setEnabled(revisionedCount
== itemsCount
);
208 QList
<QAction
*> actions
;
209 actions
.append(m_updateAction
);
210 actions
.append(m_commitAction
);
211 actions
.append(m_addAction
);
212 actions
.append(m_removeAction
);
216 QList
<QAction
*> SubversionPlugin::contextMenuActions(const QString
& directory
)
218 const bool enabled
= m_contextItems
.isEmpty();
220 m_contextDir
= directory
;
223 // Only enable the SVN actions if no SVN commands are
224 // executed currently (see slotOperationCompleted() and
225 // startSvnCommandProcess()).
226 m_updateAction
->setEnabled(enabled
);
227 m_showLocalChangesAction
->setEnabled(enabled
);
228 m_commitAction
->setEnabled(enabled
);
230 QList
<QAction
*> actions
;
231 actions
.append(m_updateAction
);
232 actions
.append(m_showLocalChangesAction
);
233 actions
.append(m_commitAction
);
237 void SubversionPlugin::updateFiles()
239 execSvnCommand("update",
240 i18nc("@info:status", "Updating SVN repository..."),
241 i18nc("@info:status", "Update of SVN repository failed."),
242 i18nc("@info:status", "Updated SVN repository."));
245 void SubversionPlugin::showLocalChanges()
247 Q_ASSERT(!m_contextDir
.isEmpty());
248 Q_ASSERT(m_contextItems
.isEmpty());
250 const QString command
= "mkfifo /tmp/fifo; svn diff " +
251 KShell::quoteArg(m_contextDir
) +
252 " > /tmp/fifo & kompare /tmp/fifo; rm /tmp/fifo";
253 KRun::runCommand(command
, 0);
256 void SubversionPlugin::commitFiles()
258 KDialog
dialog(0, Qt::Dialog
);
260 KVBox
* box
= new KVBox(&dialog
);
261 new QLabel(i18nc("@label", "Description:"), box
);
262 QTextEdit
* editor
= new QTextEdit(box
);
264 dialog
.setMainWidget(box
);
265 dialog
.setCaption(i18nc("@title:window", "SVN Commit"));
266 dialog
.setButtons(KDialog::Ok
| KDialog::Cancel
);
267 dialog
.setDefaultButton(KDialog::Ok
);
268 dialog
.setButtonText(KDialog::Ok
, i18nc("@action:button", "Commit"));
270 KConfigGroup
dialogConfig(KSharedConfig::openConfig("dolphinrc"),
272 dialog
.restoreDialogSize(dialogConfig
);
274 if (dialog
.exec() == QDialog::Accepted
) {
275 const QString description
= editor
->toPlainText();
276 execSvnCommand("commit -m " + KShell::quoteArg(description
),
277 i18nc("@info:status", "Committing SVN changes..."),
278 i18nc("@info:status", "Commit of SVN changes failed."),
279 i18nc("@info:status", "Committed SVN changes."));
282 dialog
.saveDialogSize(dialogConfig
, KConfigBase::Persistent
);
285 void SubversionPlugin::addFiles()
287 execSvnCommand("add",
288 i18nc("@info:status", "Adding files to SVN repository..."),
289 i18nc("@info:status", "Adding of files to SVN repository failed."),
290 i18nc("@info:status", "Added files to SVN repository."));
293 void SubversionPlugin::removeFiles()
295 execSvnCommand("remove",
296 i18nc("@info:status", "Removing files from SVN repository..."),
297 i18nc("@info:status", "Removing of files from SVN repository failed."),
298 i18nc("@info:status", "Removed files from SVN repository."));
301 void SubversionPlugin::slotOperationCompleted()
303 if (m_contextItems
.isEmpty()) {
304 emit
operationCompletedMessage(m_operationCompletedMsg
);
305 emit
revisionStatesChanged();
307 startSvnCommandProcess();
311 void SubversionPlugin::slotOperationError()
313 emit
errorMessage(m_errorMsg
);
315 // don't do any operation on other items anymore
316 m_contextItems
.clear();
319 void SubversionPlugin::execSvnCommand(const QString
& svnCommand
,
320 const QString
& infoMsg
,
321 const QString
& errorMsg
,
322 const QString
& operationCompletedMsg
)
324 emit
infoMessage(infoMsg
);
326 m_command
= svnCommand
;
327 m_errorMsg
= errorMsg
;
328 m_operationCompletedMsg
= operationCompletedMsg
;
330 startSvnCommandProcess();
333 void SubversionPlugin::startSvnCommandProcess()
335 QProcess
* process
= new QProcess(this);
336 connect(process
, SIGNAL(finished(int)),
337 this, SLOT(slotOperationCompleted()));
338 connect(process
, SIGNAL(error(QProcess::ProcessError
)),
339 this, SLOT(slotOperationError()));
341 QStringList arguments
;
342 arguments
<< m_command
;
343 if (!m_contextDir
.isEmpty()) {
344 process
->start("svn", arguments
<< KShell::quoteArg(m_contextDir
));
345 m_contextDir
.clear();
347 const KFileItem item
= m_contextItems
.takeLast();
348 process
->start("svn", arguments
<< KShell::quoteArg(item
.localPath()));
349 // the remaining items of m_contextItems will be executed
350 // after the process has finished (see slotOperationFinished())