]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
4d2eebcc63037c798b08445efc4df445b528e5c2
[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(showPreviewChanged()),
97 this, SLOT(slotShowPreviewChanged()));
98 connect(m_view, SIGNAL(showHiddenFilesChanged()),
99 this, SLOT(slotShowHiddenFilesChanged()));
100 connect(m_view, SIGNAL(categorizedSortingChanged()),
101 this, SLOT(slotCategorizedSortingChanged()));
102 // TODO slotSortingChanged
103 connect(m_view, SIGNAL(sortOrderChanged(Qt::SortOrder)),
104 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
105 connect(m_view, SIGNAL(additionalInfoChanged()),
106 this, SLOT(slotAdditionalInfoChanged()));
107
108 QClipboard* clipboard = QApplication::clipboard();
109 connect(clipboard, SIGNAL(dataChanged()),
110 this, SLOT(updatePasteAction()));
111
112 createActions();
113 updateViewActions();
114 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
115
116 // TODO provide the viewmode actions in the menu, merged with the existing view-mode-actions somehow
117 // [Q_PROPERTY introspection?]
118
119 // TODO sort_by_* actions
120
121 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
122 // (sort of spacial navigation)
123 }
124
125 DolphinPart::~DolphinPart()
126 {
127 delete m_dirLister;
128 }
129
130 void DolphinPart::createActions()
131 {
132 QActionGroup* viewModeActions = new QActionGroup(this);
133 viewModeActions->addAction(DolphinView::iconsModeAction(actionCollection()));
134 viewModeActions->addAction(DolphinView::detailsModeAction(actionCollection()));
135 viewModeActions->addAction(DolphinView::columnsModeAction(actionCollection()));
136 connect(viewModeActions, SIGNAL(triggered(QAction*)), this, SLOT(slotViewModeActionTriggered(QAction*)));
137
138 KAction* renameAction = DolphinView::createRenameAction(actionCollection());
139 connect(renameAction, SIGNAL(triggered()), m_view, SLOT(renameSelectedItems()));
140
141 KAction* moveToTrashAction = DolphinView::createMoveToTrashAction(actionCollection());
142 connect(moveToTrashAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)),
143 this, SLOT(slotTrashActivated(Qt::MouseButtons, Qt::KeyboardModifiers)));
144
145 KAction* deleteAction = DolphinView::createDeleteAction(actionCollection());
146 connect(deleteAction, SIGNAL(triggered()), m_view, SLOT(deleteSelectedItems()));
147
148 KAction *editMimeTypeAction = actionCollection()->addAction( "editMimeType" );
149 editMimeTypeAction->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
150 connect(editMimeTypeAction, SIGNAL(triggered()), SLOT(slotEditMimeType()));
151
152 KAction *propertiesAction = actionCollection()->addAction( "properties" );
153 propertiesAction->setText( i18nc("@action:inmenu Edit", "Properties") );
154 propertiesAction->setShortcut(Qt::ALT+Qt::Key_Return);
155 connect(propertiesAction, SIGNAL(triggered()), SLOT(slotProperties()));
156
157 // View menu
158
159 // TODO sort_by_*
160
161 KAction* sortDescending = DolphinView::createSortDescendingAction(actionCollection());
162 connect(sortDescending, SIGNAL(triggered()), m_view, SLOT(toggleSortOrder()));
163
164 QActionGroup* showInformationActionGroup = DolphinView::createAdditionalInformationActionGroup(actionCollection());
165 connect(showInformationActionGroup, SIGNAL(triggered(QAction*)), m_view, SLOT(toggleAdditionalInfo(QAction*)));
166
167 KAction* showPreview = DolphinView::createShowPreviewAction(actionCollection());
168 connect(showPreview, SIGNAL(triggered(bool)), m_view, SLOT(setShowPreview(bool)));
169
170 KAction* showInGroups = DolphinView::createShowInGroupsAction(actionCollection());
171 connect(showInGroups, SIGNAL(triggered(bool)), m_view, SLOT(setCategorizedSorting(bool)));
172
173 KAction* showHiddenFiles = DolphinView::createShowHiddenFilesAction(actionCollection());
174 connect(showHiddenFiles, SIGNAL(triggered(bool)), m_view, SLOT(setShowHiddenFiles(bool)));
175
176 // Go menu
177
178 KAction* newDirAction = DolphinView::createNewDirAction(actionCollection());
179 connect(newDirAction, SIGNAL(triggered()), SLOT(createDir()));
180
181 QActionGroup* goActionGroup = new QActionGroup(this);
182 connect(goActionGroup, SIGNAL(triggered(QAction*)),
183 this, SLOT(slotGoTriggered(QAction*)));
184
185 createGoAction("go_applications", "start-here-kde",
186 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
187 goActionGroup);
188 createGoAction("go_network_folders", "folder-remote",
189 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
190 goActionGroup);
191 createGoAction("go_settings", "preferences-system",
192 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
193 goActionGroup);
194 createGoAction("go_trash", "user-trash",
195 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
196 goActionGroup);
197 createGoAction("go_autostart", "",
198 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
199 goActionGroup);
200 }
201
202 void DolphinPart::createGoAction(const char* name, const char* iconName,
203 const QString& text, const QString& url,
204 QActionGroup* actionGroup)
205 {
206 KAction* action = actionCollection()->addAction(name);
207 action->setIcon(KIcon(iconName));
208 action->setText(text);
209 action->setData(url);
210 action->setActionGroup(actionGroup);
211 }
212
213 void DolphinPart::slotGoTriggered(QAction* action)
214 {
215 const QString url = action->data().toString();
216 emit m_extension->openUrlRequest(KUrl(url));
217 }
218
219 void DolphinPart::slotSelectionChanged(const KFileItemList& selection)
220 {
221 const bool hasSelection = !selection.isEmpty();
222 if (!hasSelection) {
223 stateChanged("has_no_selection");
224 } else {
225 stateChanged("has_selection");
226 }
227
228 QStringList actions;
229 actions << "rename" << "move_to_trash" << "delete" << "editMimeType" << "properties";
230 foreach(const QString& actionName, actions) {
231 QAction* action = actionCollection()->action(actionName);
232 Q_ASSERT(action);
233 if (action) {
234 action->setEnabled(hasSelection);
235 }
236 }
237
238 emit m_extension->enableAction("cut", hasSelection);
239 emit m_extension->enableAction("copy", hasSelection);
240 }
241
242 void DolphinPart::updatePasteAction()
243 {
244 QPair<bool, QString> pasteInfo = m_view->pasteInfo();
245 emit m_extension->enableAction( "paste", pasteInfo.first );
246 emit m_extension->setActionText( "paste", pasteInfo.second );
247 }
248
249 void DolphinPart::updateViewActions()
250 {
251 QAction* action = actionCollection()->action(m_view->currentViewModeActionName());
252 if (action != 0) {
253 KToggleAction* toggleAction = static_cast<KToggleAction*>(action);
254 toggleAction->setChecked(true);
255 }
256 }
257
258 KAboutData* DolphinPart::createAboutData()
259 {
260 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
261 }
262
263 bool DolphinPart::openUrl(const KUrl& url)
264 {
265 const bool reload = arguments().reload();
266 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
267 return true;
268 }
269 setUrl(url); // remember it at the KParts level
270 const QString prettyUrl = url.pathOrUrl();
271 emit setWindowCaption(prettyUrl);
272 emit m_extension->setLocationBarUrl(prettyUrl);
273 emit started(0); // get the wheel to spin
274 m_view->setUrl(url);
275 if (reload)
276 m_view->reload();
277 return true;
278 }
279
280 void DolphinPart::slotCompleted(const KUrl& url)
281 {
282 Q_UNUSED(url)
283 emit completed();
284 }
285
286 void DolphinPart::slotCanceled(const KUrl& url)
287 {
288 slotCompleted(url);
289 }
290
291 void DolphinPart::slotInfoMessage(const QString& msg)
292 {
293 emit setStatusBarText(msg);
294 }
295
296 void DolphinPart::slotErrorMessage(const QString& msg)
297 {
298 KMessageBox::error(m_view, msg);
299 }
300
301 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
302 {
303 emit m_extension->mouseOverInfo(item);
304 }
305
306 void DolphinPart::slotItemTriggered(const KFileItem& item)
307 {
308 KParts::OpenUrlArguments args;
309 args.setMimeType(item.mimetype());
310
311 // Ideally, konqueror should be changed to not require trustedSource for directory views,
312 // since the idea was not to need BrowserArguments for non-browser stuff...
313 KParts::BrowserArguments browserArgs;
314 browserArgs.trustedSource = true;
315
316 // MMB click support.
317 // TODO: this doesn't work, mouseButtons() is always 0.
318 // Issue N176832 for the missing QAIV signal; task 177399
319 kDebug() << QApplication::mouseButtons();
320 if (QApplication::mouseButtons() & Qt::MidButton) {
321 kDebug() << "MMB!!" << item.mimetype();
322 if (item.mimeTypePtr()->is("inode/directory")) {
323 emit m_extension->createNewWindow(item.url(), args);
324 } else {
325 kDebug() << "run()";
326 item.run();
327 }
328 } else {
329 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
330 kDebug() << "LMB";
331 emit m_extension->openUrlRequest(item.url(), args, browserArgs);
332 }
333 }
334
335 void DolphinPart::slotOpenContextMenu(const KFileItem& _item, const KUrl&)
336 {
337 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
338 | KParts::BrowserExtension::ShowProperties
339 | KParts::BrowserExtension::ShowUrlOperations;
340 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
341 // popupFlags |= KParts::BrowserExtension::NoDeletion;
342
343 KFileItem item(_item);
344
345 if (item.isNull()) { // viewport context menu
346 popupFlags |= KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowUp;
347 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
348 // and use this as fallback:
349 item = KFileItem( S_IFDIR, (mode_t)-1, url() );
350 }
351
352 KParts::BrowserExtension::ActionGroupMap actionGroups;
353 QList<QAction *> editActions;
354
355 if (!item.isNull()) { // only for context menu on one or more items
356 // TODO if ( sMoving )
357 editActions.append(actionCollection()->action("rename"));
358
359 bool addTrash = false;
360 bool addDel = false;
361
362 // TODO if ( sMoving && !isIntoTrash && !isTrashLink )
363 addTrash = true;
364
365 /* TODO if ( sDeleting ) */ {
366 if ( !item.isLocalFile() )
367 addDel = true;
368 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
369 addTrash = false;
370 addDel = true;
371 }
372 else {
373 KConfigGroup configGroup( KGlobal::config(), "KDE" );
374 if ( configGroup.readEntry( "ShowDeleteCommand", false) )
375 addDel = true;
376 }
377 }
378
379 if (addTrash)
380 editActions.append(actionCollection()->action("move_to_trash"));
381 if (addDel)
382 editActions.append(actionCollection()->action("delete"));
383 actionGroups.insert("editactions", editActions);
384
385 KFileItemList items; items.append(item);
386 emit m_extension->popupMenu(QCursor::pos(),
387 items,
388 KParts::OpenUrlArguments(),
389 KParts::BrowserArguments(),
390 popupFlags,
391 actionGroups);
392 }
393 }
394
395 void DolphinPart::slotViewModeActionTriggered(QAction* action)
396 {
397 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
398 m_view->setMode(mode);
399 }
400
401 void DolphinPart::slotUrlChanged(const KUrl& url)
402 {
403 if (m_view->url() != url) {
404 // If the view URL is not equal to 'url', then an inner URL change has
405 // been done (e. g. by activating an existing column in the column view).
406 openUrl(url);
407 emit m_extension->openUrlNotify();
408 }
409 }
410
411 ////
412
413 void DolphinPartBrowserExtension::cut()
414 {
415 m_part->view()->cutSelectedItems();
416 }
417
418 void DolphinPartBrowserExtension::copy()
419 {
420 m_part->view()->copySelectedItems();
421 }
422
423 void DolphinPartBrowserExtension::paste()
424 {
425 m_part->view()->paste();
426 }
427
428 ////
429
430 void DolphinPart::slotTrashActivated(Qt::MouseButtons, Qt::KeyboardModifiers modifiers)
431 {
432 // Note: kde3's konq_mainwindow.cpp used to check
433 // reason == KAction::PopupMenuActivation && ...
434 // but this isn't supported anymore
435 if (modifiers & Qt::ShiftModifier)
436 m_view->deleteSelectedItems();
437 else
438 m_view->trashSelectedItems();
439 }
440
441 void DolphinPart::slotEditMimeType()
442 {
443 const KFileItemList items = m_view->selectedItems();
444 if (!items.isEmpty()) {
445 KonqOperations::editMimeType(items.first().mimetype(), m_view);
446 }
447 }
448
449 void DolphinPart::slotProperties()
450 {
451 const KFileItemList items = m_view->selectedItems();
452 if (!items.isEmpty()) {
453 KPropertiesDialog dialog(items.first().url(), m_view);
454 dialog.exec();
455 }
456 }
457
458 void DolphinPart::createDir()
459 {
460 KonqOperations::newDir(m_view, url());
461 }
462
463 void DolphinPart::slotSortOrderChanged(Qt::SortOrder order)
464 {
465 KToggleAction* descending = static_cast<KToggleAction*>(actionCollection()->action("descending"));
466 const bool sortDescending = (order == Qt::DescendingOrder);
467 descending->setChecked(sortDescending);
468 }
469
470 void DolphinPart::slotAdditionalInfoChanged()
471 {
472 m_view->updateAdditionalInfoActions(actionCollection());
473 }
474
475 void DolphinPart::slotShowPreviewChanged()
476 {
477 updateViewActions(); // see DolphinMainWindow
478 }
479
480 void DolphinPart::slotShowHiddenFilesChanged()
481 {
482 QAction* showHiddenFilesAction = actionCollection()->action("show_hidden_files");
483 showHiddenFilesAction->setChecked(m_view->showHiddenFiles());
484 }
485
486 void DolphinPart::slotCategorizedSortingChanged()
487 {
488 // Duplicated from DolphinMainWindow too.
489 QAction* showInGroupsAction = actionCollection()->action("show_in_groups");
490 showInGroupsAction->setChecked(m_view->categorizedSorting());
491 showInGroupsAction->setEnabled(m_view->supportsCategorizedSorting());
492 }
493
494 // TODO a DolphinViewActionHandler for reducing the duplication in the action handling
495
496 #include "dolphinpart.moc"