]> cloud.milkyroute.net Git - dolphin.git/blob - src/revisioncontrolplugin.cpp
Use the output of 'svn status' instead of doing a custom and error-prone .svn-parsing...
[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 #include <stdio.h>
23
24 RevisionControlPlugin::RevisionControlPlugin()
25 {
26 }
27
28 RevisionControlPlugin::~RevisionControlPlugin()
29 {
30 }
31
32 #include "revisioncontrolplugin.moc"
33
34 // ----------------------------------------------------------------------------
35
36 #include <kaction.h>
37 #include <kdialog.h>
38 #include <kicon.h>
39 #include <klocale.h>
40 #include <krun.h>
41 #include <kshell.h>
42 #include <kfileitem.h>
43 #include <kvbox.h>
44 #include <QDir>
45 #include <QLabel>
46 #include <QString>
47 #include <QTextEdit>
48 #include <QTextStream>
49
50 SubversionPlugin::SubversionPlugin() :
51 m_revisionInfoHash(),
52 m_updateAction(0),
53 m_showLocalChangesAction(0),
54 m_commitAction(0),
55 m_addAction(0),
56 m_removeAction(0),
57 m_contextDir(),
58 m_contextItems()
59 {
60 m_updateAction = new KAction(this);
61 m_updateAction->setIcon(KIcon("view-refresh"));
62 m_updateAction->setText(i18nc("@item:inmenu", "SVN Update"));
63 connect(m_updateAction, SIGNAL(triggered()),
64 this, SLOT(updateFiles()));
65
66 m_showLocalChangesAction = new KAction(this);
67 m_showLocalChangesAction->setIcon(KIcon("view-split-left-right"));
68 m_showLocalChangesAction->setText(i18nc("@item:inmenu", "Show Local SVN Changes"));
69 connect(m_showLocalChangesAction, SIGNAL(triggered()),
70 this, SLOT(showLocalChanges()));
71
72 m_commitAction = new KAction(this);
73 m_commitAction->setText(i18nc("@item:inmenu", "SVN Commit..."));
74 connect(m_commitAction, SIGNAL(triggered()),
75 this, SLOT(commitFiles()));
76
77 m_addAction = new KAction(this);
78 m_addAction->setIcon(KIcon("list-add"));
79 m_addAction->setText(i18nc("@item:inmenu", "SVN Add"));
80 connect(m_addAction, SIGNAL(triggered()),
81 this, SLOT(addFiles()));
82
83 m_removeAction = new KAction(this);
84 m_removeAction->setIcon(KIcon("list-remove"));
85 m_removeAction->setText(i18nc("@item:inmenu", "SVN Delete"));
86 connect(m_removeAction, SIGNAL(triggered()),
87 this, SLOT(removeFiles()));
88 }
89
90 SubversionPlugin::~SubversionPlugin()
91 {
92 }
93
94 QString SubversionPlugin::fileName() const
95 {
96 return ".svn";
97 }
98
99 bool SubversionPlugin::beginRetrieval(const QString& directory)
100 {
101 Q_ASSERT(directory.endsWith('/'));
102
103 const QString statusCommand = "svn status " + directory;
104 FILE* in = popen(statusCommand.toAscii().data(), "r");
105 if (in == 0) {
106 return false;
107 }
108
109 char buffer[1024];
110 while (fgets(buffer, sizeof(buffer), in) != 0) {
111 RevisionState state = NormalRevision;
112
113 switch (buffer[0]) {
114 case '?': state = UnversionedRevision; break;
115 case 'M': state = LocallyModifiedRevision; break;
116 case 'A': state = AddedRevision; break;
117 case 'D': state = RemovedRevision; break;
118 case 'C': state = ConflictingRevision; break;
119 default: break;
120 }
121
122 QString filePath(buffer);
123 int pos = filePath.indexOf('/');
124 const int length = filePath.length() - pos - 1;
125 filePath = filePath.mid(pos, length);
126 if (!filePath.isEmpty()) {
127 m_revisionInfoHash.insert(filePath, state);
128 }
129 }
130 m_revisionInfoKeys = m_revisionInfoHash.keys();
131 return true;
132 }
133
134 void SubversionPlugin::endRetrieval()
135 {
136 }
137
138 RevisionControlPlugin::RevisionState SubversionPlugin::revisionState(const KFileItem& item)
139 {
140 const QString itemUrl = item.localPath();
141 if (m_revisionInfoHash.contains(itemUrl)) {
142 return m_revisionInfoHash.value(itemUrl);
143 }
144
145 if (!item.isDir()) {
146 // files that have not been listed by 'svn status' (= m_revisionInfoHash)
147 // are under revision control per definition
148 return NormalRevision;
149 }
150
151 // The item is a directory. Check whether an item listed by 'svn status' (= m_revisionInfoHash)
152 // is part of this directory. In this case a local modification should be indicated in the
153 // directory already.
154 foreach (const QString& key, m_revisionInfoKeys) {
155 if (key.startsWith(itemUrl)) {
156 const RevisionState state = m_revisionInfoHash.value(key);
157 if (state == LocallyModifiedRevision) {
158 return LocallyModifiedRevision;
159 }
160 }
161 }
162
163 return NormalRevision;
164 }
165
166 QList<QAction*> SubversionPlugin::contextMenuActions(const KFileItemList& items)
167 {
168 Q_ASSERT(!items.isEmpty());
169
170 m_contextItems = items;
171 m_contextDir.clear();
172
173 // iterate all items and check the revision state to know which
174 // actions can be enabled
175 const int itemsCount = items.count();
176 int revisionedCount = 0;
177 int editingCount = 0;
178 foreach (const KFileItem& item, items) {
179 const RevisionState state = revisionState(item);
180 if (state != UnversionedRevision) {
181 ++revisionedCount;
182 }
183
184 switch (state) {
185 case LocallyModifiedRevision:
186 case ConflictingRevision:
187 ++editingCount;
188 break;
189 default:
190 break;
191 }
192 }
193 m_commitAction->setEnabled(editingCount > 0);
194 m_addAction->setEnabled(revisionedCount == 0);
195 m_removeAction->setEnabled(revisionedCount == itemsCount);
196
197 QList<QAction*> actions;
198 actions.append(m_updateAction);
199 actions.append(m_commitAction);
200 actions.append(m_addAction);
201 actions.append(m_removeAction);
202 return actions;
203 }
204
205 QList<QAction*> SubversionPlugin::contextMenuActions(const QString& directory)
206 {
207 m_contextDir = directory;
208 m_contextItems.clear();
209
210 QList<QAction*> actions;
211 actions.append(m_updateAction);
212 actions.append(m_showLocalChangesAction);
213 actions.append(m_commitAction);
214 return actions;
215 }
216
217 void SubversionPlugin::updateFiles()
218 {
219 execSvnCommand("update");
220 }
221
222 void SubversionPlugin::showLocalChanges()
223 {
224 Q_ASSERT(!m_contextDir.isEmpty());
225 Q_ASSERT(m_contextItems.isEmpty());
226
227 const QString command = "mkfifo /tmp/fifo; svn diff " +
228 KShell::quoteArg(m_contextDir) +
229 " > /tmp/fifo & kompare /tmp/fifo; rm /tmp/fifo";
230 KRun::runCommand(command, 0);
231 }
232
233 void SubversionPlugin::commitFiles()
234 {
235 KDialog dialog(0, Qt::Dialog);
236
237 KVBox* box = new KVBox(&dialog);
238 new QLabel(i18nc("@label", "Description:"), box);
239 QTextEdit* editor = new QTextEdit(box);
240
241 dialog.setMainWidget(box);
242 dialog.setCaption(i18nc("@title:window", "SVN Commit"));
243 dialog.setButtons(KDialog::Ok | KDialog::Cancel);
244 dialog.setDefaultButton(KDialog::Ok);
245 dialog.setButtonText(KDialog::Ok, i18nc("@action:button", "Commit"));
246
247 KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
248 "SvnCommitDialog");
249 dialog.restoreDialogSize(dialogConfig);
250
251 if (dialog.exec() == QDialog::Accepted) {
252 const QString description = editor->toPlainText();
253 execSvnCommand("commit -m " + KShell::quoteArg(description));
254 }
255
256 dialog.saveDialogSize(dialogConfig, KConfigBase::Persistent);
257 }
258
259 void SubversionPlugin::addFiles()
260 {
261 execSvnCommand("add");
262 }
263
264 void SubversionPlugin::removeFiles()
265 {
266 execSvnCommand("remove");
267 }
268
269 void SubversionPlugin::execSvnCommand(const QString& svnCommand)
270 {
271 const QString command = "svn " + svnCommand + ' ';
272 if (!m_contextDir.isEmpty()) {
273 KRun::runCommand(command + KShell::quoteArg(m_contextDir), 0);
274 } else {
275 foreach (const KFileItem& item, m_contextItems) {
276 KRun::runCommand(command + KShell::quoteArg(item.localPath()), 0);
277 }
278 }
279 }