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