]> cloud.milkyroute.net Git - dolphin.git/blob - src/revisioncontrolobserver.cpp
Enable Dolphin to show the revision states of files that are under revision control...
[dolphin.git] / src / revisioncontrolobserver.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 "revisioncontrolobserver.h"
21
22 #include "dolphinmodel.h"
23 #include "revisioncontrolplugin.h"
24
25 #include <kdirlister.h>
26
27 #include <QAbstractProxyModel>
28 #include <QAbstractItemView>
29 #include <QTimer>
30
31 RevisionControlObserver::RevisionControlObserver(QAbstractItemView* view) :
32 QObject(view),
33 m_view(view),
34 m_dirLister(0),
35 m_dolphinModel(0),
36 m_dirVerificationTimer(0),
37 m_plugin(0)
38 {
39 Q_ASSERT(view != 0);
40
41 QAbstractProxyModel* proxyModel = qobject_cast<QAbstractProxyModel*>(view->model());
42 m_dolphinModel = (proxyModel == 0) ?
43 qobject_cast<DolphinModel*>(view->model()) :
44 qobject_cast<DolphinModel*>(proxyModel->sourceModel());
45 if (m_dolphinModel != 0) {
46 m_dirLister = m_dolphinModel->dirLister();
47 connect(m_dirLister, SIGNAL(completed()),
48 this, SLOT(delayedDirectoryVerification()));
49 // TODO:
50 // connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
51 // this, SLOT(refreshItems()));
52
53 // The verification timer specifies the timeout until the shown directory
54 // is checked whether it is versioned. Per default it is assumed that users
55 // don't iterate through versioned directories and a high timeout is used
56 // The timeout will be decreased as soon as a versioned directory has been
57 // found (see verifyDirectory()).
58 m_dirVerificationTimer = new QTimer(this);
59 m_dirVerificationTimer->setSingleShot(true);
60 m_dirVerificationTimer->setInterval(500);
61 connect(m_dirVerificationTimer, SIGNAL(timeout()),
62 this, SLOT(verifyDirectory()));
63 }
64 }
65
66 RevisionControlObserver::~RevisionControlObserver()
67 {
68 delete m_plugin;
69 m_plugin = 0;
70 }
71
72 void RevisionControlObserver::delayedDirectoryVerification()
73 {
74 m_dirVerificationTimer->start();
75 }
76
77 void RevisionControlObserver::verifyDirectory()
78 {
79 KUrl revisionControlUrl = m_dirLister->url();
80 if (!revisionControlUrl.isLocalFile()) {
81 return;
82 }
83
84 if (m_plugin == 0) {
85 // TODO: just for testing purposes. A plugin approach will be used later.
86 m_plugin = new SubversionPlugin();
87 }
88
89 revisionControlUrl.addPath(m_plugin->fileName());
90 KFileItem item = m_dirLister->findByUrl(revisionControlUrl);
91 if (item.isNull()) {
92 // The directory is not versioned. Reset the verification timer to a higher
93 // value, so that browsing through non-versioned directories is not slown down
94 // by an immediate verification.
95 m_dirVerificationTimer->setInterval(500);
96 } else {
97 // The directory is versioned. Assume that the user will further browse through
98 // versioned directories and decrease the verification timer.
99 m_dirVerificationTimer->setInterval(100);
100 updateItemStates();
101 }
102 }
103
104 void RevisionControlObserver::updateItemStates()
105 {
106 Q_ASSERT(m_plugin != 0);
107 const KUrl directory = m_dirLister->url();
108 if (!m_plugin->beginRetrieval(directory.toLocalFile(KUrl::AddTrailingSlash))) {
109 return;
110 }
111
112 const int rowCount = m_dolphinModel->rowCount();
113 for (int row = 0; row < rowCount; ++row) {
114 const QModelIndex index = m_dolphinModel->index(row, DolphinModel::Revision);
115 const KFileItem item = m_dolphinModel->itemForIndex(index);
116 const RevisionControlPlugin::RevisionState revision = m_plugin->revisionState(item.name());
117 m_dolphinModel->setData(index, QVariant(static_cast<int>(revision)), Qt::DecorationRole);
118 }
119 m_view->viewport()->repaint(); // TODO: this should not be necessary, as DolphinModel::setData() calls dataChanged()
120
121 m_plugin->endRetrieval();
122 }
123
124 #include "revisioncontrolobserver.moc"