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"
25 #include <kfileitem.h>
28 #include <QTextStream>
30 RevisionControlPlugin::RevisionControlPlugin()
34 RevisionControlPlugin::~RevisionControlPlugin()
38 #include "revisioncontrolplugin.moc"
40 // ----------------------------------------------------------------------------
42 SubversionPlugin::SubversionPlugin() :
50 m_updateAction
= new KAction(this);
51 m_updateAction
->setIcon(KIcon("view-refresh"));
52 m_updateAction
->setText(i18nc("@item:inmenu", "SVN Update"));
54 m_commitAction
= new KAction(this);
55 m_commitAction
->setText(i18nc("@item:inmenu", "SVN Commit..."));
57 m_addAction
= new KAction(this);
58 m_addAction
->setIcon(KIcon("list-add"));
59 m_addAction
->setText(i18nc("@item:inmenu", "SVN Add"));
61 m_removeAction
= new KAction(this);
62 m_removeAction
->setIcon(KIcon("list-remove"));
63 m_removeAction
->setText(i18nc("@item:inmenu", "SVN Delete"));
66 SubversionPlugin::~SubversionPlugin()
70 QString
SubversionPlugin::fileName() const
75 bool SubversionPlugin::beginRetrieval(const QString
& directory
)
77 Q_ASSERT(directory
.endsWith('/'));
78 m_directory
= directory
;
79 const QString path
= directory
+ ".svn/text-base/";
82 const QFileInfoList fileInfoList
= dir
.entryInfoList();
83 const int size
= fileInfoList
.size();
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()) {
92 info
.size
= fileInfoList
.at(i
).size();
93 info
.timeStamp
= fileInfoList
.at(i
).lastModified();
94 m_revisionInfoHash
.insert(fileName
, info
);
100 void SubversionPlugin::endRetrieval()
104 RevisionControlPlugin::RevisionState
SubversionPlugin::revisionState(const KFileItem
& item
)
106 const QString name
= item
.name();
108 QFile
file(m_directory
+ name
+ "/.svn");
109 if (file
.open(QIODevice::ReadOnly
)) {
112 return RevisionControlPlugin::LatestRevision
;
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
;
119 if (localTimeStamp
> versionedTimeStamp
) {
120 if ((info
.size
!= item
.size()) || !equalRevisionContent(item
.name())) {
121 return RevisionControlPlugin::EditingRevision
;
123 } else if (localTimeStamp
< versionedTimeStamp
) {
124 if ((info
.size
!= item
.size()) || !equalRevisionContent(item
.name())) {
125 return RevisionControlPlugin::UpdateRequiredRevision
;
128 return RevisionControlPlugin::LatestRevision
;
131 return RevisionControlPlugin::LocalRevision
;
134 QList
<QAction
*> SubversionPlugin::contextMenuActions(const KFileItemList
& items
) const
138 QList
<QAction
*> actions
;
139 actions
.append(m_updateAction
);
140 actions
.append(m_commitAction
);
141 actions
.append(m_addAction
);
142 actions
.append(m_removeAction
);
146 QList
<QAction
*> SubversionPlugin::contextMenuActions(const QString
& directory
) const
150 QList
<QAction
*> actions
;
151 actions
.append(m_updateAction
);
152 actions
.append(m_commitAction
);
156 bool SubversionPlugin::equalRevisionContent(const QString
& name
) const
158 QFile
localFile(m_directory
+ '/' + name
);
159 if (!localFile
.open(QIODevice::ReadOnly
| QIODevice::Text
)) {
163 QFile
revisionedFile(m_directory
+ "/.svn/text-base/" + name
+ ".svn-base");
164 if (!revisionedFile
.open(QIODevice::ReadOnly
| QIODevice::Text
)) {
168 QTextStream
localText(&localFile
);
169 QTextStream
revisionedText(&revisionedFile
);
170 while (!localText
.atEnd() && !revisionedText
.atEnd()) {
171 if (localText
.readLine() != revisionedText
.readLine()) {
176 return localText
.atEnd() && revisionedText
.atEnd();