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
)
46 KGlobal::locale()->insertCatalog("dolphin");
48 setComponentData( DolphinPartFactory::componentData() );
49 m_extension
= new DolphinPartBrowserExtension(this);
51 m_dirLister
= new KDirLister
;
52 m_dirLister
->setAutoUpdate(true);
53 m_dirLister
->setMainWindow(parentWidget
->topLevelWidget());
54 m_dirLister
->setDelayedMimeTypes(true);
56 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
57 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
58 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
60 m_dolphinModel
= new DolphinModel(this);
61 m_dolphinModel
->setDirLister(m_dirLister
);
63 m_proxyModel
= new DolphinSortFilterProxyModel(this);
64 m_proxyModel
->setSourceModel(m_dolphinModel
);
66 m_view
= new DolphinView(parentWidget
,
73 setXMLFile("dolphinpart.rc");
75 connect(m_view
, SIGNAL(infoMessage(QString
)),
76 this, SLOT(slotInfoMessage(QString
)));
77 connect(m_view
, SIGNAL(errorMessage(QString
)),
78 this, SLOT(slotErrorMessage(QString
)));
79 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
80 this, SLOT(slotItemTriggered(KFileItem
)));
81 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
)),
82 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
)));
83 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
84 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
85 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
86 this, SLOT(slotSelectionChanged(KFileItemList
)));
87 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
88 this, SLOT(slotRequestItemInfo(KFileItem
)));
89 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
90 this, SLOT(slotUrlChanged(KUrl
)));
91 connect(m_view
, SIGNAL(modeChanged()),
92 this, SLOT(updateViewActions()));
94 QClipboard
* clipboard
= QApplication::clipboard();
95 connect(clipboard
, SIGNAL(dataChanged()),
96 this, SLOT(updatePasteAction()));
100 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
102 // TODO provide the viewmode actions in the menu, merged with the existing view-mode-actions somehow
103 // [Q_PROPERTY introspection?]
105 // TODO sort_by_* actions
106 // TODO show_*_info actions
108 // TODO connect to urlsDropped
110 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
111 // (sort of spacial navigation)
113 // TODO MMB-click should do something like KonqDirPart::mmbClicked
116 DolphinPart::~DolphinPart()
121 void DolphinPart::createActions()
123 QActionGroup
* viewModeActions
= new QActionGroup(this);
124 viewModeActions
->addAction(DolphinView::iconsModeAction(actionCollection()));
125 viewModeActions
->addAction(DolphinView::detailsModeAction(actionCollection()));
126 viewModeActions
->addAction(DolphinView::columnsModeAction(actionCollection()));
127 connect(viewModeActions
, SIGNAL(triggered(QAction
*)), this, SLOT(slotViewModeActionTriggered(QAction
*)));
129 KAction
* renameAction
= DolphinView::createRenameAction(actionCollection());
130 connect(renameAction
, SIGNAL(triggered()), m_view
, SLOT(renameSelectedItems()));
132 KAction
* moveToTrashAction
= DolphinView::createMoveToTrashAction(actionCollection());
133 connect(moveToTrashAction
, SIGNAL(triggered(Qt::MouseButtons
, Qt::KeyboardModifiers
)),
134 this, SLOT(slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers
)));
136 KAction
* deleteAction
= DolphinView::createDeleteAction(actionCollection());
137 connect(deleteAction
, SIGNAL(triggered()), m_view
, SLOT(deleteSelectedItems()));
139 // This action doesn't appear in the GUI, it's for the shortcut only.
140 // KNewMenu takes care of the GUI stuff.
141 KAction
* newDirAction
= actionCollection()->addAction( "create_dir" );
142 newDirAction
->setText( i18n("Create Folder..." ) );
143 connect(newDirAction
, SIGNAL(triggered()), SLOT(slotNewDir()));
144 newDirAction
->setShortcut(Qt::Key_F10
);
145 widget()->addAction(newDirAction
);
148 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
150 // Yes, DolphinMainWindow has very similar code :/
151 const bool hasSelection
= !selection
.isEmpty();
153 stateChanged("has_no_selection");
155 stateChanged("has_selection");
159 actions
<< "rename" << "move_to_trash" << "delete";
160 foreach(const QString
& actionName
, actions
) {
161 QAction
* action
= actionCollection()->action(actionName
);
164 action
->setEnabled(hasSelection
);
168 emit m_extension
->enableAction("cut", hasSelection
);
169 emit m_extension
->enableAction("copy", hasSelection
);
172 void DolphinPart::updatePasteAction()
174 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
175 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
176 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
179 void DolphinPart::updateViewActions()
181 QAction
* action
= actionCollection()->action(m_view
->currentViewModeActionName());
183 KToggleAction
* toggleAction
= static_cast<KToggleAction
*>(action
);
184 toggleAction
->setChecked(true);
188 KAboutData
* DolphinPart::createAboutData()
190 return new KAboutData("dolphinpart", 0, ki18nc("@title", "Dolphin Part"), "0.1");
193 bool DolphinPart::openUrl(const KUrl
& url
)
195 const bool reload
= arguments().reload();
196 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
199 setUrl(url
); // remember it at the KParts level
200 const QString prettyUrl
= url
.pathOrUrl();
201 emit
setWindowCaption(prettyUrl
);
202 emit m_extension
->setLocationBarUrl(prettyUrl
);
206 emit
started(0); // get the wheel to spin
210 void DolphinPart::slotCompleted(const KUrl
& url
)
216 void DolphinPart::slotCanceled(const KUrl
& url
)
221 void DolphinPart::slotInfoMessage(const QString
& msg
)
223 emit
setStatusBarText(msg
);
226 void DolphinPart::slotErrorMessage(const QString
& msg
)
228 KMessageBox::error(m_view
, msg
);
231 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
233 emit m_extension
->mouseOverInfo(item
);
236 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
238 qDebug() << QApplication::mouseButtons();
239 if (QApplication::mouseButtons() & Qt::MidButton
) {
240 qDebug() << "MMB!!" << item
.mimetype();
241 if (item
.mimeTypePtr()->is("inode/directory")) {
242 KParts::OpenUrlArguments args
;
243 args
.setMimeType( item
.mimetype() );
244 emit m_extension
->createNewWindow( item
.url(), args
);
250 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
252 emit m_extension
->openUrlRequest(item
.url());
256 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
258 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
259 | KParts::BrowserExtension::ShowProperties
260 | KParts::BrowserExtension::ShowUrlOperations
;
261 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
262 // popupFlags |= KParts::BrowserExtension::NoDeletion;
264 KFileItem
item(_item
);
266 if (item
.isNull()) { // viewport context menu
267 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
268 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
269 // and use this as fallback:
270 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
273 KParts::BrowserExtension::ActionGroupMap actionGroups
;
274 QList
<QAction
*> editActions
;
276 if (!item
.isNull()) { // only for context menu on one or more items
277 // TODO if ( sMoving )
278 editActions
.append(actionCollection()->action("rename"));
280 bool addTrash
= false;
283 // TODO if ( sMoving && !isIntoTrash && !isTrashLink )
286 /* TODO if ( sDeleting ) */ {
287 if ( !item
.isLocalFile() )
289 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
294 KConfigGroup
configGroup( KGlobal::config(), "KDE" );
295 if ( configGroup
.readEntry( "ShowDeleteCommand", false) )
301 editActions
.append(actionCollection()->action("move_to_trash"));
303 editActions
.append(actionCollection()->action("delete"));
304 actionGroups
.insert("editactions", editActions
);
306 KFileItemList items
; items
.append(item
);
307 emit m_extension
->popupMenu(QCursor::pos(),
309 KParts::OpenUrlArguments(),
310 KParts::BrowserArguments(),
316 void DolphinPart::slotViewModeActionTriggered(QAction
* action
)
318 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
319 m_view
->setMode(mode
);
322 void DolphinPart::slotUrlChanged(const KUrl
& url
)
324 if (m_view
->url() != url
) {
325 // If the view URL is not equal to 'url', then an inner URL change has
326 // been done (e. g. by activating an existing column in the column view).
327 // From the hosts point of view this must be handled like changing the URL.
328 emit m_extension
->openUrlRequest(url
);
334 void DolphinPartBrowserExtension::cut()
336 m_part
->view()->cutSelectedItems();
339 void DolphinPartBrowserExtension::copy()
341 m_part
->view()->copySelectedItems();
344 void DolphinPartBrowserExtension::paste()
346 m_part
->view()->paste();
351 void DolphinPart::slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers modifiers
)
353 // Note: kde3's konq_mainwindow.cpp used to check
354 // reason == KAction::PopupMenuActivation && ...
355 // but this isn't supported anymore
356 if (modifiers
& Qt::ShiftModifier
)
357 m_view
->deleteSelectedItems();
359 m_view
->trashSelectedItems();
362 void DolphinPart::slotNewDir()
364 KonqOperations::newDir(widget(), url());
367 #include "dolphinpart.moc"