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))
{
Settings settings;
// Load profile and connect its signals
loadProfile(settings.value("profile").toString());
ui->setupUi(this);
resize(settings.value("window/width", 800).toInt(), settings.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(settings.value("downloads/dialogShortcut").toString()));
QAction *bookmarksAction = toolsMenu->addAction(tr("Bookmarks"), Browser::instance()->bookmarks(), SLOT(show()));
bookmarksAction->setShortcut(QKeySequence(settings.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(settings.value("ui/navtoolbarMovable", true).toBool());
this->addToolBar(Qt::TopToolBarArea, navigationToolBar);
this->addToolBarBreak(Qt::TopToolBarArea);
tabToolBar->setMovable(settings.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(settings.value("shortcuts/focusAddress").toString()));
connect(focusAddressAction, SIGNAL(triggered(bool)), this, SLOT(focusAddress()));
addAction(focusAddressAction);
if(!defaultUrl.isEmpty()) {
addNewTab(defaultUrl);
} else {
addNewTab(settings.value("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 {
Settings settings;
tabBar->addTab(m_profile, settings.value("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("smolbote %1
"
"yet another Qute browser
"
"Copyright (C) 2017 Xian Nox
"
"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.
"
"Configuration lives in: %2
")
.arg(qApp->applicationVersion()).arg(Settings::staticFilePath()));
}
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)
{
Settings settings;
setWindowTitle(settings.value("window/title").toString().replace("$title", title).replace("$profile", m_profile->storageName()));
}
void MainWindow::execProfileEditor()
{
ProfileDialog *dialog = new ProfileDialog(m_profile, this);
dialog->exec();
}