]> cloud.milkyroute.net Git - dolphin.git/blob - src/kversioncontrolplugin.cpp
Move the selection listener from DolphinView to ViewExtensionsFactory. The statusbar...
[dolphin.git] / src / kversioncontrolplugin.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 "kversioncontrolplugin.h"
21
22 KVersionControlPlugin::KVersionControlPlugin()
23 {
24 }
25
26 KVersionControlPlugin::~KVersionControlPlugin()
27 {
28 }
29
30 #include "kversioncontrolplugin.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_versionInfoHash(),
52 m_versionInfoKeys(),
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 m_tempFile()
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 VersionState state = NormalVersion;
117 QString filePath(buffer);
118
119 switch (buffer[0]) {
120 case '?': state = UnversionedVersion; break;
121 case 'M': state = LocallyModifiedVersion; break;
122 case 'A': state = AddedVersion; break;
123 case 'D': state = RemovedVersion; break;
124 case 'C': state = ConflictingVersion; break;
125 default:
126 if (filePath.contains('*')) {
127 state = UpdateRequiredVersion;
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_versionInfoHash.insert(filePath, state);
137 }
138 }
139 }
140
141 m_versionInfoKeys = m_versionInfoHash.keys();
142 return true;
143 }
144
145 void SubversionPlugin::endRetrieval()
146 {
147 }
148
149 KVersionControlPlugin::VersionState SubversionPlugin::versionState(const KFileItem& item)
150 {
151 const QString itemUrl = item.localPath();
152 if (m_versionInfoHash.contains(itemUrl)) {
153 return m_versionInfoHash.value(itemUrl);
154 }
155
156 if (!item.isDir()) {
157 // files that have not been listed by 'svn status' (= m_versionInfoHash)
158 // are under version control per definition
159 return NormalVersion;
160 }
161
162 // The item is a directory. Check whether an item listed by 'svn status' (= m_versionInfoHash)
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_versionInfoKeys) {
166 if (key.startsWith(itemUrl)) {
167 const VersionState state = m_versionInfoHash.value(key);
168 if (state == LocallyModifiedVersion) {
169 return LocallyModifiedVersion;
170 }
171 }
172 }
173
174 return NormalVersion;
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 version state to know which
186 // actions can be enabled
187 const int itemsCount = items.count();
188 int versionedCount = 0;
189 int editingCount = 0;
190 foreach (const KFileItem& item, items) {
191 const VersionState state = versionState(item);
192 if (state != UnversionedVersion) {
193 ++versionedCount;
194 }
195
196 switch (state) {
197 case LocallyModifiedVersion:
198 case ConflictingVersion:
199 ++editingCount;
200 break;
201 default:
202 break;
203 }
204 }
205 m_commitAction->setEnabled(editingCount > 0);
206 m_addAction->setEnabled(versionedCount == 0);
207 m_removeAction->setEnabled(versionedCount == 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". The temporary
278 // file must stay alive until slotOperationCompleted() is invoked and will
279 // be destroyed when the version plugin is destructed.
280 if (!m_tempFile.open()) {
281 emit errorMessage(i18nc("@info:status", "Commit of SVN changes failed."));
282 return;
283 }
284
285 QTextStream out(&m_tempFile);
286 const QString fileName = m_tempFile.fileName();
287 out << editor->toPlainText();
288 m_tempFile.close();
289
290 execSvnCommand("commit -F " + KShell::quoteArg(fileName),
291 i18nc("@info:status", "Committing SVN changes..."),
292 i18nc("@info:status", "Commit of SVN changes failed."),
293 i18nc("@info:status", "Committed SVN changes."));
294 }
295
296 dialog.saveDialogSize(dialogConfig, KConfigBase::Persistent);
297 }
298
299 void SubversionPlugin::addFiles()
300 {
301 execSvnCommand("add",
302 i18nc("@info:status", "Adding files to SVN repository..."),
303 i18nc("@info:status", "Adding of files to SVN repository failed."),
304 i18nc("@info:status", "Added files to SVN repository."));
305 }
306
307 void SubversionPlugin::removeFiles()
308 {
309 execSvnCommand("remove",
310 i18nc("@info:status", "Removing files from SVN repository..."),
311 i18nc("@info:status", "Removing of files from SVN repository failed."),
312 i18nc("@info:status", "Removed files from SVN repository."));
313 }
314
315 void SubversionPlugin::slotOperationCompleted()
316 {
317 if (m_contextItems.isEmpty()) {
318 emit operationCompletedMessage(m_operationCompletedMsg);
319 emit versionStatesChanged();
320 } else {
321 startSvnCommandProcess();
322 }
323 }
324
325 void SubversionPlugin::slotOperationError()
326 {
327 emit errorMessage(m_errorMsg);
328
329 // don't do any operation on other items anymore
330 m_contextItems.clear();
331 }
332
333 void SubversionPlugin::execSvnCommand(const QString& svnCommand,
334 const QString& infoMsg,
335 const QString& errorMsg,
336 const QString& operationCompletedMsg)
337 {
338 emit infoMessage(infoMsg);
339
340 m_command = svnCommand;
341 m_errorMsg = errorMsg;
342 m_operationCompletedMsg = operationCompletedMsg;
343
344 startSvnCommandProcess();
345 }
346
347 void SubversionPlugin::startSvnCommandProcess()
348 {
349 QProcess* process = new QProcess(this);
350 connect(process, SIGNAL(finished(int)),
351 this, SLOT(slotOperationCompleted()));
352 connect(process, SIGNAL(error(QProcess::ProcessError)),
353 this, SLOT(slotOperationError()));
354
355 const QString program = "svn " + m_command + ' ';
356 if (!m_contextDir.isEmpty()) {
357 process->start(program + KShell::quoteArg(m_contextDir));
358 m_contextDir.clear();
359 } else {
360 const KFileItem item = m_contextItems.takeLast();
361 process->start(program + KShell::quoteArg(item.localPath()));
362 // the remaining items of m_contextItems will be executed
363 // after the process has finished (see slotOperationFinished())
364 }
365 }