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