2010-06-22 17:39:49 +04:00
/*
* Bittorrent Client using Qt4 and libtorrent .
* Copyright ( C ) 2010 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
*/
2010-11-13 13:49:22 +03:00
# include <QMessageBox>
# include <QFileDialog>
# include <QDebug>
2010-11-13 18:51:36 +03:00
# include <QMenu>
# include <QCursor>
2010-11-13 13:49:22 +03:00
2010-10-31 15:35:07 +03:00
# include "automatedrssdownloader.h"
# include "ui_automatedrssdownloader.h"
2010-11-13 13:49:22 +03:00
# include "rssdownloadrulelist.h"
2015-04-13 19:19:53 +03:00
# include "core/preferences.h"
2010-11-13 22:36:46 +03:00
# include "rssmanager.h"
# include "rssfeed.h"
2015-04-19 18:17:47 +03:00
# include "guiiconprovider.h"
2013-07-22 15:46:10 +04:00
# include "autoexpandabledialog.h"
2015-05-06 14:53:27 +03:00
# include "core/utils/fs.h"
2010-06-22 17:39:49 +04:00
2012-02-19 20:53:10 +04:00
AutomatedRssDownloader : : AutomatedRssDownloader ( const QWeakPointer < RssManager > & manager , QWidget * parent ) :
2010-11-13 13:49:22 +03:00
QDialog ( parent ) ,
2010-11-13 23:08:44 +03:00
ui ( new Ui : : AutomatedRssDownloader ) ,
2012-02-19 20:53:10 +04:00
m_manager ( manager ) , m_editedRule ( 0 )
2010-06-22 17:39:49 +04:00
{
2010-11-13 13:49:22 +03:00
ui - > setupUi ( this ) ;
2010-12-12 22:37:59 +03:00
// Icons
2015-04-19 18:17:47 +03:00
ui - > removeRuleBtn - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " list-remove " ) ) ;
ui - > addRuleBtn - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " list-add " ) ) ;
2010-12-12 22:37:59 +03:00
2010-12-04 12:27:28 +03:00
// Ui Settings
2010-11-13 13:49:22 +03:00
ui - > listRules - > setSortingEnabled ( true ) ;
2010-11-13 19:08:08 +03:00
ui - > listRules - > setSelectionMode ( QAbstractItemView : : ExtendedSelection ) ;
2010-11-13 22:36:46 +03:00
ui - > treeMatchingArticles - > setSortingEnabled ( true ) ;
2010-12-04 12:27:28 +03:00
ui - > hsplitter - > setCollapsible ( 0 , false ) ;
ui - > hsplitter - > setCollapsible ( 1 , false ) ;
ui - > hsplitter - > setCollapsible ( 2 , true ) ; // Only the preview list is collapsible
2011-04-18 14:49:06 +04:00
bool ok ; Q_UNUSED ( ok ) ;
ok = connect ( ui - > checkRegex , SIGNAL ( toggled ( bool ) ) , SLOT ( updateFieldsToolTips ( bool ) ) ) ;
Q_ASSERT ( ok ) ;
ok = connect ( ui - > listRules , SIGNAL ( customContextMenuRequested ( const QPoint & ) ) , this , SLOT ( displayRulesListMenu ( const QPoint & ) ) ) ;
Q_ASSERT ( ok ) ;
2012-02-20 21:49:35 +04:00
m_ruleList = manager . toStrongRef ( ) - > downloadRules ( ) ;
2013-07-01 12:43:40 +04:00
m_editableRuleList = new RssDownloadRuleList ; // Read rule list from disk
2013-07-24 15:30:58 +04:00
m_episodeValidator = new QRegExpValidator (
QRegExp ( " ^(^[1-9]{1,1} \\ d{0,3}x([1-9]{1,1} \\ d{0,3}(-([1-9]{1,1} \\ d{0,3})?)?;){1,}){1,1} " ,
Qt : : CaseInsensitive ) ,
ui - > lineEFilter ) ;
ui - > lineEFilter - > setValidator ( m_episodeValidator ) ;
2013-07-24 16:33:28 +04:00
QString tip = " <p> " + tr ( " Matches articles based on episode filter. " ) + " </p><p><b> " + tr ( " Example: " ) +
" 1x2;8-15;5;30-;</b> " + tr ( " will match 2, 5, 8 through 15, 30 and onward episodes of season one " , " example X will match " ) + " </p> " ;
tip + = " <p> " + tr ( " Episode filter rules: " ) + " </p><ul><li> " + tr ( " Season number is a mandatory non-zero value " ) + " </li> " +
" <li> " + tr ( " Episode number is a mandatory non-zero value " ) + " </li> " +
" <li> " + tr ( " Filter must end with semicolon " ) + " </li> " +
" <li> " + tr ( " Three range types for episodes are supported: " ) + " </li> " + " <li><ul> "
" <li> " + tr ( " Single number: <b>1x25;</b> matches episode 25 of season one " ) + " </li> " +
" <li> " + tr ( " Normal range: <b>1x25-40;</b> matches episodes 25 through 40 of season one " ) + " </li> " +
2014-12-13 17:14:13 +03:00
" <li> " + tr ( " Infinite range: <b>1x25-;</b> matches episodes 25 and upward of season one " ) + " </li> " + " </ul></li></ul> " ;
2013-07-24 16:33:28 +04:00
ui - > lineEFilter - > setToolTip ( tip ) ;
2010-11-13 13:49:22 +03:00
initLabelCombobox ( ) ;
loadFeedList ( ) ;
loadSettings ( ) ;
2011-04-18 14:49:06 +04:00
ok = connect ( ui - > listRules , SIGNAL ( itemSelectionChanged ( ) ) , SLOT ( updateRuleDefinitionBox ( ) ) ) ;
Q_ASSERT ( ok ) ;
ok = connect ( ui - > listRules , SIGNAL ( itemSelectionChanged ( ) ) , SLOT ( updateFeedList ( ) ) ) ;
Q_ASSERT ( ok ) ;
ok = connect ( ui - > listFeeds , SIGNAL ( itemChanged ( QListWidgetItem * ) ) , SLOT ( handleFeedCheckStateChange ( QListWidgetItem * ) ) ) ;
Q_ASSERT ( ok ) ;
2010-11-13 22:36:46 +03:00
// Update matching articles when necessary
2011-04-18 14:49:06 +04:00
ok = connect ( ui - > lineContains , SIGNAL ( textEdited ( QString ) ) , SLOT ( updateMatchingArticles ( ) ) ) ;
Q_ASSERT ( ok ) ;
2011-04-18 19:56:22 +04:00
ok = connect ( ui - > lineContains , SIGNAL ( textEdited ( QString ) ) , SLOT ( updateMustLineValidity ( ) ) ) ;
Q_ASSERT ( ok ) ;
2011-04-18 14:49:06 +04:00
ok = connect ( ui - > lineNotContains , SIGNAL ( textEdited ( QString ) ) , SLOT ( updateMatchingArticles ( ) ) ) ;
Q_ASSERT ( ok ) ;
2011-04-18 19:56:22 +04:00
ok = connect ( ui - > lineNotContains , SIGNAL ( textEdited ( QString ) ) , SLOT ( updateMustNotLineValidity ( ) ) ) ;
Q_ASSERT ( ok ) ;
2013-02-11 13:58:00 +04:00
ok = connect ( ui - > checkRegex , SIGNAL ( stateChanged ( int ) ) , SLOT ( updateMatchingArticles ( ) ) ) ;
Q_ASSERT ( ok ) ;
ok = connect ( ui - > checkRegex , SIGNAL ( stateChanged ( int ) ) , SLOT ( updateMustLineValidity ( ) ) ) ;
Q_ASSERT ( ok ) ;
ok = connect ( ui - > checkRegex , SIGNAL ( stateChanged ( int ) ) , SLOT ( updateMustNotLineValidity ( ) ) ) ;
Q_ASSERT ( ok ) ;
2015-02-11 03:39:27 +03:00
ok = connect ( this , SIGNAL ( finished ( int ) ) , SLOT ( onFinished ( int ) ) ) ;
2013-07-01 12:52:23 +04:00
Q_ASSERT ( ok ) ;
2013-07-24 15:30:58 +04:00
ok = connect ( ui - > lineEFilter , SIGNAL ( textEdited ( QString ) ) , SLOT ( updateMatchingArticles ( ) ) ) ;
Q_ASSERT ( ok ) ;
2013-07-22 22:38:57 +04:00
editHotkey = new QShortcut ( QKeySequence ( " F2 " ) , ui - > listRules , 0 , 0 , Qt : : WidgetShortcut ) ;
ok = connect ( editHotkey , SIGNAL ( activated ( ) ) , SLOT ( renameSelectedRule ( ) ) ) ;
Q_ASSERT ( ok ) ;
ok = connect ( ui - > listRules , SIGNAL ( doubleClicked ( QModelIndex ) ) , SLOT ( renameSelectedRule ( ) ) ) ;
Q_ASSERT ( ok ) ;
deleteHotkey = new QShortcut ( QKeySequence ( QKeySequence : : Delete ) , ui - > listRules , 0 , 0 , Qt : : WidgetShortcut ) ;
ok = connect ( deleteHotkey , SIGNAL ( activated ( ) ) , SLOT ( on_removeRuleBtn_clicked ( ) ) ) ;
Q_ASSERT ( ok ) ;
2010-11-13 18:18:56 +03:00
updateRuleDefinitionBox ( ) ;
2010-11-13 20:12:13 +03:00
updateFeedList ( ) ;
2010-06-22 17:39:49 +04:00
}
2010-10-31 15:35:07 +03:00
AutomatedRssDownloader : : ~ AutomatedRssDownloader ( )
2010-06-22 17:39:49 +04:00
{
2010-11-13 13:49:22 +03:00
qDebug ( ) < < Q_FUNC_INFO ;
2013-07-22 22:38:57 +04:00
delete editHotkey ;
delete deleteHotkey ;
2010-10-31 15:35:07 +03:00
delete ui ;
2013-07-01 12:43:40 +04:00
delete m_editableRuleList ;
2013-07-24 15:30:58 +04:00
delete m_episodeValidator ;
2010-06-22 17:39:49 +04:00
}
2010-10-31 15:35:07 +03:00
void AutomatedRssDownloader : : loadSettings ( )
{
2010-11-13 14:04:40 +03:00
// load dialog geometry
2014-07-05 16:44:13 +04:00
const Preferences * const pref = Preferences : : instance ( ) ;
restoreGeometry ( pref - > getRssGeometry ( ) ) ;
ui - > checkEnableDownloader - > setChecked ( pref - > isRssDownloadingEnabled ( ) ) ;
ui - > hsplitter - > restoreState ( pref - > getRssHSplitterSizes ( ) ) ;
2010-11-13 13:49:22 +03:00
// Display download rules
loadRulesList ( ) ;
2010-10-31 15:35:07 +03:00
}
void AutomatedRssDownloader : : saveSettings ( )
{
2014-07-05 16:44:13 +04:00
Preferences : : instance ( ) - > setRssDownloadingEnabled ( ui - > checkEnableDownloader - > isChecked ( ) ) ;
2010-11-13 14:04:40 +03:00
// Save dialog geometry
2014-07-05 16:44:13 +04:00
Preferences * const pref = Preferences : : instance ( ) ;
pref - > setRssGeometry ( saveGeometry ( ) ) ;
pref - > setRssHSplitterSizes ( ui - > hsplitter - > saveState ( ) ) ;
2010-11-13 13:49:22 +03:00
}
void AutomatedRssDownloader : : loadRulesList ( )
{
2010-11-13 18:14:50 +03:00
// Make sure we save the current item before clearing
2012-02-20 21:30:53 +04:00
if ( m_editedRule ) {
2010-11-13 23:08:44 +03:00
saveEditedRule ( ) ;
2010-11-13 18:14:50 +03:00
}
ui - > listRules - > clear ( ) ;
2013-07-01 12:43:40 +04:00
foreach ( const QString & rule_name , m_editableRuleList - > ruleNames ( ) ) {
2010-11-13 13:49:22 +03:00
QListWidgetItem * item = new QListWidgetItem ( rule_name , ui - > listRules ) ;
item - > setFlags ( item - > flags ( ) | Qt : : ItemIsUserCheckable ) ;
2013-07-01 12:43:40 +04:00
if ( m_editableRuleList - > getRule ( rule_name ) - > isEnabled ( ) )
2010-11-13 13:49:22 +03:00
item - > setCheckState ( Qt : : Checked ) ;
else
item - > setCheckState ( Qt : : Unchecked ) ;
}
2012-02-20 21:30:53 +04:00
if ( ui - > listRules - > count ( ) > 0 & & ! ui - > listRules - > currentItem ( ) )
2010-11-13 20:12:13 +03:00
ui - > listRules - > setCurrentRow ( 0 ) ;
2010-11-13 13:49:22 +03:00
}
void AutomatedRssDownloader : : loadFeedList ( )
{
2014-07-05 16:44:13 +04:00
const Preferences * const pref = Preferences : : instance ( ) ;
const QStringList feed_aliases = pref - > getRssFeedsAliases ( ) ;
const QStringList feed_urls = pref - > getRssFeedsUrls ( ) ;
2010-12-18 21:42:31 +03:00
QStringList existing_urls ;
2012-02-20 21:30:53 +04:00
for ( int i = 0 ; i < feed_aliases . size ( ) ; + + i ) {
2010-12-18 21:42:31 +03:00
QString feed_url = feed_urls . at ( i ) ;
feed_url = feed_url . split ( " \\ " ) . last ( ) ;
qDebug ( ) < < Q_FUNC_INFO < < feed_url ;
2012-02-20 21:30:53 +04:00
if ( existing_urls . contains ( feed_url ) ) continue ;
2010-11-13 13:49:22 +03:00
QListWidgetItem * item = new QListWidgetItem ( feed_aliases . at ( i ) , ui - > listFeeds ) ;
2010-12-18 21:42:31 +03:00
item - > setData ( Qt : : UserRole , feed_url ) ;
2010-11-13 13:49:22 +03:00
item - > setFlags ( item - > flags ( ) | Qt : : ItemIsUserCheckable ) ;
2010-12-18 21:42:31 +03:00
existing_urls < < feed_url ;
2010-11-13 13:49:22 +03:00
}
}
2010-11-13 20:12:13 +03:00
void AutomatedRssDownloader : : updateFeedList ( )
2010-11-13 13:49:22 +03:00
{
2010-11-13 21:08:09 +03:00
disconnect ( ui - > listFeeds , SIGNAL ( itemChanged ( QListWidgetItem * ) ) , this , SLOT ( handleFeedCheckStateChange ( QListWidgetItem * ) ) ) ;
2012-02-20 21:30:53 +04:00
for ( int i = 0 ; i < ui - > listFeeds - > count ( ) ; + + i ) {
2010-11-13 13:49:22 +03:00
QListWidgetItem * item = ui - > listFeeds - > item ( i ) ;
const QString feed_url = item - > data ( Qt : : UserRole ) . toString ( ) ;
2010-11-13 20:12:13 +03:00
bool all_enabled = false ;
2012-02-20 21:30:53 +04:00
foreach ( const QListWidgetItem * ruleItem , ui - > listRules - > selectedItems ( ) ) {
2013-07-01 12:43:40 +04:00
RssDownloadRulePtr rule = m_editableRuleList - > getRule ( ruleItem - > text ( ) ) ;
2012-02-20 21:30:53 +04:00
if ( ! rule ) continue ;
2012-02-19 20:53:10 +04:00
qDebug ( ) < < " Rule " < < rule - > name ( ) < < " affects " < < rule - > rssFeeds ( ) . size ( ) < < " feeds. " ;
2012-02-20 21:30:53 +04:00
foreach ( QString test , rule - > rssFeeds ( ) ) {
2010-11-13 21:08:09 +03:00
qDebug ( ) < < " Feed is " < < test ;
}
2012-02-20 21:30:53 +04:00
if ( rule - > rssFeeds ( ) . contains ( feed_url ) ) {
2012-02-19 20:53:10 +04:00
qDebug ( ) < < " Rule " < < rule - > name ( ) < < " affects feed " < < feed_url ;
2010-11-13 21:08:09 +03:00
all_enabled = true ;
} else {
2012-02-19 20:53:10 +04:00
qDebug ( ) < < " Rule " < < rule - > name ( ) < < " does NOT affect feed " < < feed_url ;
2010-11-13 21:08:09 +03:00
all_enabled = false ;
break ;
}
2010-11-13 20:12:13 +03:00
}
2012-02-20 21:30:53 +04:00
if ( all_enabled )
2010-11-13 13:49:22 +03:00
item - > setCheckState ( Qt : : Checked ) ;
else
item - > setCheckState ( Qt : : Unchecked ) ;
}
2010-11-13 20:12:13 +03:00
ui - > listFeeds - > setEnabled ( ! ui - > listRules - > selectedItems ( ) . isEmpty ( ) ) ;
2010-11-13 21:08:09 +03:00
connect ( ui - > listFeeds , SIGNAL ( itemChanged ( QListWidgetItem * ) ) , SLOT ( handleFeedCheckStateChange ( QListWidgetItem * ) ) ) ;
2010-11-13 22:36:46 +03:00
updateMatchingArticles ( ) ;
2010-11-13 13:49:22 +03:00
}
bool AutomatedRssDownloader : : isRssDownloaderEnabled ( ) const
{
return ui - > checkEnableDownloader - > isChecked ( ) ;
}
2010-11-13 20:12:13 +03:00
void AutomatedRssDownloader : : updateRuleDefinitionBox ( )
2010-11-13 13:49:22 +03:00
{
2010-11-13 20:12:13 +03:00
qDebug ( ) < < Q_FUNC_INFO ;
2010-11-13 23:08:44 +03:00
// Save previous rule first
saveEditedRule ( ) ;
2010-11-13 13:49:22 +03:00
// Update rule definition box
2010-11-13 19:08:08 +03:00
const QList < QListWidgetItem * > selection = ui - > listRules - > selectedItems ( ) ;
2012-02-20 21:30:53 +04:00
if ( selection . count ( ) = = 1 ) {
2010-11-13 23:08:44 +03:00
m_editedRule = selection . first ( ) ;
2012-02-19 20:53:10 +04:00
RssDownloadRulePtr rule = getCurrentRule ( ) ;
if ( rule ) {
ui - > lineContains - > setText ( rule - > mustContain ( ) ) ;
ui - > lineNotContains - > setText ( rule - > mustNotContain ( ) ) ;
2013-07-24 15:30:58 +04:00
QString ep = rule - > episodeFilter ( ) ;
if ( ! ep . isEmpty ( ) )
ui - > lineEFilter - > setText ( ep ) ;
else
ui - > lineEFilter - > clear ( ) ;
2012-02-19 20:53:10 +04:00
ui - > saveDiffDir_check - > setChecked ( ! rule - > savePath ( ) . isEmpty ( ) ) ;
2015-05-06 14:53:27 +03:00
ui - > lineSavePath - > setText ( Utils : : Fs : : toNativePath ( rule - > savePath ( ) ) ) ;
2012-02-19 20:53:10 +04:00
ui - > checkRegex - > setChecked ( rule - > useRegex ( ) ) ;
if ( rule - > label ( ) . isEmpty ( ) ) {
2010-11-13 20:12:13 +03:00
ui - > comboLabel - > setCurrentIndex ( - 1 ) ;
ui - > comboLabel - > clearEditText ( ) ;
} else {
2012-02-19 20:53:10 +04:00
ui - > comboLabel - > setCurrentIndex ( ui - > comboLabel - > findText ( rule - > label ( ) ) ) ;
2010-11-13 20:12:13 +03:00
}
2014-08-18 23:58:40 +04:00
ui - > comboAddPaused - > setCurrentIndex ( rule - > addPaused ( ) ) ;
2013-07-25 00:39:05 +04:00
ui - > spinIgnorePeriod - > setValue ( rule - > ignoreDays ( ) ) ;
QDateTime dateTime = rule - > lastMatch ( ) ;
QString lMatch = tr ( " Last match: " ) ;
if ( dateTime . isValid ( ) )
lMatch + = QString : : number ( dateTime . daysTo ( QDateTime : : currentDateTime ( ) ) ) + tr ( " days ago. " ) ;
else
lMatch + = tr ( " Unknown " ) ;
ui - > lblLastMatch - > setText ( lMatch ) ;
2011-04-18 19:56:22 +04:00
updateMustLineValidity ( ) ;
updateMustNotLineValidity ( ) ;
2010-11-13 13:49:22 +03:00
} else {
2010-11-13 20:12:13 +03:00
// New rule
clearRuleDefinitionBox ( ) ;
ui - > lineContains - > setText ( selection . first ( ) - > text ( ) ) ;
2014-08-18 23:58:40 +04:00
ui - > comboAddPaused - > setCurrentIndex ( 0 ) ;
2010-11-13 13:49:22 +03:00
}
2011-04-18 14:49:06 +04:00
updateFieldsToolTips ( ui - > checkRegex - > isChecked ( ) ) ;
2010-11-13 13:49:22 +03:00
// Enable
ui - > ruleDefBox - > setEnabled ( true ) ;
} else {
2010-11-13 23:08:44 +03:00
m_editedRule = 0 ;
2010-11-13 13:49:22 +03:00
// Clear
2010-11-13 20:12:13 +03:00
clearRuleDefinitionBox ( ) ;
ui - > ruleDefBox - > setEnabled ( false ) ;
2010-11-13 13:49:22 +03:00
}
}
2010-11-13 20:12:13 +03:00
void AutomatedRssDownloader : : clearRuleDefinitionBox ( )
{
ui - > lineContains - > clear ( ) ;
ui - > lineNotContains - > clear ( ) ;
ui - > saveDiffDir_check - > setChecked ( false ) ;
ui - > lineSavePath - > clear ( ) ;
ui - > comboLabel - > clearEditText ( ) ;
2011-04-18 14:36:19 +04:00
ui - > checkRegex - > setChecked ( false ) ;
2013-07-25 00:39:05 +04:00
ui - > spinIgnorePeriod - > setValue ( 0 ) ;
2011-04-18 14:49:06 +04:00
updateFieldsToolTips ( ui - > checkRegex - > isChecked ( ) ) ;
2011-04-18 19:56:22 +04:00
updateMustLineValidity ( ) ;
updateMustNotLineValidity ( ) ;
2010-11-13 20:12:13 +03:00
}
2012-02-19 20:53:10 +04:00
RssDownloadRulePtr AutomatedRssDownloader : : getCurrentRule ( ) const
2010-11-13 13:49:22 +03:00
{
QListWidgetItem * current_item = ui - > listRules - > currentItem ( ) ;
2012-02-20 21:30:53 +04:00
if ( current_item )
2013-07-01 12:43:40 +04:00
return m_editableRuleList - > getRule ( current_item - > text ( ) ) ;
2012-02-19 20:53:10 +04:00
return RssDownloadRulePtr ( ) ;
2010-11-13 13:49:22 +03:00
}
void AutomatedRssDownloader : : initLabelCombobox ( )
{
// Load custom labels
2014-07-05 16:44:13 +04:00
const QStringList customLabels = Preferences : : instance ( ) - > getTorrentLabels ( ) ;
2012-02-20 21:30:53 +04:00
foreach ( const QString & label , customLabels ) {
2010-11-13 13:49:22 +03:00
ui - > comboLabel - > addItem ( label ) ;
}
}
2010-11-13 23:08:44 +03:00
void AutomatedRssDownloader : : saveEditedRule ( )
2010-11-13 20:12:13 +03:00
{
2012-02-20 21:30:53 +04:00
if ( ! m_editedRule ) return ;
2010-11-13 23:08:44 +03:00
qDebug ( ) < < Q_FUNC_INFO < < m_editedRule ;
2012-02-20 21:30:53 +04:00
if ( ui - > listRules - > findItems ( m_editedRule - > text ( ) , Qt : : MatchExactly ) . isEmpty ( ) ) {
2010-11-13 23:08:44 +03:00
qDebug ( ) < < " Could not find rule " < < m_editedRule - > text ( ) < < " in the UI list " ;
2010-11-13 22:36:46 +03:00
qDebug ( ) < < " Probably removed the item, no need to save it " ;
return ;
}
2013-07-01 12:43:40 +04:00
RssDownloadRulePtr rule = m_editableRuleList - > getRule ( m_editedRule - > text ( ) ) ;
2012-02-19 20:53:10 +04:00
if ( ! rule ) {
2012-03-10 03:40:57 +04:00
rule = RssDownloadRulePtr ( new RssDownloadRule ) ;
2012-02-19 20:53:10 +04:00
rule - > setName ( m_editedRule - > text ( ) ) ;
2010-11-13 13:49:22 +03:00
}
2012-02-19 20:53:10 +04:00
if ( m_editedRule - > checkState ( ) = = Qt : : Unchecked )
rule - > setEnabled ( false ) ;
2010-11-13 13:49:22 +03:00
else
2012-02-19 20:53:10 +04:00
rule - > setEnabled ( true ) ;
rule - > setUseRegex ( ui - > checkRegex - > isChecked ( ) ) ;
rule - > setMustContain ( ui - > lineContains - > text ( ) ) ;
rule - > setMustNotContain ( ui - > lineNotContains - > text ( ) ) ;
2013-07-24 15:30:58 +04:00
rule - > setEpisodeFilter ( ui - > lineEFilter - > text ( ) ) ;
2012-02-20 21:30:53 +04:00
if ( ui - > saveDiffDir_check - > isChecked ( ) )
2012-02-19 20:53:10 +04:00
rule - > setSavePath ( ui - > lineSavePath - > text ( ) ) ;
2010-11-13 13:49:22 +03:00
else
2012-02-19 20:53:10 +04:00
rule - > setSavePath ( " " ) ;
rule - > setLabel ( ui - > comboLabel - > currentText ( ) ) ;
2014-08-18 23:58:40 +04:00
rule - > setAddPaused ( RssDownloadRule : : AddPausedState ( ui - > comboAddPaused - > currentIndex ( ) ) ) ;
2010-11-13 13:49:22 +03:00
// Save new label
2012-02-20 21:30:53 +04:00
if ( ! rule - > label ( ) . isEmpty ( ) )
2014-07-05 16:44:13 +04:00
Preferences : : instance ( ) - > addTorrentLabel ( rule - > label ( ) ) ;
2013-07-25 00:39:05 +04:00
rule - > setIgnoreDays ( ui - > spinIgnorePeriod - > value ( ) ) ;
2012-02-19 20:53:10 +04:00
//rule->setRssFeeds(getSelectedFeeds());
2010-11-13 13:49:22 +03:00
// Save it
2013-07-01 12:43:40 +04:00
m_editableRuleList - > saveRule ( rule ) ;
2010-11-13 13:49:22 +03:00
}
void AutomatedRssDownloader : : on_addRuleBtn_clicked ( )
{
// Ask for a rule name
2013-07-22 15:46:10 +04:00
const QString rule_name = AutoExpandableDialog : : getText ( this , tr ( " New rule name " ) , tr ( " Please type the name of the new download rule. " ) ) ;
2012-02-20 21:30:53 +04:00
if ( rule_name . isEmpty ( ) ) return ;
2010-11-13 13:49:22 +03:00
// Check if this rule name already exists
2013-07-01 12:43:40 +04:00
if ( m_editableRuleList - > getRule ( rule_name ) ) {
2010-11-13 13:49:22 +03:00
QMessageBox : : warning ( this , tr ( " Rule name conflict " ) , tr ( " A rule with this name already exists, please choose another name. " ) ) ;
return ;
}
// Add the new rule to the list
2012-02-19 20:53:10 +04:00
QListWidgetItem * item = new QListWidgetItem ( rule_name , ui - > listRules ) ;
2010-11-13 13:49:22 +03:00
item - > setFlags ( item - > flags ( ) | Qt : : ItemIsUserCheckable ) ;
item - > setCheckState ( Qt : : Checked ) ; // Enable as a default
2010-11-13 22:36:46 +03:00
ui - > listRules - > clearSelection ( ) ;
2010-11-13 13:49:22 +03:00
ui - > listRules - > setCurrentItem ( item ) ;
}
void AutomatedRssDownloader : : on_removeRuleBtn_clicked ( )
{
2010-11-13 19:00:14 +03:00
const QList < QListWidgetItem * > selection = ui - > listRules - > selectedItems ( ) ;
2012-02-20 21:30:53 +04:00
if ( selection . isEmpty ( ) ) return ;
2010-11-13 13:49:22 +03:00
// Ask for confirmation
2010-11-13 19:00:14 +03:00
QString confirm_text ;
2012-02-20 21:30:53 +04:00
if ( selection . count ( ) = = 1 )
2010-11-13 19:00:14 +03:00
confirm_text = tr ( " Are you sure you want to remove the download rule named %1? " ) . arg ( selection . first ( ) - > text ( ) ) ;
else
confirm_text = tr ( " Are you sure you want to remove the selected download rules? " ) ;
2012-02-20 21:30:53 +04:00
if ( QMessageBox : : question ( this , tr ( " Rule deletion confirmation " ) , confirm_text , QMessageBox : : Yes , QMessageBox : : No ) ! = QMessageBox : : Yes )
2010-11-13 13:49:22 +03:00
return ;
2012-02-20 21:30:53 +04:00
foreach ( QListWidgetItem * item , selection ) {
2010-11-13 19:00:14 +03:00
// Actually remove the item
ui - > listRules - > removeItemWidget ( item ) ;
2010-11-13 23:08:44 +03:00
const QString rule_name = item - > text ( ) ;
2010-11-13 19:00:14 +03:00
// Clean up memory
delete item ;
2010-11-13 23:08:44 +03:00
qDebug ( " Removed item for the UI list " ) ;
2013-07-01 12:43:40 +04:00
// Remove it from the m_editableRuleList
m_editableRuleList - > removeRule ( rule_name ) ;
2010-11-13 19:00:14 +03:00
}
2010-11-13 13:49:22 +03:00
}
void AutomatedRssDownloader : : on_browseSP_clicked ( )
{
QString save_path = QFileDialog : : getExistingDirectory ( this , tr ( " Destination directory " ) , QDir : : homePath ( ) ) ;
2012-02-20 21:30:53 +04:00
if ( ! save_path . isEmpty ( ) )
2015-05-06 14:53:27 +03:00
ui - > lineSavePath - > setText ( Utils : : Fs : : toNativePath ( save_path ) ) ;
2010-06-22 17:39:49 +04:00
}
2010-11-13 15:08:29 +03:00
void AutomatedRssDownloader : : on_exportBtn_clicked ( )
{
2013-07-01 12:43:40 +04:00
if ( m_editableRuleList - > isEmpty ( ) ) {
2010-11-13 15:08:29 +03:00
QMessageBox : : warning ( this , tr ( " Invalid action " ) , tr ( " The list is empty, there is nothing to export. " ) ) ;
return ;
}
// Ask for a save path
2010-11-13 15:33:52 +03:00
QString save_path = QFileDialog : : getSaveFileName ( this , tr ( " Where would you like to save the list? " ) , QDir : : homePath ( ) , tr ( " Rules list (*.rssrules) " ) ) ;
2012-02-20 21:30:53 +04:00
if ( save_path . isEmpty ( ) ) return ;
if ( ! save_path . endsWith ( " .rssrules " , Qt : : CaseInsensitive ) )
2010-11-13 15:08:29 +03:00
save_path + = " .rssrules " ;
2013-07-01 12:43:40 +04:00
if ( ! m_editableRuleList - > serialize ( save_path ) ) {
2010-11-13 15:08:29 +03:00
QMessageBox : : warning ( this , tr ( " I/O Error " ) , tr ( " Failed to create the destination file " ) ) ;
return ;
}
}
void AutomatedRssDownloader : : on_importBtn_clicked ( )
{
2010-11-13 15:33:52 +03:00
// Ask for filter path
2014-08-30 22:54:57 +04:00
QString load_path = QFileDialog : : getOpenFileName ( this , tr ( " Please point to the RSS download rules file " ) , QDir : : homePath ( ) , tr ( " Rules list " ) + QString ( " (*.rssrules *.filters) " ) ) ;
2012-02-20 21:30:53 +04:00
if ( load_path . isEmpty ( ) | | ! QFile : : exists ( load_path ) ) return ;
2010-11-13 15:33:52 +03:00
// Load it
2013-07-01 12:43:40 +04:00
if ( ! m_editableRuleList - > unserialize ( load_path ) ) {
2010-11-13 15:33:52 +03:00
QMessageBox : : warning ( this , tr ( " Import Error " ) , tr ( " Failed to import the selected rules file " ) ) ;
return ;
}
2010-11-13 18:14:50 +03:00
// Reload the rule list
loadRulesList ( ) ;
2010-11-13 15:08:29 +03:00
}
2010-11-13 18:51:36 +03:00
void AutomatedRssDownloader : : displayRulesListMenu ( const QPoint & pos )
{
Q_UNUSED ( pos ) ;
QMenu menu ;
2015-04-19 18:17:47 +03:00
QAction * addAct = menu . addAction ( GuiIconProvider : : instance ( ) - > getIcon ( " list-add " ) , tr ( " Add new rule... " ) ) ;
2010-11-13 18:51:36 +03:00
QAction * delAct = 0 ;
QAction * renameAct = 0 ;
2010-11-13 19:08:08 +03:00
const QList < QListWidgetItem * > selection = ui - > listRules - > selectedItems ( ) ;
2012-02-20 21:30:53 +04:00
if ( ! selection . isEmpty ( ) ) {
if ( selection . count ( ) = = 1 ) {
2015-04-19 18:17:47 +03:00
delAct = menu . addAction ( GuiIconProvider : : instance ( ) - > getIcon ( " list-remove " ) , tr ( " Delete rule " ) ) ;
2010-11-13 19:00:14 +03:00
menu . addSeparator ( ) ;
2015-04-19 18:17:47 +03:00
renameAct = menu . addAction ( GuiIconProvider : : instance ( ) - > getIcon ( " edit-rename " ) , tr ( " Rename rule... " ) ) ;
2010-11-13 19:00:14 +03:00
} else {
2015-04-19 18:17:47 +03:00
delAct = menu . addAction ( GuiIconProvider : : instance ( ) - > getIcon ( " list-remove " ) , tr ( " Delete selected rules " ) ) ;
2010-11-13 19:00:14 +03:00
}
2010-11-13 18:51:36 +03:00
}
QAction * act = menu . exec ( QCursor : : pos ( ) ) ;
2012-02-20 21:30:53 +04:00
if ( ! act ) return ;
if ( act = = addAct ) {
2010-11-13 18:51:36 +03:00
on_addRuleBtn_clicked ( ) ;
return ;
}
2012-02-20 21:30:53 +04:00
if ( act = = delAct ) {
2010-11-13 18:51:36 +03:00
on_removeRuleBtn_clicked ( ) ;
return ;
}
2012-02-20 21:30:53 +04:00
if ( act = = renameAct ) {
2010-11-13 18:51:36 +03:00
renameSelectedRule ( ) ;
return ;
}
}
void AutomatedRssDownloader : : renameSelectedRule ( )
{
2013-07-22 22:38:57 +04:00
const QList < QListWidgetItem * > selection = ui - > listRules - > selectedItems ( ) ;
if ( selection . isEmpty ( ) )
return ;
QListWidgetItem * item = selection . first ( ) ;
2010-11-13 18:51:36 +03:00
forever {
2013-07-22 15:46:10 +04:00
QString new_name = AutoExpandableDialog : : getText ( this , tr ( " Rule renaming " ) , tr ( " Please type the new rule name " ) , QLineEdit : : Normal , item - > text ( ) ) ;
2010-11-13 18:51:36 +03:00
new_name = new_name . trimmed ( ) ;
2012-02-20 21:30:53 +04:00
if ( new_name . isEmpty ( ) ) return ;
2013-07-01 12:43:40 +04:00
if ( m_editableRuleList - > ruleNames ( ) . contains ( new_name , Qt : : CaseInsensitive ) ) {
2010-11-13 18:51:36 +03:00
QMessageBox : : warning ( this , tr ( " Rule name conflict " ) , tr ( " A rule with this name already exists, please choose another name. " ) ) ;
} else {
// Rename the rule
2013-07-01 12:43:40 +04:00
m_editableRuleList - > renameRule ( item - > text ( ) , new_name ) ;
2010-11-13 18:51:36 +03:00
item - > setText ( new_name ) ;
return ;
}
}
}
2010-11-13 20:12:13 +03:00
void AutomatedRssDownloader : : handleFeedCheckStateChange ( QListWidgetItem * feed_item )
{
2012-02-20 21:30:53 +04:00
if ( ui - > ruleDefBox - > isEnabled ( ) ) {
2010-11-13 20:12:13 +03:00
// Make sure the current rule is saved
2010-11-13 23:08:44 +03:00
saveEditedRule ( ) ;
2010-11-13 20:12:13 +03:00
}
2010-11-13 21:08:09 +03:00
const QString feed_url = feed_item - > data ( Qt : : UserRole ) . toString ( ) ;
2010-11-13 20:12:13 +03:00
foreach ( QListWidgetItem * rule_item , ui - > listRules - > selectedItems ( ) ) {
2013-07-01 12:43:40 +04:00
RssDownloadRulePtr rule = m_editableRuleList - > getRule ( rule_item - > text ( ) ) ;
2012-02-19 20:53:10 +04:00
Q_ASSERT ( rule ) ;
QStringList affected_feeds = rule - > rssFeeds ( ) ;
2012-02-20 21:30:53 +04:00
if ( feed_item - > checkState ( ) = = Qt : : Checked ) {
if ( ! affected_feeds . contains ( feed_url ) )
2010-11-13 21:08:09 +03:00
affected_feeds < < feed_url ;
} else {
2012-02-20 21:30:53 +04:00
if ( affected_feeds . contains ( feed_url ) )
2010-11-13 21:08:09 +03:00
affected_feeds . removeOne ( feed_url ) ;
}
// Save the updated rule
2012-02-20 21:30:53 +04:00
if ( affected_feeds . size ( ) ! = rule - > rssFeeds ( ) . size ( ) ) {
2012-02-19 20:53:10 +04:00
rule - > setRssFeeds ( affected_feeds ) ;
2013-07-01 12:43:40 +04:00
m_editableRuleList - > saveRule ( rule ) ;
2010-11-13 21:08:09 +03:00
}
2010-11-13 20:12:13 +03:00
}
2010-11-13 22:36:46 +03:00
// Update Matching articles
updateMatchingArticles ( ) ;
}
void AutomatedRssDownloader : : updateMatchingArticles ( )
{
ui - > treeMatchingArticles - > clear ( ) ;
2012-02-19 20:53:10 +04:00
RssManagerPtr manager = m_manager . toStrongRef ( ) ;
if ( ! manager )
return ;
const QHash < QString , RssFeedPtr > all_feeds = manager - > getAllFeedsAsHash ( ) ;
2010-11-13 22:36:46 +03:00
2015-04-04 20:12:34 +03:00
saveEditedRule ( ) ;
2012-02-20 21:30:53 +04:00
foreach ( const QListWidgetItem * rule_item , ui - > listRules - > selectedItems ( ) ) {
2013-07-01 12:43:40 +04:00
RssDownloadRulePtr rule = m_editableRuleList - > getRule ( rule_item - > text ( ) ) ;
2012-02-20 21:30:53 +04:00
if ( ! rule ) continue ;
foreach ( const QString & feed_url , rule - > rssFeeds ( ) ) {
2010-12-18 21:42:31 +03:00
qDebug ( ) < < Q_FUNC_INFO < < feed_url ;
2012-02-20 21:30:53 +04:00
if ( ! all_feeds . contains ( feed_url ) ) continue ; // Feed was removed
2012-02-19 20:53:10 +04:00
RssFeedPtr feed = all_feeds . value ( feed_url ) ;
2010-12-18 21:42:31 +03:00
Q_ASSERT ( feed ) ;
2012-02-20 21:30:53 +04:00
if ( ! feed ) continue ;
2012-02-19 20:53:10 +04:00
const QStringList matching_articles = rule - > findMatchingArticles ( feed ) ;
2012-02-20 21:30:53 +04:00
if ( ! matching_articles . isEmpty ( ) )
2010-11-13 22:36:46 +03:00
addFeedArticlesToTree ( feed , matching_articles ) ;
}
}
}
2012-02-19 20:53:10 +04:00
void AutomatedRssDownloader : : addFeedArticlesToTree ( const RssFeedPtr & feed , const QStringList & articles )
2010-11-13 22:36:46 +03:00
{
// Check if this feed is already in the tree
QTreeWidgetItem * treeFeedItem = 0 ;
2012-02-20 21:30:53 +04:00
for ( int i = 0 ; i < ui - > treeMatchingArticles - > topLevelItemCount ( ) ; + + i ) {
2010-11-13 22:36:46 +03:00
QTreeWidgetItem * item = ui - > treeMatchingArticles - > topLevelItem ( i ) ;
2012-02-20 21:30:53 +04:00
if ( item - > data ( 0 , Qt : : UserRole ) . toString ( ) = = feed - > url ( ) ) {
2010-11-13 22:36:46 +03:00
treeFeedItem = item ;
break ;
}
}
// If there is none, create it
2012-02-20 21:30:53 +04:00
if ( ! treeFeedItem ) {
2011-01-27 20:18:56 +03:00
treeFeedItem = new QTreeWidgetItem ( QStringList ( ) < < feed - > displayName ( ) ) ;
treeFeedItem - > setToolTip ( 0 , feed - > displayName ( ) ) ;
2010-11-13 23:21:49 +03:00
QFont f = treeFeedItem - > font ( 0 ) ;
f . setBold ( true ) ;
treeFeedItem - > setFont ( 0 , f ) ;
2015-04-19 18:17:47 +03:00
treeFeedItem - > setData ( 0 , Qt : : DecorationRole , GuiIconProvider : : instance ( ) - > getIcon ( " inode-directory " ) ) ;
2011-01-27 20:18:56 +03:00
treeFeedItem - > setData ( 0 , Qt : : UserRole , feed - > url ( ) ) ;
2010-11-13 22:36:46 +03:00
ui - > treeMatchingArticles - > addTopLevelItem ( treeFeedItem ) ;
}
// Insert the articles
2012-02-20 21:30:53 +04:00
foreach ( const QString & art , articles ) {
2010-11-13 22:36:46 +03:00
QTreeWidgetItem * item = new QTreeWidgetItem ( QStringList ( ) < < art ) ;
2010-12-04 12:27:28 +03:00
item - > setToolTip ( 0 , art ) ;
2010-11-13 22:36:46 +03:00
treeFeedItem - > addChild ( item ) ;
}
ui - > treeMatchingArticles - > expandItem ( treeFeedItem ) ;
2010-11-13 20:12:13 +03:00
}
2011-04-18 14:49:06 +04:00
void AutomatedRssDownloader : : updateFieldsToolTips ( bool regex )
{
QString tip ;
2012-02-20 21:30:53 +04:00
if ( regex ) {
2011-04-18 14:49:06 +04:00
tip = tr ( " Regex mode: use Perl-like regular expressions " ) ;
2011-04-18 19:56:22 +04:00
ui - > lineContains - > setToolTip ( tip ) ;
ui - > lineNotContains - > setToolTip ( tip ) ;
} else {
tip = tr ( " Wildcard mode: you can use<ul><li>? to match any single character</li><li>* to match zero or more of any characters</li><li>Whitespaces count as AND operators</li></ul> " ) ;
ui - > lineContains - > setToolTip ( tip ) ;
tip = tr ( " Wildcard mode: you can use<ul><li>? to match any single character</li><li>* to match zero or more of any characters</li><li>| is used as OR operator</li></ul> " ) ;
ui - > lineNotContains - > setToolTip ( tip ) ;
}
}
void AutomatedRssDownloader : : updateMustLineValidity ( )
{
const QString text = ui - > lineContains - > text ( ) ;
bool valid = true ;
QStringList tokens ;
2012-02-20 21:30:53 +04:00
if ( ui - > checkRegex - > isChecked ( ) )
2011-04-18 19:56:22 +04:00
tokens < < text ;
else
tokens < < text . split ( " " ) ;
2012-02-20 21:30:53 +04:00
foreach ( const QString & token , tokens ) {
2011-04-18 19:56:22 +04:00
QRegExp reg ( token , Qt : : CaseInsensitive , ui - > checkRegex - > isChecked ( ) ? QRegExp : : RegExp : QRegExp : : Wildcard ) ;
2012-02-20 21:30:53 +04:00
if ( ! reg . isValid ( ) ) {
2011-04-18 19:56:22 +04:00
valid = false ;
break ;
}
}
2012-02-20 21:30:53 +04:00
if ( valid ) {
2011-04-18 19:56:22 +04:00
ui - > lineContains - > setStyleSheet ( " " ) ;
ui - > lbl_must_stat - > setPixmap ( QPixmap ( ) ) ;
} else {
ui - > lineContains - > setStyleSheet ( " QLineEdit { color: #ff0000; } " ) ;
2015-04-19 18:17:47 +03:00
ui - > lbl_must_stat - > setPixmap ( GuiIconProvider : : instance ( ) - > getIcon ( " task-attention " ) . pixmap ( 16 , 16 ) ) ;
2011-04-18 19:56:22 +04:00
}
}
void AutomatedRssDownloader : : updateMustNotLineValidity ( )
{
const QString text = ui - > lineNotContains - > text ( ) ;
bool valid = true ;
QStringList tokens ;
2012-02-20 21:30:53 +04:00
if ( ui - > checkRegex - > isChecked ( ) )
2011-04-18 19:56:22 +04:00
tokens < < text ;
else
2014-11-25 21:01:20 +03:00
tokens < < text . split ( " | " ) ;
2012-02-20 21:30:53 +04:00
foreach ( const QString & token , tokens ) {
2011-04-18 19:56:22 +04:00
QRegExp reg ( token , Qt : : CaseInsensitive , ui - > checkRegex - > isChecked ( ) ? QRegExp : : RegExp : QRegExp : : Wildcard ) ;
2012-02-20 21:30:53 +04:00
if ( ! reg . isValid ( ) ) {
2011-04-18 19:56:22 +04:00
valid = false ;
break ;
}
}
2012-02-20 21:30:53 +04:00
if ( valid ) {
2011-04-18 19:56:22 +04:00
ui - > lineNotContains - > setStyleSheet ( " " ) ;
ui - > lbl_mustnot_stat - > setPixmap ( QPixmap ( ) ) ;
2011-04-18 14:49:06 +04:00
} else {
2011-04-18 19:56:22 +04:00
ui - > lineNotContains - > setStyleSheet ( " QLineEdit { color: #ff0000; } " ) ;
2015-04-19 18:17:47 +03:00
ui - > lbl_mustnot_stat - > setPixmap ( GuiIconProvider : : instance ( ) - > getIcon ( " task-attention " ) . pixmap ( 16 , 16 ) ) ;
2011-04-18 14:49:06 +04:00
}
}
2015-02-11 03:39:27 +03:00
void AutomatedRssDownloader : : onFinished ( int result ) {
2013-07-01 12:52:23 +04:00
Q_UNUSED ( result ) ;
// Save current item on exit
saveEditedRule ( ) ;
m_ruleList - > replace ( m_editableRuleList ) ;
m_ruleList - > saveRulesToStorage ( ) ;
saveSettings ( ) ;
}