]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kversioncontrolplugin.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 "kversioncontrolplugin.h"
22 KVersionControlPlugin::KVersionControlPlugin()
26 KVersionControlPlugin::~KVersionControlPlugin()
30 #include "kversioncontrolplugin.moc"
32 // ----------------------------------------------------------------------------
36 #include <kfileitem.h>
46 #include <QStringList>
48 #include <QTextStream>
50 SubversionPlugin::SubversionPlugin() :
54 m_showLocalChangesAction(0),
60 m_operationCompletedMsg(),
65 m_updateAction
= new KAction(this);
66 m_updateAction
->setIcon(KIcon("view-refresh"));
67 m_updateAction
->setText(i18nc("@item:inmenu", "SVN Update"));
68 connect(m_updateAction
, SIGNAL(triggered()),
69 this, SLOT(updateFiles()));
71 m_showLocalChangesAction
= new KAction(this);
72 m_showLocalChangesAction
->setIcon(KIcon("view-split-left-right"));
73 m_showLocalChangesAction
->setText(i18nc("@item:inmenu", "Show Local SVN Changes"));
74 connect(m_showLocalChangesAction
, SIGNAL(triggered()),
75 this, SLOT(showLocalChanges()));
77 m_commitAction
= new KAction(this);
78 m_commitAction
->setText(i18nc("@item:inmenu", "SVN Commit..."));
79 connect(m_commitAction
, SIGNAL(triggered()),
80 this, SLOT(commitFiles()));
82 m_addAction
= new KAction(this);
83 m_addAction
->setIcon(KIcon("list-add"));
84 m_addAction
->setText(i18nc("@item:inmenu", "SVN Add"));
85 connect(m_addAction
, SIGNAL(triggered()),
86 this, SLOT(addFiles()));
88 m_removeAction
= new KAction(this);
89 m_removeAction
->setIcon(KIcon("list-remove"));
90 m_removeAction
->setText(i18nc("@item:inmenu", "SVN Delete"));
91 connect(m_removeAction
, SIGNAL(triggered()),
92 this, SLOT(removeFiles()));
95 SubversionPlugin::~SubversionPlugin()
99 QString
SubversionPlugin::fileName() const
104 bool SubversionPlugin::beginRetrieval(const QString
& directory
)
106 Q_ASSERT(directory
.endsWith('/'));
108 QStringList arguments
;
109 arguments
<< "status" << "--show-updates" << directory
;
112 process
.start("svn", arguments
);
113 while (process
.waitForReadyRead()) {
115 while (process
.readLine(buffer
, sizeof(buffer
)) > 0) {
116 VersionState state
= NormalVersion
;
117 QString
filePath(buffer
);
120 case '?': state
= UnversionedVersion
; break;
121 case 'M': state
= LocallyModifiedVersion
; break;
122 case 'A': state
= AddedVersion
; break;
123 case 'D': state
= RemovedVersion
; break;
124 case 'C': state
= ConflictingVersion
; break;
126 if (filePath
.contains('*')) {
127 state
= UpdateRequiredVersion
;
132 int pos
= filePath
.indexOf('/');
133 const int length
= filePath
.length() - pos
- 1;
134 filePath
= filePath
.mid(pos
, length
);
135 if (!filePath
.isEmpty()) {
136 m_versionInfoHash
.insert(filePath
, state
);
141 m_versionInfoKeys
= m_versionInfoHash
.keys();
145 void SubversionPlugin::endRetrieval()
149 KVersionControlPlugin::VersionState
SubversionPlugin::versionState(const KFileItem
& item
)
151 const QString itemUrl
= item
.localPath();
152 if (m_versionInfoHash
.contains(itemUrl
)) {
153 return m_versionInfoHash
.value(itemUrl
);
157 // files that have not been listed by 'svn status' (= m_versionInfoHash)
158 // are under version control per definition
159 return NormalVersion
;
162 // The item is a directory. Check whether an item listed by 'svn status' (= m_versionInfoHash)
163 // is part of this directory. In this case a local modification should be indicated in the
164 // directory already.
165 foreach (const QString
& key
, m_versionInfoKeys
) {
166 if (key
.startsWith(itemUrl
)) {
167 const VersionState state
= m_versionInfoHash
.value(key
);
168 if (state
== LocallyModifiedVersion
) {
169 return LocallyModifiedVersion
;
174 return NormalVersion
;
177 QList
<QAction
*> SubversionPlugin::contextMenuActions(const KFileItemList
& items
)
179 Q_ASSERT(!items
.isEmpty());
180 foreach (const KFileItem
& item
, items
) {
181 m_contextItems
.append(item
);
183 m_contextDir
.clear();
185 // iterate all items and check the version state to know which
186 // actions can be enabled
187 const int itemsCount
= items
.count();
188 int versionedCount
= 0;
189 int editingCount
= 0;
190 foreach (const KFileItem
& item
, items
) {
191 const VersionState state
= versionState(item
);
192 if (state
!= UnversionedVersion
) {
197 case LocallyModifiedVersion
:
198 case ConflictingVersion
:
205 m_commitAction
->setEnabled(editingCount
> 0);
206 m_addAction
->setEnabled(versionedCount
== 0);
207 m_removeAction
->setEnabled(versionedCount
== itemsCount
);
209 QList
<QAction
*> actions
;
210 actions
.append(m_updateAction
);
211 actions
.append(m_commitAction
);
212 actions
.append(m_addAction
);
213 actions
.append(m_removeAction
);
217 QList
<QAction
*> SubversionPlugin::contextMenuActions(const QString
& directory
)
219 const bool enabled
= m_contextItems
.isEmpty();
221 m_contextDir
= directory
;
224 // Only enable the SVN actions if no SVN commands are
225 // executed currently (see slotOperationCompleted() and
226 // startSvnCommandProcess()).
227 m_updateAction
->setEnabled(enabled
);
228 m_showLocalChangesAction
->setEnabled(enabled
);
229 m_commitAction
->setEnabled(enabled
);
231 QList
<QAction
*> actions
;
232 actions
.append(m_updateAction
);
233 actions
.append(m_showLocalChangesAction
);
234 actions
.append(m_commitAction
);
238 void SubversionPlugin::updateFiles()
240 execSvnCommand("update",
241 i18nc("@info:status", "Updating SVN repository..."),
242 i18nc("@info:status", "Update of SVN repository failed."),
243 i18nc("@info:status", "Updated SVN repository."));
246 void SubversionPlugin::showLocalChanges()
248 Q_ASSERT(!m_contextDir
.isEmpty());
249 Q_ASSERT(m_contextItems
.isEmpty());
251 const QString command
= "mkfifo /tmp/fifo; svn diff " +
252 KShell::quoteArg(m_contextDir
) +
253 " > /tmp/fifo & kompare /tmp/fifo; rm /tmp/fifo";
254 KRun::runCommand(command
, 0);
257 void SubversionPlugin::commitFiles()
259 KDialog
dialog(0, Qt::Dialog
);
261 KVBox
* box
= new KVBox(&dialog
);
262 new QLabel(i18nc("@label", "Description:"), box
);
263 QTextEdit
* editor
= new QTextEdit(box
);
265 dialog
.setMainWidget(box
);
266 dialog
.setCaption(i18nc("@title:window", "SVN Commit"));
267 dialog
.setButtons(KDialog::Ok
| KDialog::Cancel
);
268 dialog
.setDefaultButton(KDialog::Ok
);
269 dialog
.setButtonText(KDialog::Ok
, i18nc("@action:button", "Commit"));
271 KConfigGroup
dialogConfig(KSharedConfig::openConfig("dolphinrc"),
273 dialog
.restoreDialogSize(dialogConfig
);
275 if (dialog
.exec() == QDialog::Accepted
) {
276 // Write the commit description into a temporary file, so
277 // that it can be read by the command "svn commit -F". The temporary
278 // file must stay alive until slotOperationCompleted() is invoked and will
279 // be destroyed when the version plugin is destructed.
280 if (!m_tempFile
.open()) {
281 emit
errorMessage(i18nc("@info:status", "Commit of SVN changes failed."));
285 QTextStream
out(&m_tempFile
);
286 const QString fileName
= m_tempFile
.fileName();
287 out
<< editor
->toPlainText();
290 execSvnCommand("commit -F " + KShell::quoteArg(fileName
),
291 i18nc("@info:status", "Committing SVN changes..."),
292 i18nc("@info:status", "Commit of SVN changes failed."),
293 i18nc("@info:status", "Committed SVN changes."));
296 dialog
.saveDialogSize(dialogConfig
, KConfigBase::Persistent
);
299 void SubversionPlugin::addFiles()
301 execSvnCommand("add",
302 i18nc("@info:status", "Adding files to SVN repository..."),
303 i18nc("@info:status", "Adding of files to SVN repository failed."),
304 i18nc("@info:status", "Added files to SVN repository."));
307 void SubversionPlugin::removeFiles()
309 execSvnCommand("remove",
310 i18nc("@info:status", "Removing files from SVN repository..."),
311 i18nc("@info:status", "Removing of files from SVN repository failed."),
312 i18nc("@info:status", "Removed files from SVN repository."));
315 void SubversionPlugin::slotOperationCompleted()
317 if (m_contextItems
.isEmpty()) {
318 emit
operationCompletedMessage(m_operationCompletedMsg
);
319 emit
versionStatesChanged();
321 startSvnCommandProcess();
325 void SubversionPlugin::slotOperationError()
327 emit
errorMessage(m_errorMsg
);
329 // don't do any operation on other items anymore
330 m_contextItems
.clear();
333 void SubversionPlugin::execSvnCommand(const QString
& svnCommand
,
334 const QString
& infoMsg
,
335 const QString
& errorMsg
,
336 const QString
& operationCompletedMsg
)
338 emit
infoMessage(infoMsg
);
340 m_command
= svnCommand
;
341 m_errorMsg
= errorMsg
;
342 m_operationCompletedMsg
= operationCompletedMsg
;
344 startSvnCommandProcess();
347 void SubversionPlugin::startSvnCommandProcess()
349 QProcess
* process
= new QProcess(this);
350 connect(process
, SIGNAL(finished(int)),
351 this, SLOT(slotOperationCompleted()));
352 connect(process
, SIGNAL(error(QProcess::ProcessError
)),
353 this, SLOT(slotOperationError()));
355 const QString program
= "svn " + m_command
+ ' ';
356 if (!m_contextDir
.isEmpty()) {
357 process
->start(program
+ KShell::quoteArg(m_contextDir
));
358 m_contextDir
.clear();
360 const KFileItem item
= m_contextItems
.takeLast();
361 process
->start(program
+ KShell::quoteArg(item
.localPath()));
362 // the remaining items of m_contextItems will be executed
363 // after the process has finished (see slotOperationFinished())