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);
66 m_dirLister
->setMainWindow(parentWidget
->window());
68 m_dirLister
->setDelayedMimeTypes(true);
70 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
71 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
72 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
73 connect(m_dirLister
, SIGNAL(percent(int)), this, SLOT(updateProgress(int)));
75 m_dolphinModel
= new DolphinModel(this);
76 m_dolphinModel
->setDirLister(m_dirLister
);
78 m_proxyModel
= new DolphinSortFilterProxyModel(this);
79 m_proxyModel
->setSourceModel(m_dolphinModel
);
81 m_view
= new DolphinView(parentWidget
,
86 m_view
->setTabsForFilesEnabled(true);
89 setXMLFile("dolphinpart.rc");
91 connect(m_view
, SIGNAL(infoMessage(QString
)),
92 this, SLOT(slotInfoMessage(QString
)));
93 connect(m_view
, SIGNAL(errorMessage(QString
)),
94 this, SLOT(slotErrorMessage(QString
)));
95 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
96 this, SLOT(slotItemTriggered(KFileItem
)));
97 connect(m_view
, SIGNAL(tabRequested(KUrl
)),
98 this, SLOT(createNewWindow(KUrl
)));
99 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
)),
100 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
)));
101 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
102 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
103 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
104 this, SLOT(slotSelectionChanged(KFileItemList
)));
105 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
106 this, SLOT(slotRequestItemInfo(KFileItem
)));
107 connect(m_view
, SIGNAL(requestUrlChange(KUrl
)),
108 this, SLOT(slotRequestUrlChange(KUrl
)));
109 connect(m_view
, SIGNAL(modeChanged()),
110 this, SIGNAL(viewModeChanged())); // relay signal
111 connect(m_view
, SIGNAL(redirection(KUrl
, KUrl
)),
112 this, SLOT(slotRedirection(KUrl
, KUrl
)));
114 // Watch for changes that should result in updates to the
116 connect(m_dirLister
, SIGNAL(deleteItem(const KFileItem
&)),
117 this, SLOT(updateStatusBar()));
118 connect(m_dirLister
, SIGNAL(clear()),
119 this, SLOT(updateStatusBar()));
120 connect(m_view
, SIGNAL(selectionChanged(const KFileItemList
)),
121 this, SLOT(updateStatusBar()));
123 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
124 m_actionHandler
->setCurrentView(m_view
);
126 QClipboard
* clipboard
= QApplication::clipboard();
127 connect(clipboard
, SIGNAL(dataChanged()),
128 this, SLOT(updatePasteAction()));
131 m_actionHandler
->updateViewActions();
132 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
134 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
135 // (sort of spacial navigation)
137 loadPlugins(this, this, componentData());
140 DolphinPart::~DolphinPart()
145 void DolphinPart::createActions()
149 m_newMenu
= new KNewMenu(actionCollection(), widget(), "new_menu");
150 connect(m_newMenu
->menu(), SIGNAL(aboutToShow()),
151 this, SLOT(updateNewMenu()));
153 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
154 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
155 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
157 // View menu: all done by DolphinViewActionHandler
161 QActionGroup
* goActionGroup
= new QActionGroup(this);
162 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
163 this, SLOT(slotGoTriggered(QAction
*)));
165 createGoAction("go_applications", "start-here-kde",
166 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
168 createGoAction("go_network_folders", "folder-remote",
169 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
171 createGoAction("go_settings", "preferences-system",
172 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
174 createGoAction("go_trash", "user-trash",
175 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
177 createGoAction("go_autostart", "",
178 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
182 if (KAuthorized::authorizeKAction("shell_access")) {
183 KAction
* action
= actionCollection()->addAction("open_terminal");
184 action
->setIcon(KIcon("utilities-terminal"));
185 action
->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
186 connect(action
, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
187 action
->setShortcut(Qt::Key_F4
);
192 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
193 const QString
& text
, const QString
& url
,
194 QActionGroup
* actionGroup
)
196 KAction
* action
= actionCollection()->addAction(name
);
197 action
->setIcon(KIcon(iconName
));
198 action
->setText(text
);
199 action
->setData(url
);
200 action
->setActionGroup(actionGroup
);
203 void DolphinPart::slotGoTriggered(QAction
* action
)
205 const QString url
= action
->data().toString();
206 emit m_extension
->openUrlRequest(KUrl(url
));
209 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
211 const bool hasSelection
= !selection
.isEmpty();
213 QAction
* renameAction
= actionCollection()->action("rename");
214 QAction
* moveToTrashAction
= actionCollection()->action("move_to_trash");
215 QAction
* deleteAction
= actionCollection()->action("delete");
216 QAction
* editMimeTypeAction
= actionCollection()->action("editMimeType");
217 QAction
* propertiesAction
= actionCollection()->action("properties");
218 QAction
* deleteWithTrashShortcut
= actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
221 stateChanged("has_no_selection");
223 emit m_extension
->enableAction("cut", false);
224 emit m_extension
->enableAction("copy", false);
225 deleteWithTrashShortcut
->setEnabled(false);
226 editMimeTypeAction
->setEnabled(false);
228 stateChanged("has_selection");
230 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
232 KonqFileItemCapabilities
capabilities(selection
);
233 const bool enableMoveToTrash
= capabilities
.isLocal() && capabilities
.supportsMoving();
235 renameAction
->setEnabled(capabilities
.supportsMoving());
236 moveToTrashAction
->setEnabled(enableMoveToTrash
);
237 deleteAction
->setEnabled(capabilities
.supportsDeleting());
238 deleteWithTrashShortcut
->setEnabled(capabilities
.supportsDeleting() && !enableMoveToTrash
);
239 editMimeTypeAction
->setEnabled(true);
240 propertiesAction
->setEnabled(true);
241 emit m_extension
->enableAction("cut", capabilities
.supportsMoving());
242 emit m_extension
->enableAction("copy", true);
246 void DolphinPart::updatePasteAction()
248 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
249 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
250 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
253 KAboutData
* DolphinPart::createAboutData()
255 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
258 bool DolphinPart::openUrl(const KUrl
& url
)
260 bool reload
= arguments().reload();
261 // A bit of a workaround so that changing the namefilter works: force reload.
262 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
263 if (m_nameFilter
!= m_dirLister
->nameFilter())
265 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
268 setUrl(url
); // remember it at the KParts level
269 KUrl
visibleUrl(url
);
270 if (!m_nameFilter
.isEmpty()) {
271 visibleUrl
.addPath(m_nameFilter
);
273 QString prettyUrl
= visibleUrl
.pathOrUrl();
274 emit
setWindowCaption(prettyUrl
);
275 emit m_extension
->setLocationBarUrl(prettyUrl
);
276 emit
started(0); // get the wheel to spin
277 m_dirLister
->setNameFilter(m_nameFilter
);
279 emit
aboutToOpenURL();
285 void DolphinPart::slotCompleted(const KUrl
& url
)
291 void DolphinPart::slotCanceled(const KUrl
& url
)
296 void DolphinPart::slotInfoMessage(const QString
& msg
)
298 emit
setStatusBarText(msg
);
301 void DolphinPart::slotErrorMessage(const QString
& msg
)
303 KMessageBox::error(m_view
, msg
);
306 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
308 emit m_extension
->mouseOverInfo(item
);
312 ReadOnlyPart::setStatusBarText(item
.getStatusBarInfo());
316 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
318 KParts::OpenUrlArguments args
;
319 args
.setMimeType(item
.mimetype());
321 // Ideally, konqueror should be changed to not require trustedSource for directory views,
322 // since the idea was not to need BrowserArguments for non-browser stuff...
323 KParts::BrowserArguments browserArgs
;
324 browserArgs
.trustedSource
= true;
325 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
328 void DolphinPart::createNewWindow(const KUrl
& url
)
330 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
331 // should be moved into DolphinPart::slotItemTriggered()
332 KFileItem
item(S_IFDIR
, (mode_t
)-1, url
);
333 KParts::OpenUrlArguments args
;
334 args
.setMimeType(item
.mimetype());
335 emit m_extension
->createNewWindow(url
, args
);
338 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
340 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
341 | KParts::BrowserExtension::ShowProperties
342 | KParts::BrowserExtension::ShowUrlOperations
;
344 KFileItem
item(_item
);
346 if (item
.isNull()) { // viewport context menu
347 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
348 item
= m_dirLister
->rootItem();
350 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
353 KParts::BrowserExtension::ActionGroupMap actionGroups
;
354 QList
<QAction
*> editActions
;
356 if (!_item
.isNull()) { // only for context menu on one or more items
357 bool sDeleting
= true;
360 // If the parent directory of the selected item is writable, moving
361 // and deleting are possible.
362 KFileItem parentDir
= m_dirLister
->rootItem();
363 if (!parentDir
.isNull() && !parentDir
.isWritable()) {
364 popupFlags
|= KParts::BrowserExtension::NoDeletion
;
370 editActions
.append(actionCollection()->action("rename"));
372 bool addTrash
= false;
375 bool isIntoTrash
= _item
.url().protocol() == "trash";
377 if ( sMoving
&& !isIntoTrash
&& item
.isLocalFile() )
381 if ( !item
.isLocalFile() )
383 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
388 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
389 KConfigGroup
configGroup(globalConfig
, "KDE");
390 if ( configGroup
.readEntry("ShowDeleteCommand", false) )
396 editActions
.append(actionCollection()->action("move_to_trash"));
398 editActions
.append(actionCollection()->action("delete"));
399 actionGroups
.insert("editactions", editActions
);
401 // Normally KonqPopupMenu only shows the "Create new" subdir in the current view
402 // since otherwise the created file would not be visible.
403 // But in treeview mode we should allow it.
404 if (m_view
->itemsExpandable())
405 popupFlags
|= KParts::BrowserExtension::ShowCreateDirectory
;
409 // TODO: We should change the signature of the slots (and signals) for being able
410 // to tell for which items we want a popup.
411 KFileItemList items
= (m_view
->selectedItems().count() ? m_view
->selectedItems()
412 : KFileItemList() << item
);
413 emit m_extension
->popupMenu(QCursor::pos(),
415 KParts::OpenUrlArguments(),
416 KParts::BrowserArguments(),
421 void DolphinPart::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
423 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
424 if (oldUrl
== url()) {
425 KParts::ReadOnlyPart::setUrl(newUrl
);
426 const QString prettyUrl
= newUrl
.pathOrUrl();
427 emit m_extension
->setLocationBarUrl(prettyUrl
);
431 void DolphinPart::slotRequestUrlChange(const KUrl
& url
)
433 if (m_view
->url() != url
) {
434 // If the view URL is not equal to 'url', then an inner URL change has
435 // been done (e. g. by activating an existing column in the column view).
437 emit m_extension
->openUrlNotify();
443 void DolphinPartBrowserExtension::cut()
445 m_part
->view()->cutSelectedItems();
448 void DolphinPartBrowserExtension::copy()
450 m_part
->view()->copySelectedItems();
453 void DolphinPartBrowserExtension::paste()
455 m_part
->view()->paste();
458 void DolphinPartBrowserExtension::reparseConfiguration()
460 m_part
->view()->refresh();
465 void DolphinPart::slotEditMimeType()
467 const KFileItemList items
= m_view
->selectedItems();
468 if (!items
.isEmpty()) {
469 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
473 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
475 QAction
* action
= actionCollection()->action(viewModeName
);
480 QString
DolphinPart::currentViewMode() const
482 return m_actionHandler
->currentViewModeActionName();
485 void DolphinPart::setNameFilter(const QString
& nameFilter
)
487 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
488 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
489 m_nameFilter
= nameFilter
;
490 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
493 void DolphinPart::slotOpenTerminal()
495 QString
dir(QDir::homePath());
499 // If the given directory is not local, it can still be the URL of an
500 // ioslave using UDS_LOCAL_PATH which to be converted first.
501 u
= KIO::NetAccess::mostLocalUrl(u
, widget());
503 //If the URL is local after the above conversion, set the directory.
504 if (u
.isLocalFile()) {
508 KToolInvocation::invokeTerminal(QString(), dir
);
511 void DolphinPart::updateNewMenu()
513 // As requested by KNewMenu :
514 m_newMenu
->slotCheckUpToDate();
515 // And set the files that the menu apply on :
516 m_newMenu
->setPopupFiles(url());
519 void DolphinPart::updateStatusBar()
521 emit
ReadOnlyPart::setStatusBarText(m_view
->statusBarText());
524 void DolphinPart::updateProgress(int percent
)
526 m_extension
->loadingProgress(percent
);
529 #include "dolphinpart.moc"