]> cloud.milkyroute.net Git - dolphin.git/blob - src/revisioncontrolplugin.cpp
The revision control plugin must be aware on which directory the context-menu-actions...
[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 <kaction.h>
23 #include <kicon.h>
24 #include <klocale.h>
25 #include <kfileitem.h>
26 #include <QDir>
27 #include <QString>
28 #include <QTextStream>
29
30 RevisionControlPlugin::RevisionControlPlugin()
31 {
32 }
33
34 RevisionControlPlugin::~RevisionControlPlugin()
35 {
36 }
37
38 #include "revisioncontrolplugin.moc"
39
40 // ----------------------------------------------------------------------------
41
42 SubversionPlugin::SubversionPlugin() :
43 m_directory(),
44 m_revisionInfoHash(),
45 m_updateAction(0),
46 m_commitAction(0),
47 m_addAction(0),
48 m_removeAction(0)
49 {
50 m_updateAction = new KAction(this);
51 m_updateAction->setIcon(KIcon("view-refresh"));
52 m_updateAction->setText(i18nc("@item:inmenu", "SVN Update"));
53
54 m_commitAction = new KAction(this);
55 m_commitAction->setText(i18nc("@item:inmenu", "SVN Commit..."));
56
57 m_addAction = new KAction(this);
58 m_addAction->setIcon(KIcon("list-add"));
59 m_addAction->setText(i18nc("@item:inmenu", "SVN Add"));
60
61 m_removeAction = new KAction(this);
62 m_removeAction->setIcon(KIcon("list-remove"));
63 m_removeAction->setText(i18nc("@item:inmenu", "SVN Delete"));
64 }
65
66 SubversionPlugin::~SubversionPlugin()
67 {
68 }
69
70 QString SubversionPlugin::fileName() const
71 {
72 return ".svn";
73 }
74
75 bool SubversionPlugin::beginRetrieval(const QString& directory)
76 {
77 Q_ASSERT(directory.endsWith('/'));
78 m_directory = directory;
79 const QString path = directory + ".svn/text-base/";
80
81 QDir dir(path);
82 const QFileInfoList fileInfoList = dir.entryInfoList();
83 const int size = fileInfoList.size();
84 QString fileName;
85 for (int i = 0; i < size; ++i) {
86 fileName = fileInfoList.at(i).fileName();
87 // Remove the ".svn-base" postfix to be able to compare the filenames
88 // in a fast way in SubversionPlugin::revisionState().
89 fileName.chop(sizeof(".svn-base") / sizeof(char) - 1);
90 if (!fileName.isEmpty()) {
91 RevisionInfo info;
92 info.size = fileInfoList.at(i).size();
93 info.timeStamp = fileInfoList.at(i).lastModified();
94 m_revisionInfoHash.insert(fileName, info);
95 }
96 }
97 return size > 0;
98 }
99
100 void SubversionPlugin::endRetrieval()
101 {
102 }
103
104 RevisionControlPlugin::RevisionState SubversionPlugin::revisionState(const KFileItem& item)
105 {
106 const QString name = item.name();
107 if (item.isDir()) {
108 QFile file(m_directory + name + "/.svn");
109 if (file.open(QIODevice::ReadOnly)) {
110 file.close();
111 // TODO...
112 return RevisionControlPlugin::LatestRevision;
113 }
114 } else if (m_revisionInfoHash.contains(name)) {
115 const RevisionInfo info = m_revisionInfoHash.value(item.name());
116 const QDateTime localTimeStamp = item.time(KFileItem::ModificationTime).dateTime();
117 const QDateTime versionedTimeStamp = info.timeStamp;
118
119 if (localTimeStamp > versionedTimeStamp) {
120 if ((info.size != item.size()) || !equalRevisionContent(item.name())) {
121 return RevisionControlPlugin::EditingRevision;
122 }
123 } else if (localTimeStamp < versionedTimeStamp) {
124 if ((info.size != item.size()) || !equalRevisionContent(item.name())) {
125 return RevisionControlPlugin::UpdateRequiredRevision;
126 }
127 }
128 return RevisionControlPlugin::LatestRevision;
129 }
130
131 return RevisionControlPlugin::LocalRevision;
132 }
133
134 QList<QAction*> SubversionPlugin::contextMenuActions(const KFileItemList& items) const
135 {
136 Q_UNUSED(items);
137
138 QList<QAction*> actions;
139 actions.append(m_updateAction);
140 actions.append(m_commitAction);
141 actions.append(m_addAction);
142 actions.append(m_removeAction);
143 return actions;
144 }
145
146 QList<QAction*> SubversionPlugin::contextMenuActions(const QString& directory) const
147 {
148 Q_UNUSED(directory);
149
150 QList<QAction*> actions;
151 actions.append(m_updateAction);
152 actions.append(m_commitAction);
153 return actions;
154 }
155
156 bool SubversionPlugin::equalRevisionContent(const QString& name) const
157 {
158 QFile localFile(m_directory + '/' + name);
159 if (!localFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
160 return false;
161 }
162
163 QFile revisionedFile(m_directory + "/.svn/text-base/" + name + ".svn-base");
164 if (!revisionedFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
165 return false;
166 }
167
168 QTextStream localText(&localFile);
169 QTextStream revisionedText(&revisionedFile);
170 while (!localText.atEnd() && !revisionedText.atEnd()) {
171 if (localText.readLine() != revisionedText.readLine()) {
172 return false;
173 }
174 }
175
176 return localText.atEnd() && revisionedText.atEnd();
177 }
178