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 <kpropertiesdialog.h>
22 #include <kglobalsettings.h>
23 #include "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "dolphinmodel.h"
27 #include <konq_operations.h>
29 #include <kactioncollection.h>
30 #include <kdirlister.h>
31 #include <kiconloader.h>
32 #include <kmessagebox.h>
33 #include <kparts/genericfactory.h>
34 #include <ktoggleaction.h>
35 #include <kconfiggroup.h>
37 #include <QActionGroup>
38 #include <QApplication>
41 typedef KParts::GenericFactory
<DolphinPart
> DolphinPartFactory
;
42 K_EXPORT_COMPONENT_FACTORY(dolphinpart
, DolphinPartFactory
)
44 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QStringList
& args
)
45 : KParts::ReadOnlyPart(parent
)
48 setComponentData( DolphinPartFactory::componentData() );
49 m_extension
= new DolphinPartBrowserExtension(this);
51 // make sure that other apps using this part find Dolphin's view-file-columns icons
52 KIconLoader::global()->addAppDir("dolphin");
54 m_dirLister
= new KDirLister
;
55 m_dirLister
->setAutoUpdate(true);
56 m_dirLister
->setMainWindow(parentWidget
->topLevelWidget());
57 m_dirLister
->setDelayedMimeTypes(true);
59 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
60 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
61 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
63 m_dolphinModel
= new DolphinModel(this);
64 m_dolphinModel
->setDirLister(m_dirLister
);
66 m_proxyModel
= new DolphinSortFilterProxyModel(this);
67 m_proxyModel
->setSourceModel(m_dolphinModel
);
69 m_view
= new DolphinView(parentWidget
,
76 setXMLFile("dolphinpart.rc");
78 connect(m_view
, SIGNAL(infoMessage(QString
)),
79 this, SLOT(slotInfoMessage(QString
)));
80 connect(m_view
, SIGNAL(errorMessage(QString
)),
81 this, SLOT(slotErrorMessage(QString
)));
82 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
83 this, SLOT(slotItemTriggered(KFileItem
)));
84 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
)),
85 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
)));
86 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
87 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
88 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
89 this, SLOT(slotSelectionChanged(KFileItemList
)));
90 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
91 this, SLOT(slotRequestItemInfo(KFileItem
)));
92 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
93 this, SLOT(slotUrlChanged(KUrl
)));
94 connect(m_view
, SIGNAL(modeChanged()),
95 this, SLOT(updateViewActions()));
97 QClipboard
* clipboard
= QApplication::clipboard();
98 connect(clipboard
, SIGNAL(dataChanged()),
99 this, SLOT(updatePasteAction()));
103 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
105 // TODO provide the viewmode actions in the menu, merged with the existing view-mode-actions somehow
106 // [Q_PROPERTY introspection?]
108 // TODO sort_by_* actions
109 // TODO show_*_info actions
111 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
112 // (sort of spacial navigation)
115 DolphinPart::~DolphinPart()
120 void DolphinPart::createActions()
122 QActionGroup
* viewModeActions
= new QActionGroup(this);
123 viewModeActions
->addAction(DolphinView::iconsModeAction(actionCollection()));
124 viewModeActions
->addAction(DolphinView::detailsModeAction(actionCollection()));
125 viewModeActions
->addAction(DolphinView::columnsModeAction(actionCollection()));
126 connect(viewModeActions
, SIGNAL(triggered(QAction
*)), this, SLOT(slotViewModeActionTriggered(QAction
*)));
128 KAction
* renameAction
= DolphinView::createRenameAction(actionCollection());
129 connect(renameAction
, SIGNAL(triggered()), m_view
, SLOT(renameSelectedItems()));
131 KAction
* moveToTrashAction
= DolphinView::createMoveToTrashAction(actionCollection());
132 connect(moveToTrashAction
, SIGNAL(triggered(Qt::MouseButtons
, Qt::KeyboardModifiers
)),
133 this, SLOT(slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers
)));
135 KAction
* deleteAction
= DolphinView::createDeleteAction(actionCollection());
136 connect(deleteAction
, SIGNAL(triggered()), m_view
, SLOT(deleteSelectedItems()));
138 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
139 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
140 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
142 KAction
*propertiesAction
= actionCollection()->addAction( "properties" );
143 propertiesAction
->setText( i18nc("@action:inmenu Edit", "Properties") );
144 propertiesAction
->setShortcut(Qt::ALT
+Qt::Key_Return
);
145 connect(propertiesAction
, SIGNAL(triggered()), SLOT(slotProperties()));
149 KAction
* newDirAction
= DolphinView::createNewDirAction(actionCollection());
150 connect(newDirAction
, SIGNAL(triggered()), SLOT(createDir()));
152 QActionGroup
* goActionGroup
= new QActionGroup(this);
153 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
154 this, SLOT(slotGoTriggered(QAction
*)));
156 createGoAction("go_applications", "start-here-kde",
157 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
159 createGoAction("go_network_folders", "folder-remote",
160 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
162 createGoAction("go_settings", "preferences-system",
163 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
165 createGoAction("go_trash", "user-trash",
166 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
168 createGoAction("go_autostart", "",
169 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
173 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
174 const QString
& text
, const QString
& url
,
175 QActionGroup
* actionGroup
)
177 KAction
* action
= actionCollection()->addAction(name
);
178 action
->setIcon(KIcon(iconName
));
179 action
->setText(text
);
180 action
->setData(url
);
181 action
->setActionGroup(actionGroup
);
184 void DolphinPart::slotGoTriggered(QAction
* action
)
186 const QString url
= action
->data().toString();
187 emit m_extension
->openUrlRequest(KUrl(url
));
190 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
192 const bool hasSelection
= !selection
.isEmpty();
194 stateChanged("has_no_selection");
196 stateChanged("has_selection");
200 actions
<< "rename" << "move_to_trash" << "delete" << "editMimeType" << "properties";
201 foreach(const QString
& actionName
, actions
) {
202 QAction
* action
= actionCollection()->action(actionName
);
205 action
->setEnabled(hasSelection
);
209 emit m_extension
->enableAction("cut", hasSelection
);
210 emit m_extension
->enableAction("copy", hasSelection
);
213 void DolphinPart::updatePasteAction()
215 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
216 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
217 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
220 void DolphinPart::updateViewActions()
222 QAction
* action
= actionCollection()->action(m_view
->currentViewModeActionName());
224 KToggleAction
* toggleAction
= static_cast<KToggleAction
*>(action
);
225 toggleAction
->setChecked(true);
229 KAboutData
* DolphinPart::createAboutData()
231 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
234 bool DolphinPart::openUrl(const KUrl
& url
)
236 const bool reload
= arguments().reload();
237 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
240 setUrl(url
); // remember it at the KParts level
241 const QString prettyUrl
= url
.pathOrUrl();
242 emit
setWindowCaption(prettyUrl
);
243 emit m_extension
->setLocationBarUrl(prettyUrl
);
244 emit
started(0); // get the wheel to spin
251 void DolphinPart::slotCompleted(const KUrl
& url
)
257 void DolphinPart::slotCanceled(const KUrl
& url
)
262 void DolphinPart::slotInfoMessage(const QString
& msg
)
264 emit
setStatusBarText(msg
);
267 void DolphinPart::slotErrorMessage(const QString
& msg
)
269 KMessageBox::error(m_view
, msg
);
272 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
274 emit m_extension
->mouseOverInfo(item
);
277 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
279 KParts::OpenUrlArguments args
;
280 args
.setMimeType(item
.mimetype());
282 // Ideally, konqueror should be changed to not require trustedSource for directory views,
283 // since the idea was not to need BrowserArguments for non-browser stuff...
284 KParts::BrowserArguments browserArgs
;
285 browserArgs
.trustedSource
= true;
287 // MMB click support.
288 // TODO: this doesn't work, mouseButtons() is always 0.
289 // Issue N176832 for the missing QAIV signal; task 177399
290 kDebug() << QApplication::mouseButtons();
291 if (QApplication::mouseButtons() & Qt::MidButton
) {
292 kDebug() << "MMB!!" << item
.mimetype();
293 if (item
.mimeTypePtr()->is("inode/directory")) {
294 emit m_extension
->createNewWindow(item
.url(), args
);
300 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
302 emit m_extension
->openUrlRequest(item
.url(), args
, browserArgs
);
306 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
308 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
309 | KParts::BrowserExtension::ShowProperties
310 | KParts::BrowserExtension::ShowUrlOperations
;
311 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
312 // popupFlags |= KParts::BrowserExtension::NoDeletion;
314 KFileItem
item(_item
);
316 if (item
.isNull()) { // viewport context menu
317 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
318 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
319 // and use this as fallback:
320 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
323 KParts::BrowserExtension::ActionGroupMap actionGroups
;
324 QList
<QAction
*> editActions
;
326 if (!item
.isNull()) { // only for context menu on one or more items
327 // TODO if ( sMoving )
328 editActions
.append(actionCollection()->action("rename"));
330 bool addTrash
= false;
333 // TODO if ( sMoving && !isIntoTrash && !isTrashLink )
336 /* TODO if ( sDeleting ) */ {
337 if ( !item
.isLocalFile() )
339 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
344 KConfigGroup
configGroup( KGlobal::config(), "KDE" );
345 if ( configGroup
.readEntry( "ShowDeleteCommand", false) )
351 editActions
.append(actionCollection()->action("move_to_trash"));
353 editActions
.append(actionCollection()->action("delete"));
354 actionGroups
.insert("editactions", editActions
);
356 KFileItemList items
; items
.append(item
);
357 emit m_extension
->popupMenu(QCursor::pos(),
359 KParts::OpenUrlArguments(),
360 KParts::BrowserArguments(),
366 void DolphinPart::slotViewModeActionTriggered(QAction
* action
)
368 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
369 m_view
->setMode(mode
);
372 void DolphinPart::slotUrlChanged(const KUrl
& url
)
374 if (m_view
->url() != url
) {
375 // If the view URL is not equal to 'url', then an inner URL change has
376 // been done (e. g. by activating an existing column in the column view).
378 emit m_extension
->openUrlNotify();
384 void DolphinPartBrowserExtension::cut()
386 m_part
->view()->cutSelectedItems();
389 void DolphinPartBrowserExtension::copy()
391 m_part
->view()->copySelectedItems();
394 void DolphinPartBrowserExtension::paste()
396 m_part
->view()->paste();
401 void DolphinPart::slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers modifiers
)
403 // Note: kde3's konq_mainwindow.cpp used to check
404 // reason == KAction::PopupMenuActivation && ...
405 // but this isn't supported anymore
406 if (modifiers
& Qt::ShiftModifier
)
407 m_view
->deleteSelectedItems();
409 m_view
->trashSelectedItems();
412 void DolphinPart::slotEditMimeType()
414 const KFileItemList items
= m_view
->selectedItems();
415 if (!items
.isEmpty()) {
416 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
420 void DolphinPart::slotProperties()
422 const KFileItemList items
= m_view
->selectedItems();
423 if (!items
.isEmpty()) {
424 KPropertiesDialog
dialog(items
.first().url(), m_view
);
429 void DolphinPart::createDir()
431 KonqOperations::newDir(m_view
, url());
434 #include "dolphinpart.moc"