]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
Load plugins at the end of the ctor, so that everything is set up already. Will impro...
[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 "dolphinviewactionhandler.h"
22 #include "dolphinsortfilterproxymodel.h"
23 #include "dolphinview.h"
24 #include "dolphinmodel.h"
25
26 #include <konq_operations.h>
27
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>
37
38 #include <QActionGroup>
39 #include <QApplication>
40 #include <QClipboard>
41
42 typedef KParts::GenericFactory<DolphinPart> DolphinPartFactory;
43 K_EXPORT_COMPONENT_FACTORY(dolphinpart, DolphinPartFactory)
44
45 DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QStringList& args)
46 : KParts::ReadOnlyPart(parent)
47 {
48 Q_UNUSED(args)
49 setComponentData(DolphinPartFactory::componentData(), false);
50 m_extension = new DolphinPartBrowserExtension(this);
51
52 // make sure that other apps using this part find Dolphin's view-file-columns icons
53 KIconLoader::global()->addAppDir("dolphin");
54
55 m_dirLister = new KDirLister;
56 m_dirLister->setAutoUpdate(true);
57 m_dirLister->setMainWindow(parentWidget->window());
58 m_dirLister->setDelayedMimeTypes(true);
59
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)));
63
64 m_dolphinModel = new DolphinModel(this);
65 m_dolphinModel->setDirLister(m_dirLister);
66
67 m_proxyModel = new DolphinSortFilterProxyModel(this);
68 m_proxyModel->setSourceModel(m_dolphinModel);
69
70 m_view = new DolphinView(parentWidget,
71 KUrl(),
72 m_dirLister,
73 m_dolphinModel,
74 m_proxyModel);
75 setWidget(m_view);
76
77 setXMLFile("dolphinpart.rc");
78
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
98 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
99 m_actionHandler->setCurrentView(m_view);
100
101 QClipboard* clipboard = QApplication::clipboard();
102 connect(clipboard, SIGNAL(dataChanged()),
103 this, SLOT(updatePasteAction()));
104
105 createActions();
106 m_actionHandler->updateViewActions();
107 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
108
109 // TODO sort_by_* actions
110
111 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
112 // (sort of spacial navigation)
113
114 loadPlugins(this, this, componentData());
115 }
116
117 DolphinPart::~DolphinPart()
118 {
119 delete m_dirLister;
120 }
121
122 void DolphinPart::createActions()
123 {
124 KAction *editMimeTypeAction = actionCollection()->addAction( "editMimeType" );
125 editMimeTypeAction->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
126 connect(editMimeTypeAction, SIGNAL(triggered()), SLOT(slotEditMimeType()));
127
128 KAction *propertiesAction = actionCollection()->addAction( "properties" );
129 propertiesAction->setText( i18nc("@action:inmenu Edit", "Properties") );
130 propertiesAction->setShortcut(Qt::ALT+Qt::Key_Return);
131 connect(propertiesAction, SIGNAL(triggered()), SLOT(slotProperties()));
132
133 // View menu: all done by DolphinViewActionHandler
134
135 // Go menu
136
137 QActionGroup* goActionGroup = new QActionGroup(this);
138 connect(goActionGroup, SIGNAL(triggered(QAction*)),
139 this, SLOT(slotGoTriggered(QAction*)));
140
141 createGoAction("go_applications", "start-here-kde",
142 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
143 goActionGroup);
144 createGoAction("go_network_folders", "folder-remote",
145 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
146 goActionGroup);
147 createGoAction("go_settings", "preferences-system",
148 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
149 goActionGroup);
150 createGoAction("go_trash", "user-trash",
151 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
152 goActionGroup);
153 createGoAction("go_autostart", "",
154 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
155 goActionGroup);
156 }
157
158 void DolphinPart::createGoAction(const char* name, const char* iconName,
159 const QString& text, const QString& url,
160 QActionGroup* actionGroup)
161 {
162 KAction* action = actionCollection()->addAction(name);
163 action->setIcon(KIcon(iconName));
164 action->setText(text);
165 action->setData(url);
166 action->setActionGroup(actionGroup);
167 }
168
169 void DolphinPart::slotGoTriggered(QAction* action)
170 {
171 const QString url = action->data().toString();
172 emit m_extension->openUrlRequest(KUrl(url));
173 }
174
175 void DolphinPart::slotSelectionChanged(const KFileItemList& selection)
176 {
177 const bool hasSelection = !selection.isEmpty();
178 if (!hasSelection) {
179 stateChanged("has_no_selection");
180 } else {
181 stateChanged("has_selection");
182 }
183
184 QStringList actions;
185 actions << "rename" << "move_to_trash" << "delete" << "editMimeType" << "properties";
186 foreach(const QString& actionName, actions) {
187 QAction* action = actionCollection()->action(actionName);
188 Q_ASSERT(action);
189 if (action) {
190 action->setEnabled(hasSelection);
191 }
192 }
193
194 emit m_extension->enableAction("cut", hasSelection);
195 emit m_extension->enableAction("copy", hasSelection);
196 }
197
198 void DolphinPart::updatePasteAction()
199 {
200 QPair<bool, QString> pasteInfo = m_view->pasteInfo();
201 emit m_extension->enableAction( "paste", pasteInfo.first );
202 emit m_extension->setActionText( "paste", pasteInfo.second );
203 }
204
205 KAboutData* DolphinPart::createAboutData()
206 {
207 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
208 }
209
210 bool DolphinPart::openUrl(const KUrl& url)
211 {
212 const bool reload = arguments().reload();
213 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
214 return true;
215 }
216 setUrl(url); // remember it at the KParts level
217 const QString prettyUrl = url.pathOrUrl();
218 emit setWindowCaption(prettyUrl);
219 emit m_extension->setLocationBarUrl(prettyUrl);
220 emit started(0); // get the wheel to spin
221 m_view->setUrl(url);
222 emit aboutToOpenURL();
223 if (reload)
224 m_view->reload();
225 return true;
226 }
227
228 void DolphinPart::slotCompleted(const KUrl& url)
229 {
230 Q_UNUSED(url)
231 emit completed();
232 }
233
234 void DolphinPart::slotCanceled(const KUrl& url)
235 {
236 slotCompleted(url);
237 }
238
239 void DolphinPart::slotInfoMessage(const QString& msg)
240 {
241 emit setStatusBarText(msg);
242 }
243
244 void DolphinPart::slotErrorMessage(const QString& msg)
245 {
246 KMessageBox::error(m_view, msg);
247 }
248
249 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
250 {
251 emit m_extension->mouseOverInfo(item);
252 }
253
254 void DolphinPart::slotItemTriggered(const KFileItem& item)
255 {
256 KParts::OpenUrlArguments args;
257 args.setMimeType(item.mimetype());
258
259 // Ideally, konqueror should be changed to not require trustedSource for directory views,
260 // since the idea was not to need BrowserArguments for non-browser stuff...
261 KParts::BrowserArguments browserArgs;
262 browserArgs.trustedSource = true;
263
264 // MMB click support.
265 // TODO: this doesn't work, mouseButtons() is always 0.
266 // Issue N176832 for the missing QAIV signal; task 177399
267 kDebug() << QApplication::mouseButtons();
268 if (QApplication::mouseButtons() & Qt::MidButton) {
269 kDebug() << "MMB!!" << item.mimetype();
270 if (item.mimeTypePtr()->is("inode/directory")) {
271 emit m_extension->createNewWindow(item.url(), args);
272 } else {
273 kDebug() << "run()";
274 item.run();
275 }
276 } else {
277 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
278 kDebug() << "LMB";
279 emit m_extension->openUrlRequest(item.targetUrl(), args, browserArgs);
280 }
281 }
282
283 void DolphinPart::slotOpenContextMenu(const KFileItem& _item, const KUrl&)
284 {
285 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
286 | KParts::BrowserExtension::ShowProperties
287 | KParts::BrowserExtension::ShowUrlOperations;
288 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
289 // popupFlags |= KParts::BrowserExtension::NoDeletion;
290
291 KFileItem item(_item);
292
293 if (item.isNull()) { // viewport context menu
294 popupFlags |= KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowUp;
295 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
296 // and use this as fallback:
297 item = KFileItem( S_IFDIR, (mode_t)-1, url() );
298 }
299
300 KParts::BrowserExtension::ActionGroupMap actionGroups;
301 QList<QAction *> editActions;
302
303 if (!item.isNull()) { // only for context menu on one or more items
304 // TODO if ( sMoving )
305 editActions.append(actionCollection()->action("rename"));
306
307 bool addTrash = false;
308 bool addDel = false;
309
310 // TODO if ( sMoving && !isIntoTrash && !isTrashLink )
311 addTrash = true;
312
313 /* TODO if ( sDeleting ) */ {
314 if ( !item.isLocalFile() )
315 addDel = true;
316 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
317 addTrash = false;
318 addDel = true;
319 }
320 else {
321 KConfigGroup configGroup( KGlobal::config(), "KDE" );
322 if ( configGroup.readEntry( "ShowDeleteCommand", false) )
323 addDel = true;
324 }
325 }
326
327 if (addTrash)
328 editActions.append(actionCollection()->action("move_to_trash"));
329 if (addDel)
330 editActions.append(actionCollection()->action("delete"));
331 actionGroups.insert("editactions", editActions);
332
333 // TODO: We should change the signature of the slots (and signals) for being able
334 // to tell for which items we want a popup.
335 KFileItemList items = (m_view->selectedItems().count() ? m_view->selectedItems()
336 : KFileItemList() << item);
337 emit m_extension->popupMenu(QCursor::pos(),
338 items,
339 KParts::OpenUrlArguments(),
340 KParts::BrowserArguments(),
341 popupFlags,
342 actionGroups);
343 }
344 }
345
346 void DolphinPart::slotUrlChanged(const KUrl& url)
347 {
348 if (m_view->url() != url) {
349 // If the view URL is not equal to 'url', then an inner URL change has
350 // been done (e. g. by activating an existing column in the column view).
351 openUrl(url);
352 emit m_extension->openUrlNotify();
353 }
354 }
355
356 ////
357
358 void DolphinPartBrowserExtension::cut()
359 {
360 m_part->view()->cutSelectedItems();
361 }
362
363 void DolphinPartBrowserExtension::copy()
364 {
365 m_part->view()->copySelectedItems();
366 }
367
368 void DolphinPartBrowserExtension::paste()
369 {
370 m_part->view()->paste();
371 }
372
373 void DolphinPartBrowserExtension::reparseConfiguration()
374 {
375 m_part->view()->refresh();
376 }
377
378 ////
379
380 void DolphinPart::slotEditMimeType()
381 {
382 const KFileItemList items = m_view->selectedItems();
383 if (!items.isEmpty()) {
384 KonqOperations::editMimeType(items.first().mimetype(), m_view);
385 }
386 }
387
388 void DolphinPart::slotProperties()
389 {
390 const KFileItemList items = m_view->selectedItems();
391 if (!items.isEmpty()) {
392 KPropertiesDialog dialog(items.first().url(), m_view);
393 dialog.exec();
394 }
395 }
396
397 void DolphinPart::setCurrentViewMode(const QString& viewModeName)
398 {
399 QAction* action = actionCollection()->action(viewModeName);
400 Q_ASSERT(action);
401 action->trigger();
402 }
403
404 QString DolphinPart::currentViewMode() const
405 {
406 return m_actionHandler->currentViewModeActionName();
407 }
408
409 #include "dolphinpart.moc"