2015-09-08 17:05:59 +02:00
# include "./adddownloaddialog.h"
2015-04-22 19:32:04 +02:00
2015-09-08 17:05:59 +02:00
# include "../network/youtubedownload.h"
2015-10-14 22:58:55 +02:00
# include "../network/vimeodownload.h"
2015-09-08 17:05:59 +02:00
# include "../network/socksharedownload.h"
# include "../network/bitsharedownload.h"
# include "../network/groovesharkdownload.h"
# include "../network/filenukedownload.h"
2015-08-29 21:34:47 +02:00
# ifdef UNDER_CONSTRUCTION
2015-09-08 17:05:59 +02:00
# include "../network/spotifydownload.h"
2015-08-29 21:34:47 +02:00
# endif
2015-04-22 19:32:04 +02:00
2015-12-05 22:56:32 +01:00
# include "ui_adddownloaddialog.h"
2015-09-08 17:05:59 +02:00
2015-09-26 20:37:48 +02:00
# include <qtutilities/misc/dialogutils.h>
2015-04-22 19:32:04 +02:00
# include <QInputDialog>
# include <QSettings>
# include <QClipboard>
# include <QMessageBox>
2015-09-26 20:37:48 +02:00
using namespace Dialogs ;
2015-04-22 19:32:04 +02:00
using namespace Network ;
namespace QtGui {
QStringList AddDownloadDialog : : m_knownDownloadTypeNames = QStringList ( )
< < QStringLiteral ( " standard http(s)/ftp " )
< < QStringLiteral ( " Youtube " )
2015-10-14 22:58:55 +02:00
< < QStringLiteral ( " Vimeo " )
2015-04-22 19:32:04 +02:00
< < QStringLiteral ( " Grooveshark " )
< < QStringLiteral ( " Sockshare/Putlocker " )
< < QStringLiteral ( " Bitshare " )
2015-08-29 21:34:47 +02:00
< < QStringLiteral ( " FileNuke " )
# ifdef UNDER_CONSTRUCTION
< < QStringLiteral ( " Spotify " )
# endif
;
2015-04-22 19:32:04 +02:00
AddDownloadDialog : : AddDownloadDialog ( QWidget * parent ) :
QDialog ( parent ) ,
m_ui ( new Ui : : AddDownloadDialog ) ,
m_downloadTypeIndex ( 0 ) ,
m_downloadTypeIndexAdjustedManually ( false ) ,
m_validInput ( false ) ,
m_selectDownloadTypeInputDialog ( nullptr )
{
m_ui - > setupUi ( this ) ;
2015-09-26 20:37:48 +02:00
makeHeading ( m_ui - > mainInstructionLabel ) ;
setStyleSheet ( dialogStyle ( ) ) ;
2015-04-22 19:32:04 +02:00
connect ( m_ui - > backPushButton , & QPushButton : : clicked , this , & AddDownloadDialog : : back ) ;
connect ( m_ui - > addPushButton , & QPushButton : : clicked , this , & AddDownloadDialog : : setLastUrl ) ;
connect ( m_ui - > addPushButton , & QPushButton : : clicked , this , & AddDownloadDialog : : addDownloadClicked ) ;
connect ( m_ui - > addPushButton , & QPushButton : : clicked , this , & AddDownloadDialog : : accept ) ;
connect ( m_ui - > urlLineEdit , & QLineEdit : : textChanged , this , & AddDownloadDialog : : textChanged ) ;
connect ( m_ui - > urlLineEdit , & QLineEdit : : returnPressed , this , & AddDownloadDialog : : returnPressed ) ;
connect ( m_ui - > adjustDetectedTypePushButton , & QPushButton : : clicked , this , & AddDownloadDialog : : adjustDetectedDownloadType ) ;
connect ( m_ui - > insertTextFromClipboardPushButton , & QPushButton : : clicked , this , & AddDownloadDialog : : insertTextFromClipboard ) ;
QSettings settings ( QSettings : : IniFormat , QSettings : : UserScope , QApplication : : organizationName ( ) , QApplication : : applicationName ( ) ) ;
settings . beginGroup ( " adddownloaddialog " ) ;
m_lastUrl = settings . value ( " lasturl " , QString ( ) ) . toString ( ) ;
m_ui - > downloadTypeInfoWidget - > setHidden ( true ) ;
m_ui - > urlLineEdit - > setText ( m_lastUrl ) ;
m_ui - > urlLineEdit - > setInputMethodHints ( Qt : : ImhUrlCharactersOnly ) ;
m_ui - > urlLineEdit - > selectAll ( ) ;
settings . endGroup ( ) ;
}
AddDownloadDialog : : ~ AddDownloadDialog ( )
2015-08-29 21:34:47 +02:00
{ }
2015-04-22 19:32:04 +02:00
Download * AddDownloadDialog : : result ( ) const
{
2015-08-29 21:34:47 +02:00
if ( ! hasValidInput ( ) ) {
2015-04-22 19:32:04 +02:00
return nullptr ;
2015-08-29 21:34:47 +02:00
}
2015-04-22 19:32:04 +02:00
QString res ( m_ui - > urlLineEdit - > text ( ) ) ;
switch ( m_downloadTypeIndex ) {
case 0 :
return new HttpDownload ( QUrl ( res ) ) ;
case 1 :
return new YoutubeDownload ( QUrl ( res ) ) ;
case 2 :
2015-10-14 22:58:55 +02:00
return new VimeoDownload ( QUrl ( res ) ) ;
2015-04-22 19:32:04 +02:00
case 3 :
2015-10-14 22:58:55 +02:00
return new GroovesharkDownload ( res ) ;
2015-08-29 21:34:47 +02:00
case 4 :
2015-10-14 22:58:55 +02:00
return new SockshareDownload ( QUrl ( res ) ) ;
2015-08-29 21:34:47 +02:00
case 5 :
2015-10-14 22:58:55 +02:00
return new BitshareDownload ( QUrl ( res ) ) ;
case 6 :
2015-04-22 19:32:04 +02:00
return new FileNukeDownload ( QUrl ( res ) ) ;
2015-08-29 21:34:47 +02:00
# ifdef UNDER_CONSTRUCTION
2015-10-14 22:58:55 +02:00
case 7 :
2015-08-29 21:34:47 +02:00
return new SpotifyDownload ( res ) ;
# endif
2015-04-22 19:32:04 +02:00
default :
return nullptr ;
}
}
void AddDownloadDialog : : reset ( )
{
m_downloadTypeIndexAdjustedManually = false ;
if ( m_ui - > urlLineEdit - > text ( ) ! = m_lastUrl ) {
m_validInput = false ;
m_ui - > urlLineEdit - > setText ( m_lastUrl ) ;
}
}
bool AddDownloadDialog : : hasValidInput ( ) const
{
return m_validInput ;
}
void AddDownloadDialog : : back ( )
{
setLastUrl ( ) ;
close ( ) ;
}
void AddDownloadDialog : : returnPressed ( )
{
if ( hasValidInput ( ) ) {
setLastUrl ( ) ;
emit addDownloadClicked ( ) ;
accept ( ) ;
}
}
void AddDownloadDialog : : insertTextFromClipboard ( )
{
QClipboard * clipboard = QApplication : : clipboard ( ) ;
QString text = clipboard - > text ( ) ;
if ( text . isEmpty ( ) ) {
QMessageBox : : warning ( this , windowTitle ( ) , tr ( " The clipboard does not contain any text. " ) ) ;
} else {
m_ui - > urlLineEdit - > setText ( text ) ;
}
}
void AddDownloadDialog : : textChanged ( const QString & text )
{
if ( ! text . isEmpty ( ) ) {
// entered value might be a grooveshark song id
bool isValidGroovesharkId = true ;
2015-10-14 22:58:55 +02:00
for ( const auto c : text ) {
2015-04-22 19:32:04 +02:00
if ( ! c . isDigit ( ) ) {
isValidGroovesharkId = false ;
break ;
}
2015-10-14 22:58:55 +02:00
}
2015-04-22 19:32:04 +02:00
if ( isValidGroovesharkId ) {
m_ui - > downloadTypeInfoWidget - > setHidden ( false ) ;
m_ui - > addPushButton - > setEnabled ( true ) ;
m_validInput = true ;
2015-10-14 22:58:55 +02:00
m_downloadTypeIndex = 3 ;
2015-04-22 19:32:04 +02:00
m_ui - > downloadTypeLabel - > setText ( tr ( " The entered number will be treated as Grooveshark song id. " ) ) ;
return ;
}
// parse entered text as url
QUrl url ( text ) ;
QString host = url . host ( ) ;
QString scheme = url . scheme ( ) ;
if ( ! ( host . isEmpty ( ) | | scheme . isEmpty ( ) ) ) {
m_ui - > downloadTypeInfoWidget - > setHidden ( false ) ;
// check if the protocol is supported
if ( scheme = = QLatin1String ( " http " ) | | scheme = = QLatin1String ( " https " ) | | scheme = = QLatin1String ( " ftp " ) ) {
m_ui - > addPushButton - > setEnabled ( true ) ;
m_validInput = true ;
// detect download type (if not adjusted manually)
if ( ! m_downloadTypeIndexAdjustedManually ) {
m_downloadTypeIndex = 0 ;
if ( host . contains ( QLatin1String ( " youtube " ) , Qt : : CaseInsensitive ) | | host . startsWith ( QLatin1String ( " youtu.be " ) ) ) {
m_downloadTypeIndex = 1 ;
2015-10-14 22:58:55 +02:00
} else if ( host . contains ( QLatin1String ( " vimeo " ) ) ) {
m_downloadTypeIndex = 2 ;
2015-04-22 19:32:04 +02:00
} else if ( host . contains ( QLatin1String ( " sockshare " ) ) | | host . contains ( QLatin1String ( " putlocker " ) ) ) {
m_downloadTypeIndex = 5 ;
2015-10-14 22:58:55 +02:00
} else if ( host . contains ( QLatin1String ( " bitshare " ) ) ) {
2015-04-22 19:32:04 +02:00
m_downloadTypeIndex = 6 ;
2015-10-14 22:58:55 +02:00
} else if ( host . contains ( QLatin1String ( " filenuke " ) ) ) {
m_downloadTypeIndex = 7 ;
2015-04-22 19:32:04 +02:00
}
if ( m_downloadTypeIndex ) {
m_ui - > downloadTypeLabel - > setText ( tr ( " The entered url seems to be from %1 so it will be added as %1 download. " ) . arg ( m_knownDownloadTypeNames . at ( m_downloadTypeIndex ) ) ) ;
} else {
m_ui - > downloadTypeLabel - > setText ( tr ( " The entered url will be added as standard %1 download. " ) . arg ( scheme ) ) ;
}
}
} else {
m_ui - > addPushButton - > setEnabled ( false ) ;
m_validInput = false ;
m_ui - > downloadTypeLabel - > setText ( tr ( " The entered protocol is not supported. " ) ) ;
m_downloadTypeIndexAdjustedManually = false ;
}
return ;
}
}
m_ui - > downloadTypeInfoWidget - > setHidden ( true ) ;
m_ui - > addPushButton - > setEnabled ( false ) ;
m_validInput = false ;
m_downloadTypeIndexAdjustedManually = false ;
}
void AddDownloadDialog : : adjustDetectedDownloadType ( )
{
if ( ! m_selectDownloadTypeInputDialog ) {
m_selectDownloadTypeInputDialog = new QInputDialog ( this ) ;
m_selectDownloadTypeInputDialog - > setWindowTitle ( tr ( " Select download type " ) ) ;
m_selectDownloadTypeInputDialog - > setLabelText ( tr ( " Select as which kind of download the entered url should be treated. " ) ) ;
m_selectDownloadTypeInputDialog - > setComboBoxItems ( m_knownDownloadTypeNames ) ;
m_selectDownloadTypeInputDialog - > setComboBoxEditable ( false ) ;
m_selectDownloadTypeInputDialog - > setOption ( QInputDialog : : UseListViewForComboBoxItems ) ;
}
m_selectDownloadTypeInputDialog - > setTextValue ( m_knownDownloadTypeNames . at ( m_downloadTypeIndex ) ) ;
if ( m_selectDownloadTypeInputDialog - > exec ( ) ) {
m_downloadTypeIndex = m_knownDownloadTypeNames . indexOf ( m_selectDownloadTypeInputDialog - > textValue ( ) ) ;
m_ui - > downloadTypeLabel - > setText ( tr ( " The entered url will be added as %1 download (adjusted). " ) . arg ( m_knownDownloadTypeNames . at ( m_downloadTypeIndex ) ) ) ;
m_downloadTypeIndexAdjustedManually = true ;
}
}
void AddDownloadDialog : : setLastUrl ( )
{
QSettings settings ( QSettings : : IniFormat , QSettings : : UserScope , QApplication : : organizationName ( ) , QApplication : : applicationName ( ) ) ;
settings . beginGroup ( " adddownloaddialog " ) ;
m_lastUrl = m_ui - > urlLineEdit - > text ( ) ;
settings . setValue ( " lasturl " , m_lastUrl ) ;
settings . endGroup ( ) ;
}
}