]> cloud.milkyroute.net Git - dolphin.git/blob - src/revisioncontrolplugin.cpp
inform the revision control observer about changed revision states
[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_command(),
59 m_errorMsg(),
60 m_operationCompletedMsg(),
61 m_contextDir(),
62 m_contextItems()
63 {
64 m_updateAction = new KAction(this);
65 m_updateAction->setIcon(KIcon("view-refresh"));
66 m_updateAction->setText(i18nc("@item:inmenu", "SVN Update"));
67 connect(m_updateAction, SIGNAL(triggered()),
68 this, SLOT(updateFiles()));
69
70 m_showLocalChangesAction = new KAction(this);
71 m_showLocalChangesAction->setIcon(KIcon("view-split-left-right"));
72 m_showLocalChangesAction->setText(i18nc("@item:inmenu", "Show Local SVN Changes"));
73 connect(m_showLocalChangesAction, SIGNAL(triggered()),
74 this, SLOT(showLocalChanges()));
75
76 m_commitAction = new KAction(this);
77 m_commitAction->setText(i18nc("@item:inmenu", "SVN Commit..."));
78 connect(m_commitAction, SIGNAL(triggered()),
79 this, SLOT(commitFiles()));
80
81 m_addAction = new KAction(this);
82 m_addAction->setIcon(KIcon("list-add"));
83 m_addAction->setText(i18nc("@item:inmenu", "SVN Add"));
84 connect(m_addAction, SIGNAL(triggered()),
85 this, SLOT(addFiles()));
86
87 m_removeAction = new KAction(this);
88 m_removeAction->setIcon(KIcon("list-remove"));
89 m_removeAction->setText(i18nc("@item:inmenu", "SVN Delete"));
90 connect(m_removeAction, SIGNAL(triggered()),
91 this, SLOT(removeFiles()));
92 }
93
94 SubversionPlugin::~SubversionPlugin()
95 {
96 }
97
98 QString SubversionPlugin::fileName() const
99 {
100 return ".svn";
101 }
102
103 bool SubversionPlugin::beginRetrieval(const QString& directory)
104 {
105 Q_ASSERT(directory.endsWith('/'));
106
107 QStringList arguments;
108 arguments << "status" << "--show-updates" << directory;
109
110 QProcess process;
111 process.start("svn", arguments);
112 while (process.waitForReadyRead()) {
113 char buffer[1024];
114 while (process.readLine(buffer, sizeof(buffer)) > 0) {
115 RevisionState state = NormalRevision;
116 QString filePath(buffer);
117
118 switch (buffer[0]) {
119 case '?': state = UnversionedRevision; break;
120 case 'M': state = LocallyModifiedRevision; break;
121 case 'A': state = AddedRevision; break;
122 case 'D': state = RemovedRevision; break;
123 case 'C': state = ConflictingRevision; break;
124 default:
125 if (filePath.contains('*')) {
126 state = UpdateRequiredRevision;
127 }
128 break;
129 }
130
131 int pos = filePath.indexOf('/');
132 const int length = filePath.length() - pos - 1;
133 filePath = filePath.mid(pos, length);
134 if (!filePath.isEmpty()) {
135 m_revisionInfoHash.insert(filePath, state);
136 }
137 }
138 }
139
140 m_revisionInfoKeys = m_revisionInfoHash.keys();
141 return true;
142 }
143
144 void SubversionPlugin::endRetrieval()
145 {
146 }
147
148 RevisionControlPlugin::RevisionState SubversionPlugin::revisionState(const KFileItem& item)
149 {
150 const QString itemUrl = item.localPath();
151 if (m_revisionInfoHash.contains(itemUrl)) {
152 return m_revisionInfoHash.value(itemUrl);
153 }
154
155 if (!item.isDir()) {
156 // files that have not been listed by 'svn status' (= m_revisionInfoHash)
157 // are under revision control per definition
158 return NormalRevision;
159 }
160
161 // The item is a directory. Check whether an item listed by 'svn status' (= m_revisionInfoHash)
162 // is part of this directory. In this case a local modification should be indicated in the
163 // directory already.
164 foreach (const QString& key, m_revisionInfoKeys) {
165 if (key.startsWith(itemUrl)) {
166 const RevisionState state = m_revisionInfoHash.value(key);
167 if (state == LocallyModifiedRevision) {
168 return LocallyModifiedRevision;
169 }
170 }
171 }
172
173 return NormalRevision;
174 }
175
176 QList<QAction*> SubversionPlugin::contextMenuActions(const KFileItemList& items)
177 {
178 Q_ASSERT(!items.isEmpty());
179 foreach (const KFileItem& item, items) {
180 m_contextItems.append(item);
181 }
182 m_contextDir.clear();
183
184 // iterate all items and check the revision state to know which
185 // actions can be enabled
186 const int itemsCount = items.count();
187 int revisionedCount = 0;
188 int editingCount = 0;
189 foreach (const KFileItem& item, items) {
190 const RevisionState state = revisionState(item);
191 if (state != UnversionedRevision) {
192 ++revisionedCount;
193 }
194
195 switch (state) {
196 case LocallyModifiedRevision:
197 case ConflictingRevision:
198 ++editingCount;
199 break;
200 default:
201 break;
202 }
203 }
204 m_commitAction->setEnabled(editingCount > 0);
205 m_addAction->setEnabled(revisionedCount == 0);
206 m_removeAction->setEnabled(revisionedCount == itemsCount);
207
208 QList<QAction*> actions;
209 actions.append(m_updateAction);
210 actions.append(m_commitAction);
211 actions.append(m_addAction);
212 actions.append(m_removeAction);
213 return actions;
214 }
215
216 QList<QAction*> SubversionPlugin::contextMenuActions(const QString& directory)
217 {
218 const bool enabled = m_contextItems.isEmpty();
219 if (enabled) {
220 m_contextDir = directory;
221 }
222
223 // Only enable the SVN actions if no SVN commands are
224 // executed currently (see slotOperationCompleted() and
225 // startSvnCommandProcess()).
226 m_updateAction->setEnabled(enabled);
227 m_showLocalChangesAction->setEnabled(enabled);
228 m_commitAction->setEnabled(enabled);
229
230 QList<QAction*> actions;
231 actions.append(m_updateAction);
232 actions.append(m_showLocalChangesAction);
233 actions.append(m_commitAction);
234 return actions;
235 }
236
237 void SubversionPlugin::updateFiles()
238 {
239 execSvnCommand("update",
240 i18nc("@info:status", "Updating SVN repository..."),
241 i18nc("@info:status", "Update of SVN repository failed."),
242 i18nc("@info:status", "Updated SVN repository."));
243 }
244
245 void SubversionPlugin::showLocalChanges()
246 {
247 Q_ASSERT(!m_contextDir.isEmpty());
248 Q_ASSERT(m_contextItems.isEmpty());
249
250 const QString command = "mkfifo /tmp/fifo; svn diff " +
251 KShell::quoteArg(m_contextDir) +
252 " > /tmp/fifo & kompare /tmp/fifo; rm /tmp/fifo";
253 KRun::runCommand(command, 0);
254 }
255
256 void SubversionPlugin::commitFiles()
257 {
258 KDialog dialog(0, Qt::Dialog);
259
260 KVBox* box = new KVBox(&dialog);
261 new QLabel(i18nc("@label", "Description:"), box);
262 QTextEdit* editor = new QTextEdit(box);
263
264 dialog.setMainWidget(box);
265 dialog.setCaption(i18nc("@title:window", "SVN Commit"));
266 dialog.setButtons(KDialog::Ok | KDialog::Cancel);
267 dialog.setDefaultButton(KDialog::Ok);
268 dialog.setButtonText(KDialog::Ok, i18nc("@action:button", "Commit"));
269
270 KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
271 "SvnCommitDialog");
272 dialog.restoreDialogSize(dialogConfig);
273
274 if (dialog.exec() == QDialog::Accepted) {
275 const QString description = editor->toPlainText();
276 execSvnCommand("commit -m " + KShell::quoteArg(description),
277 i18nc("@info:status", "Committing SVN changes..."),
278 i18nc("@info:status", "Commit of SVN changes failed."),
279 i18nc("@info:status", "Committed SVN changes."));
280 }
281
282 dialog.saveDialogSize(dialogConfig, KConfigBase::Persistent);
283 }
284
285 void SubversionPlugin::addFiles()
286 {
287 execSvnCommand("add",
288 i18nc("@info:status", "Adding files to SVN repository..."),
289 i18nc("@info:status", "Adding of files to SVN repository failed."),
290 i18nc("@info:status", "Added files to SVN repository."));
291 }
292
293 void SubversionPlugin::removeFiles()
294 {
295 execSvnCommand("remove",
296 i18nc("@info:status", "Removing files from SVN repository..."),
297 i18nc("@info:status", "Removing of files from SVN repository failed."),
298 i18nc("@info:status", "Removed files from SVN repository."));
299 }
300
301 void SubversionPlugin::slotOperationCompleted()
302 {
303 if (m_contextItems.isEmpty()) {
304 emit operationCompletedMessage(m_operationCompletedMsg);
305 emit revisionStatesChanged();
306 } else {
307 startSvnCommandProcess();
308 }
309 }
310
311 void SubversionPlugin::slotOperationError()
312 {
313 emit errorMessage(m_errorMsg);
314
315 // don't do any operation on other items anymore
316 m_contextItems.clear();
317 }
318
319 void SubversionPlugin::execSvnCommand(const QString& svnCommand,
320 const QString& infoMsg,
321 const QString& errorMsg,
322 const QString& operationCompletedMsg)
323 {
324 emit infoMessage(infoMsg);
325
326 m_command = svnCommand;
327 m_errorMsg = errorMsg;
328 m_operationCompletedMsg = operationCompletedMsg;
329
330 startSvnCommandProcess();
331 }
332
333 void SubversionPlugin::startSvnCommandProcess()
334 {
335 QProcess* process = new QProcess(this);
336 connect(process, SIGNAL(finished(int)),
337 this, SLOT(slotOperationCompleted()));
338 connect(process, SIGNAL(error(QProcess::ProcessError)),
339 this, SLOT(slotOperationError()));
340
341 QStringList arguments;
342 arguments << m_command;
343 if (!m_contextDir.isEmpty()) {
344 process->start("svn", arguments << KShell::quoteArg(m_contextDir));
345 m_contextDir.clear();
346 } else {
347 const KFileItem item = m_contextItems.takeLast();
348 process->start("svn", arguments << KShell::quoteArg(item.localPath()));
349 // the remaining items of m_contextItems will be executed
350 // after the process has finished (see slotOperationFinished())
351 }
352 }