1 /*
2 SPDX-FileCopyrightText: 2002 Wilco Greven <greven@kde.org>
3 SPDX-FileCopyrightText: 2002 Chris Cheney <ccheney@cheney.cx>
4 SPDX-FileCopyrightText: 2003 Benjamin Meyer <benjamin@csh.rit.edu>
5 SPDX-FileCopyrightText: 2003-2004 Christophe Devriese <Christophe.Devriese@student.kuleuven.ac.be>
6 SPDX-FileCopyrightText: 2003 Laurent Montel <montel@kde.org>
7 SPDX-FileCopyrightText: 2003-2004 Albert Astals Cid <aacid@kde.org>
8 SPDX-FileCopyrightText: 2003 Luboš Luňák <l.lunak@kde.org>
9 SPDX-FileCopyrightText: 2003 Malcolm Hunter <malcolm.hunter@gmx.co.uk>
10 SPDX-FileCopyrightText: 2004 Dominique Devriese <devriese@kde.org>
11 SPDX-FileCopyrightText: 2004 Dirk Mueller <mueller@kde.org>
12
13 Work sponsored by the LiMux project of the city of Munich:
14 SPDX-FileCopyrightText: 2017 Klarälvdalens Datakonsult AB a KDAB Group company <info@kdab.com>
15
16 SPDX-License-Identifier: GPL-2.0-or-later
17 */
18
19 #include "shell.h"
20
21 // qt/kde includes
22 #include <KActionCollection>
23 #include <KConfigGroup>
24 #include <KIO/Global>
25 #include <KLocalizedString>
26 #include <KMessageBox>
27 #include <KPluginFactory>
28 #include <KPluginLoader>
29 #include <KRecentFilesAction>
30 #include <KSharedConfig>
31 #include <KStandardAction>
32 #include <KToggleFullScreenAction>
33 #include <KToolBar>
34 #include <KUrlMimeData>
35 #include <KWindowSystem>
36 #include <KXMLGUIFactory>
37 #include <QApplication>
38 #include <QDBusConnection>
39 #include <QDragMoveEvent>
40 #include <QFileDialog>
41 #include <QMenuBar>
42 #include <QObject>
43 #include <QScreen>
44 #include <QTabBar>
45 #include <QTabWidget>
46 #include <QTimer>
47 #ifdef WITH_KACTIVITIES
48 #include <KActivities/ResourceInstance>
49 #endif
50
51 #include <kio_version.h>
52 #include <kxmlgui_version.h>
53
54 // local includes
55 #include "../interfaces/viewerinterface.h"
56 #include "kdocumentviewer.h"
57 #include "shellutils.h"
58
59 static const char *shouldShowMenuBarComingFromFullScreen = "shouldShowMenuBarComingFromFullScreen";
60 static const char *shouldShowToolBarComingFromFullScreen = "shouldShowToolBarComingFromFullScreen";
61
62 static const char *const SESSION_URL_KEY = "Urls";
63 static const char *const SESSION_TAB_KEY = "ActiveTab";
64
Shell(const QString & serializedOptions)65 Shell::Shell(const QString &serializedOptions)
66 : KParts::MainWindow()
67 , m_menuBarWasShown(true)
68 , m_toolBarWasShown(true)
69 #ifndef Q_OS_WIN
70 , m_activityResource(nullptr)
71 #endif
72 , m_isValid(true)
73 {
74 setObjectName(QStringLiteral("okular::Shell#"));
75 setContextMenuPolicy(Qt::NoContextMenu);
76 // otherwise .rc file won't be found by unit test
77 setComponentName(QStringLiteral("okular"), QString());
78 // set the shell's ui resource file
79 setXMLFile(QStringLiteral("shell.rc"));
80 m_fileformatsscanned = false;
81 m_showMenuBarAction = nullptr;
82 // this routine will find and load our Part. it finds the Part by
83 // name which is a bad idea usually.. but it's alright in this
84 // case since our Part is made for this Shell
85 KPluginLoader loader(QStringLiteral("okularpart"));
86 m_partFactory = loader.factory();
87 if (!m_partFactory) {
88 // if we couldn't find our Part, we exit since the Shell by
89 // itself can't do anything useful
90 m_isValid = false;
91 KMessageBox::error(this, i18n("Unable to find the Okular component: %1", loader.errorString()));
92 return;
93 }
94
95 // now that the Part plugin is loaded, create the part
96 KParts::ReadWritePart *const firstPart = m_partFactory->create<KParts::ReadWritePart>(this);
97 if (firstPart) {
98 // Setup tab bar
99 m_tabWidget = new QTabWidget(this);
100 m_tabWidget->setTabsClosable(true);
101 m_tabWidget->setElideMode(Qt::ElideRight);
102 m_tabWidget->tabBar()->hide();
103 m_tabWidget->setDocumentMode(true);
104 m_tabWidget->setMovable(true);
105
106 m_tabWidget->setAcceptDrops(true);
107 m_tabWidget->tabBar()->installEventFilter(this);
108
109 connect(m_tabWidget, &QTabWidget::currentChanged, this, &Shell::setActiveTab);
110 connect(m_tabWidget, &QTabWidget::tabCloseRequested, this, &Shell::closeTab);
111 connect(m_tabWidget->tabBar(), &QTabBar::tabMoved, this, &Shell::moveTabData);
112
113 setCentralWidget(m_tabWidget);
114
115 // then, setup our actions
116 setupActions();
117 connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, this, &QObject::deleteLater);
118 // and integrate the part's GUI with the shell's
119 setupGUI(Keys | ToolBar | Save);
120
121 m_tabs.append(TabState(firstPart));
122 m_tabWidget->addTab(firstPart->widget(), QString()); // triggers setActiveTab that calls createGUI( part )
123
124 connectPart(firstPart);
125
126 readSettings();
127
128 m_unique = ShellUtils::unique(serializedOptions);
129 if (m_unique) {
130 m_unique = QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.okular"));
131 if (!m_unique)
132 KMessageBox::information(this, i18n("There is already a unique Okular instance running. This instance won't be the unique one."));
133 } else {
134 QString serviceName = QStringLiteral("org.kde.okular-") + QString::number(qApp->applicationPid());
135 QDBusConnection::sessionBus().registerService(serviceName);
136 }
137 if (ShellUtils::noRaise(serializedOptions)) {
138 setAttribute(Qt::WA_ShowWithoutActivating);
139 }
140
141 QDBusConnection::sessionBus().registerObject(QStringLiteral("/okularshell"), this, QDBusConnection::ExportScriptableSlots);
142 } else {
143 m_isValid = false;
144 KMessageBox::error(this, i18n("Unable to find the Okular component."));
145 }
146
147 #if KXMLGUI_VERSION > QT_VERSION_CHECK(5, 78, 0)
148 connect(guiFactory(), &KXMLGUIFactory::shortcutsSaved, this, &Shell::reloadAllXML);
149 #endif
150 }
151
reloadAllXML()152 void Shell::reloadAllXML()
153 {
154 for (const TabState &tab : qAsConst(m_tabs)) {
155 tab.part->reloadXML();
156 }
157 }
158
keyPressEvent(QKeyEvent * e)159 void Shell::keyPressEvent(QKeyEvent *e)
160 {
161 if (e->key() == Qt::Key_Escape && window()->isFullScreen()) {
162 setFullScreen(false);
163 }
164 }
165
eventFilter(QObject * obj,QEvent * event)166 bool Shell::eventFilter(QObject *obj, QEvent *event)
167 {
168 QDragMoveEvent *dmEvent = dynamic_cast<QDragMoveEvent *>(event);
169 if (dmEvent) {
170 bool accept = dmEvent->mimeData()->hasUrls();
171 event->setAccepted(accept);
172 return accept;
173 }
174
175 QDropEvent *dEvent = dynamic_cast<QDropEvent *>(event);
176 if (dEvent) {
177 const QList<QUrl> list = KUrlMimeData::urlsFromMimeData(dEvent->mimeData());
178 handleDroppedUrls(list);
179 dEvent->setAccepted(true);
180 return true;
181 }
182
183 // Handle middle button click events on the tab bar
184 if (obj == m_tabWidget->tabBar() && event->type() == QEvent::MouseButtonRelease) {
185 QMouseEvent *mEvent = static_cast<QMouseEvent *>(event);
186 if (mEvent->button() == Qt::MiddleButton) {
187 int tabIndex = m_tabWidget->tabBar()->tabAt(mEvent->pos());
188 if (tabIndex != -1) {
189 closeTab(tabIndex);
190 return true;
191 }
192 }
193 }
194 return KParts::MainWindow::eventFilter(obj, event);
195 }
196
isValid() const197 bool Shell::isValid() const
198 {
199 return m_isValid;
200 }
201
showOpenRecentMenu()202 void Shell::showOpenRecentMenu()
203 {
204 m_recent->menu()->popup(QCursor::pos());
205 }
206
~Shell()207 Shell::~Shell()
208 {
209 if (!m_tabs.empty()) {
210 writeSettings();
211 for (const TabState &tab : qAsConst(m_tabs)) {
212 tab.part->closeUrl(false);
213 }
214 m_tabs.clear();
215 }
216 if (m_unique)
217 QDBusConnection::sessionBus().unregisterService(QStringLiteral("org.kde.okular"));
218
219 delete m_tabWidget;
220 }
221
222 // Open a new document if we have space for it
223 // This can hang if called on a unique instance and openUrl pops a messageBox
openDocument(const QUrl & url,const QString & serializedOptions)224 bool Shell::openDocument(const QUrl &url, const QString &serializedOptions)
225 {
226 if (m_tabs.size() <= 0)
227 return false;
228
229 KParts::ReadWritePart *const part = m_tabs[0].part;
230
231 // Return false if we can't open new tabs and the only part is occupied
232 if (!qobject_cast<Okular::ViewerInterface *>(part)->openNewFilesInTabs() && !part->url().isEmpty() && !ShellUtils::unique(serializedOptions)) {
233 return false;
234 }
235
236 openUrl(url, serializedOptions);
237
238 return true;
239 }
240
openDocument(const QString & urlString,const QString & serializedOptions)241 bool Shell::openDocument(const QString &urlString, const QString &serializedOptions)
242 {
243 return openDocument(QUrl(urlString), serializedOptions);
244 }
245
canOpenDocs(int numDocs,int desktop)246 bool Shell::canOpenDocs(int numDocs, int desktop)
247 {
248 if (m_tabs.size() <= 0 || numDocs <= 0 || m_unique)
249 return false;
250
251 KParts::ReadWritePart *const part = m_tabs[0].part;
252 const bool allowTabs = qobject_cast<Okular::ViewerInterface *>(part)->openNewFilesInTabs();
253
254 if (!allowTabs && (numDocs > 1 || !part->url().isEmpty()))
255 return false;
256
257 const KWindowInfo winfo(window()->effectiveWinId(), KWindowSystem::WMDesktop);
258 if (winfo.desktop() != desktop)
259 return false;
260
261 return true;
262 }
263
openUrl(const QUrl & url,const QString & serializedOptions)264 void Shell::openUrl(const QUrl &url, const QString &serializedOptions)
265 {
266 const int activeTab = m_tabWidget->currentIndex();
267 if (activeTab < m_tabs.size()) {
268 KParts::ReadWritePart *const activePart = m_tabs[activeTab].part;
269 if (!activePart->url().isEmpty()) {
270 if (m_unique) {
271 applyOptionsToPart(activePart, serializedOptions);
272 activePart->openUrl(url);
273 } else {
274 if (qobject_cast<Okular::ViewerInterface *>(activePart)->openNewFilesInTabs()) {
275 openNewTab(url, serializedOptions);
276 } else {
277 Shell *newShell = new Shell(serializedOptions);
278 newShell->show();
279 newShell->openUrl(url, serializedOptions);
280 }
281 }
282 } else {
283 m_tabWidget->setTabText(activeTab, url.fileName());
284 applyOptionsToPart(activePart, serializedOptions);
285 bool openOk = activePart->openUrl(url);
286 const bool isstdin = url.fileName() == QLatin1String("-") || url.scheme() == QLatin1String("fd");
287 if (!isstdin) {
288 if (openOk) {
289 #ifdef WITH_KACTIVITIES
290 if (!m_activityResource)
291 m_activityResource = new KActivities::ResourceInstance(window()->winId(), this);
292
293 m_activityResource->setUri(url);
294 #endif
295 m_recent->addUrl(url);
296 } else {
297 m_recent->removeUrl(url);
298 closeTab(activeTab);
299 }
300 }
301 }
302 }
303 }
304
closeUrl()305 void Shell::closeUrl()
306 {
307 closeTab(m_tabWidget->currentIndex());
308
309 // When closing the current tab two things can happen:
310 // * the focus was on the tab
311 // * the focus was somewhere in the toolbar
312 // we don't have other places that accept focus
313 // * If it was on the tab, logic says it should go back to the next current tab
314 // * If it was on the toolbar, we could leave it there, but since we redo the menus/toolbars for the new tab, it gets kind of lost
315 // so it's easier to set it to the next current tab which also makes sense as consistency
316 if (m_tabWidget->count() >= 0) {
317 KParts::ReadWritePart *const newPart = m_tabs[m_tabWidget->currentIndex()].part;
318 newPart->widget()->setFocus();
319 }
320 }
321
readSettings()322 void Shell::readSettings()
323 {
324 m_recent->loadEntries(KSharedConfig::openConfig()->group("Recent Files"));
325 m_recent->setEnabled(true); // force enabling
326
327 const KConfigGroup group = KSharedConfig::openConfig()->group("Desktop Entry");
328 bool fullScreen = group.readEntry("FullScreen", false);
329 setFullScreen(fullScreen);
330
331 if (fullScreen) {
332 m_menuBarWasShown = group.readEntry(shouldShowMenuBarComingFromFullScreen, true);
333 m_toolBarWasShown = group.readEntry(shouldShowToolBarComingFromFullScreen, true);
334 }
335 }
336
writeSettings()337 void Shell::writeSettings()
338 {
339 m_recent->saveEntries(KSharedConfig::openConfig()->group("Recent Files"));
340 KConfigGroup group = KSharedConfig::openConfig()->group("Desktop Entry");
341 group.writeEntry("FullScreen", m_fullScreenAction->isChecked());
342 if (m_fullScreenAction->isChecked()) {
343 group.writeEntry(shouldShowMenuBarComingFromFullScreen, m_menuBarWasShown);
344 group.writeEntry(shouldShowToolBarComingFromFullScreen, m_toolBarWasShown);
345 }
346 KSharedConfig::openConfig()->sync();
347 }
348
setupActions()349 void Shell::setupActions()
350 {
351 KStandardAction::open(this, SLOT(fileOpen()), actionCollection());
352 m_recent = KStandardAction::openRecent(this, SLOT(openUrl(QUrl)), actionCollection());
353 m_recent->setToolBarMode(KRecentFilesAction::MenuMode);
354 connect(m_recent, &QAction::triggered, this, &Shell::showOpenRecentMenu);
355 m_recent->setToolTip(i18n("Click to open a file\nClick and hold to open a recent file"));
356 m_recent->setWhatsThis(i18n("<b>Click</b> to open a file or <b>Click and hold</b> to select a recent file"));
357 m_printAction = KStandardAction::print(this, SLOT(print()), actionCollection());
358 m_printAction->setEnabled(false);
359 m_closeAction = KStandardAction::close(this, SLOT(closeUrl()), actionCollection());
360 m_closeAction->setEnabled(false);
361 KStandardAction::quit(this, SLOT(close()), actionCollection());
362
363 setStandardToolBarMenuEnabled(true);
364
365 m_showMenuBarAction = KStandardAction::showMenubar(this, SLOT(slotShowMenubar()), actionCollection());
366 m_fullScreenAction = KStandardAction::fullScreen(this, SLOT(slotUpdateFullScreen()), this, actionCollection());
367
368 m_nextTabAction = actionCollection()->addAction(QStringLiteral("tab-next"));
369 m_nextTabAction->setText(i18n("Next Tab"));
370 actionCollection()->setDefaultShortcuts(m_nextTabAction, KStandardShortcut::tabNext());
371 m_nextTabAction->setEnabled(false);
372 connect(m_nextTabAction, &QAction::triggered, this, &Shell::activateNextTab);
373
374 m_prevTabAction = actionCollection()->addAction(QStringLiteral("tab-previous"));
375 m_prevTabAction->setText(i18n("Previous Tab"));
376 actionCollection()->setDefaultShortcuts(m_prevTabAction, KStandardShortcut::tabPrev());
377 m_prevTabAction->setEnabled(false);
378 connect(m_prevTabAction, &QAction::triggered, this, &Shell::activatePrevTab);
379
380 m_undoCloseTab = actionCollection()->addAction(QStringLiteral("undo-close-tab"));
381 m_undoCloseTab->setText(i18n("Undo close tab"));
382 actionCollection()->setDefaultShortcut(m_undoCloseTab, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_T));
383 m_undoCloseTab->setIcon(QIcon::fromTheme(QStringLiteral("edit-undo")));
384 m_undoCloseTab->setEnabled(false);
385 connect(m_undoCloseTab, &QAction::triggered, this, &Shell::undoCloseTab);
386 }
387
saveProperties(KConfigGroup & group)388 void Shell::saveProperties(KConfigGroup &group)
389 {
390 if (!m_isValid) // part couldn't be loaded, nothing to save
391 return;
392
393 // Gather lists of settings to preserve
394 QStringList urls;
395 for (const TabState &tab : qAsConst(m_tabs)) {
396 urls.append(tab.part->url().url());
397 }
398 group.writePathEntry(SESSION_URL_KEY, urls);
399 group.writeEntry(SESSION_TAB_KEY, m_tabWidget->currentIndex());
400 }
401
readProperties(const KConfigGroup & group)402 void Shell::readProperties(const KConfigGroup &group)
403 {
404 // Reopen documents based on saved settings
405 QStringList urls = group.readPathEntry(SESSION_URL_KEY, QStringList());
406 int desiredTab = group.readEntry<int>(SESSION_TAB_KEY, 0);
407
408 while (!urls.isEmpty()) {
409 openUrl(QUrl(urls.takeFirst()));
410 }
411
412 if (desiredTab < m_tabs.size()) {
413 setActiveTab(desiredTab);
414 }
415 }
416
fileOpen()417 void Shell::fileOpen()
418 {
419 // this slot is called whenever the File->Open menu is selected,
420 // the Open shortcut is pressed (usually CTRL+O) or the Open toolbar
421 // button is clicked
422 const int activeTab = m_tabWidget->currentIndex();
423 if (!m_fileformatsscanned) {
424 const KDocumentViewer *const doc = qobject_cast<KDocumentViewer *>(m_tabs[activeTab].part);
425 Q_ASSERT(doc);
426
427 m_fileformats = doc->supportedMimeTypes();
428
429 m_fileformatsscanned = true;
430 }
431
432 QUrl startDir;
433 const KParts::ReadWritePart *const curPart = m_tabs[activeTab].part;
434 if (curPart->url().isLocalFile())
435 startDir = KIO::upUrl(curPart->url());
436
437 QPointer<QFileDialog> dlg(new QFileDialog(this));
438 dlg->setDirectoryUrl(startDir);
439 dlg->setAcceptMode(QFileDialog::AcceptOpen);
440 dlg->setOption(QFileDialog::HideNameFilterDetails, true);
441 dlg->setFileMode(QFileDialog::ExistingFiles); // Allow selection of more than one file
442
443 QMimeDatabase mimeDatabase;
444 // Unfortunately non Plasma file dialogs don't support the "All supported files" when using
445 // setMimeTypeFilters instead of setNameFilters, so for those use setNameFilters which is a bit
446 // worse because doesn't show you pdf files named bla.blo when you say "show me the pdf files", but
447 // that's solvable by choosing "All Files" and it's not that common while it's more convenient to
448 // only get shown the files that the application can open by default instead of all of them
449 #if KIO_VERSION >= QT_VERSION_CHECK(5, 73, 0)
450 const bool useMimeTypeFilters = qgetenv("XDG_CURRENT_DESKTOP").toLower() == QStringLiteral("kde");
451 #else
452 const bool useMimeTypeFilters = false;
453 #endif
454 if (useMimeTypeFilters) {
455 QStringList mimetypes;
456 for (const QString &mimeName : qAsConst(m_fileformats)) {
457 QMimeType mimeType = mimeDatabase.mimeTypeForName(mimeName);
458 mimetypes << mimeType.name();
459 }
460 mimetypes.prepend(QStringLiteral("application/octet-stream"));
461 dlg->setMimeTypeFilters(mimetypes);
462 } else {
463 QSet<QString> globPatterns;
464 QMap<QString, QStringList> namedGlobs;
465 for (const QString &mimeName : qAsConst(m_fileformats)) {
466 QMimeType mimeType = mimeDatabase.mimeTypeForName(mimeName);
467 const QStringList globs(mimeType.globPatterns());
468 if (globs.isEmpty()) {
469 continue;
470 }
471
472 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
473 globPatterns.unite(QSet<QString>(globs.begin(), globs.end()));
474 #else
475 globPatterns.unite(globs.toSet());
476 #endif
477
478 namedGlobs[mimeType.comment()].append(globs);
479 }
480 QStringList namePatterns;
481 for (auto it = namedGlobs.cbegin(); it != namedGlobs.cend(); ++it) {
482 namePatterns.append(it.key() + QLatin1String(" (") + it.value().join(QLatin1Char(' ')) + QLatin1Char(')'));
483 }
484
485 const QStringList allGlobPatterns = globPatterns.values();
486 namePatterns.prepend(i18n("All files (*)"));
487 namePatterns.prepend(i18n("All supported files (%1)", allGlobPatterns.join(QLatin1Char(' '))));
488 dlg->setNameFilters(namePatterns);
489 }
490
491 dlg->setWindowTitle(i18n("Open Document"));
492 if (dlg->exec() && dlg) {
493 const QList<QUrl> urlList = dlg->selectedUrls();
494 for (const QUrl &url : urlList) {
495 openUrl(url);
496 }
497 }
498
499 if (dlg) {
500 delete dlg.data();
501 }
502 }
503
tryRaise()504 void Shell::tryRaise()
505 {
506 KWindowSystem::forceActiveWindow(window()->effectiveWinId());
507 }
508
509 // only called when starting the program
setFullScreen(bool useFullScreen)510 void Shell::setFullScreen(bool useFullScreen)
511 {
512 if (useFullScreen)
513 setWindowState(windowState() | Qt::WindowFullScreen); // set
514 else
515 setWindowState(windowState() & ~Qt::WindowFullScreen); // reset
516 }
517
setCaption(const QString & caption)518 void Shell::setCaption(const QString &caption)
519 {
520 bool modified = false;
521
522 const int activeTab = m_tabWidget->currentIndex();
523 if (activeTab >= 0 && activeTab < m_tabs.size()) {
524 KParts::ReadWritePart *const activePart = m_tabs[activeTab].part;
525 QString tabCaption = activePart->url().fileName();
526 if (activePart->isModified()) {
527 modified = true;
528 if (!tabCaption.isEmpty()) {
529 tabCaption.append(QStringLiteral(" *"));
530 }
531 }
532
533 m_tabWidget->setTabText(activeTab, tabCaption);
534 }
535
536 setCaption(caption, modified);
537 }
538
showEvent(QShowEvent * e)539 void Shell::showEvent(QShowEvent *e)
540 {
541 if (!menuBar()->isNativeMenuBar() && m_showMenuBarAction)
542 m_showMenuBarAction->setChecked(menuBar()->isVisible());
543
544 KParts::MainWindow::showEvent(e);
545 }
546
slotUpdateFullScreen()547 void Shell::slotUpdateFullScreen()
548 {
549 if (m_fullScreenAction->isChecked()) {
550 m_menuBarWasShown = !menuBar()->isHidden();
551 menuBar()->hide();
552
553 m_toolBarWasShown = !toolBar()->isHidden();
554 toolBar()->hide();
555
556 KToggleFullScreenAction::setFullScreen(this, true);
557 } else {
558 if (m_menuBarWasShown) {
559 menuBar()->show();
560 }
561 if (m_toolBarWasShown) {
562 toolBar()->show();
563 }
564 KToggleFullScreenAction::setFullScreen(this, false);
565 }
566 }
567
slotShowMenubar()568 void Shell::slotShowMenubar()
569 {
570 if (menuBar()->isHidden())
571 menuBar()->show();
572 else
573 menuBar()->hide();
574 }
575
sizeHint() const576 QSize Shell::sizeHint() const
577 {
578 const QSize baseSize = QApplication::primaryScreen()->availableSize() * 0.6;
579 // Set an arbitrary yet sensible sane minimum size for very small screens;
580 // for example we don't want people using 1366x768 screens to get a tiny
581 // default window size of 820 x 460 which will elide most of the toolbar buttons.
582 return baseSize.expandedTo(QSize(1000, 700));
583 }
584
queryClose()585 bool Shell::queryClose()
586 {
587 if (m_tabs.count() > 1) {
588 const QString dontAskAgainName = QStringLiteral("ShowTabWarning");
589 KMessageBox::ButtonCode dummy = KMessageBox::Yes;
590 if (shouldBeShownYesNo(dontAskAgainName, dummy)) {
591 QDialog *dialog = new QDialog(this);
592 dialog->setWindowTitle(i18n("Confirm Close"));
593
594 QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog);
595 buttonBox->setStandardButtons(QDialogButtonBox::Yes | QDialogButtonBox::No);
596 KGuiItem::assign(buttonBox->button(QDialogButtonBox::Yes), KGuiItem(i18n("Close Tabs"), QStringLiteral("tab-close")));
597 KGuiItem::assign(buttonBox->button(QDialogButtonBox::No), KStandardGuiItem::cancel());
598
599 bool checkboxResult = true;
600 const int result = KMessageBox::createKMessageBox(dialog,
601 buttonBox,
602 QMessageBox::Question,
603 i18n("You are about to close %1 tabs. Are you sure you want to continue?", m_tabs.count()),
604 QStringList(),
605 i18n("Warn me when I attempt to close multiple tabs"),
606 &checkboxResult,
607 KMessageBox::Notify);
608
609 if (!checkboxResult) {
610 saveDontShowAgainYesNo(dontAskAgainName, dummy);
611 }
612
613 if (result != QDialogButtonBox::Yes) {
614 return false;
615 }
616 }
617 }
618
619 for (int i = 0; i < m_tabs.size(); ++i) {
620 KParts::ReadWritePart *const part = m_tabs[i].part;
621
622 // To resolve confusion about multiple modified docs, switch to relevant tab
623 if (part->isModified())
624 setActiveTab(i);
625
626 if (!part->queryClose())
627 return false;
628 }
629 return true;
630 }
631
setActiveTab(int tab)632 void Shell::setActiveTab(int tab)
633 {
634 m_tabWidget->setCurrentIndex(tab);
635 #if KXMLGUI_VERSION <= QT_VERSION_CHECK(5, 78, 0)
636 m_tabs[tab].part->reloadXML();
637 #endif
638 createGUI(m_tabs[tab].part);
639
640 m_printAction->setEnabled(m_tabs[tab].printEnabled);
641 m_closeAction->setEnabled(m_tabs[tab].closeEnabled);
642 }
643
closeTab(int tab)644 void Shell::closeTab(int tab)
645 {
646 KParts::ReadWritePart *const part = m_tabs[tab].part;
647 QUrl url = part->url();
648 if (part->closeUrl() && m_tabs.count() > 1) {
649 if (part->factory())
650 part->factory()->removeClient(part);
651 part->disconnect();
652 part->deleteLater();
653 m_tabs.removeAt(tab);
654 m_tabWidget->removeTab(tab);
655 m_undoCloseTab->setEnabled(true);
656 m_closedTabUrls.append(url);
657
658 if (m_tabWidget->count() == 1) {
659 m_tabWidget->tabBar()->hide();
660 m_nextTabAction->setEnabled(false);
661 m_prevTabAction->setEnabled(false);
662 }
663 }
664 }
665
openNewTab(const QUrl & url,const QString & serializedOptions)666 void Shell::openNewTab(const QUrl &url, const QString &serializedOptions)
667 {
668 const int previousActiveTab = m_tabWidget->currentIndex();
669 KParts::ReadWritePart *const activePart = m_tabs[previousActiveTab].part;
670
671 bool activateTabIfAlreadyOpen;
672 QMetaObject::invokeMethod(activePart, "activateTabIfAlreadyOpenFile", Q_RETURN_ARG(bool, activateTabIfAlreadyOpen));
673
674 if (activateTabIfAlreadyOpen) {
675 const int tabIndex = findTabIndex(url);
676
677 if (tabIndex >= 0) {
678 setActiveTab(tabIndex);
679 m_recent->addUrl(url);
680 return;
681 }
682 }
683
684 // Tabs are hidden when there's only one, so show it
685 if (m_tabs.size() == 1) {
686 m_tabWidget->tabBar()->show();
687 m_nextTabAction->setEnabled(true);
688 m_prevTabAction->setEnabled(true);
689 }
690
691 const int newIndex = m_tabs.size();
692
693 // Make new part
694 m_tabs.append(TabState(m_partFactory->create<KParts::ReadWritePart>(this)));
695 connectPart(m_tabs[newIndex].part);
696
697 // Update GUI
698 KParts::ReadWritePart *const part = m_tabs[newIndex].part;
699 m_tabWidget->addTab(part->widget(), url.fileName());
700
701 applyOptionsToPart(part, serializedOptions);
702
703 setActiveTab(m_tabs.size() - 1);
704
705 if (part->openUrl(url)) {
706 m_recent->addUrl(url);
707 } else {
708 setActiveTab(previousActiveTab);
709 closeTab(m_tabs.size() - 1);
710 m_recent->removeUrl(url);
711 }
712 }
713
applyOptionsToPart(QObject * part,const QString & serializedOptions)714 void Shell::applyOptionsToPart(QObject *part, const QString &serializedOptions)
715 {
716 KDocumentViewer *const doc = qobject_cast<KDocumentViewer *>(part);
717 const QString find = ShellUtils::find(serializedOptions);
718 if (ShellUtils::startInPresentation(serializedOptions))
719 doc->startPresentation();
720 if (ShellUtils::showPrintDialog(serializedOptions))
721 QMetaObject::invokeMethod(part, "enableStartWithPrint");
722 if (ShellUtils::showPrintDialogAndExit(serializedOptions))
723 QMetaObject::invokeMethod(part, "enableExitAfterPrint");
724 if (!find.isEmpty())
725 QMetaObject::invokeMethod(part, "enableStartWithFind", Q_ARG(QString, find));
726 }
727
connectPart(QObject * part)728 void Shell::connectPart(QObject *part)
729 {
730 // We're abusing the fact we know the part is our part here
731 connect(this, SIGNAL(moveSplitter(int)), part, SLOT(moveSplitter(int))); // clazy:exclude=old-style-connect
732 connect(part, SIGNAL(enablePrintAction(bool)), this, SLOT(setPrintEnabled(bool))); // clazy:exclude=old-style-connect
733 connect(part, SIGNAL(enableCloseAction(bool)), this, SLOT(setCloseEnabled(bool))); // clazy:exclude=old-style-connect
734 connect(part, SIGNAL(mimeTypeChanged(QMimeType)), this, SLOT(setTabIcon(QMimeType))); // clazy:exclude=old-style-connect
735 connect(part, SIGNAL(urlsDropped(QList<QUrl>)), this, SLOT(handleDroppedUrls(QList<QUrl>))); // clazy:exclude=old-style-connect
736 // clang-format off
737 // Otherwise the QSize,QSize gets turned into QSize, QSize that is not normalized signals and is slightly slower
738 connect(part, SIGNAL(fitWindowToPage(QSize,QSize)), this, SLOT(slotFitWindowToPage(QSize,QSize))); // clazy:exclude=old-style-connect
739 // clang-format on
740 }
741
print()742 void Shell::print()
743 {
744 QMetaObject::invokeMethod(m_tabs[m_tabWidget->currentIndex()].part, "slotPrint");
745 }
746
setPrintEnabled(bool enabled)747 void Shell::setPrintEnabled(bool enabled)
748 {
749 int i = findTabIndex(sender());
750 if (i != -1) {
751 m_tabs[i].printEnabled = enabled;
752 if (i == m_tabWidget->currentIndex())
753 m_printAction->setEnabled(enabled);
754 }
755 }
756
setCloseEnabled(bool enabled)757 void Shell::setCloseEnabled(bool enabled)
758 {
759 int i = findTabIndex(sender());
760 if (i != -1) {
761 m_tabs[i].closeEnabled = enabled;
762 if (i == m_tabWidget->currentIndex())
763 m_closeAction->setEnabled(enabled);
764 }
765 }
766
activateNextTab()767 void Shell::activateNextTab()
768 {
769 if (m_tabs.size() < 2)
770 return;
771
772 const int activeTab = m_tabWidget->currentIndex();
773 const int nextTab = (activeTab == m_tabs.size() - 1) ? 0 : activeTab + 1;
774
775 setActiveTab(nextTab);
776 }
777
activatePrevTab()778 void Shell::activatePrevTab()
779 {
780 if (m_tabs.size() < 2)
781 return;
782
783 const int activeTab = m_tabWidget->currentIndex();
784 const int prevTab = (activeTab == 0) ? m_tabs.size() - 1 : activeTab - 1;
785
786 setActiveTab(prevTab);
787 }
788
undoCloseTab()789 void Shell::undoCloseTab()
790 {
791 if (m_closedTabUrls.isEmpty()) {
792 return;
793 }
794
795 const QUrl lastTabUrl = m_closedTabUrls.takeLast();
796
797 if (m_closedTabUrls.isEmpty()) {
798 m_undoCloseTab->setEnabled(false);
799 }
800
801 openUrl(lastTabUrl);
802 }
803
setTabIcon(const QMimeType & mimeType)804 void Shell::setTabIcon(const QMimeType &mimeType)
805 {
806 int i = findTabIndex(sender());
807 if (i != -1) {
808 m_tabWidget->setTabIcon(i, QIcon::fromTheme(mimeType.iconName()));
809 }
810 }
811
findTabIndex(QObject * sender) const812 int Shell::findTabIndex(QObject *sender) const
813 {
814 for (int i = 0; i < m_tabs.size(); ++i) {
815 if (m_tabs[i].part == sender) {
816 return i;
817 }
818 }
819 return -1;
820 }
821
findTabIndex(const QUrl & url) const822 int Shell::findTabIndex(const QUrl &url) const
823 {
824 auto it = std::find_if(m_tabs.begin(), m_tabs.end(), [&url](const TabState state) { return state.part->url() == url; });
825 return (it != m_tabs.end()) ? std::distance(m_tabs.begin(), it) : -1;
826 }
827
handleDroppedUrls(const QList<QUrl> & urls)828 void Shell::handleDroppedUrls(const QList<QUrl> &urls)
829 {
830 for (const QUrl &url : urls) {
831 openUrl(url);
832 }
833 }
834
moveTabData(int from,int to)835 void Shell::moveTabData(int from, int to)
836 {
837 m_tabs.move(from, to);
838 }
839
slotFitWindowToPage(const QSize pageViewSize,const QSize pageSize)840 void Shell::slotFitWindowToPage(const QSize pageViewSize, const QSize pageSize)
841 {
842 const int xOffset = pageViewSize.width() - pageSize.width();
843 const int yOffset = pageViewSize.height() - pageSize.height();
844 showNormal();
845 resize(width() - xOffset, height() - yOffset);
846 emit moveSplitter(pageSize.width());
847 }
848
849 /* kate: replace-tabs on; indent-width 4; */
850