2009-11-18 16:18:55 +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
*/
# ifndef STATUSBAR_H
# define STATUSBAR_H
# include <QStatusBar>
# include <QFrame>
# include <QLabel>
# include <QTimer>
2009-11-18 17:54:33 +03:00
# include <QGridLayout>
2009-11-19 16:25:00 +03:00
# include <QPushButton>
2009-11-18 17:54:33 +03:00
# include <QHBoxLayout>
2010-10-26 21:54:04 +04:00
# include <QFontMetrics>
2011-03-14 22:18:52 +03:00
# include <QDebug>
2010-10-09 18:06:35 +04:00
# include "qbtsession.h"
2009-11-19 16:25:00 +03:00
# include "speedlimitdlg.h"
2011-03-13 16:35:08 +03:00
# include "iconprovider.h"
2009-11-19 16:25:00 +03:00
# include "preferences.h"
2009-11-18 16:18:55 +03:00
# include "misc.h"
class StatusBar : public QObject {
Q_OBJECT
public :
2011-03-13 15:48:53 +03:00
StatusBar ( QStatusBar * bar ) : m_bar ( bar ) {
2010-11-16 23:34:31 +03:00
Preferences pref ;
2010-11-14 00:15:52 +03:00
connect ( QBtSession : : instance ( ) , SIGNAL ( alternativeSpeedsModeChanged ( bool ) ) , this , SLOT ( updateAltSpeedsBtn ( bool ) ) ) ;
2011-03-13 15:48:53 +03:00
container = new QWidget ( bar ) ;
2011-03-13 16:35:08 +03:00
layout = new QHBoxLayout ( container ) ;
2009-11-18 17:54:33 +03:00
layout - > setContentsMargins ( 0 , 0 , 0 , 0 ) ;
container - > setLayout ( layout ) ;
2011-03-13 15:48:53 +03:00
connecStatusLblIcon = new QPushButton ( bar ) ;
2010-11-08 21:17:54 +03:00
connecStatusLblIcon - > setFlat ( true ) ;
2010-11-08 21:23:13 +03:00
connecStatusLblIcon - > setFocusPolicy ( Qt : : NoFocus ) ;
2011-03-13 16:35:08 +03:00
connecStatusLblIcon - > setFixedWidth ( 32 ) ;
2010-11-08 21:23:13 +03:00
connecStatusLblIcon - > setCursor ( Qt : : PointingHandCursor ) ;
2010-11-08 21:17:54 +03:00
connecStatusLblIcon - > setIcon ( QIcon ( " :/Icons/skin/firewalled.png " ) ) ;
2009-11-18 16:18:55 +03:00
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> " ) ) ;
2011-03-13 16:35:08 +03:00
dlSpeedLbl = new QPushButton ( bar ) ;
dlSpeedLbl - > setIconSize ( QSize ( 16 , 16 ) ) ;
dlSpeedLbl - > setIcon ( QIcon ( " :/Icons/skin/download.png " ) ) ;
2009-11-19 16:25:00 +03:00
//dlSpeedLbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
connect ( dlSpeedLbl , SIGNAL ( clicked ( ) ) , this , SLOT ( capDownloadSpeed ( ) ) ) ;
dlSpeedLbl - > setFlat ( true ) ;
2010-04-07 12:19:17 +04:00
dlSpeedLbl - > setFocusPolicy ( Qt : : NoFocus ) ;
2010-11-08 21:23:13 +03:00
dlSpeedLbl - > setCursor ( Qt : : PointingHandCursor ) ;
2010-01-23 23:44:09 +03:00
2011-03-13 15:48:53 +03:00
altSpeedsBtn = new QPushButton ( bar ) ;
2011-03-13 16:35:08 +03:00
altSpeedsBtn - > setFixedWidth ( 36 ) ;
2010-12-04 16:55:12 +03:00
altSpeedsBtn - > setIconSize ( QSize ( 32 , 32 ) ) ;
2010-04-07 12:19:17 +04:00
altSpeedsBtn - > setFlat ( true ) ;
altSpeedsBtn - > setFocusPolicy ( Qt : : NoFocus ) ;
2010-11-08 21:23:13 +03:00
altSpeedsBtn - > setCursor ( Qt : : PointingHandCursor ) ;
2010-11-16 23:34:31 +03:00
updateAltSpeedsBtn ( pref . isAltBandwidthEnabled ( ) ) ;
2010-01-23 23:44:09 +03:00
connect ( altSpeedsBtn , SIGNAL ( clicked ( ) ) , this , SLOT ( toggleAlternativeSpeeds ( ) ) ) ;
2011-03-13 16:35:08 +03:00
upSpeedLbl = new QPushButton ( bar ) ;
upSpeedLbl - > setIconSize ( QSize ( 16 , 16 ) ) ;
upSpeedLbl - > setIcon ( QIcon ( " :/Icons/skin/seeding.png " ) ) ;
2009-11-19 16:25:00 +03:00
//upSpeedLbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
connect ( upSpeedLbl , SIGNAL ( clicked ( ) ) , this , SLOT ( capUploadSpeed ( ) ) ) ;
upSpeedLbl - > setFlat ( true ) ;
2010-04-07 12:19:17 +04:00
upSpeedLbl - > setFocusPolicy ( Qt : : NoFocus ) ;
2010-11-08 21:23:13 +03:00
upSpeedLbl - > setCursor ( Qt : : PointingHandCursor ) ;
2011-03-13 15:48:53 +03:00
DHTLbl = new QLabel ( tr ( " DHT: %1 nodes " ) . arg ( 0 ) , bar ) ;
2009-11-18 17:54:33 +03:00
DHTLbl - > setSizePolicy ( QSizePolicy : : Maximum , QSizePolicy : : Preferred ) ;
2011-03-13 15:48:53 +03:00
statusSep1 = new QFrame ( bar ) ;
2010-01-18 21:24:43 +03:00
statusSep1 - > setFixedSize ( 3 , dlSpeedLbl - > fontMetrics ( ) . height ( ) ) ;
2009-11-18 18:24:48 +03:00
statusSep1 - > setFrameStyle ( QFrame : : VLine ) ;
statusSep1 - > setFrameShadow ( QFrame : : Raised ) ;
2011-03-13 15:48:53 +03:00
statusSep2 = new QFrame ( bar ) ;
2010-01-18 21:24:43 +03:00
statusSep2 - > setFixedSize ( 3 , dlSpeedLbl - > fontMetrics ( ) . height ( ) ) ;
2009-11-18 18:24:48 +03:00
statusSep2 - > setFrameStyle ( QFrame : : VLine ) ;
statusSep2 - > setFrameShadow ( QFrame : : Raised ) ;
2011-03-13 15:48:53 +03:00
statusSep3 = new QFrame ( bar ) ;
2010-01-18 21:24:43 +03:00
statusSep3 - > setFixedSize ( 3 , dlSpeedLbl - > fontMetrics ( ) . height ( ) ) ;
2009-11-18 18:24:48 +03:00
statusSep3 - > setFrameStyle ( QFrame : : VLine ) ;
statusSep3 - > setFrameShadow ( QFrame : : Raised ) ;
2011-03-13 15:48:53 +03:00
statusSep4 = new QFrame ( bar ) ;
2010-01-23 23:44:09 +03:00
statusSep4 - > setFixedSize ( 3 , dlSpeedLbl - > fontMetrics ( ) . height ( ) ) ;
statusSep4 - > setFrameStyle ( QFrame : : VLine ) ;
statusSep4 - > setFrameShadow ( QFrame : : Raised ) ;
2011-03-13 16:35:08 +03:00
layout - > addWidget ( DHTLbl ) ;
layout - > addWidget ( statusSep1 ) ;
layout - > addWidget ( connecStatusLblIcon ) ;
layout - > addWidget ( statusSep2 ) ;
layout - > addWidget ( altSpeedsBtn ) ;
layout - > addWidget ( statusSep4 ) ;
layout - > addWidget ( dlSpeedLbl ) ;
layout - > addWidget ( statusSep3 ) ;
layout - > addWidget ( upSpeedLbl ) ;
2009-11-18 16:18:55 +03:00
2009-11-18 17:54:33 +03:00
bar - > addPermanentWidget ( container ) ;
container - > setSizePolicy ( QSizePolicy : : Expanding , QSizePolicy : : Preferred ) ;
2009-11-19 16:25:00 +03:00
//bar->setStyleSheet("QWidget {padding-top: 0; padding-bottom: 0; margin-top: 0; margin-bottom: 0;}\n");
2009-11-18 17:54:33 +03:00
container - > setContentsMargins ( 0 , 0 , 0 , 1 ) ;
bar - > setContentsMargins ( 0 , 0 , 0 , 0 ) ;
2010-01-18 21:24:43 +03:00
container - > setFixedHeight ( dlSpeedLbl - > fontMetrics ( ) . height ( ) + 7 ) ;
2009-11-18 17:54:33 +03:00
bar - > setContentsMargins ( 12 , 0 , 12 , 0 ) ;
2010-01-18 21:24:43 +03:00
bar - > setFixedHeight ( dlSpeedLbl - > fontMetrics ( ) . height ( ) + 9 ) ;
2009-11-23 00:42:16 +03:00
// Is DHT enabled
2010-11-16 23:34:31 +03:00
DHTLbl - > setVisible ( pref . isDHTEnabled ( ) ) ;
2009-11-18 16:18:55 +03:00
refreshTimer = new QTimer ( bar ) ;
2011-03-13 16:35:08 +03:00
refreshStatusBar ( ) ;
2009-11-18 16:18:55 +03:00
connect ( refreshTimer , SIGNAL ( timeout ( ) ) , this , SLOT ( refreshStatusBar ( ) ) ) ;
refreshTimer - > start ( 1500 ) ;
}
~ StatusBar ( ) {
2011-03-14 22:18:52 +03:00
qDebug ( ) < < Q_FUNC_INFO ;
2009-11-18 16:18:55 +03:00
}
2010-11-08 21:17:54 +03:00
QPushButton * connectionStatusButton ( ) const {
return connecStatusLblIcon ;
}
2009-11-18 16:18:55 +03:00
public slots :
2010-08-23 23:42:15 +04:00
void showRestartRequired ( ) {
// Restart required notification
2010-10-26 21:54:04 +04:00
const QString restart_text = tr ( " qBittorrent needs to be restarted " ) ;
2011-03-13 15:48:53 +03:00
QLabel * restartIconLbl = new QLabel ( m_bar ) ;
2010-10-26 21:54:04 +04:00
restartIconLbl - > setPixmap ( QPixmap ( " :/Icons/oxygen/dialog-warning.png " ) . scaled ( QSize ( 24 , 24 ) ) ) ;
restartIconLbl - > setToolTip ( restart_text ) ;
2011-03-13 15:48:53 +03:00
m_bar - > insertWidget ( 0 , restartIconLbl ) ;
QLabel * restartLbl = new QLabel ( m_bar ) ;
2010-10-26 21:54:04 +04:00
restartLbl - > setSizePolicy ( QSizePolicy : : MinimumExpanding , QSizePolicy : : Preferred ) ;
2011-03-13 15:48:53 +03:00
m_bar - > insertWidget ( 1 , restartLbl ) ;
2010-10-26 21:54:04 +04:00
QFontMetrics fm ( restartLbl - > font ( ) ) ;
restartLbl - > setText ( fm . elidedText ( restart_text , Qt : : ElideRight , restartLbl - > width ( ) ) ) ;
2010-11-14 00:15:52 +03:00
QBtSession : : instance ( ) - > addConsoleMessage ( tr ( " qBittorrent was just updated and needs to be restarted for the changes to be effective. " ) , " red " ) ;
2010-08-23 23:42:15 +04:00
}
2010-10-02 00:03:27 +04:00
void stopTimer ( ) {
refreshTimer - > stop ( ) ;
}
2009-11-18 16:18:55 +03:00
void refreshStatusBar ( ) {
// Update connection status
2010-11-23 00:55:32 +03:00
const libtorrent : : session_status sessionStatus = QBtSession : : instance ( ) - > getSessionStatus ( ) ;
2012-02-20 21:30:53 +04:00
if ( ! QBtSession : : instance ( ) - > getSession ( ) - > is_listening ( ) ) {
2010-11-08 21:17:54 +03:00
connecStatusLblIcon - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/skin/disconnected.png " ) ) ) ;
2009-12-30 18:52:20 +03:00
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. " ) ) ;
} else {
2012-02-20 21:30:53 +04:00
if ( sessionStatus . has_incoming_connections ) {
2009-12-30 18:52:20 +03:00
// Connection OK
2010-11-08 21:17:54 +03:00
connecStatusLblIcon - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/skin/connected.png " ) ) ) ;
2009-12-30 18:52:20 +03:00
connecStatusLblIcon - > setToolTip ( QString : : fromUtf8 ( " <b> " ) + tr ( " Connection Status: " ) + QString : : fromUtf8 ( " </b><br> " ) + tr ( " Online " ) ) ;
} else {
2010-11-08 21:17:54 +03:00
connecStatusLblIcon - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/skin/firewalled.png " ) ) ) ;
2009-12-30 18:52:20 +03:00
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> " ) ) ;
}
2009-11-18 16:18:55 +03:00
}
// Update Number of DHT nodes
2012-02-20 21:30:53 +04:00
if ( QBtSession : : instance ( ) - > isDHTEnabled ( ) ) {
2009-11-18 16:18:55 +03:00
DHTLbl - > setVisible ( true ) ;
2009-11-23 00:22:18 +03:00
//statusSep1->setVisible(true);
2009-11-18 16:18:55 +03:00
DHTLbl - > setText ( tr ( " DHT: %1 nodes " ) . arg ( QString : : number ( sessionStatus . dht_nodes ) ) ) ;
} else {
DHTLbl - > setVisible ( false ) ;
2009-11-23 00:22:18 +03:00
//statusSep1->setVisible(false);
2009-11-18 16:18:55 +03:00
}
// Update speed labels
2011-03-13 16:35:08 +03:00
dlSpeedLbl - > setText ( tr ( " %1/s " , " Per second " ) . arg ( misc : : friendlyUnit ( sessionStatus . payload_download_rate ) ) + " ( " + misc : : friendlyUnit ( sessionStatus . total_payload_download ) + " ) " ) ;
upSpeedLbl - > setText ( tr ( " %1/s " , " Per second " ) . arg ( misc : : friendlyUnit ( sessionStatus . payload_upload_rate ) ) + " ( " + misc : : friendlyUnit ( sessionStatus . total_payload_upload ) + " ) " ) ;
2009-11-18 16:18:55 +03:00
}
2010-01-23 23:44:09 +03:00
void updateAltSpeedsBtn ( bool alternative ) {
2012-02-20 21:30:53 +04:00
if ( alternative ) {
2010-04-07 11:59:26 +04:00
altSpeedsBtn - > setIcon ( QIcon ( " :/Icons/slow.png " ) ) ;
2011-04-04 18:57:20 +04:00
altSpeedsBtn - > setToolTip ( tr ( " Click to switch to regular speed limits " ) ) ;
2010-06-22 02:38:28 +04:00
altSpeedsBtn - > setDown ( true ) ;
2010-01-23 23:44:09 +03:00
} else {
2010-04-07 11:59:26 +04:00
altSpeedsBtn - > setIcon ( QIcon ( " :/Icons/slow_off.png " ) ) ;
2011-04-04 18:57:20 +04:00
altSpeedsBtn - > setToolTip ( tr ( " Click to switch to alternative speed limits " ) ) ;
2010-12-05 22:53:02 +03:00
altSpeedsBtn - > setDown ( false ) ;
2010-01-23 23:44:09 +03:00
}
}
void toggleAlternativeSpeeds ( ) {
2010-11-16 23:34:31 +03:00
QBtSession : : instance ( ) - > useAlternativeSpeedsLimit ( ! Preferences ( ) . isAltBandwidthEnabled ( ) ) ;
2010-01-23 23:44:09 +03:00
}
2009-11-19 16:25:00 +03:00
void capDownloadSpeed ( ) {
bool ok = false ;
2011-04-17 14:29:44 +04:00
# if LIBTORRENT_VERSION_MINOR > 15
int cur_limit = QBtSession : : instance ( ) - > getSession ( ) - > settings ( ) . download_rate_limit ;
# else
int cur_limit = QBtSession : : instance ( ) - > getSession ( ) - > download_rate_limit ( ) ;
# endif
long new_limit = SpeedLimitDialog : : askSpeedLimit ( & ok , tr ( " Global Download Speed Limit " ) , cur_limit ) ;
2012-02-20 21:30:53 +04:00
if ( ok ) {
2010-11-16 23:34:31 +03:00
Preferences pref ;
bool alt = pref . isAltBandwidthEnabled ( ) ;
2012-02-20 21:30:53 +04:00
if ( new_limit < = 0 ) {
2009-12-22 20:39:12 +03:00
qDebug ( " Setting global download rate limit to Unlimited " ) ;
2011-04-17 14:29:44 +04:00
QBtSession : : instance ( ) - > setDownloadRateLimit ( - 1 ) ;
2012-02-20 21:30:53 +04:00
if ( ! alt )
2011-04-17 14:29:44 +04:00
pref . setGlobalDownloadLimit ( - 1 ) ;
2009-12-22 20:39:12 +03:00
} else {
qDebug ( " Setting global download rate limit to %.1fKb/s " , new_limit / 1024. ) ;
2011-04-17 14:29:44 +04:00
QBtSession : : instance ( ) - > setDownloadRateLimit ( new_limit ) ;
2012-02-20 21:30:53 +04:00
if ( ! alt )
2011-04-17 14:29:44 +04:00
pref . setGlobalDownloadLimit ( new_limit / 1024. ) ;
2009-12-22 20:39:12 +03:00
}
2009-11-19 16:25:00 +03:00
}
}
void capUploadSpeed ( ) {
bool ok = false ;
2011-04-17 14:29:44 +04:00
# if LIBTORRENT_VERSION_MINOR > 15
int cur_limit = QBtSession : : instance ( ) - > getSession ( ) - > settings ( ) . upload_rate_limit ;
# else
int cur_limit = QBtSession : : instance ( ) - > getSession ( ) - > upload_rate_limit ( ) ;
# endif
long new_limit = SpeedLimitDialog : : askSpeedLimit ( & ok , tr ( " Global Upload Speed Limit " ) , cur_limit ) ;
2012-02-20 21:30:53 +04:00
if ( ok ) {
2011-04-17 14:29:44 +04:00
Preferences pref ;
bool alt = pref . isAltBandwidthEnabled ( ) ;
2012-02-20 21:30:53 +04:00
if ( new_limit < = 0 ) {
2009-12-22 20:39:12 +03:00
qDebug ( " Setting global upload rate limit to Unlimited " ) ;
2011-04-17 14:29:44 +04:00
QBtSession : : instance ( ) - > setUploadRateLimit ( - 1 ) ;
2012-02-20 21:30:53 +04:00
if ( ! alt )
2011-04-17 14:29:44 +04:00
Preferences ( ) . setGlobalUploadLimit ( - 1 ) ;
2009-12-22 20:39:12 +03:00
} else {
qDebug ( " Setting global upload rate limit to %.1fKb/s " , new_limit / 1024. ) ;
2011-04-17 14:29:44 +04:00
QBtSession : : instance ( ) - > setUploadRateLimit ( new_limit ) ;
2012-02-20 21:30:53 +04:00
if ( ! alt )
2011-04-17 14:29:44 +04:00
Preferences ( ) . setGlobalUploadLimit ( new_limit / 1024. ) ;
2009-12-22 20:39:12 +03:00
}
2009-11-19 16:25:00 +03:00
}
}
2010-01-23 23:44:09 +03:00
2010-10-31 15:35:07 +03:00
private :
2011-03-13 15:48:53 +03:00
QStatusBar * m_bar ;
2010-10-31 15:35:07 +03:00
QPushButton * dlSpeedLbl ;
QPushButton * upSpeedLbl ;
QLabel * DHTLbl ;
QFrame * statusSep1 ;
QFrame * statusSep2 ;
QFrame * statusSep3 ;
QFrame * statusSep4 ;
2010-11-08 21:17:54 +03:00
QPushButton * connecStatusLblIcon ;
2010-10-31 15:35:07 +03:00
QPushButton * altSpeedsBtn ;
QTimer * refreshTimer ;
QWidget * container ;
2011-03-13 16:35:08 +03:00
QHBoxLayout * layout ;
2010-10-31 15:35:07 +03:00
2009-11-18 16:18:55 +03:00
} ;
# endif // STATUSBAR_H