2012-08-02 09:17:53 -04:00
/****************************************************************
* RetroShare GUI is distributed under the following license :
*
* Copyright ( C ) 2012 by Thunder
*
* 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 .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2012-08-03 21:15:40 -04:00
# include <QKeyEvent>
2012-08-02 09:17:53 -04:00
# include <QMenu>
# include <QInputDialog>
# include <QPainter>
# include <QMessageBox>
# include "FeedReaderDialog.h"
2012-12-25 18:53:55 -05:00
# include "FeedReaderMessageWidget.h"
2012-08-02 09:17:53 -04:00
# include "ui_FeedReaderDialog.h"
# include "FeedReaderNotify.h"
# include "AddFeedDialog.h"
2012-08-13 17:35:11 -04:00
# include "FeedReaderStringDefs.h"
2012-08-02 09:17:53 -04:00
# include "gui/common/RSTreeWidgetItem.h"
# include "gui/settings/rsharesettings.h"
2012-12-20 08:30:16 -05:00
# include "FeedReaderUserNotify.h"
2012-08-13 17:35:11 -04:00
2012-08-02 09:17:53 -04:00
# include "interface/rsFeedReader.h"
# include "retroshare/rsiface.h"
# define COLUMN_FEED_COUNT 1
# define COLUMN_FEED_NAME 0
# define COLUMN_FEED_DATA 0
# define ROLE_FEED_ID Qt::UserRole
# define ROLE_FEED_SORT Qt::UserRole + 1
# define ROLE_FEED_FOLDER Qt::UserRole + 2
# define ROLE_FEED_UNREAD Qt::UserRole + 3
# define ROLE_FEED_NAME Qt::UserRole + 4
# define ROLE_FEED_WORKSTATE Qt::UserRole + 5
# define ROLE_FEED_LOADING Qt::UserRole + 6
# define ROLE_FEED_ICON Qt::UserRole + 7
# define ROLE_FEED_ERROR Qt::UserRole + 8
# define ROLE_FEED_DEACTIVATED Qt::UserRole + 9
FeedReaderDialog : : FeedReaderDialog ( RsFeedReader * feedReader , QWidget * parent )
: MainPage ( parent ) , mFeedReader ( feedReader ) , ui ( new Ui : : FeedReaderDialog )
{
/* Invoke the Qt Designer generated object setup routine */
ui - > setupUi ( this ) ;
2012-12-25 18:53:55 -05:00
mProcessSettings = false ;
mOpenFeedIds = NULL ;
2012-08-02 09:17:53 -04:00
mNotify = new FeedReaderNotify ( ) ;
mFeedReader - > setNotify ( mNotify ) ;
connect ( mNotify , SIGNAL ( notifyFeedChanged ( QString , int ) ) , this , SLOT ( feedChanged ( QString , int ) ) ) ;
connect ( mNotify , SIGNAL ( notifyMsgChanged ( QString , QString , int ) ) , this , SLOT ( msgChanged ( QString , QString , int ) ) ) ;
/* connect signals */
2012-12-29 21:41:53 -05:00
connect ( ui - > feedTreeWidget , SIGNAL ( itemActivated ( QTreeWidgetItem * , int ) ) , this , SLOT ( feedTreeItemActivated ( QTreeWidgetItem * ) ) ) ;
if ( ! style ( ) - > styleHint ( QStyle : : SH_ItemView_ActivateItemOnSingleClick , NULL , this ) ) {
// need signal itemClicked too
connect ( ui - > feedTreeWidget , SIGNAL ( itemClicked ( QTreeWidgetItem * , int ) ) , this , SLOT ( feedTreeItemActivated ( QTreeWidgetItem * ) ) ) ;
}
2012-08-02 09:17:53 -04:00
connect ( ui - > feedTreeWidget , SIGNAL ( customContextMenuRequested ( QPoint ) ) , this , SLOT ( feedTreeCustomPopupMenu ( QPoint ) ) ) ;
2012-12-29 21:41:53 -05:00
connect ( ui - > messageTabWidget , SIGNAL ( tabCloseRequested ( int ) ) , this , SLOT ( messageTabCloseRequested ( int ) ) ) ;
connect ( ui - > messageTabWidget , SIGNAL ( currentChanged ( int ) ) , this , SLOT ( messageTabChanged ( int ) ) ) ;
2012-12-27 19:42:27 -05:00
connect ( ui - > feedAddButton , SIGNAL ( clicked ( ) ) , this , SLOT ( newFeed ( ) ) ) ;
connect ( ui - > feedProcessButton , SIGNAL ( clicked ( ) ) , this , SLOT ( processFeed ( ) ) ) ;
2012-08-02 09:17:53 -04:00
mFeedCompareRole = new RSTreeWidgetItemCompareRole ;
mFeedCompareRole - > setRole ( COLUMN_FEED_NAME , ROLE_FEED_SORT ) ;
/* initialize root item */
mRootItem = new QTreeWidgetItem ( ui - > feedTreeWidget ) ;
QString name = tr ( " Message Folders " ) ;
mRootItem - > setText ( COLUMN_FEED_NAME , name ) ;
mRootItem - > setIcon ( COLUMN_FEED_NAME , QIcon ( " :/images/Root.png " ) ) ;
mRootItem - > setData ( COLUMN_FEED_DATA , ROLE_FEED_NAME , name ) ;
mRootItem - > setData ( COLUMN_FEED_DATA , ROLE_FEED_FOLDER , true ) ;
mRootItem - > setData ( COLUMN_FEED_DATA , ROLE_FEED_ICON , QIcon ( " :/images/Root.png " ) ) ;
mRootItem - > setExpanded ( true ) ;
/* set initial size the splitter */
QList < int > sizes ;
sizes < < 300 < < width ( ) ; // Qt calculates the right sizes
ui - > splitter - > setSizes ( sizes ) ;
/* load settings */
processSettings ( true ) ;
/* initialize feed list */
ui - > feedTreeWidget - > sortItems ( COLUMN_FEED_NAME , Qt : : AscendingOrder ) ;
2012-12-25 18:53:55 -05:00
ui - > feedTreeWidget - > installEventFilter ( this ) ;
2012-12-27 19:42:27 -05:00
2012-12-29 21:41:53 -05:00
mMessageWidget = createMessageWidget ( " " ) ;
// remove close button of the the first tab
ui - > messageTabWidget - > hideCloseButton ( ui - > messageTabWidget - > indexOf ( mMessageWidget ) ) ;
feedTreeItemActivated ( NULL ) ;
2012-08-02 09:17:53 -04:00
}
FeedReaderDialog : : ~ FeedReaderDialog ( )
{
/* save settings */
processSettings ( false ) ;
delete ( mFeedCompareRole ) ;
delete ( ui ) ;
mFeedReader - > setNotify ( NULL ) ;
delete ( mNotify ) ;
2012-12-25 18:53:55 -05:00
if ( mOpenFeedIds ) {
delete mOpenFeedIds ;
mOpenFeedIds = NULL ;
}
2012-08-02 09:17:53 -04:00
}
2012-12-20 08:30:16 -05:00
UserNotify * FeedReaderDialog : : getUserNotify ( QObject * parent )
{
return new FeedReaderUserNotify ( this , mFeedReader , mNotify , parent ) ;
}
2012-08-02 09:17:53 -04:00
void FeedReaderDialog : : processSettings ( bool load )
{
mProcessSettings = true ;
Settings - > beginGroup ( QString ( " FeedReaderDialog " ) ) ;
if ( load ) {
// load settings
// state of splitter
ui - > splitter - > restoreState ( Settings - > value ( " Splitter " ) . toByteArray ( ) ) ;
2012-12-25 18:53:55 -05:00
// open feeds
int arrayIndex = Settings - > beginReadArray ( " Feeds " ) ;
for ( int index = 0 ; index < arrayIndex ; index + + ) {
Settings - > setArrayIndex ( index ) ;
addFeedToExpand ( Settings - > value ( " open " ) . toString ( ) . toStdString ( ) ) ;
}
Settings - > endArray ( ) ;
2012-08-02 09:17:53 -04:00
} else {
// save settings
// state of splitter
Settings - > setValue ( " Splitter " , ui - > splitter - > saveState ( ) ) ;
2012-12-25 18:53:55 -05:00
// open groups
Settings - > beginWriteArray ( " Feeds " ) ;
int arrayIndex = 0 ;
QList < std : : string > expandedFeedIds ;
getExpandedFeedIds ( expandedFeedIds ) ;
foreach ( std : : string feedId , expandedFeedIds ) {
Settings - > setArrayIndex ( arrayIndex + + ) ;
Settings - > setValue ( " open " , QString : : fromStdString ( feedId ) ) ;
}
Settings - > endArray ( ) ;
2012-08-02 09:17:53 -04:00
}
Settings - > endGroup ( ) ;
mProcessSettings = false ;
}
2012-12-25 18:53:55 -05:00
void FeedReaderDialog : : addFeedToExpand ( const std : : string & feedId )
{
if ( mOpenFeedIds = = NULL ) {
mOpenFeedIds = new QList < std : : string > ;
}
if ( mOpenFeedIds - > contains ( feedId ) ) {
return ;
}
mOpenFeedIds - > push_back ( feedId ) ;
}
void FeedReaderDialog : : getExpandedFeedIds ( QList < std : : string > & feedIds )
{
QTreeWidgetItemIterator it ( ui - > feedTreeWidget ) ;
QTreeWidgetItem * item ;
while ( ( item = * it ) ! = NULL ) {
+ + it ;
if ( ! item - > isExpanded ( ) ) {
continue ;
}
if ( ! item - > data ( COLUMN_FEED_DATA , ROLE_FEED_FOLDER ) . toBool ( ) ) {
continue ;
}
std : : string feedId = item - > data ( COLUMN_FEED_DATA , ROLE_FEED_ID ) . toString ( ) . toStdString ( ) ;
if ( feedId . empty ( ) ) {
continue ;
}
feedIds . push_back ( feedId ) ;
}
}
2012-08-02 09:17:53 -04:00
void FeedReaderDialog : : showEvent ( QShowEvent */ * event */ )
{
updateFeeds ( " " , mRootItem ) ;
}
bool FeedReaderDialog : : eventFilter ( QObject * obj , QEvent * event )
{
if ( obj = = ui - > feedTreeWidget ) {
if ( event - > type ( ) = = QEvent : : KeyPress ) {
QKeyEvent * keyEvent = static_cast < QKeyEvent * > ( event ) ;
if ( keyEvent ) {
if ( keyEvent - > key ( ) = = Qt : : Key_Delete ) {
/* Delete pressed */
removeFeed ( ) ;
return true ; // eat event
}
}
}
}
/* pass the event on to the parent class */
return MainPage : : eventFilter ( obj , event ) ;
}
std : : string FeedReaderDialog : : currentFeedId ( )
{
QTreeWidgetItem * item = ui - > feedTreeWidget - > currentItem ( ) ;
if ( ! item ) {
return " " ;
}
return item - > data ( COLUMN_FEED_DATA , ROLE_FEED_ID ) . toString ( ) . toStdString ( ) ;
}
2012-12-29 21:41:53 -05:00
void FeedReaderDialog : : setCurrentFeedId ( const std : : string & feedId )
{
if ( feedId . empty ( ) ) {
return ;
}
QTreeWidgetItemIterator it ( ui - > feedTreeWidget ) ;
QTreeWidgetItem * item ;
while ( ( item = * it ) ! = NULL ) {
if ( item - > data ( COLUMN_FEED_DATA , ROLE_FEED_ID ) . toString ( ) . toStdString ( ) = = feedId ) {
ui - > feedTreeWidget - > setCurrentItem ( item ) ;
break ;
}
+ + it ;
}
}
2012-08-02 09:17:53 -04:00
void FeedReaderDialog : : feedTreeCustomPopupMenu ( QPoint /*point*/ )
{
QMenu contextMnu ( this ) ;
bool folder = false ;
2012-12-29 21:41:53 -05:00
std : : string feedId ;
2012-08-02 09:17:53 -04:00
QTreeWidgetItem * item = ui - > feedTreeWidget - > currentItem ( ) ;
if ( item ) {
folder = item - > data ( COLUMN_FEED_DATA , ROLE_FEED_FOLDER ) . toBool ( ) ;
2012-12-29 21:41:53 -05:00
feedId = item - > data ( COLUMN_FEED_DATA , ROLE_FEED_ID ) . toString ( ) . toStdString ( ) ;
2012-08-02 09:17:53 -04:00
}
QMenu * menu = contextMnu . addMenu ( QIcon ( " " ) , tr ( " New " ) ) ;
QAction * action = menu - > addAction ( QIcon ( " :/images/FeedAdd.png " ) , tr ( " Feed " ) , this , SLOT ( newFeed ( ) ) ) ;
if ( ! item | | ! folder ) {
action - > setEnabled ( false ) ;
}
action = menu - > addAction ( QIcon ( " :/images/FolderAdd.png " ) , tr ( " Folder " ) , this , SLOT ( newFolder ( ) ) ) ;
if ( ! item | | ! folder ) {
action - > setEnabled ( false ) ;
}
contextMnu . addSeparator ( ) ;
2012-12-29 21:41:53 -05:00
action = contextMnu . addAction ( QIcon ( " " ) , tr ( " Open in new tab " ) , this , SLOT ( openInNewTab ( ) ) ) ;
if ( ! item | | folder | | feedMessageWidget ( feedId ) ) {
action - > setEnabled ( false ) ;
}
contextMnu . addSeparator ( ) ;
2012-08-02 09:17:53 -04:00
action = contextMnu . addAction ( QIcon ( " :/images/edit_16.png " ) , tr ( " Edit " ) , this , SLOT ( editFeed ( ) ) ) ;
if ( ! item | | item = = mRootItem ) {
action - > setEnabled ( false ) ;
}
action = contextMnu . addAction ( QIcon ( " :/images/delete.png " ) , tr ( " Delete " ) , this , SLOT ( removeFeed ( ) ) ) ;
if ( ! item | | item = = mRootItem ) {
action - > setEnabled ( false ) ;
}
contextMnu . addSeparator ( ) ;
bool deactivated = false ;
if ( ! folder & & item ) {
deactivated = item - > data ( COLUMN_FEED_DATA , ROLE_FEED_DEACTIVATED ) . toBool ( ) ;
}
action = contextMnu . addAction ( QIcon ( " :/images/Update.png " ) , tr ( " Update " ) , this , SLOT ( processFeed ( ) ) ) ;
action - > setEnabled ( ! deactivated ) ;
action = contextMnu . addAction ( QIcon ( " " ) , deactivated ? tr ( " Activate " ) : tr ( " Deactivate " ) , this , SLOT ( activateFeed ( ) ) ) ;
if ( ! item | | item = = mRootItem | | folder ) {
action - > setEnabled ( false ) ;
}
contextMnu . exec ( QCursor : : pos ( ) ) ;
}
void FeedReaderDialog : : updateFeeds ( const std : : string & parentId , QTreeWidgetItem * parentItem )
{
if ( ! parentItem ) {
return ;
}
/* get feed infos */
std : : list < FeedInfo > feedInfos ;
mFeedReader - > getFeedList ( parentId , feedInfos ) ;
int index = 0 ;
QTreeWidgetItem * item ;
std : : list < FeedInfo > : : iterator feedIt ;
/* update existing and delete not existing feeds */
while ( index < parentItem - > childCount ( ) ) {
item = parentItem - > child ( index ) ;
std : : string feedId = item - > data ( COLUMN_FEED_DATA , ROLE_FEED_ID ) . toString ( ) . toStdString ( ) ;
/* search existing feed */
int found = - 1 ;
for ( feedIt = feedInfos . begin ( ) ; feedIt ! = feedInfos . end ( ) ; + + feedIt ) {
if ( feedIt - > feedId = = feedId ) {
/* found it, update it */
updateFeedItem ( item , * feedIt ) ;
if ( feedIt - > flag . folder ) {
/* process child feeds */
updateFeeds ( feedIt - > feedId , item ) ;
}
feedInfos . erase ( feedIt ) ;
found = index ;
break ;
}
}
if ( found > = 0 ) {
+ + index ;
} else {
delete ( parentItem - > takeChild ( index ) ) ;
}
}
/* add new feeds */
for ( feedIt = feedInfos . begin ( ) ; feedIt ! = feedInfos . end ( ) ; + + feedIt ) {
item = new RSTreeWidgetItem ( mFeedCompareRole ) ;
parentItem - > addChild ( item ) ;
updateFeedItem ( item , * feedIt ) ;
if ( feedIt - > flag . folder ) {
/* process child feeds */
updateFeeds ( feedIt - > feedId , item ) ;
2012-12-25 18:53:55 -05:00
if ( mOpenFeedIds ) {
int index = mOpenFeedIds - > indexOf ( feedIt - > feedId ) ;
if ( index > = 0 ) {
item - > setExpanded ( true ) ;
mOpenFeedIds - > removeAt ( index ) ;
}
}
2012-08-02 09:17:53 -04:00
}
}
2012-12-25 18:53:55 -05:00
if ( mOpenFeedIds & & mOpenFeedIds - > empty ( ) ) {
delete mOpenFeedIds ;
mOpenFeedIds = NULL ;
}
2012-08-02 09:17:53 -04:00
calculateFeedItems ( ) ;
}
void FeedReaderDialog : : calculateFeedItem ( QTreeWidgetItem * item , uint32_t & unreadCount , bool & loading )
{
uint32_t unreadCountItem = 0 ;
bool loadingItem = false ;
if ( item - > data ( COLUMN_FEED_DATA , ROLE_FEED_FOLDER ) . toBool ( ) ) {
int childCount = item - > childCount ( ) ;
for ( int index = 0 ; index < childCount ; + + index ) {
calculateFeedItem ( item - > child ( index ) , unreadCountItem , loadingItem ) ;
}
} else {
unreadCountItem = item - > data ( COLUMN_FEED_DATA , ROLE_FEED_UNREAD ) . toUInt ( ) ;
loadingItem = item - > data ( COLUMN_FEED_DATA , ROLE_FEED_LOADING ) . toBool ( ) ;
}
unreadCount + = unreadCountItem ;
loading = loading | | loadingItem ;
QString name = item - > data ( COLUMN_FEED_DATA , ROLE_FEED_NAME ) . toString ( ) ;
QString workState = item - > data ( COLUMN_FEED_DATA , ROLE_FEED_WORKSTATE ) . toString ( ) ;
if ( unreadCountItem ) {
name + = QString ( " (%1) " ) . arg ( unreadCountItem ) ;
}
if ( ! workState . isEmpty ( ) ) {
name + = QString ( " (%1) " ) . arg ( workState ) ;
}
item - > setText ( COLUMN_FEED_NAME , name ) ;
bool deactivated = item - > data ( COLUMN_FEED_DATA , ROLE_FEED_DEACTIVATED ) . toBool ( ) ;
QColor colorActivated ;
QColor colorDeactivated = QColor ( Qt : : gray ) ;
for ( int i = 0 ; i < COLUMN_FEED_COUNT ; i + + ) {
QFont font = item - > font ( i ) ;
font . setBold ( unreadCountItem ! = 0 ) ;
item - > setFont ( i , font ) ;
item - > setTextColor ( COLUMN_FEED_NAME , deactivated ? colorDeactivated : colorActivated ) ;
}
QIcon icon = item - > data ( COLUMN_FEED_DATA , ROLE_FEED_ICON ) . value < QIcon > ( ) ;
if ( deactivated ) {
/* create disabled icon */
icon = icon . pixmap ( QSize ( 16 , 16 ) , QIcon : : Disabled ) ;
}
QImage overlayIcon ;
if ( loadingItem ) {
/* overlaying icon */
overlayIcon = QImage ( " :/images/FeedProcessOverlay.png " ) ;
} else if ( item - > data ( COLUMN_FEED_DATA , ROLE_FEED_ERROR ) . toBool ( ) ) {
overlayIcon = QImage ( " :/images/FeedErrorOverlay.png " ) ;
}
if ( ! overlayIcon . isNull ( ) ) {
if ( icon . isNull ( ) ) {
icon = QPixmap : : fromImage ( overlayIcon ) ;
} else {
QPixmap pixmap = icon . pixmap ( QSize ( 16 , 16 ) ) ;
QPainter painter ( & pixmap ) ;
painter . drawImage ( 0 , 0 , overlayIcon . scaled ( pixmap . size ( ) , Qt : : IgnoreAspectRatio , Qt : : SmoothTransformation ) ) ;
painter . end ( ) ;
icon = pixmap ;
}
}
item - > setIcon ( COLUMN_FEED_NAME , icon ) ;
}
void FeedReaderDialog : : calculateFeedItems ( )
{
uint32_t unreadCount = 0 ;
bool loading = false ;
calculateFeedItem ( mRootItem , unreadCount , loading ) ;
ui - > feedTreeWidget - > sortItems ( COLUMN_FEED_NAME , Qt : : AscendingOrder ) ;
}
void FeedReaderDialog : : updateFeedItem ( QTreeWidgetItem * item , FeedInfo & info )
{
QIcon icon ;
if ( info . flag . folder ) {
/* use folder icon */
icon = QIcon ( " :/images/Folder.png " ) ;
} else {
long todo ; // show icon from feed
// if (info.icon.empty()) {
/* use standard icon */
icon = QIcon ( " :/images/Feed.png " ) ;
// } else {
// /* use icon from feed */
// icon = QIcon(QPixmap::fromImage(QImage((uchar*) QByteArray::fromBase64(info.icon.c_str()).constData(), 16, 16, QImage::Format_RGB32)));
// }
}
item - > setData ( COLUMN_FEED_DATA , ROLE_FEED_ICON , icon ) ;
QString name = QString : : fromUtf8 ( info . name . c_str ( ) ) ;
item - > setData ( COLUMN_FEED_DATA , ROLE_FEED_NAME , name . isEmpty ( ) ? tr ( " No name " ) : name ) ;
2012-08-13 17:35:11 -04:00
item - > setData ( COLUMN_FEED_DATA , ROLE_FEED_WORKSTATE , FeedReaderStringDefs : : workState ( info . workstate ) ) ;
2012-08-02 09:17:53 -04:00
uint32_t unreadCount ;
mFeedReader - > getMessageCount ( info . feedId , NULL , NULL , & unreadCount ) ;
item - > setData ( COLUMN_FEED_NAME , ROLE_FEED_SORT , QString ( " %1_%2 " ) . arg ( QString ( info . flag . folder ? " 0 " : " 1 " ) , name ) ) ;
item - > setData ( COLUMN_FEED_DATA , ROLE_FEED_UNREAD , unreadCount ) ;
item - > setData ( COLUMN_FEED_DATA , ROLE_FEED_LOADING , info . workstate ! = FeedInfo : : WAITING ) ;
item - > setData ( COLUMN_FEED_DATA , ROLE_FEED_ID , QString : : fromStdString ( info . feedId ) ) ;
item - > setData ( COLUMN_FEED_DATA , ROLE_FEED_FOLDER , info . flag . folder ) ;
item - > setData ( COLUMN_FEED_DATA , ROLE_FEED_DEACTIVATED , info . flag . deactivated ) ;
2012-08-13 17:35:11 -04:00
item - > setData ( COLUMN_FEED_DATA , ROLE_FEED_ERROR , ( bool ) ( info . errorState ! = RS_FEED_ERRORSTATE_OK ) ) ;
item - > setToolTip ( COLUMN_FEED_NAME , ( info . errorState ! = RS_FEED_ERRORSTATE_OK ) ? FeedReaderStringDefs : : errorString ( info ) : " " ) ;
2012-08-02 09:17:53 -04:00
}
void FeedReaderDialog : : feedChanged ( const QString & feedId , int type )
{
if ( ! isVisible ( ) ) {
/* complete update in showEvent */
return ;
}
if ( feedId . isEmpty ( ) ) {
return ;
}
FeedInfo feedInfo ;
if ( type ! = NOTIFY_TYPE_DEL ) {
if ( ! mFeedReader - > getFeedInfo ( feedId . toStdString ( ) , feedInfo ) ) {
return ;
}
2012-08-13 17:35:11 -04:00
if ( feedInfo . flag . preview ) {
return ;
}
2012-08-02 09:17:53 -04:00
}
if ( type = = NOTIFY_TYPE_MOD | | type = = NOTIFY_TYPE_DEL ) {
QTreeWidgetItemIterator it ( ui - > feedTreeWidget ) ;
QTreeWidgetItem * item ;
while ( ( item = * it ) ! = NULL ) {
if ( item - > data ( COLUMN_FEED_DATA , ROLE_FEED_ID ) . toString ( ) = = feedId ) {
if ( type = = NOTIFY_TYPE_MOD ) {
updateFeedItem ( item , feedInfo ) ;
} else {
delete ( item ) ;
}
break ;
}
+ + it ;
}
}
if ( type = = NOTIFY_TYPE_ADD ) {
QString id = QString : : fromStdString ( feedInfo . parentId ) ;
QTreeWidgetItemIterator it ( ui - > feedTreeWidget ) ;
QTreeWidgetItem * itemParent ;
while ( ( itemParent = * it ) ! = NULL ) {
if ( itemParent - > data ( COLUMN_FEED_DATA , ROLE_FEED_ID ) . toString ( ) = = id ) {
QTreeWidgetItem * item = new RSTreeWidgetItem ( mFeedCompareRole ) ;
itemParent - > addChild ( item ) ;
updateFeedItem ( item , feedInfo ) ;
break ;
}
+ + it ;
}
}
calculateFeedItems ( ) ;
}
2012-12-25 18:53:55 -05:00
FeedReaderMessageWidget * FeedReaderDialog : : feedMessageWidget ( const std : : string & id )
2012-08-02 09:17:53 -04:00
{
2012-12-25 18:53:55 -05:00
int tabCount = ui - > messageTabWidget - > count ( ) ;
for ( int index = 0 ; index < tabCount ; + + index ) {
FeedReaderMessageWidget * childWidget = dynamic_cast < FeedReaderMessageWidget * > ( ui - > messageTabWidget - > widget ( index ) ) ;
2012-12-29 21:41:53 -05:00
if ( childWidget = = mMessageWidget ) {
continue ;
}
2012-12-25 18:53:55 -05:00
if ( childWidget & & childWidget - > feedId ( ) = = id ) {
return childWidget ;
2012-08-02 09:17:53 -04:00
}
}
2012-12-25 18:53:55 -05:00
return NULL ;
2012-08-02 09:17:53 -04:00
}
2012-12-29 21:41:53 -05:00
FeedReaderMessageWidget * FeedReaderDialog : : createMessageWidget ( const std : : string & feedId )
{
FeedReaderMessageWidget * messageWidget = new FeedReaderMessageWidget ( feedId , mFeedReader , mNotify ) ;
int index = ui - > messageTabWidget - > addTab ( messageWidget , messageWidget - > feedName ( true ) ) ;
ui - > messageTabWidget - > setTabIcon ( index , messageWidget - > feedIcon ( ) ) ;
connect ( messageWidget , SIGNAL ( feedMessageChanged ( QWidget * ) ) , this , SLOT ( messageTabInfoChanged ( QWidget * ) ) ) ;
return messageWidget ;
}
void FeedReaderDialog : : feedTreeItemActivated ( QTreeWidgetItem * item )
2012-08-02 09:17:53 -04:00
{
if ( ! item ) {
2012-12-27 19:42:27 -05:00
ui - > feedAddButton - > setEnabled ( false ) ;
ui - > feedProcessButton - > setEnabled ( false ) ;
2012-08-02 09:17:53 -04:00
return ;
}
2012-12-27 19:42:27 -05:00
ui - > feedProcessButton - > setEnabled ( true ) ;
2012-12-25 18:53:55 -05:00
if ( item - > data ( COLUMN_FEED_DATA , ROLE_FEED_FOLDER ) . toBool ( ) ) {
2012-12-27 19:42:27 -05:00
ui - > feedAddButton - > setEnabled ( true ) ;
2012-08-02 09:17:53 -04:00
return ;
}
2012-12-27 19:42:27 -05:00
ui - > feedAddButton - > setEnabled ( false ) ;
2012-12-25 18:53:55 -05:00
std : : string feedId = item - > data ( COLUMN_FEED_DATA , ROLE_FEED_ID ) . toString ( ) . toStdString ( ) ;
/* search exisiting tab */
FeedReaderMessageWidget * messageWidget = feedMessageWidget ( feedId ) ;
2012-12-29 21:41:53 -05:00
if ( ! messageWidget ) {
/* not found, use standard tab */
messageWidget = mMessageWidget ;
messageWidget - > setFeedId ( feedId ) ;
}
ui - > messageTabWidget - > setCurrentWidget ( messageWidget ) ;
}
2012-08-02 09:17:53 -04:00
2012-12-29 21:41:53 -05:00
void FeedReaderDialog : : openInNewTab ( )
{
std : : string feedId = currentFeedId ( ) ;
if ( feedId . empty ( ) ) {
return ;
}
/* search exisiting tab */
FeedReaderMessageWidget * messageWidget = feedMessageWidget ( feedId ) ;
2012-12-25 18:53:55 -05:00
if ( ! messageWidget ) {
2012-12-29 21:41:53 -05:00
/* not found, create new tab */
messageWidget = createMessageWidget ( feedId ) ;
2012-08-02 09:17:53 -04:00
}
2012-12-25 18:53:55 -05:00
ui - > messageTabWidget - > setCurrentWidget ( messageWidget ) ;
2012-08-02 09:17:53 -04:00
}
2012-12-25 18:53:55 -05:00
void FeedReaderDialog : : messageTabCloseRequested ( int index )
2012-08-02 09:17:53 -04:00
{
2012-12-25 18:53:55 -05:00
FeedReaderMessageWidget * messageWidget = dynamic_cast < FeedReaderMessageWidget * > ( ui - > messageTabWidget - > widget ( index ) ) ;
2012-12-29 21:41:53 -05:00
if ( ! messageWidget ) {
return ;
}
if ( messageWidget = = mMessageWidget ) {
return ;
2012-08-02 09:17:53 -04:00
}
2012-12-29 21:41:53 -05:00
delete ( messageWidget ) ;
}
void FeedReaderDialog : : messageTabChanged ( int index )
{
FeedReaderMessageWidget * messageWidget = dynamic_cast < FeedReaderMessageWidget * > ( ui - > messageTabWidget - > widget ( index ) ) ;
if ( ! messageWidget ) {
return ;
}
setCurrentFeedId ( messageWidget - > feedId ( ) ) ;
2012-08-02 09:17:53 -04:00
}
2012-12-29 21:41:53 -05:00
void FeedReaderDialog : : messageTabInfoChanged ( QWidget * widget )
2012-08-02 09:17:53 -04:00
{
2012-12-25 18:53:55 -05:00
int index = ui - > messageTabWidget - > indexOf ( widget ) ;
if ( index < 0 ) {
2012-08-02 09:17:53 -04:00
return ;
}
2012-12-25 18:53:55 -05:00
FeedReaderMessageWidget * messageWidget = dynamic_cast < FeedReaderMessageWidget * > ( ui - > messageTabWidget - > widget ( index ) ) ;
if ( ! messageWidget ) {
return ;
2012-08-02 09:17:53 -04:00
}
2012-12-25 18:53:55 -05:00
ui - > messageTabWidget - > setTabText ( index , messageWidget - > feedName ( true ) ) ;
ui - > messageTabWidget - > setTabIcon ( index , messageWidget - > feedIcon ( ) ) ;
2012-08-02 09:17:53 -04:00
}
void FeedReaderDialog : : newFolder ( )
{
QInputDialog dialog ;
dialog . setWindowTitle ( tr ( " Add new folder " ) ) ;
dialog . setLabelText ( tr ( " Please enter a name for the folder " ) ) ;
dialog . setWindowIcon ( QIcon ( " :/images/FeedReader.png " ) ) ;
if ( dialog . exec ( ) = = QDialog : : Accepted & & ! dialog . textValue ( ) . isEmpty ( ) ) {
std : : string feedId ;
RsFeedAddResult result = mFeedReader - > addFolder ( currentFeedId ( ) , dialog . textValue ( ) . toUtf8 ( ) . constData ( ) , feedId ) ;
2012-08-13 17:35:11 -04:00
FeedReaderStringDefs : : showError ( this , result , tr ( " Create folder " ) , tr ( " Cannot create folder. " ) ) ;
2012-08-02 09:17:53 -04:00
}
}
void FeedReaderDialog : : newFeed ( )
{
2012-08-13 17:35:11 -04:00
AddFeedDialog dialog ( mFeedReader , mNotify , this ) ;
2012-08-02 09:17:53 -04:00
dialog . setParent ( currentFeedId ( ) ) ;
dialog . exec ( ) ;
}
void FeedReaderDialog : : removeFeed ( )
{
std : : string feedId = currentFeedId ( ) ;
if ( feedId . empty ( ) ) {
return ;
}
QTreeWidgetItem * item = ui - > feedTreeWidget - > currentItem ( ) ;
if ( ! item ) {
return ;
}
bool folder = item - > data ( COLUMN_FEED_DATA , ROLE_FEED_FOLDER ) . toBool ( ) ;
QString name = item - > data ( COLUMN_FEED_DATA , ROLE_FEED_NAME ) . toString ( ) ;
if ( QMessageBox : : question ( this , folder ? tr ( " Remove folder " ) : tr ( " Remove feed " ) , folder ? tr ( " Do you want to remove the folder %1? " ) . arg ( name ) : tr ( " Do you want to remove the feed %1? " ) . arg ( name ) , QMessageBox : : Yes | QMessageBox : : No , QMessageBox : : No ) = = QMessageBox : : Yes ) {
mFeedReader - > removeFeed ( feedId ) ;
}
}
void FeedReaderDialog : : editFeed ( )
{
std : : string feedId = currentFeedId ( ) ;
if ( feedId . empty ( ) ) {
return ;
}
QTreeWidgetItem * item = ui - > feedTreeWidget - > currentItem ( ) ;
if ( ! item ) {
return ;
}
bool folder = item - > data ( COLUMN_FEED_DATA , ROLE_FEED_FOLDER ) . toBool ( ) ;
if ( folder ) {
QInputDialog dialog ;
dialog . setWindowTitle ( tr ( " Edit folder " ) ) ;
dialog . setLabelText ( tr ( " Please enter a new name for the folder " ) ) ;
dialog . setWindowIcon ( QIcon ( " :/images/FeedReader.png " ) ) ;
dialog . setTextValue ( item - > data ( COLUMN_FEED_DATA , ROLE_FEED_NAME ) . toString ( ) ) ;
if ( dialog . exec ( ) = = QDialog : : Accepted & & ! dialog . textValue ( ) . isEmpty ( ) ) {
RsFeedAddResult result = mFeedReader - > setFolder ( feedId , dialog . textValue ( ) . toUtf8 ( ) . constData ( ) ) ;
2012-08-13 17:35:11 -04:00
FeedReaderStringDefs : : showError ( this , result , tr ( " Create folder " ) , tr ( " Cannot create folder. " ) ) ;
2012-08-02 09:17:53 -04:00
}
} else {
2012-08-13 17:35:11 -04:00
AddFeedDialog dialog ( mFeedReader , mNotify , this ) ;
2012-08-02 09:17:53 -04:00
if ( ! dialog . fillFeed ( feedId ) ) {
return ;
}
dialog . exec ( ) ;
}
}
void FeedReaderDialog : : activateFeed ( )
{
std : : string feedId = currentFeedId ( ) ;
if ( feedId . empty ( ) ) {
return ;
}
FeedInfo feedInfo ;
if ( ! mFeedReader - > getFeedInfo ( feedId , feedInfo ) ) {
return ;
}
if ( feedInfo . flag . folder ) {
return ;
}
feedInfo . flag . deactivated = ! feedInfo . flag . deactivated ;
mFeedReader - > setFeed ( feedId , feedInfo ) ;
}
void FeedReaderDialog : : processFeed ( )
{
std : : string feedId = currentFeedId ( ) ;
/* empty feed id process all feeds */
mFeedReader - > processFeed ( feedId ) ;
}