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