@ -29,6 +29,7 @@
*/
*/
# include <QFile>
# include <QFile>
# include <QDataStream>
# include <QDebug>
# include <QDebug>
# include "rssdownloadrulelist.h"
# include "rssdownloadrulelist.h"
@ -73,7 +74,7 @@ void RssDownloadRuleList::loadRulesFromStorage()
{
{
QIniSettings qBTRSS ( " qBittorrent " , " qBittorrent-rss " ) ;
QIniSettings qBTRSS ( " qBittorrent " , " qBittorrent-rss " ) ;
if ( qBTRSS . contains ( " feed_filters " ) ) {
if ( qBTRSS . contains ( " feed_filters " ) ) {
importRule sInOldFormat ( qBTRSS . value ( " feed_filters " ) . toHash ( ) ) ;
importFeed sInOldFormat ( qBTRSS . value ( " feed_filters " ) . toHash ( ) ) ;
// Remove outdated rules
// Remove outdated rules
qBTRSS . remove ( " feed_filters " ) ;
qBTRSS . remove ( " feed_filters " ) ;
// Save to new format
// Save to new format
@ -84,12 +85,17 @@ void RssDownloadRuleList::loadRulesFromStorage()
loadRulesFromVariantHash ( qBTRSS . value ( " download_rules " ) . toHash ( ) ) ;
loadRulesFromVariantHash ( qBTRSS . value ( " download_rules " ) . toHash ( ) ) ;
}
}
void RssDownloadRuleList : : importRule sInOldFormat ( const QHash < QString , QVariant > & rules )
void RssDownloadRuleList : : importFeed sInOldFormat ( const QHash < QString , QVariant > & rules )
{
{
foreach ( const QString & feed_url , rules . keys ( ) ) {
foreach ( const QString & feed_url , rules . keys ( ) ) {
const QHash < QString , QVariant > feed_rules = rules . value ( feed_url ) . toHash ( ) ;
importFeedRulesInOldFormat ( feed_url , rules . value ( feed_url ) . toHash ( ) ) ;
foreach ( const QString & rule_name , feed_rules . keys ( ) ) {
}
RssDownloadRule rule = RssDownloadRule : : fromOldFormat ( feed_rules . value ( rule_name ) . toHash ( ) , feed_url , rule_name ) ;
}
void RssDownloadRuleList : : importFeedRulesInOldFormat ( const QString & feed_url , const QHash < QString , QVariant > & rules )
{
foreach ( const QString & rule_name , rules . keys ( ) ) {
RssDownloadRule rule = RssDownloadRule : : fromOldFormat ( rules . value ( rule_name ) . toHash ( ) , feed_url , rule_name ) ;
if ( ! rule . isValid ( ) ) continue ;
if ( ! rule . isValid ( ) ) continue ;
// Check for rule name clash
// Check for rule name clash
while ( m_rules . contains ( rule . name ( ) ) ) {
while ( m_rules . contains ( rule . name ( ) ) ) {
@ -99,7 +105,6 @@ void RssDownloadRuleList::importRulesInOldFormat(const QHash<QString, QVariant>
saveRule ( rule ) ;
saveRule ( rule ) ;
}
}
}
}
}
QVariantHash RssDownloadRuleList : : toVariantHash ( ) const
QVariantHash RssDownloadRuleList : : toVariantHash ( ) const
{
{
@ -192,23 +197,30 @@ bool RssDownloadRuleList::unserialize(const QString &path)
QFile f ( path ) ;
QFile f ( path ) ;
if ( f . open ( QIODevice : : ReadOnly ) ) {
if ( f . open ( QIODevice : : ReadOnly ) ) {
QDataStream in ( & f ) ;
QDataStream in ( & f ) ;
if ( path . endsWith ( " .filters " ) ) {
if ( path . endsWith ( " .filters " , Qt : : CaseInsensitive ) ) {
// Old format (< 2.5.0)
// Old format (< 2.5.0)
qDebug ( " Old serialization format detected, processing... " ) ;
in . setVersion ( QDataStream : : Qt_4_3 ) ;
in . setVersion ( QDataStream : : Qt_4_3 ) ;
QVariantHash tmp ;
QVariantHash tmp ;
in > > tmp ;
in > > tmp ;
f . close ( ) ;
f . close ( ) ;
if ( tmp . isEmpty ( ) ) return false ;
if ( tmp . isEmpty ( ) ) return false ;
importRulesInOldFormat ( tmp ) ;
qDebug ( " Processing was successful! " ) ;
// Unfortunately the feed_url is lost
importFeedRulesInOldFormat ( " " , tmp ) ;
} else {
} else {
qDebug ( " New serialization format detected, processing... " ) ;
in . setVersion ( QDataStream : : Qt_4_5 ) ;
in . setVersion ( QDataStream : : Qt_4_5 ) ;
QVariantHash tmp ;
QVariantHash tmp ;
in > > tmp ;
in > > tmp ;
f . close ( ) ;
f . close ( ) ;
if ( tmp . isEmpty ( ) ) return false ;
if ( tmp . isEmpty ( ) ) return false ;
qDebug ( " Processing was successful! " ) ;
loadRulesFromVariantHash ( tmp ) ;
loadRulesFromVariantHash ( tmp ) ;
}
}
return true ;
return true ;
}
}
qDebug ( " Error: could not open file at %s " , qPrintable ( path ) ) ;
return false ;
return false ;
}
}