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 <kmessagebox.h>
32 #include <kparts/genericfactory.h>
33 #include <ktoggleaction.h>
34 #include <kconfiggroup.h>
36 #include <QActionGroup>
37 #include <QApplication>
40 typedef KParts::GenericFactory
<DolphinPart
> DolphinPartFactory
;
41 K_EXPORT_COMPONENT_FACTORY(dolphinpart
, DolphinPartFactory
)
43 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QStringList
& args
)
44 : KParts::ReadOnlyPart(parent
)
47 setComponentData( DolphinPartFactory::componentData() );
48 m_extension
= new DolphinPartBrowserExtension(this);
50 m_dirLister
= new KDirLister
;
51 m_dirLister
->setAutoUpdate(true);
52 m_dirLister
->setMainWindow(parentWidget
->topLevelWidget());
53 m_dirLister
->setDelayedMimeTypes(true);
55 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
56 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
57 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
59 m_dolphinModel
= new DolphinModel(this);
60 m_dolphinModel
->setDirLister(m_dirLister
);
62 m_proxyModel
= new DolphinSortFilterProxyModel(this);
63 m_proxyModel
->setSourceModel(m_dolphinModel
);
65 m_view
= new DolphinView(parentWidget
,
72 setXMLFile("dolphinpart.rc");
74 connect(m_view
, SIGNAL(infoMessage(QString
)),
75 this, SLOT(slotInfoMessage(QString
)));
76 connect(m_view
, SIGNAL(errorMessage(QString
)),
77 this, SLOT(slotErrorMessage(QString
)));
78 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
79 this, SLOT(slotItemTriggered(KFileItem
)));
80 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
)),
81 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
)));
82 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
83 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
84 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
85 this, SLOT(slotSelectionChanged(KFileItemList
)));
86 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
87 this, SLOT(slotRequestItemInfo(KFileItem
)));
88 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
89 this, SLOT(slotUrlChanged(KUrl
)));
90 connect(m_view
, SIGNAL(modeChanged()),
91 this, SLOT(updateViewActions()));
93 QClipboard
* clipboard
= QApplication::clipboard();
94 connect(clipboard
, SIGNAL(dataChanged()),
95 this, SLOT(updatePasteAction()));
99 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
101 // TODO provide the viewmode actions in the menu, merged with the existing view-mode-actions somehow
102 // [Q_PROPERTY introspection?]
104 // TODO sort_by_* actions
105 // TODO show_*_info actions
107 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
108 // (sort of spacial navigation)
111 DolphinPart::~DolphinPart()
116 void DolphinPart::createActions()
118 QActionGroup
* viewModeActions
= new QActionGroup(this);
119 viewModeActions
->addAction(DolphinView::iconsModeAction(actionCollection()));
120 viewModeActions
->addAction(DolphinView::detailsModeAction(actionCollection()));
121 viewModeActions
->addAction(DolphinView::columnsModeAction(actionCollection()));
122 connect(viewModeActions
, SIGNAL(triggered(QAction
*)), this, SLOT(slotViewModeActionTriggered(QAction
*)));
124 KAction
* renameAction
= DolphinView::createRenameAction(actionCollection());
125 connect(renameAction
, SIGNAL(triggered()), m_view
, SLOT(renameSelectedItems()));
127 KAction
* moveToTrashAction
= DolphinView::createMoveToTrashAction(actionCollection());
128 connect(moveToTrashAction
, SIGNAL(triggered(Qt::MouseButtons
, Qt::KeyboardModifiers
)),
129 this, SLOT(slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers
)));
131 KAction
* deleteAction
= DolphinView::createDeleteAction(actionCollection());
132 connect(deleteAction
, SIGNAL(triggered()), m_view
, SLOT(deleteSelectedItems()));
134 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
135 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
136 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
138 KAction
*propertiesAction
= actionCollection()->addAction( "properties" );
139 propertiesAction
->setText( i18nc("@action:inmenu Edit", "Properties") );
140 propertiesAction
->setShortcut(Qt::ALT
+Qt::Key_Return
);
141 connect(propertiesAction
, SIGNAL(triggered()), SLOT(slotProperties()));
143 // This action doesn't appear in the GUI, it's for the shortcut only.
144 // KNewMenu takes care of the GUI stuff.
145 KAction
* newDirAction
= actionCollection()->addAction( "create_dir" );
146 newDirAction
->setText( i18n("Create Folder..." ) );
147 connect(newDirAction
, SIGNAL(triggered()), SLOT(slotNewDir()));
148 newDirAction
->setShortcut(Qt::Key_F10
);
149 widget()->addAction(newDirAction
);
153 QActionGroup
* goActionGroup
= new QActionGroup(this);
154 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
155 this, SLOT(slotGoTriggered(QAction
*)));
157 createGoAction("go_applications", "start-here",
158 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
160 createGoAction("go_network_folders", "drive-remote",
161 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
163 createGoAction("go_settings", "preferences-system",
164 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
166 createGoAction("go_trash", "user-trash",
167 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
169 createGoAction("go_autostart", "",
170 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
174 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
175 const QString
& text
, const QString
& url
,
176 QActionGroup
* actionGroup
)
178 KAction
* action
= actionCollection()->addAction(name
);
179 action
->setIcon(KIcon(iconName
));
180 action
->setText(text
);
181 action
->setData(url
);
182 action
->setActionGroup(actionGroup
);
185 void DolphinPart::slotGoTriggered(QAction
* action
)
187 const QString url
= action
->data().toString();
188 emit m_extension
->openUrlRequest(KUrl(url
));
191 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
193 const bool hasSelection
= !selection
.isEmpty();
195 stateChanged("has_no_selection");
197 stateChanged("has_selection");
201 actions
<< "rename" << "move_to_trash" << "delete" << "editMimeType" << "properties";
202 foreach(const QString
& actionName
, actions
) {
203 QAction
* action
= actionCollection()->action(actionName
);
206 action
->setEnabled(hasSelection
);
210 emit m_extension
->enableAction("cut", hasSelection
);
211 emit m_extension
->enableAction("copy", hasSelection
);
214 void DolphinPart::updatePasteAction()
216 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
217 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
218 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
221 void DolphinPart::updateViewActions()
223 QAction
* action
= actionCollection()->action(m_view
->currentViewModeActionName());
225 KToggleAction
* toggleAction
= static_cast<KToggleAction
*>(action
);
226 toggleAction
->setChecked(true);
230 KAboutData
* DolphinPart::createAboutData()
232 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
235 bool DolphinPart::openUrl(const KUrl
& url
)
237 const bool reload
= arguments().reload();
238 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
241 setUrl(url
); // remember it at the KParts level
242 const QString prettyUrl
= url
.pathOrUrl();
243 emit
setWindowCaption(prettyUrl
);
244 emit m_extension
->setLocationBarUrl(prettyUrl
);
245 emit
started(0); // get the wheel to spin
252 void DolphinPart::slotCompleted(const KUrl
& url
)
258 void DolphinPart::slotCanceled(const KUrl
& url
)
263 void DolphinPart::slotInfoMessage(const QString
& msg
)
265 emit
setStatusBarText(msg
);
268 void DolphinPart::slotErrorMessage(const QString
& msg
)
270 KMessageBox::error(m_view
, msg
);
273 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
275 emit m_extension
->mouseOverInfo(item
);
278 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
280 // MMB click support.
281 // TODO: this doesn't work, mouseButtons() is always 0.
282 // Issue N176832 for the missing QAIV signal; task 177399
283 kDebug() << QApplication::mouseButtons();
284 if (QApplication::mouseButtons() & Qt::MidButton
) {
285 kDebug() << "MMB!!" << item
.mimetype();
286 if (item
.mimeTypePtr()->is("inode/directory")) {
287 KParts::OpenUrlArguments args
;
288 args
.setMimeType( item
.mimetype() );
289 emit m_extension
->createNewWindow( item
.url(), args
);
295 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
297 emit m_extension
->openUrlRequest(item
.url());
301 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
303 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
304 | KParts::BrowserExtension::ShowProperties
305 | KParts::BrowserExtension::ShowUrlOperations
;
306 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
307 // popupFlags |= KParts::BrowserExtension::NoDeletion;
309 KFileItem
item(_item
);
311 if (item
.isNull()) { // viewport context menu
312 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
313 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
314 // and use this as fallback:
315 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
318 KParts::BrowserExtension::ActionGroupMap actionGroups
;
319 QList
<QAction
*> editActions
;
321 if (!item
.isNull()) { // only for context menu on one or more items
322 // TODO if ( sMoving )
323 editActions
.append(actionCollection()->action("rename"));
325 bool addTrash
= false;
328 // TODO if ( sMoving && !isIntoTrash && !isTrashLink )
331 /* TODO if ( sDeleting ) */ {
332 if ( !item
.isLocalFile() )
334 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
339 KConfigGroup
configGroup( KGlobal::config(), "KDE" );
340 if ( configGroup
.readEntry( "ShowDeleteCommand", false) )
346 editActions
.append(actionCollection()->action("move_to_trash"));
348 editActions
.append(actionCollection()->action("delete"));
349 actionGroups
.insert("editactions", editActions
);
351 KFileItemList items
; items
.append(item
);
352 emit m_extension
->popupMenu(QCursor::pos(),
354 KParts::OpenUrlArguments(),
355 KParts::BrowserArguments(),
361 void DolphinPart::slotViewModeActionTriggered(QAction
* action
)
363 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
364 m_view
->setMode(mode
);
367 void DolphinPart::slotUrlChanged(const KUrl
& url
)
369 if (m_view
->url() != url
) {
370 // If the view URL is not equal to 'url', then an inner URL change has
371 // been done (e. g. by activating an existing column in the column view).
373 emit m_extension
->openUrlNotify();
379 void DolphinPartBrowserExtension::cut()
381 m_part
->view()->cutSelectedItems();
384 void DolphinPartBrowserExtension::copy()
386 m_part
->view()->copySelectedItems();
389 void DolphinPartBrowserExtension::paste()
391 m_part
->view()->paste();
396 void DolphinPart::slotTrashActivated(Qt::MouseButtons
, Qt::KeyboardModifiers modifiers
)
398 // Note: kde3's konq_mainwindow.cpp used to check
399 // reason == KAction::PopupMenuActivation && ...
400 // but this isn't supported anymore
401 if (modifiers
& Qt::ShiftModifier
)
402 m_view
->deleteSelectedItems();
404 m_view
->trashSelectedItems();
407 void DolphinPart::slotNewDir()
409 KonqOperations::newDir(widget(), url());
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 #include "dolphinpart.moc"