]>
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),
61 m_updateAction
= new KAction(this);
62 m_updateAction
->setIcon(KIcon("view-refresh"));
63 m_updateAction
->setText(i18nc("@item:inmenu", "SVN Update"));
64 connect(m_updateAction
, SIGNAL(triggered()),
65 this, SLOT(updateFiles()));
67 m_showLocalChangesAction
= new KAction(this);
68 m_showLocalChangesAction
->setIcon(KIcon("view-split-left-right"));
69 m_showLocalChangesAction
->setText(i18nc("@item:inmenu", "Show Local SVN Changes"));
70 connect(m_showLocalChangesAction
, SIGNAL(triggered()),
71 this, SLOT(showLocalChanges()));
73 m_commitAction
= new KAction(this);
74 m_commitAction
->setText(i18nc("@item:inmenu", "SVN Commit..."));
75 connect(m_commitAction
, SIGNAL(triggered()),
76 this, SLOT(commitFiles()));
78 m_addAction
= new KAction(this);
79 m_addAction
->setIcon(KIcon("list-add"));
80 m_addAction
->setText(i18nc("@item:inmenu", "SVN Add"));
81 connect(m_addAction
, SIGNAL(triggered()),
82 this, SLOT(addFiles()));
84 m_removeAction
= new KAction(this);
85 m_removeAction
->setIcon(KIcon("list-remove"));
86 m_removeAction
->setText(i18nc("@item:inmenu", "SVN Delete"));
87 connect(m_removeAction
, SIGNAL(triggered()),
88 this, SLOT(removeFiles()));
91 SubversionPlugin::~SubversionPlugin()
95 QString
SubversionPlugin::fileName() const
100 bool SubversionPlugin::beginRetrieval(const QString
& directory
)
102 Q_ASSERT(directory
.endsWith('/'));
104 QStringList arguments
;
105 arguments
<< "status" << directory
;
108 process
.start("svn", arguments
);
109 if (!process
.waitForReadyRead()) {
114 while (process
.readLine(buffer
, sizeof(buffer
)) > 0) {
115 RevisionState state
= NormalRevision
;
118 case '?': state
= UnversionedRevision
; break;
119 case 'M': state
= LocallyModifiedRevision
; break;
120 case 'A': state
= AddedRevision
; break;
121 case 'D': state
= RemovedRevision
; break;
122 case 'C': state
= ConflictingRevision
; break;
126 QString
filePath(buffer
);
127 int pos
= filePath
.indexOf('/');
128 const int length
= filePath
.length() - pos
- 1;
129 filePath
= filePath
.mid(pos
, length
);
130 if (!filePath
.isEmpty()) {
131 m_revisionInfoHash
.insert(filePath
, state
);
134 m_revisionInfoKeys
= m_revisionInfoHash
.keys();
138 void SubversionPlugin::endRetrieval()
142 RevisionControlPlugin::RevisionState
SubversionPlugin::revisionState(const KFileItem
& item
)
144 const QString itemUrl
= item
.localPath();
145 if (m_revisionInfoHash
.contains(itemUrl
)) {
146 return m_revisionInfoHash
.value(itemUrl
);
150 // files that have not been listed by 'svn status' (= m_revisionInfoHash)
151 // are under revision control per definition
152 return NormalRevision
;
155 // The item is a directory. Check whether an item listed by 'svn status' (= m_revisionInfoHash)
156 // is part of this directory. In this case a local modification should be indicated in the
157 // directory already.
158 foreach (const QString
& key
, m_revisionInfoKeys
) {
159 if (key
.startsWith(itemUrl
)) {
160 const RevisionState state
= m_revisionInfoHash
.value(key
);
161 if (state
== LocallyModifiedRevision
) {
162 return LocallyModifiedRevision
;
167 return NormalRevision
;
170 QList
<QAction
*> SubversionPlugin::contextMenuActions(const KFileItemList
& items
)
172 Q_ASSERT(!items
.isEmpty());
174 m_contextItems
= items
;
175 m_contextDir
.clear();
177 // iterate all items and check the revision state to know which
178 // actions can be enabled
179 const int itemsCount
= items
.count();
180 int revisionedCount
= 0;
181 int editingCount
= 0;
182 foreach (const KFileItem
& item
, items
) {
183 const RevisionState state
= revisionState(item
);
184 if (state
!= UnversionedRevision
) {
189 case LocallyModifiedRevision
:
190 case ConflictingRevision
:
197 m_commitAction
->setEnabled(editingCount
> 0);
198 m_addAction
->setEnabled(revisionedCount
== 0);
199 m_removeAction
->setEnabled(revisionedCount
== itemsCount
);
201 QList
<QAction
*> actions
;
202 actions
.append(m_updateAction
);
203 actions
.append(m_commitAction
);
204 actions
.append(m_addAction
);
205 actions
.append(m_removeAction
);
209 QList
<QAction
*> SubversionPlugin::contextMenuActions(const QString
& directory
)
211 m_contextDir
= directory
;
212 m_contextItems
.clear();
214 QList
<QAction
*> actions
;
215 actions
.append(m_updateAction
);
216 actions
.append(m_showLocalChangesAction
);
217 actions
.append(m_commitAction
);
221 void SubversionPlugin::updateFiles()
223 execSvnCommand("update");
226 void SubversionPlugin::showLocalChanges()
228 Q_ASSERT(!m_contextDir
.isEmpty());
229 Q_ASSERT(m_contextItems
.isEmpty());
231 const QString command
= "mkfifo /tmp/fifo; svn diff " +
232 KShell::quoteArg(m_contextDir
) +
233 " > /tmp/fifo & kompare /tmp/fifo; rm /tmp/fifo";
234 KRun::runCommand(command
, 0);
237 void SubversionPlugin::commitFiles()
239 KDialog
dialog(0, Qt::Dialog
);
241 KVBox
* box
= new KVBox(&dialog
);
242 new QLabel(i18nc("@label", "Description:"), box
);
243 QTextEdit
* editor
= new QTextEdit(box
);
245 dialog
.setMainWidget(box
);
246 dialog
.setCaption(i18nc("@title:window", "SVN Commit"));
247 dialog
.setButtons(KDialog::Ok
| KDialog::Cancel
);
248 dialog
.setDefaultButton(KDialog::Ok
);
249 dialog
.setButtonText(KDialog::Ok
, i18nc("@action:button", "Commit"));
251 KConfigGroup
dialogConfig(KSharedConfig::openConfig("dolphinrc"),
253 dialog
.restoreDialogSize(dialogConfig
);
255 if (dialog
.exec() == QDialog::Accepted
) {
256 const QString description
= editor
->toPlainText();
257 execSvnCommand("commit -m " + KShell::quoteArg(description
));
260 dialog
.saveDialogSize(dialogConfig
, KConfigBase::Persistent
);
263 void SubversionPlugin::addFiles()
265 execSvnCommand("add");
268 void SubversionPlugin::removeFiles()
270 execSvnCommand("remove");
273 void SubversionPlugin::execSvnCommand(const QString
& svnCommand
)
275 const QString command
= "svn " + svnCommand
+ ' ';
276 if (!m_contextDir
.isEmpty()) {
277 KRun::runCommand(command
+ KShell::quoteArg(m_contextDir
), 0);
279 foreach (const KFileItem
& item
, m_contextItems
) {
280 KRun::runCommand(command
+ KShell::quoteArg(item
.localPath()), 0);