2007-04-05 23:26:30 +04:00
/*
* Bittorrent Client using Qt4 and libtorrent .
2007-07-14 18:31:59 +04:00
* Copyright ( C ) 2006 Christophe Dumez , Arnaud Demaiziere
2007-04-05 23:26:30 +04:00
*
2007-07-14 18:31:59 +04:00
* 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 .
2007-04-05 23:26:30 +04:00
*
* 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
2007-07-14 18:31:59 +04:00
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
*
2009-04-05 21:00:55 +04:00
* 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 .
*
2007-07-14 18:31:59 +04:00
* Contact : chris @ qbittorrent . org arnaud @ qbittorrent . org
2007-04-05 23:26:30 +04:00
*/
# include <QDesktopServices>
# include <QMenu>
# include <QStandardItemModel>
2007-07-23 16:12:55 +04:00
# include <QMessageBox>
2007-09-29 12:00:14 +04:00
# include <QString>
2009-08-21 11:40:57 +04:00
# include <QClipboard>
2009-08-23 12:10:56 +04:00
# include <QDragMoveEvent>
2014-07-05 16:44:13 +04:00
# include <QDebug>
2007-04-05 23:26:30 +04:00
2009-08-21 11:40:57 +04:00
# include "rss_imp.h"
2010-10-10 15:03:09 +04:00
# include "feedlistwidget.h"
2015-04-19 18:17:47 +03:00
# include "core/bittorrent/session.h"
# include "core/net/downloadmanager.h"
2010-05-17 18:57:45 +04:00
# include "cookiesdlg.h"
2015-04-13 19:19:53 +03:00
# include "core/preferences.h"
2010-10-31 15:35:07 +03:00
# include "rsssettingsdlg.h"
2010-10-16 21:39:03 +04:00
# include "rssmanager.h"
# include "rssfolder.h"
# include "rssarticle.h"
2012-08-18 19:43:20 +04:00
# include "rssparser.h"
2010-10-16 21:39:03 +04:00
# include "rssfeed.h"
2010-11-13 13:49:22 +03:00
# include "automatedrssdownloader.h"
2015-04-19 18:17:47 +03:00
# include "guiiconprovider.h"
2013-07-22 15:46:10 +04:00
# include "autoexpandabledialog.h"
2015-04-19 18:17:47 +03:00
# include "addnewtorrentdialog.h"
2007-04-05 23:26:30 +04:00
2014-12-07 13:11:18 +03:00
namespace Article
{
enum ArticleRoles
{
TitleRole = Qt : : DisplayRole ,
IconRole = Qt : : DecorationRole ,
ColorRole = Qt : : ForegroundRole ,
IdRole = Qt : : UserRole + 1 ,
FeedUrlRole = Qt : : UserRole + 2
} ;
2011-01-29 16:03:12 +03:00
}
2009-09-01 21:24:47 +04:00
2009-08-21 11:40:57 +04:00
// display a right-click menu
2012-08-19 18:36:03 +04:00
void RSSImp : : displayRSSListMenu ( const QPoint & pos )
{
2014-12-07 13:11:18 +03:00
if ( ! m_feedList - > indexAt ( pos ) . isValid ( ) )
// No item under the mouse, clear selection
m_feedList - > clearSelection ( ) ;
QMenu myRSSListMenu ( this ) ;
QList < QTreeWidgetItem * > selectedItems = m_feedList - > selectedItems ( ) ;
if ( selectedItems . size ( ) > 0 ) {
myRSSListMenu . addAction ( actionUpdate ) ;
myRSSListMenu . addAction ( actionMark_items_read ) ;
2009-08-24 10:28:31 +04:00
myRSSListMenu . addSeparator ( ) ;
2014-12-07 13:11:18 +03:00
if ( selectedItems . size ( ) = = 1 ) {
if ( m_feedList - > getRSSItem ( selectedItems . first ( ) ) ! = m_rssManager ) {
myRSSListMenu . addAction ( actionRename ) ;
myRSSListMenu . addAction ( actionDelete ) ;
myRSSListMenu . addSeparator ( ) ;
if ( m_feedList - > isFolder ( selectedItems . first ( ) ) )
myRSSListMenu . addAction ( actionNew_folder ) ;
else
myRSSListMenu . addAction ( actionManage_cookies ) ;
}
}
else {
myRSSListMenu . addAction ( actionDelete ) ;
myRSSListMenu . addSeparator ( ) ;
}
myRSSListMenu . addAction ( actionNew_subscription ) ;
if ( m_feedList - > isFeed ( selectedItems . first ( ) ) ) {
myRSSListMenu . addSeparator ( ) ;
myRSSListMenu . addAction ( actionCopy_feed_URL ) ;
2010-05-17 18:57:45 +04:00
}
2007-04-05 23:26:30 +04:00
}
2013-07-22 22:50:56 +04:00
else {
2014-12-07 13:11:18 +03:00
myRSSListMenu . addAction ( actionNew_subscription ) ;
myRSSListMenu . addAction ( actionNew_folder ) ;
myRSSListMenu . addSeparator ( ) ;
myRSSListMenu . addAction ( actionUpdate_all_feeds ) ;
2009-08-21 13:06:57 +04:00
}
2014-12-07 13:11:18 +03:00
myRSSListMenu . exec ( QCursor : : pos ( ) ) ;
2009-08-21 11:40:57 +04:00
}
2007-04-05 23:26:30 +04:00
2012-08-19 18:36:03 +04:00
void RSSImp : : displayItemsListMenu ( const QPoint & )
{
2014-12-07 13:11:18 +03:00
QMenu myItemListMenu ( this ) ;
QList < QListWidgetItem * > selectedItems = listArticles - > selectedItems ( ) ;
2015-08-04 02:30:31 +03:00
if ( selectedItems . size ( ) < = 0 )
return ;
bool hasTorrent = false ;
bool hasLink = false ;
foreach ( const QListWidgetItem * item , selectedItems ) {
if ( ! item ) continue ;
RssFeedPtr feed = m_feedList - > getRSSItemFromUrl ( item - > data ( Article : : FeedUrlRole ) . toString ( ) ) ;
if ( ! feed ) continue ;
RssArticlePtr article = feed - > getItem ( item - > data ( Article : : IdRole ) . toString ( ) ) ;
if ( ! article ) continue ;
if ( ! article - > torrentUrl ( ) . isEmpty ( ) )
hasTorrent = true ;
if ( ! article - > link ( ) . isEmpty ( ) )
hasLink = true ;
if ( hasTorrent & & hasLink )
break ;
2009-08-29 07:47:14 +04:00
}
2015-08-04 02:30:31 +03:00
if ( hasTorrent )
myItemListMenu . addAction ( actionDownload_torrent ) ;
if ( hasLink )
myItemListMenu . addAction ( actionOpen_news_URL ) ;
if ( hasTorrent | | hasLink )
myItemListMenu . exec ( QCursor : : pos ( ) ) ;
2009-08-21 11:40:57 +04:00
}
2007-04-05 23:26:30 +04:00
2012-08-19 18:36:03 +04:00
void RSSImp : : on_actionManage_cookies_triggered ( )
{
2014-12-07 13:11:18 +03:00
Q_ASSERT ( ! m_feedList - > selectedItems ( ) . empty ( ) ) ;
// Get feed hostname
QString feed_url = m_feedList - > getItemID ( m_feedList - > selectedItems ( ) . first ( ) ) ;
QString feed_hostname = QUrl : : fromEncoded ( feed_url . toUtf8 ( ) ) . host ( ) ;
qDebug ( " RSS Feed hostname is: %s " , qPrintable ( feed_hostname ) ) ;
Q_ASSERT ( ! feed_hostname . isEmpty ( ) ) ;
bool ok = false ;
Preferences * const pref = Preferences : : instance ( ) ;
QList < QByteArray > raw_cookies = CookiesDlg : : askForCookies ( this , pref - > getHostNameCookies ( feed_hostname ) , & ok ) ;
if ( ok ) {
qDebug ( ) < < " Settings cookies for host name: " < < feed_hostname ;
pref - > setHostNameCookies ( feed_hostname , raw_cookies ) ;
2015-04-19 18:17:47 +03:00
Net : : DownloadManager : : instance ( ) - > setCookiesFromUrl ( pref - > getHostNameQNetworkCookies ( feed_hostname ) , feed_hostname ) ;
2014-12-07 13:11:18 +03:00
}
2010-05-17 18:57:45 +04:00
}
2012-08-19 18:36:03 +04:00
void RSSImp : : askNewFolder ( )
{
2014-12-07 13:11:18 +03:00
QTreeWidgetItem * parent_item = 0 ;
RssFolderPtr rss_parent ;
if ( m_feedList - > selectedItems ( ) . size ( ) > 0 ) {
parent_item = m_feedList - > selectedItems ( ) . at ( 0 ) ;
rss_parent = qSharedPointerDynamicCast < RssFolder > ( m_feedList - > getRSSItem ( parent_item ) ) ;
Q_ASSERT ( rss_parent ) ;
}
else {
rss_parent = m_rssManager ;
}
bool ok ;
QString new_name = AutoExpandableDialog : : getText ( this , tr ( " Please choose a folder name " ) , tr ( " Folder name: " ) , QLineEdit : : Normal , tr ( " New folder " ) , & ok ) ;
if ( ! ok )
return ;
RssFolderPtr newFolder = rss_parent - > addFolder ( new_name ) ;
QTreeWidgetItem * folderItem = createFolderListItem ( newFolder ) ;
if ( parent_item )
parent_item - > addChild ( folderItem ) ;
else
m_feedList - > addTopLevelItem ( folderItem ) ;
// Notify TreeWidget
m_feedList - > itemAdded ( folderItem , newFolder ) ;
// Expand parent folder to display new folder
if ( parent_item )
parent_item - > setExpanded ( true ) ;
m_rssManager - > saveStreamList ( ) ;
2009-08-22 19:26:39 +04:00
}
2009-08-21 11:40:57 +04:00
// add a stream by a button
2012-08-19 18:36:03 +04:00
void RSSImp : : on_newFeedButton_clicked ( )
{
2014-12-07 13:11:18 +03:00
// Determine parent folder for new feed
QTreeWidgetItem * parent_item = 0 ;
QList < QTreeWidgetItem * > selected_items = m_feedList - > selectedItems ( ) ;
if ( ! selected_items . empty ( ) ) {
parent_item = selected_items . first ( ) ;
// Consider the case where the user clicked on Unread item
if ( parent_item = = m_feedList - > stickyUnreadItem ( ) )
parent_item = 0 ;
else
if ( ! m_feedList - > isFolder ( parent_item ) )
parent_item = parent_item - > parent ( ) ;
2009-08-24 12:17:45 +04:00
}
2014-12-07 13:11:18 +03:00
RssFolderPtr rss_parent ;
if ( parent_item )
rss_parent = qSharedPointerCast < RssFolder > ( m_feedList - > getRSSItem ( parent_item ) ) ;
else
rss_parent = m_rssManager ;
// Ask for feed URL
bool ok ;
QString clip_txt = qApp - > clipboard ( ) - > text ( ) ;
QString default_url = " http:// " ;
if ( clip_txt . startsWith ( " http:// " , Qt : : CaseInsensitive ) | | clip_txt . startsWith ( " https:// " , Qt : : CaseInsensitive ) | | clip_txt . startsWith ( " ftp:// " , Qt : : CaseInsensitive ) )
default_url = clip_txt ;
2015-03-30 14:15:48 +03:00
QString newUrl = AutoExpandableDialog : : getText ( this , tr ( " Please type a RSS stream URL " ) , tr ( " Stream URL: " ) , QLineEdit : : Normal , default_url , & ok ) ;
2014-12-07 13:11:18 +03:00
if ( ! ok )
return ;
newUrl = newUrl . trimmed ( ) ;
if ( newUrl . isEmpty ( ) )
return ;
if ( m_feedList - > hasFeed ( newUrl ) ) {
QMessageBox : : warning ( this , " qBittorrent " ,
2015-03-30 14:15:48 +03:00
tr ( " This RSS feed is already in the list. " ) ,
2014-12-07 13:11:18 +03:00
QMessageBox : : Ok ) ;
return ;
}
RssFeedPtr stream = rss_parent - > addStream ( m_rssManager . data ( ) , newUrl ) ;
// Create TreeWidget item
QTreeWidgetItem * item = createFolderListItem ( stream ) ;
if ( parent_item )
parent_item - > addChild ( item ) ;
else
m_feedList - > addTopLevelItem ( item ) ;
// Notify TreeWidget
m_feedList - > itemAdded ( item , stream ) ;
stream - > refresh ( ) ;
m_rssManager - > saveStreamList ( ) ;
2009-08-21 11:40:57 +04:00
}
2007-04-15 23:06:19 +04:00
2009-08-21 11:40:57 +04:00
// delete a stream by a button
2012-08-19 18:36:03 +04:00
void RSSImp : : deleteSelectedItems ( )
{
2014-12-07 13:11:18 +03:00
QList < QTreeWidgetItem * > selectedItems = m_feedList - > selectedItems ( ) ;
if ( selectedItems . isEmpty ( ) )
return ;
2015-08-04 02:41:34 +03:00
if ( ( selectedItems . size ( ) = = 1 ) & & ( selectedItems . first ( ) = = m_feedList - > stickyUnreadItem ( ) ) )
return ;
2014-12-07 13:11:18 +03:00
2015-08-04 02:41:34 +03:00
QMessageBox : : StandardButton answer = QMessageBox : : question ( this , tr ( " Deletion confirmation " ) ,
tr ( " Are you sure you want to delete the selected RSS feeds? " ) ,
QMessageBox : : Yes | QMessageBox : : No , QMessageBox : : No ) ;
if ( answer = = QMessageBox : : No )
2014-12-07 13:11:18 +03:00
return ;
foreach ( QTreeWidgetItem * item , selectedItems ) {
if ( m_feedList - > currentFeed ( ) = = item ) {
textBrowser - > clear ( ) ;
m_currentArticle = 0 ;
listArticles - > clear ( ) ;
}
if ( item = = m_feedList - > stickyUnreadItem ( ) )
continue ;
RssFilePtr rss_item = m_feedList - > getRSSItem ( item ) ;
QTreeWidgetItem * parent = item - > parent ( ) ;
// Notify TreeWidget
m_feedList - > itemAboutToBeRemoved ( item ) ;
// Actually delete the item
rss_item - > parent ( ) - > removeChild ( rss_item - > id ( ) ) ;
delete item ;
// Update parents count
while ( parent & & parent ! = m_feedList - > invisibleRootItem ( ) ) {
updateItemInfos ( parent ) ;
parent = parent - > parent ( ) ;
}
// Clear feed data from RSS parser (possible caching).
RssFeed * rssFeed = dynamic_cast < RssFeed * > ( rss_item . data ( ) ) ;
if ( rssFeed )
m_rssManager - > rssParser ( ) - > clearFeedData ( rssFeed - > url ( ) ) ;
2007-04-05 23:26:30 +04:00
}
2014-12-07 13:11:18 +03:00
m_rssManager - > saveStreamList ( ) ;
// Update Unread items
updateItemInfos ( m_feedList - > stickyUnreadItem ( ) ) ;
2009-08-21 11:40:57 +04:00
}
2007-04-05 23:26:30 +04:00
2012-08-19 18:36:03 +04:00
void RSSImp : : loadFoldersOpenState ( )
{
2014-12-07 13:11:18 +03:00
QStringList open_folders = Preferences : : instance ( ) - > getRssOpenFolders ( ) ;
foreach ( const QString & var_path , open_folders ) {
QStringList path = var_path . split ( " \\ " ) ;
QTreeWidgetItem * parent = 0 ;
foreach ( const QString & name , path ) {
int nbChildren = 0 ;
if ( parent )
nbChildren = parent - > childCount ( ) ;
else
nbChildren = m_feedList - > topLevelItemCount ( ) ;
for ( int i = 0 ; i < nbChildren ; + + i ) {
QTreeWidgetItem * child ;
if ( parent )
child = parent - > child ( i ) ;
else
child = m_feedList - > topLevelItem ( i ) ;
if ( m_feedList - > getRSSItem ( child ) - > id ( ) = = name ) {
parent = child ;
parent - > setExpanded ( true ) ;
qDebug ( " expanding folder %s " , qPrintable ( name ) ) ;
break ;
}
}
}
2009-08-23 18:56:44 +04:00
}
}
2012-08-19 18:36:03 +04:00
void RSSImp : : saveFoldersOpenState ( )
{
2014-12-07 13:11:18 +03:00
QStringList open_folders ;
QList < QTreeWidgetItem * > items = m_feedList - > getAllOpenFolders ( ) ;
foreach ( QTreeWidgetItem * item , items ) {
QString path = m_feedList - > getItemPath ( item ) . join ( " \\ " ) ;
qDebug ( " saving open folder: %s " , qPrintable ( path ) ) ;
open_folders < < path ;
}
Preferences : : instance ( ) - > setRssOpenFolders ( open_folders ) ;
2009-08-23 18:56:44 +04:00
}
2009-08-21 11:40:57 +04:00
// refresh all streams by a button
2012-08-19 18:36:03 +04:00
void RSSImp : : refreshAllFeeds ( )
{
2014-12-07 13:11:18 +03:00
foreach ( QTreeWidgetItem * item , m_feedList - > getAllFeedItems ( ) )
2015-01-18 15:13:06 +03:00
item - > setData ( 0 , Qt : : DecorationRole , QVariant ( QIcon ( " :/icons/loading.png " ) ) ) ;
2014-12-07 13:11:18 +03:00
m_rssManager - > refresh ( ) ;
2009-08-21 11:40:57 +04:00
}
2007-09-09 15:31:51 +04:00
2012-08-19 18:36:03 +04:00
void RSSImp : : downloadSelectedTorrents ( )
{
2014-12-07 13:11:18 +03:00
QList < QListWidgetItem * > selected_items = listArticles - > selectedItems ( ) ;
foreach ( const QListWidgetItem * item , selected_items ) {
if ( ! item ) continue ;
RssFeedPtr feed = m_feedList - > getRSSItemFromUrl ( item - > data ( Article : : FeedUrlRole ) . toString ( ) ) ;
if ( ! feed ) continue ;
RssArticlePtr article = feed - > getItem ( item - > data ( Article : : IdRole ) . toString ( ) ) ;
if ( ! article ) continue ;
2015-08-04 03:43:45 +03:00
if ( article - > torrentUrl ( ) . isEmpty ( ) )
continue ;
2015-06-23 11:19:34 +03:00
if ( Preferences : : instance ( ) - > useAdditionDialog ( ) )
AddNewTorrentDialog : : show ( article - > torrentUrl ( ) ) ;
2015-04-19 18:17:47 +03:00
else
2015-06-23 11:19:34 +03:00
BitTorrent : : Session : : instance ( ) - > addTorrent ( article - > torrentUrl ( ) ) ;
2009-08-29 07:47:14 +04:00
}
2009-08-21 11:40:57 +04:00
}
2007-04-05 23:26:30 +04:00
2012-08-19 16:48:28 +04:00
// open the url of the selected RSS articles in the Web browser
2012-08-19 18:36:03 +04:00
void RSSImp : : openSelectedArticlesUrls ( )
{
2014-12-07 13:11:18 +03:00
QList < QListWidgetItem * > selected_items = listArticles - > selectedItems ( ) ;
foreach ( const QListWidgetItem * item , selected_items ) {
RssArticlePtr news = m_feedList - > getRSSItemFromUrl ( item - > data ( Article : : FeedUrlRole ) . toString ( ) )
- > getItem ( item - > data ( Article : : IdRole ) . toString ( ) ) ;
const QString link = news - > link ( ) ;
if ( ! link . isEmpty ( ) )
QDesktopServices : : openUrl ( QUrl ( link ) ) ;
}
2009-08-21 11:40:57 +04:00
}
2007-08-03 18:37:56 +04:00
2009-08-23 13:05:36 +04:00
//right-click on stream : give it an alias
2012-08-19 18:36:03 +04:00
void RSSImp : : renameSelectedRssFile ( )
{
2014-12-07 13:11:18 +03:00
QList < QTreeWidgetItem * > selectedItems = m_feedList - > selectedItems ( ) ;
if ( selectedItems . size ( ) ! = 1 )
return ;
QTreeWidgetItem * item = selectedItems . first ( ) ;
if ( item = = m_feedList - > stickyUnreadItem ( ) )
return ;
RssFilePtr rss_item = m_feedList - > getRSSItem ( item ) ;
bool ok ;
QString newName ;
do {
newName = AutoExpandableDialog : : getText ( this , tr ( " Please choose a new name for this RSS feed " ) , tr ( " New feed name: " ) , QLineEdit : : Normal , m_feedList - > getRSSItem ( item ) - > displayName ( ) , & ok ) ;
// Check if name is already taken
if ( ok ) {
if ( rss_item - > parent ( ) - > hasChild ( newName ) ) {
QMessageBox : : warning ( 0 , tr ( " Name already in use " ) , tr ( " This name is already used by another item, please choose another one. " ) ) ;
ok = false ;
}
}
else {
return ;
}
} while ( ! ok ) ;
// Rename item
rss_item - > rename ( newName ) ;
// Update TreeWidget
updateItemInfos ( item ) ;
2009-08-21 11:40:57 +04:00
}
2007-04-06 02:38:46 +04:00
2012-08-19 16:48:28 +04:00
// right-click on stream : refresh it
2012-08-19 18:36:03 +04:00
void RSSImp : : refreshSelectedItems ( )
{
2014-12-07 13:11:18 +03:00
QList < QTreeWidgetItem * > selectedItems = m_feedList - > selectedItems ( ) ;
foreach ( QTreeWidgetItem * item , selectedItems ) {
RssFilePtr file = m_feedList - > getRSSItem ( item ) ;
// Update icons
if ( item = = m_feedList - > stickyUnreadItem ( ) ) {
refreshAllFeeds ( ) ;
return ;
}
else {
if ( ! file - > refresh ( ) )
continue ;
// Update UI
if ( qSharedPointerDynamicCast < RssFeed > ( file ) ) {
2015-01-18 15:13:06 +03:00
item - > setData ( 0 , Qt : : DecorationRole , QVariant ( QIcon ( " :/icons/loading.png " ) ) ) ;
2014-12-07 13:11:18 +03:00
}
else if ( qSharedPointerDynamicCast < RssFolder > ( file ) ) {
// Update feeds in the folder
foreach ( QTreeWidgetItem * feed , m_feedList - > getAllFeedItems ( item ) )
2015-01-18 15:13:06 +03:00
feed - > setData ( 0 , Qt : : DecorationRole , QVariant ( QIcon ( " :/icons/loading.png " ) ) ) ;
2014-12-07 13:11:18 +03:00
}
}
2009-08-23 17:20:26 +04:00
}
2009-08-21 11:40:57 +04:00
}
2007-04-05 23:26:30 +04:00
2012-08-19 18:36:03 +04:00
void RSSImp : : copySelectedFeedsURL ( )
{
2014-12-07 13:11:18 +03:00
QStringList URLs ;
QList < QTreeWidgetItem * > selectedItems = m_feedList - > selectedItems ( ) ;
QTreeWidgetItem * item ;
foreach ( item , selectedItems )
if ( m_feedList - > isFeed ( item ) )
URLs < < m_feedList - > getItemID ( item ) ;
qApp - > clipboard ( ) - > setText ( URLs . join ( " \n " ) ) ;
2009-08-21 11:40:57 +04:00
}
2007-04-05 23:26:30 +04:00
2012-08-19 18:36:03 +04:00
void RSSImp : : on_markReadButton_clicked ( )
{
2014-12-07 13:11:18 +03:00
QList < QTreeWidgetItem * > selectedItems = m_feedList - > selectedItems ( ) ;
foreach ( QTreeWidgetItem * item , selectedItems ) {
RssFilePtr rss_item = m_feedList - > getRSSItem ( item ) ;
Q_ASSERT ( rss_item ) ;
rss_item - > markAsRead ( ) ;
updateItemInfos ( item ) ;
}
// Update article list
if ( ! selectedItems . isEmpty ( ) )
populateArticleList ( m_feedList - > currentItem ( ) ) ;
2009-08-21 11:40:57 +04:00
}
2007-08-03 18:37:56 +04:00
2012-08-19 16:48:28 +04:00
QTreeWidgetItem * RSSImp : : createFolderListItem ( const RssFilePtr & rssFile )
{
2014-12-07 13:11:18 +03:00
Q_ASSERT ( rssFile ) ;
QTreeWidgetItem * item = new QTreeWidgetItem ;
item - > setData ( 0 , Qt : : DisplayRole , QVariant ( rssFile - > displayName ( ) + QString : : fromUtf8 ( " ( " ) + QString : : number ( rssFile - > unreadCount ( ) ) + QString ( " ) " ) ) ) ;
item - > setData ( 0 , Qt : : DecorationRole , rssFile - > icon ( ) ) ;
2012-08-19 16:48:28 +04:00
2014-12-07 13:11:18 +03:00
return item ;
2012-08-19 16:48:28 +04:00
}
2012-08-19 18:36:03 +04:00
void RSSImp : : fillFeedsList ( QTreeWidgetItem * parent , const RssFolderPtr & rss_parent )
{
2014-12-07 13:11:18 +03:00
QList < RssFilePtr > children ;
2012-08-19 16:48:28 +04:00
if ( parent )
2014-12-07 13:11:18 +03:00
children = rss_parent - > getContent ( ) ;
2009-08-22 18:06:05 +04:00
else
2014-12-07 13:11:18 +03:00
children = m_rssManager - > getContent ( ) ;
foreach ( const RssFilePtr & rssFile , children ) {
QTreeWidgetItem * item = createFolderListItem ( rssFile ) ;
Q_ASSERT ( item ) ;
if ( parent )
parent - > addChild ( item ) ;
else
m_feedList - > addTopLevelItem ( item ) ;
2012-08-19 16:48:28 +04:00
2014-12-07 13:11:18 +03:00
// Notify TreeWidget of item addition
m_feedList - > itemAdded ( item , rssFile ) ;
2012-08-19 16:48:28 +04:00
2014-12-07 13:11:18 +03:00
// Recursive call if this is a folder.
if ( RssFolderPtr folder = qSharedPointerDynamicCast < RssFolder > ( rssFile ) )
fillFeedsList ( item , folder ) ;
}
2009-08-21 11:40:57 +04:00
}
2007-04-05 23:26:30 +04:00
2012-08-19 15:55:30 +04:00
QListWidgetItem * RSSImp : : createArticleListItem ( const RssArticlePtr & article )
{
2014-12-07 13:11:18 +03:00
Q_ASSERT ( article ) ;
QListWidgetItem * item = new QListWidgetItem ;
2012-08-19 15:55:30 +04:00
2014-12-07 13:11:18 +03:00
item - > setData ( Article : : TitleRole , article - > title ( ) ) ;
item - > setData ( Article : : FeedUrlRole , article - > parent ( ) - > url ( ) ) ;
item - > setData ( Article : : IdRole , article - > guid ( ) ) ;
if ( article - > isRead ( ) ) {
item - > setData ( Article : : ColorRole , QVariant ( QColor ( " grey " ) ) ) ;
2015-01-18 15:13:06 +03:00
item - > setData ( Article : : IconRole , QVariant ( QIcon ( " :/icons/sphere.png " ) ) ) ;
2014-12-07 13:11:18 +03:00
}
else {
item - > setData ( Article : : ColorRole , QVariant ( QColor ( " blue " ) ) ) ;
2015-01-18 15:13:06 +03:00
item - > setData ( Article : : IconRole , QVariant ( QIcon ( " :/icons/sphere2.png " ) ) ) ;
2014-12-07 13:11:18 +03:00
}
2012-08-19 15:55:30 +04:00
2014-12-07 13:11:18 +03:00
return item ;
2012-08-19 15:55:30 +04:00
}
2009-08-21 11:40:57 +04:00
// fills the newsList
2012-08-19 15:55:30 +04:00
void RSSImp : : populateArticleList ( QTreeWidgetItem * item )
{
2014-12-07 13:11:18 +03:00
if ( ! item ) {
listArticles - > clear ( ) ;
return ;
}
2009-08-23 16:22:19 +04:00
2014-12-07 13:11:18 +03:00
RssFilePtr rss_item = m_feedList - > getRSSItem ( item ) ;
if ( ! rss_item )
return ;
2009-08-22 19:26:39 +04:00
2014-12-07 13:11:18 +03:00
// Clear the list first
textBrowser - > clear ( ) ;
m_currentArticle = 0 ;
listArticles - > clear ( ) ;
2012-08-19 15:55:30 +04:00
2014-12-07 13:11:18 +03:00
qDebug ( " Getting the list of news " ) ;
RssArticleList articles ;
if ( rss_item = = m_rssManager )
articles = rss_item - > unreadArticleListByDateDesc ( ) ;
else
articles = rss_item - > articleListByDateDesc ( ) ;
2012-08-19 15:55:30 +04:00
2014-12-07 13:11:18 +03:00
qDebug ( " Got the list of news " ) ;
foreach ( const RssArticlePtr & article , articles ) {
QListWidgetItem * articleItem = createArticleListItem ( article ) ;
listArticles - > addItem ( articleItem ) ;
}
qDebug ( " Added all news to the GUI " ) ;
2009-08-21 11:40:57 +04:00
}
2007-08-03 18:37:56 +04:00
2009-08-21 11:40:57 +04:00
// display a news
2012-08-19 18:36:03 +04:00
void RSSImp : : refreshTextBrowser ( )
{
2014-12-07 13:11:18 +03:00
QList < QListWidgetItem * > selection = listArticles - > selectedItems ( ) ;
if ( selection . empty ( ) ) return ;
Q_ASSERT ( selection . size ( ) = = 1 ) ;
QListWidgetItem * item = selection . first ( ) ;
Q_ASSERT ( item ) ;
if ( item = = m_currentArticle ) return ;
2015-08-04 18:21:04 +03:00
m_currentArticle = item ;
2014-12-07 13:11:18 +03:00
RssFeedPtr stream = m_feedList - > getRSSItemFromUrl ( item - > data ( Article : : FeedUrlRole ) . toString ( ) ) ;
RssArticlePtr article = stream - > getItem ( item - > data ( Article : : IdRole ) . toString ( ) ) ;
QString html ;
html + = " <div style='border: 2px solid red; margin-left: 5px; margin-right: 5px; margin-bottom: 5px;'> " ;
html + = " <div style='background-color: #678db2; font-weight: bold; color: #fff;'> " + article - > title ( ) + " </div> " ;
if ( article - > date ( ) . isValid ( ) )
html + = " <div style='background-color: #efefef;'><b> " + tr ( " Date: " ) + " </b> " + article - > date ( ) . toLocalTime ( ) . toString ( Qt : : SystemLocaleLongDate ) + " </div> " ;
if ( ! article - > author ( ) . isEmpty ( ) )
html + = " <div style='background-color: #efefef;'><b> " + tr ( " Author: " ) + " </b> " + article - > author ( ) + " </div> " ;
html + = " </div> " ;
html + = " <div style='margin-left: 5px; margin-right: 5px;'> " ;
if ( Qt : : mightBeRichText ( article - > description ( ) ) ) {
html + = article - > description ( ) ;
}
else {
QString description = article - > description ( ) ;
QRegExp rx ;
// If description is plain text, replace BBCode tags with HTML and wrap everything in <pre></pre> so it looks nice
rx . setMinimal ( true ) ;
rx . setCaseSensitivity ( Qt : : CaseInsensitive ) ;
rx . setPattern ( " \\ [img \\ ](.+) \\ [/img \\ ] " ) ;
description = description . replace ( rx , " <img src= \" \\ 1 \" > " ) ;
rx . setPattern ( " \\ [url=( \" )?(.+) \\ 1 \\ ] " ) ;
description = description . replace ( rx , " <a href= \" \\ 2 \" > " ) ;
description = description . replace ( " [/url] " , " </a> " , Qt : : CaseInsensitive ) ;
rx . setPattern ( " \\ [(/)?([bius]) \\ ] " ) ;
description = description . replace ( rx , " < \\ 1 \\ 2> " ) ;
rx . setPattern ( " \\ [color=( \" )?(.+) \\ 1 \\ ] " ) ;
description = description . replace ( rx , " <span style= \" color: \\ 2 \" > " ) ;
description = description . replace ( " [/color] " , " </span> " , Qt : : CaseInsensitive ) ;
rx . setPattern ( " \\ [size=( \" )?(.+) \\ d \\ 1 \\ ] " ) ;
description = description . replace ( rx , " <span style= \" font-size: \\ 2px \" > " ) ;
description = description . replace ( " [/size] " , " </span> " , Qt : : CaseInsensitive ) ;
html + = " <pre> " + description + " </pre> " ;
2009-08-28 21:29:34 +04:00
}
2014-12-07 13:11:18 +03:00
html + = " </div> " ;
textBrowser - > setHtml ( html ) ;
article - > markAsRead ( ) ;
item - > setData ( Article : : ColorRole , QVariant ( QColor ( " grey " ) ) ) ;
2015-01-18 15:13:06 +03:00
item - > setData ( Article : : IconRole , QVariant ( QIcon ( " :/icons/sphere.png " ) ) ) ;
2014-12-07 13:11:18 +03:00
// Decrement feed nb unread news
updateItemInfos ( m_feedList - > stickyUnreadItem ( ) ) ;
updateItemInfos ( m_feedList - > getTreeItemFromUrl ( item - > data ( Article : : FeedUrlRole ) . toString ( ) ) ) ;
2009-08-21 11:40:57 +04:00
}
2007-08-03 18:37:56 +04:00
2012-08-19 18:36:03 +04:00
void RSSImp : : saveSlidersPosition ( )
{
2014-12-07 13:11:18 +03:00
// Remember sliders positions
Preferences * const pref = Preferences : : instance ( ) ;
pref - > setRssHSplitterState ( splitter_h - > saveState ( ) ) ;
pref - > setRssVSplitterState ( splitter_v - > saveState ( ) ) ;
qDebug ( " Splitters position saved " ) ;
2009-08-21 11:40:57 +04:00
}
2012-08-19 18:36:03 +04:00
void RSSImp : : restoreSlidersPosition ( )
{
2014-12-07 13:11:18 +03:00
const Preferences * const pref = Preferences : : instance ( ) ;
QByteArray pos_h = pref - > getRssHSplitterState ( ) ;
if ( ! pos_h . isNull ( ) )
splitter_h - > restoreState ( pos_h ) ;
QByteArray pos_v = pref - > getRssVSplitterState ( ) ;
if ( ! pos_v . isNull ( ) )
splitter_v - > restoreState ( pos_v ) ;
2009-08-21 11:40:57 +04:00
}
2012-08-19 18:36:03 +04:00
void RSSImp : : updateItemsInfos ( const QList < QTreeWidgetItem * > & items )
{
2014-12-07 13:11:18 +03:00
foreach ( QTreeWidgetItem * item , items )
updateItemInfos ( item ) ;
2009-08-23 16:49:37 +04:00
}
2012-08-19 18:36:03 +04:00
void RSSImp : : updateItemInfos ( QTreeWidgetItem * item )
{
2014-12-07 13:11:18 +03:00
RssFilePtr rss_item = m_feedList - > getRSSItem ( item ) ;
if ( ! rss_item )
return ;
2011-10-05 19:41:23 +04:00
2014-12-07 13:11:18 +03:00
QString name ;
2015-03-10 21:21:29 +03:00
if ( rss_item = = m_rssManager ) {
2014-12-07 13:11:18 +03:00
name = tr ( " Unread " ) ;
2015-03-10 21:21:29 +03:00
emit updateRSSCount ( rss_item - > unreadCount ( ) ) ;
}
2014-12-07 13:11:18 +03:00
else
name = rss_item - > displayName ( ) ;
item - > setText ( 0 , name + QString : : fromUtf8 ( " ( " ) + QString : : number ( rss_item - > unreadCount ( ) ) + QString ( " ) " ) ) ;
// If item has a parent, update it too
if ( item - > parent ( ) )
updateItemInfos ( item - > parent ( ) ) ;
2009-08-23 16:49:37 +04:00
}
2012-08-19 18:36:03 +04:00
void RSSImp : : updateFeedIcon ( const QString & url , const QString & iconPath )
{
2014-12-07 13:11:18 +03:00
QTreeWidgetItem * item = m_feedList - > getTreeItemFromUrl ( url ) ;
item - > setData ( 0 , Qt : : DecorationRole , QVariant ( QIcon ( iconPath ) ) ) ;
2009-08-21 11:40:57 +04:00
}
2012-08-18 23:25:21 +04:00
void RSSImp : : updateFeedInfos ( const QString & url , const QString & display_name , uint nbUnread )
{
2014-12-07 13:11:18 +03:00
qDebug ( ) < < Q_FUNC_INFO < < display_name ;
QTreeWidgetItem * item = m_feedList - > getTreeItemFromUrl ( url ) ;
RssFeedPtr stream = qSharedPointerCast < RssFeed > ( m_feedList - > getRSSItem ( item ) ) ;
item - > setText ( 0 , display_name + QString : : fromUtf8 ( " ( " ) + QString : : number ( nbUnread ) + QString ( " ) " ) ) ;
if ( ! stream - > isLoading ( ) )
item - > setData ( 0 , Qt : : DecorationRole , QVariant ( stream - > icon ( ) ) ) ;
// Update parent
if ( item - > parent ( ) )
updateItemInfos ( item - > parent ( ) ) ;
// Update Unread item
updateItemInfos ( m_feedList - > stickyUnreadItem ( ) ) ;
2012-08-18 23:25:21 +04:00
}
void RSSImp : : onFeedContentChanged ( const QString & url )
{
2014-12-07 13:11:18 +03:00
qDebug ( ) < < Q_FUNC_INFO < < url ;
QTreeWidgetItem * item = m_feedList - > getTreeItemFromUrl ( url ) ;
// If the feed is selected, update the displayed news
2015-08-04 18:21:04 +03:00
if ( m_feedList - > currentItem ( ) = = item )
2014-12-07 13:11:18 +03:00
populateArticleList ( item ) ;
2015-08-04 18:21:04 +03:00
// Update unread items
else if ( m_feedList - > currentItem ( ) = = m_feedList - > stickyUnreadItem ( ) )
populateArticleList ( m_feedList - > stickyUnreadItem ( ) ) ;
2009-08-21 11:40:57 +04:00
}
2012-08-19 18:36:03 +04:00
void RSSImp : : updateRefreshInterval ( uint val )
{
2014-12-07 13:11:18 +03:00
m_rssManager - > updateRefreshInterval ( val ) ;
}
RSSImp : : RSSImp ( QWidget * parent ) :
QWidget ( parent ) ,
m_rssManager ( new RssManager )
{
setupUi ( this ) ;
// Icons
2015-04-19 18:17:47 +03:00
actionCopy_feed_URL - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " edit-copy " ) ) ;
actionDelete - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " edit-delete " ) ) ;
actionDownload_torrent - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " download " ) ) ;
actionManage_cookies - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " preferences-web-browser-cookies " ) ) ;
actionMark_items_read - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " mail-mark-read " ) ) ;
actionNew_folder - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " folder-new " ) ) ;
actionNew_subscription - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " list-add " ) ) ;
actionOpen_news_URL - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " application-x-mswinurl " ) ) ;
actionRename - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " edit-rename " ) ) ;
actionUpdate - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " view-refresh " ) ) ;
actionUpdate_all_feeds - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " view-refresh " ) ) ;
newFeedButton - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " list-add " ) ) ;
markReadButton - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " mail-mark-read " ) ) ;
updateAllButton - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " view-refresh " ) ) ;
rssDownloaderBtn - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " download " ) ) ;
settingsButton - > setIcon ( GuiIconProvider : : instance ( ) - > getIcon ( " preferences-system " ) ) ;
2014-12-07 13:11:18 +03:00
m_feedList = new FeedListWidget ( splitter_h , m_rssManager ) ;
splitter_h - > insertWidget ( 0 , m_feedList ) ;
listArticles - > setSelectionBehavior ( QAbstractItemView : : SelectItems ) ;
listArticles - > setSelectionMode ( QAbstractItemView : : SingleSelection ) ;
editHotkey = new QShortcut ( QKeySequence ( " F2 " ) , m_feedList , 0 , 0 , Qt : : WidgetShortcut ) ;
connect ( editHotkey , SIGNAL ( activated ( ) ) , SLOT ( renameSelectedRssFile ( ) ) ) ;
connect ( m_feedList , SIGNAL ( doubleClicked ( QModelIndex ) ) , SLOT ( renameSelectedRssFile ( ) ) ) ;
deleteHotkey = new QShortcut ( QKeySequence ( QKeySequence : : Delete ) , m_feedList , 0 , 0 , Qt : : WidgetShortcut ) ;
connect ( deleteHotkey , SIGNAL ( activated ( ) ) , SLOT ( deleteSelectedItems ( ) ) ) ;
m_rssManager - > loadStreamList ( ) ;
fillFeedsList ( ) ;
populateArticleList ( m_feedList - > currentItem ( ) ) ;
loadFoldersOpenState ( ) ;
connect ( m_rssManager . data ( ) , SIGNAL ( feedInfosChanged ( QString , QString , unsigned int ) ) , SLOT ( updateFeedInfos ( QString , QString , unsigned int ) ) ) ;
connect ( m_rssManager . data ( ) , SIGNAL ( feedContentChanged ( QString ) ) , SLOT ( onFeedContentChanged ( QString ) ) ) ;
connect ( m_rssManager . data ( ) , SIGNAL ( feedIconChanged ( QString , QString ) ) , SLOT ( updateFeedIcon ( QString , QString ) ) ) ;
connect ( m_feedList , SIGNAL ( customContextMenuRequested ( const QPoint & ) ) , SLOT ( displayRSSListMenu ( const QPoint & ) ) ) ;
connect ( listArticles , SIGNAL ( customContextMenuRequested ( const QPoint & ) ) , SLOT ( displayItemsListMenu ( const QPoint & ) ) ) ;
// Feeds list actions
connect ( actionDelete , SIGNAL ( triggered ( ) ) , this , SLOT ( deleteSelectedItems ( ) ) ) ;
connect ( actionRename , SIGNAL ( triggered ( ) ) , this , SLOT ( renameSelectedRssFile ( ) ) ) ;
connect ( actionUpdate , SIGNAL ( triggered ( ) ) , this , SLOT ( refreshSelectedItems ( ) ) ) ;
connect ( actionNew_folder , SIGNAL ( triggered ( ) ) , this , SLOT ( askNewFolder ( ) ) ) ;
connect ( actionNew_subscription , SIGNAL ( triggered ( ) ) , this , SLOT ( on_newFeedButton_clicked ( ) ) ) ;
connect ( actionUpdate_all_feeds , SIGNAL ( triggered ( ) ) , this , SLOT ( refreshAllFeeds ( ) ) ) ;
connect ( updateAllButton , SIGNAL ( clicked ( ) ) , SLOT ( refreshAllFeeds ( ) ) ) ;
connect ( actionCopy_feed_URL , SIGNAL ( triggered ( ) ) , this , SLOT ( copySelectedFeedsURL ( ) ) ) ;
connect ( actionMark_items_read , SIGNAL ( triggered ( ) ) , this , SLOT ( on_markReadButton_clicked ( ) ) ) ;
// News list actions
connect ( actionOpen_news_URL , SIGNAL ( triggered ( ) ) , this , SLOT ( openSelectedArticlesUrls ( ) ) ) ;
connect ( actionDownload_torrent , SIGNAL ( triggered ( ) ) , this , SLOT ( downloadSelectedTorrents ( ) ) ) ;
connect ( m_feedList , SIGNAL ( currentItemChanged ( QTreeWidgetItem * , QTreeWidgetItem * ) ) , this , SLOT ( populateArticleList ( QTreeWidgetItem * ) ) ) ;
connect ( m_feedList , SIGNAL ( foldersAltered ( QList < QTreeWidgetItem * > ) ) , this , SLOT ( updateItemsInfos ( QList < QTreeWidgetItem * > ) ) ) ;
connect ( listArticles , SIGNAL ( itemSelectionChanged ( ) ) , this , SLOT ( refreshTextBrowser ( ) ) ) ;
connect ( listArticles , SIGNAL ( itemDoubleClicked ( QListWidgetItem * ) ) , this , SLOT ( downloadSelectedTorrents ( ) ) ) ;
// Restore sliders position
restoreSlidersPosition ( ) ;
// Bind saveSliders slots
connect ( splitter_v , SIGNAL ( splitterMoved ( int , int ) ) , this , SLOT ( saveSlidersPosition ( ) ) ) ;
connect ( splitter_h , SIGNAL ( splitterMoved ( int , int ) ) , this , SLOT ( saveSlidersPosition ( ) ) ) ;
qDebug ( " RSSImp constructed " ) ;
2009-08-21 11:40:57 +04:00
}
2012-08-19 18:36:03 +04:00
RSSImp : : ~ RSSImp ( )
{
2014-12-07 13:11:18 +03:00
qDebug ( " Deleting RSSImp... " ) ;
saveFoldersOpenState ( ) ;
delete editHotkey ;
delete deleteHotkey ;
delete m_feedList ;
qDebug ( " RSSImp deleted " ) ;
2009-08-21 11:40:57 +04:00
}
2007-04-05 23:26:30 +04:00
2012-08-19 18:36:03 +04:00
void RSSImp : : on_settingsButton_clicked ( )
{
2014-12-07 13:11:18 +03:00
RssSettingsDlg dlg ( this ) ;
if ( dlg . exec ( ) )
updateRefreshInterval ( Preferences : : instance ( ) - > getRSSRefreshInterval ( ) ) ;
2010-06-22 17:39:49 +04:00
}
2010-11-13 13:49:22 +03:00
void RSSImp : : on_rssDownloaderBtn_clicked ( )
{
2014-12-07 13:11:18 +03:00
AutomatedRssDownloader dlg ( m_rssManager , this ) ;
dlg . exec ( ) ;
if ( dlg . isRssDownloaderEnabled ( ) ) {
m_rssManager - > recheckRssItemsForDownload ( ) ;
refreshAllFeeds ( ) ;
}
2010-11-13 13:49:22 +03:00
}