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 connect to urlsDropped
107 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
108 // (sort of spacial navigation)
110 // TODO MMB-click should do something like KonqDirPart::mmbClicked
113 DolphinPart::~DolphinPart()
118 void DolphinPart::createActions()
120 QActionGroup
* viewModeActions
= new QActionGroup(this);
121 viewModeActions
->addAction(DolphinView::iconsModeAction(actionCollection()));
122 viewModeActions
->addAction(DolphinView::detailsModeAction(actionCollection()));
123 viewModeActions
->addAction(DolphinView::columnsModeAction(actionCollection()));
124 connect(viewModeActions
, SIGNAL(triggered(QAction
*)), this, SLOT(slotViewModeActionTriggered(QAction
*)));
126 KAction
* renameAction
= DolphinView::createRenameAction(actionCollection());
127 connect(renameAction
, SIGNAL(triggered()), m_view
, SLOT(renameSelectedItems()));
129 KAction
* moveToTrashAction
= DolphinView::createMoveToTrashAction(actionCollection());
130 connect(moveToTrashAction
, SIGNAL(triggered(Qt::MouseButtons
, Qt::KeyboardModifiers
)),
131 this, SLOT(slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers
)));
133 KAction
* deleteAction
= DolphinView::createDeleteAction(actionCollection());
134 connect(deleteAction
, SIGNAL(triggered()), m_view
, SLOT(deleteSelectedItems()));
136 // This action doesn't appear in the GUI, it's for the shortcut only.
137 // KNewMenu takes care of the GUI stuff.
138 KAction
* newDirAction
= actionCollection()->addAction( "create_dir" );
139 newDirAction
->setText( i18n("Create Folder..." ) );
140 connect(newDirAction
, SIGNAL(triggered()), SLOT(slotNewDir()));
141 newDirAction
->setShortcut(Qt::Key_F10
);
142 widget()->addAction(newDirAction
);
145 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
147 // Yes, DolphinMainWindow has very similar code :/
148 const bool hasSelection
= !selection
.isEmpty();
150 stateChanged("has_no_selection");
152 stateChanged("has_selection");
156 actions
<< "rename" << "move_to_trash" << "delete";
157 foreach(const QString
& actionName
, actions
) {
158 QAction
* action
= actionCollection()->action(actionName
);
161 action
->setEnabled(hasSelection
);
165 emit m_extension
->enableAction("cut", hasSelection
);
166 emit m_extension
->enableAction("copy", hasSelection
);
169 void DolphinPart::updatePasteAction()
171 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
172 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
173 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
176 void DolphinPart::updateViewActions()
178 QAction
* action
= actionCollection()->action(m_view
->currentViewModeActionName());
180 KToggleAction
* toggleAction
= static_cast<KToggleAction
*>(action
);
181 toggleAction
->setChecked(true);
185 KAboutData
* DolphinPart::createAboutData()
187 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
190 bool DolphinPart::openUrl(const KUrl
& url
)
192 const bool reload
= arguments().reload();
193 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
196 setUrl(url
); // remember it at the KParts level
197 const QString prettyUrl
= url
.pathOrUrl();
198 emit
setWindowCaption(prettyUrl
);
199 emit m_extension
->setLocationBarUrl(prettyUrl
);
200 emit
started(0); // get the wheel to spin
207 void DolphinPart::slotCompleted(const KUrl
& url
)
213 void DolphinPart::slotCanceled(const KUrl
& url
)
218 void DolphinPart::slotInfoMessage(const QString
& msg
)
220 emit
setStatusBarText(msg
);
223 void DolphinPart::slotErrorMessage(const QString
& msg
)
225 KMessageBox::error(m_view
, msg
);
228 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
230 emit m_extension
->mouseOverInfo(item
);
233 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
235 qDebug() << QApplication::mouseButtons();
236 if (QApplication::mouseButtons() & Qt::MidButton
) {
237 qDebug() << "MMB!!" << item
.mimetype();
238 if (item
.mimeTypePtr()->is("inode/directory")) {
239 KParts::OpenUrlArguments args
;
240 args
.setMimeType( item
.mimetype() );
241 emit m_extension
->createNewWindow( item
.url(), args
);
247 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
249 emit m_extension
->openUrlRequest(item
.url());
253 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
255 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
256 | KParts::BrowserExtension::ShowProperties
257 | KParts::BrowserExtension::ShowUrlOperations
;
258 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
259 // popupFlags |= KParts::BrowserExtension::NoDeletion;
261 KFileItem
item(_item
);
263 if (item
.isNull()) { // viewport context menu
264 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
265 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
266 // and use this as fallback:
267 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
270 KParts::BrowserExtension::ActionGroupMap actionGroups
;
271 QList
<QAction
*> editActions
;
273 if (!item
.isNull()) { // only for context menu on one or more items
274 // TODO if ( sMoving )
275 editActions
.append(actionCollection()->action("rename"));
277 bool addTrash
= false;
280 // TODO if ( sMoving && !isIntoTrash && !isTrashLink )
283 /* TODO if ( sDeleting ) */ {
284 if ( !item
.isLocalFile() )
286 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
291 KConfigGroup
configGroup( KGlobal::config(), "KDE" );
292 if ( configGroup
.readEntry( "ShowDeleteCommand", false) )
298 editActions
.append(actionCollection()->action("move_to_trash"));
300 editActions
.append(actionCollection()->action("delete"));
301 actionGroups
.insert("editactions", editActions
);
303 KFileItemList items
; items
.append(item
);
304 emit m_extension
->popupMenu(QCursor::pos(),
306 KParts::OpenUrlArguments(),
307 KParts::BrowserArguments(),
313 void DolphinPart::slotViewModeActionTriggered(QAction
* action
)
315 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
316 m_view
->setMode(mode
);
319 void DolphinPart::slotUrlChanged(const KUrl
& url
)
321 if (m_view
->url() != url
) {
322 // If the view URL is not equal to 'url', then an inner URL change has
323 // been done (e. g. by activating an existing column in the column view).
324 // From the hosts point of view this must be handled like changing the URL.
325 emit m_extension
->openUrlRequest(url
);
331 void DolphinPartBrowserExtension::cut()
333 m_part
->view()->cutSelectedItems();
336 void DolphinPartBrowserExtension::copy()
338 m_part
->view()->copySelectedItems();
341 void DolphinPartBrowserExtension::paste()
343 m_part
->view()->paste();
348 void DolphinPart::slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers modifiers
)
350 // Note: kde3's konq_mainwindow.cpp used to check
351 // reason == KAction::PopupMenuActivation && ...
352 // but this isn't supported anymore
353 if (modifiers
& Qt::ShiftModifier
)
354 m_view
->deleteSelectedItems();
356 m_view
->trashSelectedItems();
359 void DolphinPart::slotNewDir()
361 KonqOperations::newDir(widget(), url());
364 #include "dolphinpart.moc"