1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on KFilePlacesModel from kdelibs: *
5 * Copyright (C) 2007 Kevin Ottens <ervin@kde.org> *
6 * Copyright (C) 2007 David Faure <faure@kde.org> *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
22 ***************************************************************************/
24 #include "placesitemmodel.h"
27 #include <Nepomuk/ResourceManager>
28 #include <Nepomuk/Query/ComparisonTerm>
29 #include <Nepomuk/Query/LiteralTerm>
30 #include <Nepomuk/Query/Query>
31 #include <Nepomuk/Query/ResourceTypeTerm>
32 #include <Nepomuk/Vocabulary/NFO>
33 #include <Nepomuk/Vocabulary/NIE>
37 #include <KBookmarkGroup>
38 #include <KBookmarkManager>
39 #include <KComponentData>
43 #include <KStandardDirs>
45 #include "placesitem.h"
49 #include <Solid/Device>
50 #include <Solid/DeviceNotifier>
52 PlacesItemModel::PlacesItemModel(QObject
* parent
) :
53 KStandardItemModel(parent
),
54 m_nepomukRunning(false),
55 m_hiddenItemsShown(false),
60 m_systemBookmarksIndexes(),
64 m_nepomukRunning
= (Nepomuk::ResourceManager::instance()->initialized());
66 const QString file
= KStandardDirs::locateLocal("data", "kfileplaces/bookmarks.xml");
67 m_bookmarkManager
= KBookmarkManager::managerForFile(file
, "kfilePlaces");
69 createSystemBookmarks();
70 initializeAvailableDevices();
74 PlacesItemModel::~PlacesItemModel()
76 qDeleteAll(m_hiddenItems
);
77 m_hiddenItems
.clear();
80 PlacesItem
* PlacesItemModel::placesItem(int index
) const
82 return dynamic_cast<PlacesItem
*>(item(index
));
85 int PlacesItemModel::hiddenCount() const
89 foreach (const PlacesItem
* hiddenItem
, m_hiddenItems
) {
93 if (placesItem(modelIndex
)->isHidden()) {
103 void PlacesItemModel::setItemHidden(int index
, bool hide
)
105 if (index
>= 0 && index
< count()) {
106 PlacesItem
* shownItem
= placesItem(index
);
107 shownItem
->setHidden(true);
108 if (!m_hiddenItemsShown
&& hide
) {
109 const int newIndex
= hiddenIndex(index
);
110 PlacesItem
* hiddenItem
= new PlacesItem(*shownItem
);
112 m_hiddenItems
.insert(newIndex
, hiddenItem
);
114 #ifdef PLACESITEMMODEL_DEBUG
115 kDebug() << "Changed hide-state from" << index
<< "to" << hide
;
121 bool PlacesItemModel::isItemHidden(int index
) const
123 return (index
>= 0 && index
< count()) ? m_hiddenItems
[index
] != 0 : false;
126 void PlacesItemModel::setHiddenItemsShown(bool show
)
128 if (m_hiddenItemsShown
== show
) {
132 m_hiddenItemsShown
= show
;
135 // Move all items that are part of m_hiddenItems to the model.
137 for (int hiddenIndex
= 0; hiddenIndex
< m_hiddenItems
.count(); ++hiddenIndex
) {
138 if (m_hiddenItems
[hiddenIndex
]) {
139 PlacesItem
* visibleItem
= new PlacesItem(*m_hiddenItems
[hiddenIndex
]);
140 delete m_hiddenItems
[hiddenIndex
];
141 m_hiddenItems
.removeAt(hiddenIndex
);
142 insertItem(modelIndex
, visibleItem
);
143 Q_ASSERT(!m_hiddenItems
[hiddenIndex
]);
148 // Move all items of the model, where the "isHidden" property is true, to
150 Q_ASSERT(m_hiddenItems
.count() == count());
151 for (int i
= count() - 1; i
>= 0; --i
) {
152 PlacesItem
* visibleItem
= placesItem(i
);
153 if (visibleItem
->isHidden()) {
154 PlacesItem
* hiddenItem
= new PlacesItem(*visibleItem
);
156 m_hiddenItems
.insert(i
, hiddenItem
);
160 #ifdef PLACESITEMMODEL_DEBUG
161 kDebug() << "Changed visibility of hidden items";
166 bool PlacesItemModel::hiddenItemsShown() const
168 return m_hiddenItemsShown
;
171 bool PlacesItemModel::isSystemItem(int index
) const
173 if (index
>= 0 && index
< count()) {
174 const KUrl url
= placesItem(index
)->url();
175 return m_systemBookmarksIndexes
.contains(url
);
180 int PlacesItemModel::closestItem(const KUrl
& url
) const
185 for (int i
= 0; i
< count(); ++i
) {
186 const KUrl itemUrl
= placesItem(i
)->url();
187 if (itemUrl
.isParentOf(url
)) {
188 const int length
= itemUrl
.prettyUrl().length();
189 if (length
> maxLength
) {
199 QString
PlacesItemModel::groupName(const KUrl
&url
) const
201 const QString protocol
= url
.protocol();
203 if (protocol
.contains(QLatin1String("search"))) {
204 return searchForGroupName();
207 if (protocol
== QLatin1String("timeline")) {
208 return recentlyAccessedGroupName();
211 return placesGroupName();
214 QAction
* PlacesItemModel::ejectAction(int index
) const
216 const PlacesItem
* item
= placesItem(index
);
217 if (item
&& item
->device().is
<Solid::OpticalDisc
>()) {
218 return new QAction(KIcon("media-eject"), i18nc("@item", "Eject '%1'", item
->text()), 0);
224 QAction
* PlacesItemModel::tearDownAction(int index
) const
226 // TODO: This is a dummy-implementation to have at least all
227 // translation-strings as part of the code before the freeze
233 text
= i18nc("@item", "Release '%1'", label
);
236 text
= i18nc("@item", "Safely Remove '%1'", label
);
237 iconName
= "media-eject";
240 text
= i18nc("@item", "Unmount '%1'", label
);
241 iconName
= "media-eject";
247 //return new QAction(KIcon(iconName), text, 0);
251 void PlacesItemModel::onItemInserted(int index
)
255 while (hiddenIndex
< m_hiddenItems
.count()) {
256 if (!m_hiddenItems
[hiddenIndex
]) {
258 if (modelIndex
+ 1 == index
) {
265 m_hiddenItems
.insert(hiddenIndex
, 0);
267 #ifdef PLACESITEMMODEL_DEBUG
268 kDebug() << "Inserted item" << index
;
273 void PlacesItemModel::onItemRemoved(int index
)
275 const int removeIndex
= hiddenIndex(index
);
276 Q_ASSERT(!m_hiddenItems
[removeIndex
]);
277 m_hiddenItems
.removeAt(removeIndex
);
278 #ifdef PLACESITEMMODEL_DEBUG
279 kDebug() << "Removed item" << index
;
284 void PlacesItemModel::slotDeviceAdded(const QString
& udi
)
289 void PlacesItemModel::slotDeviceRemoved(const QString
& udi
)
294 void PlacesItemModel::loadBookmarks()
296 KBookmarkGroup root
= m_bookmarkManager
->root();
297 KBookmark bookmark
= root
.first();
298 QSet
<QString
> devices
= m_availableDevices
;
300 QSet
<KUrl
> missingSystemBookmarks
;
301 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
302 missingSystemBookmarks
.insert(data
.url
);
305 // The bookmarks might have a mixed order of "places" and "devices". In
306 // Dolphin's places panel the devices should always be appended as last
308 QList
<PlacesItem
*> placesItems
;
309 QList
<PlacesItem
*> devicesItems
;
311 while (!bookmark
.isNull()) {
312 const QString udi
= bookmark
.metaDataItem("UDI");
313 const KUrl url
= bookmark
.url();
314 const QString appName
= bookmark
.metaDataItem("OnlyInApp");
315 const bool deviceAvailable
= devices
.remove(udi
);
317 const bool allowedHere
= (appName
.isEmpty() || appName
== KGlobal::mainComponent().componentName())
318 && (m_nepomukRunning
|| url
.protocol() != QLatin1String("timeline"));
320 if ((udi
.isEmpty() && allowedHere
) || deviceAvailable
) {
321 PlacesItem
* item
= new PlacesItem(bookmark
);
322 if (deviceAvailable
) {
323 devicesItems
.append(item
);
325 placesItems
.append(item
);
327 if (missingSystemBookmarks
.contains(url
)) {
328 missingSystemBookmarks
.remove(url
);
330 // Apply the translated text to the system bookmarks, otherwise an outdated
331 // translation might be shown.
332 const int index
= m_systemBookmarksIndexes
.value(url
);
333 item
->setText(m_systemBookmarks
[index
].text
);
335 // The system bookmarks don't contain "real" queries stored as URLs, so
336 // they must be translated first.
337 item
->setUrl(translatedSystemBookmarkUrl(url
));
342 bookmark
= root
.next(bookmark
);
345 addItems(placesItems
);
347 if (!missingSystemBookmarks
.isEmpty()) {
348 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
349 if (missingSystemBookmarks
.contains(data
.url
)) {
350 PlacesItem
* item
= new PlacesItem();
351 item
->setIcon(data
.icon
);
352 item
->setText(data
.text
);
353 item
->setUrl(translatedSystemBookmarkUrl(data
.url
));
354 item
->setGroup(data
.group
);
360 // Create items for devices that have not stored as bookmark yet
361 foreach (const QString
& udi
, devices
) {
362 PlacesItem
* item
= new PlacesItem(udi
);
363 devicesItems
.append(item
);
366 addItems(devicesItems
);
368 #ifdef PLACESITEMMODEL_DEBUG
369 kDebug() << "Loaded bookmarks";
374 void PlacesItemModel::addItems(const QList
<PlacesItem
*>& items
)
376 foreach (PlacesItem
* item
, items
) {
377 if (item
->isHidden()) {
378 m_hiddenItems
.append(item
);
385 void PlacesItemModel::createSystemBookmarks()
387 Q_ASSERT(m_systemBookmarks
.isEmpty());
388 Q_ASSERT(m_systemBookmarksIndexes
.isEmpty());
390 const QString placesGroup
= placesGroupName();
391 const QString recentlyAccessedGroup
= recentlyAccessedGroupName();
392 const QString searchForGroup
= searchForGroupName();
393 const QString timeLineIcon
= "package_utility_time"; // TODO: Ask the Oxygen team to create
394 // a custom icon for the timeline-protocol
396 m_systemBookmarks
.append(SystemBookmarkData(KUrl(KUser().homeDir()),
398 i18nc("@item", "Home"),
400 m_systemBookmarks
.append(SystemBookmarkData(KUrl("remote:/"),
402 i18nc("@item", "Network"),
404 m_systemBookmarks
.append(SystemBookmarkData(KUrl("/"),
406 i18nc("@item", "Root"),
408 m_systemBookmarks
.append(SystemBookmarkData(KUrl("trash:/"),
410 i18nc("@item", "Trash"),
413 if (m_nepomukRunning
) {
414 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/today"),
416 i18nc("@item Recently Accessed", "Today"),
417 recentlyAccessedGroup
));
418 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/yesterday"),
420 i18nc("@item Recently Accessed", "Yesterday"),
421 recentlyAccessedGroup
));
422 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/thismonth"),
424 i18nc("@item Recently Accessed", "This Month"),
425 recentlyAccessedGroup
));
426 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/lastmonth"),
428 i18nc("@item Recently Accessed", "Last Month"),
429 recentlyAccessedGroup
));
430 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/documents"),
432 i18nc("@item Commonly Accessed", "Documents"),
434 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/images"),
436 i18nc("@item Commonly Accessed", "Images"),
438 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/audio"),
440 i18nc("@item Commonly Accessed", "Audio"),
442 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/videos"),
444 i18nc("@item Commonly Accessed", "Videos"),
448 for (int i
= 0; i
< m_systemBookmarks
.count(); ++i
) {
449 const KUrl url
= translatedSystemBookmarkUrl(m_systemBookmarks
[i
].url
);
450 m_systemBookmarksIndexes
.insert(url
, i
);
454 void PlacesItemModel::initializeAvailableDevices()
456 m_predicate
= Solid::Predicate::fromString(
457 "[[[[ StorageVolume.ignored == false AND [ StorageVolume.usage == 'FileSystem' OR StorageVolume.usage == 'Encrypted' ]]"
459 "[ IS StorageAccess AND StorageDrive.driveType == 'Floppy' ]]"
461 "OpticalDisc.availableContent & 'Audio' ]"
463 "StorageAccess.ignored == false ]");
464 Q_ASSERT(m_predicate
.isValid());
466 Solid::DeviceNotifier
* notifier
= Solid::DeviceNotifier::instance();
467 connect(notifier
, SIGNAL(deviceAdded(QString
)), this, SLOT(slotDeviceAdded(QString
)));
468 connect(notifier
, SIGNAL(deviceRemoved(QString
)), this, SLOT(slotDeviceRemoved(QString
)));
470 const QList
<Solid::Device
>& deviceList
= Solid::Device::listFromQuery(m_predicate
);
471 foreach(const Solid::Device
& device
, deviceList
) {
472 m_availableDevices
<< device
.udi();
476 int PlacesItemModel::hiddenIndex(int index
) const
479 int visibleItemIndex
= 0;
480 while (hiddenIndex
< m_hiddenItems
.count()) {
481 if (!m_hiddenItems
[hiddenIndex
]) {
482 if (visibleItemIndex
== index
) {
490 return hiddenIndex
>= m_hiddenItems
.count() ? -1 : hiddenIndex
;
493 QString
PlacesItemModel::placesGroupName()
495 return i18nc("@item", "Places");
498 QString
PlacesItemModel::recentlyAccessedGroupName()
500 return i18nc("@item", "Recently Accessed");
503 QString
PlacesItemModel::searchForGroupName()
505 return i18nc("@item", "Search For");
508 KUrl
PlacesItemModel::translatedSystemBookmarkUrl(const KUrl
& url
)
510 KUrl translatedUrl
= url
;
511 if (url
.protocol() == QLatin1String("timeline")) {
512 translatedUrl
= createTimelineUrl(url
);
513 } else if (url
.protocol() == QLatin1String("search")) {
514 translatedUrl
= createSearchUrl(url
);
517 return translatedUrl
;
520 KUrl
PlacesItemModel::createTimelineUrl(const KUrl
& url
)
522 // TODO: Clarify with the Nepomuk-team whether it makes sense
523 // provide default-timeline-URLs like 'yesterday', 'this month'
527 const QString path
= url
.pathOrUrl();
528 if (path
.endsWith("yesterday")) {
529 const QDate date
= QDate::currentDate().addDays(-1);
530 const int year
= date
.year();
531 const int month
= date
.month();
532 const int day
= date
.day();
533 timelineUrl
= "timeline:/" + timelineDateString(year
, month
) +
534 '/' + timelineDateString(year
, month
, day
);
535 } else if (path
.endsWith("thismonth")) {
536 const QDate date
= QDate::currentDate();
537 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
538 } else if (path
.endsWith("lastmonth")) {
539 const QDate date
= QDate::currentDate().addMonths(-1);
540 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
542 Q_ASSERT(path
.endsWith("today"));
549 QString
PlacesItemModel::timelineDateString(int year
, int month
, int day
)
551 QString date
= QString::number(year
) + '-';
555 date
+= QString::number(month
);
562 date
+= QString::number(day
);
568 KUrl
PlacesItemModel::createSearchUrl(const KUrl
& url
)
573 const QString path
= url
.pathOrUrl();
574 if (path
.endsWith("documents")) {
575 searchUrl
= searchUrlForTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Document()));
576 } else if (path
.endsWith("images")) {
577 searchUrl
= searchUrlForTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Image()));
578 } else if (path
.endsWith("audio")) {
579 searchUrl
= searchUrlForTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
580 Nepomuk::Query::LiteralTerm("audio")));
581 } else if (path
.endsWith("videos")) {
582 searchUrl
= searchUrlForTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
583 Nepomuk::Query::LiteralTerm("video")));
595 KUrl
PlacesItemModel::searchUrlForTerm(const Nepomuk::Query::Term
& term
)
597 const Nepomuk::Query::Query
query(term
);
598 return query
.toSearchUrl();
602 #ifdef PLACESITEMMODEL_DEBUG
603 void PlacesItemModel::showModelState()
605 kDebug() << "hidden-index model-index text";
607 for (int i
= 0; i
< m_hiddenItems
.count(); ++i
) {
608 if (m_hiddenItems
[i
]) {
609 kDebug() << i
<< "(Hidden) " << " " << m_hiddenItems
[i
]->dataValue("text").toString();
611 kDebug() << i
<< " " << j
<< " " << item(j
)->dataValue("text").toString();
618 #include "placesitemmodel.moc"