1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/***************************************************************************
* Copyright (C) 2012 by Peter Penz <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
#ifndef PLACESITEMMODEL_H
#define PLACESITEMMODEL_H
#include <config-nepomuk.h>
#include <kitemviews/kstandarditemmodel.h>
#include <KUrl>
#include <QHash>
#include <QList>
#include <QSet>
#include <Solid/Predicate>
#include <Solid/StorageAccess>
class KBookmarkManager;
class PlacesItem;
class QAction;
class QTimer;
// #define PLACESITEMMODEL_DEBUG
/**
* @brief Model for maintaining the bookmarks of the places panel.
*
* It is compatible to the KFilePlacesModel from kdelibs but adds
* the ability to have groups for places.
*/
class PlacesItemModel: public KStandardItemModel
{
Q_OBJECT
public:
explicit PlacesItemModel(QObject* parent = 0);
virtual ~PlacesItemModel();
PlacesItem* placesItem(int index) const;
void setHiddenItemsShown(bool show);
bool hiddenItemsShown() const;
int hiddenCount() const;
/**
* Search the item which is equal to the URL or at least
* is a parent URL. If there are more than one possible
* candidates, return the item which covers the biggest
* range of the URL. -1 is returned if no closest item
* could be found.
*/
int closestItem(const KUrl& url) const;
/**
* @return Name of the group where the item with the URL
* \a URL belongs to.
*/
QString groupName(const KUrl& url) const;
QAction* ejectAction(int index) const;
QAction* teardownAction(int index) const;
void requestEject(int index);
void requestTeardown(int index);
signals:
void errorMessage(const QString& message);
protected:
virtual void onItemInserted(int index);
virtual void onItemRemoved(int index);
virtual void onItemChanged(int index, const QSet<QByteArray>& changedRoles);
private slots:
void slotDeviceAdded(const QString& udi);
void slotDeviceRemoved(const QString& udi);
void slotStorageTeardownDone(Solid::ErrorType error, const QVariant& errorData);
void removeHiddenItem();
void saveBookmarks();
private:
void loadBookmarks();
/**
* Helper method for loadBookmarks(): Adds the items
* to the model if the "isHidden"-property is false,
* otherwise the items get added to m_hiddenItems.
*/
void addItems(const QList<PlacesItem*>& items);
/**
* Creates system bookmarks that are shown per default and can
* only be hidden but not removed. The result will be stored
* in m_systemBookmarks.
*/
void createSystemBookmarks();
void initializeAvailableDevices();
/**
* @param index Item index related to the model.
* @return Corresponding item index related to m_hiddenItems.
*/
int hiddenIndex(int index) const;
static QString placesGroupName();
static QString recentlyAccessedGroupName();
static QString searchForGroupName();
#ifdef PLACESITEMMODEL_DEBUG
void showModelState();
#endif
private:
bool m_nepomukRunning;
bool m_hiddenItemsShown;
QSet<QString> m_availableDevices;
Solid::Predicate m_predicate;
KBookmarkManager* m_bookmarkManager;
struct SystemBookmarkData
{
SystemBookmarkData(const KUrl& url,
const QString& icon,
const QString& text,
const QString& group) :
url(url), icon(icon), text(text), group(group) {}
KUrl url;
QString icon;
QString text;
QString group;
};
QList<SystemBookmarkData> m_systemBookmarks;
QHash<KUrl, int> m_systemBookmarksIndexes;
QList<PlacesItem*> m_hiddenItems;
// Index of the hidden item that should be removed in
// removeHiddenItem(). The removing must be done
// asynchronously as in the scope of onItemChanged()
// removing an item is not allowed.
int m_hiddenItemToRemove;
QTimer* m_saveBookmarksTimer;
};
#endif
|