]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
Remove code that's no longer needed now that Qt supports ARGB windows.
[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 <kdebug.h>
22 #include "dolphinviewactionhandler.h"
23 #include "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "dolphinmodel.h"
26 #include "dolphinnewmenuobserver.h"
27
28 #include <konq_fileitemcapabilities.h>
29 #include <konq_operations.h>
30
31 #include <kaboutdata.h>
32 #include <kactioncollection.h>
33 #include <kconfiggroup.h>
34 #include <kdirlister.h>
35 #include <kglobalsettings.h>
36 #include <kiconloader.h>
37 #include <klocale.h>
38 #include <kmessagebox.h>
39 #include <kpluginfactory.h>
40 #include <ktoggleaction.h>
41 #include <kio/netaccess.h>
42 #include <ktoolinvocation.h>
43 #include <kauthorized.h>
44 #include <knewmenu.h>
45 #include <kmenu.h>
46
47 #include <QActionGroup>
48 #include <QApplication>
49 #include <QClipboard>
50
51 K_PLUGIN_FACTORY(DolphinPartFactory, registerPlugin<DolphinPart>();)
52 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
53
54 DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QVariantList& args)
55 : KParts::ReadOnlyPart(parent)
56 {
57 Q_UNUSED(args)
58 setComponentData(DolphinPartFactory::componentData(), false);
59 m_extension = new DolphinPartBrowserExtension(this);
60
61 // make sure that other apps using this part find Dolphin's view-file-columns icons
62 KIconLoader::global()->addAppDir("dolphin");
63
64 m_dirLister = new KDirLister;
65 m_dirLister->setAutoUpdate(true);
66 if (parentWidget) {
67 m_dirLister->setMainWindow(parentWidget->window());
68 }
69 m_dirLister->setDelayedMimeTypes(true);
70
71 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
72 connect(m_dirLister, SIGNAL(completed(KUrl)), this, SLOT(slotCompleted(KUrl)));
73 connect(m_dirLister, SIGNAL(canceled(KUrl)), this, SLOT(slotCanceled(KUrl)));
74 connect(m_dirLister, SIGNAL(percent(int)), this, SLOT(updateProgress(int)));
75
76 m_dolphinModel = new DolphinModel(this);
77 m_dolphinModel->setDirLister(m_dirLister);
78
79 m_proxyModel = new DolphinSortFilterProxyModel(this);
80 m_proxyModel->setSourceModel(m_dolphinModel);
81
82 m_view = new DolphinView(parentWidget,
83 KUrl(),
84 m_dirLister,
85 m_dolphinModel,
86 m_proxyModel);
87 m_view->setTabsForFilesEnabled(true);
88 setWidget(m_view);
89
90 setXMLFile("dolphinpart.rc");
91
92 connect(m_view, SIGNAL(infoMessage(QString)),
93 this, SLOT(slotInfoMessage(QString)));
94 connect(m_view, SIGNAL(errorMessage(QString)),
95 this, SLOT(slotErrorMessage(QString)));
96 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
97 this, SLOT(slotItemTriggered(KFileItem)));
98 connect(m_view, SIGNAL(tabRequested(KUrl)),
99 this, SLOT(createNewWindow(KUrl)));
100 connect(m_view, SIGNAL(requestContextMenu(KFileItem,KUrl,QList<QAction*>)),
101 this, SLOT(slotOpenContextMenu(KFileItem,KUrl,QList<QAction*>)));
102 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
103 m_extension, SIGNAL(selectionInfo(KFileItemList)));
104 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
105 this, SLOT(slotSelectionChanged(KFileItemList)));
106 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
107 this, SLOT(slotRequestItemInfo(KFileItem)));
108 connect(m_view, SIGNAL(requestUrlChange(KUrl)),
109 this, SLOT(slotRequestUrlChange(KUrl)));
110 connect(m_view, SIGNAL(modeChanged()),
111 this, SIGNAL(viewModeChanged())); // relay signal
112 connect(m_view, SIGNAL(redirection(KUrl, KUrl)),
113 this, SLOT(slotRedirection(KUrl, KUrl)));
114
115 // Watch for changes that should result in updates to the
116 // status bar text.
117 connect(m_dirLister, SIGNAL(deleteItem(const KFileItem&)),
118 this, SLOT(updateStatusBar()));
119 connect(m_dirLister, SIGNAL(clear()),
120 this, SLOT(updateStatusBar()));
121 connect(m_view, SIGNAL(selectionChanged(const KFileItemList)),
122 this, SLOT(updateStatusBar()));
123
124 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
125 m_actionHandler->setCurrentView(m_view);
126
127 QClipboard* clipboard = QApplication::clipboard();
128 connect(clipboard, SIGNAL(dataChanged()),
129 this, SLOT(updatePasteAction()));
130
131 createActions();
132 m_actionHandler->updateViewActions();
133 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
134
135 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
136 // (sort of spacial navigation)
137
138 loadPlugins(this, this, componentData());
139 }
140
141 DolphinPart::~DolphinPart()
142 {
143 DolphinNewMenuObserver::instance().detach(m_newMenu);
144 delete m_dirLister;
145 }
146
147 void DolphinPart::createActions()
148 {
149 // Edit menu
150
151 m_newMenu = new KNewMenu(actionCollection(), widget(), "new_menu");
152 DolphinNewMenuObserver::instance().attach(m_newMenu);
153 connect(m_newMenu->menu(), SIGNAL(aboutToShow()),
154 this, SLOT(updateNewMenu()));
155
156 KAction *editMimeTypeAction = actionCollection()->addAction( "editMimeType" );
157 editMimeTypeAction->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
158 connect(editMimeTypeAction, SIGNAL(triggered()), SLOT(slotEditMimeType()));
159
160 // View menu: all done by DolphinViewActionHandler
161
162 // Go menu
163
164 QActionGroup* goActionGroup = new QActionGroup(this);
165 connect(goActionGroup, SIGNAL(triggered(QAction*)),
166 this, SLOT(slotGoTriggered(QAction*)));
167
168 createGoAction("go_applications", "start-here-kde",
169 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
170 goActionGroup);
171 createGoAction("go_network_folders", "folder-remote",
172 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
173 goActionGroup);
174 createGoAction("go_settings", "preferences-system",
175 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
176 goActionGroup);
177 createGoAction("go_trash", "user-trash",
178 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
179 goActionGroup);
180 createGoAction("go_autostart", "",
181 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
182 goActionGroup);
183
184 // Tools menu
185 if (KAuthorized::authorizeKAction("shell_access")) {
186 KAction* action = actionCollection()->addAction("open_terminal");
187 action->setIcon(KIcon("utilities-terminal"));
188 action->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
189 connect(action, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
190 action->setShortcut(Qt::Key_F4);
191 }
192
193 }
194
195 void DolphinPart::createGoAction(const char* name, const char* iconName,
196 const QString& text, const QString& url,
197 QActionGroup* actionGroup)
198 {
199 KAction* action = actionCollection()->addAction(name);
200 action->setIcon(KIcon(iconName));
201 action->setText(text);
202 action->setData(url);
203 action->setActionGroup(actionGroup);
204 }
205
206 void DolphinPart::slotGoTriggered(QAction* action)
207 {
208 const QString url = action->data().toString();
209 emit m_extension->openUrlRequest(KUrl(url));
210 }
211
212 void DolphinPart::slotSelectionChanged(const KFileItemList& selection)
213 {
214 const bool hasSelection = !selection.isEmpty();
215
216 QAction* renameAction = actionCollection()->action("rename");
217 QAction* moveToTrashAction = actionCollection()->action("move_to_trash");
218 QAction* deleteAction = actionCollection()->action("delete");
219 QAction* editMimeTypeAction = actionCollection()->action("editMimeType");
220 QAction* propertiesAction = actionCollection()->action("properties");
221 QAction* deleteWithTrashShortcut = actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
222
223 if (!hasSelection) {
224 stateChanged("has_no_selection");
225
226 emit m_extension->enableAction("cut", false);
227 emit m_extension->enableAction("copy", false);
228 deleteWithTrashShortcut->setEnabled(false);
229 editMimeTypeAction->setEnabled(false);
230 } else {
231 stateChanged("has_selection");
232
233 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
234 // in libkonq
235 KonqFileItemCapabilities capabilities(selection);
236 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
237
238 renameAction->setEnabled(capabilities.supportsMoving());
239 moveToTrashAction->setEnabled(enableMoveToTrash);
240 deleteAction->setEnabled(capabilities.supportsDeleting());
241 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
242 editMimeTypeAction->setEnabled(true);
243 propertiesAction->setEnabled(true);
244 emit m_extension->enableAction("cut", capabilities.supportsMoving());
245 emit m_extension->enableAction("copy", true);
246 }
247 }
248
249 void DolphinPart::updatePasteAction()
250 {
251 QPair<bool, QString> pasteInfo = m_view->pasteInfo();
252 emit m_extension->enableAction( "paste", pasteInfo.first );
253 emit m_extension->setActionText( "paste", pasteInfo.second );
254 }
255
256 KAboutData* DolphinPart::createAboutData()
257 {
258 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
259 }
260
261 bool DolphinPart::openUrl(const KUrl& url)
262 {
263 bool reload = arguments().reload();
264 // A bit of a workaround so that changing the namefilter works: force reload.
265 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
266 if (m_nameFilter != m_dirLister->nameFilter())
267 reload = true;
268 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
269 return true;
270 }
271 setUrl(url); // remember it at the KParts level
272 KUrl visibleUrl(url);
273 if (!m_nameFilter.isEmpty()) {
274 visibleUrl.addPath(m_nameFilter);
275 }
276 QString prettyUrl = visibleUrl.pathOrUrl();
277 emit setWindowCaption(prettyUrl);
278 emit m_extension->setLocationBarUrl(prettyUrl);
279 emit started(0); // get the wheel to spin
280 m_dirLister->setNameFilter(m_nameFilter);
281 m_view->setUrl(url);
282 updatePasteAction();
283 emit aboutToOpenURL();
284 if (reload)
285 m_view->reload();
286 return true;
287 }
288
289 void DolphinPart::slotCompleted(const KUrl& url)
290 {
291 Q_UNUSED(url)
292 emit completed();
293 }
294
295 void DolphinPart::slotCanceled(const KUrl& url)
296 {
297 slotCompleted(url);
298 }
299
300 void DolphinPart::slotInfoMessage(const QString& msg)
301 {
302 emit setStatusBarText(msg);
303 }
304
305 void DolphinPart::slotErrorMessage(const QString& msg)
306 {
307 KMessageBox::error(m_view, msg);
308 }
309
310 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
311 {
312 emit m_extension->mouseOverInfo(item);
313 if (item.isNull()) {
314 updateStatusBar();
315 } else {
316 ReadOnlyPart::setStatusBarText(item.getStatusBarInfo());
317 }
318 }
319
320 void DolphinPart::slotItemTriggered(const KFileItem& item)
321 {
322 KParts::OpenUrlArguments args;
323 args.setMimeType(item.mimetype());
324
325 // Ideally, konqueror should be changed to not require trustedSource for directory views,
326 // since the idea was not to need BrowserArguments for non-browser stuff...
327 KParts::BrowserArguments browserArgs;
328 browserArgs.trustedSource = true;
329 emit m_extension->openUrlRequest(item.targetUrl(), args, browserArgs);
330 }
331
332 void DolphinPart::createNewWindow(const KUrl& url)
333 {
334 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
335 // should be moved into DolphinPart::slotItemTriggered()
336 KFileItem item(S_IFDIR, (mode_t)-1, url);
337 KParts::OpenUrlArguments args;
338 args.setMimeType(item.mimetype());
339 emit m_extension->createNewWindow(url, args);
340 }
341
342 void DolphinPart::slotOpenContextMenu(const KFileItem& _item,
343 const KUrl&,
344 const QList<QAction*>& customActions)
345 {
346 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
347 | KParts::BrowserExtension::ShowProperties
348 | KParts::BrowserExtension::ShowUrlOperations;
349
350 KFileItem item(_item);
351
352 if (item.isNull()) { // viewport context menu
353 popupFlags |= KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowUp;
354 item = m_dirLister->rootItem();
355 if (item.isNull())
356 item = KFileItem( S_IFDIR, (mode_t)-1, url() );
357 }
358
359 KParts::BrowserExtension::ActionGroupMap actionGroups;
360 QList<QAction *> editActions;
361
362 editActions += customActions;
363
364 if (!_item.isNull()) { // only for context menu on one or more items
365 bool sDeleting = true;
366 bool sMoving = true;
367
368 // If the parent directory of the selected item is writable, moving
369 // and deleting are possible.
370 KFileItem parentDir = m_dirLister->rootItem();
371 if (!parentDir.isNull() && !parentDir.isWritable()) {
372 popupFlags |= KParts::BrowserExtension::NoDeletion;
373 sDeleting = false;
374 sMoving = false;
375 }
376
377 if ( sMoving )
378 editActions.append(actionCollection()->action("rename"));
379
380 bool addTrash = false;
381 bool addDel = false;
382
383 bool isIntoTrash = _item.url().protocol() == "trash";
384
385 if ( sMoving && !isIntoTrash && item.isLocalFile() )
386 addTrash = true;
387
388 if ( sDeleting ) {
389 if ( !item.isLocalFile() )
390 addDel = true;
391 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
392 addTrash = false;
393 addDel = true;
394 }
395 else {
396 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
397 KConfigGroup configGroup(globalConfig, "KDE");
398 if ( configGroup.readEntry("ShowDeleteCommand", false) )
399 addDel = true;
400 }
401 }
402
403 if (addTrash)
404 editActions.append(actionCollection()->action("move_to_trash"));
405 if (addDel)
406 editActions.append(actionCollection()->action("delete"));
407
408 // Normally KonqPopupMenu only shows the "Create new" subdir in the current view
409 // since otherwise the created file would not be visible.
410 // But in treeview mode we should allow it.
411 if (m_view->itemsExpandable())
412 popupFlags |= KParts::BrowserExtension::ShowCreateDirectory;
413
414 }
415
416 actionGroups.insert("editactions", editActions);
417
418 // TODO: We should change the signature of the slots (and signals) for being able
419 // to tell for which items we want a popup.
420 KFileItemList items = (m_view->selectedItems().count() ? m_view->selectedItems()
421 : KFileItemList() << item);
422 emit m_extension->popupMenu(QCursor::pos(),
423 items,
424 KParts::OpenUrlArguments(),
425 KParts::BrowserArguments(),
426 popupFlags,
427 actionGroups);
428 }
429
430 void DolphinPart::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
431 {
432 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
433 if (oldUrl == url()) {
434 KParts::ReadOnlyPart::setUrl(newUrl);
435 const QString prettyUrl = newUrl.pathOrUrl();
436 emit m_extension->setLocationBarUrl(prettyUrl);
437 }
438 }
439
440 void DolphinPart::slotRequestUrlChange(const KUrl& url)
441 {
442 if (m_view->url() != url) {
443 // If the view URL is not equal to 'url', then an inner URL change has
444 // been done (e. g. by activating an existing column in the column view).
445 openUrl(url);
446 emit m_extension->openUrlNotify();
447 }
448 }
449
450 ////
451
452 void DolphinPartBrowserExtension::cut()
453 {
454 m_part->view()->cutSelectedItems();
455 }
456
457 void DolphinPartBrowserExtension::copy()
458 {
459 m_part->view()->copySelectedItems();
460 }
461
462 void DolphinPartBrowserExtension::paste()
463 {
464 m_part->view()->paste();
465 }
466
467 void DolphinPartBrowserExtension::reparseConfiguration()
468 {
469 m_part->view()->refresh();
470 }
471
472 ////
473
474 void DolphinPart::slotEditMimeType()
475 {
476 const KFileItemList items = m_view->selectedItems();
477 if (!items.isEmpty()) {
478 KonqOperations::editMimeType(items.first().mimetype(), m_view);
479 }
480 }
481
482 void DolphinPart::setCurrentViewMode(const QString& viewModeName)
483 {
484 QAction* action = actionCollection()->action(viewModeName);
485 Q_ASSERT(action);
486 action->trigger();
487 }
488
489 QString DolphinPart::currentViewMode() const
490 {
491 return m_actionHandler->currentViewModeActionName();
492 }
493
494 void DolphinPart::setNameFilter(const QString& nameFilter)
495 {
496 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
497 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
498 m_nameFilter = nameFilter;
499 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
500 }
501
502 void DolphinPart::slotOpenTerminal()
503 {
504 QString dir(QDir::homePath());
505
506 KUrl u(url());
507
508 // If the given directory is not local, it can still be the URL of an
509 // ioslave using UDS_LOCAL_PATH which to be converted first.
510 u = KIO::NetAccess::mostLocalUrl(u, widget());
511
512 //If the URL is local after the above conversion, set the directory.
513 if (u.isLocalFile()) {
514 dir = u.toLocalFile();
515 }
516
517 KToolInvocation::invokeTerminal(QString(), dir);
518 }
519
520 void DolphinPart::updateNewMenu()
521 {
522 // As requested by KNewMenu :
523 m_newMenu->slotCheckUpToDate();
524 // And set the files that the menu apply on :
525 m_newMenu->setPopupFiles(url());
526 }
527
528 void DolphinPart::updateStatusBar()
529 {
530 emit ReadOnlyPart::setStatusBarText(m_view->statusBarText());
531 }
532
533 void DolphinPart::updateProgress(int percent)
534 {
535 m_extension->loadingProgress(percent);
536 }
537
538 #include "dolphinpart.moc"