File: main.cpp

package info (click to toggle)
cmst 2022.11.30-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,220 kB
  • sloc: cpp: 6,996; xml: 142; makefile: 9
file content (215 lines) | stat: -rwxr-xr-x 9,144 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**************************** main.cpp *********************************

C++ main routine.

Copyright (C) 2013-2022
by: Andrew J. Bibb
License: MIT

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
***********************************************************************/

# include <QtGlobal>
# include <QtCore/QDebug>
# include <QApplication>
# include <QCommandLineOption>
# include <QCommandLineParser>
# include <QStringList>
# include <QStyleFactory>
# include <QLocalSocket>
# include <QSessionManager>
# include <QTranslator>
# include <QLibraryInfo>

# include <signal.h>

# include "./control_box/controlbox.h"
# include "../resource.h"


// Create a signal handler to catch ^C from console
void signalhandler(int sig) {
   if(sig == SIGINT || sig == SIGTERM) {
      qApp->quit();
   }

   return;
}

int main(int argc, char *argv[])
{

# if QT_VERSION < 0x060000
   QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
# endif

   QApplication::setApplicationName(LONG_NAME);
   QApplication::setApplicationVersion(VERSION);
   QApplication::setOrganizationName(ORG);
   QApplication::setDesktopSettingsAware(true);
   QApplication app(argc, argv);

   // make sure only one instance is running
   QLocalSocket* socket = new QLocalSocket();
   socket->connectToServer(SOCKET_NAME);
   bool b_connected = socket->waitForConnected(500);
   socket->abort();
   delete socket;
   if (b_connected) {
      qDebug() <<  QCoreApplication::translate("main.cpp", "Another running instance of CMST has been detected.  This instance is aborting");
      return 1;
   }

   // setup the command line parser
   QCommandLineParser parser;
   parser.setApplicationDescription(QApplication::translate("main.cpp", "Connman System Tray.") );

   QCommandLineOption bypassState(QStringList() << "b" << "bypass-restore-state",
      QCoreApplication::translate("main.cpp", "Bypass restoring the window state if restoring window state is specified in the settings file.") );
   parser.addOption(bypassState);

   QCommandLineOption bypassStartOptions(QStringList() << "B" << "bypass-start-options",
      QCoreApplication::translate("main.cpp", "Bypass restoring any start options in the settings file.") );
   parser.addOption(bypassStartOptions);

   QCommandLineOption enableCounters(QStringList() << "c" << "enable-counters",
      QCoreApplication::translate("main.cpp", "[Experimental] Enable data counters.") );
   parser.addOption(enableCounters);

   QCommandLineOption disableTrayIcon(QStringList() << "d" << "disable-tray-icon",
      QCoreApplication::translate("main.cpp", "Disable the system tray icon.  May be needed for system trays not compliant with the Freedesktop.org system tray specification.") );
   parser.addOption(disableTrayIcon);

   parser.addHelpOption();

   QCommandLineOption useIconTheme(QStringList() << "i" << "icon-theme",
      QCoreApplication::translate("main.cpp", "Use an icon theme from your system."),
      QCoreApplication::translate("main.cpp", "Icon Theme Name"),
      QString("") );
   parser.addOption(useIconTheme);

   QCommandLineOption iconScale(QStringList() << "I" << "icon-scale",
      QCoreApplication::translate("main.cpp", "Scale program artwork for high DPI monitors."),
      QCoreApplication::translate("main.cpp", "Icon Scale" "1"),
      QString("") );
   parser.addOption(iconScale);

   QCommandLineOption logInputRequest(QStringList() << "l" << "log-input-request",
      QCoreApplication::translate("main.cpp", "Log the connman inputRequest for debugging purposes.") );
   parser.addOption(logInputRequest);

   QCommandLineOption startMinimized(QStringList() << "m" << "minimized",
      QCoreApplication::translate("main.cpp", "Start the GUI minimized in the system tray.") );
   parser.addOption(startMinimized);

   QCommandLineOption disableMinimize(QStringList() << "M" << "disable-minimize",
      QCoreApplication::translate("main.cpp", "Disable the minimize button. Use when you want to have the window manager have sole control of minimizing the interface.") );
   parser.addOption(disableMinimize);

   QCommandLineOption disableVPN(QStringList() << "n" << "disable-vpn",
      QCoreApplication::translate("main.cpp", "Disable VPN support.") );
   parser.addOption(disableVPN);

   parser.addVersionOption();

   QCommandLineOption waitTime(QStringList() << "w" << "wait-time",
      QCoreApplication::translate("main.cpp", "Specify the wait time in seconds before starting the system tray icon."),
      QCoreApplication::translate("main.cpp", "seconds"),
      "0");
   parser.addOption(waitTime);

   QCommandLineOption counterUpdateKb (QStringList() << "counter-update-kb",
      QCoreApplication::translate("main.cpp", "[Experimental] The number of kb that have to be transmitted before the counter updates."),
      QCoreApplication::translate("main.cpp", "KB"),
      "1024" );
   parser.addOption(counterUpdateKb);

   QCommandLineOption counterUpdateRate (QStringList() << "counter-update-rate",
      QCoreApplication::translate("main.cpp", "[Experimental] The interval in seconds between counter updates."),
      QCoreApplication::translate("main.cpp", "seconds"),
      "10" );
   parser.addOption(counterUpdateRate);

   // Added on 2015.01.04 to work around QT5.4 bug with transparency not always working
   QCommandLineOption fakeTransparency(QStringList() << "fake-transparency",
      QCoreApplication::translate("main.cpp", "If tray icon fake transparency is required, specify the background color to use (format: 0xRRGGBB)"),
      QCoreApplication::translate("main.cpp", "RRGGBB"),
      "0x222222" );
   parser.addOption(fakeTransparency);

   # ifdef XFCE
   // Added on 2014.11.24 to work around a bug where QT5.3 won't show an icon in XFCE,  My fix may not work, but keep it in for now.  If this gets fixed in
   // QT5.4 keep the command line option so users start up commands don't break, but make it a NOP.
   QCommandLineOption useXFCE(QStringList() << "use-xfce",
      QCoreApplication::translate("main.cpp", "Use XFCE specific code.") );
   parser.addOption(useXFCE);

   // Added on 2014.12.16 to work around a similar bug where QT5.3 won't show an icon in MATE.
   QCommandLineOption useMATE(QStringList() << "use-mate",
      QCoreApplication::translate("main.cpp", "Use MATE DE specific code.") );
   parser.addOption(useMATE);
   # endif

   // Setup translations
   QTranslator qtTranslator;
   # if QT_VERSION < 0x060000
      qtTranslator.load("qt_" + QLocale::system().name(),
      QLibraryInfo::location(QLibraryInfo::TranslationsPath));
   # else
      (void) qtTranslator.load("qt_" + QLocale::system().name(),
      QLibraryInfo::path(QLibraryInfo::TranslationsPath));
   # endif
   app.installTranslator(&qtTranslator);

   QTranslator cmstTranslator;
   if (cmstTranslator.load(":/i18n/cmst_" + QLocale::system().name()) ) {
      app.installTranslator(&cmstTranslator);
   }
   // else use en_US as it contains Connman strings properized and some singular/plural strings
   else if (cmstTranslator.load(":/i18n/cmst_en_US") ) {
      app.installTranslator(&cmstTranslator);
   }

   // Make sure all the command lines can be parsed
   // using parse() instead of process() as process stops on an error if an option needs a value
   // and it is not specified, even if we provide a default.  We're supposed to catch errors if we
   // use parse(), but parse.errorText() returns an empty string on this.  Bag the error checking
   // for now.
   parser.parse(QCoreApplication::arguments() );
   QStringList sl = parser.unknownOptionNames();
   if (sl.size() > 0 ) parser.showHelp(1);
   if (parser.isSet("help") ) parser.showHelp(1);
   if (parser.isSet("version") ) {
   #if QT_VERSION >= 0x050400
      parser.showVersion();
   #else
      QTextStream out(stdout);
      out << qPrintable(LONG_NAME) << " " << qPrintable(VERSION) << endl;
      return 0;
   #endif
   }

   // signal handler
   signal(SIGINT, signalhandler);

   // Showing the dialog (or not) is controlled in the createSystemTrayIcon() function
   // called from the ControlBox constructor.   We don't show it from here.
   ControlBox ctlbox(parser);
   return app.exec();
}