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