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