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 <kpropertiesdialog.h>
29 #include <kglobalsettings.h>
30 #include <kactioncollection.h>
31 #include <kdirlister.h>
32 #include <kiconloader.h>
33 #include <kmessagebox.h>
34 #include <kparts/genericfactory.h>
35 #include <ktoggleaction.h>
36 #include <kconfiggroup.h>
38 #include <QActionGroup>
39 #include <QApplication>
42 typedef KParts::GenericFactory
<DolphinPart
> DolphinPartFactory
;
43 K_EXPORT_COMPONENT_FACTORY(dolphinpart
, DolphinPartFactory
)
45 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QStringList
& args
)
46 : KParts::ReadOnlyPart(parent
)
49 setComponentData( DolphinPartFactory::componentData() );
50 m_extension
= new DolphinPartBrowserExtension(this);
52 // make sure that other apps using this part find Dolphin's view-file-columns icons
53 KIconLoader::global()->addAppDir("dolphin");
55 m_dirLister
= new KDirLister
;
56 m_dirLister
->setAutoUpdate(true);
57 m_dirLister
->setMainWindow(parentWidget
->topLevelWidget());
58 m_dirLister
->setDelayedMimeTypes(true);
60 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
61 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
62 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
64 m_dolphinModel
= new DolphinModel(this);
65 m_dolphinModel
->setDirLister(m_dirLister
);
67 m_proxyModel
= new DolphinSortFilterProxyModel(this);
68 m_proxyModel
->setSourceModel(m_dolphinModel
);
70 m_view
= new DolphinView(parentWidget
,
77 setXMLFile("dolphinpart.rc");
79 connect(m_view
, SIGNAL(infoMessage(QString
)),
80 this, SLOT(slotInfoMessage(QString
)));
81 connect(m_view
, SIGNAL(errorMessage(QString
)),
82 this, SLOT(slotErrorMessage(QString
)));
83 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
84 this, SLOT(slotItemTriggered(KFileItem
)));
85 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
)),
86 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
)));
87 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
88 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
89 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
90 this, SLOT(slotSelectionChanged(KFileItemList
)));
91 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
92 this, SLOT(slotRequestItemInfo(KFileItem
)));
93 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
94 this, SLOT(slotUrlChanged(KUrl
)));
95 connect(m_view
, SIGNAL(modeChanged()),
96 this, SIGNAL(viewModeChanged())); // relay signal
97 // TODO slotSortingChanged
99 m_actionHandler
= new DolphinViewActionHandler(actionCollection(), this);
100 m_actionHandler
->setCurrentView(m_view
);
102 QClipboard
* clipboard
= QApplication::clipboard();
103 connect(clipboard
, SIGNAL(dataChanged()),
104 this, SLOT(updatePasteAction()));
107 m_actionHandler
->updateViewActions();
108 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
110 // TODO sort_by_* actions
112 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
113 // (sort of spacial navigation)
116 DolphinPart::~DolphinPart()
121 void DolphinPart::createActions()
123 KAction
*editMimeTypeAction
= actionCollection()->addAction( "editMimeType" );
124 editMimeTypeAction
->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
125 connect(editMimeTypeAction
, SIGNAL(triggered()), SLOT(slotEditMimeType()));
127 KAction
*propertiesAction
= actionCollection()->addAction( "properties" );
128 propertiesAction
->setText( i18nc("@action:inmenu Edit", "Properties") );
129 propertiesAction
->setShortcut(Qt::ALT
+Qt::Key_Return
);
130 connect(propertiesAction
, SIGNAL(triggered()), SLOT(slotProperties()));
132 // View menu: all done by DolphinViewActionHandler
136 QActionGroup
* goActionGroup
= new QActionGroup(this);
137 connect(goActionGroup
, SIGNAL(triggered(QAction
*)),
138 this, SLOT(slotGoTriggered(QAction
*)));
140 createGoAction("go_applications", "start-here-kde",
141 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
143 createGoAction("go_network_folders", "folder-remote",
144 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
146 createGoAction("go_settings", "preferences-system",
147 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
149 createGoAction("go_trash", "user-trash",
150 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
152 createGoAction("go_autostart", "",
153 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
157 void DolphinPart::createGoAction(const char* name
, const char* iconName
,
158 const QString
& text
, const QString
& url
,
159 QActionGroup
* actionGroup
)
161 KAction
* action
= actionCollection()->addAction(name
);
162 action
->setIcon(KIcon(iconName
));
163 action
->setText(text
);
164 action
->setData(url
);
165 action
->setActionGroup(actionGroup
);
168 void DolphinPart::slotGoTriggered(QAction
* action
)
170 const QString url
= action
->data().toString();
171 emit m_extension
->openUrlRequest(KUrl(url
));
174 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
176 const bool hasSelection
= !selection
.isEmpty();
178 stateChanged("has_no_selection");
180 stateChanged("has_selection");
184 actions
<< "rename" << "move_to_trash" << "delete" << "editMimeType" << "properties";
185 foreach(const QString
& actionName
, actions
) {
186 QAction
* action
= actionCollection()->action(actionName
);
189 action
->setEnabled(hasSelection
);
193 emit m_extension
->enableAction("cut", hasSelection
);
194 emit m_extension
->enableAction("copy", hasSelection
);
197 void DolphinPart::updatePasteAction()
199 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
200 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
201 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
204 KAboutData
* DolphinPart::createAboutData()
206 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
209 bool DolphinPart::openUrl(const KUrl
& url
)
211 const bool reload
= arguments().reload();
212 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
215 setUrl(url
); // remember it at the KParts level
216 const QString prettyUrl
= url
.pathOrUrl();
217 emit
setWindowCaption(prettyUrl
);
218 emit m_extension
->setLocationBarUrl(prettyUrl
);
219 emit
started(0); // get the wheel to spin
226 void DolphinPart::slotCompleted(const KUrl
& url
)
232 void DolphinPart::slotCanceled(const KUrl
& url
)
237 void DolphinPart::slotInfoMessage(const QString
& msg
)
239 emit
setStatusBarText(msg
);
242 void DolphinPart::slotErrorMessage(const QString
& msg
)
244 KMessageBox::error(m_view
, msg
);
247 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
249 emit m_extension
->mouseOverInfo(item
);
252 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
254 KParts::OpenUrlArguments args
;
255 args
.setMimeType(item
.mimetype());
257 // Ideally, konqueror should be changed to not require trustedSource for directory views,
258 // since the idea was not to need BrowserArguments for non-browser stuff...
259 KParts::BrowserArguments browserArgs
;
260 browserArgs
.trustedSource
= true;
262 // MMB click support.
263 // TODO: this doesn't work, mouseButtons() is always 0.
264 // Issue N176832 for the missing QAIV signal; task 177399
265 kDebug() << QApplication::mouseButtons();
266 if (QApplication::mouseButtons() & Qt::MidButton
) {
267 kDebug() << "MMB!!" << item
.mimetype();
268 if (item
.mimeTypePtr()->is("inode/directory")) {
269 emit m_extension
->createNewWindow(item
.url(), args
);
275 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
277 emit m_extension
->openUrlRequest(item
.url(), args
, browserArgs
);
281 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
283 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
284 | KParts::BrowserExtension::ShowProperties
285 | KParts::BrowserExtension::ShowUrlOperations
;
286 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
287 // popupFlags |= KParts::BrowserExtension::NoDeletion;
289 KFileItem
item(_item
);
291 if (item
.isNull()) { // viewport context menu
292 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
293 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
294 // and use this as fallback:
295 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
298 KParts::BrowserExtension::ActionGroupMap actionGroups
;
299 QList
<QAction
*> editActions
;
301 if (!item
.isNull()) { // only for context menu on one or more items
302 // TODO if ( sMoving )
303 editActions
.append(actionCollection()->action("rename"));
305 bool addTrash
= false;
308 // TODO if ( sMoving && !isIntoTrash && !isTrashLink )
311 /* TODO if ( sDeleting ) */ {
312 if ( !item
.isLocalFile() )
314 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier
) {
319 KConfigGroup
configGroup( KGlobal::config(), "KDE" );
320 if ( configGroup
.readEntry( "ShowDeleteCommand", false) )
326 editActions
.append(actionCollection()->action("move_to_trash"));
328 editActions
.append(actionCollection()->action("delete"));
329 actionGroups
.insert("editactions", editActions
);
331 KFileItemList items
; items
.append(item
);
332 emit m_extension
->popupMenu(QCursor::pos(),
334 KParts::OpenUrlArguments(),
335 KParts::BrowserArguments(),
341 void DolphinPart::slotUrlChanged(const KUrl
& url
)
343 if (m_view
->url() != url
) {
344 // If the view URL is not equal to 'url', then an inner URL change has
345 // been done (e. g. by activating an existing column in the column view).
347 emit m_extension
->openUrlNotify();
353 void DolphinPartBrowserExtension::cut()
355 m_part
->view()->cutSelectedItems();
358 void DolphinPartBrowserExtension::copy()
360 m_part
->view()->copySelectedItems();
363 void DolphinPartBrowserExtension::paste()
365 m_part
->view()->paste();
370 void DolphinPart::slotEditMimeType()
372 const KFileItemList items
= m_view
->selectedItems();
373 if (!items
.isEmpty()) {
374 KonqOperations::editMimeType(items
.first().mimetype(), m_view
);
378 void DolphinPart::slotProperties()
380 const KFileItemList items
= m_view
->selectedItems();
381 if (!items
.isEmpty()) {
382 KPropertiesDialog
dialog(items
.first().url(), m_view
);
387 void DolphinPart::setCurrentViewMode(const QString
& viewModeName
)
389 QAction
* action
= actionCollection()->action(viewModeName
);
394 QString
DolphinPart::currentViewMode() const
396 return m_actionHandler
->currentViewModeActionName();
399 #include "dolphinpart.moc"