1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
/***************************************************************************
* Copyright (C) 2009 by Peter Penz <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
#include "revisioncontrolplugin.h"
RevisionControlPlugin::RevisionControlPlugin()
{
}
RevisionControlPlugin::~RevisionControlPlugin()
{
}
#include "revisioncontrolplugin.moc"
// ----------------------------------------------------------------------------
#include <kaction.h>
#include <kdialog.h>
#include <kicon.h>
#include <klocale.h>
#include <krun.h>
#include <kshell.h>
#include <kfileitem.h>
#include <kvbox.h>
#include <QDir>
#include <QLabel>
#include <QString>
#include <QTextEdit>
#include <QTextStream>
SubversionPlugin::SubversionPlugin() :
m_retrievalDir(),
m_revisionInfoHash(),
m_updateAction(0),
m_showLocalChangesAction(0),
m_commitAction(0),
m_addAction(0),
m_removeAction(0),
m_contextDir(),
m_contextItems()
{
m_updateAction = new KAction(this);
m_updateAction->setIcon(KIcon("view-refresh"));
m_updateAction->setText(i18nc("@item:inmenu", "SVN Update"));
connect(m_updateAction, SIGNAL(triggered()),
this, SLOT(updateFiles()));
m_showLocalChangesAction = new KAction(this);
m_showLocalChangesAction->setIcon(KIcon("view-split-left-right"));
m_showLocalChangesAction->setText(i18nc("@item:inmenu", "Show Local SVN Changes"));
connect(m_showLocalChangesAction, SIGNAL(triggered()),
this, SLOT(showLocalChanges()));
m_commitAction = new KAction(this);
m_commitAction->setText(i18nc("@item:inmenu", "SVN Commit..."));
connect(m_commitAction, SIGNAL(triggered()),
this, SLOT(commitFiles()));
m_addAction = new KAction(this);
m_addAction->setIcon(KIcon("list-add"));
m_addAction->setText(i18nc("@item:inmenu", "SVN Add"));
connect(m_addAction, SIGNAL(triggered()),
this, SLOT(addFiles()));
m_removeAction = new KAction(this);
m_removeAction->setIcon(KIcon("list-remove"));
m_removeAction->setText(i18nc("@item:inmenu", "SVN Delete"));
connect(m_removeAction, SIGNAL(triggered()),
this, SLOT(removeFiles()));
}
SubversionPlugin::~SubversionPlugin()
{
}
QString SubversionPlugin::fileName() const
{
return ".svn";
}
bool SubversionPlugin::beginRetrieval(const QString& directory)
{
Q_ASSERT(directory.endsWith('/'));
m_retrievalDir = directory;
const QString path = directory + ".svn/text-base/";
QDir dir(path);
const QFileInfoList fileInfoList = dir.entryInfoList();
const int size = fileInfoList.size();
QString fileName;
for (int i = 0; i < size; ++i) {
const QFileInfo fileInfo = fileInfoList.at(i);
fileName = fileInfo.fileName();
// Remove the ".svn-base" postfix to be able to compare the filenames
// in a fast way in SubversionPlugin::revisionState().
fileName.chop(sizeof(".svn-base") / sizeof(char) - 1);
if (!fileName.isEmpty()) {
RevisionInfo info;
info.size = fileInfo.size();
info.timeStamp = fileInfo.lastModified();
m_revisionInfoHash.insert(directory + fileName, info);
}
}
return size > 0;
}
void SubversionPlugin::endRetrieval()
{
}
RevisionControlPlugin::RevisionState SubversionPlugin::revisionState(const KFileItem& item)
{
const QString itemUrl = item.localPath();
if (item.isDir()) {
QFile file(itemUrl + "/.svn");
if (file.open(QIODevice::ReadOnly)) {
file.close();
return RevisionControlPlugin::NormalRevision;
}
} else if (m_revisionInfoHash.contains(itemUrl)) {
const RevisionInfo info = m_revisionInfoHash.value(itemUrl);
const QDateTime localTimeStamp = item.time(KFileItem::ModificationTime).dateTime();
const QDateTime versionedTimeStamp = info.timeStamp;
if (localTimeStamp > versionedTimeStamp) {
if ((info.size != item.size()) || !equalRevisionContent(item.name())) {
return RevisionControlPlugin::LocallyModifiedRevision;
}
} else if (localTimeStamp < versionedTimeStamp) {
if ((info.size != item.size()) || !equalRevisionContent(item.name())) {
return RevisionControlPlugin::UpdateRequiredRevision;
}
}
return RevisionControlPlugin::NormalRevision;
}
return RevisionControlPlugin::UnversionedRevision;
}
QList<QAction*> SubversionPlugin::contextMenuActions(const KFileItemList& items)
{
Q_ASSERT(!items.isEmpty());
m_contextItems = items;
m_contextDir.clear();
// iterate all items and check the revision state to know which
// actions can be enabled
const int itemsCount = items.count();
int revisionedCount = 0;
int editingCount = 0;
foreach (const KFileItem& item, items) {
const RevisionState state = revisionState(item);
if (state != UnversionedRevision) {
++revisionedCount;
}
switch (state) {
case LocallyModifiedRevision:
case ConflictingRevision:
++editingCount;
break;
default:
break;
}
}
m_commitAction->setEnabled(editingCount > 0);
m_addAction->setEnabled(revisionedCount == 0);
m_removeAction->setEnabled(revisionedCount == itemsCount);
QList<QAction*> actions;
actions.append(m_updateAction);
actions.append(m_commitAction);
actions.append(m_addAction);
actions.append(m_removeAction);
return actions;
}
QList<QAction*> SubversionPlugin::contextMenuActions(const QString& directory)
{
m_contextDir = directory;
m_contextItems.clear();
QList<QAction*> actions;
actions.append(m_updateAction);
actions.append(m_showLocalChangesAction);
actions.append(m_commitAction);
return actions;
}
void SubversionPlugin::updateFiles()
{
execSvnCommand("update");
}
void SubversionPlugin::showLocalChanges()
{
Q_ASSERT(!m_contextDir.isEmpty());
Q_ASSERT(m_contextItems.isEmpty());
const QString command = "mkfifo /tmp/fifo; svn diff " +
KShell::quoteArg(m_contextDir) +
" > /tmp/fifo & kompare /tmp/fifo; rm /tmp/fifo";
KRun::runCommand(command, 0);
}
void SubversionPlugin::commitFiles()
{
KDialog dialog(0, Qt::Dialog);
KVBox* box = new KVBox(&dialog);
new QLabel(i18nc("@label", "Description:"), box);
QTextEdit* editor = new QTextEdit(box);
dialog.setMainWidget(box);
dialog.setCaption(i18nc("@title:window", "SVN Commit"));
dialog.setButtons(KDialog::Ok | KDialog::Cancel);
dialog.setDefaultButton(KDialog::Ok);
dialog.setButtonText(KDialog::Ok, i18nc("@action:button", "Commit"));
KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
"SvnCommitDialog");
dialog.restoreDialogSize(dialogConfig);
if (dialog.exec() == QDialog::Accepted) {
const QString description = editor->toPlainText();
execSvnCommand("commit -m " + KShell::quoteArg(description));
}
dialog.saveDialogSize(dialogConfig, KConfigBase::Persistent);
}
void SubversionPlugin::addFiles()
{
execSvnCommand("add");
}
void SubversionPlugin::removeFiles()
{
execSvnCommand("remove");
}
void SubversionPlugin::execSvnCommand(const QString& svnCommand)
{
const QString command = "svn " + svnCommand + ' ';
if (!m_contextDir.isEmpty()) {
KRun::runCommand(command + KShell::quoteArg(m_contextDir), 0);
} else {
foreach (const KFileItem& item, m_contextItems) {
KRun::runCommand(command + KShell::quoteArg(item.localPath()), 0);
}
}
}
bool SubversionPlugin::equalRevisionContent(const QString& name) const
{
QFile localFile(m_retrievalDir + '/' + name);
if (!localFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
return false;
}
QFile revisionedFile(m_retrievalDir + "/.svn/text-base/" + name + ".svn-base");
if (!revisionedFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
return false;
}
QTextStream localText(&localFile);
QTextStream revisionedText(&revisionedFile);
while (!localText.atEnd() && !revisionedText.atEnd()) {
if (localText.readLine() != revisionedText.readLine()) {
return false;
}
}
return localText.atEnd() && revisionedText.atEnd();
}
|