summaryrefslogtreecommitdiff
path: root/src/application_instance.cpp
blob: 8c744d107fdda8863b814b544721c819458a17a7 (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
/* ============================================================
 *     The rekonq project
 * ============================================================
 * SPDX-License-Identifier: GPL-3.0-only
 * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net>
 * ============================================================
 * Description: Application Main Class
 *              Instance communication and Command Line parsing
 * ============================================================ */

#include "application.hpp"
#include <QCommandLineParser>

static const char *description = "A lightweight Web Browser based on Qt WebEngine";

void Application::parseCommandLine(quint32 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->newView() : this->newView(QUrl::fromUserInput(positionalArguments.first()));
  else {
    // create main window
    auto *window = newWindow();
    if (positionalArguments.isEmpty()) newView(QUrl(), window);
    else
      for (const auto &url : positionalArguments) newView(QUrl::fromUserInput(url), window);
  }
}