1 /* This file is part of the KDE project
2 Copyright (c) 2007 David Faure <faure@kde.org>
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.
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.
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.
20 #include "dolphinpart.h"
21 #include "dolphinviewactionhandler.h"
22 #include "dolphinsortfilterproxymodel.h"
23 #include "dolphinview.h"
24 #include "dolphinmodel.h"
25 #include "dolphinnewmenuobserver.h"
27 #include <konq_fileitemcapabilities.h>
28 #include <konq_operations.h>
30 #include <kaboutdata.h>
31 #include <kactioncollection.h>
32 #include <kconfiggroup.h>
33 #include <kdirlister.h>
34 #include <kglobalsettings.h>
35 #include <kiconloader.h>
37 #include <kmessagebox.h>
38 #include <kpluginfactory.h>
39 #include <ktoggleaction.h>
40 #include <kio/netaccess.h>
41 #include <ktoolinvocation.h>
42 #include <kauthorized.h>
46 #include "settings/dolphinsettings.h"
48 #include <QActionGroup>
49 #include <QApplication>
52 K_PLUGIN_FACTORY(DolphinPartFactory
, registerPlugin
<DolphinPart
>();)
53 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
55 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QVariantList
& args
)
56 : KParts::ReadOnlyPart(parent
)
59 setComponentData(DolphinPartFactory::componentData(), false);
60 m_extension
= new DolphinPartBrowserExtension(this);
62 // make sure that other apps using this part find Dolphin's view-file-columns icons
63 KIconLoader::global()->addAppDir("dolphin");
65 m_dirLister
= new KDirLister
;
66 m_dirLister
->setAutoUpdate(true);
68 m_dirLister
->setMainWindow(parentWidget
->window());
70 m_dirLister
->setDelayedMimeTypes(true);
72 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
73 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
74 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
75 connect(m_dirLister
, SIGNAL(percent(int)), this, SLOT(updateProgress(int)));
77 m_dolphinModel
= new DolphinModel(this);
78 m_dolphinModel
->setDirLister(m_dirLister
);
80 m_proxyModel
= new DolphinSortFilterProxyModel(this);
81 m_proxyModel
->setSourceModel(m_dolphinModel
);
83 m_view
= new DolphinView(parentWidget
,
88 m_view
->setTabsForFilesEnabled(true);
91 setXMLFile("dolphinpart.rc");
93 connect(m_view
, SIGNAL(infoMessage(QString
)),
94 this, SLOT(slotInfoMessage(QString
)));
95 connect(m_view
, SIGNAL(errorMessage(QString
)),
96 this, SLOT(slotErrorMessage(QString
)));
97 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
98 this, SLOT(slotItemTriggered(KFileItem
)));
99 connect(m_view
, SIGNAL(tabRequested(KUrl
)),
100 this, SLOT(createNewWindow(KUrl
)));
101 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
,QList
<QAction
*>)),
102 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
,QList
<QAction
*>)));
103 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
104 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
105 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
106 this, SLOT(slotSelectionChanged(KFileItemList
)));
107 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
108 this, SLOT(slotRequestItemInfo(KFileItem
)));
109 connect(m_view
, SIGNAL(requestUrlChange(KUrl
)),
110 this, SLOT(slotRequestUrlChange(KUrl
)));
111 connect(m_view
, SIGNAL(modeChanged()),
112 this, SIGNAL(viewModeChanged())); // relay signal
113 connect(m_view
, SIGNAL(redirection(KUrl
, KUrl
)),
114 this, SLOT(slotRedirection(KUrl
, KUrl
)));
116 // Watch for changes that should result in updates to the
118 connect(m_dirLister
, SIGNAL(deleteItem(const KFileItem
&)),
119 this, SLOT(updateStatusBar()));
120 connect(m_dirLister
, SIGNAL(clear()),
121 this, SLOT(updateStatusBar()));
122 connect(m_view
, SIGNAL(selectionChanged(const KFileItemList
)),
123 this, SLOT(updateStatusBar()));
125 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
126 m_actionHandler
->setCurrentView(m_view
);
128 QClipboard
* clipboard
= QApplication::clipboard();
129 connect(clipboard
, SIGNAL(dataChanged()),
130 this, SLOT(updatePasteAction()));
133 m_actionHandler
->updateViewActions();
134 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
136 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
137 // (sort of spacial navigation)
139 loadPlugins(this, this, componentData());
142 DolphinPart::~DolphinPart()
144 DolphinSettings::instance().save();
145 DolphinNewMenuObserver::instance().detach(m_newMenu
);
149 void DolphinPart::createActions()
153 m_newMenu
= new KNewMenu(actionCollection(), widget(), "new_menu");
154 DolphinNewMenuObserver::instance().attach(m_newMenu
);
155 connect(m_newMenu
->menu(), SIGNAL(aboutToShow()),
156 this, SLOT(updateNewMenu()));
158 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
159 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
160 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
162 // View menu: all done by DolphinViewActionHandler
166 QActionGroup
* goActionGroup
= new QActionGroup(this);
167 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
168 this, SLOT(slotGoTriggered(QAction
*)));
170 createGoAction("go_applications", "start-here-kde",
171 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
173 createGoAction("go_network_folders", "folder-remote",
174 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
176 createGoAction("go_settings", "preferences-system",
177 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
179 createGoAction("go_trash", "user-trash",
180 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
182 createGoAction("go_autostart", "",
183 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
187 if (KAuthorized::authorizeKAction("shell_access")) {
188 KAction
* action
= actionCollection()->addAction("open_terminal");
189 action
->setIcon(KIcon("utilities-terminal"));
190 action
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
191 connect(action
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
192 action
->setShortcut(Qt::Key_F4
);
197 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
198 const QString
& text
, const QString
& url
,
199 QActionGroup
* actionGroup
)
201 KAction
* action
= actionCollection()->addAction(name
);
202 action
->setIcon(KIcon(iconName
));
203 action
->setText(text
);
204 action
->setData(url
);
205 action
->setActionGroup(actionGroup
);
208 void DolphinPart::slotGoTriggered(QAction
* action
)
210 const QString url
= action
->data().toString();
211 emit m_extension
->openUrlRequest(KUrl(url
));
214 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
216 const bool hasSelection
= !selection
.isEmpty();
218 QAction
* renameAction
= actionCollection()->action("rename");
219 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
220 QAction
* deleteAction
= actionCollection()->action("delete");
221 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
222 QAction
* propertiesAction
= actionCollection()->action("properties");
223 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
226 stateChanged("has_no_selection");
228 emit m_extension
->enableAction("cut", false);
229 emit m_extension
->enableAction("copy", false);
230 deleteWithTrashShortcut
->setEnabled(false);
231 editMimeTypeAction
->setEnabled(false);
233 stateChanged("has_selection");
235 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
237 KonqFileItemCapabilities
capabilities(selection
);
238 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
240 renameAction
->setEnabled(capabilities
.supportsMoving());
241 moveToTrashAction
->setEnabled(enableMoveToTrash
);
242 deleteAction
->setEnabled(capabilities
.supportsDeleting());
243 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
244 editMimeTypeAction
->setEnabled(true);
245 propertiesAction
->setEnabled(true);
246 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
247 emit m_extension
->enableAction("copy", true);
251 void DolphinPart::updatePasteAction()
253 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
254 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
255 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
258 KAboutData
* DolphinPart::createAboutData()
260 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
263 bool DolphinPart::openUrl(const KUrl
& url
)
265 bool reload
= arguments().reload();
266 // A bit of a workaround so that changing the namefilter works: force reload.
267 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
268 if (m_nameFilter
!= m_dirLister
->nameFilter())
270 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
273 setUrl(url
); // remember it at the KParts level
274 KUrl
visibleUrl(url
);
275 if (!m_nameFilter
.isEmpty()) {
276 visibleUrl
.addPath(m_nameFilter
);
278 QString prettyUrl
= visibleUrl
.pathOrUrl();
279 emit
setWindowCaption(prettyUrl
);
280 emit m_extension
->setLocationBarUrl(prettyUrl
);
281 emit
started(0); // get the wheel to spin
282 m_dirLister
->setNameFilter(m_nameFilter
);
285 emit
aboutToOpenURL();
291 void DolphinPart::slotCompleted(const KUrl
& url
)
297 void DolphinPart::slotCanceled(const KUrl
& url
)
302 void DolphinPart::slotInfoMessage(const QString
& msg
)
304 emit
setStatusBarText(msg
);
307 void DolphinPart::slotErrorMessage(const QString
& msg
)
309 KMessageBox::error(m_view
, msg
);
312 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
314 emit m_extension
->mouseOverInfo(item
);
318 ReadOnlyPart::setStatusBarText(item
.getStatusBarInfo());
322 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
324 KParts::OpenUrlArguments args
;
325 args
.setMimeType(item
.mimetype());
327 // Ideally, konqueror should be changed to not require trustedSource for directory views,
328 // since the idea was not to need BrowserArguments for non-browser stuff...
329 KParts::BrowserArguments browserArgs
;
330 browserArgs
.trustedSource
= true;
331 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
334 void DolphinPart::createNewWindow(const KUrl
& url
)
336 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
337 // should be moved into DolphinPart::slotItemTriggered()
338 KFileItem
item(S_IFDIR
, (mode_t
)-1, url
);
339 KParts::OpenUrlArguments args
;
340 args
.setMimeType(item
.mimetype());
341 emit m_extension
->createNewWindow(url
, args
);
344 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
,
346 const QList
<QAction
*>& customActions
)
348 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
349 | KParts::BrowserExtension::ShowProperties
350 | KParts::BrowserExtension::ShowUrlOperations
;
352 KFileItem
item(_item
);
354 if (item
.isNull()) { // viewport context menu
355 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
356 item
= m_dirLister
->rootItem();
358 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
361 KParts::BrowserExtension::ActionGroupMap actionGroups
;
362 QList
<QAction
*> editActions
;
364 editActions
+= customActions
;
366 if (!_item
.isNull()) { // only for context menu on one or more items
367 bool sDeleting
= true;
370 // If the parent directory of the selected item is writable, moving
371 // and deleting are possible.
372 KFileItem parentDir
= m_dirLister
->rootItem();
373 if (!parentDir
.isNull() && !parentDir
.isWritable()) {
374 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
380 editActions
.append(actionCollection()->action("rename"));
382 bool addTrash
= false;
385 bool isIntoTrash
= _item
.url().protocol() == "trash";
387 if ( sMoving
&& !isIntoTrash
&& item
.isLocalFile() )
391 if ( !item
.isLocalFile() )
393 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
398 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
399 KConfigGroup
configGroup(globalConfig
, "KDE");
400 if ( configGroup
.readEntry("ShowDeleteCommand", false) )
406 editActions
.append(actionCollection()->action("move_to_trash"));
408 editActions
.append(actionCollection()->action("delete"));
410 // Normally KonqPopupMenu only shows the "Create new" subdir in the current view
411 // since otherwise the created file would not be visible.
412 // But in treeview mode we should allow it.
413 if (m_view
->itemsExpandable())
414 popupFlags
|= KParts::BrowserExtension::ShowCreateDirectory
;
418 actionGroups
.insert("editactions", editActions
);
420 // TODO: We should change the signature of the slots (and signals) for being able
421 // to tell for which items we want a popup.
422 KFileItemList items
= (m_view
->selectedItems().count() ? m_view
->selectedItems()
423 : KFileItemList() << item
);
424 emit m_extension
->popupMenu(QCursor::pos(),
426 KParts::OpenUrlArguments(),
427 KParts::BrowserArguments(),
432 void DolphinPart::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
434 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
435 if (oldUrl
== url()) {
436 KParts::ReadOnlyPart::setUrl(newUrl
);
437 const QString prettyUrl
= newUrl
.pathOrUrl();
438 emit m_extension
->setLocationBarUrl(prettyUrl
);
442 void DolphinPart::slotRequestUrlChange(const KUrl
& url
)
444 if (m_view
->url() != url
) {
445 // If the view URL is not equal to 'url', then an inner URL change has
446 // been done (e. g. by activating an existing column in the column view).
448 emit m_extension
->openUrlNotify();
454 void DolphinPartBrowserExtension::cut()
456 m_part
->view()->cutSelectedItems();
459 void DolphinPartBrowserExtension::copy()
461 m_part
->view()->copySelectedItems();
464 void DolphinPartBrowserExtension::paste()
466 m_part
->view()->paste();
469 void DolphinPartBrowserExtension::reparseConfiguration()
471 m_part
->view()->refresh();
476 void DolphinPart::slotEditMimeType()
478 const KFileItemList items
= m_view
->selectedItems();
479 if (!items
.isEmpty()) {
480 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
484 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
486 QAction
* action
= actionCollection()->action(viewModeName
);
491 QString
DolphinPart::currentViewMode() const
493 return m_actionHandler
->currentViewModeActionName();
496 void DolphinPart::setNameFilter(const QString
& nameFilter
)
498 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
499 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
500 m_nameFilter
= nameFilter
;
501 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
504 void DolphinPart::slotOpenTerminal()
506 QString
dir(QDir::homePath());
510 // If the given directory is not local, it can still be the URL of an
511 // ioslave using UDS_LOCAL_PATH which to be converted first.
512 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
514 //If the URL is local after the above conversion, set the directory.
515 if (u
.isLocalFile()) {
516 dir
= u
.toLocalFile();
519 KToolInvocation::invokeTerminal(QString(), dir
);
522 void DolphinPart::updateNewMenu()
524 // As requested by KNewMenu :
525 m_newMenu
->slotCheckUpToDate();
526 // And set the files that the menu apply on :
527 m_newMenu
->setPopupFiles(url());
530 void DolphinPart::updateStatusBar()
532 emit
ReadOnlyPart::setStatusBarText(m_view
->statusBarText());
535 void DolphinPart::updateProgress(int percent
)
537 m_extension
->loadingProgress(percent
);
540 #include "dolphinpart.moc"