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