aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow/addressbar.cpp
blob: d37a6a749cb56ee0b9da7ddcff73fff9a9e613c1 (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
/*
 * 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 "addressbar.h"
#include "ui_addressbar.h"
#include <QShortcut>
#include "configuration.h"
#include "webengine/webview.h"

AddressBar::AddressBar(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::AddressBar)
{
    ui->setupUi(this);

    Configuration conf;
    ui->urlBar->menuAction->setShortcut(QKeySequence(conf.value<QString>("shortcuts.address.menu").value()));

    auto *focusShortcut = new QShortcut(QKeySequence(conf.value<QString>("shortcuts.address.focus").value()), parent);
    connect(focusShortcut, &QShortcut::activated, ui->urlBar, [this]() {
        ui->urlBar->setFocus();
        ui->urlBar->selectAll();
    });

    connect(ui->urlBar, &UrlLineEdit::textEdited, [this](const QString &text) {
        std::function<void(QStringList &)> callback = std::bind(&UrlLineEdit::updateCompleter, ui->urlBar, std::placeholders::_1);
        emit complete(text, callback);
    });

    connect(ui->urlBar, &UrlLineEdit::returnPressed, [this]() {
        const QUrl url = QUrl::fromUserInput(ui->urlBar->text());

        // check if url contains \w+:// (matches protocol://) or contains a '.' (matches site.domain)
        // this is because single words are valid URLs for QUrl (searchterm becomes http://searchterm)
        // check for protocol://site because \. wouldn't match it (localhost is a search term; http://localhost is an address)
        if(ui->urlBar->text().contains(QRegularExpression("\\w+://|\\.")) && url.isValid()) {
            emit load(url);
        } else {
            emit search(ui->urlBar->text());
        }
        ui->urlBar->clearFocus();
    });
}

void AddressBar::connectView(WebView *view)
{
    disconnect(url_connection);
    disconnect(progress_connection);
    disconnect(search_connection);
    disconnect(load_connection);
    disconnect(focus_connection);

    if(view == nullptr) {
        ui->urlBar->clear();
        ui->loadingBar->setValue(100);
    } else {
        ui->urlBar->setUrl(view->url());
        ui->loadingBar->setValue(100);
        url_connection = connect(view, &WebView::urlChanged, ui->urlBar, &UrlLineEdit::setUrl);
        progress_connection = connect(view, &WebView::loadProgress, ui->loadingBar, &QProgressBar::setValue);

        search_connection = connect(this, &AddressBar::search, view, &WebView::search);
        load_connection = connect(this, &AddressBar::load, view, qOverload<const QUrl &>(&WebView::load));
        focus_connection = connect(this, &AddressBar::giveFocus, view, qOverload<>(&WebView::setFocus));
    }
}