]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
Moved popupmenu logic for trash+del to dolphinpart
[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 "dolphinsortfilterproxymodel.h"
22 #include "dolphinview.h"
23 #include "dolphinmodel.h"
24
25 #include <kactioncollection.h>
26 #include <kdirlister.h>
27 #include <kmessagebox.h>
28 #include <kparts/genericfactory.h>
29 #include <ktoggleaction.h>
30 #include <kconfiggroup.h>
31
32 #include <QActionGroup>
33 #include <QApplication>
34 #include <QClipboard>
35
36 typedef KParts::GenericFactory<DolphinPart> DolphinPartFactory;
37 K_EXPORT_COMPONENT_FACTORY(dolphinpart, DolphinPartFactory)
38
39 DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QStringList& args)
40 : KParts::ReadOnlyPart(parent)
41 {
42 Q_UNUSED(args)
43 setComponentData( DolphinPartFactory::componentData() );
44 m_extension = new DolphinPartBrowserExtension(this);
45
46 m_dirLister = new KDirLister;
47 m_dirLister->setAutoUpdate(true);
48 m_dirLister->setMainWindow(parentWidget->topLevelWidget());
49 m_dirLister->setDelayedMimeTypes(true);
50
51 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
52 connect(m_dirLister, SIGNAL(completed(KUrl)), this, SLOT(slotCompleted(KUrl)));
53 connect(m_dirLister, SIGNAL(canceled(KUrl)), this, SLOT(slotCanceled(KUrl)));
54
55 m_dolphinModel = new DolphinModel(this);
56 m_dolphinModel->setDirLister(m_dirLister);
57
58 m_proxyModel = new DolphinSortFilterProxyModel(this);
59 m_proxyModel->setSourceModel(m_dolphinModel);
60
61 m_view = new DolphinView(parentWidget,
62 KUrl(),
63 m_dirLister,
64 m_dolphinModel,
65 m_proxyModel);
66 setWidget(m_view);
67
68 setXMLFile("dolphinpart.rc");
69
70 connect(m_view, SIGNAL(infoMessage(QString)),
71 this, SLOT(slotInfoMessage(QString)));
72 connect(m_view, SIGNAL(errorMessage(QString)),
73 this, SLOT(slotErrorMessage(QString)));
74 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
75 this, SLOT(slotItemTriggered(KFileItem)));
76 connect(m_view, SIGNAL(requestContextMenu(KFileItem,KUrl)),
77 this, SLOT(slotOpenContextMenu(KFileItem,KUrl)));
78 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
79 m_extension, SIGNAL(selectionInfo(KFileItemList)));
80 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
81 this, SLOT(slotSelectionChanged(KFileItemList)));
82 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
83 this, SLOT(slotRequestItemInfo(KFileItem)));
84 connect(m_view, SIGNAL(urlChanged(KUrl)),
85 this, SLOT(slotUrlChanged(KUrl)));
86 connect(m_view, SIGNAL(modeChanged()),
87 this, SLOT(updateViewActions()));
88
89 QClipboard* clipboard = QApplication::clipboard();
90 connect(clipboard, SIGNAL(dataChanged()),
91 this, SLOT(updatePasteAction()));
92
93 createActions();
94 updateViewActions();
95 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
96
97 // TODO provide the viewmode actions in the menu, merged with the existing view-mode-actions somehow
98 // [Q_PROPERTY introspection?]
99
100 // TODO sort_by_* actions
101 // TODO show_*_info actions
102
103 // TODO connect to urlsDropped
104
105 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
106 // (sort of spacial navigation)
107
108 // TODO MMB-click should do something like KonqDirPart::mmbClicked
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
135 void DolphinPart::slotSelectionChanged(const KFileItemList& selection)
136 {
137 // Yes, DolphinMainWindow has very similar code :/
138 const bool hasSelection = !selection.isEmpty();
139 if (!hasSelection) {
140 stateChanged("has_no_selection");
141 } else {
142 stateChanged("has_selection");
143 }
144
145 QStringList actions;
146 actions << "rename" << "move_to_trash" << "delete";
147 foreach(const QString& actionName, actions) {
148 QAction* action = actionCollection()->action(actionName);
149 Q_ASSERT(action);
150 if (action) {
151 action->setEnabled(hasSelection);
152 }
153 }
154
155 emit m_extension->enableAction("cut", hasSelection);
156 emit m_extension->enableAction("copy", hasSelection);
157 }
158
159 void DolphinPart::updatePasteAction()
160 {
161 QPair<bool, QString> pasteInfo = m_view->pasteInfo();
162 emit m_extension->enableAction( "paste", pasteInfo.first );
163 emit m_extension->setActionText( "paste", pasteInfo.second );
164 }
165
166 void DolphinPart::updateViewActions()
167 {
168 QAction* action = actionCollection()->action(m_view->currentViewModeActionName());
169 if (action != 0) {
170 KToggleAction* toggleAction = static_cast<KToggleAction*>(action);
171 toggleAction->setChecked(true);
172 }
173 }
174
175 KAboutData* DolphinPart::createAboutData()
176 {
177 return new KAboutData("dolphinpart", 0, ki18nc("@title", "Dolphin Part"), "0.1");
178 }
179
180 bool DolphinPart::openUrl(const KUrl& url)
181 {
182 const bool reload = arguments().reload();
183 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
184 return true;
185 }
186 setUrl(url); // remember it at the KParts level
187 const QString prettyUrl = url.pathOrUrl();
188 emit setWindowCaption(prettyUrl);
189 emit m_extension->setLocationBarUrl(prettyUrl);
190 m_view->setUrl(url);
191 if (reload)
192 m_view->reload();
193 emit started(0); // get the wheel to spin
194 return true;
195 }
196
197 void DolphinPart::slotCompleted(const KUrl& url)
198 {
199 Q_UNUSED(url)
200 emit completed();
201 }
202
203 void DolphinPart::slotCanceled(const KUrl& url)
204 {
205 slotCompleted(url);
206 }
207
208 void DolphinPart::slotInfoMessage(const QString& msg)
209 {
210 emit setStatusBarText(msg);
211 }
212
213 void DolphinPart::slotErrorMessage(const QString& msg)
214 {
215 KMessageBox::error(m_view, msg);
216 }
217
218 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
219 {
220 emit m_extension->mouseOverInfo(item);
221 }
222
223 void DolphinPart::slotItemTriggered(const KFileItem& item)
224 {
225 qDebug() << QApplication::mouseButtons();
226 if (QApplication::mouseButtons() & Qt::MidButton) {
227 qDebug() << "MMB!!" << item.mimetype();
228 if (item.mimeTypePtr()->is("inode/directory")) {
229 KParts::OpenUrlArguments args;
230 args.setMimeType( item.mimetype() );
231 emit m_extension->createNewWindow( item.url(), args );
232 } else {
233 qDebug() << "run()";
234 item.run();
235 }
236 } else {
237 // Left button. [Right button goes to slotOpenContextMenu before triggered can be emitted]
238 qDebug() << "LMB";
239 emit m_extension->openUrlRequest(item.url());
240 }
241 }
242
243 void DolphinPart::slotOpenContextMenu(const KFileItem& _item, const KUrl&)
244 {
245 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
246 | KParts::BrowserExtension::ShowProperties
247 | KParts::BrowserExtension::ShowUrlOperations;
248 // TODO KonqKfmIconView had if ( !rootItem->isWritable() )
249 // popupFlags |= KParts::BrowserExtension::NoDeletion;
250
251 KFileItem item(_item);
252
253 if (item.isNull()) { // viewport context menu
254 popupFlags |= KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowUp;
255 // TODO get m_dirLister->rootItem if possible. or via kdirmodel?
256 // and use this as fallback:
257 item = KFileItem( S_IFDIR, (mode_t)-1, url() );
258 }
259
260 KParts::BrowserExtension::ActionGroupMap actionGroups;
261 QList<QAction *> editActions;
262
263 if (!item.isNull()) { // only for context menu on one or more items
264 // TODO if ( sMoving )
265 editActions.append(actionCollection()->action("rename"));
266
267 bool addTrash = false;
268 bool addDel = false;
269
270 // TODO if ( sMoving && !isIntoTrash && !isTrashLink )
271 addTrash = true;
272
273 /* TODO if ( sDeleting ) */ {
274 if ( !item.isLocalFile() )
275 addDel = true;
276 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
277 addTrash = false;
278 addDel = true;
279 }
280 else {
281 KConfigGroup configGroup( KGlobal::config(), "KDE" );
282 if ( configGroup.readEntry( "ShowDeleteCommand", false) )
283 addDel = true;
284 }
285 }
286
287 if (addTrash)
288 editActions.append(actionCollection()->action("move_to_trash"));
289 if (addDel)
290 editActions.append(actionCollection()->action("delete"));
291 actionGroups.insert("editactions", editActions);
292
293 KFileItemList items; items.append(item);
294 emit m_extension->popupMenu(QCursor::pos(),
295 items,
296 KParts::OpenUrlArguments(),
297 KParts::BrowserArguments(),
298 popupFlags,
299 actionGroups);
300 }
301 }
302
303 void DolphinPart::slotViewModeActionTriggered(QAction* action)
304 {
305 const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
306 m_view->setMode(mode);
307 }
308
309 void DolphinPart::slotUrlChanged(const KUrl& url)
310 {
311 if (m_view->url() != url) {
312 // If the view URL is not equal to 'url', then an inner URL change has
313 // been done (e. g. by activating an existing column in the column view).
314 // From the hosts point of view this must be handled like changing the URL.
315 emit m_extension->openUrlRequest(url);
316 }
317 }
318
319 ////
320
321 void DolphinPartBrowserExtension::cut()
322 {
323 m_part->view()->cutSelectedItems();
324 }
325
326 void DolphinPartBrowserExtension::copy()
327 {
328 m_part->view()->copySelectedItems();
329 }
330
331 void DolphinPartBrowserExtension::paste()
332 {
333 m_part->view()->paste();
334 }
335
336 ////
337
338 void DolphinPart::slotTrashActivated(Qt::MouseButtons, Qt::KeyboardModifiers modifiers)
339 {
340 // Note: kde3's konq_mainwindow.cpp used to check
341 // reason == KAction::PopupMenuActivation && ...
342 // but this isn't supported anymore
343 if (modifiers & Qt::ShiftModifier)
344 m_view->deleteSelectedItems();
345 else
346 m_view->trashSelectedItems();
347 }
348
349 #include "dolphinpart.moc"