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