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