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-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>
# 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
2016-09-24 16:19:23 +02:00
int initSyncthingTray ( bool windowed , bool waitForTray )
2016-09-01 16:34:30 +02:00
{
2016-09-24 16:19:23 +02:00
if ( windowed ) {
if ( Settings : : launchSynchting ( ) ) {
2016-09-29 21:19:54 +02:00
syncthingProcess ( ) . startSyncthing ( Settings : : syncthingCmd ( ) ) ;
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 ) {
if ( Settings : : launchSynchting ( ) ) {
2016-09-29 21:19:54 +02:00
syncthingProcess ( ) . startSyncthing ( Settings : : syncthingCmd ( ) ) ;
2016-09-24 16:19:23 +02:00
}
auto * trayIcon = new TrayIcon ;
trayIcon - > show ( ) ;
if ( Settings : : firstLaunch ( ) ) {
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 : : c ritical ( 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 ;
}
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 ;
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-09-24 16:19:23 +02:00
Argument showWebUi ( " webui " , ' \0 ' , " instantly shows the web UI - meant for creating shortcut to web UI " ) ;
showWebUi . 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-09-01 16:34:30 +02:00
qtConfigArgs . qtWidgetsGuiArg ( ) . addSubArgument ( & windowedArg ) ;
2016-09-24 16:19:23 +02:00
qtConfigArgs . qtWidgetsGuiArg ( ) . addSubArgument ( & showWebUi ) ;
2016-09-14 19:34:14 +02:00
qtConfigArgs . qtWidgetsGuiArg ( ) . 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 ( ) ;
Settings : : qtSettings ( ) . apply ( ) ;
qtConfigArgs . applySettings ( true ) ;
LOAD_QT_TRANSLATIONS ;
2016-10-05 23:01:53 +02:00
TranslationFiles : : loadApplicationTranslationFile ( QStringLiteral ( " syncthingconnection " ) ) ;
TranslationFiles : : loadApplicationTranslationFile ( QStringLiteral ( " syncthingmodel " ) ) ;
2016-09-24 16:19:23 +02:00
QtUtilitiesResources : : init ( ) ;
int res = initSyncthingTray ( windowedArg . isPresent ( ) , waitForTrayArg . isPresent ( ) ) ;
if ( ! res ) {
if ( ! TrayWidget : : instances ( ) . empty ( ) & & showWebUi . isPresent ( ) ) {
TrayWidget : : instances ( ) . front ( ) - > showWebUi ( ) ;
2016-09-01 16:34:30 +02:00
}
res = application . exec ( ) ;
2016-09-24 16:19:23 +02:00
}
Settings : : save ( ) ;
QtUtilitiesResources : : cleanup ( ) ;
return res ;
} else {
if ( ! TrayWidget : : instances ( ) . empty ( ) & & showWebUi . isPresent ( ) ) {
2016-10-03 00:41:38 +02:00
// if --webui is present don't create a new tray icon, just show the web UI of the present one
2016-09-24 16:19:23 +02:00
TrayWidget : : instances ( ) . front ( ) - > showWebUi ( ) ;
2016-09-01 16:34:30 +02:00
} else {
2016-09-24 16:19:23 +02:00
return initSyncthingTray ( windowedArg . isPresent ( ) , waitForTrayArg . isPresent ( ) ) ;
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 ) ;
}