]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
979ef25a64460a7a27d6088cb56eb6271b0bdfa1
[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 "placesitemeditdialog.h"
42 #include "placesitemlistgroupheader.h"
43 #include "placesitemlistwidget.h"
44 #include "placesitemmodel.h"
45 #include <views/draganddrophelper.h>
46 #include <QVBoxLayout>
47 #include <QShowEvent>
48
49 PlacesPanel::PlacesPanel(QWidget* parent) :
50 Panel(parent),
51 m_controller(0),
52 m_model(0)
53 {
54 }
55
56 PlacesPanel::~PlacesPanel()
57 {
58 }
59
60 bool PlacesPanel::urlChanged()
61 {
62 return true;
63 }
64
65 void PlacesPanel::showEvent(QShowEvent* event)
66 {
67 if (event->spontaneous()) {
68 Panel::showEvent(event);
69 return;
70 }
71
72 if (!m_controller) {
73 // Postpone the creating of the controller to the first show event.
74 // This assures that no performance and memory overhead is given when the folders panel is not
75 // used at all and stays invisible.
76 m_model = new PlacesItemModel(this);
77 m_model->setGroupedSorting(true);
78 m_model->setSortRole("group");
79 connect(m_model, SIGNAL(errorMessage(QString)),
80 this, SIGNAL(errorMessage(QString)));
81
82 KStandardItemListView* view = new KStandardItemListView();
83 view->setWidgetCreator(new KItemListWidgetCreator<PlacesItemListWidget>());
84 view->setGroupHeaderCreator(new KItemListGroupHeaderCreator<PlacesItemListGroupHeader>());
85
86 m_controller = new KItemListController(m_model, view, this);
87 m_controller->setSelectionBehavior(KItemListController::SingleSelection);
88 connect(m_controller, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
89 connect(m_controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
90 connect(m_controller, SIGNAL(itemContextMenuRequested(int,QPointF)), this, SLOT(slotItemContextMenuRequested(int,QPointF)));
91 connect(m_controller, SIGNAL(viewContextMenuRequested(QPointF)), this, SLOT(slotViewContextMenuRequested(QPointF)));
92
93 KItemListContainer* container = new KItemListContainer(m_controller, this);
94 container->setEnabledFrame(false);
95
96 QVBoxLayout* layout = new QVBoxLayout(this);
97 layout->setMargin(0);
98 layout->addWidget(container);
99
100 selectClosestItem();
101 }
102
103 Panel::showEvent(event);
104 }
105
106 void PlacesPanel::slotItemActivated(int index)
107 {
108 const KUrl url = m_model->data(index).value("url").value<KUrl>();
109 if (!url.isEmpty()) {
110 emit placeActivated(url);
111 }
112 }
113
114 void PlacesPanel::slotItemMiddleClicked(int index)
115 {
116 const KUrl url = m_model->data(index).value("url").value<KUrl>();
117 if (!url.isEmpty()) {
118 emit placeMiddleClicked(url);
119 }
120 }
121
122 void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
123 {
124 const QHash<QByteArray, QVariant> data = m_model->data(index);
125 const QString label = data.value("text").toString();
126
127 KMenu menu(this);
128
129 QAction* emptyTrashAction = 0;
130 QAction* addAction = 0;
131 QAction* mainSeparator = 0;
132 QAction* editAction = 0;
133 QAction* teardownAction = 0;
134 QAction* ejectAction = 0;
135
136 const bool isSystemItem = m_model->isSystemItem(index);
137 const bool isDevice = !data.value("udi").toString().isEmpty();
138 if (isDevice) {
139 ejectAction = m_model->ejectAction(index);
140 if (ejectAction) {
141 ejectAction->setParent(&menu);
142 menu.addAction(ejectAction);
143 }
144
145 teardownAction = m_model->teardownAction(index);
146 if (teardownAction) {
147 teardownAction->setParent(&menu);
148 menu.addAction(teardownAction);
149 }
150
151 if (teardownAction || ejectAction) {
152 mainSeparator = menu.addSeparator();
153 }
154 } else {
155 if (data.value("url").value<KUrl>() == KUrl("trash:/")) {
156 emptyTrashAction = menu.addAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"));
157 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
158 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
159 menu.addSeparator();
160 }
161 addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
162 mainSeparator = menu.addSeparator();
163 editAction = menu.addAction(KIcon("document-properties"), i18nc("@item:inmenu", "Edit '%1'...", label));
164 }
165
166 if (!addAction) {
167 addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
168 }
169
170 QAction* openInNewTabAction = menu.addAction(i18nc("@item:inmenu", "Open '%1' in New Tab", label));
171 openInNewTabAction->setIcon(KIcon("tab-new"));
172
173 QAction* removeAction = 0;
174 if (!isDevice && !isSystemItem) {
175 removeAction = menu.addAction(KIcon("edit-delete"), i18nc("@item:inmenu", "Remove '%1'", label));
176 }
177
178 QAction* hideAction = menu.addAction(i18nc("@item:inmenu", "Hide '%1'", label));
179 hideAction->setCheckable(true);
180 hideAction->setChecked(data.value("isHidden").toBool());
181
182 QAction* showAllAction = 0;
183 if (m_model->hiddenCount() > 0) {
184 if (!mainSeparator) {
185 mainSeparator = menu.addSeparator();
186 }
187 showAllAction = menu.addAction(i18nc("@item:inmenu", "Show All Entries"));
188 showAllAction->setCheckable(true);
189 showAllAction->setChecked(m_model->hiddenItemsShown());
190 }
191
192 menu.addSeparator();
193 foreach (QAction* action, customContextMenuActions()) {
194 menu.addAction(action);
195 }
196
197 QAction* action = menu.exec(pos.toPoint());
198 if (action) {
199 if (action == emptyTrashAction) {
200 emptyTrash();
201 } else if (action == addAction) {
202 addEntry();
203 } else if (action == editAction) {
204 editEntry(index);
205 } else if (action == removeAction) {
206 m_model->removeItem(index);
207 } else if (action == hideAction) {
208 m_model->setItemHidden(index, hideAction->isChecked());
209 } else if (action == openInNewTabAction) {
210 const KUrl url = m_model->item(index)->dataValue("url").value<KUrl>();
211 emit placeMiddleClicked(url);
212 } else if (action == showAllAction) {
213 m_model->setHiddenItemsShown(showAllAction->isChecked());
214 } else if (action == teardownAction) {
215 m_model->requestTeardown(index);
216 } else if (action == ejectAction) {
217 m_model->requestEject(index);
218 }
219 }
220
221 selectClosestItem();
222 }
223
224 void PlacesPanel::slotViewContextMenuRequested(const QPointF& pos)
225 {
226 KMenu menu(this);
227
228 QAction* addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
229
230 QAction* showAllAction = 0;
231 if (m_model->hiddenCount() > 0) {
232 showAllAction = menu.addAction(i18nc("@item:inmenu", "Show All Entries"));
233 showAllAction->setCheckable(true);
234 showAllAction->setChecked(m_model->hiddenItemsShown());
235 }
236
237 menu.addSeparator();
238 foreach (QAction* action, customContextMenuActions()) {
239 menu.addAction(action);
240 }
241
242 QAction* action = menu.exec(pos.toPoint());
243 if (action) {
244 if (action == addAction) {
245 addEntry();
246 } else if (action == showAllAction) {
247 m_model->setHiddenItemsShown(showAllAction->isChecked());
248 }
249 }
250
251 selectClosestItem();
252 }
253
254 void PlacesPanel::slotUrlsDropped(const KUrl& dest, QDropEvent* event, QWidget* parent)
255 {
256 Q_UNUSED(parent);
257 const QString error = DragAndDropHelper::dropUrls(KFileItem(), dest, event);
258 if (!error.isEmpty()) {
259 emit errorMessage(error);
260 }
261
262 }
263
264 void PlacesPanel::slotTrashUpdated(KJob* job)
265 {
266 if (job->error()) {
267 // TODO: Show error-string inside Dolphin, don't use job->ui->showErrorMessage().
268 }
269 org::kde::KDirNotify::emitFilesAdded("trash:/");
270 }
271
272 void PlacesPanel::emptyTrash()
273 {
274 const QString text = i18nc("@info", "Do you really want to empty the Trash? All items will be deleted.");
275 const bool del = KMessageBox::warningContinueCancel(window(),
276 text,
277 QString(),
278 KGuiItem(i18nc("@action:button", "Empty Trash"),
279 KIcon("user-trash"))
280 ) == KMessageBox::Continue;
281 if (del) {
282 QByteArray packedArgs;
283 QDataStream stream(&packedArgs, QIODevice::WriteOnly);
284 stream << int(1);
285 KIO::Job *job = KIO::special(KUrl("trash:/"), packedArgs);
286 KNotification::event("Trash: emptied", QString() , QPixmap() , 0, KNotification::DefaultEvent);
287 job->ui()->setWindow(parentWidget());
288 connect(job, SIGNAL(result(KJob*)), SLOT(slotTrashUpdated(KJob*)));
289 }
290 }
291
292 void PlacesPanel::addEntry()
293 {
294 const int index = m_controller->selectionManager()->currentItem();
295 const KUrl url = m_model->data(index).value("url").value<KUrl>();
296
297 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
298 dialog->setCaption(i18nc("@title:window", "Add Places Entry"));
299 dialog->setAllowGlobal(true);
300 dialog->setUrl(url);
301 if (dialog->exec() == QDialog::Accepted) {
302 KStandardItem* item = createStandardItemFromDialog(dialog);
303
304 // Insert the item as last item of the corresponding group.
305 int i = 0;
306 while (i < m_model->count() && m_model->item(i)->group() != item->group()) {
307 ++i;
308 }
309
310 bool inserted = false;
311 while (!inserted && i < m_model->count()) {
312 if (m_model->item(i)->group() != item->group()) {
313 m_model->insertItem(i, item);
314 inserted = true;
315 }
316 ++i;
317 }
318
319 if (!inserted) {
320 m_model->appendItem(item);
321 }
322 }
323
324 delete dialog;
325 }
326
327 void PlacesPanel::editEntry(int index)
328 {
329 QHash<QByteArray, QVariant> data = m_model->data(index);
330
331 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
332 dialog->setCaption(i18nc("@title:window", "Edit Places Entry"));
333 dialog->setIcon(data.value("iconName").toString());
334 dialog->setText(data.value("text").toString());
335 dialog->setUrl(data.value("url").value<KUrl>());
336 dialog->setAllowGlobal(true);
337 if (dialog->exec() == QDialog::Accepted) {
338 KStandardItem* oldItem = m_model->item(index);
339 if (oldItem) {
340 KStandardItem* item = createStandardItemFromDialog(dialog);
341 // Although the user might have changed the URL of the item in a way
342 // that another group should be assigned, we still apply the old
343 // group to keep the same position for the item.
344 item->setGroup(oldItem->group());
345 m_model->replaceItem(index, item);
346 }
347 }
348
349 delete dialog;
350 }
351
352 void PlacesPanel::selectClosestItem()
353 {
354 const int index = m_model->closestItem(url());
355 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
356 selectionManager->setCurrentItem(index);
357 selectionManager->clearSelection();
358 selectionManager->setSelected(index);
359 }
360
361 KStandardItem* PlacesPanel::createStandardItemFromDialog(PlacesItemEditDialog* dialog) const
362 {
363 Q_ASSERT(dialog);
364
365 const KUrl newUrl = dialog->url();
366 KStandardItem* item = new KStandardItem();
367 item->setIcon(dialog->icon());
368 item->setText(dialog->text());
369 item->setDataValue("url", newUrl);
370 item->setGroup(m_model->groupName(newUrl));
371
372 return item;
373 }
374
375 #include "placespanel.moc"