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