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