]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
Places Panel: Prepare code to save state of bookmarks
[dolphin.git] / src / panels / places / placespanel.cpp
1 /***************************************************************************
2 * Copyright (C) 2008-2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on KFilePlacesView 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 <KConfigGroup>
27 #include <KDebug>
28 #include <KDirNotify>
29 #include <KIcon>
30 #include <KIO/Job>
31 #include <KIO/JobUiDelegate>
32 #include <KLocale>
33 #include <kitemviews/kitemlistcontainer.h>
34 #include <kitemviews/kitemlistcontroller.h>
35 #include <kitemviews/kitemlistselectionmanager.h>
36 #include <kitemviews/kstandarditem.h>
37 #include <kitemviews/kstandarditemlistview.h>
38 #include <KMenu>
39 #include <KMessageBox>
40 #include <KNotification>
41 #include "placesitem.h"
42 #include "placesitemeditdialog.h"
43 #include "placesitemlistgroupheader.h"
44 #include "placesitemlistwidget.h"
45 #include "placesitemmodel.h"
46 #include <views/draganddrophelper.h>
47 #include <QVBoxLayout>
48 #include <QShowEvent>
49
50 #ifdef HAVE_NEPOMUK
51 #include <Nepomuk/Query/ComparisonTerm>
52 #include <Nepomuk/Query/LiteralTerm>
53 #include <Nepomuk/Query/Query>
54 #include <Nepomuk/Query/ResourceTypeTerm>
55 #include <Nepomuk/Vocabulary/NFO>
56 #include <Nepomuk/Vocabulary/NIE>
57 #endif
58
59 PlacesPanel::PlacesPanel(QWidget* parent) :
60 Panel(parent),
61 m_controller(0),
62 m_model(0)
63 {
64 }
65
66 PlacesPanel::~PlacesPanel()
67 {
68 }
69
70 bool PlacesPanel::urlChanged()
71 {
72 return true;
73 }
74
75 void PlacesPanel::showEvent(QShowEvent* event)
76 {
77 if (event->spontaneous()) {
78 Panel::showEvent(event);
79 return;
80 }
81
82 if (!m_controller) {
83 // Postpone the creating of the controller to the first show event.
84 // This assures that no performance and memory overhead is given when the folders panel is not
85 // used at all and stays invisible.
86 m_model = new PlacesItemModel(this);
87 m_model->setGroupedSorting(true);
88 m_model->setSortRole("group");
89 connect(m_model, SIGNAL(errorMessage(QString)),
90 this, SIGNAL(errorMessage(QString)));
91
92 KStandardItemListView* view = new KStandardItemListView();
93 view->setWidgetCreator(new KItemListWidgetCreator<PlacesItemListWidget>());
94 view->setGroupHeaderCreator(new KItemListGroupHeaderCreator<PlacesItemListGroupHeader>());
95
96 m_controller = new KItemListController(m_model, view, this);
97 m_controller->setSelectionBehavior(KItemListController::SingleSelection);
98 connect(m_controller, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
99 connect(m_controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
100 connect(m_controller, SIGNAL(itemContextMenuRequested(int,QPointF)), this, SLOT(slotItemContextMenuRequested(int,QPointF)));
101 connect(m_controller, SIGNAL(viewContextMenuRequested(QPointF)), this, SLOT(slotViewContextMenuRequested(QPointF)));
102
103 KItemListContainer* container = new KItemListContainer(m_controller, this);
104 container->setEnabledFrame(false);
105
106 QVBoxLayout* layout = new QVBoxLayout(this);
107 layout->setMargin(0);
108 layout->addWidget(container);
109
110 selectClosestItem();
111 }
112
113 Panel::showEvent(event);
114 }
115
116 void PlacesPanel::slotItemActivated(int index)
117 {
118 const KUrl url = m_model->data(index).value("url").value<KUrl>();
119 if (!url.isEmpty()) {
120 emit placeActivated(convertedUrl(url));
121 }
122 }
123
124 void PlacesPanel::slotItemMiddleClicked(int index)
125 {
126 const KUrl url = m_model->data(index).value("url").value<KUrl>();
127 if (!url.isEmpty()) {
128 emit placeMiddleClicked(convertedUrl(url));
129 }
130 }
131
132 void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
133 {
134 const PlacesItem* item = m_model->placesItem(index);
135 if (!item) {
136 return;
137 }
138
139 KMenu menu(this);
140
141 QAction* emptyTrashAction = 0;
142 QAction* addAction = 0;
143 QAction* mainSeparator = 0;
144 QAction* editAction = 0;
145 QAction* teardownAction = 0;
146 QAction* ejectAction = 0;
147
148 const QString label = item->text();
149
150 const bool isDevice = !item->udi().isEmpty();
151 if (isDevice) {
152 ejectAction = m_model->ejectAction(index);
153 if (ejectAction) {
154 ejectAction->setParent(&menu);
155 menu.addAction(ejectAction);
156 }
157
158 teardownAction = m_model->teardownAction(index);
159 if (teardownAction) {
160 teardownAction->setParent(&menu);
161 menu.addAction(teardownAction);
162 }
163
164 if (teardownAction || ejectAction) {
165 mainSeparator = menu.addSeparator();
166 }
167 } else {
168 if (item->url() == KUrl("trash:/")) {
169 emptyTrashAction = menu.addAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"));
170 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
171 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
172 menu.addSeparator();
173 }
174 addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
175 mainSeparator = menu.addSeparator();
176 editAction = menu.addAction(KIcon("document-properties"), i18nc("@item:inmenu", "Edit '%1'...", label));
177 }
178
179 if (!addAction) {
180 addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
181 }
182
183 QAction* openInNewTabAction = menu.addAction(i18nc("@item:inmenu", "Open '%1' in New Tab", label));
184 openInNewTabAction->setIcon(KIcon("tab-new"));
185
186 QAction* removeAction = 0;
187 if (!isDevice && !item->isSystemItem()) {
188 removeAction = menu.addAction(KIcon("edit-delete"), i18nc("@item:inmenu", "Remove '%1'", label));
189 }
190
191 QAction* hideAction = menu.addAction(i18nc("@item:inmenu", "Hide '%1'", label));
192 hideAction->setCheckable(true);
193 hideAction->setChecked(item->isHidden());
194
195 QAction* showAllAction = 0;
196 if (m_model->hiddenCount() > 0) {
197 if (!mainSeparator) {
198 mainSeparator = menu.addSeparator();
199 }
200 showAllAction = menu.addAction(i18nc("@item:inmenu", "Show All Entries"));
201 showAllAction->setCheckable(true);
202 showAllAction->setChecked(m_model->hiddenItemsShown());
203 }
204
205 menu.addSeparator();
206 foreach (QAction* action, customContextMenuActions()) {
207 menu.addAction(action);
208 }
209
210 QAction* action = menu.exec(pos.toPoint());
211 if (action) {
212 if (action == emptyTrashAction) {
213 emptyTrash();
214 } else if (action == addAction) {
215 addEntry();
216 } else if (action == editAction) {
217 editEntry(index);
218 } else if (action == removeAction) {
219 m_model->removeItem(index);
220 m_model->save();
221 } else if (action == hideAction) {
222 m_model->setItemHidden(index, hideAction->isChecked());
223 m_model->save();
224 } else if (action == openInNewTabAction) {
225 const KUrl url = m_model->item(index)->dataValue("url").value<KUrl>();
226 emit placeMiddleClicked(url);
227 } else if (action == showAllAction) {
228 m_model->setHiddenItemsShown(showAllAction->isChecked());
229 } else if (action == teardownAction) {
230 m_model->requestTeardown(index);
231 } else if (action == ejectAction) {
232 m_model->requestEject(index);
233 }
234 }
235
236 selectClosestItem();
237 }
238
239 void PlacesPanel::slotViewContextMenuRequested(const QPointF& pos)
240 {
241 KMenu menu(this);
242
243 QAction* addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
244
245 QAction* showAllAction = 0;
246 if (m_model->hiddenCount() > 0) {
247 showAllAction = menu.addAction(i18nc("@item:inmenu", "Show All Entries"));
248 showAllAction->setCheckable(true);
249 showAllAction->setChecked(m_model->hiddenItemsShown());
250 }
251
252 menu.addSeparator();
253 foreach (QAction* action, customContextMenuActions()) {
254 menu.addAction(action);
255 }
256
257 QAction* action = menu.exec(pos.toPoint());
258 if (action) {
259 if (action == addAction) {
260 addEntry();
261 } else if (action == showAllAction) {
262 m_model->setHiddenItemsShown(showAllAction->isChecked());
263 }
264 }
265
266 selectClosestItem();
267 }
268
269 void PlacesPanel::slotUrlsDropped(const KUrl& dest, QDropEvent* event, QWidget* parent)
270 {
271 Q_UNUSED(parent);
272 const QString error = DragAndDropHelper::dropUrls(KFileItem(), dest, event);
273 if (!error.isEmpty()) {
274 emit errorMessage(error);
275 }
276
277 }
278
279 void PlacesPanel::slotTrashUpdated(KJob* job)
280 {
281 if (job->error()) {
282 emit errorMessage(job->errorString());
283 }
284 org::kde::KDirNotify::emitFilesAdded("trash:/");
285 }
286
287 void PlacesPanel::emptyTrash()
288 {
289 const QString text = i18nc("@info", "Do you really want to empty the Trash? All items will be deleted.");
290 const bool del = KMessageBox::warningContinueCancel(window(),
291 text,
292 QString(),
293 KGuiItem(i18nc("@action:button", "Empty Trash"),
294 KIcon("user-trash"))
295 ) == KMessageBox::Continue;
296 if (del) {
297 QByteArray packedArgs;
298 QDataStream stream(&packedArgs, QIODevice::WriteOnly);
299 stream << int(1);
300 KIO::Job *job = KIO::special(KUrl("trash:/"), packedArgs);
301 KNotification::event("Trash: emptied", QString() , QPixmap() , 0, KNotification::DefaultEvent);
302 job->ui()->setWindow(parentWidget());
303 connect(job, SIGNAL(result(KJob*)), SLOT(slotTrashUpdated(KJob*)));
304 }
305 }
306
307 void PlacesPanel::addEntry()
308 {
309 const int index = m_controller->selectionManager()->currentItem();
310 const KUrl url = m_model->data(index).value("url").value<KUrl>();
311
312 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
313 dialog->setCaption(i18nc("@title:window", "Add Places Entry"));
314 dialog->setAllowGlobal(true);
315 dialog->setUrl(url);
316 if (dialog->exec() == QDialog::Accepted) {
317 KStandardItem* item = createStandardItemFromDialog(dialog);
318
319 // Insert the item as last item of the corresponding group.
320 int i = 0;
321 while (i < m_model->count() && m_model->item(i)->group() != item->group()) {
322 ++i;
323 }
324
325 bool inserted = false;
326 while (!inserted && i < m_model->count()) {
327 if (m_model->item(i)->group() != item->group()) {
328 m_model->insertItem(i, item);
329 inserted = true;
330 }
331 ++i;
332 }
333
334 if (!inserted) {
335 m_model->appendItem(item);
336 }
337 }
338
339 delete dialog;
340
341 m_model->save();
342 }
343
344 void PlacesPanel::editEntry(int index)
345 {
346 QHash<QByteArray, QVariant> data = m_model->data(index);
347
348 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
349 dialog->setCaption(i18nc("@title:window", "Edit Places Entry"));
350 dialog->setIcon(data.value("iconName").toString());
351 dialog->setText(data.value("text").toString());
352 dialog->setUrl(data.value("url").value<KUrl>());
353 dialog->setAllowGlobal(true);
354 if (dialog->exec() == QDialog::Accepted) {
355 KStandardItem* oldItem = m_model->item(index);
356 if (oldItem) {
357 KStandardItem* item = createStandardItemFromDialog(dialog);
358 // Although the user might have changed the URL of the item in a way
359 // that another group should be assigned, we still apply the old
360 // group to keep the same position for the item.
361 item->setGroup(oldItem->group());
362 m_model->replaceItem(index, item);
363 }
364 }
365
366 delete dialog;
367
368 m_model->save();
369 }
370
371 void PlacesPanel::selectClosestItem()
372 {
373 const int index = m_model->closestItem(url());
374 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
375 selectionManager->setCurrentItem(index);
376 selectionManager->clearSelection();
377 selectionManager->setSelected(index);
378 }
379
380 KStandardItem* PlacesPanel::createStandardItemFromDialog(PlacesItemEditDialog* dialog) const
381 {
382 Q_ASSERT(dialog);
383
384 const KUrl newUrl = dialog->url();
385 KStandardItem* item = new KStandardItem();
386 item->setIcon(dialog->icon());
387 item->setText(dialog->text());
388 item->setDataValue("url", newUrl);
389 item->setGroup(m_model->groupName(newUrl));
390
391 return item;
392 }
393
394 KUrl PlacesPanel::convertedUrl(const KUrl& url)
395 {
396 KUrl newUrl = url;
397 if (url.protocol() == QLatin1String("timeline")) {
398 newUrl = createTimelineUrl(url);
399 } else if (url.protocol() == QLatin1String("search")) {
400 newUrl = createSearchUrl(url);
401 }
402
403 return newUrl;
404 }
405
406 KUrl PlacesPanel::createTimelineUrl(const KUrl& url)
407 {
408 // TODO: Clarify with the Nepomuk-team whether it makes sense
409 // provide default-timeline-URLs like 'yesterday', 'this month'
410 // and 'last month'.
411 KUrl timelineUrl;
412
413 const QString path = url.pathOrUrl();
414 if (path.endsWith("yesterday")) {
415 const QDate date = QDate::currentDate().addDays(-1);
416 const int year = date.year();
417 const int month = date.month();
418 const int day = date.day();
419 timelineUrl = "timeline:/" + timelineDateString(year, month) +
420 '/' + timelineDateString(year, month, day);
421 } else if (path.endsWith("thismonth")) {
422 const QDate date = QDate::currentDate();
423 timelineUrl = "timeline:/" + timelineDateString(date.year(), date.month());
424 } else if (path.endsWith("lastmonth")) {
425 const QDate date = QDate::currentDate().addMonths(-1);
426 timelineUrl = "timeline:/" + timelineDateString(date.year(), date.month());
427 } else {
428 Q_ASSERT(path.endsWith("today"));
429 timelineUrl= url;
430 }
431
432 return timelineUrl;
433 }
434
435 QString PlacesPanel::timelineDateString(int year, int month, int day)
436 {
437 QString date = QString::number(year) + '-';
438 if (month < 10) {
439 date += '0';
440 }
441 date += QString::number(month);
442
443 if (day >= 1) {
444 date += '-';
445 if (day < 10) {
446 date += '0';
447 }
448 date += QString::number(day);
449 }
450
451 return date;
452 }
453
454 KUrl PlacesPanel::createSearchUrl(const KUrl& url)
455 {
456 KUrl searchUrl;
457
458 #ifdef HAVE_NEPOMUK
459 const QString path = url.pathOrUrl();
460 if (path.endsWith("documents")) {
461 searchUrl = searchUrlForTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Document()));
462 } else if (path.endsWith("images")) {
463 searchUrl = searchUrlForTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Image()));
464 } else if (path.endsWith("audio")) {
465 searchUrl = searchUrlForTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
466 Nepomuk::Query::LiteralTerm("audio")));
467 } else if (path.endsWith("videos")) {
468 searchUrl = searchUrlForTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
469 Nepomuk::Query::LiteralTerm("video")));
470 } else {
471 Q_ASSERT(false);
472 }
473 #else
474 Q_UNUSED(url);
475 #endif
476
477 return searchUrl;
478 }
479
480 #ifdef HAVE_NEPOMUK
481 KUrl PlacesPanel::searchUrlForTerm(const Nepomuk::Query::Term& term)
482 {
483 const Nepomuk::Query::Query query(term);
484 return query.toSearchUrl();
485 }
486 #endif
487
488 #include "placespanel.moc"