]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
Make sort/descending available in dolphinpart
[dolphin.git] / src / dolphinpart.cpp
1 /* This file is part of the KDE project
2 Copyright (c) 2007 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20 #include "dolphinpart.h"
21 #include <kpropertiesdialog.h>
22 #include <kglobalsettings.h>
23 #include "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "dolphinmodel.h"
26
27 #include <konq_operations.h>
28
29 #include <kactioncollection.h>
30 #include <kdirlister.h>
31 #include <kiconloader.h>
32 #include <kmessagebox.h>
33 #include <kparts/genericfactory.h>
34 #include <ktoggleaction.h>
35 #include <kconfiggroup.h>
36
37 #include <QActionGroup>
38 #include <QApplication>
39 #include <QClipboard>
40
41 typedef KParts::GenericFactory<DolphinPart> DolphinPartFactory;
42 K_EXPORT_COMPONENT_FACTORY(dolphinpart, DolphinPartFactory)
43
44 DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QStringList& args)
45 : KParts::ReadOnlyPart(parent)
46 {
47 Q_UNUSED(args)
48 setComponentData( DolphinPartFactory::componentData() );
49 m_extension = new DolphinPartBrowserExtension(this);
50
51 // make sure that other apps using this part find Dolphin's view-file-columns icons
52 KIconLoader::global()->addAppDir("dolphin");
53
54 m_dirLister = new KDirLister;
55 m_dirLister->setAutoUpdate(true);
56 m_dirLister->setMainWindow(parentWidget->topLevelWidget());
57 m_dirLister->setDelayedMimeTypes(true);
58
59 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
60 connect(m_dirLister, SIGNAL(completed(KUrl)), this, SLOT(slotCompleted(KUrl)));
61 connect(m_dirLister, SIGNAL(canceled(KUrl)), this, SLOT(slotCanceled(KUrl)));
62
63 m_dolphinModel = new DolphinModel(this);
64 m_dolphinModel->setDirLister(m_dirLister);
65
66 m_proxyModel = new DolphinSortFilterProxyModel(this);
67 m_proxyModel->setSourceModel(m_dolphinModel);
68
69 m_view = new DolphinView(parentWidget,
70 KUrl(),
71 m_dirLister,
72 m_dolphinModel,
73 m_proxyModel);
74 setWidget(m_view);
75
76 setXMLFile("dolphinpart.rc");
77
78 connect(m_view, SIGNAL(infoMessage(QString)),
79 this, SLOT(slotInfoMessage(QString)));
80 connect(m_view, SIGNAL(errorMessage(QString)),
81 this, SLOT(slotErrorMessage(QString)));
82 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
83 this, SLOT(slotItemTriggered(KFileItem)));
84 connect(m_view, SIGNAL(requestContextMenu(KFileItem,KUrl)),
85 this, SLOT(slotOpenContextMenu(KFileItem,KUrl)));
86 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
87 m_extension, SIGNAL(selectionInfo(KFileItemList)));
88 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
89 this, SLOT(slotSelectionChanged(KFileItemList)));
90 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
91 this, SLOT(slotRequestItemInfo(KFileItem)));
92 connect(m_view, SIGNAL(urlChanged(KUrl)),
93 this, SLOT(slotUrlChanged(KUrl)));
94 connect(m_view, SIGNAL(modeChanged()),
95 this, SLOT(updateViewActions()));
96 connect(m_view, SIGNAL(sortOrderChanged(Qt::SortOrder)),
97 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
98
99 QClipboard* clipboard = QApplication::clipboard();
100 connect(clipboard, SIGNAL(dataChanged()),
101 this, SLOT(updatePasteAction()));
102
103 createActions();
104 updateViewActions();
105 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
106
107 // TODO provide the viewmode actions in the menu, merged with the existing view-mode-actions somehow
108 // [Q_PROPERTY introspection?]
109
110 // TODO sort_by_* actions
111 // TODO show_*_info actions
112
113 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
114 // (sort of spacial navigation)
115 }
116
117 DolphinPart::~DolphinPart()
118 {
119 delete m_dirLister;
120 }
121
122 void DolphinPart::createActions()
123 {
124 QActionGroup* viewModeActions = new QActionGroup(this);
125 viewModeActions->addAction(DolphinView::iconsModeAction(actionCollection()));
126 viewModeActions->addAction(DolphinView::detailsModeAction(actionCollection()));
127 viewModeActions->addAction(DolphinView::columnsModeAction(actionCollection()));
128 connect(viewModeActions, SIGNAL(triggered(QAction*)), this, SLOT(slotViewModeActionTriggered(QAction*)));
129
130 KAction* renameAction = DolphinView::createRenameAction(actionCollection());
131 connect(renameAction, SIGNAL(triggered()), m_view, SLOT(renameSelectedItems()));
132
133 KAction* moveToTrashAction = DolphinView::createMoveToTrashAction(actionCollection());
134 connect(moveToTrashAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)),
135 this, SLOT(slotTrashActivated(Qt::MouseButtons, Qt::KeyboardModifiers)));
136
137 KAction* deleteAction = DolphinView::createDeleteAction(actionCollection());
138 connect(deleteAction, SIGNAL(triggered()), m_view, SLOT(deleteSelectedItems()));
139
140 KAction *editMimeTypeAction = actionCollection()->addAction( "editMimeType" );
141 editMimeTypeAction->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
142 connect(editMimeTypeAction, SIGNAL(triggered()), SLOT(slotEditMimeType()));
143
144 KAction *propertiesAction = actionCollection()->addAction( "properties" );
145 propertiesAction->setText( i18nc("@action:inmenu Edit", "Properties") );
146 propertiesAction->setShortcut(Qt::ALT+Qt::Key_Return);
147 connect(propertiesAction, SIGNAL(triggered()), SLOT(slotProperties()));
148
149 // View menu
150
151 // TODO sort_by_*
152
153 KAction* sortDescending = DolphinView::createSortDescendingAction(actionCollection());
154 connect(sortDescending, SIGNAL(triggered()), m_view, SLOT(toggleSortOrder()));
155
156 // Go menu
157
158 KAction* newDirAction = DolphinView::createNewDirAction(actionCollection());
159 connect(newDirAction, SIGNAL(triggered()), SLOT(createDir()));
160
161 QActionGroup* goActionGroup = new QActionGroup(this);
162 connect(goActionGroup, SIGNAL(triggered(QAction*)),
163 this, SLOT(slotGoTriggered(QAction*)));
164
165 createGoAction("go_applications", "start-here-kde",
166 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
167 goActionGroup);
168 createGoAction("go_network_folders", "folder-remote",
169 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
170 goActionGroup);
171 createGoAction("go_settings", "preferences-system",
172 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
173 goActionGroup);
174 createGoAction("go_trash", "user-trash",
175 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
176 goActionGroup);
177 createGoAction("go_autostart", "",
178 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
179 goActionGroup);
180 }
181
182 void DolphinPart::createGoAction(const char* name, const char* iconName,
183 const QString& text, const QString& url,
184 QActionGroup* actionGroup)
185 {
186 KAction* action = actionCollection()->addAction(name);
187 action->setIcon(KIcon(iconName));
188 action->setText(text);
189 action->setData(url);
190 action->setActionGroup(actionGroup);
191 }
192
193 void DolphinPart::slotGoTriggered(QAction* action)
194 {
195 const QString url = action->data().toString();
196 emit m_extension->openUrlRequest(KUrl(url));
197 }
198
199 void DolphinPart::slotSelectionChanged(const KFileItemList& selection)
200 {
201 const bool hasSelection = !selection.isEmpty();
202 if (!hasSelection) {
203 stateChanged("has_no_selection");
204 } else {
205 stateChanged("has_selection");
206 }
207
208 QStringList actions;
209 actions << "rename" << "move_to_trash" << "delete" << "editMimeType" << "properties";
210 foreach(const QString& actionName, actions) {
211 QAction* action = actionCollection()->action(actionName);
212 Q_ASSERT(action);
213 if (action) {
214 action->setEnabled(hasSelection);
215 }
216 }
217
218 emit m_extension->enableAction("cut", hasSelection);
219 emit m_extension->enableAction("copy", hasSelection);
220 }
221
222 void DolphinPart::updatePasteAction()
223 {
224 QPair<bool, QString> pasteInfo = m_view->pasteInfo();
225 emit m_extension->enableAction( "paste", pasteInfo.first );
226 emit m_extension->setActionText( "paste", pasteInfo.second );
227 }
228
229 void DolphinPart::updateViewActions()
230 {
231 QAction* action = actionCollection()->action(m_view->currentViewModeActionName());
232 if (action != 0) {
233 KToggleAction* toggleAction = static_cast<KToggleAction*>(action);
234 toggleAction->setChecked(true);
235 }
236 }
237
238 KAboutData* DolphinPart::createAboutData()
239 {
240 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
241 }
242
243 bool DolphinPart::openUrl(const KUrl& url)
244 {
245 const bool reload = arguments().reload();
246 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
247 return true;
248 }
249 setUrl(url); // remember it at the KParts level
250 const QString prettyUrl = url.pathOrUrl();
251 emit setWindowCaption(prettyUrl);
252 emit m_extension->setLocationBarUrl(prettyUrl);
253 emit started(0); // get the wheel to spin
254 m_view->setUrl(url);
255 if (reload)
256 m_view->reload();
257 return true;
258 }
259
260 void DolphinPart::slotCompleted(const KUrl& url)
261 {
262 Q_UNUSED(url)
263 emit completed();
264 }
265
266 void DolphinPart::slotCanceled(const KUrl& url)
267 {
268 slotCompleted(url);
269 }
270
271 void DolphinPart::slotInfoMessage(const QString& msg)
272 {
273 emit setStatusBarText(msg);
274 }
275
276 void DolphinPart::slotErrorMessage(const QString& msg)
277 {
278 KMessageBox::error(m_view, msg);
279 }
280
281 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
282 {
283 emit m_extension->mouseOverInfo(item);
284 }
285
286 void DolphinPart::slotItemTriggered(const KFileItem& item)
287 {
288 KParts::OpenUrlArguments args;
289 args.setMimeType(item.mimetype());
290
291 // Ideally, konqueror should be changed to not require trustedSource for directory views,
292 // since the idea was not to need BrowserArguments for non-browser stuff...
293 KParts::BrowserArguments browserArgs;
294 browserArgs.trustedSource = true;
295
296 // MMB click support.
297 // TODO: this doesn't work, mouseButtons() is always 0.
298 // Issue N176832 for the missing QAIV signal; task 177399
299 kDebug() << QApplication::mouseButtons();
300 if (QApplication::mouseButtons() & Qt::MidButton) {
301 kDebug() << "MMB!!" << item.mimetype();
302 if (item.mimeTypePtr()->is("inode/directory")) {
303 emit m_extension->createNewWindow(item.url(), args);
304 } else {
305 kDebug() << "run()";
306 item.run();
307 }
308 } else {
309 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
310 kDebug() << "LMB";
311 emit m_extension->openUrlRequest(item.url(), args, browserArgs);
312 }
313 }
314
315 void DolphinPart::slotOpenContextMenu(const KFileItem& _item, const KUrl&)
316 {
317 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
318 | KParts::BrowserExtension::ShowProperties
319 | KParts::BrowserExtension::ShowUrlOperations;
320 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
321 // popupFlags |= KParts::BrowserExtension::NoDeletion;
322
323 KFileItem item(_item);
324
325 if (item.isNull()) { // viewport context menu
326 popupFlags |= KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowUp;
327 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
328 // and use this as fallback:
329 item = KFileItem( S_IFDIR, (mode_t)-1, url() );
330 }
331
332 KParts::BrowserExtension::ActionGroupMap actionGroups;
333 QList<QAction *> editActions;
334
335 if (!item.isNull()) { // only for context menu on one or more items
336 // TODO if ( sMoving )
337 editActions.append(actionCollection()->action("rename"));
338
339 bool addTrash = false;
340 bool addDel = false;
341
342 // TODO if ( sMoving && !isIntoTrash && !isTrashLink )
343 addTrash = true;
344
345 /* TODO if ( sDeleting ) */ {
346 if ( !item.isLocalFile() )
347 addDel = true;
348 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
349 addTrash = false;
350 addDel = true;
351 }
352 else {
353 KConfigGroup configGroup( KGlobal::config(), "KDE" );
354 if ( configGroup.readEntry( "ShowDeleteCommand", false) )
355 addDel = true;
356 }
357 }
358
359 if (addTrash)
360 editActions.append(actionCollection()->action("move_to_trash"));
361 if (addDel)
362 editActions.append(actionCollection()->action("delete"));
363 actionGroups.insert("editactions", editActions);
364
365 KFileItemList items; items.append(item);
366 emit m_extension->popupMenu(QCursor::pos(),
367 items,
368 KParts::OpenUrlArguments(),
369 KParts::BrowserArguments(),
370 popupFlags,
371 actionGroups);
372 }
373 }
374
375 void DolphinPart::slotViewModeActionTriggered(QAction* action)
376 {
377 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
378 m_view->setMode(mode);
379 }
380
381 void DolphinPart::slotUrlChanged(const KUrl& url)
382 {
383 if (m_view->url() != url) {
384 // If the view URL is not equal to 'url', then an inner URL change has
385 // been done (e. g. by activating an existing column in the column view).
386 openUrl(url);
387 emit m_extension->openUrlNotify();
388 }
389 }
390
391 ////
392
393 void DolphinPartBrowserExtension::cut()
394 {
395 m_part->view()->cutSelectedItems();
396 }
397
398 void DolphinPartBrowserExtension::copy()
399 {
400 m_part->view()->copySelectedItems();
401 }
402
403 void DolphinPartBrowserExtension::paste()
404 {
405 m_part->view()->paste();
406 }
407
408 ////
409
410 void DolphinPart::slotTrashActivated(Qt::MouseButtons, Qt::KeyboardModifiers modifiers)
411 {
412 // Note: kde3's konq_mainwindow.cpp used to check
413 // reason == KAction::PopupMenuActivation && ...
414 // but this isn't supported anymore
415 if (modifiers & Qt::ShiftModifier)
416 m_view->deleteSelectedItems();
417 else
418 m_view->trashSelectedItems();
419 }
420
421 void DolphinPart::slotEditMimeType()
422 {
423 const KFileItemList items = m_view->selectedItems();
424 if (!items.isEmpty()) {
425 KonqOperations::editMimeType(items.first().mimetype(), m_view);
426 }
427 }
428
429 void DolphinPart::slotProperties()
430 {
431 const KFileItemList items = m_view->selectedItems();
432 if (!items.isEmpty()) {
433 KPropertiesDialog dialog(items.first().url(), m_view);
434 dialog.exec();
435 }
436 }
437
438 void DolphinPart::createDir()
439 {
440 KonqOperations::newDir(m_view, url());
441 }
442
443 void DolphinPart::slotSortOrderChanged(Qt::SortOrder order)
444 {
445 KToggleAction* descending = static_cast<KToggleAction*>(actionCollection()->action("descending"));
446 const bool sortDescending = (order == Qt::DescendingOrder);
447 descending->setChecked(sortDescending);
448 }
449
450 #include "dolphinpart.moc"