]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
DolphinPart: provide a way to switch between view modes in konqueror.
[dolphin.git] / src / dolphinpart.cpp
1 /* This file is part of the KDE project
2 Copyright (c) 2007 David Faure <faure@kde.org>
3
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.
8
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.
13
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.
18 */
19
20 #include "dolphinpart.h"
21 #include <kactioncollection.h>
22 #include <ktoggleaction.h>
23 #include <QActionGroup>
24 #include "dolphinsortfilterproxymodel.h"
25 #include "dolphinview.h"
26 #include "dolphinmodel.h"
27
28 #include <kdirlister.h>
29 #include <kmessagebox.h>
30 #include <kparts/browserextension.h>
31 #include <kparts/genericfactory.h>
32 #include <QApplication>
33
34 typedef KParts::GenericFactory<DolphinPart> DolphinPartFactory;
35 K_EXPORT_COMPONENT_FACTORY(dolphinpart, DolphinPartFactory)
36
37 class DolphinPartBrowserExtension : public KParts::BrowserExtension
38 {
39 public:
40 DolphinPartBrowserExtension( KParts::ReadOnlyPart* part )
41 : KParts::BrowserExtension( part ) {}
42 };
43
44 DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QStringList& args)
45 : KParts::ReadOnlyPart(parent)
46 {
47 Q_UNUSED(args)
48 setComponentData( DolphinPartFactory::componentData() );
49 m_extension = new DolphinPartBrowserExtension(this);
50
51 m_dirLister = new KDirLister;
52 m_dirLister->setAutoUpdate(true);
53 m_dirLister->setMainWindow(parentWidget->topLevelWidget());
54 m_dirLister->setDelayedMimeTypes(true);
55
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)));
59
60 m_dolphinModel = new DolphinModel(this);
61 m_dolphinModel->setDirLister(m_dirLister);
62
63 m_proxyModel = new DolphinSortFilterProxyModel(this);
64 m_proxyModel->setSourceModel(m_dolphinModel);
65
66 m_view = new DolphinView(parentWidget,
67 KUrl(),
68 m_dirLister,
69 m_dolphinModel,
70 m_proxyModel);
71 setWidget(m_view);
72
73 setXMLFile("dolphinpart.rc");
74
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, const KUrl&)),
82 this, SLOT(slotOpenContextMenu(KFileItem, const KUrl&)));
83 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
84 m_extension, SIGNAL(selectionInfo(KFileItemList)));
85
86 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
87 this, SLOT(slotRequestItemInfo(KFileItem)));
88
89 createActions();
90 updateViewActions();
91
92 // TODO provide these actions in the menu, merged with the existing view-mode-actions somehow
93 // [Q_PROPERTY introspection?]
94
95 // TODO connect to urlsDropped
96
97 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
98 // (sort of spacial navigation)
99
100 // TODO MMB-click should do something like KonqDirPart::mmbClicked
101
102 // TODO updating the paste action
103 // if (paste) emit m_extension->setActionText( "paste", actionText );
104 // emit m_extension->enableAction( "paste", paste );
105
106 // TODO updating the trash and del actions too - or removing special handling of those from konq?
107 }
108
109 DolphinPart::~DolphinPart()
110 {
111 delete m_dirLister;
112 }
113
114 void DolphinPart::createActions()
115 {
116 QActionGroup* viewModeActions = new QActionGroup(this);
117 viewModeActions->addAction(DolphinView::iconsModeAction(actionCollection()));
118 viewModeActions->addAction(DolphinView::detailsModeAction(actionCollection()));
119 viewModeActions->addAction(DolphinView::columnsModeAction(actionCollection()));
120 connect(viewModeActions, SIGNAL(triggered(QAction*)), this, SLOT(slotViewModeActionTriggered(QAction*)));
121 }
122
123 void DolphinPart::updateViewActions()
124 {
125 QAction* action = actionCollection()->action(m_view->currentViewModeActionName());
126 if (action != 0) {
127 KToggleAction* toggleAction = static_cast<KToggleAction*>(action);
128 toggleAction->setChecked(true);
129 }
130 }
131
132 KAboutData* DolphinPart::createAboutData()
133 {
134 return new KAboutData("dolphinpart", 0, ki18nc("@title", "Dolphin Part"), "0.1");
135 }
136
137 bool DolphinPart::openUrl(const KUrl& url)
138 {
139 const QString prettyUrl = url.pathOrUrl();
140 emit setWindowCaption(prettyUrl);
141 emit m_extension->setLocationBarUrl(prettyUrl);
142 const bool reload = arguments().reload();
143 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
144 return true;
145 }
146 setUrl(url); // remember it at the KParts level
147 m_view->setUrl(url);
148 if (reload)
149 m_view->reload();
150 emit started(0); // get the wheel to spin
151 return true;
152 }
153
154 void DolphinPart::slotCompleted(const KUrl& url)
155 {
156 Q_UNUSED(url)
157 emit completed();
158 }
159
160 void DolphinPart::slotCanceled(const KUrl& url)
161 {
162 slotCompleted(url);
163 }
164
165 void DolphinPart::slotInfoMessage(const QString& msg)
166 {
167 emit setStatusBarText(msg);
168 }
169
170 void DolphinPart::slotErrorMessage(const QString& msg)
171 {
172 KMessageBox::error(m_view, msg);
173 }
174
175 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
176 {
177 emit m_extension->mouseOverInfo(item);
178 }
179
180 void DolphinPart::slotItemTriggered(const KFileItem& item)
181 {
182 qDebug() << QApplication::mouseButtons();
183 if (QApplication::mouseButtons() & Qt::MidButton) {
184 qDebug() << "MMB!!" << item.mimetype();
185 if (item.mimeTypePtr()->is("inode/directory")) {
186 KParts::OpenUrlArguments args;
187 args.setMimeType( item.mimetype() );
188 emit m_extension->createNewWindow( item.url(), args );
189 } else {
190 qDebug() << "run()";
191 item.run();
192 }
193 } else {
194 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
195 qDebug() << "LMB";
196 emit m_extension->openUrlRequest(item.url());
197 }
198 }
199
200 void DolphinPart::slotOpenContextMenu(const KFileItem& _item, const KUrl&)
201 {
202 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
203 | KParts::BrowserExtension::ShowProperties
204 | KParts::BrowserExtension::ShowUrlOperations;
205 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
206 // popupFlags |= KParts::BrowserExtension::NoDeletion;
207
208 KFileItem item(_item);
209
210 if (item.isNull()) { // viewport context menu
211 popupFlags |= KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowUp;
212 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
213 // and use this as fallback:
214 item = KFileItem( S_IFDIR, (mode_t)-1, url() );
215 }
216
217 KFileItemList items; items.append(item);
218 emit m_extension->popupMenu( QCursor::pos(), items, KParts::OpenUrlArguments(), KParts::BrowserArguments(), popupFlags );
219 }
220
221 void DolphinPart::slotViewModeActionTriggered(QAction* action)
222 {
223 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
224 m_view->setMode(mode);
225 }
226
227 #include "dolphinpart.moc"