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 <kactioncollection.h>
26 #include <kdirlister.h>
27 #include <kmessagebox.h>
28 #include <kparts/genericfactory.h>
29 #include <ktoggleaction.h>
31 #include <QActionGroup>
32 #include <QApplication>
35 typedef KParts::GenericFactory
<DolphinPart
> DolphinPartFactory
;
36 K_EXPORT_COMPONENT_FACTORY(dolphinpart
, DolphinPartFactory
)
38 DolphinPart::DolphinPart(QWidget
* parentWidget
, QObject
* parent
, const QStringList
& args
)
39 : KParts::ReadOnlyPart(parent
)
42 setComponentData( DolphinPartFactory::componentData() );
43 m_extension
= new DolphinPartBrowserExtension(this);
45 m_dirLister
= new KDirLister
;
46 m_dirLister
->setAutoUpdate(true);
47 m_dirLister
->setMainWindow(parentWidget
->topLevelWidget());
48 m_dirLister
->setDelayedMimeTypes(true);
50 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
51 connect(m_dirLister
, SIGNAL(completed(KUrl
)), this, SLOT(slotCompleted(KUrl
)));
52 connect(m_dirLister
, SIGNAL(canceled(KUrl
)), this, SLOT(slotCanceled(KUrl
)));
54 m_dolphinModel
= new DolphinModel(this);
55 m_dolphinModel
->setDirLister(m_dirLister
);
57 m_proxyModel
= new DolphinSortFilterProxyModel(this);
58 m_proxyModel
->setSourceModel(m_dolphinModel
);
60 m_view
= new DolphinView(parentWidget
,
67 setXMLFile("dolphinpart.rc");
69 connect(m_view
, SIGNAL(infoMessage(QString
)),
70 this, SLOT(slotInfoMessage(QString
)));
71 connect(m_view
, SIGNAL(errorMessage(QString
)),
72 this, SLOT(slotErrorMessage(QString
)));
73 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
74 this, SLOT(slotItemTriggered(KFileItem
)));
75 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
,KUrl
)),
76 this, SLOT(slotOpenContextMenu(KFileItem
,KUrl
)));
77 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
78 m_extension
, SIGNAL(selectionInfo(KFileItemList
)));
79 connect(m_view
, SIGNAL(selectionChanged(KFileItemList
)),
80 this, SLOT(slotSelectionChanged(KFileItemList
)));
81 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
82 this, SLOT(slotRequestItemInfo(KFileItem
)));
83 connect(m_view
, SIGNAL(urlChanged(KUrl
)),
84 this, SLOT(slotUrlChanged(KUrl
)));
85 connect(m_view
, SIGNAL(modeChanged()),
86 this, SLOT(updateViewActions()));
88 QClipboard
* clipboard
= QApplication::clipboard();
89 connect(clipboard
, SIGNAL(dataChanged()),
90 this, SLOT(updatePasteAction()));
95 // TODO provide these actions in the menu, merged with the existing view-mode-actions somehow
96 // [Q_PROPERTY introspection?]
98 // TODO sort_by_* actions
99 // TODO show_*_info actions
101 // TODO connect to urlsDropped
103 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
104 // (sort of spacial navigation)
106 // TODO MMB-click should do something like KonqDirPart::mmbClicked
108 // TODO updating the trash and del actions too - or removing special handling of those from konq?
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
= new KAction(i18nc("@action:inmenu", "Rename..."), this);
125 connect(renameAction
, SIGNAL(triggered()), m_view
, SLOT(renameSelectedItems()));
126 renameAction
->setEnabled(false);
127 renameAction
->setShortcut(Qt::Key_F2
);
128 actionCollection()->addAction("rename", renameAction
);
131 void DolphinPart::slotSelectionChanged(const KFileItemList
& selection
)
133 // Yes, DolphinMainWindow has very similar code :/
134 const bool hasSelection
= !selection
.isEmpty();
136 stateChanged("has_no_selection");
138 stateChanged("has_selection");
140 QAction
* renameAction
= actionCollection()->action("rename");
141 Q_ASSERT(renameAction
);
143 renameAction
->setEnabled(true);
146 emit m_extension
->enableAction("cut", hasSelection
);
147 emit m_extension
->enableAction("copy", hasSelection
);
150 void DolphinPart::updatePasteAction()
152 QPair
<bool, QString
> pasteInfo
= m_view
->pasteInfo();
153 emit m_extension
->enableAction( "paste", pasteInfo
.first
);
154 emit m_extension
->setActionText( "paste", pasteInfo
.second
);
157 void DolphinPart::updateViewActions()
159 QAction
* action
= actionCollection()->action(m_view
->currentViewModeActionName());
161 KToggleAction
* toggleAction
= static_cast<KToggleAction
*>(action
);
162 toggleAction
->setChecked(true);
166 KAboutData
* DolphinPart::createAboutData()
168 return new KAboutData("dolphinpart", 0, ki18nc("@title", "Dolphin Part"), "0.1");
171 bool DolphinPart::openUrl(const KUrl
& url
)
173 const bool reload
= arguments().reload();
174 if (m_view
->url() == url
&& !reload
) { // DolphinView won't do anything in that case, so don't emit started
177 setUrl(url
); // remember it at the KParts level
178 const QString prettyUrl
= url
.pathOrUrl();
179 emit
setWindowCaption(prettyUrl
);
180 emit m_extension
->setLocationBarUrl(prettyUrl
);
184 emit
started(0); // get the wheel to spin
188 void DolphinPart::slotCompleted(const KUrl
& url
)
194 void DolphinPart::slotCanceled(const KUrl
& url
)
199 void DolphinPart::slotInfoMessage(const QString
& msg
)
201 emit
setStatusBarText(msg
);
204 void DolphinPart::slotErrorMessage(const QString
& msg
)
206 KMessageBox::error(m_view
, msg
);
209 void DolphinPart::slotRequestItemInfo(const KFileItem
& item
)
211 emit m_extension
->mouseOverInfo(item
);
214 void DolphinPart::slotItemTriggered(const KFileItem
& item
)
216 qDebug() << QApplication::mouseButtons();
217 if (QApplication::mouseButtons() & Qt::MidButton
) {
218 qDebug() << "MMB!!" << item
.mimetype();
219 if (item
.mimeTypePtr()->is("inode/directory")) {
220 KParts::OpenUrlArguments args
;
221 args
.setMimeType( item
.mimetype() );
222 emit m_extension
->createNewWindow( item
.url(), args
);
228 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
230 emit m_extension
->openUrlRequest(item
.url());
234 void DolphinPart::slotOpenContextMenu(const KFileItem
& _item
, const KUrl
&)
236 KParts::BrowserExtension::PopupFlags popupFlags
= KParts::BrowserExtension::DefaultPopupItems
237 | KParts::BrowserExtension::ShowProperties
238 | KParts::BrowserExtension::ShowUrlOperations
;
239 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
240 // popupFlags |= KParts::BrowserExtension::NoDeletion;
242 KFileItem
item(_item
);
244 if (item
.isNull()) { // viewport context menu
245 popupFlags
|= KParts::BrowserExtension::ShowNavigationItems
| KParts::BrowserExtension::ShowUp
;
246 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
247 // and use this as fallback:
248 item
= KFileItem( S_IFDIR
, (mode_t
)-1, url() );
251 KParts::BrowserExtension::ActionGroupMap actionGroups
;
252 QList
<QAction
*> editActions
;
253 editActions
.append(actionCollection()->action("rename"));
254 actionGroups
.insert("editactions", editActions
);
256 KFileItemList items
; items
.append(item
);
257 emit m_extension
->popupMenu(QCursor::pos(),
259 KParts::OpenUrlArguments(),
260 KParts::BrowserArguments(),
265 void DolphinPart::slotViewModeActionTriggered(QAction
* action
)
267 const DolphinView::Mode mode
= action
->data().value
<DolphinView::Mode
>();
268 m_view
->setMode(mode
);
271 void DolphinPart::slotUrlChanged(const KUrl
& url
)
273 if (m_view
->url() != url
) {
274 // If the view URL is not equal to 'url', then an inner URL change has
275 // been done (e. g. by activating an existing column in the column view).
276 // From the hosts point of view this must be handled like changing the URL.
277 emit m_extension
->openUrlRequest(url
);
283 void DolphinPartBrowserExtension::cut()
285 m_part
->view()->cutSelectedItems();
288 void DolphinPartBrowserExtension::copy()
290 m_part
->view()->copySelectedItems();
293 void DolphinPartBrowserExtension::paste()
295 m_part
->view()->paste();
298 #include "dolphinpart.moc"