aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow/subwindow.cpp
blob: 497e6d01f36281931c2f8c5b1c7489a0ad05fbc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/smolbote.hg
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "subwindow.h"
#include "browser.h"
#include "webengine/webprofile.h"
#include "webengine/webview.h"
#include "widgets/tabwidget.h"
#include <QAction>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMenu>
#include <QShortcut>
#include <QStyle>
#include <QToolButton>

SubWindow::SubWindow(const QHash<QString, QString> &config, QWidget *parent, Qt::WindowFlags flags)
    : QMdiSubWindow(parent, flags)
    , tabWidget(new TabWidget(this))
{
    // delete this window when it closes
    setAttribute(Qt::WA_DeleteOnClose, true);

    resize(800, 600);
    setWidget(tabWidget);

    profile = WebProfile::defaultProfile();

    // system menu
    {
        auto *menu = systemMenu();
        menu->addSeparator();

        auto *profileName_action = menu->addAction(tr("Profile: %1").arg(profile->name()));
        profileName_action->setEnabled(false);
        auto *loadProfile_menu = menu->addMenu(tr("Load profile"));

        Browser *browser = qobject_cast<Browser *>(qApp);
        Q_CHECK_PTR(browser);

        for(const QString &name : browser->profiles()) {
            auto *loadAction = loadProfile_menu->addAction(name);
            connect(loadAction, &QAction::triggered, this, [name, browser, profileName_action, this]() {
                auto *profile = browser->profile(name);
                this->setProfile(profile);
                profileName_action->setText(tr("Profile: %1").arg(name));
            });
        }

#ifdef QT_DEBUG
        menu->addSeparator();
        menu->addAction(tr("Debug menu"))->setEnabled(false);
        auto *saveSession_action = menu->addAction(tr("Save session"));
        menu->addAction(tr("Load session"))->setEnabled(false);
        setSystemMenu(menu);

        connect(saveSession_action, &QAction::triggered, [this]() {
            QJsonDocument doc(session());
            qDebug("%s", qUtf8Printable(doc.toJson()));
        });
#endif
    }

    // new tab button
    auto *newTab_button = new QToolButton(this);
    newTab_button->setIcon(style()->standardIcon(QStyle::SP_FileIcon));
    newTab_button->setToolTip(tr("Add tab"));
    newTab_button->setShortcut(QKeySequence(config.value("window.shortcuts.new")));
    connect(newTab_button, &QToolButton::clicked, this, [=]() {
        auto index = addTab(WebProfile::defaultProfile()->newtab());
        tabWidget->setCurrentIndex(index);
    });
    tabWidget->setCornerWidget(newTab_button, Qt::TopRightCorner);

    // general actions
    auto *closeTab_shortcut = new QShortcut(QKeySequence(config.value("window.shortcuts.close")), this);
    connect(closeTab_shortcut, &QShortcut::activated, this, [=]() {
        tabWidget->deleteTab(tabWidget->currentIndex());
    });

    auto *leftTab_shortcut = new QShortcut(QKeySequence(config.value("window.shortcuts.left")), this);
    connect(leftTab_shortcut, &QShortcut::activated, this, [=]() {
        tabWidget->setCurrentIndex(qMax(0, tabWidget->currentIndex() - 1));
    });

    auto *rightTab_shortcut = new QShortcut(QKeySequence(config.value("window.shortcuts.right")), this);
    connect(rightTab_shortcut, &QShortcut::activated, this, [=]() {
        tabWidget->setCurrentIndex(qMin(tabWidget->currentIndex() + 1, tabWidget->count() - 1));
    });

    connect(tabWidget, &TabWidget::currentChanged, [this](int index) {
        if(index < 0) {
            // last tab has been closed
            close();
        } else {
            auto *view = dynamic_cast<WebView *>(tabWidget->widget(index));
            Q_CHECK_PTR(view);

            disconnect(titleConnection);
            disconnect(linkHoveredConnection);

            titleConnection = connect(view, &WebView::titleChanged, this, [this](const QString &title) {
                auto *v = qobject_cast<WebView *>(sender());
                this->setWindowTitle(QString("%1 :%2").arg(title, v->profile()->name()));
            });
            setWindowTitle(QString("%1 :%2").arg(view->title(), view->profile()->name()));

            linkHoveredConnection = connect(view->page(), &WebPage::linkHovered, this, [this](const QString &url) {
                if(!url.isEmpty())
                    emit showStatusMessage(url, 3000);
            });

            emit currentViewChanged(view);
        }
    });
}

SubWindow::~SubWindow()
{
    delete tabWidget;
}

WebView *SubWindow::currentView()
{
    return qobject_cast<WebView *>(tabWidget->currentWidget());
}

WebView *SubWindow::view(int index) const
{
    return qobject_cast<WebView *>(tabWidget->widget(index));
}

void SubWindow::setProfile(WebProfile *profile)
{
    Q_CHECK_PTR(profile);
    this->profile = profile;
    for(int i = 0; i < tabWidget->count(); ++i) {
        auto *view = qobject_cast<WebView *>(tabWidget->widget(i));
        view->setProfile(profile);
    }
}

int SubWindow::addTab(const QUrl &url)
{
    auto *view = new WebView(profile, this);
    if(!url.isEmpty())
        view->load(url);
    return tabWidget->addTab(view);
}

void SubWindow::setCurrentTab(int index)
{
    tabWidget->setCurrentIndex(index);
}

QJsonObject SubWindow::session() const
{
    QJsonObject obj;
    obj.insert("profile", QJsonValue(""));

    QJsonArray tabs;
    for(int i = 0; i < tabWidget->count(); ++i) {
        auto *view = qobject_cast<WebView *>(tabWidget->widget(i));
        if(view) {
            tabs.append(view->url().toString());
        }
    }
    obj.insert("tabs", tabs);

    return obj;
}

void SubWindow::restoreSession(const QJsonObject &sessionData)
{
    auto *browser = qobject_cast<Browser *>(qApp);
    Q_CHECK_PTR(browser);

    Q_ASSERT_X(sessionData.value("profile") != QJsonValue::Undefined, "Window::restoreSession", "no profile in json");
    if(browser->profiles().contains(sessionData.value("profile").toString()))
        profile = browser->profile(sessionData.value("profile").toString());

    Q_ASSERT_X(sessionData.value("tabs") != QJsonValue::Undefined, "Window::restoreSession", "no tabs in json");
    const QJsonArray tabs = sessionData.value("tabs").toArray();

    if(tabs.count() == 0) {
        // open a newtab
        auto *view = new WebView(profile, this);
        view->load(profile->newtab());
        tabWidget->addTab(view);
        return;
    }

    for(const auto tab : tabs) {
        auto *view = new WebView(profile, this);
        view->load(QUrl::fromUserInput(tab.toString()));
        tabWidget->addTab(view);
    }
}