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"
22 #include "dolphinviewactionhandler.h"
23 #include "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "dolphinmodel.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 <QActionGroup>
47 #include <QApplication>
50 K_PLUGIN_FACTORY(DolphinPartFactory
, registerPlugin
<DolphinPart
>();)
51 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
53 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QVariantList
& args
)
54 : KParts::ReadOnlyPart(parent
)
57 setComponentData(DolphinPartFactory::componentData(), false);
58 m_extension
= new DolphinPartBrowserExtension(this);
60 // make sure that other apps using this part find Dolphin's view-file-columns icons
61 KIconLoader::global()->addAppDir("dolphin");
63 m_dirLister
= new KDirLister
;
64 m_dirLister
->setAutoUpdate(true);
65 m_dirLister
->setMainWindow(parentWidget
->window());
66 m_dirLister
->setDelayedMimeTypes(true);
68 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
69 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
70 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
71 connect(m_dirLister
, SIGNAL(percent(int)), this, SLOT(updateProgress(int)));
73 m_dolphinModel
= new DolphinModel(this);
74 m_dolphinModel
->setDirLister(m_dirLister
);
76 m_proxyModel
= new DolphinSortFilterProxyModel(this);
77 m_proxyModel
->setSourceModel(m_dolphinModel
);
79 m_view
= new DolphinView(parentWidget
,
84 m_view
->setTabsForFilesEnabled(true);
87 setXMLFile("dolphinpart.rc");
89 connect(m_view
, SIGNAL(infoMessage(QString
)),
90 this, SLOT(slotInfoMessage(QString
)));
91 connect(m_view
, SIGNAL(errorMessage(QString
)),
92 this, SLOT(slotErrorMessage(QString
)));
93 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
94 this, SLOT(slotItemTriggered(KFileItem
)));
95 connect(m_view
, SIGNAL(tabRequested(KUrl
)),
96 this, SLOT(createNewWindow(KUrl
)));
97 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
)),
98 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
)));
99 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
100 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
101 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
102 this, SLOT(slotSelectionChanged(KFileItemList
)));
103 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
104 this, SLOT(slotRequestItemInfo(KFileItem
)));
105 connect(m_view
, SIGNAL(requestUrlChange(KUrl
)),
106 this, SLOT(slotRequestUrlChange(KUrl
)));
107 connect(m_view
, SIGNAL(modeChanged()),
108 this, SIGNAL(viewModeChanged())); // relay signal
109 connect(m_view
, SIGNAL(redirection(KUrl
, KUrl
)),
110 this, SLOT(slotRedirection(KUrl
, KUrl
)));
112 // Watch for changes that should result in updates to the
114 connect(m_dirLister
, SIGNAL(deleteItem(const KFileItem
&)),
115 this, SLOT(updateStatusBar()));
116 connect(m_dirLister
, SIGNAL(clear()),
117 this, SLOT(updateStatusBar()));
118 connect(m_view
, SIGNAL(selectionChanged(const KFileItemList
)),
119 this, SLOT(updateStatusBar()));
121 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
122 m_actionHandler
->setCurrentView(m_view
);
124 QClipboard
* clipboard
= QApplication::clipboard();
125 connect(clipboard
, SIGNAL(dataChanged()),
126 this, SLOT(updatePasteAction()));
129 m_actionHandler
->updateViewActions();
130 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
132 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
133 // (sort of spacial navigation)
135 loadPlugins(this, this, componentData());
138 DolphinPart::~DolphinPart()
143 void DolphinPart::createActions()
147 m_newMenu
= new KNewMenu(actionCollection(), widget(), "new_menu");
148 connect(m_newMenu
->menu(), SIGNAL(aboutToShow()),
149 this, SLOT(updateNewMenu()));
151 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
152 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
153 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
155 // View menu: all done by DolphinViewActionHandler
159 QActionGroup
* goActionGroup
= new QActionGroup(this);
160 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
161 this, SLOT(slotGoTriggered(QAction
*)));
163 createGoAction("go_applications", "start-here-kde",
164 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
166 createGoAction("go_network_folders", "folder-remote",
167 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
169 createGoAction("go_settings", "preferences-system",
170 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
172 createGoAction("go_trash", "user-trash",
173 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
175 createGoAction("go_autostart", "",
176 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
180 if (KAuthorized::authorizeKAction("shell_access")) {
181 KAction
* action
= actionCollection()->addAction("open_terminal");
182 action
->setIcon(KIcon("utilities-terminal"));
183 action
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
184 connect(action
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
185 action
->setShortcut(Qt::Key_F4
);
190 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
191 const QString
& text
, const QString
& url
,
192 QActionGroup
* actionGroup
)
194 KAction
* action
= actionCollection()->addAction(name
);
195 action
->setIcon(KIcon(iconName
));
196 action
->setText(text
);
197 action
->setData(url
);
198 action
->setActionGroup(actionGroup
);
201 void DolphinPart::slotGoTriggered(QAction
* action
)
203 const QString url
= action
->data().toString();
204 emit m_extension
->openUrlRequest(KUrl(url
));
207 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
209 const bool hasSelection
= !selection
.isEmpty();
211 QAction
* renameAction
= actionCollection()->action("rename");
212 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
213 QAction
* deleteAction
= actionCollection()->action("delete");
214 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
215 QAction
* propertiesAction
= actionCollection()->action("properties");
216 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
219 stateChanged("has_no_selection");
221 emit m_extension
->enableAction("cut", false);
222 emit m_extension
->enableAction("copy", false);
223 deleteWithTrashShortcut
->setEnabled(false);
224 editMimeTypeAction
->setEnabled(false);
226 stateChanged("has_selection");
228 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
230 KonqFileItemCapabilities
capabilities(selection
);
231 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
233 renameAction
->setEnabled(capabilities
.supportsMoving());
234 moveToTrashAction
->setEnabled(enableMoveToTrash
);
235 deleteAction
->setEnabled(capabilities
.supportsDeleting());
236 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
237 editMimeTypeAction
->setEnabled(true);
238 propertiesAction
->setEnabled(true);
239 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
240 emit m_extension
->enableAction("copy", true);
244 void DolphinPart::updatePasteAction()
246 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
247 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
248 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
251 KAboutData
* DolphinPart::createAboutData()
253 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
256 bool DolphinPart::openUrl(const KUrl
& url
)
258 bool reload
= arguments().reload();
259 // A bit of a workaround so that changing the namefilter works: force reload.
260 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
261 if (m_nameFilter
!= m_dirLister
->nameFilter())
263 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
266 setUrl(url
); // remember it at the KParts level
267 KUrl
visibleUrl(url
);
268 if (!m_nameFilter
.isEmpty()) {
269 visibleUrl
.addPath(m_nameFilter
);
271 QString prettyUrl
= visibleUrl
.pathOrUrl();
272 emit
setWindowCaption(prettyUrl
);
273 emit m_extension
->setLocationBarUrl(prettyUrl
);
274 emit
started(0); // get the wheel to spin
275 m_dirLister
->setNameFilter(m_nameFilter
);
277 emit
aboutToOpenURL();
283 void DolphinPart::slotCompleted(const KUrl
& url
)
289 void DolphinPart::slotCanceled(const KUrl
& url
)
294 void DolphinPart::slotInfoMessage(const QString
& msg
)
296 emit
setStatusBarText(msg
);
299 void DolphinPart::slotErrorMessage(const QString
& msg
)
301 KMessageBox::error(m_view
, msg
);
304 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
306 emit m_extension
->mouseOverInfo(item
);
310 ReadOnlyPart::setStatusBarText(item
.getStatusBarInfo());
314 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
316 KParts::OpenUrlArguments args
;
317 args
.setMimeType(item
.mimetype());
319 // Ideally, konqueror should be changed to not require trustedSource for directory views,
320 // since the idea was not to need BrowserArguments for non-browser stuff...
321 KParts::BrowserArguments browserArgs
;
322 browserArgs
.trustedSource
= true;
323 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
326 void DolphinPart::createNewWindow(const KUrl
& url
)
328 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
329 // should be moved into DolphinPart::slotItemTriggered()
330 KFileItem
item(S_IFDIR
, (mode_t
)-1, url
);
331 KParts::OpenUrlArguments args
;
332 args
.setMimeType(item
.mimetype());
333 emit m_extension
->createNewWindow(url
, args
);
336 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
338 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
339 | KParts::BrowserExtension::ShowProperties
340 | KParts::BrowserExtension::ShowUrlOperations
;
342 KFileItem
item(_item
);
344 if (item
.isNull()) { // viewport context menu
345 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
346 item
= m_dirLister
->rootItem();
348 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
351 KParts::BrowserExtension::ActionGroupMap actionGroups
;
352 QList
<QAction
*> editActions
;
354 if (!_item
.isNull()) { // only for context menu on one or more items
355 bool sDeleting
= true;
358 // If the parent directory of the selected item is writable, moving
359 // and deleting are possible.
360 KFileItem parentDir
= m_dirLister
->rootItem();
361 if (!parentDir
.isNull() && !parentDir
.isWritable()) {
362 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
368 editActions
.append(actionCollection()->action("rename"));
370 bool addTrash
= false;
373 bool isIntoTrash
= _item
.url().protocol() == "trash";
375 if ( sMoving
&& !isIntoTrash
&& item
.isLocalFile() )
379 if ( !item
.isLocalFile() )
381 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
386 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
387 KConfigGroup
configGroup(globalConfig
, "KDE");
388 if ( configGroup
.readEntry("ShowDeleteCommand", false) )
394 editActions
.append(actionCollection()->action("move_to_trash"));
396 editActions
.append(actionCollection()->action("delete"));
397 actionGroups
.insert("editactions", editActions
);
399 // Normally KonqPopupMenu only shows the "Create new" subdir in the current view
400 // since otherwise the created file would not be visible.
401 // But in treeview mode we should allow it.
402 if (m_view
->itemsExpandable())
403 popupFlags
|= KParts::BrowserExtension::ShowCreateDirectory
;
407 // TODO: We should change the signature of the slots (and signals) for being able
408 // to tell for which items we want a popup.
409 KFileItemList items
= (m_view
->selectedItems().count() ? m_view
->selectedItems()
410 : KFileItemList() << item
);
411 emit m_extension
->popupMenu(QCursor::pos(),
413 KParts::OpenUrlArguments(),
414 KParts::BrowserArguments(),
419 void DolphinPart::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
421 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
422 if (oldUrl
== url()) {
423 KParts::ReadOnlyPart::setUrl(newUrl
);
424 const QString prettyUrl
= newUrl
.pathOrUrl();
425 emit m_extension
->setLocationBarUrl(prettyUrl
);
429 void DolphinPart::slotRequestUrlChange(const KUrl
& url
)
431 if (m_view
->url() != url
) {
432 // If the view URL is not equal to 'url', then an inner URL change has
433 // been done (e. g. by activating an existing column in the column view).
435 emit m_extension
->openUrlNotify();
441 void DolphinPartBrowserExtension::cut()
443 m_part
->view()->cutSelectedItems();
446 void DolphinPartBrowserExtension::copy()
448 m_part
->view()->copySelectedItems();
451 void DolphinPartBrowserExtension::paste()
453 m_part
->view()->paste();
456 void DolphinPartBrowserExtension::reparseConfiguration()
458 m_part
->view()->refresh();
463 void DolphinPart::slotEditMimeType()
465 const KFileItemList items
= m_view
->selectedItems();
466 if (!items
.isEmpty()) {
467 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
471 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
473 QAction
* action
= actionCollection()->action(viewModeName
);
478 QString
DolphinPart::currentViewMode() const
480 return m_actionHandler
->currentViewModeActionName();
483 void DolphinPart::setNameFilter(const QString
& nameFilter
)
485 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
486 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
487 m_nameFilter
= nameFilter
;
488 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
491 void DolphinPart::slotOpenTerminal()
493 QString
dir(QDir::homePath());
497 // If the given directory is not local, it can still be the URL of an
498 // ioslave using UDS_LOCAL_PATH which to be converted first.
499 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
501 //If the URL is local after the above conversion, set the directory.
502 if (u
.isLocalFile()) {
506 KToolInvocation::invokeTerminal(QString(), dir
);
509 void DolphinPart::updateNewMenu()
511 // As requested by KNewMenu :
512 m_newMenu
->slotCheckUpToDate();
513 // And set the files that the menu apply on :
514 m_newMenu
->setPopupFiles(url());
517 void DolphinPart::updateStatusBar()
519 emit
ReadOnlyPart::setStatusBarText(m_view
->statusBarText());
522 void DolphinPart::updateProgress(int percent
)
524 m_extension
->loadingProgress(percent
);
527 #include "dolphinpart.moc"