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