1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 ***************************************************************************/
20 #include "revisioncontrolplugin.h"
22 #include <kfileitem.h>
25 #include <QTextStream>
27 RevisionControlPlugin::RevisionControlPlugin()
31 RevisionControlPlugin::~RevisionControlPlugin()
35 #include "revisioncontrolplugin.moc"
37 // ----------------------------------------------------------------------------
39 SubversionPlugin::SubversionPlugin() :
45 SubversionPlugin::~SubversionPlugin()
49 QString
SubversionPlugin::fileName() const
54 bool SubversionPlugin::beginRetrieval(const QString
& directory
)
56 Q_ASSERT(directory
.endsWith('/'));
57 m_directory
= directory
;
58 const QString path
= directory
+ ".svn/text-base/";
61 const QFileInfoList fileInfoList
= dir
.entryInfoList();
62 const int size
= fileInfoList
.size();
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()) {
71 info
.size
= fileInfoList
.at(i
).size();
72 info
.timeStamp
= fileInfoList
.at(i
).lastModified();
73 m_revisionInfoHash
.insert(fileName
, info
);
79 void SubversionPlugin::endRetrieval()
83 RevisionControlPlugin::RevisionState
SubversionPlugin::revisionState(const KFileItem
& item
)
85 const QString name
= item
.name();
87 QFile
file(m_directory
+ name
+ "/.svn");
88 if (file
.open(QIODevice::ReadOnly
)) {
91 return RevisionControlPlugin::LatestRevision
;
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
;
98 if (localTimeStamp
> versionedTimeStamp
) {
99 if ((info
.size
!= item
.size()) || !equalRevisionContent(item
.name())) {
100 return RevisionControlPlugin::EditingRevision
;
102 } else if (localTimeStamp
< versionedTimeStamp
) {
103 if ((info
.size
!= item
.size()) || !equalRevisionContent(item
.name())) {
104 return RevisionControlPlugin::UpdateRequiredRevision
;
107 return RevisionControlPlugin::LatestRevision
;
110 return RevisionControlPlugin::LocalRevision
;
113 QList
<QAction
*> SubversionPlugin::contextMenuActions(const KFileItemList
& items
) const
117 return QList
<QAction
*>();
120 bool SubversionPlugin::equalRevisionContent(const QString
& name
) const
122 QFile
localFile(m_directory
+ '/' + name
);
123 if (!localFile
.open(QIODevice::ReadOnly
| QIODevice::Text
)) {
127 QFile
revisionedFile(m_directory
+ "/.svn/text-base/" + name
+ ".svn-base");
128 if (!revisionedFile
.open(QIODevice::ReadOnly
| QIODevice::Text
)) {
132 QTextStream
localText(&localFile
);
133 QTextStream
revisionedText(&revisionedFile
);
134 while (!localText
.atEnd() && !revisionedText
.atEnd()) {
135 if (localText
.readLine() != revisionedText
.readLine()) {
140 return localText
.atEnd() && revisionedText
.atEnd();