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 <QInputDialog>
# 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-06-22 17:39:49 +04:00
# include "rsssettings.h"
2010-11-13 13:49:22 +03:00
# include "rssdownloadrulelist.h"
# include "preferences.h"
# include "qinisettings.h"
2010-11-13 22:36:46 +03:00
# include "rssmanager.h"
# include "rssfeed.h"
2010-06-22 17:39:49 +04:00
2010-10-31 15:35:07 +03:00
AutomatedRssDownloader : : AutomatedRssDownloader ( QWidget * parent ) :
2010-11-13 13:49:22 +03:00
QDialog ( parent ) ,
2010-11-13 23:08:44 +03:00
ui ( new Ui : : AutomatedRssDownloader ) ,
m_editedRule ( 0 )
2010-06-22 17:39:49 +04:00
{
2010-11-13 13:49:22 +03:00
ui - > setupUi ( this ) ;
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-11-13 18:51:36 +03:00
connect ( ui - > listRules , SIGNAL ( customContextMenuRequested ( const QPoint & ) ) , this , SLOT ( displayRulesListMenu ( const QPoint & ) ) ) ;
2010-11-13 13:49:22 +03:00
m_ruleList = RssDownloadRuleList : : instance ( ) ;
initLabelCombobox ( ) ;
loadFeedList ( ) ;
loadSettings ( ) ;
2010-11-13 20:12:13 +03:00
connect ( ui - > listRules , SIGNAL ( itemSelectionChanged ( ) ) , SLOT ( updateRuleDefinitionBox ( ) ) ) ;
connect ( ui - > listRules , SIGNAL ( itemSelectionChanged ( ) ) , SLOT ( updateFeedList ( ) ) ) ;
connect ( ui - > listFeeds , SIGNAL ( itemChanged ( QListWidgetItem * ) ) , SLOT ( handleFeedCheckStateChange ( QListWidgetItem * ) ) ) ;
2010-11-13 22:36:46 +03:00
// Update matching articles when necessary
connect ( ui - > lineContains , SIGNAL ( textEdited ( QString ) ) , SLOT ( updateMatchingArticles ( ) ) ) ;
connect ( ui - > lineNotContains , SIGNAL ( textEdited ( QString ) ) , SLOT ( updateMatchingArticles ( ) ) ) ;
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 ;
// Save current item on exit
2010-11-13 23:08:44 +03:00
saveEditedRule ( ) ;
2010-10-31 15:35:07 +03:00
saveSettings ( ) ;
delete ui ;
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
QIniSettings settings ( " qBittorrent " , " qBittorrent " ) ;
restoreGeometry ( settings . value ( " RssFeedDownloader/geometry " ) . toByteArray ( ) ) ;
2010-10-31 15:35:07 +03:00
ui - > checkEnableDownloader - > setChecked ( RssSettings : : isRssDownloadingEnabled ( ) ) ;
2010-11-13 13:49:22 +03:00
// Display download rules
loadRulesList ( ) ;
2010-10-31 15:35:07 +03:00
}
void AutomatedRssDownloader : : saveSettings ( )
{
RssSettings : : setRssDownloadingEnabled ( ui - > checkEnableDownloader - > isChecked ( ) ) ;
2010-11-13 14:04:40 +03:00
// Save dialog geometry
QIniSettings settings ( " qBittorrent " , " qBittorrent " ) ;
settings . setValue ( " RssFeedDownloader/geometry " , saveGeometry ( ) ) ;
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
2010-11-13 23:08:44 +03:00
if ( m_editedRule ) {
saveEditedRule ( ) ;
2010-11-13 18:14:50 +03:00
}
ui - > listRules - > clear ( ) ;
2010-11-13 13:49:22 +03:00
foreach ( const QString & rule_name , m_ruleList - > ruleNames ( ) ) {
QListWidgetItem * item = new QListWidgetItem ( rule_name , ui - > listRules ) ;
item - > setFlags ( item - > flags ( ) | Qt : : ItemIsUserCheckable ) ;
if ( m_ruleList - > getRule ( rule_name ) . isEnabled ( ) )
item - > setCheckState ( Qt : : Checked ) ;
else
item - > setCheckState ( Qt : : Unchecked ) ;
}
2010-11-13 20:12:13 +03:00
if ( ui - > listRules - > count ( ) > 0 & & ! ui - > listRules - > currentItem ( ) )
ui - > listRules - > setCurrentRow ( 0 ) ;
2010-11-13 13:49:22 +03:00
}
void AutomatedRssDownloader : : loadFeedList ( )
{
const QStringList feed_aliases = RssSettings : : getRssFeedsAliases ( ) ;
const QStringList feed_urls = RssSettings : : getRssFeedsUrls ( ) ;
for ( int i = 0 ; i < feed_aliases . size ( ) ; + + i ) {
QListWidgetItem * item = new QListWidgetItem ( feed_aliases . at ( i ) , ui - > listFeeds ) ;
item - > setData ( Qt : : UserRole , feed_urls . at ( i ) ) ;
item - > setFlags ( item - > flags ( ) | Qt : : ItemIsUserCheckable ) ;
}
}
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 * ) ) ) ;
2010-11-13 13:49:22 +03:00
for ( int i = 0 ; i < ui - > listFeeds - > count ( ) ; + + i ) {
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 ;
2010-11-13 21:08:09 +03:00
foreach ( const QListWidgetItem * ruleItem , ui - > listRules - > selectedItems ( ) ) {
RssDownloadRule rule = m_ruleList - > getRule ( ruleItem - > text ( ) ) ;
if ( ! rule . isValid ( ) ) continue ;
qDebug ( ) < < " Rule " < < rule . name ( ) < < " affects " < < rule . rssFeeds ( ) . size ( ) < < " feeds. " ;
foreach ( QString test , rule . rssFeeds ( ) ) {
qDebug ( ) < < " Feed is " < < test ;
}
if ( rule . rssFeeds ( ) . contains ( feed_url ) ) {
qDebug ( ) < < " Rule " < < rule . name ( ) < < " affects feed " < < feed_url ;
all_enabled = true ;
} else {
qDebug ( ) < < " Rule " < < rule . name ( ) < < " does NOT affect feed " < < feed_url ;
all_enabled = false ;
break ;
}
2010-11-13 20:12:13 +03: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 ( ) ;
2010-11-13 20:12:13 +03:00
if ( selection . count ( ) = = 1 ) {
2010-11-13 23:08:44 +03:00
m_editedRule = selection . first ( ) ;
2010-11-13 20:12:13 +03:00
RssDownloadRule rule = getCurrentRule ( ) ;
if ( rule . isValid ( ) ) {
ui - > lineContains - > setText ( rule . mustContain ( ) ) ;
ui - > lineNotContains - > setText ( rule . mustNotContain ( ) ) ;
ui - > saveDiffDir_check - > setChecked ( ! rule . savePath ( ) . isEmpty ( ) ) ;
ui - > lineSavePath - > setText ( rule . savePath ( ) ) ;
if ( rule . label ( ) . isEmpty ( ) ) {
ui - > comboLabel - > setCurrentIndex ( - 1 ) ;
ui - > comboLabel - > clearEditText ( ) ;
} else {
ui - > comboLabel - > setCurrentIndex ( ui - > comboLabel - > findText ( rule . label ( ) ) ) ;
}
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 ( ) ) ;
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 ( ) ;
}
2010-11-13 13:49:22 +03:00
RssDownloadRule AutomatedRssDownloader : : getCurrentRule ( ) const
{
QListWidgetItem * current_item = ui - > listRules - > currentItem ( ) ;
if ( current_item )
return m_ruleList - > getRule ( current_item - > text ( ) ) ;
return RssDownloadRule ( ) ;
}
void AutomatedRssDownloader : : initLabelCombobox ( )
{
// Load custom labels
2010-11-16 23:34:31 +03:00
const QStringList customLabels = Preferences ( ) . getTorrentLabels ( ) ;
2010-11-13 13:49:22 +03:00
foreach ( const QString & label , customLabels ) {
ui - > comboLabel - > addItem ( label ) ;
}
}
2010-11-13 23:08:44 +03:00
void AutomatedRssDownloader : : saveEditedRule ( )
2010-11-13 20:12:13 +03:00
{
2010-11-13 23:08:44 +03:00
if ( ! m_editedRule ) return ;
qDebug ( ) < < Q_FUNC_INFO < < m_editedRule ;
if ( ui - > listRules - > findItems ( m_editedRule - > text ( ) , Qt : : MatchExactly ) . isEmpty ( ) ) {
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 ;
}
2010-11-13 23:08:44 +03:00
RssDownloadRule rule = m_ruleList - > getRule ( m_editedRule - > text ( ) ) ;
2010-11-13 13:49:22 +03:00
if ( ! rule . isValid ( ) ) {
2010-11-13 23:08:44 +03:00
rule . setName ( m_editedRule - > text ( ) ) ;
2010-11-13 13:49:22 +03:00
}
2010-11-13 23:08:44 +03:00
if ( m_editedRule - > checkState ( ) = = Qt : : Unchecked )
2010-11-13 13:49:22 +03:00
rule . setEnabled ( false ) ;
else
rule . setEnabled ( true ) ;
rule . setMustContain ( ui - > lineContains - > text ( ) ) ;
rule . setMustNotContain ( ui - > lineNotContains - > text ( ) ) ;
if ( ui - > saveDiffDir_check - > isChecked ( ) )
rule . setSavePath ( ui - > lineSavePath - > text ( ) ) ;
else
rule . setSavePath ( " " ) ;
rule . setLabel ( ui - > comboLabel - > currentText ( ) ) ;
// Save new label
if ( ! rule . label ( ) . isEmpty ( ) )
2010-11-16 23:34:31 +03:00
Preferences ( ) . addTorrentLabel ( rule . label ( ) ) ;
2010-11-13 21:08:09 +03:00
//rule.setRssFeeds(getSelectedFeeds());
2010-11-13 13:49:22 +03:00
// Save it
m_ruleList - > saveRule ( rule ) ;
}
void AutomatedRssDownloader : : on_addRuleBtn_clicked ( )
{
// Ask for a rule name
const QString rule = QInputDialog : : getText ( this , tr ( " New rule name " ) , tr ( " Please type the name of the new download rule. " ) ) ;
if ( rule . isEmpty ( ) ) return ;
// Check if this rule name already exists
if ( m_ruleList - > getRule ( rule ) . isValid ( ) ) {
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
QListWidgetItem * item = new QListWidgetItem ( rule , ui - > listRules ) ;
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 ( ) ;
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 ;
if ( selection . count ( ) = = 1 )
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? " ) ;
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 ;
2010-11-13 19:00:14 +03:00
foreach ( QListWidgetItem * item , selection ) {
// 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 " ) ;
// Remove it from the m_ruleList
m_ruleList - > 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 ( ) ) ;
if ( ! save_path . isEmpty ( ) )
ui - > lineSavePath - > setText ( save_path ) ;
2010-06-22 17:39:49 +04:00
}
2010-11-13 15:08:29 +03:00
void AutomatedRssDownloader : : on_exportBtn_clicked ( )
{
if ( m_ruleList - > isEmpty ( ) ) {
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) " ) ) ;
2010-11-13 15:08:29 +03:00
if ( save_path . isEmpty ( ) ) return ;
if ( ! save_path . endsWith ( " .rssrules " , Qt : : CaseInsensitive ) )
save_path + = " .rssrules " ;
if ( ! m_ruleList - > serialize ( save_path ) ) {
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
QString load_path = QFileDialog : : getOpenFileName ( this , tr ( " Please point to the RSS download rules file " ) , QDir : : homePath ( ) , tr ( " Rules list (*.rssrules *.filters) " ) ) ;
if ( load_path . isEmpty ( ) | | ! QFile : : exists ( load_path ) ) return ;
// Load it
2010-11-13 18:14:50 +03:00
if ( ! m_ruleList - > 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 ;
QAction * addAct = menu . addAction ( QIcon ( " :/Icons/oxygen/list-add.png " ) , tr ( " Add new rule... " ) ) ;
QAction * delAct = 0 ;
QAction * renameAct = 0 ;
2010-11-13 19:08:08 +03:00
const QList < QListWidgetItem * > selection = ui - > listRules - > selectedItems ( ) ;
if ( ! selection . isEmpty ( ) ) {
if ( selection . count ( ) = = 1 ) {
2010-11-13 19:00:14 +03:00
delAct = menu . addAction ( QIcon ( " :/Icons/oxygen/list-remove.png " ) , tr ( " Delete rule " ) ) ;
menu . addSeparator ( ) ;
renameAct = menu . addAction ( QIcon ( " :/Icons/oxygen/edit_clear.png " ) , tr ( " Rename rule... " ) ) ;
} else {
delAct = menu . addAction ( QIcon ( " :/Icons/oxygen/list-remove.png " ) , tr ( " Delete selected rules " ) ) ;
}
2010-11-13 18:51:36 +03:00
}
QAction * act = menu . exec ( QCursor : : pos ( ) ) ;
if ( ! act ) return ;
if ( act = = addAct ) {
on_addRuleBtn_clicked ( ) ;
return ;
}
if ( act = = delAct ) {
on_removeRuleBtn_clicked ( ) ;
return ;
}
if ( act = = renameAct ) {
renameSelectedRule ( ) ;
return ;
}
}
void AutomatedRssDownloader : : renameSelectedRule ( )
{
QListWidgetItem * item = ui - > listRules - > currentItem ( ) ;
if ( ! item ) return ;
forever {
QString new_name = QInputDialog : : getText ( this , tr ( " Rule renaming " ) , tr ( " Please type the new rule name " ) , QLineEdit : : Normal , item - > text ( ) ) ;
new_name = new_name . trimmed ( ) ;
if ( new_name . isEmpty ( ) ) return ;
if ( m_ruleList - > ruleNames ( ) . contains ( new_name , Qt : : CaseInsensitive ) ) {
QMessageBox : : warning ( this , tr ( " Rule name conflict " ) , tr ( " A rule with this name already exists, please choose another name. " ) ) ;
} else {
// Rename the rule
m_ruleList - > renameRule ( item - > text ( ) , new_name ) ;
item - > setText ( new_name ) ;
return ;
}
}
}
2010-11-13 20:12:13 +03:00
void AutomatedRssDownloader : : handleFeedCheckStateChange ( QListWidgetItem * feed_item )
{
if ( ui - > ruleDefBox - > isEnabled ( ) ) {
// 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 ( ) ) {
RssDownloadRule rule = m_ruleList - > getRule ( rule_item - > text ( ) ) ;
2010-11-13 21:08:09 +03:00
Q_ASSERT ( rule . isValid ( ) ) ;
QStringList affected_feeds = rule . rssFeeds ( ) ;
if ( feed_item - > checkState ( ) = = Qt : : Checked ) {
if ( ! affected_feeds . contains ( feed_url ) )
affected_feeds < < feed_url ;
} else {
if ( affected_feeds . contains ( feed_url ) )
affected_feeds . removeOne ( feed_url ) ;
}
// Save the updated rule
if ( affected_feeds . size ( ) ! = rule . rssFeeds ( ) . size ( ) ) {
rule . setRssFeeds ( affected_feeds ) ;
m_ruleList - > saveRule ( rule ) ;
}
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 ( ) ;
if ( ui - > ruleDefBox - > isEnabled ( ) ) {
2010-11-13 23:08:44 +03:00
saveEditedRule ( ) ;
2010-11-13 22:36:46 +03:00
}
const QHash < QString , RssFeed * > all_feeds = RssManager : : instance ( ) - > getAllFeedsAsHash ( ) ;
foreach ( const QListWidgetItem * rule_item , ui - > listRules - > selectedItems ( ) ) {
RssDownloadRule rule = m_ruleList - > getRule ( rule_item - > text ( ) ) ;
if ( ! rule . isValid ( ) ) continue ;
foreach ( const QString & feed_url , rule . rssFeeds ( ) ) {
Q_ASSERT ( all_feeds . contains ( feed_url ) ) ;
const RssFeed * feed = all_feeds . value ( feed_url ) ;
const QStringList matching_articles = rule . findMatchingArticles ( feed ) ;
if ( ! matching_articles . isEmpty ( ) )
addFeedArticlesToTree ( feed , matching_articles ) ;
}
}
}
void AutomatedRssDownloader : : addFeedArticlesToTree ( const RssFeed * feed , const QStringList & articles )
{
// Check if this feed is already in the tree
QTreeWidgetItem * treeFeedItem = 0 ;
for ( int i = 0 ; i < ui - > treeMatchingArticles - > topLevelItemCount ( ) ; + + i ) {
QTreeWidgetItem * item = ui - > treeMatchingArticles - > topLevelItem ( i ) ;
if ( item - > data ( 0 , Qt : : UserRole ) . toString ( ) = = feed - > getUrl ( ) ) {
treeFeedItem = item ;
break ;
}
}
// If there is none, create it
if ( ! treeFeedItem ) {
treeFeedItem = new QTreeWidgetItem ( QStringList ( ) < < feed - > getName ( ) ) ;
2010-11-13 23:21:49 +03:00
QFont f = treeFeedItem - > font ( 0 ) ;
f . setBold ( true ) ;
treeFeedItem - > setFont ( 0 , f ) ;
treeFeedItem - > setData ( 0 , Qt : : DecorationRole , QIcon ( " :/Icons/oxygen/folder.png " ) ) ;
2010-11-13 22:36:46 +03:00
treeFeedItem - > setData ( 0 , Qt : : UserRole , feed - > getUrl ( ) ) ;
ui - > treeMatchingArticles - > addTopLevelItem ( treeFeedItem ) ;
}
// Insert the articles
foreach ( const QString & art , articles ) {
QTreeWidgetItem * item = new QTreeWidgetItem ( QStringList ( ) < < art ) ;
treeFeedItem - > addChild ( item ) ;
}
ui - > treeMatchingArticles - > expandItem ( treeFeedItem ) ;
2010-11-13 20:12:13 +03:00
}