]> cloud.milkyroute.net Git - dolphin.git/blob - src/revisioncontrolplugin.cpp
- indicate whether the server provides an updated version of a file
[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" << "--show-updates" << directory;
106
107 QProcess process;
108 process.start("svn", arguments);
109 while (process.waitForReadyRead()) {
110 char buffer[1024];
111 while (process.readLine(buffer, sizeof(buffer)) > 0) {
112 RevisionState state = NormalRevision;
113 QString filePath(buffer);
114
115 switch (buffer[0]) {
116 case '?': state = UnversionedRevision; break;
117 case 'M': state = LocallyModifiedRevision; break;
118 case 'A': state = AddedRevision; break;
119 case 'D': state = RemovedRevision; break;
120 case 'C': state = ConflictingRevision; break;
121 default:
122 if (filePath.contains('*')) {
123 state = UpdateRequiredRevision;
124 }
125 break;
126 }
127
128 int pos = filePath.indexOf('/');
129 const int length = filePath.length() - pos - 1;
130 filePath = filePath.mid(pos, length);
131 if (!filePath.isEmpty()) {
132 m_revisionInfoHash.insert(filePath, state);
133 }
134 }
135 }
136
137 m_revisionInfoKeys = m_revisionInfoHash.keys();
138 return true;
139 }
140
141 void SubversionPlugin::endRetrieval()
142 {
143 }
144
145 RevisionControlPlugin::RevisionState SubversionPlugin::revisionState(const KFileItem& item)
146 {
147 const QString itemUrl = item.localPath();
148 if (m_revisionInfoHash.contains(itemUrl)) {
149 return m_revisionInfoHash.value(itemUrl);
150 }
151
152 if (!item.isDir()) {
153 // files that have not been listed by 'svn status' (= m_revisionInfoHash)
154 // are under revision control per definition
155 return NormalRevision;
156 }
157
158 // The item is a directory. Check whether an item listed by 'svn status' (= m_revisionInfoHash)
159 // is part of this directory. In this case a local modification should be indicated in the
160 // directory already.
161 foreach (const QString& key, m_revisionInfoKeys) {
162 if (key.startsWith(itemUrl)) {
163 const RevisionState state = m_revisionInfoHash.value(key);
164 if (state == LocallyModifiedRevision) {
165 return LocallyModifiedRevision;
166 }
167 }
168 }
169
170 return NormalRevision;
171 }
172
173 QList<QAction*> SubversionPlugin::contextMenuActions(const KFileItemList& items)
174 {
175 Q_ASSERT(!items.isEmpty());
176
177 m_contextItems = items;
178 m_contextDir.clear();
179
180 // iterate all items and check the revision state to know which
181 // actions can be enabled
182 const int itemsCount = items.count();
183 int revisionedCount = 0;
184 int editingCount = 0;
185 foreach (const KFileItem& item, items) {
186 const RevisionState state = revisionState(item);
187 if (state != UnversionedRevision) {
188 ++revisionedCount;
189 }
190
191 switch (state) {
192 case LocallyModifiedRevision:
193 case ConflictingRevision:
194 ++editingCount;
195 break;
196 default:
197 break;
198 }
199 }
200 m_commitAction->setEnabled(editingCount > 0);
201 m_addAction->setEnabled(revisionedCount == 0);
202 m_removeAction->setEnabled(revisionedCount == itemsCount);
203
204 QList<QAction*> actions;
205 actions.append(m_updateAction);
206 actions.append(m_commitAction);
207 actions.append(m_addAction);
208 actions.append(m_removeAction);
209 return actions;
210 }
211
212 QList<QAction*> SubversionPlugin::contextMenuActions(const QString& directory)
213 {
214 m_contextDir = directory;
215 m_contextItems.clear();
216
217 QList<QAction*> actions;
218 actions.append(m_updateAction);
219 actions.append(m_showLocalChangesAction);
220 actions.append(m_commitAction);
221 return actions;
222 }
223
224 void SubversionPlugin::updateFiles()
225 {
226 execSvnCommand("update");
227 }
228
229 void SubversionPlugin::showLocalChanges()
230 {
231 Q_ASSERT(!m_contextDir.isEmpty());
232 Q_ASSERT(m_contextItems.isEmpty());
233
234 const QString command = "mkfifo /tmp/fifo; svn diff " +
235 KShell::quoteArg(m_contextDir) +
236 " > /tmp/fifo & kompare /tmp/fifo; rm /tmp/fifo";
237 KRun::runCommand(command, 0);
238 }
239
240 void SubversionPlugin::commitFiles()
241 {
242 KDialog dialog(0, Qt::Dialog);
243
244 KVBox* box = new KVBox(&dialog);
245 new QLabel(i18nc("@label", "Description:"), box);
246 QTextEdit* editor = new QTextEdit(box);
247
248 dialog.setMainWidget(box);
249 dialog.setCaption(i18nc("@title:window", "SVN Commit"));
250 dialog.setButtons(KDialog::Ok | KDialog::Cancel);
251 dialog.setDefaultButton(KDialog::Ok);
252 dialog.setButtonText(KDialog::Ok, i18nc("@action:button", "Commit"));
253
254 KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
255 "SvnCommitDialog");
256 dialog.restoreDialogSize(dialogConfig);
257
258 if (dialog.exec() == QDialog::Accepted) {
259 const QString description = editor->toPlainText();
260 execSvnCommand("commit -m " + KShell::quoteArg(description));
261 }
262
263 dialog.saveDialogSize(dialogConfig, KConfigBase::Persistent);
264 }
265
266 void SubversionPlugin::addFiles()
267 {
268 execSvnCommand("add");
269 }
270
271 void SubversionPlugin::removeFiles()
272 {
273 execSvnCommand("remove");
274 }
275
276 void SubversionPlugin::execSvnCommand(const QString& svnCommand)
277 {
278 const QString command = "svn " + svnCommand + ' ';
279 if (!m_contextDir.isEmpty()) {
280 KRun::runCommand(command + KShell::quoteArg(m_contextDir), 0);
281 } else {
282 foreach (const KFileItem& item, m_contextItems) {
283 KRun::runCommand(command + KShell::quoteArg(item.localPath()), 0);
284 }
285 }
286 }