• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ddanilov / soundscape / 15516810038

08 Jun 2025 08:54AM UTC coverage: 77.388% (-0.2%) from 77.539%
15516810038

push

github

ddanilov
add cli option for single instance

0 of 8 new or added lines in 1 file covered. (0.0%)

2 existing lines in 1 file now uncovered.

794 of 1026 relevant lines covered (77.39%)

24.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Main.cpp
1
// SPDX-FileCopyrightText: 2022-2025 Denis Danilov
2
// SPDX-License-Identifier: GPL-3.0-only
3

4
#include "MainWindow.h"
5
#include "Version.h"
6

7
#include <QApplication>
8
#include <QCommandLineParser>
9
#include <QLibraryInfo>
10
#include <QLocale>
11
#include <QMessageBox>
12
#include <QSharedMemory>
13
#include <QTranslator>
14

15
#include <iostream>
16

UNCOV
17
int main(int argc, char* argv[])
×
18
{
19
  QApplication a(argc, argv);
×
20
  QApplication::setApplicationName(APP_TITLE);
×
21
  QApplication::setApplicationVersion(APP_VERSION);
×
22

23
  QList<QTranslator*> translators;
×
24
  auto loadTranslation = [&translators, &a](const auto& name, const auto& path) {
×
25
    auto* translator = translators.emplace_back(new QTranslator(&a));
×
26
    if (translator->load(QLocale::system(), name, "_", path))
×
27
    {
28
      QApplication::installTranslator(translator);
×
29
    }
30
  };
×
31
  loadTranslation("soundscape", ":/i18n/");
×
32
  loadTranslation("qtbase", QLibraryInfo::path(QLibraryInfo::TranslationsPath));
×
33
  loadTranslation("qtmultimedia", QLibraryInfo::path(QLibraryInfo::TranslationsPath));
×
34

35
  QCommandLineParser parser;
×
36
  parser.addHelpOption();
×
37
  parser.addVersionOption();
×
38

39
  const QCommandLineOption load_option("load",
40
                                       QCoreApplication::translate("Help", "Load track list from file."),
×
41
                                       QCoreApplication::translate("Help", "path to file"));
×
42
  parser.addOption(load_option);
×
43

44
  const QCommandLineOption minimize_option("minimize", QCoreApplication::translate("Help", "Minimize window to tray."));
×
45
  parser.addOption(minimize_option);
×
46

47
#if defined(Q_OS_MACOS)
48
  const QCommandLineOption tray_option("enable-tray", QCoreApplication::translate("Help", "Enable tray icon."));
49
#else
50
  const QCommandLineOption tray_option("disable-tray", QCoreApplication::translate("Help", "Disable tray icon."));
×
51
#endif
52
  parser.addOption(tray_option);
×
53

NEW
54
  const QCommandLineOption single_instance_option("single-instance", QCoreApplication::translate("Help", "Start only single instance."));
×
NEW
55
  parser.addOption(single_instance_option);
×
56

UNCOV
57
  parser.process(a);
×
58

59
  const QString file_name = parser.value(load_option);
×
60
  const bool minimize = parser.isSet(minimize_option);
×
61
#if defined(Q_OS_MACOS)
62
  const bool disable_tray = !parser.isSet(tray_option);
63
#else
64
  const bool disable_tray = parser.isSet(tray_option);
×
65
#endif
66

67
  QSharedMemory run_guard("io.github.ddanilov.soundscape");
×
68
  if (run_guard.create(sizeof(qint64)))
×
69
  {
70
    qint64 pid = QCoreApplication::applicationPid();
×
71
    auto* data = static_cast<qint64*>(run_guard.data());
×
72
    *data = pid;
×
73
  }
74
  else
75
  {
76
    if (!run_guard.attach())
×
77
    {
78
      return 0;
×
79
    }
80
    auto* data = static_cast<const qint64*>(run_guard.data());
×
81

NEW
82
    QString info1(QCoreApplication::translate("Main", "%1 is already running (PID: %2).").arg(APP_TITLE).arg(*data));
×
NEW
83
    if(parser.isSet(single_instance_option))
×
84
    {
NEW
85
      std::cerr << info1.toStdString() << std::endl;
×
NEW
86
      return 0;
×
87
    }
88

NEW
89
    const auto& info2 = info1.append("\n").append(QCoreApplication::translate("Main", "Continue anyway?"));
×
NEW
90
    const auto clicked = QMessageBox::question(nullptr, APP_TITLE, info2,
×
91
                                               QMessageBox::StandardButtons(QMessageBox::Yes | QMessageBox::No),
92
                                               QMessageBox::No);
93
    if (clicked != QMessageBox::Yes)
×
94
    {
95
      return 0;
×
96
    }
97
  }
×
98

99
  MainWindow w(disable_tray);
×
100
  w.start(file_name, minimize);
×
101

102
  return QApplication::exec();
×
103
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc