2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
2012-01-01 15:29:55 +01:00
* Copyright ( C ) 2010 - 2012 David Rosca < nowrep @ gmail . com >
2011-03-03 18:29:20 +01:00
*
* 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 3 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 , see < http : //www.gnu.org/licenses/>.
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
2011-03-02 16:57:41 +01:00
# include "downloadmanager.h"
# include "ui_downloadmanager.h"
# include "qupzilla.h"
2012-01-21 23:19:38 +01:00
# include "mainapplication.h"
2011-03-02 16:57:41 +01:00
# include "downloadoptionsdialog.h"
# include "downloaditem.h"
# include "ecwin7.h"
2011-03-20 17:42:56 +01:00
# include "networkmanager.h"
2011-04-07 18:00:26 +02:00
# include "qtwin.h"
2011-04-24 22:40:35 +02:00
# include "desktopnotificationsfactory.h"
2011-09-21 14:20:49 +02:00
# include "globalfunctions.h"
2011-09-23 15:40:14 +02:00
# include "webpage.h"
2011-09-30 19:22:50 +02:00
# include "downloadfilehelper.h"
2012-01-11 21:58:25 +01:00
# include "settings.h"
2011-03-02 16:57:41 +01:00
2012-02-29 18:33:50 +01:00
# include <QCloseEvent>
# include <QDir>
# include <QProcess>
# include <QMessageBox>
2011-12-28 13:16:04 +01:00
# ifdef Q_WS_WIN
# define DEFAULT_USE_NATIVE_DIALOG false
# else
# define DEFAULT_USE_NATIVE_DIALOG true
# endif
2011-10-30 16:32:36 +01:00
DownloadManager : : DownloadManager ( QWidget * parent )
: QWidget ( parent )
, ui ( new Ui : : DownloadManager )
, m_isClosing ( false )
2011-03-02 16:57:41 +01:00
{
2011-05-25 14:26:36 +02:00
setWindowFlags ( windowFlags ( ) ^ Qt : : WindowMaximizeButtonHint ) ;
2011-03-02 16:57:41 +01:00
ui - > setupUi ( this ) ;
2011-04-07 18:00:26 +02:00
# ifdef Q_WS_WIN
2011-11-06 17:01:23 +01:00
if ( QtWin : : isCompositionEnabled ( ) ) {
2011-04-07 18:00:26 +02:00
QtWin : : extendFrameIntoClientArea ( this ) ;
2011-11-06 17:01:23 +01:00
}
2011-04-07 18:00:26 +02:00
# endif
2011-03-02 16:57:41 +01:00
ui - > clearButton - > setIcon ( QIcon : : fromTheme ( " edit-clear " ) ) ;
2011-09-21 14:20:49 +02:00
qz_centerWidgetOnScreen ( this ) ;
2011-03-02 16:57:41 +01:00
2011-03-20 17:42:56 +01:00
m_networkManager = mApp - > networkManager ( ) ;
2011-03-02 16:57:41 +01:00
connect ( ui - > clearButton , SIGNAL ( clicked ( ) ) , this , SLOT ( clearList ( ) ) ) ;
2011-05-25 14:26:36 +02:00
loadSettings ( ) ;
2012-01-28 12:02:37 +01:00
# ifdef W7TASKBAR
2011-11-06 17:01:23 +01:00
if ( QtWin : : isRunningWindows7 ( ) ) {
2011-05-01 22:07:57 +02:00
win7 . init ( this - > winId ( ) ) ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
# endif
}
2011-05-25 14:26:36 +02:00
void DownloadManager : : loadSettings ( )
{
2012-01-11 21:58:25 +01:00
Settings settings ;
2011-05-25 14:26:36 +02:00
settings . beginGroup ( " DownloadManager " ) ;
m_downloadPath = settings . value ( " defaultDownloadPath " , " " ) . toString ( ) ;
2011-10-17 09:59:09 +02:00
m_lastDownloadPath = settings . value ( " lastDownloadPath " , QDir : : homePath ( ) + " / " ) . toString ( ) ;
2011-10-30 16:32:36 +01:00
m_closeOnFinish = settings . value ( " CloseManagerOnFinish " , false ) . toBool ( ) ;
2011-12-28 13:16:04 +01:00
m_useNativeDialog = settings . value ( " useNativeDialog " , DEFAULT_USE_NATIVE_DIALOG ) . toBool ( ) ;
2012-01-28 15:37:11 +01:00
m_useExternalManager = settings . value ( " UseExternalManager " , false ) . toBool ( ) ;
m_externalExecutable = settings . value ( " ExternalManagerExecutable " , " " ) . toString ( ) ;
m_externalArguments = settings . value ( " ExternalManagerArguments " , " " ) . toString ( ) ;
2011-05-25 14:26:36 +02:00
settings . endGroup ( ) ;
}
2011-10-18 14:30:17 +02:00
void DownloadManager : : show ( )
{
2011-11-06 17:01:23 +01:00
m_timer . start ( 1000 * 2 , this ) ;
2011-10-18 14:30:17 +02:00
QWidget : : show ( ) ;
}
2011-11-06 17:01:23 +01:00
void DownloadManager : : resizeEvent ( QResizeEvent * e )
2011-05-25 14:26:36 +02:00
{
QWidget : : resizeEvent ( e ) ;
emit resized ( size ( ) ) ;
}
2012-01-28 15:37:11 +01:00
void DownloadManager : : startExternalManager ( const QUrl & url )
{
QStringList arguments = m_externalArguments . split ( " " ) ;
arguments < < url . toString ( ) ;
bool success = QProcess : : startDetached ( m_externalExecutable , arguments ) ;
if ( ! success ) {
QString info = " <ul><li><b> " + tr ( " Executable: " ) + " </b> " + m_externalExecutable + " </li><li><b> " + tr ( " Arguments: " ) + " </b> " + arguments . join ( " " ) + " </li></ul> " ;
QMessageBox : : critical ( this , tr ( " Cannot start external download manager " ) , tr ( " Cannot start external download manager! %1 " ) . arg ( info ) ) ;
}
}
2012-01-28 12:02:37 +01:00
# ifdef W7TASKBAR
2011-03-17 17:03:04 +01:00
bool DownloadManager : : winEvent ( MSG * message , long * result )
2011-03-02 16:57:41 +01:00
{
return win7 . winEvent ( message , result ) ;
}
# endif
2011-03-17 17:03:04 +01:00
void DownloadManager : : timerEvent ( QTimerEvent * event )
2011-03-02 16:57:41 +01:00
{
QList < QTime > remTimes ;
QList < int > progresses ;
QList < double > speeds ;
if ( event - > timerId ( ) = = m_timer . timerId ( ) ) {
if ( ! ui - > list - > count ( ) ) {
ui - > speedLabel - > clear ( ) ;
setWindowTitle ( tr ( " Download Manager " ) ) ;
return ;
}
for ( int i = 0 ; i < ui - > list - > count ( ) ; i + + ) {
DownloadItem * downItem = qobject_cast < DownloadItem * > ( ui - > list - > itemWidget ( ui - > list - > item ( i ) ) ) ;
2011-11-06 17:01:23 +01:00
if ( ! downItem | | ( downItem & & downItem - > isCancelled ( ) ) | | ! downItem - > isDownloading ( ) ) {
2011-03-02 16:57:41 +01:00
continue ;
2011-11-06 17:01:23 +01:00
}
2011-04-24 22:40:35 +02:00
progresses . append ( downItem - > progress ( ) ) ;
2011-03-02 16:57:41 +01:00
remTimes . append ( downItem - > remainingTime ( ) ) ;
speeds . append ( downItem - > currentSpeed ( ) ) ;
}
2011-11-06 17:01:23 +01:00
if ( remTimes . isEmpty ( ) ) {
2011-03-02 16:57:41 +01:00
return ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
QTime remaining ;
2012-01-24 19:12:31 +01:00
foreach ( const QTime & time , remTimes ) {
2011-11-06 17:01:23 +01:00
if ( time > remaining ) {
2011-03-02 16:57:41 +01:00
remaining = time ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
}
int progress = 0 ;
2011-11-06 17:01:23 +01:00
foreach ( int prog , progresses )
progress + = prog ;
2011-03-02 16:57:41 +01:00
progress = progress / progresses . count ( ) ;
double speed = 0.00 ;
2011-11-06 17:01:23 +01:00
foreach ( double spee , speeds )
speed + = spee ;
2011-03-02 16:57:41 +01:00
2011-11-06 17:01:23 +01:00
ui - > speedLabel - > setText ( tr ( " %1% of %2 files (%3) %4 remaining " ) . arg ( QString : : number ( progress ) , QString : : number ( progresses . count ( ) ) ,
DownloadItem : : currentSpeedToString ( speed ) ,
DownloadItem : : remaingTimeToString ( remaining ) ) ) ;
2011-03-02 16:57:41 +01:00
setWindowTitle ( QString : : number ( progress ) + tr ( " % - Download Manager " ) ) ;
2012-01-28 12:02:37 +01:00
# ifdef W7TASKBAR
2011-05-01 22:07:57 +02:00
if ( QtWin : : isRunningWindows7 ( ) ) {
win7 . setProgressValue ( progress , 100 ) ;
win7 . setProgressState ( win7 . Normal ) ;
}
2011-03-02 16:57:41 +01:00
# endif
2011-11-06 17:01:23 +01:00
}
else {
2011-03-02 16:57:41 +01:00
QWidget : : timerEvent ( event ) ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
}
void DownloadManager : : clearList ( )
{
QList < DownloadItem * > items ;
for ( int i = 0 ; i < ui - > list - > count ( ) ; i + + ) {
DownloadItem * downItem = qobject_cast < DownloadItem * > ( ui - > list - > itemWidget ( ui - > list - > item ( i ) ) ) ;
2011-11-06 17:01:23 +01:00
if ( ! downItem ) {
2011-03-02 16:57:41 +01:00
continue ;
2011-11-06 17:01:23 +01:00
}
if ( downItem - > isDownloading ( ) ) {
2011-03-02 16:57:41 +01:00
continue ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
items . append ( downItem ) ;
}
qDeleteAll ( items ) ;
}
2011-11-08 15:20:53 +01:00
void DownloadManager : : download ( const QNetworkRequest & request , WebPage * page , bool askWhatToDo )
2011-03-02 16:57:41 +01:00
{
2012-01-21 20:27:45 +01:00
if ( ! page ) {
return ;
}
2011-11-08 15:20:53 +01:00
// Clearing web page info from request
QNetworkRequest req = request ;
req . setAttribute ( ( QNetworkRequest : : Attribute ) ( QNetworkRequest : : User + 100 ) , 0 ) ;
req . setAttribute ( ( QNetworkRequest : : Attribute ) ( QNetworkRequest : : User + 101 ) , 0 ) ;
handleUnsupportedContent ( m_networkManager - > get ( req ) , page , askWhatToDo ) ;
2011-03-02 16:57:41 +01:00
}
2011-11-08 15:20:53 +01:00
void DownloadManager : : handleUnsupportedContent ( QNetworkReply * reply , WebPage * page , bool askWhatToDo )
2011-03-02 16:57:41 +01:00
{
2011-11-06 17:01:23 +01:00
if ( reply - > url ( ) . scheme ( ) = = " qupzilla " ) {
2011-10-08 12:10:25 +02:00
return ;
2011-11-06 17:01:23 +01:00
}
2011-10-08 12:10:25 +02:00
2012-01-28 15:37:11 +01:00
if ( m_useExternalManager ) {
startExternalManager ( reply - > url ( ) ) ;
reply - > abort ( ) ;
reply - > deleteLater ( ) ;
return ;
}
2011-11-08 15:20:53 +01:00
reply - > setProperty ( " downReply " , true ) ;
DownloadFileHelper * h = new DownloadFileHelper ( m_lastDownloadPath , m_downloadPath , m_useNativeDialog , page ) ;
2011-11-06 17:01:23 +01:00
connect ( h , SIGNAL ( itemCreated ( QListWidgetItem * , DownloadItem * ) ) , this , SLOT ( itemCreated ( QListWidgetItem * , DownloadItem * ) ) ) ;
2011-10-08 12:10:25 +02:00
2011-12-06 19:18:06 +01:00
h - > setLastDownloadOption ( m_lastDownloadOption ) ;
2011-10-07 15:37:49 +02:00
h - > setDownloadManager ( this ) ;
2011-09-30 19:22:50 +02:00
h - > setListWidget ( ui - > list ) ;
h - > handleUnsupportedContent ( reply , askWhatToDo ) ;
2011-05-08 12:54:49 +02:00
}
2011-03-02 16:57:41 +01:00
2011-11-06 17:01:23 +01:00
void DownloadManager : : itemCreated ( QListWidgetItem * item , DownloadItem * downItem )
2011-05-08 12:54:49 +02:00
{
2011-03-02 16:57:41 +01:00
connect ( downItem , SIGNAL ( deleteItem ( DownloadItem * ) ) , this , SLOT ( deleteItem ( DownloadItem * ) ) ) ;
2011-04-24 22:40:35 +02:00
connect ( downItem , SIGNAL ( downloadFinished ( bool ) ) , this , SLOT ( downloadFinished ( bool ) ) ) ;
2011-09-30 19:22:50 +02:00
2011-03-02 16:57:41 +01:00
ui - > list - > setItemWidget ( item , downItem ) ;
item - > setSizeHint ( downItem - > sizeHint ( ) ) ;
2012-02-04 18:45:59 +01:00
downItem - > show ( ) ;
2011-09-30 19:22:50 +02:00
2011-03-02 16:57:41 +01:00
show ( ) ;
2011-10-21 23:26:34 +02:00
raise ( ) ;
2011-03-02 16:57:41 +01:00
activateWindow ( ) ;
}
2011-04-24 22:40:35 +02:00
void DownloadManager : : downloadFinished ( bool success )
{
bool downloadingAllFilesFinished = true ;
for ( int i = 0 ; i < ui - > list - > count ( ) ; i + + ) {
DownloadItem * downItem = qobject_cast < DownloadItem * > ( ui - > list - > itemWidget ( ui - > list - > item ( i ) ) ) ;
2011-11-06 17:01:23 +01:00
if ( ! downItem | | ( downItem & & downItem - > isCancelled ( ) ) | | ! downItem - > isDownloading ( ) ) {
2011-04-24 22:40:35 +02:00
continue ;
2011-11-06 17:01:23 +01:00
}
2011-04-24 22:40:35 +02:00
downloadingAllFilesFinished = false ;
}
if ( downloadingAllFilesFinished ) {
2011-04-29 17:47:55 +02:00
if ( success & & qApp - > activeWindow ( ) ! = this ) {
2012-01-12 21:38:38 +01:00
mApp - > desktopNotifications ( ) - > showNotifications ( QIcon : : fromTheme ( " mail-inbox " , QIcon ( " :icons/notifications/download.png " ) ) . pixmap ( 48 ) , tr ( " Download Finished " ) , tr ( " All files have been successfully downloaded. " ) ) ;
2011-10-30 16:32:36 +01:00
if ( ! m_closeOnFinish ) {
raise ( ) ;
activateWindow ( ) ;
}
2011-04-29 17:47:55 +02:00
}
2011-04-24 22:40:35 +02:00
ui - > speedLabel - > clear ( ) ;
setWindowTitle ( tr ( " Download Manager " ) ) ;
2012-01-28 12:02:37 +01:00
# ifdef W7TASKBAR
2011-05-01 22:07:57 +02:00
if ( QtWin : : isRunningWindows7 ( ) ) {
2011-05-06 12:58:07 +02:00
win7 . setProgressValue ( 0 , 100 ) ;
win7 . setProgressState ( win7 . NoProgress ) ;
2011-05-01 22:07:57 +02:00
}
2011-04-24 22:40:35 +02:00
# endif
2011-11-06 17:01:23 +01:00
if ( m_closeOnFinish ) {
2011-10-30 16:32:36 +01:00
close ( ) ;
2011-11-06 17:01:23 +01:00
}
2011-04-24 22:40:35 +02:00
}
}
2011-03-17 17:03:04 +01:00
void DownloadManager : : deleteItem ( DownloadItem * item )
2011-03-02 16:57:41 +01:00
{
2011-11-06 17:01:23 +01:00
if ( item & & ! item - > isDownloading ( ) ) {
2011-03-02 16:57:41 +01:00
delete item ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
}
bool DownloadManager : : canClose ( )
{
2011-11-06 17:01:23 +01:00
if ( m_isClosing ) {
2011-03-02 16:57:41 +01:00
return true ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
bool isDownloading = false ;
for ( int i = 0 ; i < ui - > list - > count ( ) ; i + + ) {
DownloadItem * downItem = qobject_cast < DownloadItem * > ( ui - > list - > itemWidget ( ui - > list - > item ( i ) ) ) ;
2011-11-06 17:01:23 +01:00
if ( ! downItem ) {
2011-03-02 16:57:41 +01:00
continue ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
if ( downItem - > isDownloading ( ) ) {
isDownloading = true ;
break ;
}
}
return ! isDownloading ;
}
2011-03-17 17:03:04 +01:00
void DownloadManager : : closeEvent ( QCloseEvent * e )
2011-03-02 16:57:41 +01:00
{
2011-03-05 13:16:13 +01:00
if ( mApp - > windowCount ( ) = = 0 ) { // No main windows -> we are going to quit
2011-11-06 17:01:23 +01:00
if ( ! canClose ( ) ) {
2011-03-02 16:57:41 +01:00
QMessageBox : : StandardButton button = QMessageBox : : warning ( this , tr ( " Warning " ) ,
2011-11-06 17:01:23 +01:00
tr ( " Are you sure to quit? All uncompleted downloads will be cancelled! " ) , QMessageBox : : Yes | QMessageBox : : No ) ;
2011-03-02 16:57:41 +01:00
if ( button ! = QMessageBox : : Yes ) {
e - > ignore ( ) ;
return ;
}
m_isClosing = true ;
}
2011-03-04 13:59:07 +01:00
mApp - > quitApplication ( ) ;
2011-03-02 16:57:41 +01:00
}
e - > accept ( ) ;
}
DownloadManager : : ~ DownloadManager ( )
{
delete ui ;
}