2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
* Copyright ( C ) 2010 - 2011 nowrep
*
* 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"
# 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-03-02 16:57:41 +01:00
2011-03-17 17:03:04 +01:00
DownloadManager : : DownloadManager ( QWidget * parent ) :
2011-03-02 16:57:41 +01:00
QWidget ( parent )
, ui ( new Ui : : DownloadManager )
, m_isClosing ( false )
{
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
if ( QtWin : : isCompositionEnabled ( ) )
QtWin : : extendFrameIntoClientArea ( this ) ;
# endif
2011-03-02 16:57:41 +01:00
ui - > clearButton - > setIcon ( QIcon : : fromTheme ( " edit-clear " ) ) ;
//CENTER on screen
const QRect screen = QApplication : : desktop ( ) - > screenGeometry ( ) ;
const QRect & size = QWidget : : geometry ( ) ;
QWidget : : move ( ( screen . width ( ) - size . width ( ) ) / 2 , ( screen . height ( ) - size . height ( ) ) / 2 ) ;
m_iconProvider = new QFileIconProvider ( ) ;
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 ( ) ;
2011-04-03 21:50:44 +02:00
# ifdef W7API
2011-05-01 22:07:57 +02:00
if ( QtWin : : isRunningWindows7 ( ) )
win7 . init ( this - > winId ( ) ) ;
2011-03-02 16:57:41 +01:00
# endif
}
2011-05-25 14:26:36 +02:00
void DownloadManager : : loadSettings ( )
{
QSettings settings ( mApp - > getActiveProfil ( ) + " settings.ini " , QSettings : : IniFormat ) ;
settings . beginGroup ( " DownloadManager " ) ;
m_downloadPath = settings . value ( " defaultDownloadPath " , " " ) . toString ( ) ;
m_lastDownloadPath = settings . value ( " lastDownloadPath " , QDir : : homePath ( ) + " / " ) . toString ( ) ;
m_useNativeDialog = settings . value ( " useNativeDialog " ,
# ifdef Q_WS_WIN
false
# else
true
# endif
) . toBool ( ) ;
settings . endGroup ( ) ;
}
void DownloadManager : : resizeEvent ( QResizeEvent * e )
{
QWidget : : resizeEvent ( e ) ;
emit resized ( size ( ) ) ;
}
2011-04-03 21:50:44 +02:00
# ifdef W7API
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-04-24 22:40:35 +02:00
if ( ! downItem | | ( downItem & & downItem - > isCancelled ( ) ) | | ! downItem - > isDownloading ( ) )
2011-03-02 16:57:41 +01:00
continue ;
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-04-24 22:40:35 +02:00
if ( remTimes . isEmpty ( ) )
2011-03-02 16:57:41 +01:00
return ;
QTime remaining ;
foreach ( QTime time , remTimes ) {
if ( time > remaining )
remaining = time ;
}
int progress = 0 ;
foreach ( int prog , progresses )
progress + = prog ;
progress = progress / progresses . count ( ) ;
double speed = 0.00 ;
foreach ( double spee , speeds )
speed + = spee ;
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 ) ) ) ;
setWindowTitle ( QString : : number ( progress ) + tr ( " % - Download Manager " ) ) ;
2011-04-03 21:50:44 +02:00
# ifdef W7API
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
} else
QWidget : : timerEvent ( event ) ;
}
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 ) ) ) ;
if ( ! downItem )
continue ;
if ( downItem - > isDownloading ( ) )
continue ;
items . append ( downItem ) ;
}
qDeleteAll ( items ) ;
}
2011-03-24 22:31:38 +01:00
void DownloadManager : : download ( const QNetworkRequest & request , bool askWhatToDo )
2011-03-02 16:57:41 +01:00
{
2011-03-24 22:31:38 +01:00
handleUnsupportedContent ( m_networkManager - > get ( request ) , askWhatToDo ) ;
2011-03-02 16:57:41 +01:00
}
2011-05-08 12:54:49 +02:00
//////////////////////////////////////////////////////
//// Getting where to download requested file
//// in 3 functions, as we are using non blocking
//// dialogs ( this is important to make secured downloading
//// on windows working properly )
//////////////////////////////////////////////////////
2011-03-24 22:31:38 +01:00
void DownloadManager : : handleUnsupportedContent ( QNetworkReply * reply , bool askWhatToDo )
2011-03-02 16:57:41 +01:00
{
2011-05-10 21:25:31 +02:00
m_htimer = new QTime ( ) ;
m_htimer - > start ( ) ;
2011-05-08 12:54:49 +02:00
m_h_fileName = getFileName ( reply ) ;
m_hreply = reply ;
2011-03-02 16:57:41 +01:00
2011-03-24 22:31:38 +01:00
QFileInfo info ( reply - > url ( ) . toString ( ) ) ;
QTemporaryFile tempFile ( " XXXXXX. " + info . suffix ( ) ) ;
tempFile . open ( ) ;
QFileInfo tempInfo ( tempFile . fileName ( ) ) ;
2011-05-08 12:54:49 +02:00
m_hfileIcon = m_iconProvider - > icon ( tempInfo ) . pixmap ( 30 , 30 ) ;
2011-03-24 22:31:38 +01:00
QString mimeType = m_iconProvider - > type ( tempInfo ) ;
2011-03-02 16:57:41 +01:00
2011-03-24 22:31:38 +01:00
if ( askWhatToDo ) {
2011-05-08 12:54:49 +02:00
DownloadOptionsDialog * dialog = new DownloadOptionsDialog ( m_h_fileName , m_hfileIcon , mimeType , reply - > url ( ) , mApp - > activeWindow ( ) ) ;
dialog - > show ( ) ;
connect ( dialog , SIGNAL ( dialogFinished ( int ) ) , this , SLOT ( optionsDialogAccepted ( int ) ) ) ;
} else
optionsDialogAccepted ( 2 ) ;
}
2011-03-02 16:57:41 +01:00
2011-05-08 12:54:49 +02:00
void DownloadManager : : optionsDialogAccepted ( int finish )
{
m_hOpenFileChoosed = false ;
switch ( finish ) {
case 0 : //Cancelled
2011-05-10 21:25:31 +02:00
if ( m_htimer )
delete m_htimer ;
2011-05-08 12:54:49 +02:00
return ;
break ;
case 1 : //Open
m_hOpenFileChoosed = true ;
break ;
case 2 : //Save
break ;
}
2011-03-24 22:31:38 +01:00
2011-05-08 12:54:49 +02:00
if ( ! m_hOpenFileChoosed ) {
if ( m_downloadPath . isEmpty ( ) ) {
2011-05-25 14:26:36 +02:00
if ( m_useNativeDialog ) {
fileNameChoosed ( QFileDialog : : getSaveFileName ( mApp - > getWindow ( ) , tr ( " Save file as... " ) , m_lastDownloadPath + m_h_fileName ) ) ;
} else {
QFileDialog * dialog = new QFileDialog ( mApp - > getWindow ( ) ) ;
dialog - > setAttribute ( Qt : : WA_DeleteOnClose ) ;
dialog - > setWindowTitle ( tr ( " Save file as... " ) ) ;
dialog - > setDirectory ( m_lastDownloadPath ) ;
dialog - > selectFile ( m_h_fileName ) ;
dialog - > show ( ) ;
connect ( dialog , SIGNAL ( fileSelected ( QString ) ) , this , SLOT ( fileNameChoosed ( QString ) ) ) ;
}
2011-03-24 22:31:38 +01:00
}
2011-05-08 12:54:49 +02:00
else
fileNameChoosed ( m_downloadPath + m_h_fileName ) ;
2011-03-24 22:31:38 +01:00
} else
2011-05-08 12:54:49 +02:00
fileNameChoosed ( QDir : : tempPath ( ) + " / " + m_h_fileName ) ;
}
2011-03-24 22:31:38 +01:00
2011-05-08 12:54:49 +02:00
void DownloadManager : : fileNameChoosed ( const QString & name )
{
m_huserFileName = name ;
if ( m_huserFileName . isEmpty ( ) ) {
m_hreply - > abort ( ) ;
2011-05-10 21:25:31 +02:00
if ( m_htimer )
delete m_htimer ;
2011-05-08 12:54:49 +02:00
return ;
}
int pos = m_huserFileName . lastIndexOf ( " / " ) ;
if ( pos ! = - 1 ) {
int size = m_huserFileName . size ( ) ;
m_hpath = m_huserFileName . left ( pos + 1 ) ;
m_hfileName = m_huserFileName . right ( size - pos - 1 ) ;
2011-03-02 16:57:41 +01:00
}
2011-05-08 12:54:49 +02:00
if ( ! m_hpath . contains ( QDir : : tempPath ( ) ) )
m_lastDownloadPath = m_hpath ;
2011-03-24 22:31:38 +01:00
2011-03-04 13:59:07 +01:00
QSettings settings ( mApp - > getActiveProfil ( ) + " settings.ini " , QSettings : : IniFormat ) ;
2011-03-02 16:57:41 +01:00
settings . beginGroup ( " DownloadManager " ) ;
settings . setValue ( " lastDownloadPath " , m_lastDownloadPath ) ;
settings . endGroup ( ) ;
QListWidgetItem * item = new QListWidgetItem ( ui - > list ) ;
2011-05-10 21:25:31 +02:00
DownloadItem * downItem = new DownloadItem ( item , m_hreply , m_hpath , m_hfileName , m_hfileIcon , m_htimer , m_hOpenFileChoosed , this ) ;
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-03-02 16:57:41 +01:00
ui - > list - > setItemWidget ( item , downItem ) ;
item - > setSizeHint ( downItem - > sizeHint ( ) ) ;
show ( ) ;
activateWindow ( ) ;
2011-05-08 12:54:49 +02:00
//Negating all used variables
m_hpath . clear ( ) ;
m_hfileName . clear ( ) ;
m_huserFileName . clear ( ) ;
m_h_fileName . clear ( ) ;
m_hreply = 0 ;
m_hfileIcon = QPixmap ( ) ;
m_hOpenFileChoosed = false ;
2011-03-02 16:57:41 +01:00
}
2011-05-08 12:54:49 +02:00
//////////////////////////////////////////////////////
//// End here
//////////////////////////////////////////////////////
2011-03-02 16:57:41 +01:00
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 ) ) ) ;
if ( ! downItem | | ( downItem & & downItem - > isCancelled ( ) ) | | ! downItem - > isDownloading ( ) )
continue ;
downloadingAllFilesFinished = false ;
}
if ( downloadingAllFilesFinished ) {
2011-04-29 17:47:55 +02:00
if ( success & & qApp - > activeWindow ( ) ! = this ) {
2011-05-10 21:25:31 +02:00
mApp - > desktopNotifications ( ) - > notify ( QPixmap ( " :icons/notifications/download.png " ) , tr ( " Download Finished " ) , tr ( " All files have been successfuly downloaded. " ) ) ;
2011-04-29 17:47:55 +02:00
raise ( ) ;
activateWindow ( ) ;
}
2011-04-24 22:40:35 +02:00
ui - > speedLabel - > clear ( ) ;
setWindowTitle ( tr ( " Download Manager " ) ) ;
# ifdef W7API
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-03-17 17:03:04 +01:00
void DownloadManager : : deleteItem ( DownloadItem * item )
2011-03-02 16:57:41 +01:00
{
if ( item & & ! item - > isDownloading ( ) )
delete item ;
}
2011-03-17 17:03:04 +01:00
QString DownloadManager : : getFileName ( QNetworkReply * reply )
2011-03-02 16:57:41 +01:00
{
QString path ;
if ( reply - > hasRawHeader ( " Content-Disposition " ) ) {
QString value = reply - > rawHeader ( " Content-Disposition " ) ;
int pos = value . indexOf ( " filename= " ) ;
if ( pos ! = - 1 ) {
QString name = value . mid ( pos + 9 ) ;
if ( name . startsWith ( ' " ' ) & & name . endsWith ( ' " ' ) )
name = name . mid ( 1 , name . size ( ) - 2 ) ;
path = name ;
}
}
if ( path . isEmpty ( ) )
path = reply - > url ( ) . path ( ) ;
QFileInfo info ( path ) ;
QString baseName = info . completeBaseName ( ) ;
QString endName = info . suffix ( ) ;
if ( baseName . isEmpty ( ) ) {
baseName = tr ( " NoNameDownload " ) ;
}
if ( ! endName . isEmpty ( ) )
endName = " . " + endName ;
2011-03-26 13:34:08 +01:00
QString name = baseName + endName ;
if ( name . startsWith ( " \" " ) )
name = name . mid ( 1 ) ;
if ( name . endsWith ( " \" ; " ) )
name . remove ( " \" ; " ) ;
return name ;
2011-03-02 16:57:41 +01:00
}
bool DownloadManager : : canClose ( )
{
if ( m_isClosing )
return true ;
bool isDownloading = false ;
for ( int i = 0 ; i < ui - > list - > count ( ) ; i + + ) {
DownloadItem * downItem = qobject_cast < DownloadItem * > ( ui - > list - > itemWidget ( ui - > list - > item ( i ) ) ) ;
if ( ! downItem )
continue ;
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-03-02 16:57:41 +01:00
if ( ! canClose ( ) ) {
QMessageBox : : StandardButton button = QMessageBox : : warning ( this , tr ( " Warning " ) ,
tr ( " Are you sure to quit? All uncompleted downloads will be cancelled! " ) , QMessageBox : : Yes | QMessageBox : : No ) ;
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 ;
delete m_networkManager ;
delete m_iconProvider ;
}