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 <kglobalsettings.h>
22 #include "dolphinsortfilterproxymodel.h"
23 #include "dolphinview.h"
24 #include "dolphinmodel.h"
26 #include <konq_operations.h>
28 #include <kactioncollection.h>
29 #include <kdirlister.h>
30 #include <kmessagebox.h>
31 #include <kparts/genericfactory.h>
32 #include <ktoggleaction.h>
33 #include <kconfiggroup.h>
35 #include <QActionGroup>
36 #include <QApplication>
39 typedef KParts::GenericFactory
<DolphinPart
> DolphinPartFactory
;
40 K_EXPORT_COMPONENT_FACTORY(dolphinpart
, DolphinPartFactory
)
42 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QStringList
& args
)
43 : KParts::ReadOnlyPart(parent
)
46 setComponentData( DolphinPartFactory::componentData() );
47 m_extension
= new DolphinPartBrowserExtension(this);
49 m_dirLister
= new KDirLister
;
50 m_dirLister
->setAutoUpdate(true);
51 m_dirLister
->setMainWindow(parentWidget
->topLevelWidget());
52 m_dirLister
->setDelayedMimeTypes(true);
54 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
55 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
56 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
58 m_dolphinModel
= new DolphinModel(this);
59 m_dolphinModel
->setDirLister(m_dirLister
);
61 m_proxyModel
= new DolphinSortFilterProxyModel(this);
62 m_proxyModel
->setSourceModel(m_dolphinModel
);
64 m_view
= new DolphinView(parentWidget
,
71 setXMLFile("dolphinpart.rc");
73 connect(m_view
, SIGNAL(infoMessage(QString
)),
74 this, SLOT(slotInfoMessage(QString
)));
75 connect(m_view
, SIGNAL(errorMessage(QString
)),
76 this, SLOT(slotErrorMessage(QString
)));
77 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
78 this, SLOT(slotItemTriggered(KFileItem
)));
79 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
)),
80 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
)));
81 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
82 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
83 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
84 this, SLOT(slotSelectionChanged(KFileItemList
)));
85 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
86 this, SLOT(slotRequestItemInfo(KFileItem
)));
87 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
88 this, SLOT(slotUrlChanged(KUrl
)));
89 connect(m_view
, SIGNAL(modeChanged()),
90 this, SLOT(updateViewActions()));
92 QClipboard
* clipboard
= QApplication::clipboard();
93 connect(clipboard
, SIGNAL(dataChanged()),
94 this, SLOT(updatePasteAction()));
98 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
100 // TODO provide the viewmode actions in the menu, merged with the existing view-mode-actions somehow
101 // [Q_PROPERTY introspection?]
103 // TODO sort_by_* actions
104 // TODO show_*_info actions
106 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
107 // (sort of spacial navigation)
110 DolphinPart::~DolphinPart()
115 void DolphinPart::createActions()
117 QActionGroup
* viewModeActions
= new QActionGroup(this);
118 viewModeActions
->addAction(DolphinView::iconsModeAction(actionCollection()));
119 viewModeActions
->addAction(DolphinView::detailsModeAction(actionCollection()));
120 viewModeActions
->addAction(DolphinView::columnsModeAction(actionCollection()));
121 connect(viewModeActions
, SIGNAL(triggered(QAction
*)), this, SLOT(slotViewModeActionTriggered(QAction
*)));
123 KAction
* renameAction
= DolphinView::createRenameAction(actionCollection());
124 connect(renameAction
, SIGNAL(triggered()), m_view
, SLOT(renameSelectedItems()));
126 KAction
* moveToTrashAction
= DolphinView::createMoveToTrashAction(actionCollection());
127 connect(moveToTrashAction
, SIGNAL(triggered(Qt::MouseButtons
, Qt::KeyboardModifiers
)),
128 this, SLOT(slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers
)));
130 KAction
* deleteAction
= DolphinView::createDeleteAction(actionCollection());
131 connect(deleteAction
, SIGNAL(triggered()), m_view
, SLOT(deleteSelectedItems()));
133 // This action doesn't appear in the GUI, it's for the shortcut only.
134 // KNewMenu takes care of the GUI stuff.
135 KAction
* newDirAction
= actionCollection()->addAction( "create_dir" );
136 newDirAction
->setText( i18n("Create Folder..." ) );
137 connect(newDirAction
, SIGNAL(triggered()), SLOT(slotNewDir()));
138 newDirAction
->setShortcut(Qt::Key_F10
);
139 widget()->addAction(newDirAction
);
143 QActionGroup
* goActionGroup
= new QActionGroup(this);
144 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
145 this, SLOT(slotGoTriggered(QAction
*)));
147 createGoAction("go_applications", "start-here",
148 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
150 createGoAction("go_network_folders", "drive-remote",
151 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
153 createGoAction("go_settings", "preferences-system",
154 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
156 createGoAction("go_trash", "user-trash",
157 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
159 createGoAction("go_autostart", "",
160 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
164 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
165 const QString
& text
, const QString
& url
,
166 QActionGroup
* actionGroup
)
168 KAction
* action
= actionCollection()->addAction(name
);
169 action
->setIcon(KIcon(iconName
));
170 action
->setText(text
);
171 action
->setData(url
);
172 action
->setActionGroup(actionGroup
);
175 void DolphinPart::slotGoTriggered(QAction
* action
)
177 const QString url
= action
->data().toString();
178 emit m_extension
->openUrlRequest(KUrl(url
));
181 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
183 const bool hasSelection
= !selection
.isEmpty();
185 stateChanged("has_no_selection");
187 stateChanged("has_selection");
191 actions
<< "rename" << "move_to_trash" << "delete";
192 foreach(const QString
& actionName
, actions
) {
193 QAction
* action
= actionCollection()->action(actionName
);
196 action
->setEnabled(hasSelection
);
200 emit m_extension
->enableAction("cut", hasSelection
);
201 emit m_extension
->enableAction("copy", hasSelection
);
204 void DolphinPart::updatePasteAction()
206 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
207 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
208 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
211 void DolphinPart::updateViewActions()
213 QAction
* action
= actionCollection()->action(m_view
->currentViewModeActionName());
215 KToggleAction
* toggleAction
= static_cast<KToggleAction
*>(action
);
216 toggleAction
->setChecked(true);
220 KAboutData
* DolphinPart::createAboutData()
222 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
225 bool DolphinPart::openUrl(const KUrl
& url
)
227 const bool reload
= arguments().reload();
228 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
231 setUrl(url
); // remember it at the KParts level
232 const QString prettyUrl
= url
.pathOrUrl();
233 emit
setWindowCaption(prettyUrl
);
234 emit m_extension
->setLocationBarUrl(prettyUrl
);
235 emit
started(0); // get the wheel to spin
242 void DolphinPart::slotCompleted(const KUrl
& url
)
248 void DolphinPart::slotCanceled(const KUrl
& url
)
253 void DolphinPart::slotInfoMessage(const QString
& msg
)
255 emit
setStatusBarText(msg
);
258 void DolphinPart::slotErrorMessage(const QString
& msg
)
260 KMessageBox::error(m_view
, msg
);
263 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
265 emit m_extension
->mouseOverInfo(item
);
268 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
270 // MMB click support.
271 // TODO: this doesn't work, mouseButtons() is always 0.
272 // Issue N176832 for the missing QAIV signal; task 177399
273 kDebug() << QApplication::mouseButtons();
274 if (QApplication::mouseButtons() & Qt::MidButton
) {
275 kDebug() << "MMB!!" << item
.mimetype();
276 if (item
.mimeTypePtr()->is("inode/directory")) {
277 KParts::OpenUrlArguments args
;
278 args
.setMimeType( item
.mimetype() );
279 emit m_extension
->createNewWindow( item
.url(), args
);
285 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
287 emit m_extension
->openUrlRequest(item
.url());
291 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
293 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
294 | KParts::BrowserExtension::ShowProperties
295 | KParts::BrowserExtension::ShowUrlOperations
;
296 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
297 // popupFlags |= KParts::BrowserExtension::NoDeletion;
299 KFileItem
item(_item
);
301 if (item
.isNull()) { // viewport context menu
302 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
303 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
304 // and use this as fallback:
305 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
308 KParts::BrowserExtension::ActionGroupMap actionGroups
;
309 QList
<QAction
*> editActions
;
311 if (!item
.isNull()) { // only for context menu on one or more items
312 // TODO if ( sMoving )
313 editActions
.append(actionCollection()->action("rename"));
315 bool addTrash
= false;
318 // TODO if ( sMoving && !isIntoTrash && !isTrashLink )
321 /* TODO if ( sDeleting ) */ {
322 if ( !item
.isLocalFile() )
324 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
329 KConfigGroup
configGroup( KGlobal::config(), "KDE" );
330 if ( configGroup
.readEntry( "ShowDeleteCommand", false) )
336 editActions
.append(actionCollection()->action("move_to_trash"));
338 editActions
.append(actionCollection()->action("delete"));
339 actionGroups
.insert("editactions", editActions
);
341 KFileItemList items
; items
.append(item
);
342 emit m_extension
->popupMenu(QCursor::pos(),
344 KParts::OpenUrlArguments(),
345 KParts::BrowserArguments(),
351 void DolphinPart::slotViewModeActionTriggered(QAction
* action
)
353 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
354 m_view
->setMode(mode
);
357 void DolphinPart::slotUrlChanged(const KUrl
& url
)
359 if (m_view
->url() != url
) {
360 // If the view URL is not equal to 'url', then an inner URL change has
361 // been done (e. g. by activating an existing column in the column view).
363 emit m_extension
->openUrlNotify();
369 void DolphinPartBrowserExtension::cut()
371 m_part
->view()->cutSelectedItems();
374 void DolphinPartBrowserExtension::copy()
376 m_part
->view()->copySelectedItems();
379 void DolphinPartBrowserExtension::paste()
381 m_part
->view()->paste();
386 void DolphinPart::slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers modifiers
)
388 // Note: kde3's konq_mainwindow.cpp used to check
389 // reason == KAction::PopupMenuActivation && ...
390 // but this isn't supported anymore
391 if (modifiers
& Qt::ShiftModifier
)
392 m_view
->deleteSelectedItems();
394 m_view
->trashSelectedItems();
397 void DolphinPart::slotNewDir()
399 KonqOperations::newDir(widget(), url());
402 #include "dolphinpart.moc"