]> cloud.milkyroute.net Git - dolphin.git/blob - src/revisioncontrolplugin.cpp
Use QProcess instead of the low-level API popen(). Thanks to André Wöbbeking for...
[dolphin.git] / src / revisioncontrolplugin.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "revisioncontrolplugin.h"
21
22 RevisionControlPlugin::RevisionControlPlugin()
23 {
24 }
25
26 RevisionControlPlugin::~RevisionControlPlugin()
27 {
28 }
29
30 #include "revisioncontrolplugin.moc"
31
32 // ----------------------------------------------------------------------------
33
34 #include <kaction.h>
35 #include <kdialog.h>
36 #include <kfileitem.h>
37 #include <kicon.h>
38 #include <klocale.h>
39 #include <krun.h>
40 #include <kshell.h>
41 #include <kvbox.h>
42 #include <QDir>
43 #include <QLabel>
44 #include <QProcess>
45 #include <QString>
46 #include <QStringList>
47 #include <QTextEdit>
48 #include <QTextStream>
49
50 SubversionPlugin::SubversionPlugin() :
51 m_revisionInfoHash(),
52 m_revisionInfoKeys(),
53 m_updateAction(0),
54 m_showLocalChangesAction(0),
55 m_commitAction(0),
56 m_addAction(0),
57 m_removeAction(0),
58 m_contextDir(),
59 m_contextItems()
60 {
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()));
66
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()));
72
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()));
77
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()));
83
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()));
89 }
90
91 SubversionPlugin::~SubversionPlugin()
92 {
93 }
94
95 QString SubversionPlugin::fileName() const
96 {
97 return ".svn";
98 }
99
100 bool SubversionPlugin::beginRetrieval(const QString& directory)
101 {
102 Q_ASSERT(directory.endsWith('/'));
103
104 QStringList arguments;
105 arguments << "status" << directory;
106
107 QProcess process;
108 process.start("svn", arguments);
109 if (!process.waitForReadyRead()) {
110 return false;
111 }
112
113 char buffer[1024];
114 while (process.readLine(buffer, sizeof(buffer)) > 0) {
115 RevisionState state = NormalRevision;
116
117 switch (buffer[0]) {
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;
123 default: break;
124 }
125
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);
132 }
133 }
134 m_revisionInfoKeys = m_revisionInfoHash.keys();
135 return true;
136 }
137
138 void SubversionPlugin::endRetrieval()
139 {
140 }
141
142 RevisionControlPlugin::RevisionState SubversionPlugin::revisionState(const KFileItem& item)
143 {
144 const QString itemUrl = item.localPath();
145 if (m_revisionInfoHash.contains(itemUrl)) {
146 return m_revisionInfoHash.value(itemUrl);
147 }
148
149 if (!item.isDir()) {
150 // files that have not been listed by 'svn status' (= m_revisionInfoHash)
151 // are under revision control per definition
152 return NormalRevision;
153 }
154
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;
163 }
164 }
165 }
166
167 return NormalRevision;
168 }
169
170 QList<QAction*> SubversionPlugin::contextMenuActions(const KFileItemList& items)
171 {
172 Q_ASSERT(!items.isEmpty());
173
174 m_contextItems = items;
175 m_contextDir.clear();
176
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) {
185 ++revisionedCount;
186 }
187
188 switch (state) {
189 case LocallyModifiedRevision:
190 case ConflictingRevision:
191 ++editingCount;
192 break;
193 default:
194 break;
195 }
196 }
197 m_commitAction->setEnabled(editingCount > 0);
198 m_addAction->setEnabled(revisionedCount == 0);
199 m_removeAction->setEnabled(revisionedCount == itemsCount);
200
201 QList<QAction*> actions;
202 actions.append(m_updateAction);
203 actions.append(m_commitAction);
204 actions.append(m_addAction);
205 actions.append(m_removeAction);
206 return actions;
207 }
208
209 QList<QAction*> SubversionPlugin::contextMenuActions(const QString& directory)
210 {
211 m_contextDir = directory;
212 m_contextItems.clear();
213
214 QList<QAction*> actions;
215 actions.append(m_updateAction);
216 actions.append(m_showLocalChangesAction);
217 actions.append(m_commitAction);
218 return actions;
219 }
220
221 void SubversionPlugin::updateFiles()
222 {
223 execSvnCommand("update");
224 }
225
226 void SubversionPlugin::showLocalChanges()
227 {
228 Q_ASSERT(!m_contextDir.isEmpty());
229 Q_ASSERT(m_contextItems.isEmpty());
230
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);
235 }
236
237 void SubversionPlugin::commitFiles()
238 {
239 KDialog dialog(0, Qt::Dialog);
240
241 KVBox* box = new KVBox(&dialog);
242 new QLabel(i18nc("@label", "Description:"), box);
243 QTextEdit* editor = new QTextEdit(box);
244
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"));
250
251 KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
252 "SvnCommitDialog");
253 dialog.restoreDialogSize(dialogConfig);
254
255 if (dialog.exec() == QDialog::Accepted) {
256 const QString description = editor->toPlainText();
257 execSvnCommand("commit -m " + KShell::quoteArg(description));
258 }
259
260 dialog.saveDialogSize(dialogConfig, KConfigBase::Persistent);
261 }
262
263 void SubversionPlugin::addFiles()
264 {
265 execSvnCommand("add");
266 }
267
268 void SubversionPlugin::removeFiles()
269 {
270 execSvnCommand("remove");
271 }
272
273 void SubversionPlugin::execSvnCommand(const QString& svnCommand)
274 {
275 const QString command = "svn " + svnCommand + ' ';
276 if (!m_contextDir.isEmpty()) {
277 KRun::runCommand(command + KShell::quoteArg(m_contextDir), 0);
278 } else {
279 foreach (const KFileItem& item, m_contextItems) {
280 KRun::runCommand(command + KShell::quoteArg(item.localPath()), 0);
281 }
282 }
283 }