]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
Move a bunch of Go menu actions from konqueror to dolphinpart, so that they don't...
[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 // This action doesn't appear in the GUI, it's for the shortcut only.
134 // KNewMenu takes care of the GUI stuff.
135 KAction* newDirAction = actionCollection()->addAction( "create_dir" );
136 newDirAction->setText( i18n("Create Folder..." ) );
137 connect(newDirAction, SIGNAL(triggered()), SLOT(slotNewDir()));
138 newDirAction->setShortcut(Qt::Key_F10);
139 widget()->addAction(newDirAction);
140
141 // Go menu
142
143 QActionGroup* goActionGroup = new QActionGroup(this);
144 connect(goActionGroup, SIGNAL(triggered(QAction*)),
145 this, SLOT(slotGoTriggered(QAction*)));
146
147 createGoAction("go_applications", "start-here",
148 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
149 goActionGroup);
150 createGoAction("go_network_folders", "drive-remote",
151 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
152 goActionGroup);
153 createGoAction("go_settings", "preferences-system",
154 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
155 goActionGroup);
156 createGoAction("go_trash", "user-trash",
157 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
158 goActionGroup);
159 createGoAction("go_autostart", "",
160 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
161 goActionGroup);
162 }
163
164 void DolphinPart::createGoAction(const char* name, const char* iconName,
165 const QString& text, const QString& url,
166 QActionGroup* actionGroup)
167 {
168 KAction* action = actionCollection()->addAction(name);
169 action->setIcon(KIcon(iconName));
170 action->setText(text);
171 action->setData(url);
172 action->setActionGroup(actionGroup);
173 }
174
175 void DolphinPart::slotGoTriggered(QAction* action)
176 {
177 const QString url = action->data().toString();
178 emit m_extension->openUrlRequest(KUrl(url));
179 }
180
181 void DolphinPart::slotSelectionChanged(const KFileItemList& selection)
182 {
183 const bool hasSelection = !selection.isEmpty();
184 if (!hasSelection) {
185 stateChanged("has_no_selection");
186 } else {
187 stateChanged("has_selection");
188 }
189
190 QStringList actions;
191 actions << "rename" << "move_to_trash" << "delete";
192 foreach(const QString& actionName, actions) {
193 QAction* action = actionCollection()->action(actionName);
194 Q_ASSERT(action);
195 if (action) {
196 action->setEnabled(hasSelection);
197 }
198 }
199
200 emit m_extension->enableAction("cut", hasSelection);
201 emit m_extension->enableAction("copy", hasSelection);
202 }
203
204 void DolphinPart::updatePasteAction()
205 {
206 QPair<bool, QString> pasteInfo = m_view->pasteInfo();
207 emit m_extension->enableAction( "paste", pasteInfo.first );
208 emit m_extension->setActionText( "paste", pasteInfo.second );
209 }
210
211 void DolphinPart::updateViewActions()
212 {
213 QAction* action = actionCollection()->action(m_view->currentViewModeActionName());
214 if (action != 0) {
215 KToggleAction* toggleAction = static_cast<KToggleAction*>(action);
216 toggleAction->setChecked(true);
217 }
218 }
219
220 KAboutData* DolphinPart::createAboutData()
221 {
222 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
223 }
224
225 bool DolphinPart::openUrl(const KUrl& url)
226 {
227 const bool reload = arguments().reload();
228 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
229 return true;
230 }
231 setUrl(url); // remember it at the KParts level
232 const QString prettyUrl = url.pathOrUrl();
233 emit setWindowCaption(prettyUrl);
234 emit m_extension->setLocationBarUrl(prettyUrl);
235 emit started(0); // get the wheel to spin
236 m_view->setUrl(url);
237 if (reload)
238 m_view->reload();
239 return true;
240 }
241
242 void DolphinPart::slotCompleted(const KUrl& url)
243 {
244 Q_UNUSED(url)
245 emit completed();
246 }
247
248 void DolphinPart::slotCanceled(const KUrl& url)
249 {
250 slotCompleted(url);
251 }
252
253 void DolphinPart::slotInfoMessage(const QString& msg)
254 {
255 emit setStatusBarText(msg);
256 }
257
258 void DolphinPart::slotErrorMessage(const QString& msg)
259 {
260 KMessageBox::error(m_view, msg);
261 }
262
263 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
264 {
265 emit m_extension->mouseOverInfo(item);
266 }
267
268 void DolphinPart::slotItemTriggered(const KFileItem& item)
269 {
270 // MMB click support.
271 // TODO: this doesn't work, mouseButtons() is always 0.
272 // Issue N176832 for the missing QAIV signal; task 177399
273 kDebug() << QApplication::mouseButtons();
274 if (QApplication::mouseButtons() & Qt::MidButton) {
275 kDebug() << "MMB!!" << item.mimetype();
276 if (item.mimeTypePtr()->is("inode/directory")) {
277 KParts::OpenUrlArguments args;
278 args.setMimeType( item.mimetype() );
279 emit m_extension->createNewWindow( item.url(), args );
280 } else {
281 kDebug() << "run()";
282 item.run();
283 }
284 } else {
285 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
286 kDebug() << "LMB";
287 emit m_extension->openUrlRequest(item.url());
288 }
289 }
290
291 void DolphinPart::slotOpenContextMenu(const KFileItem& _item, const KUrl&)
292 {
293 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
294 | KParts::BrowserExtension::ShowProperties
295 | KParts::BrowserExtension::ShowUrlOperations;
296 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
297 // popupFlags |= KParts::BrowserExtension::NoDeletion;
298
299 KFileItem item(_item);
300
301 if (item.isNull()) { // viewport context menu
302 popupFlags |= KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowUp;
303 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
304 // and use this as fallback:
305 item = KFileItem( S_IFDIR, (mode_t)-1, url() );
306 }
307
308 KParts::BrowserExtension::ActionGroupMap actionGroups;
309 QList<QAction *> editActions;
310
311 if (!item.isNull()) { // only for context menu on one or more items
312 // TODO if ( sMoving )
313 editActions.append(actionCollection()->action("rename"));
314
315 bool addTrash = false;
316 bool addDel = false;
317
318 // TODO if ( sMoving && !isIntoTrash && !isTrashLink )
319 addTrash = true;
320
321 /* TODO if ( sDeleting ) */ {
322 if ( !item.isLocalFile() )
323 addDel = true;
324 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
325 addTrash = false;
326 addDel = true;
327 }
328 else {
329 KConfigGroup configGroup( KGlobal::config(), "KDE" );
330 if ( configGroup.readEntry( "ShowDeleteCommand", false) )
331 addDel = true;
332 }
333 }
334
335 if (addTrash)
336 editActions.append(actionCollection()->action("move_to_trash"));
337 if (addDel)
338 editActions.append(actionCollection()->action("delete"));
339 actionGroups.insert("editactions", editActions);
340
341 KFileItemList items; items.append(item);
342 emit m_extension->popupMenu(QCursor::pos(),
343 items,
344 KParts::OpenUrlArguments(),
345 KParts::BrowserArguments(),
346 popupFlags,
347 actionGroups);
348 }
349 }
350
351 void DolphinPart::slotViewModeActionTriggered(QAction* action)
352 {
353 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
354 m_view->setMode(mode);
355 }
356
357 void DolphinPart::slotUrlChanged(const KUrl& url)
358 {
359 if (m_view->url() != url) {
360 // If the view URL is not equal to 'url', then an inner URL change has
361 // been done (e. g. by activating an existing column in the column view).
362 openUrl(url);
363 emit m_extension->openUrlNotify();
364 }
365 }
366
367 ////
368
369 void DolphinPartBrowserExtension::cut()
370 {
371 m_part->view()->cutSelectedItems();
372 }
373
374 void DolphinPartBrowserExtension::copy()
375 {
376 m_part->view()->copySelectedItems();
377 }
378
379 void DolphinPartBrowserExtension::paste()
380 {
381 m_part->view()->paste();
382 }
383
384 ////
385
386 void DolphinPart::slotTrashActivated(Qt::MouseButtons, Qt::KeyboardModifiers modifiers)
387 {
388 // Note: kde3's konq_mainwindow.cpp used to check
389 // reason == KAction::PopupMenuActivation && ...
390 // but this isn't supported anymore
391 if (modifiers & Qt::ShiftModifier)
392 m_view->deleteSelectedItems();
393 else
394 m_view->trashSelectedItems();
395 }
396
397 void DolphinPart::slotNewDir()
398 {
399 KonqOperations::newDir(widget(), url());
400 }
401
402 #include "dolphinpart.moc"