]>
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"
24 RevisionControlPlugin::RevisionControlPlugin()
28 RevisionControlPlugin::~RevisionControlPlugin()
32 #include "revisioncontrolplugin.moc"
34 // ----------------------------------------------------------------------------
42 #include <kfileitem.h>
48 #include <QTextStream>
50 SubversionPlugin::SubversionPlugin() :
53 m_showLocalChangesAction(0),
60 m_updateAction
= new KAction(this);
61 m_updateAction
->setIcon(KIcon("view-refresh"));
62 m_updateAction
->setText(i18nc("@item:inmenu", "SVN Update"));
63 connect(m_updateAction
, SIGNAL(triggered()),
64 this, SLOT(updateFiles()));
66 m_showLocalChangesAction
= new KAction(this);
67 m_showLocalChangesAction
->setIcon(KIcon("view-split-left-right"));
68 m_showLocalChangesAction
->setText(i18nc("@item:inmenu", "Show Local SVN Changes"));
69 connect(m_showLocalChangesAction
, SIGNAL(triggered()),
70 this, SLOT(showLocalChanges()));
72 m_commitAction
= new KAction(this);
73 m_commitAction
->setText(i18nc("@item:inmenu", "SVN Commit..."));
74 connect(m_commitAction
, SIGNAL(triggered()),
75 this, SLOT(commitFiles()));
77 m_addAction
= new KAction(this);
78 m_addAction
->setIcon(KIcon("list-add"));
79 m_addAction
->setText(i18nc("@item:inmenu", "SVN Add"));
80 connect(m_addAction
, SIGNAL(triggered()),
81 this, SLOT(addFiles()));
83 m_removeAction
= new KAction(this);
84 m_removeAction
->setIcon(KIcon("list-remove"));
85 m_removeAction
->setText(i18nc("@item:inmenu", "SVN Delete"));
86 connect(m_removeAction
, SIGNAL(triggered()),
87 this, SLOT(removeFiles()));
90 SubversionPlugin::~SubversionPlugin()
94 QString
SubversionPlugin::fileName() const
99 bool SubversionPlugin::beginRetrieval(const QString
& directory
)
101 Q_ASSERT(directory
.endsWith('/'));
103 const QString statusCommand
= "svn status " + directory
;
104 FILE* in
= popen(statusCommand
.toAscii().data(), "r");
110 while (fgets(buffer
, sizeof(buffer
), in
) != 0) {
111 RevisionState state
= NormalRevision
;
114 case '?': state
= UnversionedRevision
; break;
115 case 'M': state
= LocallyModifiedRevision
; break;
116 case 'A': state
= AddedRevision
; break;
117 case 'D': state
= RemovedRevision
; break;
118 case 'C': state
= ConflictingRevision
; break;
122 QString
filePath(buffer
);
123 int pos
= filePath
.indexOf('/');
124 const int length
= filePath
.length() - pos
- 1;
125 filePath
= filePath
.mid(pos
, length
);
126 if (!filePath
.isEmpty()) {
127 m_revisionInfoHash
.insert(filePath
, state
);
130 m_revisionInfoKeys
= m_revisionInfoHash
.keys();
134 void SubversionPlugin::endRetrieval()
138 RevisionControlPlugin::RevisionState
SubversionPlugin::revisionState(const KFileItem
& item
)
140 const QString itemUrl
= item
.localPath();
141 if (m_revisionInfoHash
.contains(itemUrl
)) {
142 return m_revisionInfoHash
.value(itemUrl
);
146 // files that have not been listed by 'svn status' (= m_revisionInfoHash)
147 // are under revision control per definition
148 return NormalRevision
;
151 // The item is a directory. Check whether an item listed by 'svn status' (= m_revisionInfoHash)
152 // is part of this directory. In this case a local modification should be indicated in the
153 // directory already.
154 foreach (const QString
& key
, m_revisionInfoKeys
) {
155 if (key
.startsWith(itemUrl
)) {
156 const RevisionState state
= m_revisionInfoHash
.value(key
);
157 if (state
== LocallyModifiedRevision
) {
158 return LocallyModifiedRevision
;
163 return NormalRevision
;
166 QList
<QAction
*> SubversionPlugin::contextMenuActions(const KFileItemList
& items
)
168 Q_ASSERT(!items
.isEmpty());
170 m_contextItems
= items
;
171 m_contextDir
.clear();
173 // iterate all items and check the revision state to know which
174 // actions can be enabled
175 const int itemsCount
= items
.count();
176 int revisionedCount
= 0;
177 int editingCount
= 0;
178 foreach (const KFileItem
& item
, items
) {
179 const RevisionState state
= revisionState(item
);
180 if (state
!= UnversionedRevision
) {
185 case LocallyModifiedRevision
:
186 case ConflictingRevision
:
193 m_commitAction
->setEnabled(editingCount
> 0);
194 m_addAction
->setEnabled(revisionedCount
== 0);
195 m_removeAction
->setEnabled(revisionedCount
== itemsCount
);
197 QList
<QAction
*> actions
;
198 actions
.append(m_updateAction
);
199 actions
.append(m_commitAction
);
200 actions
.append(m_addAction
);
201 actions
.append(m_removeAction
);
205 QList
<QAction
*> SubversionPlugin::contextMenuActions(const QString
& directory
)
207 m_contextDir
= directory
;
208 m_contextItems
.clear();
210 QList
<QAction
*> actions
;
211 actions
.append(m_updateAction
);
212 actions
.append(m_showLocalChangesAction
);
213 actions
.append(m_commitAction
);
217 void SubversionPlugin::updateFiles()
219 execSvnCommand("update");
222 void SubversionPlugin::showLocalChanges()
224 Q_ASSERT(!m_contextDir
.isEmpty());
225 Q_ASSERT(m_contextItems
.isEmpty());
227 const QString command
= "mkfifo /tmp/fifo; svn diff " +
228 KShell::quoteArg(m_contextDir
) +
229 " > /tmp/fifo & kompare /tmp/fifo; rm /tmp/fifo";
230 KRun::runCommand(command
, 0);
233 void SubversionPlugin::commitFiles()
235 KDialog
dialog(0, Qt::Dialog
);
237 KVBox
* box
= new KVBox(&dialog
);
238 new QLabel(i18nc("@label", "Description:"), box
);
239 QTextEdit
* editor
= new QTextEdit(box
);
241 dialog
.setMainWidget(box
);
242 dialog
.setCaption(i18nc("@title:window", "SVN Commit"));
243 dialog
.setButtons(KDialog::Ok
| KDialog::Cancel
);
244 dialog
.setDefaultButton(KDialog::Ok
);
245 dialog
.setButtonText(KDialog::Ok
, i18nc("@action:button", "Commit"));
247 KConfigGroup
dialogConfig(KSharedConfig::openConfig("dolphinrc"),
249 dialog
.restoreDialogSize(dialogConfig
);
251 if (dialog
.exec() == QDialog::Accepted
) {
252 const QString description
= editor
->toPlainText();
253 execSvnCommand("commit -m " + KShell::quoteArg(description
));
256 dialog
.saveDialogSize(dialogConfig
, KConfigBase::Persistent
);
259 void SubversionPlugin::addFiles()
261 execSvnCommand("add");
264 void SubversionPlugin::removeFiles()
266 execSvnCommand("remove");
269 void SubversionPlugin::execSvnCommand(const QString
& svnCommand
)
271 const QString command
= "svn " + svnCommand
+ ' ';
272 if (!m_contextDir
.isEmpty()) {
273 KRun::runCommand(command
+ KShell::quoteArg(m_contextDir
), 0);
275 foreach (const KFileItem
& item
, m_contextItems
) {
276 KRun::runCommand(command
+ KShell::quoteArg(item
.localPath()), 0);