]> cloud.milkyroute.net Git - dolphin.git/blob - src/revisioncontrolplugin.cpp
- Documentation updates.
[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 <kfileitem.h>
23 #include <QDir>
24 #include <QString>
25 #include <QTextStream>
26
27 RevisionControlPlugin::RevisionControlPlugin()
28 {
29 }
30
31 RevisionControlPlugin::~RevisionControlPlugin()
32 {
33 }
34
35 #include "revisioncontrolplugin.moc"
36
37 // ----------------------------------------------------------------------------
38
39 SubversionPlugin::SubversionPlugin() :
40 m_directory(),
41 m_revisionInfoHash()
42 {
43 }
44
45 SubversionPlugin::~SubversionPlugin()
46 {
47 }
48
49 QString SubversionPlugin::fileName() const
50 {
51 return ".svn";
52 }
53
54 bool SubversionPlugin::beginRetrieval(const QString& directory)
55 {
56 Q_ASSERT(directory.endsWith('/'));
57 m_directory = directory;
58 const QString path = directory + ".svn/text-base/";
59
60 QDir dir(path);
61 const QFileInfoList fileInfoList = dir.entryInfoList();
62 const int size = fileInfoList.size();
63 QString fileName;
64 for (int i = 0; i < size; ++i) {
65 fileName = fileInfoList.at(i).fileName();
66 // Remove the ".svn-base" postfix to be able to compare the filenames
67 // in a fast way in SubversionPlugin::revisionState().
68 fileName.chop(sizeof(".svn-base") / sizeof(char) - 1);
69 if (!fileName.isEmpty()) {
70 RevisionInfo info;
71 info.size = fileInfoList.at(i).size();
72 info.timeStamp = fileInfoList.at(i).lastModified();
73 m_revisionInfoHash.insert(fileName, info);
74 }
75 }
76 return size > 0;
77 }
78
79 void SubversionPlugin::endRetrieval()
80 {
81 }
82
83 RevisionControlPlugin::RevisionState SubversionPlugin::revisionState(const KFileItem& item)
84 {
85 const QString name = item.name();
86 if (item.isDir()) {
87 QFile file(m_directory + name + "/.svn");
88 if (file.open(QIODevice::ReadOnly)) {
89 file.close();
90 // TODO...
91 return RevisionControlPlugin::LatestRevision;
92 }
93 } else if (m_revisionInfoHash.contains(name)) {
94 const RevisionInfo info = m_revisionInfoHash.value(item.name());
95 const QDateTime localTimeStamp = item.time(KFileItem::ModificationTime).dateTime();
96 const QDateTime versionedTimeStamp = info.timeStamp;
97
98 if (localTimeStamp > versionedTimeStamp) {
99 if ((info.size != item.size()) || !equalRevisionContent(item.name())) {
100 return RevisionControlPlugin::EditingRevision;
101 }
102 } else if (localTimeStamp < versionedTimeStamp) {
103 if ((info.size != item.size()) || !equalRevisionContent(item.name())) {
104 return RevisionControlPlugin::UpdateRequiredRevision;
105 }
106 }
107 return RevisionControlPlugin::LatestRevision;
108 }
109
110 return RevisionControlPlugin::LocalRevision;
111 }
112
113 QList<QAction*> SubversionPlugin::contextMenuActions(const KFileItemList& items) const
114 {
115 Q_UNUSED(items);
116 // TODO...
117 return QList<QAction*>();
118 }
119
120 bool SubversionPlugin::equalRevisionContent(const QString& name) const
121 {
122 QFile localFile(m_directory + '/' + name);
123 if (!localFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
124 return false;
125 }
126
127 QFile revisionedFile(m_directory + "/.svn/text-base/" + name + ".svn-base");
128 if (!revisionedFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
129 return false;
130 }
131
132 QTextStream localText(&localFile);
133 QTextStream revisionedText(&revisionedFile);
134 while (!localText.atEnd() && !revisionedText.atEnd()) {
135 if (localText.readLine() != revisionedText.readLine()) {
136 return false;
137 }
138 }
139
140 return localText.atEnd() && revisionedText.atEnd();
141 }
142