aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/webview.cpp
blob: 135f25cdc417a2a52b92730c50e591564859915a (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
/*
 * 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/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "webview.h"
#include "webpage.h"
#include "webprofile.h"
#include "webprofilemanager.h"
#include "webviewcontextmenu.h"
#include <QContextMenuEvent>
#include <QWebEngineHistoryItem>

WebView::WebView(QWidget *parent)
    : QWebEngineView(parent)
{
    // load status and progress
    connect(this, &QWebEngineView::loadStarted, this, [this]() {
        m_loaded = false;
    });
    connect(this, &QWebEngineView::loadFinished, this, [this]() {
        m_loaded = true;
    });

    // TODO for Qt 5.15, check for fix on QTBUG 65223
    connect(this, &QWebEngineView::loadProgress, this, [this](int progress) {
        if(progress == 100) {
            emit loadFinished(true);
        }
    });
}

WebView::WebView(WebProfile *profile, cb_createWindow_t cb, QWidget *parent)
    : WebView(parent)
{
    cb_createWindow = cb;
    Q_CHECK_PTR(profile);
    m_profile = profile;
    setPage(new WebPage(profile, this));
}

WebView::WebView(const Session::WebView &data, cb_createWindow_t cb, QWidget *parent)
    : WebView(parent)
{
    cb_createWindow = cb;
    WebProfileManager profileManager;

    auto *profile = profileManager.profile(data.profile);
    if(profile != nullptr) {
        setProfile(profile);
    }

    if(!data.url.isEmpty())
        load(QUrl::fromUserInput(data.url));
    else {
        QByteArray copy(data.history);
        QDataStream historyStream(&copy, QIODevice::ReadOnly);
        historyStream >> *history();
    }
}

void WebView::setProfile(WebProfile *profile)
{
    m_profile = profile;
    const auto url = this->url();
    setPage(new WebPage(profile, this));
    this->load(url);
}

Session::WebView WebView::serialize() const
{
    QByteArray historyData;
    QDataStream historyStream(&historyData, QIODevice::WriteOnly);
    historyStream << *history();

    return { profile()->getId(), QString(), historyData };
}

void WebView::search(const QString &term)
{
    const QString searchUrl = m_profile->search().arg(QString(QUrl::toPercentEncoding(term)));
    load(searchUrl);
}

void WebView::contextMenuEvent(QContextMenuEvent *event)
{
    auto *menu = new WebViewContextMenu(this);
    //const auto ctxdata = page()->contextMenuData();
    menu->exec(event->globalPos());
}