]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
5ea4b971cb9f2650c97eef49fa44c914fb910fb2
[dolphin.git] / src / panels / places / placespanel.cpp
1 /***************************************************************************
2 * Copyright (C) 2008-2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on KFilePlacesModel from kdelibs: *
5 * Copyright (C) 2007 Kevin Ottens <ervin@kde.org> *
6 * Copyright (C) 2007 David Faure <faure@kde.org> *
7 * *
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. *
12 * *
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. *
17 * *
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 ***************************************************************************/
23
24 #include "placespanel.h"
25
26 #include <KBookmark>
27 #include <KBookmarkGroup>
28 #include <KBookmarkManager>
29 #include <KComponentData>
30 #include <KDebug>
31 #include <KIcon>
32 #include <kitemviews/kitemlistcontainer.h>
33 #include <kitemviews/kitemlistcontroller.h>
34 #include <kitemviews/kstandarditem.h>
35 #include <kitemviews/kstandarditemlistview.h>
36 #include <kitemviews/kstandarditemmodel.h>
37 #include <KStandardDirs>
38 #include <views/draganddrophelper.h>
39 #include <QVBoxLayout>
40 #include <QShowEvent>
41
42 PlacesPanel::PlacesPanel(QWidget* parent) :
43 Panel(parent),
44 m_controller(0),
45 m_model(0),
46 m_availableDevices(),
47 m_bookmarkManager(0)
48 {
49 }
50
51 PlacesPanel::~PlacesPanel()
52 {
53 }
54
55 bool PlacesPanel::urlChanged()
56 {
57 return true;
58 }
59
60 void PlacesPanel::showEvent(QShowEvent* event)
61 {
62 if (event->spontaneous()) {
63 Panel::showEvent(event);
64 return;
65 }
66
67 if (!m_controller) {
68 // Postpone the creating of the controller to the first show event.
69 // This assures that no performance and memory overhead is given when the folders panel is not
70 // used at all and stays invisible.
71 const QString file = KStandardDirs::locateLocal("data", "kfileplaces/bookmarks.xml");
72 m_bookmarkManager = KBookmarkManager::managerForFile(file, "kfileplaces");
73 m_model = new KStandardItemModel(this);
74 loadBookmarks();
75
76 KStandardItemListView* view = new KStandardItemListView();
77
78 m_controller = new KItemListController(m_model, view, this);
79 m_controller->setSelectionBehavior(KItemListController::SingleSelection);
80 connect(m_controller, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
81 connect(m_controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
82
83 KItemListContainer* container = new KItemListContainer(m_controller, this);
84 container->setEnabledFrame(false);
85
86 QVBoxLayout* layout = new QVBoxLayout(this);
87 layout->setMargin(0);
88 layout->addWidget(container);
89 }
90
91 Panel::showEvent(event);
92 }
93
94 void PlacesPanel::slotItemActivated(int index)
95 {
96 const KStandardItem* item = m_model->item(index);
97 if (item) {
98 const KUrl url = item->dataValue("url").value<KUrl>();
99 emit placeActivated(url);
100 }
101 }
102
103 void PlacesPanel::slotItemMiddleClicked(int index)
104 {
105 const KStandardItem* item = m_model->item(index);
106 if (item) {
107 const KUrl url = item->dataValue("url").value<KUrl>();
108 emit placeMiddleClicked(url);
109 }
110 }
111
112 void PlacesPanel::slotUrlsDropped(const KUrl& dest, QDropEvent* event, QWidget* parent)
113 {
114 Q_UNUSED(parent);
115 DragAndDropHelper::dropUrls(KFileItem(), dest, event);
116 }
117
118 void PlacesPanel::loadBookmarks()
119 {
120 KBookmarkGroup root = m_bookmarkManager->root();
121 KBookmark bookmark = root.first();
122 QSet<QString> devices = m_availableDevices;
123
124 while (!bookmark.isNull()) {
125 const QString udi = bookmark.metaDataItem("UDI");
126 const QString appName = bookmark.metaDataItem("OnlyInApp");
127 const bool deviceAvailable = devices.remove(udi);
128
129 const bool allowedHere = appName.isEmpty() || (appName == KGlobal::mainComponent().componentName());
130
131 if ((udi.isEmpty() && allowedHere) || deviceAvailable) {
132 KStandardItem* item = new KStandardItem();
133 item->setIcon(KIcon(bookmark.icon()));
134 item->setText(bookmark.text());
135 item->setDataValue("address", bookmark.address());
136 item->setDataValue("url", bookmark.url());
137 if (deviceAvailable) {
138 item->setDataValue("udi", udi);
139 }
140 m_model->appendItem(item);
141 }
142
143 bookmark = root.next(bookmark);
144 }
145 }
146
147 #include "placespanel.moc"