]>
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" << "--show-updates" << directory
;
108 process
.start("svn", arguments
);
109 while (process
.waitForReadyRead()) {
111 while (process
.readLine(buffer
, sizeof(buffer
)) > 0) {
112 RevisionState state
= NormalRevision
;
113 QString
filePath(buffer
);
116 case '?': state
= UnversionedRevision
; break;
117 case 'M': state
= LocallyModifiedRevision
; break;
118 case 'A': state
= AddedRevision
; break;
119 case 'D': state
= RemovedRevision
; break;
120 case 'C': state
= ConflictingRevision
; break;
122 if (filePath
.contains('*')) {
123 state
= UpdateRequiredRevision
;
128 int pos
= filePath
.indexOf('/');
129 const int length
= filePath
.length() - pos
- 1;
130 filePath
= filePath
.mid(pos
, length
);
131 if (!filePath
.isEmpty()) {
132 m_revisionInfoHash
.insert(filePath
, state
);
137 m_revisionInfoKeys
= m_revisionInfoHash
.keys();
141 void SubversionPlugin::endRetrieval()
145 RevisionControlPlugin::RevisionState
SubversionPlugin::revisionState(const KFileItem
& item
)
147 const QString itemUrl
= item
.localPath();
148 if (m_revisionInfoHash
.contains(itemUrl
)) {
149 return m_revisionInfoHash
.value(itemUrl
);
153 // files that have not been listed by 'svn status' (= m_revisionInfoHash)
154 // are under revision control per definition
155 return NormalRevision
;
158 // The item is a directory. Check whether an item listed by 'svn status' (= m_revisionInfoHash)
159 // is part of this directory. In this case a local modification should be indicated in the
160 // directory already.
161 foreach (const QString
& key
, m_revisionInfoKeys
) {
162 if (key
.startsWith(itemUrl
)) {
163 const RevisionState state
= m_revisionInfoHash
.value(key
);
164 if (state
== LocallyModifiedRevision
) {
165 return LocallyModifiedRevision
;
170 return NormalRevision
;
173 QList
<QAction
*> SubversionPlugin::contextMenuActions(const KFileItemList
& items
)
175 Q_ASSERT(!items
.isEmpty());
177 m_contextItems
= items
;
178 m_contextDir
.clear();
180 // iterate all items and check the revision state to know which
181 // actions can be enabled
182 const int itemsCount
= items
.count();
183 int revisionedCount
= 0;
184 int editingCount
= 0;
185 foreach (const KFileItem
& item
, items
) {
186 const RevisionState state
= revisionState(item
);
187 if (state
!= UnversionedRevision
) {
192 case LocallyModifiedRevision
:
193 case ConflictingRevision
:
200 m_commitAction
->setEnabled(editingCount
> 0);
201 m_addAction
->setEnabled(revisionedCount
== 0);
202 m_removeAction
->setEnabled(revisionedCount
== itemsCount
);
204 QList
<QAction
*> actions
;
205 actions
.append(m_updateAction
);
206 actions
.append(m_commitAction
);
207 actions
.append(m_addAction
);
208 actions
.append(m_removeAction
);
212 QList
<QAction
*> SubversionPlugin::contextMenuActions(const QString
& directory
)
214 m_contextDir
= directory
;
215 m_contextItems
.clear();
217 QList
<QAction
*> actions
;
218 actions
.append(m_updateAction
);
219 actions
.append(m_showLocalChangesAction
);
220 actions
.append(m_commitAction
);
224 void SubversionPlugin::updateFiles()
226 execSvnCommand("update");
229 void SubversionPlugin::showLocalChanges()
231 Q_ASSERT(!m_contextDir
.isEmpty());
232 Q_ASSERT(m_contextItems
.isEmpty());
234 const QString command
= "mkfifo /tmp/fifo; svn diff " +
235 KShell::quoteArg(m_contextDir
) +
236 " > /tmp/fifo & kompare /tmp/fifo; rm /tmp/fifo";
237 KRun::runCommand(command
, 0);
240 void SubversionPlugin::commitFiles()
242 KDialog
dialog(0, Qt::Dialog
);
244 KVBox
* box
= new KVBox(&dialog
);
245 new QLabel(i18nc("@label", "Description:"), box
);
246 QTextEdit
* editor
= new QTextEdit(box
);
248 dialog
.setMainWidget(box
);
249 dialog
.setCaption(i18nc("@title:window", "SVN Commit"));
250 dialog
.setButtons(KDialog::Ok
| KDialog::Cancel
);
251 dialog
.setDefaultButton(KDialog::Ok
);
252 dialog
.setButtonText(KDialog::Ok
, i18nc("@action:button", "Commit"));
254 KConfigGroup
dialogConfig(KSharedConfig::openConfig("dolphinrc"),
256 dialog
.restoreDialogSize(dialogConfig
);
258 if (dialog
.exec() == QDialog::Accepted
) {
259 const QString description
= editor
->toPlainText();
260 execSvnCommand("commit -m " + KShell::quoteArg(description
));
263 dialog
.saveDialogSize(dialogConfig
, KConfigBase::Persistent
);
266 void SubversionPlugin::addFiles()
268 execSvnCommand("add");
271 void SubversionPlugin::removeFiles()
273 execSvnCommand("remove");
276 void SubversionPlugin::execSvnCommand(const QString
& svnCommand
)
278 const QString command
= "svn " + svnCommand
+ ' ';
279 if (!m_contextDir
.isEmpty()) {
280 KRun::runCommand(command
+ KShell::quoteArg(m_contextDir
), 0);
282 foreach (const KFileItem
& item
, m_contextItems
) {
283 KRun::runCommand(command
+ KShell::quoteArg(item
.localPath()), 0);