From c9b00d9b114eac7fe0f50c63cf2eede704c8f067 Mon Sep 17 00:00:00 2001 From: aqua Date: Wed, 17 Aug 2022 15:31:23 +0300 Subject: Pass command line from secondary to primary instance --- src/CMakeLists.txt | 2 +- src/application.cpp | 7 ++- src/application.h | 119 ----------------------------------------- src/application.hpp | 123 +++++++++++++++++++++++++++++++++++++++++++ src/application_instance.cpp | 50 ++++++++++++++++++ src/main.cpp | 43 +++------------ 6 files changed, 185 insertions(+), 159 deletions(-) delete mode 100644 src/application.h create mode 100644 src/application.hpp create mode 100644 src/application_instance.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5f154e3f..f1596ceb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -215,7 +215,7 @@ ADD_DEFINITIONS ( ${KDE4_DEFINITIONS} ) #KDE4_ADD_KDEINIT_EXECUTABLE( rekonq ${rekonq_KDEINIT_SRCS} main.cpp ) add_executable(rekonq - main.cpp application.cpp application.h + main.cpp application.cpp application_instance.cpp application.hpp # ---------------------------------------- plugins/rplugininterface.hpp plugins/rview.hpp ) diff --git a/src/application.cpp b/src/application.cpp index 9e34e5e2..f3bb5559 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -9,7 +9,7 @@ * Description: Application Main Class * ============================================================ */ -#include "application.h" +#include "application.hpp" #include "plugins/pluginloader.h" #include "plugins/rplugininterface.hpp" #include @@ -17,8 +17,11 @@ // --------------------------------------------------------------------------------------------------------------- // Ctor and Dtor -Application::Application(int &argc, char *argv[]) : SingleApplication(argc, argv) +Application::Application(int &argc, char *argv[]) : SingleApplication(argc, argv, true) { + // constructor needs to allow secondary instances (allowSecondary = true) + connect(this, &SingleApplication::receivedMessage, this, &Application::parseCommandLine); + // updating rekonq configuration // updateConfiguration(); diff --git a/src/application.h b/src/application.h deleted file mode 100644 index 407a8681..00000000 --- a/src/application.h +++ /dev/null @@ -1,119 +0,0 @@ -/* ============================================================ - * The rekonq project - * ============================================================ - * SPDX-License-Identifier: GPL-2.0-or-later - * Copyright (C) 2012-2013 by Andrea Diamantini - * SPDX-License-Identifier: GPL-3.0-only - * Copyright (C) 2022 aqua - * ============================================================ - * Description: Application Main Class - * ============================================================ */ - -#pragma once - -#include "rekonq.hpp" -#include -#include -#include - -// Forward Declarations -class rView; -class PluginLoader; -// class RekonqWindow; - -// class WebTab; -// class WebPage; - -// typedef QList< QWeakPointer > RekonqWindowList; -// typedef QList WebAppList; - -// --------------------------------------------------------------------------------------------------------------- - -/** - * Rekonq Application class - */ -class Application : public SingleApplication { - Q_OBJECT - -public: - Application(int &argc, char *argv[]); - ~Application() override; - - bool registerPlugin(const QString &path); - - // int newInstance(); - static Application *instance() { return (qobject_cast(QCoreApplication::instance())); } - - // RekonqWindow *rekonqWindow(const QString &activityID = QString()); - // RekonqWindowList rekonqWindowList(); - - /** - * @returns the list of windows associated with activity whose id is @param activityID - * @param activityID the ID of the activity (if empty, it is interpreted as the current activity) - * @note If activities are disabled, returns the function returns the list of all tabs. - */ - // RekonqWindowList tabsForActivity(const QString &activityID = QString()); - - /** - * @returns the true if there are windows associated with activity whose id is @param activityID - * @param activityID the ID of the activity (if empty, it is interpreted as the current activity) - * @note If activities are disabled, returns true if there are any tabs. - */ - // bool haveWindowsForActivity(const QString &activityID = QString()); - - // WebAppList webAppList(); - - // void bookmarksToolbarToggled(bool); - -public slots: - /** - * Save application's configuration - * - * @see ReKonfig::self()->writeConfig(); - */ - // void saveConfiguration() const; - - /** - * @short load url - * - * @param url The url to load - * @param type the type where loading the url. @see Rekonq::OpenType - */ - // void loadUrl(const KUrl& url, const Rekonq::OpenType& type = Rekonq::CurrentTab); - - // RekonqWindow *newWindow(bool withTab = true, bool PrivateBrowsingMode = false); - // RekonqWindow *newWindow(WebPage *pg); - rView *newWebApp(const QUrl &url = QUrl()); - - // void createWebAppShortcut(const QString & urlString = QString(), const QString & titleString = QString()); - -protected: - // This is used to track which window was activated most recently - // bool eventFilter(QObject *watched, QEvent *event); - -signals: - void toggleBookmarksToolbar(bool); - -private: - // void setWindowInfo(RekonqWindow *); - -private slots: - // void updateConfiguration(); - - // clear private data - // void clearPrivateData(); - - // void queryQuit(); - - // void newPrivateBrowsingWindow(); - - // void pageCreated(WebPage *); - -private: - QList m_plugins; - // RekonqWindowList m_rekonqWindows; - QList m_webApps; -}; - -[[deprecated]] static inline auto *rApp_f() { return Application::instance(); } -#define rApp rApp_f() diff --git a/src/application.hpp b/src/application.hpp new file mode 100644 index 00000000..e74350f1 --- /dev/null +++ b/src/application.hpp @@ -0,0 +1,123 @@ +/* ============================================================ + * The rekonq project + * ============================================================ + * SPDX-License-Identifier: GPL-2.0-or-later + * Copyright (C) 2012-2013 by Andrea Diamantini + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2022 aqua + * ============================================================ + * Description: Application Main Class + * ============================================================ */ + +#pragma once + +#include "rekonq.hpp" +#include +#include +#include + +// Forward Declarations +class rView; +class PluginLoader; +// class RekonqWindow; + +// class WebTab; +// class WebPage; + +// typedef QList< QWeakPointer > RekonqWindowList; +// typedef QList WebAppList; + +// --------------------------------------------------------------------------------------------------------------- + +/** + * Rekonq Application class + */ +class Application : public SingleApplication { + Q_OBJECT + +public: + Application(int &argc, char *argv[]); + ~Application() override; + + bool registerPlugin(const QString &path); + + // int newInstance(); + [[deprecated]] static Application *instance() { return (qobject_cast(QCoreApplication::instance())); } + + // RekonqWindow *rekonqWindow(const QString &activityID = QString()); + // RekonqWindowList rekonqWindowList(); + + /** + * @returns the list of windows associated with activity whose id is @param activityID + * @param activityID the ID of the activity (if empty, it is interpreted as the current activity) + * @note If activities are disabled, returns the function returns the list of all tabs. + */ + // RekonqWindowList tabsForActivity(const QString &activityID = QString()); + + /** + * @returns the true if there are windows associated with activity whose id is @param activityID + * @param activityID the ID of the activity (if empty, it is interpreted as the current activity) + * @note If activities are disabled, returns true if there are any tabs. + */ + // bool haveWindowsForActivity(const QString &activityID = QString()); + + // WebAppList webAppList(); + + // void bookmarksToolbarToggled(bool); + +public slots: + /** + * Parse the command line in @param message and carry out the required actions + * @param instanceId if the current instance, check QCoreApplication::arguments instead of @param message + * @param message the command line + */ + void parseCommandLine(int instanceId, const QByteArray &message); + + /** + * Save application's configuration + * + * @see ReKonfig::self()->writeConfig(); + */ + // void saveConfiguration() const; + + /** + * @short load url + * + * @param url The url to load + * @param type the type where loading the url. @see Rekonq::OpenType + */ + // void loadUrl(const KUrl& url, const Rekonq::OpenType& type = Rekonq::CurrentTab); + + // RekonqWindow *newWindow(bool withTab = true, bool PrivateBrowsingMode = false); + // RekonqWindow *newWindow(WebPage *pg); + rView *newWebApp(const QUrl &url = QUrl()); + + // void createWebAppShortcut(const QString & urlString = QString(), const QString & titleString = QString()); + +protected: + // This is used to track which window was activated most recently + // bool eventFilter(QObject *watched, QEvent *event); + +signals: + void toggleBookmarksToolbar(bool); + +private: + // void setWindowInfo(RekonqWindow *); + +private slots: + // void updateConfiguration(); + + // clear private data + // void clearPrivateData(); + + // void queryQuit(); + + // void newPrivateBrowsingWindow(); + + // void pageCreated(WebPage *); + +private: + QList m_plugins; + // RekonqWindowList m_rekonqWindows; + QList m_webApps; +}; diff --git a/src/application_instance.cpp b/src/application_instance.cpp new file mode 100644 index 00000000..c2005acd --- /dev/null +++ b/src/application_instance.cpp @@ -0,0 +1,50 @@ +/* ============================================================ + * The rekonq project + * ============================================================ + * SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2022 aqua + * ============================================================ + * Description: Application Main Class + * Instance communication and Command Line parsing + * ============================================================ */ + +#include "application.hpp" +#include + +static const char *description = "A lightweight Web Browser based on Qt WebEngine"; + +void Application::parseCommandLine(int instanceId, const QByteArray &message) +{ + // Initialize command line args + QCommandLineParser parser; + parser.setApplicationDescription(description); + parser.addHelpOption(); + parser.addVersionOption(); + + // Define the command line options + QCommandLineOption options_incognito("incognito", QCoreApplication::translate("main", "Open in incognito mode")); + QCommandLineOption options_webapp("webapp", + QCoreApplication::translate("main", "Open URL as web app (in a simple window)")); + QCommandLineOption options_plugin({"l", "load"}, QCoreApplication::translate("main", "Add plugin to load path"), + "path"); + parser.addOptions({options_incognito, options_webapp, options_plugin}); + + // Define the positional arguments + parser.addPositionalArgument("URL", QCoreApplication::translate("main", "Location to open")); + + if (instanceId == this->instanceId()) parser.process(*this); + else { + QStringList arguments; + for (const auto &arg : message.split('\n')) arguments.append(arg); + parser.process(arguments); + } + + if (parser.isSet(options_plugin)) { + for (const auto &plugin : parser.values(options_plugin)) this->registerPlugin(plugin); + } + + const auto positionalArguments = parser.positionalArguments(); + if (parser.isSet(options_webapp)) + positionalArguments.isEmpty() ? this->newWebApp() + : this->newWebApp(QUrl::fromUserInput(positionalArguments.first())); +} diff --git a/src/main.cpp b/src/main.cpp index 6db6f36c..1d6e30da 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,13 +9,7 @@ * Description: Application Entry Point * ============================================================ */ -#include "application.h" -//#include "sessionmanager.h" -//#include "rekonqwindow.h" -//#include "urlresolver.h" -#include - -static const char *description = "A lightweight Web Browser based on Qt WebEngine"; +#include "application.hpp" int main(int argc, char **argv) { @@ -27,37 +21,12 @@ int main(int argc, char **argv) QCoreApplication::setApplicationName(QL1S("rekonq")); QCoreApplication::setApplicationVersion(REKONQ_VERSION); - // Initialize command line args - QCommandLineParser parser; - parser.setApplicationDescription(description); - parser.addHelpOption(); - parser.addVersionOption(); - - // Define the command line options - QCommandLineOption options_incognito("incognito", QCoreApplication::translate("main", "Open in incognito mode")); - QCommandLineOption options_webapp("webapp", - QCoreApplication::translate("main", "Open URL as web app (in a simple window)")); - QCommandLineOption options_plugin({"l", "load"}, QCoreApplication::translate("main", "Add plugin to load path"), - "path"); - parser.addOptions({options_incognito, options_webapp, options_plugin}); - - // Define the positional arguments - parser.addPositionalArgument("URL", QCoreApplication::translate("main", "Location to open")); - - parser.process(app); - - if (parser.isSet(options_plugin)) { - for (const auto &plugin : parser.values(options_plugin)) app.registerPlugin(plugin); - } - - const auto positionalArguments = parser.positionalArguments(); - if (parser.isSet(options_webapp)) - positionalArguments.isEmpty() ? app.newWebApp() : app.newWebApp(QUrl::fromUserInput(positionalArguments.first())); - - /*if (!Application::start()) { - kWarning() << "rekonq is already running!"; + if (app.isSecondary()) { + app.sendMessage(Application::arguments().join('\n').toUtf8()); return 0; - }*/ + } + else + app.parseCommandLine(app.instanceId(), {}); /* -- cgit v1.2.1