2016-08-30 20:01:07 +02:00
# include "./traywidget.h"
# include "./traymenu.h"
2016-09-24 16:19:23 +02:00
# include "./trayicon.h"
2016-08-25 00:45:32 +02:00
# include "./settingsdialog.h"
# include "./webviewdialog.h"
2016-09-08 23:35:15 +02:00
# include "./textviewdialog.h"
2016-08-25 00:45:32 +02:00
# include "../application/settings.h"
# include "resources/config.h"
# include "ui_traywidget.h"
# include <qtutilities/resources/qtconfigarguments.h>
# include <qtutilities/resources/resources.h>
# include <qtutilities/settingsdialog/qtsettings.h>
# include <qtutilities/aboutdialog/aboutdialog.h>
# include <qtutilities/misc/dialogutils.h>
# include <qtutilities/misc/desktoputils.h>
2016-08-29 20:51:30 +02:00
# include <c++utilities/conversion/stringconversion.h>
2016-08-30 20:01:07 +02:00
# include <QCoreApplication>
2016-08-25 00:45:32 +02:00
# include <QDesktopServices>
# include <QMessageBox>
# include <QClipboard>
# include <QDir>
# include <QTextBrowser>
# include <QStringBuilder>
2016-09-03 19:39:43 +02:00
# include <QFontDatabase>
2016-08-25 00:45:32 +02:00
# include <functional>
2016-09-24 16:19:23 +02:00
# include <algorithm>
2016-08-25 00:45:32 +02:00
using namespace ApplicationUtilities ;
2016-08-29 20:51:30 +02:00
using namespace ConversionUtilities ;
2016-09-08 23:35:15 +02:00
using namespace ChronoUtilities ;
2016-08-25 00:45:32 +02:00
using namespace Dialogs ;
using namespace Data ;
using namespace std ;
namespace QtGui {
2016-09-25 20:54:09 +02:00
SettingsDialog * TrayWidget : : m_settingsDlg = nullptr ;
Dialogs : : AboutDialog * TrayWidget : : m_aboutDlg = nullptr ;
2016-09-24 16:19:23 +02:00
vector < TrayWidget * > TrayWidget : : m_instances ;
2016-08-25 00:45:32 +02:00
/*!
* \ brief Instantiates a new tray widget .
*/
TrayWidget : : TrayWidget ( TrayMenu * parent ) :
QWidget ( parent ) ,
m_menu ( parent ) ,
m_ui ( new Ui : : TrayWidget ) ,
# ifndef SYNCTHINGTRAY_NO_WEBVIEW
m_webViewDlg ( nullptr ) ,
# endif
m_dirModel ( m_connection ) ,
2016-09-03 20:14:52 +02:00
m_devModel ( m_connection ) ,
2016-09-21 21:09:12 +02:00
m_dlModel ( m_connection ) ,
2016-09-03 20:14:52 +02:00
m_selectedConnection ( nullptr )
2016-08-25 00:45:32 +02:00
{
2016-09-24 16:19:23 +02:00
m_instances . push_back ( this ) ;
2016-08-25 00:45:32 +02:00
m_ui - > setupUi ( this ) ;
// setup model and view
m_ui - > dirsTreeView - > setModel ( & m_dirModel ) ;
m_ui - > devsTreeView - > setModel ( & m_devModel ) ;
2016-09-21 21:09:12 +02:00
m_ui - > downloadsTreeView - > setModel ( & m_dlModel ) ;
2016-08-25 00:45:32 +02:00
// setup sync-all button
2016-09-03 19:39:43 +02:00
m_cornerFrame = new QFrame ( this ) ;
auto * cornerFrameLayout = new QHBoxLayout ( m_cornerFrame ) ;
2016-08-25 00:45:32 +02:00
cornerFrameLayout - > setSpacing ( 0 ) , cornerFrameLayout - > setMargin ( 0 ) ;
2016-09-03 19:39:43 +02:00
//cornerFrameLayout->addStretch();
m_cornerFrame - > setLayout ( cornerFrameLayout ) ;
auto * viewIdButton = new QPushButton ( m_cornerFrame ) ;
2016-08-25 00:45:32 +02:00
viewIdButton - > setToolTip ( tr ( " View own device ID " ) ) ;
2016-08-29 20:51:30 +02:00
viewIdButton - > setIcon ( QIcon : : fromTheme ( QStringLiteral ( " view-barcode " ) , QIcon ( QStringLiteral ( " :/icons/hicolor/scalable/actions/view-barcode.svg " ) ) ) ) ;
2016-08-25 00:45:32 +02:00
viewIdButton - > setFlat ( true ) ;
cornerFrameLayout - > addWidget ( viewIdButton ) ;
2016-09-03 19:39:43 +02:00
auto * restartButton = new QPushButton ( m_cornerFrame ) ;
restartButton - > setToolTip ( tr ( " Restart Syncthing " ) ) ;
restartButton - > setIcon ( QIcon : : fromTheme ( QStringLiteral ( " system-reboot " ) , QIcon ( QStringLiteral ( " :/icons/hicolor/scalable/actions/view-refresh.svg " ) ) ) ) ;
restartButton - > setFlat ( true ) ;
cornerFrameLayout - > addWidget ( restartButton ) ;
auto * showLogButton = new QPushButton ( m_cornerFrame ) ;
2016-08-25 00:45:32 +02:00
showLogButton - > setToolTip ( tr ( " Show Syncthing log " ) ) ;
2016-09-03 19:39:43 +02:00
showLogButton - > setIcon ( QIcon : : fromTheme ( QStringLiteral ( " text-x-generic " ) , QIcon ( QStringLiteral ( " :/icons/hicolor/scalable/mimetypes/text-x-generic.svg " ) ) ) ) ;
2016-08-25 00:45:32 +02:00
showLogButton - > setFlat ( true ) ;
cornerFrameLayout - > addWidget ( showLogButton ) ;
2016-09-03 19:39:43 +02:00
auto * scanAllButton = new QPushButton ( m_cornerFrame ) ;
2016-08-25 00:45:32 +02:00
scanAllButton - > setToolTip ( tr ( " Rescan all directories " ) ) ;
2016-08-29 20:51:30 +02:00
scanAllButton - > setIcon ( QIcon : : fromTheme ( QStringLiteral ( " folder-sync " ) , QIcon ( QStringLiteral ( " :/icons/hicolor/scalable/actions/folder-sync.svg " ) ) ) ) ;
2016-08-25 00:45:32 +02:00
scanAllButton - > setFlat ( true ) ;
cornerFrameLayout - > addWidget ( scanAllButton ) ;
2016-09-03 19:39:43 +02:00
m_ui - > tabWidget - > setCornerWidget ( m_cornerFrame , Qt : : BottomRightCorner ) ;
2016-09-03 20:14:52 +02:00
// setup connection menu
m_connectionsActionGroup = new QActionGroup ( m_connectionsMenu = new QMenu ( tr ( " Connection " ) , this ) ) ;
m_connectionsMenu - > setIcon ( QIcon : : fromTheme ( QStringLiteral ( " network-connect " ) , QIcon ( QStringLiteral ( " :/icons/hicolor/scalable/actions/network-connect.svg " ) ) ) ) ;
2016-09-17 19:13:49 +02:00
m_ui - > connectionsPushButton - > setText ( Settings : : primaryConnectionSettings ( ) . label ) ;
m_ui - > connectionsPushButton - > setMenu ( m_connectionsMenu ) ;
2016-09-03 20:14:52 +02:00
2016-09-03 19:39:43 +02:00
// apply settings, this also establishes the connection to Syncthing
applySettings ( ) ;
2016-08-25 00:45:32 +02:00
2016-09-08 23:35:15 +02:00
// setup other widgets
m_ui - > notificationsPushButton - > setHidden ( true ) ;
2016-08-29 20:51:30 +02:00
m_ui - > trafficIconLabel - > setPixmap ( QIcon : : fromTheme ( QStringLiteral ( " network-card " ) , QIcon ( QStringLiteral ( " :/icons/hicolor/scalable/devices/network-card.svg " ) ) ) . pixmap ( 32 ) ) ;
2016-08-25 00:45:32 +02:00
// connect signals and slots
2016-08-29 20:51:30 +02:00
connect ( m_ui - > statusPushButton , & QPushButton : : clicked , this , & TrayWidget : : changeStatus ) ;
2016-09-24 16:19:23 +02:00
connect ( m_ui - > closePushButton , & QPushButton : : clicked , this , & TrayWidget : : quitTray ) ;
2016-08-25 00:45:32 +02:00
connect ( m_ui - > aboutPushButton , & QPushButton : : clicked , this , & TrayWidget : : showAboutDialog ) ;
connect ( m_ui - > webUiPushButton , & QPushButton : : clicked , this , & TrayWidget : : showWebUi ) ;
connect ( m_ui - > settingsPushButton , & QPushButton : : clicked , this , & TrayWidget : : showSettingsDialog ) ;
2016-09-06 22:55:49 +02:00
connect ( & m_connection , & SyncthingConnection : : statusChanged , this , & TrayWidget : : handleStatusChanged ) ;
2016-08-29 20:51:30 +02:00
connect ( & m_connection , & SyncthingConnection : : trafficChanged , this , & TrayWidget : : updateTraffic ) ;
2016-09-01 16:34:30 +02:00
connect ( & m_connection , & SyncthingConnection : : newNotification , this , & TrayWidget : : handleNewNotification ) ;
2016-08-25 00:45:32 +02:00
connect ( m_ui - > dirsTreeView , & DirView : : openDir , this , & TrayWidget : : openDir ) ;
connect ( m_ui - > dirsTreeView , & DirView : : scanDir , this , & TrayWidget : : scanDir ) ;
2016-08-26 16:43:53 +02:00
connect ( m_ui - > devsTreeView , & DevView : : pauseResumeDev , this , & TrayWidget : : pauseResumeDev ) ;
2016-09-21 21:09:12 +02:00
connect ( m_ui - > downloadsTreeView , & DownloadView : : openDir , this , & TrayWidget : : openDir ) ;
connect ( m_ui - > downloadsTreeView , & DownloadView : : openItemDir , this , & TrayWidget : : openItemDir ) ;
2016-08-26 16:43:53 +02:00
connect ( scanAllButton , & QPushButton : : clicked , & m_connection , & SyncthingConnection : : rescanAllDirs ) ;
connect ( viewIdButton , & QPushButton : : clicked , this , & TrayWidget : : showOwnDeviceId ) ;
2016-09-08 23:35:15 +02:00
connect ( showLogButton , & QPushButton : : clicked , this , & TrayWidget : : showLog ) ;
connect ( m_ui - > notificationsPushButton , & QPushButton : : clicked , this , & TrayWidget : : showNotifications ) ;
connect ( restartButton , & QPushButton : : clicked , & m_connection , & SyncthingConnection : : restart ) ;
2016-09-03 20:14:52 +02:00
connect ( m_connectionsActionGroup , & QActionGroup : : triggered , this , & TrayWidget : : handleConnectionSelected ) ;
2016-08-25 00:45:32 +02:00
}
TrayWidget : : ~ TrayWidget ( )
2016-09-24 16:19:23 +02:00
{
auto i = std : : find ( m_instances . begin ( ) , m_instances . end ( ) , this ) ;
if ( i ! = m_instances . end ( ) ) {
m_instances . erase ( i ) ;
}
if ( m_instances . empty ( ) ) {
QCoreApplication : : quit ( ) ;
}
}
2016-08-25 00:45:32 +02:00
void TrayWidget : : showSettingsDialog ( )
{
if ( ! m_settingsDlg ) {
m_settingsDlg = new SettingsDialog ( & m_connection , this ) ;
2016-09-25 20:54:09 +02:00
connect ( m_settingsDlg , & SettingsDialog : : applied , & TrayWidget : : applySettings ) ;
2016-08-25 00:45:32 +02:00
}
centerWidget ( m_settingsDlg ) ;
2016-09-08 23:35:15 +02:00
showDialog ( m_settingsDlg ) ;
2016-08-25 00:45:32 +02:00
}
void TrayWidget : : showAboutDialog ( )
{
if ( ! m_aboutDlg ) {
2016-09-01 16:34:30 +02:00
m_aboutDlg = new AboutDialog ( this , QString ( ) , QStringLiteral ( APP_AUTHOR " \n fallback icons from KDE/Breeze project \n Syncthing icons from Syncthing project " ) , QString ( ) , QString ( ) , QStringLiteral ( APP_DESCRIPTION ) , QImage ( QStringLiteral ( " :/icons/hicolor/scalable/app/syncthingtray.svg " ) ) ) ;
m_aboutDlg - > setWindowTitle ( tr ( " About " ) + QStringLiteral ( " - " APP_NAME ) ) ;
2016-08-25 00:45:32 +02:00
m_aboutDlg - > setWindowIcon ( QIcon ( QStringLiteral ( " :/icons/hicolor/scalable/app/syncthingtray.svg " ) ) ) ;
}
centerWidget ( m_aboutDlg ) ;
2016-09-08 23:35:15 +02:00
showDialog ( m_aboutDlg ) ;
2016-08-25 00:45:32 +02:00
}
void TrayWidget : : showWebUi ( )
{
# ifndef SYNCTHINGTRAY_NO_WEBVIEW
if ( Settings : : webViewDisabled ( ) ) {
# endif
2016-09-03 20:14:52 +02:00
QDesktopServices : : openUrl ( m_connection . syncthingUrl ( ) ) ;
2016-08-25 00:45:32 +02:00
# ifndef SYNCTHINGTRAY_NO_WEBVIEW
} else {
if ( ! m_webViewDlg ) {
m_webViewDlg = new WebViewDialog ( this ) ;
2016-09-03 20:14:52 +02:00
if ( m_selectedConnection ) {
m_webViewDlg - > applySettings ( * m_selectedConnection ) ;
2016-08-25 00:45:32 +02:00
}
2016-09-03 20:14:52 +02:00
connect ( m_webViewDlg , & WebViewDialog : : destroyed , this , & TrayWidget : : handleWebViewDeleted ) ;
2016-08-25 00:45:32 +02:00
}
2016-09-08 23:35:15 +02:00
showDialog ( m_webViewDlg ) ;
2016-08-25 00:45:32 +02:00
}
# endif
}
void TrayWidget : : showOwnDeviceId ( )
{
2016-09-03 19:39:43 +02:00
auto * dlg = new QWidget ( this , Qt : : Window ) ;
2016-09-01 16:34:30 +02:00
dlg - > setWindowTitle ( tr ( " Own device ID " ) + QStringLiteral ( " - " APP_NAME ) ) ;
2016-08-25 00:45:32 +02:00
dlg - > setWindowIcon ( QIcon ( QStringLiteral ( " :/icons/hicolor/scalable/app/syncthingtray.svg " ) ) ) ;
dlg - > setAttribute ( Qt : : WA_DeleteOnClose ) ;
dlg - > setBackgroundRole ( QPalette : : Background ) ;
auto * layout = new QVBoxLayout ( dlg ) ;
layout - > setAlignment ( Qt : : AlignCenter ) ;
auto * pixmapLabel = new QLabel ( dlg ) ;
pixmapLabel - > setAlignment ( Qt : : AlignCenter ) ;
layout - > addWidget ( pixmapLabel ) ;
auto * textLabel = new QLabel ( dlg ) ;
textLabel - > setText ( m_connection . myId ( ) . isEmpty ( ) ? tr ( " device ID is unknown " ) : m_connection . myId ( ) ) ;
QFont defaultFont = textLabel - > font ( ) ;
defaultFont . setBold ( true ) ;
defaultFont . setPointSize ( defaultFont . pointSize ( ) + 2 ) ;
textLabel - > setFont ( defaultFont ) ;
textLabel - > setAlignment ( Qt : : AlignCenter ) ;
layout - > addWidget ( textLabel ) ;
auto * copyPushButton = new QPushButton ( dlg ) ;
copyPushButton - > setText ( tr ( " Copy to clipboard " ) ) ;
connect ( copyPushButton , & QPushButton : : clicked , bind ( & QClipboard : : setText , QGuiApplication : : clipboard ( ) , m_connection . myId ( ) , QClipboard : : Clipboard ) ) ;
layout - > addWidget ( copyPushButton ) ;
2016-09-03 19:39:43 +02:00
connect ( dlg , & QWidget : : destroyed ,
bind ( static_cast < bool ( * ) ( const QMetaObject : : Connection & ) > ( & QObject : : disconnect ) ,
2016-09-30 23:55:25 +02:00
m_connection . requestQrCode ( m_connection . myId ( ) , [ pixmapLabel ] ( const QByteArray & data ) {
QPixmap pixmap ;
pixmap . loadFromData ( data ) ;
pixmapLabel - > setPixmap ( pixmap ) ;
} ) ) ) ;
2016-08-25 00:45:32 +02:00
dlg - > setLayout ( layout ) ;
centerWidget ( dlg ) ;
2016-09-08 23:35:15 +02:00
showDialog ( dlg ) ;
2016-08-25 00:45:32 +02:00
}
void TrayWidget : : showLog ( )
{
2016-09-08 23:35:15 +02:00
auto * dlg = new TextViewDialog ( tr ( " Log " ) , this ) ;
2016-09-03 19:39:43 +02:00
connect ( dlg , & QWidget : : destroyed ,
bind ( static_cast < bool ( * ) ( const QMetaObject : : Connection & ) > ( & QObject : : disconnect ) ,
2016-09-08 23:35:15 +02:00
m_connection . requestLog ( [ dlg ] ( const std : : vector < SyncthingLogEntry > & entries ) {
2016-09-03 19:39:43 +02:00
for ( const SyncthingLogEntry & entry : entries ) {
2016-09-08 23:35:15 +02:00
dlg - > browser ( ) - > append ( entry . when % QChar ( ' : ' ) % QChar ( ' ' ) % QChar ( ' \n ' ) % entry . message % QChar ( ' \n ' ) ) ;
2016-09-03 19:39:43 +02:00
}
} )
) ) ;
2016-09-08 23:35:15 +02:00
showDialog ( dlg ) ;
}
void TrayWidget : : showNotifications ( )
{
auto * dlg = new TextViewDialog ( tr ( " New notifications " ) , this ) ;
for ( const SyncthingLogEntry & entry : m_notifications ) {
dlg - > browser ( ) - > append ( entry . when % QChar ( ' : ' ) % QChar ( ' ' ) % QChar ( ' \n ' ) % entry . message % QChar ( ' \n ' ) ) ;
2016-08-25 00:45:32 +02:00
}
2016-09-08 23:35:15 +02:00
m_notifications . clear ( ) ;
showDialog ( dlg ) ;
2016-09-27 21:20:17 +02:00
m_connection . considerAllNotificationsRead ( ) ;
2016-09-08 23:35:15 +02:00
m_ui - > notificationsPushButton - > setHidden ( true ) ;
2016-08-25 00:45:32 +02:00
}
2016-09-24 16:19:23 +02:00
void TrayWidget : : quitTray ( )
{
QObject * parent ;
if ( m_menu ) {
if ( m_menu - > icon ( ) ) {
parent = m_menu - > icon ( ) ;
} else {
parent = m_menu ;
}
} else {
parent = this ;
}
parent - > deleteLater ( ) ;
}
2016-09-06 22:55:49 +02:00
void TrayWidget : : handleStatusChanged ( SyncthingStatus status )
2016-08-25 00:45:32 +02:00
{
switch ( status ) {
case SyncthingStatus : : Disconnected :
m_ui - > statusPushButton - > setText ( tr ( " Connect " ) ) ;
m_ui - > statusPushButton - > setToolTip ( tr ( " Not connected to Syncthing, click to connect " ) ) ;
2016-08-29 20:51:30 +02:00
m_ui - > statusPushButton - > setIcon ( QIcon : : fromTheme ( QStringLiteral ( " view-refresh " ) , QIcon ( QStringLiteral ( " :/icons/hicolor/scalable/actions/view-refresh.svg " ) ) ) ) ;
2016-09-06 22:55:49 +02:00
m_ui - > statusPushButton - > setHidden ( false ) ;
updateTraffic ( ) ; // ensure previous traffic statistics are no longer shown
break ;
case SyncthingStatus : : Reconnecting :
m_ui - > statusPushButton - > setHidden ( true ) ;
2016-08-25 00:45:32 +02:00
break ;
2016-09-03 19:39:43 +02:00
case SyncthingStatus : : Idle :
case SyncthingStatus : : Scanning :
2016-08-25 00:45:32 +02:00
case SyncthingStatus : : NotificationsAvailable :
case SyncthingStatus : : Synchronizing :
m_ui - > statusPushButton - > setText ( tr ( " Pause " ) ) ;
2016-08-26 16:43:53 +02:00
m_ui - > statusPushButton - > setToolTip ( tr ( " Syncthing is running, click to pause all devices " ) ) ;
2016-08-29 20:51:30 +02:00
m_ui - > statusPushButton - > setIcon ( QIcon : : fromTheme ( QStringLiteral ( " media-playback-pause " ) , QIcon ( QStringLiteral ( " :/icons/hicolor/scalable/actions/media-playback-pause.svg " ) ) ) ) ;
2016-09-06 22:55:49 +02:00
m_ui - > statusPushButton - > setHidden ( false ) ;
2016-08-25 00:45:32 +02:00
break ;
case SyncthingStatus : : Paused :
m_ui - > statusPushButton - > setText ( tr ( " Continue " ) ) ;
2016-08-26 16:43:53 +02:00
m_ui - > statusPushButton - > setToolTip ( tr ( " At least one device is paused, click to resume " ) ) ;
2016-08-29 20:51:30 +02:00
m_ui - > statusPushButton - > setIcon ( QIcon : : fromTheme ( QStringLiteral ( " media-playback-start " ) , QIcon ( QStringLiteral ( " :/icons/hicolor/scalable/actions/media-playback-resume.svg " ) ) ) ) ;
2016-09-06 22:55:49 +02:00
m_ui - > statusPushButton - > setHidden ( false ) ;
2016-08-25 00:45:32 +02:00
break ;
}
}
void TrayWidget : : applySettings ( )
{
2016-09-25 20:54:09 +02:00
for ( TrayWidget * instance : m_instances ) {
// update connections menu
int connectionIndex = 0 ;
const int connectionCount = static_cast < int > ( 1 + Settings : : secondaryConnectionSettings ( ) . size ( ) ) ;
const QList < QAction * > connectionActions = instance - > m_connectionsActionGroup - > actions ( ) ;
instance - > m_selectedConnection = nullptr ;
for ( ; connectionIndex < connectionCount ; + + connectionIndex ) {
2016-09-29 21:19:54 +02:00
SyncthingConnectionSettings & connectionSettings = ( connectionIndex = = 0 ? Settings : : primaryConnectionSettings ( ) : Settings : : secondaryConnectionSettings ( ) [ static_cast < size_t > ( connectionIndex - 1 ) ] ) ;
2016-09-25 20:54:09 +02:00
if ( connectionIndex < connectionActions . size ( ) ) {
QAction * action = connectionActions . at ( connectionIndex ) ;
action - > setText ( connectionSettings . label ) ;
if ( action - > isChecked ( ) ) {
instance - > m_selectedConnection = & connectionSettings ;
}
} else {
QAction * action = instance - > m_connectionsMenu - > addAction ( connectionSettings . label ) ;
action - > setCheckable ( true ) ;
instance - > m_connectionsActionGroup - > addAction ( action ) ;
2016-09-03 20:14:52 +02:00
}
}
2016-09-25 20:54:09 +02:00
for ( ; connectionIndex < connectionActions . size ( ) ; + + connectionIndex ) {
delete connectionActions . at ( connectionIndex ) ;
}
if ( ! instance - > m_selectedConnection ) {
instance - > m_selectedConnection = & Settings : : primaryConnectionSettings ( ) ;
instance - > m_connectionsMenu - > actions ( ) . at ( 0 ) - > setChecked ( true ) ;
instance - > m_ui - > connectionsPushButton - > setText ( instance - > m_selectedConnection - > label ) ;
}
2016-09-03 20:14:52 +02:00
2016-09-25 20:54:09 +02:00
instance - > m_connection . reconnect ( * instance - > m_selectedConnection ) ;
2016-09-03 20:14:52 +02:00
2016-09-25 20:54:09 +02:00
// web view
2016-09-03 20:14:52 +02:00
# ifndef SYNCTHINGTRAY_NO_WEBVIEW
2016-09-25 20:54:09 +02:00
if ( instance - > m_webViewDlg ) {
instance - > m_webViewDlg - > applySettings ( * instance - > m_selectedConnection ) ;
}
2016-09-03 20:14:52 +02:00
# endif
2016-09-25 20:54:09 +02:00
// update visual appearance
instance - > m_ui - > trafficFormWidget - > setVisible ( Settings : : showTraffic ( ) ) ;
if ( Settings : : showTraffic ( ) ) {
instance - > updateTraffic ( ) ;
}
instance - > m_ui - > infoFrame - > setFrameStyle ( Settings : : frameStyle ( ) ) ;
instance - > m_ui - > buttonsFrame - > setFrameStyle ( Settings : : frameStyle ( ) ) ;
if ( QApplication : : style ( ) & & ! QApplication : : style ( ) - > objectName ( ) . compare ( QLatin1String ( " adwaita " ) , Qt : : CaseInsensitive ) ) {
instance - > m_cornerFrame - > setFrameStyle ( QFrame : : NoFrame ) ;
} else {
instance - > m_cornerFrame - > setFrameStyle ( Settings : : frameStyle ( ) ) ;
}
2016-09-03 19:39:43 +02:00
}
2016-08-25 00:45:32 +02:00
}
2016-09-21 21:09:12 +02:00
void TrayWidget : : openDir ( const SyncthingDir & dir )
2016-08-25 00:45:32 +02:00
{
2016-09-21 21:09:12 +02:00
if ( QDir ( dir . path ) . exists ( ) ) {
DesktopUtils : : openLocalFileOrDir ( dir . path ) ;
} else {
QMessageBox : : warning ( this , QCoreApplication : : applicationName ( ) , tr ( " The directory <i>%1</i> does not exist on the local machine. " ) . arg ( dir . path ) ) ;
2016-08-25 00:45:32 +02:00
}
}
2016-09-21 21:09:12 +02:00
void TrayWidget : : openItemDir ( const SyncthingItemDownloadProgress & item )
2016-08-25 00:45:32 +02:00
{
2016-09-21 21:09:12 +02:00
if ( item . fileInfo . exists ( ) ) {
DesktopUtils : : openLocalFileOrDir ( item . fileInfo . path ( ) ) ;
} else {
QMessageBox : : warning ( this , QCoreApplication : : applicationName ( ) , tr ( " The file <i>%1</i> does not exist on the local machine. " ) . arg ( item . fileInfo . filePath ( ) ) ) ;
2016-08-25 00:45:32 +02:00
}
}
2016-09-21 21:09:12 +02:00
void TrayWidget : : scanDir ( const SyncthingDir & dir )
2016-08-26 16:43:53 +02:00
{
2016-09-21 21:09:12 +02:00
m_connection . rescan ( dir . id ) ;
}
void TrayWidget : : pauseResumeDev ( const SyncthingDev & dev )
{
if ( dev . paused ) {
m_connection . resume ( dev . id ) ;
} else {
m_connection . pause ( dev . id ) ;
2016-08-26 16:43:53 +02:00
}
}
2016-08-29 20:51:30 +02:00
void TrayWidget : : changeStatus ( )
2016-08-25 00:45:32 +02:00
{
switch ( m_connection . status ( ) ) {
case SyncthingStatus : : Disconnected :
m_connection . connect ( ) ;
break ;
2016-09-06 22:55:49 +02:00
case SyncthingStatus : : Reconnecting :
break ;
2016-09-03 19:39:43 +02:00
case SyncthingStatus : : Idle :
case SyncthingStatus : : Scanning :
2016-08-25 00:45:32 +02:00
case SyncthingStatus : : NotificationsAvailable :
case SyncthingStatus : : Synchronizing :
m_connection . pauseAllDevs ( ) ;
break ;
case SyncthingStatus : : Paused :
m_connection . resumeAllDevs ( ) ;
break ;
}
}
2016-08-30 20:01:07 +02:00
void TrayWidget : : updateTraffic ( )
2016-08-25 00:45:32 +02:00
{
2016-09-08 23:35:15 +02:00
if ( m_ui - > trafficFormWidget - > isHidden ( ) ) {
2016-08-30 20:01:07 +02:00
return ;
2016-08-25 00:45:32 +02:00
}
2016-09-06 22:55:49 +02:00
static const QString unknownStr ( tr ( " unknown " ) ) ;
if ( m_connection . isConnected ( ) ) {
if ( m_connection . totalIncomingRate ( ) ! = 0.0 ) {
m_ui - > inTrafficLabel - > setText ( m_connection . totalIncomingTraffic ( ) > = 0
? QStringLiteral ( " %1 (%2) " ) . arg ( QString : : fromUtf8 ( bitrateToString ( m_connection . totalIncomingRate ( ) , true ) . data ( ) ) , QString : : fromUtf8 ( dataSizeToString ( m_connection . totalIncomingTraffic ( ) ) . data ( ) ) )
: QString : : fromUtf8 ( bitrateToString ( m_connection . totalIncomingRate ( ) , true ) . data ( ) ) ) ;
} else {
m_ui - > inTrafficLabel - > setText ( m_connection . totalIncomingTraffic ( ) > = 0 ? QString : : fromUtf8 ( dataSizeToString ( m_connection . totalIncomingTraffic ( ) ) . data ( ) ) : unknownStr ) ;
}
if ( m_connection . totalOutgoingRate ( ) ! = 0.0 ) {
m_ui - > outTrafficLabel - > setText ( m_connection . totalIncomingTraffic ( ) > = 0
? QStringLiteral ( " %1 (%2) " ) . arg ( QString : : fromUtf8 ( bitrateToString ( m_connection . totalOutgoingRate ( ) , true ) . data ( ) ) , QString : : fromUtf8 ( dataSizeToString ( m_connection . totalOutgoingTraffic ( ) ) . data ( ) ) )
: QString : : fromUtf8 ( bitrateToString ( m_connection . totalOutgoingRate ( ) , true ) . data ( ) ) ) ;
} else {
m_ui - > outTrafficLabel - > setText ( m_connection . totalOutgoingTraffic ( ) > = 0 ? QString : : fromUtf8 ( dataSizeToString ( m_connection . totalOutgoingTraffic ( ) ) . data ( ) ) : unknownStr ) ;
}
2016-08-30 20:01:07 +02:00
} else {
2016-09-06 22:55:49 +02:00
m_ui - > inTrafficLabel - > setText ( unknownStr ) ;
m_ui - > outTrafficLabel - > setText ( unknownStr ) ;
2016-08-25 00:45:32 +02:00
}
}
2016-09-03 20:14:52 +02:00
# ifndef SYNCTHINGTRAY_NO_WEBVIEW
2016-08-30 20:01:07 +02:00
void TrayWidget : : handleWebViewDeleted ( )
2016-08-25 00:45:32 +02:00
{
2016-08-30 20:01:07 +02:00
m_webViewDlg = nullptr ;
2016-08-25 00:45:32 +02:00
}
2016-09-03 20:14:52 +02:00
# endif
2016-08-25 00:45:32 +02:00
2016-09-08 23:35:15 +02:00
void TrayWidget : : handleNewNotification ( DateTime when , const QString & msg )
2016-09-01 16:34:30 +02:00
{
2016-09-08 23:35:15 +02:00
m_notifications . emplace_back ( QString : : fromLocal8Bit ( when . toString ( DateTimeOutputFormat : : DateAndTime , true ) . data ( ) ) , msg ) ;
m_ui - > notificationsPushButton - > setHidden ( false ) ;
2016-09-01 16:34:30 +02:00
}
2016-09-03 20:14:52 +02:00
void TrayWidget : : handleConnectionSelected ( QAction * connectionAction )
{
int index = m_connectionsMenu - > actions ( ) . indexOf ( connectionAction ) ;
if ( index > = 0 ) {
m_selectedConnection = ( index = = 0 )
? & Settings : : primaryConnectionSettings ( )
: & Settings : : secondaryConnectionSettings ( ) [ static_cast < size_t > ( index - 1 ) ] ;
2016-09-17 19:13:49 +02:00
m_ui - > connectionsPushButton - > setText ( m_selectedConnection - > label ) ;
2016-09-03 20:14:52 +02:00
m_connection . reconnect ( * m_selectedConnection ) ;
# ifndef SYNCTHINGTRAY_NO_WEBVIEW
if ( m_webViewDlg ) {
m_webViewDlg - > applySettings ( * m_selectedConnection ) ;
}
# endif
}
}
2016-09-08 23:35:15 +02:00
void TrayWidget : : showDialog ( QWidget * dlg )
{
if ( m_menu ) {
m_menu - > close ( ) ;
}
dlg - > show ( ) ;
dlg - > activateWindow ( ) ;
}
2016-08-25 00:45:32 +02:00
}