diff options
-rw-r--r-- | smolbote.qbs | 1 | ||||
-rw-r--r-- | src/browser.cpp | 45 | ||||
-rw-r--r-- | src/browser.h | 28 | ||||
-rw-r--r-- | src/main.cpp | 6 | ||||
-rw-r--r-- | src/mainwindow.h | 2 | ||||
-rw-r--r-- | src/singleapplication.cpp | 23 | ||||
-rw-r--r-- | src/singleapplication.h | 2 |
7 files changed, 64 insertions, 43 deletions
diff --git a/smolbote.qbs b/smolbote.qbs index 0637da3..045f8ce 100644 --- a/smolbote.qbs +++ b/smolbote.qbs @@ -65,6 +65,7 @@ Project { defines.push("QT_DEPRECATED_WARNINGS", "QT_DISABLE_DEPRECATED_BEFORE="+project.deprecatedBefore); return defines; } + cpp.cxxLanguageVersion: "c++14" Group { name: "main" diff --git a/src/browser.cpp b/src/browser.cpp index 897b0ad..c01c165 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -30,15 +30,12 @@ Browser::Browser(int &argc, char *argv[]) : SingleApplication(argc, argv) { - m_settings = nullptr; - - m_networkAccessManager = nullptr; - m_urlRequestInterceptor = nullptr; - m_bookmarksManager = nullptr; - m_downloadManager = nullptr; - m_blocklistManager = nullptr; - - m_plugin = nullptr; + setApplicationName("smolbote"); +#ifdef GIT_VERSION + setApplicationVersion(GIT_VERSION); +#else + setApplicationVersion("1.0.0"); +#endif } Browser::~Browser() @@ -208,23 +205,27 @@ void Browser::loadSettings(const QString &path) #endif } -MainWindow *Browser::mainWindow() +MainWindow *Browser::activeWindow() { - if(m_windows.isEmpty()) { + if(m_windows.empty()) { addWindow(new MainWindow()); } - return m_windows.first(); + + for(auto it = m_windows.cbegin(); it != m_windows.cend(); it++) { + if(it->data()->isActiveWindow()) { + return it->data(); + } + } + return m_windows.first().data(); } void Browser::addWindow(MainWindow *window) { - if(m_windows.contains(window)) { - return; - } + Q_ASSERT(window != nullptr); - m_windows.append(window); - connect(window, &QObject::destroyed, [this, window]() { - this->removeWindow(window); + m_windows.append(QPointer<MainWindow>(window)); + connect(window, &QObject::destroyed, [this]() { + this->clean(); }); window->show(); @@ -237,9 +238,13 @@ MainWindow *Browser::addWindow(const QStringList params) return w; } -void Browser::removeWindow(MainWindow *window) +void Browser::clean() { - m_windows.removeOne(window); + for(int i = m_windows.size(); i >= 0; i--) { + if(m_windows[i].isNull()) { + m_windows.removeAt(i); + } + } } WebEngineProfile* Browser::profile(const QString name) diff --git a/src/browser.h b/src/browser.h index 0edaf0c..43ff1f4 100644 --- a/src/browser.h +++ b/src/browser.h @@ -22,7 +22,6 @@ #define BROWSER_H #include "singleapplication.h" -#include <QVector> #include <bookmarks/bookmarkswidget.h> #include "forms/downloadswidget.h" #include "settings.h" @@ -34,8 +33,9 @@ #ifdef qApp #undef qApp -#define qApp Browser::instance() #endif +#define qApp Browser::instance() + #define sSettings Browser::instance()->settings() #define sNetwork Browser::instance()->network() @@ -45,7 +45,7 @@ class Browser : public SingleApplication Q_OBJECT public: - Browser(int &argc, char *argv[]); + explicit Browser(int &argc, char *argv[]); ~Browser(); QString applicationLongVersion() const; @@ -62,7 +62,7 @@ public: DownloadsWidget *downloads(); BlockerManager *blocklists(); - MainWindow *mainWindow(); + MainWindow *activeWindow(); WebEngineProfile *profile(const QString name); QStringList profiles(); @@ -73,21 +73,23 @@ public: public slots: void addWindow(MainWindow* window); MainWindow *addWindow(const QStringList params); - void removeWindow(MainWindow* window); + void clean(); private: - Settings *m_settings; + Q_DISABLE_COPY(Browser) + + Settings *m_settings = nullptr; - QVector<MainWindow *> m_windows; + QVector<QPointer<MainWindow>> m_windows; QHash<QString, WebEngineProfile *> m_profiles; - QNetworkAccessManager *m_networkAccessManager; - UrlRequestInterceptor *m_urlRequestInterceptor; - BookmarksWidget *m_bookmarksManager; - DownloadsWidget *m_downloadManager; - BlockerManager *m_blocklistManager; + QNetworkAccessManager *m_networkAccessManager = nullptr; + UrlRequestInterceptor *m_urlRequestInterceptor = nullptr; + BookmarksWidget *m_bookmarksManager = nullptr; + DownloadsWidget *m_downloadManager = nullptr; + BlockerManager *m_blocklistManager = nullptr; - QObject *m_plugin; + QObject *m_plugin = nullptr; }; diff --git a/src/main.cpp b/src/main.cpp index 36a30da..86b39db 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,12 +41,6 @@ int main(int argc, char *argv[]) { // Create application object Browser app(argc, argv); - app.setApplicationName("smolbote"); -#ifdef GIT_VERSION - app.setApplicationVersion(GIT_VERSION); -#else - app.setApplicationVersion("1.0.0"); -#endif // parse command line options QCommandLineParser parser; diff --git a/src/mainwindow.h b/src/mainwindow.h index 7f8e251..398e460 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -68,6 +68,8 @@ private slots: void handleTitleUpdated(const QString &title); private: + Q_DISABLE_COPY(MainWindow) + Ui::MainWindow *ui; QToolBar *navigationToolBar, *tabToolBar; WebViewTabBar *tabBar; diff --git a/src/singleapplication.cpp b/src/singleapplication.cpp index 01b2e65..331268b 100644 --- a/src/singleapplication.cpp +++ b/src/singleapplication.cpp @@ -38,10 +38,22 @@ SingleApplication::~SingleApplication() void SingleApplication::bindLocalSocket() { m_localServer = new QLocalServer(this); - connect(m_localServer, SIGNAL(newConnection()), this, SLOT(slot_receiveMessage())); + connect(m_localServer, &QLocalServer::newConnection, this, &SingleApplication::slot_receiveMessage); - if(!m_localServer->listen(LOCALSERVER_KEY)) { - qWarning("Cannot bind local server [%s]", qUtf8Printable(LOCALSERVER_KEY)); + // check for running local server by connecting to it + QLocalSocket socket; + socket.connectToServer(LOCALSERVER_KEY); + if(socket.waitForConnected(LOCALSERVER_TIMEOUT)) { + // there is another server + qWarning("Another server is running"); + socket.close(); + return; + } else { + // no other server + QLocalServer::removeServer(LOCALSERVER_KEY); + if(!m_localServer->listen(LOCALSERVER_KEY)) { + qWarning("Cannot bind local server [%s]", qUtf8Printable(LOCALSERVER_KEY)); + } } } @@ -80,6 +92,11 @@ void SingleApplication::slot_receiveMessage() QByteArray argumentData = socket->readAll(); + // skip if we got no data + if(argumentData.isEmpty()) { + return; + } + QHash<QString, QVariant> params; QDataStream ds(argumentData); ds >> params; diff --git a/src/singleapplication.h b/src/singleapplication.h index e14625a..b512b34 100644 --- a/src/singleapplication.h +++ b/src/singleapplication.h @@ -44,7 +44,7 @@ private slots: private: const int LOCALSERVER_TIMEOUT = 500; - const QString LOCALSERVER_KEY = "smolbote_KEY"; + const QString LOCALSERVER_KEY = "smolbote_socket"; QLocalServer *m_localServer; }; |