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 "dolphinsortfilterproxymodel.h"
22 #include "dolphinview.h"
23 #include "dolphinmodel.h"
25 #include <konq_operations.h>
27 #include <kactioncollection.h>
28 #include <kdirlister.h>
29 #include <kmessagebox.h>
30 #include <kparts/genericfactory.h>
31 #include <ktoggleaction.h>
32 #include <kconfiggroup.h>
34 #include <QActionGroup>
35 #include <QApplication>
38 typedef KParts::GenericFactory
<DolphinPart
> DolphinPartFactory
;
39 K_EXPORT_COMPONENT_FACTORY(dolphinpart
, DolphinPartFactory
)
41 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QStringList
& args
)
42 : KParts::ReadOnlyPart(parent
)
45 setComponentData( DolphinPartFactory::componentData() );
46 m_extension
= new DolphinPartBrowserExtension(this);
48 m_dirLister
= new KDirLister
;
49 m_dirLister
->setAutoUpdate(true);
50 m_dirLister
->setMainWindow(parentWidget
->topLevelWidget());
51 m_dirLister
->setDelayedMimeTypes(true);
53 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
54 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
55 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
57 m_dolphinModel
= new DolphinModel(this);
58 m_dolphinModel
->setDirLister(m_dirLister
);
60 m_proxyModel
= new DolphinSortFilterProxyModel(this);
61 m_proxyModel
->setSourceModel(m_dolphinModel
);
63 m_view
= new DolphinView(parentWidget
,
70 setXMLFile("dolphinpart.rc");
72 connect(m_view
, SIGNAL(infoMessage(QString
)),
73 this, SLOT(slotInfoMessage(QString
)));
74 connect(m_view
, SIGNAL(errorMessage(QString
)),
75 this, SLOT(slotErrorMessage(QString
)));
76 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
77 this, SLOT(slotItemTriggered(KFileItem
)));
78 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
)),
79 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
)));
80 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
81 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
82 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
83 this, SLOT(slotSelectionChanged(KFileItemList
)));
84 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
85 this, SLOT(slotRequestItemInfo(KFileItem
)));
86 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
87 this, SLOT(slotUrlChanged(KUrl
)));
88 connect(m_view
, SIGNAL(modeChanged()),
89 this, SLOT(updateViewActions()));
91 QClipboard
* clipboard
= QApplication::clipboard();
92 connect(clipboard
, SIGNAL(dataChanged()),
93 this, SLOT(updatePasteAction()));
97 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
99 // TODO provide the viewmode actions in the menu, merged with the existing view-mode-actions somehow
100 // [Q_PROPERTY introspection?]
102 // TODO sort_by_* actions
103 // TODO show_*_info actions
105 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
106 // (sort of spacial navigation)
109 DolphinPart::~DolphinPart()
114 void DolphinPart::createActions()
116 QActionGroup
* viewModeActions
= new QActionGroup(this);
117 viewModeActions
->addAction(DolphinView::iconsModeAction(actionCollection()));
118 viewModeActions
->addAction(DolphinView::detailsModeAction(actionCollection()));
119 viewModeActions
->addAction(DolphinView::columnsModeAction(actionCollection()));
120 connect(viewModeActions
, SIGNAL(triggered(QAction
*)), this, SLOT(slotViewModeActionTriggered(QAction
*)));
122 KAction
* renameAction
= DolphinView::createRenameAction(actionCollection());
123 connect(renameAction
, SIGNAL(triggered()), m_view
, SLOT(renameSelectedItems()));
125 KAction
* moveToTrashAction
= DolphinView::createMoveToTrashAction(actionCollection());
126 connect(moveToTrashAction
, SIGNAL(triggered(Qt::MouseButtons
, Qt::KeyboardModifiers
)),
127 this, SLOT(slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers
)));
129 KAction
* deleteAction
= DolphinView::createDeleteAction(actionCollection());
130 connect(deleteAction
, SIGNAL(triggered()), m_view
, SLOT(deleteSelectedItems()));
132 // This action doesn't appear in the GUI, it's for the shortcut only.
133 // KNewMenu takes care of the GUI stuff.
134 KAction
* newDirAction
= actionCollection()->addAction( "create_dir" );
135 newDirAction
->setText( i18n("Create Folder..." ) );
136 connect(newDirAction
, SIGNAL(triggered()), SLOT(slotNewDir()));
137 newDirAction
->setShortcut(Qt::Key_F10
);
138 widget()->addAction(newDirAction
);
141 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
143 const bool hasSelection
= !selection
.isEmpty();
145 stateChanged("has_no_selection");
147 stateChanged("has_selection");
151 actions
<< "rename" << "move_to_trash" << "delete";
152 foreach(const QString
& actionName
, actions
) {
153 QAction
* action
= actionCollection()->action(actionName
);
156 action
->setEnabled(hasSelection
);
160 emit m_extension
->enableAction("cut", hasSelection
);
161 emit m_extension
->enableAction("copy", hasSelection
);
164 void DolphinPart::updatePasteAction()
166 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
167 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
168 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
171 void DolphinPart::updateViewActions()
173 QAction
* action
= actionCollection()->action(m_view
->currentViewModeActionName());
175 KToggleAction
* toggleAction
= static_cast<KToggleAction
*>(action
);
176 toggleAction
->setChecked(true);
180 KAboutData
* DolphinPart::createAboutData()
182 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
185 bool DolphinPart::openUrl(const KUrl
& url
)
187 const bool reload
= arguments().reload();
188 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
191 setUrl(url
); // remember it at the KParts level
192 const QString prettyUrl
= url
.pathOrUrl();
193 emit
setWindowCaption(prettyUrl
);
194 emit m_extension
->setLocationBarUrl(prettyUrl
);
195 emit
started(0); // get the wheel to spin
202 void DolphinPart::slotCompleted(const KUrl
& url
)
208 void DolphinPart::slotCanceled(const KUrl
& url
)
213 void DolphinPart::slotInfoMessage(const QString
& msg
)
215 emit
setStatusBarText(msg
);
218 void DolphinPart::slotErrorMessage(const QString
& msg
)
220 KMessageBox::error(m_view
, msg
);
223 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
225 emit m_extension
->mouseOverInfo(item
);
228 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
230 // MMB click support.
231 // TODO: this doesn't work, mouseButtons() is always 0.
232 // Issue N176832 for the missing QAIV signal; task 177399
233 kDebug() << QApplication::mouseButtons();
234 if (QApplication::mouseButtons() & Qt::MidButton
) {
235 kDebug() << "MMB!!" << item
.mimetype();
236 if (item
.mimeTypePtr()->is("inode/directory")) {
237 KParts::OpenUrlArguments args
;
238 args
.setMimeType( item
.mimetype() );
239 emit m_extension
->createNewWindow( item
.url(), args
);
245 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
247 emit m_extension
->openUrlRequest(item
.url());
251 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
253 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
254 | KParts::BrowserExtension::ShowProperties
255 | KParts::BrowserExtension::ShowUrlOperations
;
256 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
257 // popupFlags |= KParts::BrowserExtension::NoDeletion;
259 KFileItem
item(_item
);
261 if (item
.isNull()) { // viewport context menu
262 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
263 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
264 // and use this as fallback:
265 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
268 KParts::BrowserExtension::ActionGroupMap actionGroups
;
269 QList
<QAction
*> editActions
;
271 if (!item
.isNull()) { // only for context menu on one or more items
272 // TODO if ( sMoving )
273 editActions
.append(actionCollection()->action("rename"));
275 bool addTrash
= false;
278 // TODO if ( sMoving && !isIntoTrash && !isTrashLink )
281 /* TODO if ( sDeleting ) */ {
282 if ( !item
.isLocalFile() )
284 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
289 KConfigGroup
configGroup( KGlobal::config(), "KDE" );
290 if ( configGroup
.readEntry( "ShowDeleteCommand", false) )
296 editActions
.append(actionCollection()->action("move_to_trash"));
298 editActions
.append(actionCollection()->action("delete"));
299 actionGroups
.insert("editactions", editActions
);
301 KFileItemList items
; items
.append(item
);
302 emit m_extension
->popupMenu(QCursor::pos(),
304 KParts::OpenUrlArguments(),
305 KParts::BrowserArguments(),
311 void DolphinPart::slotViewModeActionTriggered(QAction
* action
)
313 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
314 m_view
->setMode(mode
);
317 void DolphinPart::slotUrlChanged(const KUrl
& url
)
319 if (m_view
->url() != url
) {
320 // If the view URL is not equal to 'url', then an inner URL change has
321 // been done (e. g. by activating an existing column in the column view).
322 // From the hosts point of view this must be handled like changing the URL.
323 emit m_extension
->openUrlRequestDelayed(url
, KParts::OpenUrlArguments(), KParts::BrowserArguments());
329 void DolphinPartBrowserExtension::cut()
331 m_part
->view()->cutSelectedItems();
334 void DolphinPartBrowserExtension::copy()
336 m_part
->view()->copySelectedItems();
339 void DolphinPartBrowserExtension::paste()
341 m_part
->view()->paste();
346 void DolphinPart::slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers modifiers
)
348 // Note: kde3's konq_mainwindow.cpp used to check
349 // reason == KAction::PopupMenuActivation && ...
350 // but this isn't supported anymore
351 if (modifiers
& Qt::ShiftModifier
)
352 m_view
->deleteSelectedItems();
354 m_view
->trashSelectedItems();
357 void DolphinPart::slotNewDir()
359 KonqOperations::newDir(widget(), url());
362 #include "dolphinpart.moc"