aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow.cpp
blob: 6870a655a694c053f66def1e1f469a40f713c788 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/** LICENSE ********************************************************************
 **
 ** smolbote: yet another qute browser
 ** Copyright (C) 2017  Xian Nox
 **
 ** 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 3 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.  If not, see <http://www.gnu.org/licenses/>.
 **
 ******************************************************************************/

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMenu>
#include <QMenuBar>
#include <QCloseEvent>
#include <QMessageBox>
#include "browser.h"
#include "forms/profiledialog.h"
#include <QApplication>
#include <QInputDialog>
#include <QWebEngineDownloadItem>
#include <QStatusBar>

MainWindow::MainWindow(QUrl defaultUrl, QWidget *parent) :
    QMainWindow(parent),
    blocklistManager(new BlockerDialog(this)),
    ui(new Ui::MainWindow),
    navigationToolBar(new QToolBar(tr("Navigation"), this)),
    tabToolBar(new QToolBar(tr("Tab bar"), this)),
    tabBar(new WebViewTabBar(this)),
    urlLineEdit(new UrlLineEdit(navigationToolBar)),
    progressBar(new LoadingBar(this))
{
    // Load profile and connect its signals
    loadProfile(sSettings->value("general.profile").toString());

    ui->setupUi(this);
    resize(sSettings->value("window.width", 800).toInt(), sSettings->value("window.height", 600).toInt());

    // Browser menu
    QMenu *browserMenu = new QMenu(qApp->applicationName(), ui->menuBar);
    ui->menuBar->addMenu(browserMenu);
    browserMenu->addAction(tr("New Window"), this, SLOT(handleNewWindow()), QKeySequence(tr("Ctrl+N")));
    browserMenu->addAction(tr("New Tab"), this, SLOT(addNewTab()), QKeySequence(tr("Ctrl+T")));
    browserMenu->addSeparator();
    browserMenu->addAction(tr("About"), this, SLOT(about()), QKeySequence(tr("F1")));
    browserMenu->addAction(tr("About Qt"), qApp, SLOT(aboutQt()));
    browserMenu->addAction(tr("Quit"), qApp, SLOT(quit()), QKeySequence(tr("Ctrl+Q")));

    // Tools menu
    QMenu *toolsMenu = new QMenu(tr("Tools"), ui->menuBar);
    ui->menuBar->addMenu(toolsMenu);
    QAction *downloadsAction = toolsMenu->addAction(tr("Downloads"), Browser::instance()->downloads(), SLOT(show()));
    downloadsAction->setShortcut(QKeySequence::fromString(sSettings->value("downloads.dialogShortcut").toString()));
    QAction *bookmarksAction = toolsMenu->addAction(tr("Bookmarks"), Browser::instance()->bookmarks(), SLOT(show()));
    bookmarksAction->setShortcut(QKeySequence(sSettings->value("bookmarks.dialogShortcut").toString()));
    toolsMenu->addSeparator();
    toolsMenu->addAction(tr("Blocker"), blocklistManager, SLOT(show()));

    // Profile menu
    QMenu *profileMenu = new QMenu(tr("Profile"), ui->menuBar);
    ui->menuBar->addMenu(profileMenu);
    profileMenu->addAction(tr("View profile"), this, SLOT(execProfileEditor()));
    profileMenu->addAction(tr("Load profile"), this, SLOT(loadProfileGUI()));
    //profileMenu->addAction(tr("Settings"));
    //profileMenu->addAction(tr("Cookies"));

    navigationToolBar->setMovable(sSettings->value("ui.navtoolbarMovable", true).toBool());
    this->addToolBar(Qt::TopToolBarArea, navigationToolBar);
    this->addToolBarBreak(Qt::TopToolBarArea);
    tabToolBar->setMovable(sSettings->value("ui.tabtoolbarMovable", true).toBool());
    this->addToolBar(Qt::TopToolBarArea, tabToolBar);

    navigationToolBar->addWidget(urlLineEdit);
    connect(urlLineEdit, SIGNAL(returnPressed()), this, SLOT(handleUrlChanged()));

    tabToolBar->addWidget(tabBar);
    connect(tabBar, SIGNAL(currentTabChanged(WebView*)), this, SLOT(handleTabChanged(WebView*)));

    // loading bar
    ui->statusBar->addPermanentWidget(progressBar);

    // shortcuts
    QAction *focusAddressAction = new QAction(this);
    focusAddressAction->setShortcut(QKeySequence::fromString(sSettings->value("shortcuts.focusAddress").toString()));
    connect(focusAddressAction, SIGNAL(triggered(bool)), this, SLOT(focusAddress()));
    addAction(focusAddressAction);

    if(!defaultUrl.isEmpty()) {
        addNewTab(defaultUrl);
    } else {
        addNewTab(sSettings->value("general.homepage", QUrl("about:blank")).toUrl());
    }
}

MainWindow::MainWindow(const QStringList urlList, QWidget *parent) :
    MainWindow(QUrl(""), parent)
{
    for(QString url : urlList) {
        addNewTab(QUrl::fromUserInput(url));
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::addNewTab(const QUrl &url)
{
    if(!url.isEmpty()) {
        tabBar->addTab(m_profile, url);
    } else {
        tabBar->addTab(m_profile, sSettings->value("general.newtab").toUrl());
    }
}

void MainWindow::focusAddress()
{
    urlLineEdit->setFocus();
    urlLineEdit->selectAll();
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    if(tabBar->count() > 1) {
        int ret = QMessageBox::warning(this, tr("Close window?"), tr("Close multiple tabs?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
        if(ret == QMessageBox::No) {
            event->ignore();
            return;
        }
    }
    QMainWindow::closeEvent(event);
}

void MainWindow::about()
{
    QMessageBox::about(this, tr("About"), tr("<h3>smolbote %1</h3>"
                       "<p><i>yet another Qute browser</i></p>"
                       "<p>Copyright (C) 2017  Xian Nox</p>"
                       "<p>This program comes with ABSOLUTELY NO WARRANTY. "
                       "This is free software, and you are welcome to redistribute it under the conditions set by the GNU GPLv3.</p>"
                       "<p>Configuration lives in: %2</p>")
                       .arg(qApp->applicationVersion()).arg(sSettings->filePath()));
}

void MainWindow::loadProfile(const QString &name)
{
    if(m_profile) {
        m_profile->deleteLater();
    }

    if(name.isEmpty()) {
        //profileName = tr("Off the record");
        m_profile = new WebEngineProfile(this);
        qDebug("Creating off-the-record profile");
    } else {
        //profileName = name;
        m_profile = new WebEngineProfile(name, this);
        qDebug("Using profile: %s", qUtf8Printable(m_profile->storageName()));
    }

    UrlRequestInterceptor *interceptor = new UrlRequestInterceptor(this);
    interceptor->setSubscription(blocklistManager->subscription());
    m_profile->setRequestInterceptor(interceptor);
    connect(m_profile, SIGNAL(downloadRequested(QWebEngineDownloadItem*)), Browser::instance()->downloads(), SLOT(addDownload(QWebEngineDownloadItem*)));
}

void MainWindow::loadProfileGUI()
{
    bool ok;
    QString name = QInputDialog::getText(this, tr("Load Profile"), tr("Enter Profile name"), QLineEdit::Normal, QString(""), &ok);
    if(ok) {
        loadProfile(name);
        tabBar->setProfile(m_profile);
    }
}

void MainWindow::handleNewWindow(const QUrl &url)
{
    Browser::instance()->addWindow(new MainWindow(url));
}

void MainWindow::handleTabChanged(WebView *view)
{
    // clear the parent of the central widget so it doesn't get deleted
    centralWidget()->setParent(0);

    // disconnect signals
    disconnect(centralWidget());

    // set new central widget
    setCentralWidget(view);

    // connect signals
    connect(view, SIGNAL(urlChanged(QUrl)), urlLineEdit, SLOT(setUrl(QUrl)));
    connect(view, SIGNAL(titleChanged(QString)), this, SLOT(handleTitleUpdated(QString)));
    connect(view, SIGNAL(linkHovered(QString)), ui->statusBar, SLOT(showMessage(QString)));

    progressBar->connectWebView(view);

    // update UI
    urlLineEdit->setUrl(view->url());
    this->handleTitleUpdated(view->title());
    centralWidget()->setFocus();
}

void MainWindow::handleUrlChanged()
{
    tabBar->currentView()->load(urlLineEdit->url());
}

void MainWindow::handleTitleUpdated(const QString &title)
{
    setWindowTitle(sSettings->value("window.title").toString().replace("$title", title).replace("$profile", m_profile->storageName()));
}

void MainWindow::execProfileEditor()
{
    ProfileDialog *dialog = new ProfileDialog(m_profile, this);
    dialog->exec();
}