konqmainwindowfactory.cpp 3.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* This file is part of the KDE project
   Copyright (C) 1998, 1999, 2016 David Faure <faure@kde.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "konqmainwindowfactory.h"
#include "konqmainwindow.h"
22
#include "konqview.h"
23
#include "konqsessionmanager.h"
24
#include "konqsettingsxt.h"
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <KWindowInfo>
#include <KStartupInfo>

// Terminates fullscreen-mode for any full-screen window on the current desktop
static void abortFullScreenMode()
{
    QList<KonqMainWindow *> *mainWindows = KonqMainWindow::mainWindowList();
    if (mainWindows) {
        foreach (KonqMainWindow *window, *mainWindows) {
            if (window->fullScreenMode()) {
                KWindowInfo info(window->winId(), NET::WMDesktop);
                if (info.valid() && info.isOnCurrentDesktop()) {
                    window->setWindowState(window->windowState() & ~Qt::WindowFullScreen);
                }
            }
        }
    }
}

44 45 46 47 48 49 50 51
// Prepare another preloaded window for next time
static void ensurePreloadedWindow()
{
    if (KonqSettings::alwaysHavePreloaded()) {
        QTimer::singleShot(500, nullptr, []() { new KonqMainWindow(QUrl(QStringLiteral("about:blank"))); });
    }
}

52 53 54 55 56 57 58 59 60 61 62
KonqMainWindow *KonqMainWindowFactory::createEmptyWindow()
{
    abortFullScreenMode();
    // Ask the user to recover session if applicable
    if (KonqSessionManager::self()->askUserToRestoreAutosavedAbandonedSessions()) {
        QList<KonqMainWindow *> *mainWindowList = KonqMainWindow::mainWindowList();
        if (mainWindowList && !mainWindowList->isEmpty()) {
            return mainWindowList->first();
        } else { // This should never happen but just to be sure
            return new KonqMainWindow;
        }
63
    }
64 65 66 67 68

    // Let's see if we can reuse a preloaded window
    QList<KonqMainWindow *> *mainWindowList = KonqMainWindow::mainWindowList();
    if (mainWindowList) {
        for (KonqMainWindow *win : *mainWindowList) {
David Faure's avatar
David Faure committed
69
            if (win->isPreloaded()) {
70 71 72 73 74 75
                qDebug() << "Reusing preloaded window" << win;
                KStartupInfo::setWindowStartupId(win->winId(), KStartupInfo::startupId());
                ensurePreloadedWindow();
                return win;
            }
        }
76
    }
77
    ensurePreloadedWindow();
78 79
    return new KonqMainWindow;
}
80 81 82 83 84 85 86 87 88

KonqMainWindow *KonqMainWindowFactory::createNewWindow(const QUrl &url,
                                          const KonqOpenURLRequest &req)
{
    KonqMainWindow *mainWindow = KonqMainWindowFactory::createEmptyWindow();
    if (!url.isEmpty()) {
        mainWindow->openUrl(Q_NULLPTR, url, QString(), req);
        mainWindow->setInitialFrameName(req.browserArgs.frameName);
    } else {
89
        mainWindow->openUrl(Q_NULLPTR, QUrl(KonqSettings::startURL()));
90 91 92 93
        mainWindow->focusLocationBar();
    }
    return mainWindow;
}