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