]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
Simplify DolphinController: don't remember the show-preview state in the controller...
[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 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
86 this, SLOT(slotRequestItemInfo(KFileItem)));
87 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
88 this, SLOT(slotUrlChanged(const KUrl&)));
89
90 createActions();
91 updateViewActions();
92
93 // TODO provide these actions in the menu, merged with the existing view-mode-actions somehow
94 // [Q_PROPERTY introspection?]
95
96 // TODO connect to urlsDropped
97
98 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
99 // (sort of spacial navigation)
100
101 // TODO MMB-click should do something like KonqDirPart::mmbClicked
102
103 // TODO updating the paste action
104 // if (paste) emit m_extension->setActionText( "paste", actionText );
105 // emit m_extension->enableAction( "paste", paste );
106
107 // TODO updating the trash and del actions too - or removing special handling of those from konq?
108 }
109
110 DolphinPart::~DolphinPart()
111 {
112 delete m_dirLister;
113 }
114
115 void DolphinPart::createActions()
116 {
117 QActionGroup* viewModeActions = new QActionGroup(this);
118 viewModeActions->addAction(DolphinView::iconsModeAction(actionCollection()));
119 viewModeActions->addAction(DolphinView::detailsModeAction(actionCollection()));
120 viewModeActions->addAction(DolphinView::columnsModeAction(actionCollection()));
121 connect(viewModeActions, SIGNAL(triggered(QAction*)), this, SLOT(slotViewModeActionTriggered(QAction*)));
122 }
123
124 void DolphinPart::updateViewActions()
125 {
126 QAction* action = actionCollection()->action(m_view->currentViewModeActionName());
127 if (action != 0) {
128 KToggleAction* toggleAction = static_cast<KToggleAction*>(action);
129 toggleAction->setChecked(true);
130 }
131 }
132
133 KAboutData* DolphinPart::createAboutData()
134 {
135 return new KAboutData("dolphinpart", 0, ki18nc("@title", "Dolphin Part"), "0.1");
136 }
137
138 bool DolphinPart::openUrl(const KUrl& url)
139 {
140 const QString prettyUrl = url.pathOrUrl();
141 emit setWindowCaption(prettyUrl);
142 emit m_extension->setLocationBarUrl(prettyUrl);
143 const bool reload = arguments().reload();
144 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
145 return true;
146 }
147 setUrl(url); // remember it at the KParts level
148 m_view->setUrl(url);
149 if (reload)
150 m_view->reload();
151 emit started(0); // get the wheel to spin
152 return true;
153 }
154
155 void DolphinPart::slotCompleted(const KUrl& url)
156 {
157 Q_UNUSED(url)
158 emit completed();
159 }
160
161 void DolphinPart::slotCanceled(const KUrl& url)
162 {
163 slotCompleted(url);
164 }
165
166 void DolphinPart::slotInfoMessage(const QString& msg)
167 {
168 emit setStatusBarText(msg);
169 }
170
171 void DolphinPart::slotErrorMessage(const QString& msg)
172 {
173 KMessageBox::error(m_view, msg);
174 }
175
176 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
177 {
178 emit m_extension->mouseOverInfo(item);
179 }
180
181 void DolphinPart::slotItemTriggered(const KFileItem& item)
182 {
183 qDebug() << QApplication::mouseButtons();
184 if (QApplication::mouseButtons() & Qt::MidButton) {
185 qDebug() << "MMB!!" << item.mimetype();
186 if (item.mimeTypePtr()->is("inode/directory")) {
187 KParts::OpenUrlArguments args;
188 args.setMimeType( item.mimetype() );
189 emit m_extension->createNewWindow( item.url(), args );
190 } else {
191 qDebug() << "run()";
192 item.run();
193 }
194 } else {
195 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
196 qDebug() << "LMB";
197 emit m_extension->openUrlRequest(item.url());
198 }
199 }
200
201 void DolphinPart::slotOpenContextMenu(const KFileItem& _item, const KUrl&)
202 {
203 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
204 | KParts::BrowserExtension::ShowProperties
205 | KParts::BrowserExtension::ShowUrlOperations;
206 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
207 // popupFlags |= KParts::BrowserExtension::NoDeletion;
208
209 KFileItem item(_item);
210
211 if (item.isNull()) { // viewport context menu
212 popupFlags |= KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowUp;
213 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
214 // and use this as fallback:
215 item = KFileItem( S_IFDIR, (mode_t)-1, url() );
216 }
217
218 KFileItemList items; items.append(item);
219 emit m_extension->popupMenu( QCursor::pos(), items, KParts::OpenUrlArguments(), KParts::BrowserArguments(), popupFlags );
220 }
221
222 void DolphinPart::slotViewModeActionTriggered(QAction* action)
223 {
224 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
225 m_view->setMode(mode);
226 }
227
228 void DolphinPart::slotUrlChanged(const KUrl& url)
229 {
230 if (m_view->url() != url) {
231 // If the view URL is not equal to 'url', then an inner URL change has
232 // been done (e. g. by activating an existing column in the column view).
233 // From the hosts point of view this must be handled like changing the URL.
234 emit m_extension->openUrlRequest(url);
235 }
236 }
237
238 #include "dolphinpart.moc"