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