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 "dolphinviewactionhandler.h"
22 #include "dolphinsortfilterproxymodel.h"
23 #include "dolphinview.h"
24 #include "dolphinmodel.h"
26 #include <konq_operations.h>
28 #include <kaboutdata.h>
29 #include <kactioncollection.h>
30 #include <kconfiggroup.h>
32 #include <kdirlister.h>
33 #include <kglobalsettings.h>
34 #include <kiconloader.h>
36 #include <kmessagebox.h>
37 #include <kpluginfactory.h>
38 #include <kpropertiesdialog.h>
39 #include <ktoggleaction.h>
41 #include <QActionGroup>
42 #include <QApplication>
45 K_PLUGIN_FACTORY(DolphinPartFactory
, registerPlugin
<DolphinPart
>();)
46 K_EXPORT_PLUGIN(DolphinPartFactory("dolphin"))
48 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QVariantList
& args
)
49 : KParts::ReadOnlyPart(parent
)
52 setComponentData(DolphinPartFactory::componentData(), false);
53 m_extension
= new DolphinPartBrowserExtension(this);
55 // make sure that other apps using this part find Dolphin's view-file-columns icons
56 KIconLoader::global()->addAppDir("dolphin");
58 m_dirLister
= new KDirLister
;
59 m_dirLister
->setAutoUpdate(true);
60 m_dirLister
->setMainWindow(parentWidget
->window());
61 m_dirLister
->setDelayedMimeTypes(true);
63 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
64 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
65 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
67 m_dolphinModel
= new DolphinModel(this);
68 m_dolphinModel
->setDirLister(m_dirLister
);
70 m_proxyModel
= new DolphinSortFilterProxyModel(this);
71 m_proxyModel
->setSourceModel(m_dolphinModel
);
73 m_view
= new DolphinView(parentWidget
,
80 setXMLFile("dolphinpart.rc");
82 connect(m_view
, SIGNAL(infoMessage(QString
)),
83 this, SLOT(slotInfoMessage(QString
)));
84 connect(m_view
, SIGNAL(errorMessage(QString
)),
85 this, SLOT(slotErrorMessage(QString
)));
86 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
87 this, SLOT(slotItemTriggered(KFileItem
)));
88 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
)),
89 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
)));
90 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
91 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
92 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
93 this, SLOT(slotSelectionChanged(KFileItemList
)));
94 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
95 this, SLOT(slotRequestItemInfo(KFileItem
)));
96 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
97 this, SLOT(slotUrlChanged(KUrl
)));
98 connect(m_view
, SIGNAL(modeChanged()),
99 this, SIGNAL(viewModeChanged())); // relay signal
101 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
102 m_actionHandler
->setCurrentView(m_view
);
104 QClipboard
* clipboard
= QApplication::clipboard();
105 connect(clipboard
, SIGNAL(dataChanged()),
106 this, SLOT(updatePasteAction()));
109 m_actionHandler
->updateViewActions();
110 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
112 // TODO sort_by_* actions
114 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
115 // (sort of spacial navigation)
117 loadPlugins(this, this, componentData());
120 DolphinPart::~DolphinPart()
125 void DolphinPart::createActions()
127 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
128 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
129 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
131 KAction
*propertiesAction
= actionCollection()->addAction( "properties" );
132 propertiesAction
->setText( i18nc("@action:inmenu Edit", "Properties") );
133 propertiesAction
->setShortcut(Qt::ALT
+Qt::Key_Return
);
134 connect(propertiesAction
, SIGNAL(triggered()), SLOT(slotProperties()));
136 // View menu: all done by DolphinViewActionHandler
140 QActionGroup
* goActionGroup
= new QActionGroup(this);
141 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
142 this, SLOT(slotGoTriggered(QAction
*)));
144 createGoAction("go_applications", "start-here-kde",
145 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
147 createGoAction("go_network_folders", "folder-remote",
148 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
150 createGoAction("go_settings", "preferences-system",
151 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
153 createGoAction("go_trash", "user-trash",
154 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
156 createGoAction("go_autostart", "",
157 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
161 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
162 const QString
& text
, const QString
& url
,
163 QActionGroup
* actionGroup
)
165 KAction
* action
= actionCollection()->addAction(name
);
166 action
->setIcon(KIcon(iconName
));
167 action
->setText(text
);
168 action
->setData(url
);
169 action
->setActionGroup(actionGroup
);
172 void DolphinPart::slotGoTriggered(QAction
* action
)
174 const QString url
= action
->data().toString();
175 emit m_extension
->openUrlRequest(KUrl(url
));
178 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
180 const bool hasSelection
= !selection
.isEmpty();
182 stateChanged("has_no_selection");
184 stateChanged("has_selection");
188 actions
<< "rename" << "move_to_trash" << "delete" << "editMimeType" << "properties";
189 foreach(const QString
& actionName
, actions
) {
190 QAction
* action
= actionCollection()->action(actionName
);
193 action
->setEnabled(hasSelection
);
197 emit m_extension
->enableAction("cut", hasSelection
);
198 emit m_extension
->enableAction("copy", hasSelection
);
201 void DolphinPart::updatePasteAction()
203 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
204 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
205 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
208 KAboutData
* DolphinPart::createAboutData()
210 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
213 bool DolphinPart::openUrl(const KUrl
& url
)
215 const bool reload
= arguments().reload();
216 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
219 setUrl(url
); // remember it at the KParts level
220 const QString prettyUrl
= url
.pathOrUrl();
221 emit
setWindowCaption(prettyUrl
);
222 emit m_extension
->setLocationBarUrl(prettyUrl
);
223 emit
started(0); // get the wheel to spin
225 emit
aboutToOpenURL();
231 void DolphinPart::slotCompleted(const KUrl
& url
)
237 void DolphinPart::slotCanceled(const KUrl
& url
)
242 void DolphinPart::slotInfoMessage(const QString
& msg
)
244 emit
setStatusBarText(msg
);
247 void DolphinPart::slotErrorMessage(const QString
& msg
)
249 KMessageBox::error(m_view
, msg
);
252 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
254 emit m_extension
->mouseOverInfo(item
);
257 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
259 KParts::OpenUrlArguments args
;
260 args
.setMimeType(item
.mimetype());
262 // Ideally, konqueror should be changed to not require trustedSource for directory views,
263 // since the idea was not to need BrowserArguments for non-browser stuff...
264 KParts::BrowserArguments browserArgs
;
265 browserArgs
.trustedSource
= true;
267 // MMB click support.
268 // TODO: this doesn't work, mouseButtons() is always 0.
269 // Issue N176832 for the missing QAIV signal; task 177399
270 kDebug() << QApplication::mouseButtons();
271 if (QApplication::mouseButtons() & Qt::MidButton
) {
272 kDebug() << "MMB!!" << item
.mimetype();
273 if (item
.mimeTypePtr()->is("inode/directory")) {
274 emit m_extension
->createNewWindow(item
.url(), args
);
280 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
282 emit m_extension
->openUrlRequest(item
.targetUrl(), args
, browserArgs
);
286 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
288 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
289 | KParts::BrowserExtension::ShowProperties
290 | KParts::BrowserExtension::ShowUrlOperations
;
291 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
292 // popupFlags |= KParts::BrowserExtension::NoDeletion;
294 KFileItem
item(_item
);
296 if (item
.isNull()) { // viewport context menu
297 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
298 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
299 // and use this as fallback:
300 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
303 KParts::BrowserExtension::ActionGroupMap actionGroups
;
304 QList
<QAction
*> editActions
;
306 if (!item
.isNull()) { // only for context menu on one or more items
307 // TODO if ( sMoving )
308 editActions
.append(actionCollection()->action("rename"));
310 bool addTrash
= false;
313 // TODO if ( sMoving && !isIntoTrash && !isTrashLink )
316 /* TODO if ( sDeleting ) */ {
317 if ( !item
.isLocalFile() )
319 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
324 KConfigGroup
configGroup( KGlobal::config(), "KDE" );
325 if ( configGroup
.readEntry( "ShowDeleteCommand", false) )
331 editActions
.append(actionCollection()->action("move_to_trash"));
333 editActions
.append(actionCollection()->action("delete"));
334 actionGroups
.insert("editactions", editActions
);
336 // TODO: We should change the signature of the slots (and signals) for being able
337 // to tell for which items we want a popup.
338 KFileItemList items
= (m_view
->selectedItems().count() ? m_view
->selectedItems()
339 : KFileItemList() << item
);
340 emit m_extension
->popupMenu(QCursor::pos(),
342 KParts::OpenUrlArguments(),
343 KParts::BrowserArguments(),
349 void DolphinPart::slotUrlChanged(const KUrl
& url
)
351 if (m_view
->url() != url
) {
352 // If the view URL is not equal to 'url', then an inner URL change has
353 // been done (e. g. by activating an existing column in the column view).
355 emit m_extension
->openUrlNotify();
361 void DolphinPartBrowserExtension::cut()
363 m_part
->view()->cutSelectedItems();
366 void DolphinPartBrowserExtension::copy()
368 m_part
->view()->copySelectedItems();
371 void DolphinPartBrowserExtension::paste()
373 m_part
->view()->paste();
376 void DolphinPartBrowserExtension::reparseConfiguration()
378 m_part
->view()->refresh();
383 void DolphinPart::slotEditMimeType()
385 const KFileItemList items
= m_view
->selectedItems();
386 if (!items
.isEmpty()) {
387 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
391 void DolphinPart::slotProperties()
393 const KFileItemList items
= m_view
->selectedItems();
394 if (!items
.isEmpty()) {
395 KPropertiesDialog
dialog(items
.first().url(), m_view
);
400 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
402 QAction
* action
= actionCollection()->action(viewModeName
);
407 QString
DolphinPart::currentViewMode() const
409 return m_actionHandler
->currentViewModeActionName();
412 #include "dolphinpart.moc"