2014-11-05 02:54:06 +03:00
/*
* Bittorrent Client using Qt4 and libtorrent .
* Copyright ( C ) 2006 Christophe Dumez
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation ; either version 2
* of the License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
*
* In addition , as a special exception , the copyright holders give permission to
* link this program with the OpenSSL project ' s " OpenSSL " library ( or with
* modified versions of it that use the same license as the " OpenSSL " library ) ,
* and distribute the linked executables . You must obey the GNU General Public
* License in all respects for all of the code used other than " OpenSSL " . If you
* modify file ( s ) , you may extend this exception to your version of the file ( s ) ,
* but you are not obligated to do so . If you do not wish to do so , delete this
* exception statement from your version .
*
* Contact : chris @ qbittorrent . org
*/
# include "statusbar.h"
2015-12-01 19:25:25 +03:00
# include <QStatusBar>
# include <QFrame>
# include <QLabel>
# include <QTimer>
# include <QPushButton>
# include <QHBoxLayout>
2014-11-05 02:54:06 +03:00
# include <QFontMetrics>
# include <QDebug>
2015-09-25 11:10:05 +03:00
# include "base/bittorrent/session.h"
# include "base/bittorrent/sessionstatus.h"
2014-11-05 02:54:06 +03:00
# include "speedlimitdlg.h"
2015-04-19 18:17:47 +03:00
# include "guiiconprovider.h"
2015-09-25 11:10:05 +03:00
# include "base/preferences.h"
# include "base/utils/misc.h"
# include "base/logger.h"
2014-11-05 02:54:06 +03:00
StatusBar : : StatusBar ( QStatusBar * bar )
2015-12-01 19:25:25 +03:00
: m_bar ( bar )
2014-11-05 02:54:06 +03:00
{
2015-12-01 19:25:25 +03:00
qApp - > setStyleSheet ( " QStatusBar::item { border-width: 0; } " ) ;
2015-12-01 19:19:39 +03:00
2015-12-01 19:25:25 +03:00
Preferences * const pref = Preferences : : instance ( ) ;
connect ( BitTorrent : : Session : : instance ( ) , SIGNAL ( speedLimitModeChanged ( bool ) ) , this , SLOT ( updateAltSpeedsBtn ( bool ) ) ) ;
2015-12-27 10:27:06 +03:00
m_container = new QWidget ( bar ) ;
m_layout = new QHBoxLayout ( m_container ) ;
m_layout - > setContentsMargins ( 0 , 0 , 0 , 0 ) ;
2014-11-05 02:54:06 +03:00
2015-12-27 10:27:06 +03:00
m_container - > setLayout ( m_layout ) ;
m_connecStatusLblIcon = new QPushButton ( bar ) ;
m_connecStatusLblIcon - > setFlat ( true ) ;
m_connecStatusLblIcon - > setFocusPolicy ( Qt : : NoFocus ) ;
m_connecStatusLblIcon - > setCursor ( Qt : : PointingHandCursor ) ;
m_connecStatusLblIcon - > setIcon ( QIcon ( " :/icons/skin/firewalled.png " ) ) ;
m_connecStatusLblIcon - > setToolTip ( QString : : fromUtf8 ( " <b> " ) + tr ( " Connection status: " ) + QString : : fromUtf8 ( " </b><br> " ) + QString : : fromUtf8 ( " <i> " ) + tr ( " No direct connections. This may indicate network configuration problems. " ) + QString : : fromUtf8 ( " </i> " ) ) ;
2016-01-13 22:44:06 +03:00
2015-12-27 10:27:06 +03:00
m_dlSpeedLbl = new QPushButton ( bar ) ;
m_dlSpeedLbl - > setIcon ( QIcon ( " :/icons/skin/download.png " ) ) ;
connect ( m_dlSpeedLbl , SIGNAL ( clicked ( ) ) , this , SLOT ( capDownloadSpeed ( ) ) ) ;
m_dlSpeedLbl - > setFlat ( true ) ;
m_dlSpeedLbl - > setFocusPolicy ( Qt : : NoFocus ) ;
m_dlSpeedLbl - > setCursor ( Qt : : PointingHandCursor ) ;
2016-01-13 22:44:06 +03:00
m_dlSpeedLbl - > setStyleSheet ( " text-align:left; " ) ;
m_dlSpeedLbl - > setMinimumWidth ( 200 ) ;
2014-11-05 02:54:06 +03:00
2015-12-27 10:27:06 +03:00
m_upSpeedLbl = new QPushButton ( bar ) ;
m_upSpeedLbl - > setIcon ( QIcon ( " :/icons/skin/seeding.png " ) ) ;
connect ( m_upSpeedLbl , SIGNAL ( clicked ( ) ) , this , SLOT ( capUploadSpeed ( ) ) ) ;
m_upSpeedLbl - > setFlat ( true ) ;
m_upSpeedLbl - > setFocusPolicy ( Qt : : NoFocus ) ;
m_upSpeedLbl - > setCursor ( Qt : : PointingHandCursor ) ;
2016-01-13 22:44:06 +03:00
m_upSpeedLbl - > setStyleSheet ( " text-align:left; " ) ;
m_upSpeedLbl - > setMinimumWidth ( 200 ) ;
2015-12-27 10:27:06 +03:00
m_DHTLbl = new QLabel ( tr ( " DHT: %1 nodes " ) . arg ( 0 ) , bar ) ;
m_DHTLbl - > setSizePolicy ( QSizePolicy : : Maximum , QSizePolicy : : Preferred ) ;
2014-11-03 00:44:24 +03:00
2015-12-27 10:27:06 +03:00
m_altSpeedsBtn = new QPushButton ( bar ) ;
m_altSpeedsBtn - > setFlat ( true ) ;
m_altSpeedsBtn - > setFocusPolicy ( Qt : : NoFocus ) ;
m_altSpeedsBtn - > setCursor ( Qt : : PointingHandCursor ) ;
2015-12-01 19:25:25 +03:00
updateAltSpeedsBtn ( pref - > isAltBandwidthEnabled ( ) ) ;
2015-12-27 10:27:06 +03:00
connect ( m_altSpeedsBtn , SIGNAL ( clicked ( ) ) , this , SLOT ( toggleAlternativeSpeeds ( ) ) ) ;
2014-10-21 20:00:26 +04:00
2015-12-01 19:25:25 +03:00
// Because on some platforms the default icon size is bigger
// and it will result in taller/fatter statusbar, even if the
// icons are actually 16x16
2015-12-27 10:27:06 +03:00
m_connecStatusLblIcon - > setIconSize ( QSize ( 16 , 16 ) ) ;
m_dlSpeedLbl - > setIconSize ( QSize ( 16 , 16 ) ) ;
m_upSpeedLbl - > setIconSize ( QSize ( 16 , 16 ) ) ;
m_altSpeedsBtn - > setIconSize ( QSize ( 28 , 16 ) ) ;
2014-11-29 20:44:46 +03:00
2015-12-01 19:25:25 +03:00
// Set to the known maximum width(plus some padding)
// so the speed widgets will take the rest of the space
2015-12-27 10:27:06 +03:00
m_connecStatusLblIcon - > setMaximumWidth ( 16 + 6 ) ;
m_altSpeedsBtn - > setMaximumWidth ( 28 + 6 ) ;
2014-11-03 00:44:24 +03:00
2015-12-27 10:27:06 +03:00
m_statusSep1 = new QFrame ( bar ) ;
m_statusSep1 - > setFrameStyle ( QFrame : : VLine ) ;
m_statusSep1 - > setFrameShadow ( QFrame : : Raised ) ;
m_statusSep2 = new QFrame ( bar ) ;
m_statusSep2 - > setFrameStyle ( QFrame : : VLine ) ;
m_statusSep2 - > setFrameShadow ( QFrame : : Raised ) ;
m_statusSep3 = new QFrame ( bar ) ;
m_statusSep3 - > setFrameStyle ( QFrame : : VLine ) ;
m_statusSep3 - > setFrameShadow ( QFrame : : Raised ) ;
m_statusSep4 = new QFrame ( bar ) ;
m_statusSep4 - > setFrameStyle ( QFrame : : VLine ) ;
m_statusSep4 - > setFrameShadow ( QFrame : : Raised ) ;
m_layout - > addWidget ( m_DHTLbl ) ;
m_layout - > addWidget ( m_statusSep1 ) ;
m_layout - > addWidget ( m_connecStatusLblIcon ) ;
m_layout - > addWidget ( m_statusSep2 ) ;
m_layout - > addWidget ( m_altSpeedsBtn ) ;
m_layout - > addWidget ( m_statusSep4 ) ;
m_layout - > addWidget ( m_dlSpeedLbl ) ;
m_layout - > addWidget ( m_statusSep3 ) ;
m_layout - > addWidget ( m_upSpeedLbl ) ;
2014-11-05 02:54:06 +03:00
2015-12-27 10:27:06 +03:00
bar - > addPermanentWidget ( m_container ) ;
2015-12-01 19:25:25 +03:00
bar - > setStyleSheet ( " QWidget {margin: 0;} " ) ;
2015-12-27 10:27:06 +03:00
m_container - > adjustSize ( ) ;
2015-12-01 19:25:25 +03:00
bar - > adjustSize ( ) ;
// Is DHT enabled
2015-12-27 10:27:06 +03:00
m_DHTLbl - > setVisible ( pref - > isDHTEnabled ( ) ) ;
m_refreshTimer = new QTimer ( bar ) ;
2015-12-01 19:25:25 +03:00
refreshStatusBar ( ) ;
2015-12-27 10:27:06 +03:00
connect ( m_refreshTimer , SIGNAL ( timeout ( ) ) , this , SLOT ( refreshStatusBar ( ) ) ) ;
m_refreshTimer - > start ( 1500 ) ;
2014-11-05 02:54:06 +03:00
}
2015-12-01 19:25:25 +03:00
StatusBar : : ~ StatusBar ( )
{
qDebug ( ) < < Q_FUNC_INFO ;
2014-11-05 02:54:06 +03:00
}
2015-12-01 19:25:25 +03:00
QPushButton * StatusBar : : connectionStatusButton ( ) const
{
2015-12-27 10:27:06 +03:00
return m_connecStatusLblIcon ;
2014-11-05 02:54:06 +03:00
}
2015-12-01 19:25:25 +03:00
void StatusBar : : showRestartRequired ( )
{
// Restart required notification
2015-12-27 10:27:06 +03:00
const QString restartText = tr ( " qBittorrent needs to be restarted " ) ;
2015-12-01 19:25:25 +03:00
QLabel * restartIconLbl = new QLabel ( m_bar ) ;
2015-12-27 10:27:06 +03:00
restartIconLbl - > setPixmap ( QPixmap ( " :/icons/oxygen/dialog-warning.png " ) . scaled ( QSize ( 24 , 24 ) ) ) ;
restartIconLbl - > setToolTip ( restartText ) ;
2015-12-01 19:25:25 +03:00
m_bar - > insertWidget ( 0 , restartIconLbl ) ;
QLabel * restartLbl = new QLabel ( m_bar ) ;
restartLbl - > setSizePolicy ( QSizePolicy : : MinimumExpanding , QSizePolicy : : Preferred ) ;
m_bar - > insertWidget ( 1 , restartLbl ) ;
QFontMetrics fm ( restartLbl - > font ( ) ) ;
2015-12-27 10:27:06 +03:00
restartLbl - > setText ( fm . elidedText ( restartText , Qt : : ElideRight , restartLbl - > width ( ) ) ) ;
2015-12-01 19:25:25 +03:00
Logger : : instance ( ) - > addMessage ( tr ( " qBittorrent was just updated and needs to be restarted for the changes to be effective. " ) , Log : : CRITICAL ) ;
2014-11-05 02:54:06 +03:00
}
2015-12-01 19:25:25 +03:00
void StatusBar : : stopTimer ( )
{
2015-12-27 10:27:06 +03:00
m_refreshTimer - > stop ( ) ;
2014-11-05 02:54:06 +03:00
}
2015-12-27 10:49:53 +03:00
void StatusBar : : updateConnectionStatus ( const BitTorrent : : SessionStatus & sessionStatus )
2015-12-01 19:25:25 +03:00
{
if ( ! BitTorrent : : Session : : instance ( ) - > isListening ( ) ) {
2015-12-27 10:27:06 +03:00
m_connecStatusLblIcon - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/icons/skin/disconnected.png " ) ) ) ;
m_connecStatusLblIcon - > setToolTip ( QString : : fromUtf8 ( " <b> " ) + tr ( " Connection Status: " ) + QString : : fromUtf8 ( " </b><br> " ) + tr ( " Offline. This usually means that qBittorrent failed to listen on the selected port for incoming connections. " ) ) ;
2015-12-01 19:25:25 +03:00
}
else {
if ( sessionStatus . hasIncomingConnections ( ) ) {
// Connection OK
2015-12-27 10:27:06 +03:00
m_connecStatusLblIcon - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/icons/skin/connected.png " ) ) ) ;
m_connecStatusLblIcon - > setToolTip ( QString : : fromUtf8 ( " <b> " ) + tr ( " Connection Status: " ) + QString : : fromUtf8 ( " </b><br> " ) + tr ( " Online " ) ) ;
2015-12-01 19:25:25 +03:00
}
else {
2015-12-27 10:27:06 +03:00
m_connecStatusLblIcon - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/icons/skin/firewalled.png " ) ) ) ;
m_connecStatusLblIcon - > setToolTip ( QString : : fromUtf8 ( " <b> " ) + tr ( " Connection status: " ) + QString : : fromUtf8 ( " </b><br> " ) + QString : : fromUtf8 ( " <i> " ) + tr ( " No direct connections. This may indicate network configuration problems. " ) + QString : : fromUtf8 ( " </i> " ) ) ;
2015-12-01 19:25:25 +03:00
}
}
2015-12-27 10:49:53 +03:00
}
void StatusBar : : updateDHTNodesNumber ( const BitTorrent : : SessionStatus & sessionStatus )
{
2015-12-01 19:25:25 +03:00
if ( BitTorrent : : Session : : instance ( ) - > isDHTEnabled ( ) ) {
2015-12-27 10:27:06 +03:00
m_DHTLbl - > setVisible ( true ) ;
m_DHTLbl - > setText ( tr ( " DHT: %1 nodes " ) . arg ( QString : : number ( sessionStatus . dhtNodes ( ) ) ) ) ;
2014-11-05 02:54:06 +03:00
}
2015-12-01 19:25:25 +03:00
else {
2015-12-27 10:27:06 +03:00
m_DHTLbl - > setVisible ( false ) ;
2015-12-01 19:25:25 +03:00
}
2015-12-27 10:49:53 +03:00
}
void StatusBar : : updateSpeedLabels ( const BitTorrent : : SessionStatus & sessionStatus )
{
2016-01-13 22:44:06 +03:00
QString speedLbl = Utils : : Misc : : friendlyUnit ( sessionStatus . payloadDownloadRate ( ) , true ) ;
2015-12-01 19:25:25 +03:00
int speedLimit = BitTorrent : : Session : : instance ( ) - > downloadRateLimit ( ) ;
if ( speedLimit )
2016-01-13 22:44:06 +03:00
speedLbl + = " [ " + Utils : : Misc : : friendlyUnit ( speedLimit , true ) + " ] " ;
speedLbl + = " ( " + Utils : : Misc : : friendlyUnit ( sessionStatus . totalPayloadDownload ( ) ) + " ) " ;
2015-12-27 10:27:06 +03:00
m_dlSpeedLbl - > setText ( speedLbl ) ;
2015-12-01 19:25:25 +03:00
speedLimit = BitTorrent : : Session : : instance ( ) - > uploadRateLimit ( ) ;
2016-01-13 22:44:06 +03:00
speedLbl = Utils : : Misc : : friendlyUnit ( sessionStatus . payloadUploadRate ( ) , true ) ;
2015-12-01 19:25:25 +03:00
if ( speedLimit )
2016-01-13 22:44:06 +03:00
speedLbl + = " [ " + Utils : : Misc : : friendlyUnit ( speedLimit , true ) + " ] " ;
speedLbl + = " ( " + Utils : : Misc : : friendlyUnit ( sessionStatus . totalPayloadUpload ( ) ) + " ) " ;
2015-12-27 10:27:06 +03:00
m_upSpeedLbl - > setText ( speedLbl ) ;
2014-11-05 02:54:06 +03:00
}
2015-12-27 10:49:53 +03:00
void StatusBar : : refreshStatusBar ( )
{
const BitTorrent : : SessionStatus sessionStatus = BitTorrent : : Session : : instance ( ) - > status ( ) ;
updateConnectionStatus ( sessionStatus ) ;
updateDHTNodesNumber ( sessionStatus ) ;
updateSpeedLabels ( sessionStatus ) ;
}
2015-12-01 19:25:25 +03:00
void StatusBar : : updateAltSpeedsBtn ( bool alternative )
{
if ( alternative ) {
2015-12-27 10:27:06 +03:00
m_altSpeedsBtn - > setIcon ( QIcon ( " :/icons/slow.png " ) ) ;
m_altSpeedsBtn - > setToolTip ( tr ( " Click to switch to regular speed limits " ) ) ;
m_altSpeedsBtn - > setDown ( true ) ;
2015-12-01 19:25:25 +03:00
}
else {
2015-12-27 10:27:06 +03:00
m_altSpeedsBtn - > setIcon ( QIcon ( " :/icons/slow_off.png " ) ) ;
m_altSpeedsBtn - > setToolTip ( tr ( " Click to switch to alternative speed limits " ) ) ;
m_altSpeedsBtn - > setDown ( false ) ;
2015-12-01 19:25:25 +03:00
}
refreshStatusBar ( ) ;
2014-11-05 02:54:06 +03:00
}
2015-12-01 19:25:25 +03:00
void StatusBar : : toggleAlternativeSpeeds ( )
{
Preferences * const pref = Preferences : : instance ( ) ;
if ( pref - > isSchedulerEnabled ( ) )
m_bar - > showMessage ( tr ( " Manual change of rate limits mode. The scheduler is disabled. " ) , 5000 ) ;
BitTorrent : : Session : : instance ( ) - > changeSpeedLimitMode ( ! pref - > isAltBandwidthEnabled ( ) ) ;
2014-11-05 02:54:06 +03:00
}
2015-12-01 19:25:25 +03:00
void StatusBar : : capDownloadSpeed ( )
{
bool ok = false ;
2015-12-27 10:27:06 +03:00
int curLimit = BitTorrent : : Session : : instance ( ) - > downloadRateLimit ( ) ;
long newLimit = SpeedLimitDialog : : askSpeedLimit ( & ok , tr ( " Global Download Speed Limit " ) , curLimit ) ;
2015-12-01 19:25:25 +03:00
if ( ok ) {
Preferences * const pref = Preferences : : instance ( ) ;
bool alt = pref - > isAltBandwidthEnabled ( ) ;
2015-12-27 10:27:06 +03:00
if ( newLimit < = 0 ) {
2015-12-01 19:25:25 +03:00
qDebug ( " Setting global download rate limit to Unlimited " ) ;
BitTorrent : : Session : : instance ( ) - > setDownloadRateLimit ( - 1 ) ;
if ( ! alt )
pref - > setGlobalDownloadLimit ( - 1 ) ;
else
pref - > setAltGlobalDownloadLimit ( - 1 ) ;
}
else {
2015-12-27 10:27:06 +03:00
qDebug ( " Setting global download rate limit to %.1fKb/s " , newLimit / 1024. ) ;
BitTorrent : : Session : : instance ( ) - > setDownloadRateLimit ( newLimit ) ;
2015-12-01 19:25:25 +03:00
if ( ! alt )
2015-12-27 10:27:06 +03:00
pref - > setGlobalDownloadLimit ( newLimit / 1024. ) ;
2015-12-01 19:25:25 +03:00
else
2015-12-27 10:27:06 +03:00
pref - > setAltGlobalDownloadLimit ( newLimit / 1024. ) ;
2015-12-01 19:25:25 +03:00
}
refreshStatusBar ( ) ;
2014-11-05 02:54:06 +03:00
}
}
2015-12-01 19:25:25 +03:00
void StatusBar : : capUploadSpeed ( )
{
bool ok = false ;
2015-12-27 10:27:06 +03:00
int curLimit = BitTorrent : : Session : : instance ( ) - > uploadRateLimit ( ) ;
long newLimit = SpeedLimitDialog : : askSpeedLimit ( & ok , tr ( " Global Upload Speed Limit " ) , curLimit ) ;
2015-12-01 19:25:25 +03:00
if ( ok ) {
Preferences * const pref = Preferences : : instance ( ) ;
bool alt = pref - > isAltBandwidthEnabled ( ) ;
2015-12-27 10:27:06 +03:00
if ( newLimit < = 0 ) {
2015-12-01 19:25:25 +03:00
qDebug ( " Setting global upload rate limit to Unlimited " ) ;
BitTorrent : : Session : : instance ( ) - > setUploadRateLimit ( - 1 ) ;
if ( ! alt )
Preferences : : instance ( ) - > setGlobalUploadLimit ( - 1 ) ;
else
Preferences : : instance ( ) - > setAltGlobalUploadLimit ( - 1 ) ;
}
else {
2015-12-27 10:27:06 +03:00
qDebug ( " Setting global upload rate limit to %.1fKb/s " , newLimit / 1024. ) ;
BitTorrent : : Session : : instance ( ) - > setUploadRateLimit ( newLimit ) ;
2015-12-01 19:25:25 +03:00
if ( ! alt )
2015-12-27 10:27:06 +03:00
Preferences : : instance ( ) - > setGlobalUploadLimit ( newLimit / 1024. ) ;
2015-12-01 19:25:25 +03:00
else
2015-12-27 10:27:06 +03:00
Preferences : : instance ( ) - > setAltGlobalUploadLimit ( newLimit / 1024. ) ;
2015-12-01 19:25:25 +03:00
}
refreshStatusBar ( ) ;
2014-11-05 02:54:06 +03:00
}
}