diff options
-rw-r--r-- | src/browser.cpp | 31 | ||||
-rw-r--r-- | src/browser.h | 8 | ||||
-rw-r--r-- | src/forms/bookmarksdialog.cpp | 6 | ||||
-rw-r--r-- | src/main.cpp | 17 | ||||
-rw-r--r-- | src/mainwindow.cpp | 34 | ||||
-rw-r--r-- | src/mainwindow.h | 7 | ||||
-rw-r--r-- | src/settings.cpp | 19 | ||||
-rw-r--r-- | src/settings.h | 9 | ||||
-rw-r--r-- | test/config.ini | 16 |
9 files changed, 88 insertions, 59 deletions
diff --git a/src/browser.cpp b/src/browser.cpp index 4bbfccb..6531070 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -23,30 +23,35 @@ #include "settings.h" #include <QtWebEngine> -Browser::Browser(QString configPath, QObject *parent) : QObject(parent) +Browser::Browser(int &argc, char *argv[]) : + QApplication(argc, argv) { - if(configPath.isEmpty()) { - // set default config path - Settings::setFilepath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/config.ini"); - } else { - // set custom config path - Settings::setFilepath(configPath); - } - - // TODO Restore previous session - QtWebEngine::initialize(); } Browser::~Browser() { - // TODO Save session - // cleanup qDeleteAll(m_windows); m_windows.clear(); } +Browser *Browser::instance() +{ + return static_cast<Browser *>(QCoreApplication::instance()); +} + +void Browser::setConfigPath(const QString &path) +{ + if(path.isEmpty()) { + // set default config path + Settings::setFilePath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/config.ini"); + } else { + // set custom config path + Settings::setFilePath(path); + } +} + void Browser::addWindow(MainWindow *window) { if(m_windows.contains(window)) { diff --git a/src/browser.h b/src/browser.h index 2638929..4230e7f 100644 --- a/src/browser.h +++ b/src/browser.h @@ -21,18 +21,20 @@ #ifndef BROWSER_H #define BROWSER_H -#include <QObject> +#include <QApplication> #include <QVector> class MainWindow; -class Browser : public QObject +class Browser : public QApplication { Q_OBJECT public: - explicit Browser(QString configPath, QObject *parent = 0); + Browser(int &argc, char *argv[]); ~Browser(); + static Browser *instance(); + void setConfigPath(const QString &path); void addWindow(MainWindow* window); public slots: diff --git a/src/forms/bookmarksdialog.cpp b/src/forms/bookmarksdialog.cpp index d61cf18..c8a0425 100644 --- a/src/forms/bookmarksdialog.cpp +++ b/src/forms/bookmarksdialog.cpp @@ -60,7 +60,9 @@ BookmarksDialog::BookmarksDialog(MainWindow *parent) : BookmarksDialog::~BookmarksDialog() { - xbel->write("bookmarks.xbel"); + if(ui->treeWidget->topLevelItemCount() > 0) { + xbel->write("bookmarks.xbel"); + } delete xbel; delete ui; @@ -75,7 +77,7 @@ void BookmarksDialog::openItem(QTreeWidgetItem *item, int column) { Q_UNUSED(column) - parentWindow->createNewTab(QUrl::fromUserInput(item->text(1))); + parentWindow->addNewTab(QUrl::fromUserInput(item->text(1))); } void BookmarksDialog::showItemDetails() diff --git a/src/main.cpp b/src/main.cpp index 34b93d5..8dcfb03 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,7 +25,7 @@ int main(int argc, char *argv[]) { - QApplication app(argc, argv); + Browser app(argc, argv); app.setApplicationName("smolbote"); #ifdef VERSION app.setApplicationVersion(VERSION); @@ -44,18 +44,9 @@ int main(int argc, char *argv[]) parser.process(app); - Browser instance(parser.value(configOption)); - - if(parser.positionalArguments().length() > 0) { - QUrl url = QUrl::fromUserInput(parser.positionalArguments().at(0)); - if(url.isValid()) { - instance.addWindow(new MainWindow(&instance, url)); - } else { - instance.addWindow(new MainWindow(&instance)); - } - } else { - instance.addWindow(new MainWindow(&instance)); - } + app.setConfigPath(parser.value(configOption)); + + app.addWindow(new MainWindow(parser.positionalArguments())); return app.exec(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 3bc5597..29b7e66 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -32,7 +32,7 @@ #include <QWebEngineDownloadItem> #include <QStatusBar> -MainWindow::MainWindow(Browser *instance, QUrl defaultUrl, QWidget *parent) : +MainWindow::MainWindow(QUrl defaultUrl, QWidget *parent) : QMainWindow(parent), downloadManager(new DownloadDialog(this)), blocklistManager(new BlockerDialog(this)), @@ -44,11 +44,10 @@ MainWindow::MainWindow(Browser *instance, QUrl defaultUrl, QWidget *parent) : urlLineEdit(new UrlLineEdit(navigationToolBar)), progressBar(new LoadingBar(this)) { - browserInstance = instance; Settings settings; // Load profile and connect its signals - loadProfile(settings.value("defaults/profile").toString()); + loadProfile(settings.value("profile").toString()); ui->setupUi(this); resize(settings.value("window/width", 800).toInt(), settings.value("window/height", 600).toInt()); @@ -57,7 +56,7 @@ MainWindow::MainWindow(Browser *instance, QUrl defaultUrl, QWidget *parent) : 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(createNewTab()), QKeySequence(tr("Ctrl+T"))); + 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())); @@ -91,9 +90,17 @@ MainWindow::MainWindow(Browser *instance, QUrl defaultUrl, QWidget *parent) : ui->statusBar->addPermanentWidget(progressBar); if(!defaultUrl.isEmpty()) { - createNewTab(defaultUrl); + addNewTab(defaultUrl); } else { - createNewTab(settings.value("defaults/url", QUrl("http://duckduckgo.com")).toUrl()); + 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)); } } @@ -102,9 +109,14 @@ MainWindow::~MainWindow() delete ui; } -void MainWindow::createNewTab(const QUrl &url) +void MainWindow::addNewTab(const QUrl &url) { - tabBar->addTab(profile, url); + if(!url.isEmpty()) { + tabBar->addTab(profile, url); + } else { + Settings settings; + tabBar->addTab(profile, settings.value("newtab").toUrl()); + } } void MainWindow::closeEvent(QCloseEvent *event) @@ -163,7 +175,7 @@ void MainWindow::loadProfileGUI() void MainWindow::handleNewWindow(const QUrl &url) { - browserInstance->addWindow(new MainWindow(browserInstance, url)); + Browser::instance()->addWindow(new MainWindow(url)); } void MainWindow::handleTabChanged(WebView *view) @@ -199,7 +211,9 @@ void MainWindow::handleTitleUpdated(const QString &title) { // For some reason, the long dash gets garbled if read from the settings //setWindowTitle(title + settings.value("window/title").toString()); - setWindowTitle(title + tr(" — smolbote")); + + Settings settings; + setWindowTitle(settings.value("window/title").toString().replace("$title", title).replace("$profile", profileName)); } void MainWindow::execProfileEditor() diff --git a/src/mainwindow.h b/src/mainwindow.h index 15e406a..bee1aab 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -38,17 +38,17 @@ namespace Ui { class MainWindow; } -class Browser; class MainWindow : public QMainWindow { Q_OBJECT public: - explicit MainWindow(Browser *instance, QUrl defaultUrl = QUrl(""), QWidget *parent = 0); + MainWindow(QUrl defaultUrl = QUrl(""), QWidget *parent = 0); + MainWindow(const QStringList urlList, QWidget *parent = 0); ~MainWindow(); public slots: - void createNewTab(const QUrl &url = QUrl("")); + void addNewTab(const QUrl &url = QUrl("")); protected: void closeEvent(QCloseEvent *event) override; @@ -66,7 +66,6 @@ private slots: void handleTitleUpdated(const QString &title); private: - Browser *browserInstance; DownloadDialog *downloadManager; BlockerDialog *blocklistManager; BookmarksDialog *bookmarkManager; diff --git a/src/settings.cpp b/src/settings.cpp index e4871ad..0d69e23 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -25,15 +25,24 @@ QString Settings::_path = QString(""); Settings::Settings(QObject *parent) : QSettings(_path, QSettings::IniFormat, parent) { + setIniCodec("UTF-8"); } -Settings::~Settings() +void Settings::setFilePath(const QString &path) { - this->sync(); + qDebug("config=[%s]", qUtf8Printable(path)); + _path = path; } -void Settings::setFilepath(const QString &path) +QVariant Settings::value(const QString &key, const QVariant &defaultValue) const { - qDebug("Setting config file location to: %s", qUtf8Printable(path)); - _path = path; + QString value = QSettings::value(key, defaultValue).toString(); + + QRegularExpressionMatch referenceMatch = referencePattern.match(value); + if(referenceMatch.hasMatch()) { + QString pattern = referenceMatch.capturedTexts().first(); + value.replace(pattern, this->value(pattern.mid(1, pattern.length()-2)).toString()); + } + + return QVariant(value); } diff --git a/src/settings.h b/src/settings.h index 7167aa1..f10cec7 100644 --- a/src/settings.h +++ b/src/settings.h @@ -22,16 +22,18 @@ #define SETTINGS_H #include <QSettings> - +#include <QRegularExpression> class Settings : public QSettings { Q_OBJECT public: explicit Settings(QObject *parent = 0); - ~Settings(); - static void setFilepath(const QString &path); + static void setFilePath(const QString &path); + + QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const; + //void setValue(const QString &key, const QVariant &value); signals: @@ -39,6 +41,7 @@ public slots: private: static QString _path; + const QRegularExpression referencePattern = QRegularExpression("~[\\w/]+~"); }; #endif // SETTINGS_H diff --git a/test/config.ini b/test/config.ini index 96de205..db6c896 100644 --- a/test/config.ini +++ b/test/config.ini @@ -1,10 +1,14 @@ -[defaults] -url=https://duckduckgo.com +[general] +homepage=https://duckduckgo.com +newtab=about:blank profile=Default -[window] -width=1280 -height=720 - [blocker] subscription= + +[bookmarks] +path=bookmarks.xbel + +[window] +height=720 +width=1280 |