]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
Assure that non-default messages don't get hidden after a very short time by default...
[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 "dolphinviewactionhandler.h"
22 #include "dolphinsortfilterproxymodel.h"
23 #include "dolphinview.h"
24 #include "dolphinmodel.h"
25 #include "dolphinnewmenuobserver.h"
26 #include "dolphinremoteencoding.h"
27
28 #include <kfileitemlistproperties.h>
29 #include <konq_operations.h>
30
31 #include <kaboutdata.h>
32 #include <kactioncollection.h>
33 #include <kconfiggroup.h>
34 #include <kdirlister.h>
35 #include <kglobalsettings.h>
36 #include <kiconloader.h>
37 #include <klocale.h>
38 #include <kmessagebox.h>
39 #include <kpluginfactory.h>
40 #include <ktoggleaction.h>
41 #include <kio/netaccess.h>
42 #include <ktoolinvocation.h>
43 #include <kauthorized.h>
44 #include <knewmenu.h>
45 #include <kmenu.h>
46
47 #include "settings/dolphinsettings.h"
48
49 #include <QActionGroup>
50 #include <QApplication>
51 #include <QClipboard>
52
53 K_PLUGIN_FACTORY(DolphinPartFactory, registerPlugin<DolphinPart>();)
54 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
55
56 DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QVariantList& args)
57 : KParts::ReadOnlyPart(parent)
58 {
59 Q_UNUSED(args)
60 setComponentData(DolphinPartFactory::componentData(), false);
61 m_extension = new DolphinPartBrowserExtension(this);
62
63 // make sure that other apps using this part find Dolphin's view-file-columns icons
64 KIconLoader::global()->addAppDir("dolphin");
65
66 m_dirLister = new KDirLister;
67 m_dirLister->setAutoUpdate(true);
68 if (parentWidget) {
69 m_dirLister->setMainWindow(parentWidget->window());
70 }
71 m_dirLister->setDelayedMimeTypes(true);
72
73 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
74 connect(m_dirLister, SIGNAL(completed(KUrl)), this, SLOT(slotCompleted(KUrl)));
75 connect(m_dirLister, SIGNAL(canceled(KUrl)), this, SLOT(slotCanceled(KUrl)));
76 connect(m_dirLister, SIGNAL(percent(int)), this, SLOT(updateProgress(int)));
77
78 m_dolphinModel = new DolphinModel(this);
79 m_dolphinModel->setDirLister(m_dirLister);
80
81 m_proxyModel = new DolphinSortFilterProxyModel(this);
82 m_proxyModel->setSourceModel(m_dolphinModel);
83
84 m_view = new DolphinView(parentWidget,
85 KUrl(),
86 m_dirLister,
87 m_dolphinModel,
88 m_proxyModel);
89 m_view->setTabsForFilesEnabled(true);
90 setWidget(m_view);
91
92 setXMLFile("dolphinpart.rc");
93
94 connect(m_view, SIGNAL(infoMessage(QString)),
95 this, SLOT(slotMessage(QString)));
96 connect(m_view, SIGNAL(operationCompletedMessage(QString)),
97 this, SLOT(slotMessage(QString)));
98 connect(m_view, SIGNAL(errorMessage(QString)),
99 this, SLOT(slotErrorMessage(QString)));
100 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
101 this, SLOT(slotItemTriggered(KFileItem)));
102 connect(m_view, SIGNAL(tabRequested(KUrl)),
103 this, SLOT(createNewWindow(KUrl)));
104 connect(m_view, SIGNAL(requestContextMenu(KFileItem,KUrl,QList<QAction*>)),
105 this, SLOT(slotOpenContextMenu(KFileItem,KUrl,QList<QAction*>)));
106 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
107 m_extension, SIGNAL(selectionInfo(KFileItemList)));
108 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
109 this, SLOT(slotSelectionChanged(KFileItemList)));
110 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
111 this, SLOT(slotRequestItemInfo(KFileItem)));
112 connect(m_view, SIGNAL(requestUrlChange(KUrl)),
113 this, SLOT(slotRequestUrlChange(KUrl)));
114 connect(m_view, SIGNAL(modeChanged()),
115 this, SIGNAL(viewModeChanged())); // relay signal
116 connect(m_view, SIGNAL(redirection(KUrl, KUrl)),
117 this, SLOT(slotRedirection(KUrl, KUrl)));
118
119 // Watch for changes that should result in updates to the
120 // status bar text.
121 connect(m_dirLister, SIGNAL(itemsDeleted(const KFileItemList&)),
122 this, SLOT(updateStatusBar()));
123 connect(m_dirLister, SIGNAL(clear()),
124 this, SLOT(updateStatusBar()));
125 connect(m_view, SIGNAL(selectionChanged(const KFileItemList)),
126 this, SLOT(updateStatusBar()));
127
128 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
129 m_actionHandler->setCurrentView(m_view);
130 connect(m_actionHandler, SIGNAL(createDirectory()), SLOT(createDirectory()));
131
132 m_remoteEncoding = new DolphinRemoteEncoding(this, m_actionHandler);
133 connect(this, SIGNAL(aboutToOpenURL()),
134 m_remoteEncoding, SLOT(slotAboutToOpenUrl()));
135
136 QClipboard* clipboard = QApplication::clipboard();
137 connect(clipboard, SIGNAL(dataChanged()),
138 this, SLOT(updatePasteAction()));
139
140 createActions();
141 m_actionHandler->updateViewActions();
142 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
143
144 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
145 // (sort of spacial navigation)
146
147 loadPlugins(this, this, componentData());
148
149 }
150
151 DolphinPart::~DolphinPart()
152 {
153 DolphinSettings::instance().save();
154 DolphinNewMenuObserver::instance().detach(m_newMenu);
155 delete m_dirLister;
156 }
157
158 void DolphinPart::createActions()
159 {
160 // Edit menu
161
162 m_newMenu = new KNewMenu(actionCollection(), widget(), "new_menu");
163 DolphinNewMenuObserver::instance().attach(m_newMenu);
164 connect(m_newMenu->menu(), SIGNAL(aboutToShow()),
165 this, SLOT(updateNewMenu()));
166
167 KAction *editMimeTypeAction = actionCollection()->addAction( "editMimeType" );
168 editMimeTypeAction->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
169 connect(editMimeTypeAction, SIGNAL(triggered()), SLOT(slotEditMimeType()));
170
171 // View menu: all done by DolphinViewActionHandler
172
173 // Go menu
174
175 QActionGroup* goActionGroup = new QActionGroup(this);
176 connect(goActionGroup, SIGNAL(triggered(QAction*)),
177 this, SLOT(slotGoTriggered(QAction*)));
178
179 createGoAction("go_applications", "start-here-kde",
180 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
181 goActionGroup);
182 createGoAction("go_network_folders", "folder-remote",
183 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
184 goActionGroup);
185 createGoAction("go_settings", "preferences-system",
186 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
187 goActionGroup);
188 createGoAction("go_trash", "user-trash",
189 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
190 goActionGroup);
191 createGoAction("go_autostart", "",
192 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
193 goActionGroup);
194
195 // Tools menu
196 if (KAuthorized::authorizeKAction("shell_access")) {
197 KAction* action = actionCollection()->addAction("open_terminal");
198 action->setIcon(KIcon("utilities-terminal"));
199 action->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
200 connect(action, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
201 action->setShortcut(Qt::Key_F4);
202 }
203
204 }
205
206 void DolphinPart::createGoAction(const char* name, const char* iconName,
207 const QString& text, const QString& url,
208 QActionGroup* actionGroup)
209 {
210 KAction* action = actionCollection()->addAction(name);
211 action->setIcon(KIcon(iconName));
212 action->setText(text);
213 action->setData(url);
214 action->setActionGroup(actionGroup);
215 }
216
217 void DolphinPart::slotGoTriggered(QAction* action)
218 {
219 const QString url = action->data().toString();
220 emit m_extension->openUrlRequest(KUrl(url));
221 }
222
223 void DolphinPart::slotSelectionChanged(const KFileItemList& selection)
224 {
225 const bool hasSelection = !selection.isEmpty();
226
227 QAction* renameAction = actionCollection()->action("rename");
228 QAction* moveToTrashAction = actionCollection()->action("move_to_trash");
229 QAction* deleteAction = actionCollection()->action("delete");
230 QAction* editMimeTypeAction = actionCollection()->action("editMimeType");
231 QAction* propertiesAction = actionCollection()->action("properties");
232 QAction* deleteWithTrashShortcut = actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
233
234 if (!hasSelection) {
235 stateChanged("has_no_selection");
236
237 emit m_extension->enableAction("cut", false);
238 emit m_extension->enableAction("copy", false);
239 deleteWithTrashShortcut->setEnabled(false);
240 editMimeTypeAction->setEnabled(false);
241 } else {
242 stateChanged("has_selection");
243
244 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
245 // in libkonq
246 KFileItemListProperties capabilities(selection);
247 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
248
249 renameAction->setEnabled(capabilities.supportsMoving());
250 moveToTrashAction->setEnabled(enableMoveToTrash);
251 deleteAction->setEnabled(capabilities.supportsDeleting());
252 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
253 editMimeTypeAction->setEnabled(true);
254 propertiesAction->setEnabled(true);
255 emit m_extension->enableAction("cut", capabilities.supportsMoving());
256 emit m_extension->enableAction("copy", true);
257 }
258 }
259
260 void DolphinPart::updatePasteAction()
261 {
262 QPair<bool, QString> pasteInfo = m_view->pasteInfo();
263 emit m_extension->enableAction( "paste", pasteInfo.first );
264 emit m_extension->setActionText( "paste", pasteInfo.second );
265 }
266
267 KAboutData* DolphinPart::createAboutData()
268 {
269 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
270 }
271
272 bool DolphinPart::openUrl(const KUrl& url)
273 {
274 bool reload = arguments().reload();
275 // A bit of a workaround so that changing the namefilter works: force reload.
276 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
277 if (m_nameFilter != m_dirLister->nameFilter())
278 reload = true;
279 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
280 return true;
281 }
282 setUrl(url); // remember it at the KParts level
283 KUrl visibleUrl(url);
284 if (!m_nameFilter.isEmpty()) {
285 visibleUrl.addPath(m_nameFilter);
286 }
287 QString prettyUrl = visibleUrl.pathOrUrl();
288 emit setWindowCaption(prettyUrl);
289 emit m_extension->setLocationBarUrl(prettyUrl);
290 emit started(0); // get the wheel to spin
291 m_dirLister->setNameFilter(m_nameFilter);
292 m_view->setUrl(url);
293 updatePasteAction();
294 emit aboutToOpenURL();
295 if (reload)
296 m_view->reload();
297 return true;
298 }
299
300 void DolphinPart::slotCompleted(const KUrl& url)
301 {
302 Q_UNUSED(url)
303 emit completed();
304 }
305
306 void DolphinPart::slotCanceled(const KUrl& url)
307 {
308 slotCompleted(url);
309 }
310
311 void DolphinPart::slotMessage(const QString& msg)
312 {
313 emit setStatusBarText(msg);
314 }
315
316 void DolphinPart::slotErrorMessage(const QString& msg)
317 {
318 KMessageBox::error(m_view, msg);
319 }
320
321 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
322 {
323 emit m_extension->mouseOverInfo(item);
324 if (item.isNull()) {
325 updateStatusBar();
326 } else {
327 ReadOnlyPart::setStatusBarText(item.getStatusBarInfo());
328 }
329 }
330
331 void DolphinPart::slotItemTriggered(const KFileItem& item)
332 {
333 KParts::OpenUrlArguments args;
334 args.setMimeType(item.mimetype());
335
336 // Ideally, konqueror should be changed to not require trustedSource for directory views,
337 // since the idea was not to need BrowserArguments for non-browser stuff...
338 KParts::BrowserArguments browserArgs;
339 browserArgs.trustedSource = true;
340 emit m_extension->openUrlRequest(item.targetUrl(), args, browserArgs);
341 }
342
343 void DolphinPart::createNewWindow(const KUrl& url)
344 {
345 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
346 // should be moved into DolphinPart::slotItemTriggered()
347 emit m_extension->createNewWindow(url);
348 }
349
350 void DolphinPart::slotOpenContextMenu(const KFileItem& _item,
351 const KUrl&,
352 const QList<QAction*>& customActions)
353 {
354 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
355 | KParts::BrowserExtension::ShowProperties
356 | KParts::BrowserExtension::ShowUrlOperations;
357
358 KFileItem item(_item);
359
360 if (item.isNull()) { // viewport context menu
361 popupFlags |= KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowUp;
362 item = m_dirLister->rootItem();
363 if (item.isNull())
364 item = KFileItem( S_IFDIR, (mode_t)-1, url() );
365 }
366
367 KParts::BrowserExtension::ActionGroupMap actionGroups;
368 QList<QAction *> editActions;
369
370 editActions += customActions;
371
372 if (!_item.isNull()) { // only for context menu on one or more items
373 bool sDeleting = true;
374 bool sMoving = true;
375
376 // If the parent directory of the selected item is writable, moving
377 // and deleting are possible.
378 KFileItem parentDir = m_dirLister->rootItem();
379 if (!parentDir.isNull() && !parentDir.isWritable()) {
380 popupFlags |= KParts::BrowserExtension::NoDeletion;
381 sDeleting = false;
382 sMoving = false;
383 }
384
385 if ( sMoving )
386 editActions.append(actionCollection()->action("rename"));
387
388 bool addTrash = false;
389 bool addDel = false;
390
391 bool isIntoTrash = _item.url().protocol() == "trash";
392
393 if ( sMoving && !isIntoTrash && item.isLocalFile() )
394 addTrash = true;
395
396 if ( sDeleting ) {
397 if ( !item.isLocalFile() )
398 addDel = true;
399 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
400 addTrash = false;
401 addDel = true;
402 }
403 else {
404 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
405 KConfigGroup configGroup(globalConfig, "KDE");
406 if ( configGroup.readEntry("ShowDeleteCommand", false) )
407 addDel = true;
408 }
409 }
410
411 if (addTrash)
412 editActions.append(actionCollection()->action("move_to_trash"));
413 if (addDel)
414 editActions.append(actionCollection()->action("delete"));
415
416 // Normally KonqPopupMenu only shows the "Create new" subdir in the current view
417 // since otherwise the created file would not be visible.
418 // But in treeview mode we should allow it.
419 if (m_view->itemsExpandable())
420 popupFlags |= KParts::BrowserExtension::ShowCreateDirectory;
421
422 }
423
424 actionGroups.insert("editactions", editActions);
425
426 // TODO: We should change the signature of the slots (and signals) for being able
427 // to tell for which items we want a popup.
428 KFileItemList items = (m_view->selectedItems().count() ? m_view->selectedItems()
429 : KFileItemList() << item);
430 emit m_extension->popupMenu(QCursor::pos(),
431 items,
432 KParts::OpenUrlArguments(),
433 KParts::BrowserArguments(),
434 popupFlags,
435 actionGroups);
436 }
437
438 void DolphinPart::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
439 {
440 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
441 if (oldUrl == url()) {
442 KParts::ReadOnlyPart::setUrl(newUrl);
443 const QString prettyUrl = newUrl.pathOrUrl();
444 emit m_extension->setLocationBarUrl(prettyUrl);
445 }
446 }
447
448 void DolphinPart::slotRequestUrlChange(const KUrl& url)
449 {
450 if (m_view->url() != url) {
451 // If the view URL is not equal to 'url', then an inner URL change has
452 // been done (e. g. by activating an existing column in the column view).
453 openUrl(url);
454 emit m_extension->openUrlNotify();
455 }
456 }
457
458 ////
459
460 void DolphinPartBrowserExtension::cut()
461 {
462 m_part->view()->cutSelectedItems();
463 }
464
465 void DolphinPartBrowserExtension::copy()
466 {
467 m_part->view()->copySelectedItems();
468 }
469
470 void DolphinPartBrowserExtension::paste()
471 {
472 m_part->view()->paste();
473 }
474
475 void DolphinPartBrowserExtension::reparseConfiguration()
476 {
477 m_part->view()->refresh();
478 }
479
480 ////
481
482 void DolphinPart::slotEditMimeType()
483 {
484 const KFileItemList items = m_view->selectedItems();
485 if (!items.isEmpty()) {
486 KonqOperations::editMimeType(items.first().mimetype(), m_view);
487 }
488 }
489
490 void DolphinPart::setCurrentViewMode(const QString& viewModeName)
491 {
492 QAction* action = actionCollection()->action(viewModeName);
493 Q_ASSERT(action);
494 action->trigger();
495 }
496
497 QString DolphinPart::currentViewMode() const
498 {
499 return m_actionHandler->currentViewModeActionName();
500 }
501
502 void DolphinPart::setNameFilter(const QString& nameFilter)
503 {
504 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
505 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
506 m_nameFilter = nameFilter;
507 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
508 }
509
510 void DolphinPart::slotOpenTerminal()
511 {
512 QString dir(QDir::homePath());
513
514 KUrl u(url());
515
516 // If the given directory is not local, it can still be the URL of an
517 // ioslave using UDS_LOCAL_PATH which to be converted first.
518 u = KIO::NetAccess::mostLocalUrl(u, widget());
519
520 //If the URL is local after the above conversion, set the directory.
521 if (u.isLocalFile()) {
522 dir = u.toLocalFile();
523 }
524
525 KToolInvocation::invokeTerminal(QString(), dir);
526 }
527
528 void DolphinPart::updateNewMenu()
529 {
530 // As requested by KNewMenu :
531 m_newMenu->slotCheckUpToDate();
532 // And set the files that the menu apply on :
533 m_newMenu->setPopupFiles(url());
534 }
535
536 void DolphinPart::updateStatusBar()
537 {
538 emit ReadOnlyPart::setStatusBarText(m_view->statusBarText());
539 }
540
541 void DolphinPart::updateProgress(int percent)
542 {
543 m_extension->loadingProgress(percent);
544 }
545
546 void DolphinPart::createDirectory()
547 {
548 m_newMenu->setPopupFiles(url());
549 m_newMenu->createDirectory();
550 }
551
552 #include "dolphinpart.moc"