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