2009-08-21 11:40:57 +04: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 FEEDDOWNLOADER_H
# define FEEDDOWNLOADER_H
# include <QString>
# include <QListWidget>
# include <QListWidgetItem>
# include <QInputDialog>
# include <QMessageBox>
# include <QRegExp>
2009-08-21 13:01:18 +04:00
# include <QMenu>
2009-08-21 14:17:00 +04:00
# include <QFile>
# include <QDataStream>
# include <QFileDialog>
2009-08-21 11:40:57 +04:00
# include "bittorrent.h"
2009-11-20 11:20:05 +03:00
# include "ui_feeddownloader.h"
2010-07-16 19:03:18 +04:00
# include "qinisettings.h"
2009-08-21 11:40:57 +04:00
2010-03-04 21:40:11 +03:00
# if QT_VERSION >= 0x040500
2009-11-29 00:46:46 +03:00
# include <QHash>
# else
# include <QMap>
# define QHash QMap
# define toHash toMap
# endif
2009-08-21 11:40:57 +04:00
class FeedFilter : public QHash < QString , QVariant > {
private :
bool valid ;
public :
FeedFilter ( ) : valid ( true ) { }
FeedFilter ( bool valid ) : valid ( valid ) { }
FeedFilter ( QHash < QString , QVariant > filter ) : QHash < QString , QVariant > ( filter ) , valid ( true ) { }
bool matches ( QString s ) {
2009-08-21 12:19:32 +04:00
QStringList match_tokens = getMatchingTokens ( ) ;
foreach ( const QString & token , match_tokens ) {
2009-08-21 14:17:00 +04:00
if ( token . isEmpty ( ) | | token = = " " )
2009-08-21 12:19:32 +04:00
continue ;
2009-12-30 00:40:23 +03:00
QRegExp reg ( token , Qt : : CaseInsensitive , QRegExp : : Wildcard ) ;
//reg.setMinimal(false);
2009-08-21 11:40:57 +04:00
if ( reg . indexIn ( s ) < 0 ) return false ;
}
2009-12-30 00:40:23 +03:00
qDebug ( " Checking not matching tokens " ) ;
2009-08-21 12:19:32 +04:00
// Checking not matching
QStringList notmatch_tokens = getNotMatchingTokens ( ) ;
foreach ( const QString & token , notmatch_tokens ) {
if ( token . isEmpty ( ) ) continue ;
2009-12-30 00:42:31 +03:00
QRegExp reg ( token , Qt : : CaseInsensitive , QRegExp : : Wildcard ) ;
2009-08-21 12:19:32 +04:00
if ( reg . indexIn ( s ) > - 1 ) return false ;
}
2009-08-21 11:40:57 +04:00
return true ;
}
bool isValid ( ) const {
return valid ;
}
QStringList getMatchingTokens ( ) const {
2009-08-21 14:17:00 +04:00
QString matches = this - > value ( " matches " , " " ) . toString ( ) ;
2009-08-21 11:40:57 +04:00
return matches . split ( " " ) ;
}
QString getMatchingTokens_str ( ) const {
2009-08-21 22:27:45 +04:00
return this - > value ( " matches " , " " ) . toString ( ) ;
2009-08-21 11:40:57 +04:00
}
void setMatchingTokens ( QString tokens ) {
tokens = tokens . trimmed ( ) ;
if ( tokens . isEmpty ( ) )
2009-08-21 14:17:00 +04:00
( * this ) [ " matches " ] = " " ;
2009-08-21 11:40:57 +04:00
else
( * this ) [ " matches " ] = tokens ;
}
QStringList getNotMatchingTokens ( ) const {
QString notmatching = this - > value ( " not " , " " ) . toString ( ) ;
return notmatching . split ( " " ) ;
}
QString getNotMatchingTokens_str ( ) const {
return this - > value ( " not " , " " ) . toString ( ) ;
}
void setNotMatchingTokens ( QString tokens ) {
( * this ) [ " not " ] = tokens . trimmed ( ) ;
}
QString getSavePath ( ) const {
return this - > value ( " save_path " , " " ) . toString ( ) ;
}
void setSavePath ( QString save_path ) {
( * this ) [ " save_path " ] = save_path ;
}
} ;
class FeedFilters : public QHash < QString , QVariant > {
private :
QString feed_url ;
public :
FeedFilters ( ) { }
FeedFilters ( QString feed_url , QHash < QString , QVariant > filters ) : QHash < QString , QVariant > ( filters ) , feed_url ( feed_url ) { }
bool hasFilter ( QString name ) const {
return this - > contains ( name ) ;
}
FeedFilter * matches ( QString s ) {
if ( ! isDownloadingEnabled ( ) ) return 0 ;
if ( this - > size ( ) = = 0 ) return new FeedFilter ( false ) ;
foreach ( QVariant var_hash_filter , this - > values ( ) ) {
QHash < QString , QVariant > hash_filter = var_hash_filter . toHash ( ) ;
FeedFilter * filter = new FeedFilter ( hash_filter ) ;
if ( filter - > matches ( s ) )
return filter ;
else
delete filter ;
}
return 0 ;
}
QStringList names ( ) const {
return this - > keys ( ) ;
}
FeedFilter getFilter ( QString name ) const {
if ( this - > contains ( name ) )
return FeedFilter ( this - > value ( name ) . toHash ( ) ) ;
return FeedFilter ( ) ;
}
void setFilter ( QString name , FeedFilter f ) {
( * this ) [ name ] = f ;
}
bool isDownloadingEnabled ( ) const {
2010-07-16 19:03:18 +04:00
QIniSettings qBTRSS ( " qBittorrent " , " qBittorrent-rss " ) ;
2009-08-21 11:40:57 +04:00
QHash < QString , QVariant > feeds_w_downloader = qBTRSS . value ( " downloader_on " , QHash < QString , QVariant > ( ) ) . toHash ( ) ;
return feeds_w_downloader . value ( feed_url , false ) . toBool ( ) ;
}
void setDownloadingEnabled ( bool enabled ) {
2010-07-16 19:03:18 +04:00
QIniSettings qBTRSS ( " qBittorrent " , " qBittorrent-rss " ) ;
2009-08-21 11:40:57 +04:00
QHash < QString , QVariant > feeds_w_downloader = qBTRSS . value ( " downloader_on " , QHash < QString , QVariant > ( ) ) . toHash ( ) ;
feeds_w_downloader [ feed_url ] = enabled ;
qBTRSS . setValue ( " downloader_on " , feeds_w_downloader ) ;
}
static FeedFilters getFeedFilters ( QString url ) {
2010-07-16 19:03:18 +04:00
QIniSettings qBTRSS ( " qBittorrent " , " qBittorrent-rss " ) ;
2009-08-21 11:40:57 +04:00
QHash < QString , QVariant > all_feeds_filters = qBTRSS . value ( " feed_filters " , QHash < QString , QVariant > ( ) ) . toHash ( ) ;
return FeedFilters ( url , all_feeds_filters . value ( url , QHash < QString , QVariant > ( ) ) . toHash ( ) ) ;
}
2009-08-21 13:01:18 +04:00
void rename ( QString old_name , QString new_name ) {
Q_ASSERT ( this - > contains ( old_name ) ) ;
( * this ) [ new_name ] = this - > take ( old_name ) ;
}
2009-08-21 14:17:00 +04:00
bool serialize ( QString path ) {
QFile f ( path ) ;
if ( f . open ( QIODevice : : WriteOnly ) ) {
QDataStream out ( & f ) ;
out . setVersion ( QDataStream : : Qt_4_3 ) ;
out < < ( QHash < QString , QVariant > ) ( * this ) ;
f . close ( ) ;
return true ;
} else {
return false ;
}
}
bool unserialize ( QString path ) {
QFile f ( path ) ;
if ( f . open ( QIODevice : : ReadOnly ) ) {
QDataStream in ( & f ) ;
in . setVersion ( QDataStream : : Qt_4_3 ) ;
QHash < QString , QVariant > tmp ;
in > > tmp ;
qDebug ( " Unserialized %d filters " , tmp . size ( ) ) ;
foreach ( const QString & key , tmp . keys ( ) ) {
( * this ) [ key ] = tmp [ key ] ;
}
return true ;
} else {
return false ;
}
}
2009-08-21 11:40:57 +04:00
void save ( ) {
2010-07-16 19:03:18 +04:00
QIniSettings qBTRSS ( " qBittorrent " , " qBittorrent-rss " ) ;
2009-08-21 11:40:57 +04:00
QHash < QString , QVariant > all_feeds_filters = qBTRSS . value ( " feed_filters " , QHash < QString , QVariant > ( ) ) . toHash ( ) ;
2010-03-04 23:19:25 +03:00
qDebug ( " Saving filters for feed: %s (%d filters) " , qPrintable ( feed_url ) , ( * this ) . size ( ) ) ;
2009-08-21 11:40:57 +04:00
all_feeds_filters [ feed_url ] = * this ;
qBTRSS . setValue ( " feed_filters " , all_feeds_filters ) ;
}
} ;
class FeedDownloaderDlg : public QDialog , private Ui_FeedDownloader {
Q_OBJECT
private :
QString feed_url ;
QString feed_name ;
FeedFilters filters ;
2009-11-20 11:01:59 +03:00
Bittorrent * BTSession ;
2009-08-21 11:40:57 +04:00
QString selected_filter ; // name
public :
2009-11-20 11:01:59 +03:00
FeedDownloaderDlg ( QWidget * parent , QString feed_url , QString feed_name , Bittorrent * BTSession ) : QDialog ( parent ) , feed_url ( feed_url ) , feed_name ( feed_name ) , BTSession ( BTSession ) , selected_filter ( QString : : null ) {
2009-08-21 11:40:57 +04:00
setupUi ( this ) ;
setAttribute ( Qt : : WA_DeleteOnClose ) ;
Q_ASSERT ( ! feed_name . isEmpty ( ) ) ;
rssfeed_lbl - > setText ( feed_name ) ;
filters = FeedFilters : : getFeedFilters ( feed_url ) ;
// Connect Signals/Slots
connect ( filtersList , SIGNAL ( currentItemChanged ( QListWidgetItem * , QListWidgetItem * ) ) , this , SLOT ( showFilterSettings ( QListWidgetItem * ) ) ) ;
2009-08-21 13:01:18 +04:00
connect ( filtersList , SIGNAL ( customContextMenuRequested ( const QPoint & ) ) , this , SLOT ( displayFiltersListMenu ( const QPoint & ) ) ) ;
connect ( actionAdd_filter , SIGNAL ( triggered ( ) ) , this , SLOT ( addFilter ( ) ) ) ;
connect ( actionRemove_filter , SIGNAL ( triggered ( ) ) , this , SLOT ( deleteFilter ( ) ) ) ;
connect ( actionRename_filter , SIGNAL ( triggered ( ) ) , this , SLOT ( renameFilter ( ) ) ) ;
2009-08-21 11:40:57 +04:00
connect ( del_button , SIGNAL ( clicked ( bool ) ) , this , SLOT ( deleteFilter ( ) ) ) ;
connect ( add_button , SIGNAL ( clicked ( bool ) ) , this , SLOT ( addFilter ( ) ) ) ;
connect ( enableDl_cb , SIGNAL ( stateChanged ( int ) ) , this , SLOT ( enableFilterBox ( int ) ) ) ;
// Restore saved info
enableDl_cb - > setChecked ( filters . isDownloadingEnabled ( ) ) ;
2009-08-21 14:17:00 +04:00
fillFiltersList ( ) ;
2009-08-21 11:40:57 +04:00
if ( filters . size ( ) > 0 ) {
// Select first filter
filtersList - > setCurrentItem ( filtersList - > item ( 0 ) ) ;
//showFilterSettings(filtersList->item(0));
}
// Show
show ( ) ;
}
~ FeedDownloaderDlg ( ) {
2010-01-07 21:42:47 +03:00
if ( enableDl_cb - > isChecked ( ) )
emit filteringEnabled ( ) ;
2009-08-21 11:40:57 +04:00
// Make sure we save everything
saveCurrentFilterSettings ( ) ;
filters . save ( ) ;
}
protected slots :
void saveCurrentFilterSettings ( ) {
if ( ! selected_filter . isEmpty ( ) ) {
FeedFilter filter = filters . getFilter ( selected_filter ) ;
filter . setMatchingTokens ( match_line - > text ( ) ) ;
filter . setNotMatchingTokens ( notmatch_line - > text ( ) ) ;
QString save_path = savepath_line - > text ( ) . trimmed ( ) ;
if ( save_path . isEmpty ( ) )
save_path = BTSession - > getDefaultSavePath ( ) ;
filter . setSavePath ( save_path ) ;
// Save updated filter
filters . setFilter ( selected_filter , filter ) ;
}
}
2009-12-01 22:41:52 +03:00
void on_browse_button_clicked ( ) {
QString default_path = savepath_line - > text ( ) ;
if ( default_path . isEmpty ( ) | | ! QDir ( default_path ) . exists ( ) ) {
default_path = QDir : : homePath ( ) ;
}
QString dir = QFileDialog : : getExistingDirectory ( this , tr ( " Choose save path " ) , QDir : : homePath ( ) ) ;
2010-06-05 17:43:36 +04:00
if ( ! dir . isNull ( ) & & QDir ( dir ) . exists ( ) ) {
2009-12-01 22:41:52 +03:00
savepath_line - > setText ( dir ) ;
}
}
2009-08-21 14:17:00 +04:00
void fillFiltersList ( ) {
// Fill filter list
foreach ( QString filter_name , filters . names ( ) ) {
new QListWidgetItem ( filter_name , filtersList ) ;
}
}
2009-08-21 13:01:18 +04:00
void displayFiltersListMenu ( const QPoint & ) {
QMenu myFiltersListMenu ( this ) ;
if ( filtersList - > selectedItems ( ) . size ( ) > 0 ) {
myFiltersListMenu . addAction ( actionRename_filter ) ;
myFiltersListMenu . addAction ( actionRemove_filter ) ;
} else {
myFiltersListMenu . addAction ( actionAdd_filter ) ;
}
// Call menu
myFiltersListMenu . exec ( QCursor : : pos ( ) ) ;
}
2009-08-21 11:40:57 +04:00
void showFilterSettings ( QListWidgetItem * item ) {
// First, save current filter settings
saveCurrentFilterSettings ( ) ;
2009-08-21 12:19:32 +04:00
// Clear all fields
clearFields ( ) ;
2009-08-21 11:40:57 +04:00
if ( ! item ) {
qDebug ( " No new selected item " ) ;
return ;
}
// Actually show filter settings
QString filter_name = item - > text ( ) ;
FeedFilter filter = filters . getFilter ( filter_name ) ;
filterSettingsBox - > setEnabled ( true ) ;
match_line - > setText ( filter . getMatchingTokens_str ( ) ) ;
2009-08-21 14:17:00 +04:00
if ( match_line - > text ( ) . trimmed ( ) . isEmpty ( ) ) {
match_line - > setText ( filter_name ) ;
}
2009-08-21 11:40:57 +04:00
notmatch_line - > setText ( filter . getNotMatchingTokens_str ( ) ) ;
QString save_path = filter . getSavePath ( ) ;
if ( save_path . isEmpty ( ) )
save_path = BTSession - > getDefaultSavePath ( ) ;
savepath_line - > setText ( save_path ) ;
// Update selected filter
selected_filter = filter_name ;
}
void deleteFilter ( ) {
QList < QListWidgetItem * > items = filtersList - > selectedItems ( ) ;
if ( items . size ( ) = = 1 ) {
QListWidgetItem * item = items . first ( ) ;
2009-08-21 13:01:18 +04:00
filters . remove ( item - > text ( ) ) ;
selected_filter = QString : : null ;
2009-08-21 11:40:57 +04:00
delete item ;
// Reset Filter settings view
if ( filters . size ( ) = = 0 ) {
clearFields ( ) ;
filterSettingsBox - > setEnabled ( false ) ;
}
}
}
2009-08-21 13:01:18 +04:00
void renameFilter ( ) {
QList < QListWidgetItem * > items = filtersList - > selectedItems ( ) ;
if ( items . size ( ) = = 1 ) {
QListWidgetItem * item = items . first ( ) ;
QString current_name = item - > text ( ) ;
QString new_name ;
bool validated = false ;
do {
new_name = askFilterName ( current_name ) ;
if ( new_name . isNull ( ) | | new_name = = current_name ) return ;
if ( ! filters . hasFilter ( new_name ) ) {
validated = true ;
} else {
2009-08-23 16:22:19 +04:00
QMessageBox : : warning ( 0 , tr ( " Invalid filter name " ) , tr ( " This filter name is already in use. " ) ) ;
2009-08-21 13:01:18 +04:00
}
} while ( ! validated ) ;
2010-01-11 17:39:58 +03:00
// Save the current filter
saveCurrentFilterSettings ( ) ;
2009-08-21 13:01:18 +04:00
// Rename the filter
filters . rename ( current_name , new_name ) ;
if ( selected_filter = = current_name )
selected_filter = new_name ;
item - > setText ( new_name ) ;
}
}
2009-08-21 11:40:57 +04:00
void enableFilterBox ( int state ) {
if ( state = = Qt : : Checked ) {
filtersBox - > setEnabled ( true ) ;
filters . setDownloadingEnabled ( true ) ;
} else {
filtersBox - > setEnabled ( false ) ;
filters . setDownloadingEnabled ( false ) ;
}
}
QString askFilterName ( QString name = QString : : null ) {
QString name_prop ;
if ( name . isEmpty ( ) )
name_prop = tr ( " New filter " ) ;
else
name_prop = name ;
QString new_name ;
bool validated = false ;
do {
bool ok ;
new_name = QInputDialog : : getText ( this , tr ( " Please choose a name for this filter " ) , tr ( " Filter name: " ) , QLineEdit : : Normal , name_prop , & ok ) ;
if ( ! ok ) {
return QString : : null ;
}
// Validate filter name
new_name = new_name . trimmed ( ) ;
if ( new_name . isEmpty ( ) ) {
// Cannot be left empty
2009-08-23 16:22:19 +04:00
QMessageBox : : warning ( 0 , tr ( " Invalid filter name " ) , tr ( " The filter name cannot be left empty. " ) ) ;
2009-08-21 11:40:57 +04:00
} else {
validated = true ;
}
} while ( ! validated ) ;
return new_name ;
}
void addFilter ( ) {
QString filter_name = QString : : null ;
bool validated = false ;
do {
filter_name = askFilterName ( ) ;
2009-08-21 13:01:18 +04:00
if ( filter_name . isNull ( ) ) return ;
2009-08-21 11:40:57 +04:00
if ( filters . hasFilter ( filter_name ) ) {
// Filter alread exists
2009-08-23 16:22:19 +04:00
QMessageBox : : warning ( 0 , tr ( " Invalid filter name " ) , tr ( " This filter name is already in use. " ) ) ;
2009-08-21 11:40:57 +04:00
} else {
validated = true ;
}
} while ( ! validated ) ;
QListWidgetItem * it = new QListWidgetItem ( filter_name , filtersList ) ;
filtersList - > setCurrentItem ( it ) ;
//showFilterSettings(it);
}
void clearFields ( ) {
match_line - > clear ( ) ;
notmatch_line - > clear ( ) ;
savepath_line - > clear ( ) ;
2009-08-21 12:19:32 +04:00
test_res_lbl - > setText ( " " ) ;
test_line - > clear ( ) ;
}
void on_testButton_clicked ( bool ) {
2009-12-01 22:41:52 +03:00
test_res_lbl - > clear ( ) ;
if ( selected_filter . isEmpty ( ) ) {
qDebug ( " No filter is selected!!! " ) ;
return ;
}
2009-08-21 12:19:32 +04:00
QString s = test_line - > text ( ) . trimmed ( ) ;
if ( s . isEmpty ( ) ) {
2009-08-23 16:22:19 +04:00
QMessageBox : : warning ( 0 , tr ( " Filter testing error " ) , tr ( " Please specify a test torrent name. " ) ) ;
2009-08-21 12:19:32 +04:00
return ;
}
// Get current filter
saveCurrentFilterSettings ( ) ;
FeedFilter f = filters . getFilter ( selected_filter ) ;
if ( f . matches ( s ) )
test_res_lbl - > setText ( " <b><font color= \" green \" > " + tr ( " matches " ) + " </font></b> " ) ;
else
test_res_lbl - > setText ( " <b><font color= \" red \" > " + tr ( " does not match " ) + " </font></b> " ) ;
2009-08-21 11:40:57 +04:00
}
2009-08-21 14:17:00 +04:00
void on_importButton_clicked ( bool ) {
QString source = QFileDialog : : getOpenFileName ( 0 , tr ( " Select file to import " ) , QDir : : homePath ( ) , tr ( " Filters Files " ) + QString : : fromUtf8 ( " (*.filters) " ) ) ;
if ( source . isEmpty ( ) ) return ;
if ( filters . unserialize ( source ) ) {
// Clean up first
clearFields ( ) ;
filtersList - > clear ( ) ;
selected_filter = QString : : null ;
fillFiltersList ( ) ;
if ( filters . size ( ) > 0 )
filtersList - > setCurrentItem ( filtersList - > item ( 0 ) ) ;
QMessageBox : : information ( 0 , tr ( " Import successful " ) , tr ( " Filters import was successful. " ) ) ;
} else {
2009-08-23 16:22:19 +04:00
QMessageBox : : warning ( 0 , tr ( " Import failure " ) , tr ( " Filters could not be imported due to an I/O error. " ) ) ;
2009-08-21 14:17:00 +04:00
}
}
void on_exportButton_clicked ( bool ) {
QString destination = QFileDialog : : getSaveFileName ( this , tr ( " Select destination file " ) , QDir : : homePath ( ) , tr ( " Filters Files " ) + QString : : fromUtf8 ( " (*.filters) " ) ) ;
if ( destination . isEmpty ( ) ) return ;
// Append file extension
if ( ! destination . endsWith ( " .filters " ) )
destination + = " .filters " ;
2010-01-11 17:39:58 +03:00
/*if(QFile::exists(destination)) {
2009-08-22 17:44:05 +04:00
int ret = QMessageBox : : question ( 0 , tr ( " Overwriting confirmation " ) , tr ( " Are you sure you want to overwrite existing file? " ) , QMessageBox : : Yes | QMessageBox : : No ) ;
2009-08-21 14:17:00 +04:00
if ( ret ! = QMessageBox : : Yes ) return ;
2010-01-11 17:39:58 +03:00
} */
2009-08-21 14:17:00 +04:00
if ( filters . serialize ( destination ) )
QMessageBox : : information ( 0 , tr ( " Export successful " ) , tr ( " Filters export was successful. " ) ) ;
else
2009-08-23 16:22:19 +04:00
QMessageBox : : warning ( 0 , tr ( " Export failure " ) , tr ( " Filters could not be exported due to an I/O error. " ) ) ;
2009-08-21 14:17:00 +04:00
}
2010-01-07 21:42:47 +03:00
signals :
void filteringEnabled ( ) ;
2009-08-21 11:40:57 +04:00
} ;
2009-11-29 00:46:46 +03:00
# undef QHash
# undef toHash
2009-08-21 11:40:57 +04:00
# endif // FEEDDOWNLOADER_H