2016-09-01 16:34:30 +02:00
# include "./settings.h"
2016-09-24 16:19:23 +02:00
# include "./singleinstance.h"
2016-09-01 16:34:30 +02:00
# include "../gui/trayicon.h"
# include "../gui/traywidget.h"
2016-09-24 16:19:23 +02:00
2016-09-30 23:55:25 +02:00
# include "../../connector/syncthingprocess.h"
2016-12-18 16:50:35 +01:00
# ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
# include ".. / .. / connector / syncthingservice.h"
# endif
2016-09-01 16:34:30 +02:00
# include "resources/config.h"
# include <c++utilities/application/argumentparser.h>
# include <c++utilities/application/commandlineutils.h>
# include <c++utilities/application/failure.h>
# include <qtutilities/resources/qtconfigarguments.h>
# include <qtutilities/resources/resources.h>
# include <qtutilities/resources/importplugin.h>
# include <qtutilities/settingsdialog/qtsettings.h>
# include <QApplication>
2016-10-02 21:59:28 +02:00
# include <QNetworkAccessManager>
2016-09-01 16:34:30 +02:00
# include <QMessageBox>
2016-12-18 16:50:35 +01:00
# include <QStringBuilder>
2016-09-01 16:34:30 +02:00
# include <iostream>
using namespace std ;
using namespace ApplicationUtilities ;
using namespace QtGui ;
2016-09-03 19:39:43 +02:00
using namespace Data ;
2016-09-01 16:34:30 +02:00
2017-02-11 02:42:10 +01:00
ENABLE_QT_RESOURCES_OF_STATIC_DEPENDENCIES
2016-12-18 16:50:35 +01:00
# ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
void handleSystemdServiceError ( const QString & context , const QString & name , const QString & message )
{
QMessageBox msgBox ;
msgBox . setIcon ( QMessageBox : : Critical ) ;
msgBox . setText ( QCoreApplication : : translate ( " main " , " Unable to " ) + context ) ;
2016-12-27 16:16:12 +01:00
msgBox . setInformativeText ( name % QStringLiteral ( " : \n " ) % message ) ;
2016-12-18 16:50:35 +01:00
msgBox . exec ( ) ;
}
# endif
2016-09-24 16:19:23 +02:00
int initSyncthingTray ( bool windowed , bool waitForTray )
2016-09-01 16:34:30 +02:00
{
2016-11-02 20:03:38 +01:00
auto & v = Settings : : values ( ) ;
2016-12-18 16:50:35 +01:00
# ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
SyncthingService & service = syncthingService ( ) ;
service . setUnitName ( v . systemd . syncthingUnit ) ;
QObject : : connect ( & service , & SyncthingService : : errorOccurred , & handleSystemdServiceError ) ;
# endif
2016-09-24 16:19:23 +02:00
if ( windowed ) {
2017-03-17 00:38:42 +01:00
v . launcher . autostart ( ) ;
2016-09-24 16:19:23 +02:00
auto * trayWidget = new TrayWidget ;
trayWidget - > setAttribute ( Qt : : WA_DeleteOnClose ) ;
trayWidget - > show ( ) ;
} else {
# ifndef QT_NO_SYSTEMTRAYICON
if ( QSystemTrayIcon : : isSystemTrayAvailable ( ) | | waitForTray ) {
2017-03-17 00:38:42 +01:00
v . launcher . autostart ( ) ;
2016-09-24 16:19:23 +02:00
auto * trayIcon = new TrayIcon ;
trayIcon - > show ( ) ;
2016-11-02 20:03:38 +01:00
if ( v . firstLaunch ) {
2016-09-24 16:19:23 +02:00
QMessageBox msgBox ;
msgBox . setIcon ( QMessageBox : : Information ) ;
msgBox . setText ( QCoreApplication : : translate ( " main " , " You must configure how to connect to Syncthing when using Syncthing Tray the first time. " ) ) ;
msgBox . setInformativeText ( QCoreApplication : : translate ( " main " , " Note that the settings dialog allows importing URL, credentials and API-key from the local Syncthing configuration. " ) ) ;
msgBox . exec ( ) ;
trayIcon - > trayMenu ( ) . widget ( ) - > showSettingsDialog ( ) ;
}
} else {
QMessageBox : : critical ( nullptr , QApplication : : applicationName ( ) , QApplication : : translate ( " main " , " The system tray is (currently) not available. You could open the tray menu as a regular window using the -w flag, though. " ) ) ;
return - 1 ;
}
# else
QMessageBox : : critical ( nullptr , QApplication : : applicationName ( ) , QApplication : : translate ( " main " , " The Qt libraries have not been built with tray icon support. You could open the tray menu as a regular window using the -w flag, though. " ) ) ;
return - 2 ;
# endif
}
return 0 ;
}
2016-12-26 19:50:10 +01:00
void trigger ( bool tray , bool webUi )
{
if ( ! TrayWidget : : instances ( ) . empty ( ) & & ( tray | | webUi ) ) {
TrayWidget * trayWidget = TrayWidget : : instances ( ) . front ( ) ;
if ( webUi ) {
trayWidget - > showWebUi ( ) ;
}
if ( tray ) {
trayWidget - > showAtCursor ( ) ;
}
}
}
2016-09-24 16:19:23 +02:00
int runApplication ( int argc , const char * const * argv )
{
static bool firstRun = true ;
2016-09-01 16:34:30 +02:00
// setup argument parser
2016-09-24 16:19:23 +02:00
SET_APPLICATION_INFO ;
2017-01-02 23:25:58 +01:00
CMD_UTILS_CONVERT_ARGS_TO_UTF8 ;
2016-09-01 16:34:30 +02:00
ArgumentParser parser ;
HelpArgument helpArg ( parser ) ;
// Qt configuration arguments
QT_CONFIG_ARGUMENTS qtConfigArgs ;
Argument windowedArg ( " windowed " , ' w ' , " opens the tray menu as a regular window " ) ;
windowedArg . setCombinable ( true ) ;
2016-12-26 19:50:10 +01:00
Argument showWebUiArg ( " webui " , ' \0 ' , " instantly shows the web UI - meant for creating shortcut to web UI " ) ;
showWebUiArg . setCombinable ( true ) ;
Argument triggerArg ( " trigger " , ' \0 ' , " instantly shows the left-click tray menu - meant for creating a shortcut " ) ;
triggerArg . setCombinable ( true ) ;
2016-09-14 19:34:14 +02:00
Argument waitForTrayArg ( " wait " , ' \0 ' , " wait until the system tray becomes available instead of showing an error message if the system tray is not available on start-up " ) ;
waitForTrayArg . setCombinable ( true ) ;
2016-12-26 19:50:10 +01:00
Argument & widgetsGuiArg = qtConfigArgs . qtWidgetsGuiArg ( ) ;
widgetsGuiArg . addSubArgument ( & windowedArg ) ;
widgetsGuiArg . addSubArgument ( & showWebUiArg ) ;
widgetsGuiArg . addSubArgument ( & triggerArg ) ;
widgetsGuiArg . addSubArgument ( & waitForTrayArg ) ;
2016-09-01 16:34:30 +02:00
parser . setMainArguments ( { & qtConfigArgs . qtWidgetsGuiArg ( ) , & helpArg } ) ;
try {
parser . parseArgs ( argc , argv ) ;
if ( qtConfigArgs . qtWidgetsGuiArg ( ) . isPresent ( ) ) {
2016-09-24 16:19:23 +02:00
if ( firstRun ) {
firstRun = false ;
SET_QT_APPLICATION_INFO ;
QApplication application ( argc , const_cast < char * * > ( argv ) ) ;
QGuiApplication : : setQuitOnLastWindowClosed ( false ) ;
SingleInstance singleInstance ( argc , argv ) ;
2016-10-02 21:59:28 +02:00
networkAccessManager ( ) . setParent ( & singleInstance ) ;
2016-09-24 16:19:23 +02:00
QObject : : connect ( & singleInstance , & SingleInstance : : newInstance , & runApplication ) ;
Settings : : restore ( ) ;
2016-11-02 20:03:38 +01:00
Settings : : values ( ) . qt . apply ( ) ;
2016-09-24 16:19:23 +02:00
qtConfigArgs . applySettings ( true ) ;
LOAD_QT_TRANSLATIONS ;
int res = initSyncthingTray ( windowedArg . isPresent ( ) , waitForTrayArg . isPresent ( ) ) ;
if ( ! res ) {
2016-12-26 19:50:10 +01:00
trigger ( triggerArg . isPresent ( ) , showWebUiArg . isPresent ( ) ) ;
2016-09-01 16:34:30 +02:00
res = application . exec ( ) ;
2016-09-24 16:19:23 +02:00
}
2017-03-17 00:38:42 +01:00
Settings : : Launcher : : terminate ( ) ;
2016-09-24 16:19:23 +02:00
Settings : : save ( ) ;
return res ;
} else {
2016-12-26 19:50:10 +01:00
if ( ! TrayWidget : : instances ( ) . empty ( ) & & ( showWebUiArg . isPresent ( ) | | triggerArg . isPresent ( ) ) ) {
// if --webui or --trigger is present don't create a new tray icon, just trigger actions
trigger ( triggerArg . isPresent ( ) , showWebUiArg . isPresent ( ) ) ;
2016-09-01 16:34:30 +02:00
} else {
2016-12-26 19:50:10 +01:00
const int res = initSyncthingTray ( windowedArg . isPresent ( ) , waitForTrayArg . isPresent ( ) ) ;
if ( ! res ) {
trigger ( triggerArg . isPresent ( ) , showWebUiArg . isPresent ( ) ) ;
}
return res ;
2016-09-01 16:34:30 +02:00
}
}
}
} catch ( const Failure & ex ) {
CMD_UTILS_START_CONSOLE ;
2016-10-02 21:59:28 +02:00
cerr < < " Unable to parse arguments. " < < ex . what ( ) < < " \n See --help for available commands. " < < endl ;
return 1 ;
2016-09-01 16:34:30 +02:00
}
return 0 ;
}
2016-09-24 16:19:23 +02:00
int main ( int argc , char * argv [ ] )
{
return runApplication ( argc , argv ) ;
}