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