2008-06-20 08:43:23 -04:00
/****************************************************************
* RetroShare is distributed under the following license :
*
* Copyright ( C ) 2008 Robert Fernie
*
* 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
2009-09-12 06:55:40 -04:00
* Foundation , Inc . , 51 Franklin Street , Fifth Floor ,
2008-06-20 08:43:23 -04:00
* Boston , MA 02110 - 1301 , USA .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2010-07-23 14:52:58 -04:00
# include <QMenu>
# include <QTimer>
# include <QStandardItemModel>
2012-04-21 09:00:17 -04:00
# include <QMessageBox>
2008-06-20 08:43:23 -04:00
2008-06-24 00:36:45 -04:00
# include <iostream>
2008-11-02 10:20:42 -05:00
# include <algorithm>
2010-11-06 20:02:51 -04:00
# include <set>
# include <map>
2008-06-24 00:36:45 -04:00
2008-06-20 08:43:23 -04:00
# include "ChannelFeed.h"
2010-01-29 20:13:03 -05:00
2010-10-06 20:17:42 -04:00
# include "feeds/ChanMsgItem.h"
2010-12-25 07:52:29 -05:00
# include "common/PopularityDefs.h"
2011-01-04 15:19:45 -05:00
# include "settings/rsharesettings.h"
2008-06-20 08:43:23 -04:00
2010-10-06 20:17:42 -04:00
# include "channels/CreateChannel.h"
# include "channels/ChannelDetails.h"
# include "channels/CreateChannelMsg.h"
# include "channels/EditChanDetails.h"
# include "channels/ShareKey.h"
# include "notifyqt.h"
2011-04-19 15:42:44 -04:00
# include "RetroShareLink.h"
2008-06-24 00:36:45 -04:00
2010-05-14 09:06:58 -04:00
# define CHAN_DEFAULT_IMAGE ": / images / channels.png"
2010-11-06 20:02:51 -04:00
# define WARNING_LIMIT 3600*24*2
2008-07-09 05:53:47 -04:00
/****
* # define CHAN_DEBUG
* * */
2008-06-24 00:36:45 -04:00
2012-04-24 11:34:22 -04:00
# define USE_THREAD
2008-06-20 08:43:23 -04:00
/** Constructor */
ChannelFeed : : ChannelFeed ( QWidget * parent )
2010-07-23 07:55:21 -04:00
: RsAutoUpdatePage ( 1000 , parent )
2008-06-20 08:43:23 -04:00
{
2010-10-06 20:17:42 -04:00
/* Invoke the Qt Designer generated object setup routine */
setupUi ( this ) ;
2012-01-13 11:26:24 -05:00
connect ( newChannelButton , SIGNAL ( clicked ( ) ) , this , SLOT ( createChannel ( ) ) ) ;
2010-10-06 20:17:42 -04:00
connect ( postButton , SIGNAL ( clicked ( ) ) , this , SLOT ( createMsg ( ) ) ) ;
connect ( subscribeButton , SIGNAL ( clicked ( void ) ) , this , SLOT ( subscribeChannel ( void ) ) ) ;
connect ( unsubscribeButton , SIGNAL ( clicked ( void ) ) , this , SLOT ( unsubscribeChannel ( void ) ) ) ;
2012-01-13 11:26:24 -05:00
connect ( actionsetAllAsRead , SIGNAL ( triggered ( ) ) , this , SLOT ( setAllAsReadClicked ( ) ) ) ;
connect ( actionEnable_Auto_Download , SIGNAL ( triggered ( ) ) , this , SLOT ( toggleAutoDownload ( ) ) ) ;
2010-10-06 20:17:42 -04:00
connect ( NotifyQt : : getInstance ( ) , SIGNAL ( channelMsgReadSatusChanged ( QString , QString , int ) ) , this , SLOT ( channelMsgReadSatusChanged ( QString , QString , int ) ) ) ;
/*************** Setup Left Hand Side (List of Channels) ****************/
2010-12-26 19:15:25 -05:00
connect ( treeWidget , SIGNAL ( treeCustomContextMenuRequested ( QPoint ) ) , this , SLOT ( channelListCustomPopupMenu ( QPoint ) ) ) ;
connect ( treeWidget , SIGNAL ( treeCurrentItemChanged ( QString ) ) , this , SLOT ( selectChannel ( QString ) ) ) ;
2010-10-06 20:17:42 -04:00
mChannelId . clear ( ) ;
2010-10-11 18:39:47 -04:00
2011-02-03 19:53:44 -05:00
/* Set initial size the splitter */
QList < int > sizes ;
sizes < < 300 < < width ( ) ; // Qt calculates the right sizes
splitter - > setSizes ( sizes ) ;
2011-01-04 15:19:45 -05:00
/* Initialize group tree */
treeWidget - > initDisplayMenu ( displayButton ) ;
ownChannels = treeWidget - > addCategoryItem ( tr ( " Own Channels " ) , QIcon ( ) , true ) ;
subcribedChannels = treeWidget - > addCategoryItem ( tr ( " Subscribed Channels " ) , QIcon ( ) , true ) ;
popularChannels = treeWidget - > addCategoryItem ( tr ( " Popular Channels " ) , QIcon ( ) , false ) ;
otherChannels = treeWidget - > addCategoryItem ( tr ( " Other Channels " ) , QIcon ( ) , false ) ;
2010-09-15 12:06:31 -04:00
2012-01-13 11:26:24 -05:00
QMenu * channeloptsmenu = new QMenu ( ) ;
channeloptsmenu - > addAction ( actionsetAllAsRead ) ;
channeloptsmenu - > addAction ( actionEnable_Auto_Download ) ;
channeloptions_Button - > setMenu ( channeloptsmenu ) ;
2011-04-07 18:10:10 -04:00
2012-04-21 09:00:17 -04:00
progressLabel - > hide ( ) ;
progressBar - > hide ( ) ;
fillThread = NULL ;
2010-10-06 20:17:42 -04:00
//added from ahead
updateChannelList ( ) ;
2009-09-13 12:33:59 -04:00
2010-12-26 19:15:25 -05:00
nameLabel - > setFont ( QFont ( " MS SANS SERIF " , 22 ) ) ;
2010-10-06 20:17:42 -04:00
nameLabel - > setMinimumWidth ( 20 ) ;
2011-01-04 15:19:45 -05:00
/* load settings */
processSettings ( true ) ;
2010-09-13 11:11:20 -04:00
updateChannelMsgs ( ) ;
2008-06-20 08:43:23 -04:00
}
2011-01-04 15:19:45 -05:00
ChannelFeed : : ~ ChannelFeed ( )
{
2012-04-21 09:00:17 -04:00
if ( fillThread ) {
fillThread - > stop ( ) ;
delete ( fillThread ) ;
fillThread = NULL ;
}
2011-01-04 15:19:45 -05:00
// save settings
processSettings ( false ) ;
}
void ChannelFeed : : processSettings ( bool load )
{
Settings - > beginGroup ( QString ( " ChannelFeed " ) ) ;
if ( load ) {
// load settings
2011-02-03 19:53:44 -05:00
// state of splitter
splitter - > restoreState ( Settings - > value ( " Splitter " ) . toByteArray ( ) ) ;
2011-01-04 15:19:45 -05:00
} else {
// save settings
2011-02-03 19:53:44 -05:00
// state of splitter
Settings - > setValue ( " Splitter " , splitter - > saveState ( ) ) ;
2011-01-04 15:19:45 -05:00
}
treeWidget - > processSettings ( Settings , load ) ;
Settings - > endGroup ( ) ;
}
2011-08-12 10:06:29 -04:00
void ChannelFeed : : channelListCustomPopupMenu ( QPoint /*point*/ )
2010-01-19 19:29:40 -05:00
{
2010-10-06 20:17:42 -04:00
ChannelInfo ci ;
if ( ! rsChannels - > getChannelInfo ( mChannelId , ci ) ) {
return ;
}
QMenu contextMnu ( this ) ;
QAction * postchannelAct = new QAction ( QIcon ( " :/images/mail_reply.png " ) , tr ( " Post to Channel " ) , & contextMnu ) ;
connect ( postchannelAct , SIGNAL ( triggered ( ) ) , this , SLOT ( createMsg ( ) ) ) ;
QAction * subscribechannelAct = new QAction ( QIcon ( " :/images/edit_add24.png " ) , tr ( " Subscribe to Channel " ) , & contextMnu ) ;
connect ( subscribechannelAct , SIGNAL ( triggered ( ) ) , this , SLOT ( subscribeChannel ( ) ) ) ;
QAction * unsubscribechannelAct = new QAction ( QIcon ( " :/images/cancel.png " ) , tr ( " Unsubscribe to Channel " ) , & contextMnu ) ;
connect ( unsubscribechannelAct , SIGNAL ( triggered ( ) ) , this , SLOT ( unsubscribeChannel ( ) ) ) ;
QAction * channeldetailsAct = new QAction ( QIcon ( " :/images/info16.png " ) , tr ( " Show Channel Details " ) , & contextMnu ) ;
connect ( channeldetailsAct , SIGNAL ( triggered ( ) ) , this , SLOT ( showChannelDetails ( ) ) ) ;
2011-02-06 15:41:15 -05:00
QAction * restoreKeysAct = new QAction ( QIcon ( " :/images/settings16.png " ) , tr ( " Restore Publish Rights for Channel " ) , & contextMnu ) ;
connect ( restoreKeysAct , SIGNAL ( triggered ( ) ) , this , SLOT ( restoreChannelKeys ( ) ) ) ;
2010-10-06 20:17:42 -04:00
QAction * editChannelDetailAct = new QAction ( QIcon ( " :/images/edit_16.png " ) , tr ( " Edit Channel Details " ) , & contextMnu ) ;
connect ( editChannelDetailAct , SIGNAL ( triggered ( ) ) , this , SLOT ( editChannelDetail ( ) ) ) ;
QAction * shareKeyAct = new QAction ( QIcon ( " :/images/gpgp_key_generate.png " ) , tr ( " Share Channel " ) , & contextMnu ) ;
connect ( shareKeyAct , SIGNAL ( triggered ( ) ) , this , SLOT ( shareKey ( ) ) ) ;
if ( ( ci . channelFlags & RS_DISTRIB_PUBLISH ) & & ( ci . channelFlags & RS_DISTRIB_ADMIN ) ) {
2010-06-26 18:28:04 -04:00
contextMnu . addAction ( postchannelAct ) ;
contextMnu . addSeparator ( ) ;
contextMnu . addAction ( editChannelDetailAct ) ;
contextMnu . addAction ( shareKeyAct ) ;
2010-09-24 11:20:35 -04:00
contextMnu . addAction ( channeldetailsAct ) ;
2010-10-06 20:17:42 -04:00
}
2010-11-06 20:02:51 -04:00
else if ( ( ci . channelFlags & RS_DISTRIB_PUBLISH ) & & ( ci . channelFlags & RS_DISTRIB_SUBSCRIBED ) ) {
2010-02-28 16:59:39 -05:00
contextMnu . addAction ( postchannelAct ) ;
contextMnu . addSeparator ( ) ;
contextMnu . addAction ( channeldetailsAct ) ;
2010-06-19 12:38:13 -04:00
contextMnu . addAction ( shareKeyAct ) ;
2011-02-06 15:41:15 -05:00
contextMnu . addAction ( restoreKeysAct ) ;
2010-10-06 20:17:42 -04:00
} else if ( ci . channelFlags & RS_DISTRIB_SUBSCRIBED ) {
2010-02-28 16:59:39 -05:00
contextMnu . addAction ( unsubscribechannelAct ) ;
contextMnu . addSeparator ( ) ;
2010-05-19 18:43:07 -04:00
contextMnu . addAction ( channeldetailsAct ) ;
2011-02-06 15:41:15 -05:00
contextMnu . addAction ( restoreKeysAct ) ;
2010-10-06 20:17:42 -04:00
} else {
2010-02-28 16:59:39 -05:00
contextMnu . addAction ( subscribechannelAct ) ;
contextMnu . addSeparator ( ) ;
contextMnu . addAction ( channeldetailsAct ) ;
2011-02-06 15:41:15 -05:00
contextMnu . addAction ( restoreKeysAct ) ;
2010-10-06 20:17:42 -04:00
}
2010-01-19 19:29:40 -05:00
2011-04-19 15:42:44 -04:00
QAction * action = contextMnu . addAction ( QIcon ( " :/images/copyrslink.png " ) , tr ( " Copy RetroShare Link " ) , this , SLOT ( copyChannelLink ( ) ) ) ;
action - > setEnabled ( ! mChannelId . empty ( ) ) ;
2012-04-21 09:00:17 -04:00
# ifdef CHAN_DEBUG
contextMnu . addSeparator ( ) ;
action = contextMnu . addAction ( " Generate mass data " , this , SLOT ( generateMassData ( ) ) ) ;
action - > setEnabled ( ! mChannelId . empty ( ) & & ( ci . channelFlags & RS_DISTRIB_PUBLISH ) ) ;
# endif
2010-10-06 20:17:42 -04:00
contextMnu . exec ( QCursor : : pos ( ) ) ;
2010-01-19 19:29:40 -05:00
}
2008-06-20 08:43:23 -04:00
2008-06-24 00:36:45 -04:00
void ChannelFeed : : createChannel ( )
{
2010-05-04 20:05:36 -04:00
CreateChannel cf ( this ) ;
cf . exec ( ) ;
2008-06-24 00:36:45 -04:00
}
/*************************************************************************************/
/*************************************************************************************/
/*************************************************************************************/
2008-06-20 08:43:23 -04:00
2011-08-12 10:06:29 -04:00
void ChannelFeed : : deleteFeedItem ( QWidget */ * item */ , uint32_t /*type*/ )
2008-06-20 08:43:23 -04:00
{
}
2011-08-12 10:06:29 -04:00
void ChannelFeed : : openChat ( std : : string /*peerId*/ )
2008-06-20 08:43:23 -04:00
{
}
2010-06-04 18:52:01 -04:00
void ChannelFeed : : editChannelDetail ( ) {
EditChanDetails editUi ( this , 0 , mChannelId ) ;
editUi . exec ( ) ;
}
2010-06-19 12:38:13 -04:00
void ChannelFeed : : shareKey ( )
{
2011-08-29 17:20:48 -04:00
ShareKey shareUi ( this , 0 , mChannelId , CHANNEL_KEY_SHARE ) ;
2010-06-19 12:38:13 -04:00
shareUi . exec ( ) ;
}
2011-04-19 15:42:44 -04:00
void ChannelFeed : : copyChannelLink ( )
{
if ( mChannelId . empty ( ) ) {
return ;
}
ChannelInfo ci ;
if ( rsChannels - > getChannelInfo ( mChannelId , ci ) ) {
2011-05-04 06:22:49 -04:00
RetroShareLink link ;
2011-05-08 19:11:27 -04:00
if ( link . createChannel ( ci . channelId , " " ) ) {
2011-09-11 18:07:24 -04:00
QList < RetroShareLink > urls ;
2011-04-19 15:42:44 -04:00
urls . push_back ( link ) ;
RSLinkClipboard : : copyLinks ( urls ) ;
}
}
}
2010-01-18 16:02:16 -05:00
void ChannelFeed : : createMsg ( )
{
2010-10-06 20:17:42 -04:00
if ( mChannelId . empty ( ) ) {
2010-01-18 16:02:16 -05:00
return ;
2010-10-06 20:17:42 -04:00
}
2010-01-18 16:02:16 -05:00
2010-10-06 20:17:42 -04:00
CreateChannelMsg * msgDialog = new CreateChannelMsg ( mChannelId ) ;
msgDialog - > show ( ) ;
2010-05-04 20:05:36 -04:00
2010-10-06 20:17:42 -04:00
/* window will destroy itself! */
2010-01-18 16:02:16 -05:00
}
2010-10-06 20:17:42 -04:00
void ChannelFeed : : restoreChannelKeys ( )
2008-06-24 00:36:45 -04:00
{
2010-10-06 20:17:42 -04:00
rsChannels - > channelRestoreKeys ( mChannelId ) ;
2008-06-24 00:36:45 -04:00
}
2010-12-26 19:15:25 -05:00
void ChannelFeed : : selectChannel ( const QString & id )
2010-10-06 20:17:42 -04:00
{
2010-12-26 19:15:25 -05:00
mChannelId = id . toStdString ( ) ;
2010-10-06 20:17:42 -04:00
2011-04-07 18:10:10 -04:00
bool autoDl = false ;
rsChannels - > channelGetAutoDl ( mChannelId , autoDl ) ;
2011-04-19 15:42:44 -04:00
setAutoDownloadButton ( autoDl ) ;
2011-04-07 18:10:10 -04:00
2010-10-06 20:17:42 -04:00
updateChannelMsgs ( ) ;
2009-09-12 06:55:40 -04:00
}
2010-07-23 07:55:21 -04:00
void ChannelFeed : : updateDisplay ( )
2008-06-24 00:36:45 -04:00
{
2010-10-06 20:17:42 -04:00
if ( ! rsChannels ) {
return ;
}
2008-06-24 00:36:45 -04:00
2010-10-06 20:17:42 -04:00
std : : list < std : : string > chanIds ;
std : : list < std : : string > : : iterator it ;
2008-06-24 00:36:45 -04:00
2010-10-06 20:17:42 -04:00
if ( rsChannels - > channelsChanged ( chanIds ) ) {
/* update channel list */
updateChannelList ( ) ;
it = std : : find ( chanIds . begin ( ) , chanIds . end ( ) , mChannelId ) ;
if ( it ! = chanIds . end ( ) ) {
updateChannelMsgs ( ) ;
}
}
2008-06-24 00:36:45 -04:00
}
2010-12-26 19:15:25 -05:00
static void channelInfoToGroupItemInfo ( const ChannelInfo & channelInfo , GroupItemInfo & groupItemInfo )
{
groupItemInfo . id = QString : : fromStdString ( channelInfo . channelId ) ;
groupItemInfo . name = QString : : fromStdWString ( channelInfo . channelName ) ;
groupItemInfo . description = QString : : fromStdWString ( channelInfo . channelDesc ) ;
groupItemInfo . popularity = channelInfo . pop ;
groupItemInfo . lastpost = QDateTime : : fromTime_t ( channelInfo . lastPost ) ;
QPixmap chanImage ;
if ( channelInfo . pngImageLen ) {
chanImage . loadFromData ( channelInfo . pngChanImage , channelInfo . pngImageLen , " PNG " ) ;
} else {
chanImage = QPixmap ( CHAN_DEFAULT_IMAGE ) ;
2010-10-25 16:46:26 -04:00
}
2010-10-17 10:43:49 -04:00
2010-12-26 19:15:25 -05:00
groupItemInfo . icon = QIcon ( chanImage ) ;
2010-10-17 10:43:49 -04:00
}
2010-10-06 20:17:42 -04:00
void ChannelFeed : : updateChannelList ( )
2008-06-24 00:36:45 -04:00
{
2010-12-26 19:15:25 -05:00
if ( ! rsChannels ) {
return ;
}
2010-10-25 16:46:26 -04:00
2010-12-26 19:15:25 -05:00
std : : list < ChannelInfo > channelList ;
std : : list < ChannelInfo > : : iterator it ;
rsChannels - > getChannelList ( channelList ) ;
std : : list < std : : string > keysAvailable ;
std : : list < std : : string > : : iterator keyIt ;
rsChannels - > getPubKeysAvailableGrpIds ( keysAvailable ) ;
/* get the ids for our lists */
QList < GroupItemInfo > adminList ;
QList < GroupItemInfo > subList ;
QList < GroupItemInfo > popList ;
QList < GroupItemInfo > otherList ;
std : : multimap < uint32_t , GroupItemInfo > popMap ;
for ( it = channelList . begin ( ) ; it ! = channelList . end ( ) ; it + + ) {
/* sort it into Publish (Own), Subscribed, Popular and Other */
uint32_t flags = it - > channelFlags ;
GroupItemInfo groupItemInfo ;
channelInfoToGroupItemInfo ( * it , groupItemInfo ) ;
if ( ( flags & RS_DISTRIB_ADMIN ) & & ( flags & RS_DISTRIB_PUBLISH ) & & ( flags & RS_DISTRIB_SUBSCRIBED ) ) {
adminList . push_back ( groupItemInfo ) ;
} else {
for ( keyIt = keysAvailable . begin ( ) ; keyIt ! = keysAvailable . end ( ) ; keyIt + + ) {
if ( it - > channelId = = * keyIt ) {
/* Found Key, set title text to bold and colored blue */
groupItemInfo . privatekey = true ;
break ;
}
}
if ( ( flags & RS_DISTRIB_SUBSCRIBED ) | | ( ( flags & RS_DISTRIB_SUBSCRIBED ) & & ( flags & RS_DISTRIB_PUBLISH ) ) ) {
subList . push_back ( groupItemInfo ) ;
} else {
/* rate the others by popularity */
popMap . insert ( std : : make_pair ( it - > pop , groupItemInfo ) ) ;
}
2010-10-25 16:46:26 -04:00
}
}
2010-12-26 19:15:25 -05:00
/* iterate backwards through popMap - take the top 5 or 10% of list */
uint32_t popCount = 5 ;
if ( popCount < popMap . size ( ) / 10 ) {
popCount = popMap . size ( ) / 10 ;
}
2010-10-25 16:46:26 -04:00
2010-12-26 19:15:25 -05:00
uint32_t i = 0 ;
std : : multimap < uint32_t , GroupItemInfo > : : reverse_iterator rit ;
for ( rit = popMap . rbegin ( ) ; rit ! = popMap . rend ( ) ; rit + + ) {
if ( i < popCount ) {
popList . push_back ( rit - > second ) ;
i + + ;
} else {
otherList . push_back ( rit - > second ) ;
}
}
2010-11-06 20:02:51 -04:00
2010-12-26 19:15:25 -05:00
/* now we have our lists ---> update entries */
2010-11-06 20:02:51 -04:00
2010-12-26 19:15:25 -05:00
treeWidget - > fillGroupItems ( ownChannels , adminList ) ;
treeWidget - > fillGroupItems ( subcribedChannels , subList ) ;
treeWidget - > fillGroupItems ( popularChannels , popList ) ;
treeWidget - > fillGroupItems ( otherChannels , otherList ) ;
2010-11-06 20:02:51 -04:00
2010-12-26 19:15:25 -05:00
updateMessageSummaryList ( " " ) ;
2010-11-06 20:02:51 -04:00
}
2008-06-24 00:36:45 -04:00
2011-08-12 10:06:29 -04:00
void ChannelFeed : : channelMsgReadSatusChanged ( const QString & channelId , const QString & /*msgId*/ , int /*status*/ )
2008-06-24 00:36:45 -04:00
{
2010-10-06 20:17:42 -04:00
updateMessageSummaryList ( channelId . toStdString ( ) ) ;
}
2010-01-29 20:13:03 -05:00
2010-10-06 20:17:42 -04:00
void ChannelFeed : : updateMessageSummaryList ( const std : : string & channelId )
{
2010-12-26 19:15:25 -05:00
QTreeWidgetItem * items [ 2 ] = { ownChannels , subcribedChannels } ;
for ( int item = 0 ; item < 2 ; item + + ) {
int child ;
int childCount = items [ item ] - > childCount ( ) ;
for ( child = 0 ; child < childCount ; child + + ) {
QTreeWidgetItem * childItem = items [ item ] - > child ( child ) ;
std : : string childId = treeWidget - > itemId ( childItem ) . toStdString ( ) ;
if ( childId . empty ( ) ) {
continue ;
}
if ( channelId . empty ( ) | | childId = = channelId ) {
/* Calculate unread messages */
unsigned int newMessageCount = 0 ;
unsigned int unreadMessageCount = 0 ;
2011-04-19 15:42:44 -04:00
rsChannels - > getMessageCount ( childId , newMessageCount , unreadMessageCount ) ;
2010-12-26 19:15:25 -05:00
treeWidget - > setUnreadCount ( childItem , unreadMessageCount ) ;
if ( channelId . empty ( ) = = false ) {
/* Calculate only this channel */
break ;
}
}
}
}
2008-06-24 00:36:45 -04:00
}
2010-10-05 16:39:14 -04:00
static bool sortChannelMsgSummary ( const ChannelMsgSummary & msg1 , const ChannelMsgSummary & msg2 )
{
2010-10-07 15:31:37 -04:00
return ( msg1 . ts > msg2 . ts ) ;
2010-10-05 16:39:14 -04:00
}
2008-06-24 00:36:45 -04:00
void ChannelFeed : : updateChannelMsgs ( )
{
2012-04-21 09:00:17 -04:00
if ( fillThread ) {
# ifdef CHAN_DEBUG
std : : cerr < < " ChannelFeed::updateChannelMsgs() stop current fill thread " < < std : : endl ;
# endif
// stop current fill thread
fillThread - > stop ( ) ;
delete ( fillThread ) ;
fillThread = NULL ;
progressLabel - > hide ( ) ;
progressBar - > hide ( ) ;
}
2010-10-06 20:17:42 -04:00
if ( ! rsChannels ) {
return ;
}
2010-10-07 15:31:37 -04:00
/* replace all the messages with new ones */
2012-04-21 09:00:17 -04:00
QList < ChanMsgItem * > : : iterator mit ;
2010-10-07 15:31:37 -04:00
for ( mit = mChanMsgItems . begin ( ) ; mit ! = mChanMsgItems . end ( ) ; mit + + ) {
delete ( * mit ) ;
}
mChanMsgItems . clear ( ) ;
2010-10-06 20:17:42 -04:00
ChannelInfo ci ;
if ( ! rsChannels - > getChannelInfo ( mChannelId , ci ) ) {
postButton - > setEnabled ( false ) ;
subscribeButton - > setEnabled ( false ) ;
unsubscribeButton - > setEnabled ( false ) ;
2012-01-13 11:26:24 -05:00
subscribeButton - > hide ( ) ;
unsubscribeButton - > hide ( ) ;
actionsetAllAsRead - > setEnabled ( false ) ;
actionEnable_Auto_Download - > setEnabled ( false ) ;
2010-10-06 20:17:42 -04:00
nameLabel - > setText ( tr ( " No Channel Selected " ) ) ;
iconLabel - > setPixmap ( QPixmap ( " :/images/channels.png " ) ) ;
iconLabel - > setEnabled ( false ) ;
return ;
}
if ( ci . pngImageLen ! = 0 ) {
QPixmap chanImage ;
chanImage . loadFromData ( ci . pngChanImage , ci . pngImageLen , " PNG " ) ;
iconLabel - > setPixmap ( chanImage ) ;
iconLabel - > setStyleSheet ( " QLabel{border: 3px solid white;} " ) ;
} else {
QPixmap defaulImage ( CHAN_DEFAULT_IMAGE ) ;
iconLabel - > setPixmap ( defaulImage ) ;
iconLabel - > setStyleSheet ( " QLabel{border: 2px solid white;border-radius: 10px;} " ) ;
}
iconLabel - > setEnabled ( true ) ;
/* set textcolor for Channel name */
2012-04-21 09:00:17 -04:00
QString channelStr ( " <span style= \" font-size:22pt; font-weight:500;color:#4F4F4F; \" >%1</span> " ) ;
2010-10-06 20:17:42 -04:00
/* set Channel name */
QString cname = QString : : fromStdWString ( ci . channelName ) ;
nameLabel - > setText ( channelStr . arg ( cname ) ) ;
/* do buttons */
if ( ci . channelFlags & RS_DISTRIB_SUBSCRIBED ) {
subscribeButton - > setEnabled ( false ) ;
2012-01-13 11:26:24 -05:00
subscribeButton - > hide ( ) ;
unsubscribeButton - > show ( ) ;
2010-10-06 20:17:42 -04:00
unsubscribeButton - > setEnabled ( true ) ;
2012-01-13 11:26:24 -05:00
actionsetAllAsRead - > setEnabled ( true ) ;
2010-10-06 20:17:42 -04:00
} else {
subscribeButton - > setEnabled ( true ) ;
2012-01-13 11:26:24 -05:00
subscribeButton - > show ( ) ;
2010-10-06 20:17:42 -04:00
unsubscribeButton - > setEnabled ( false ) ;
2012-01-13 11:26:24 -05:00
unsubscribeButton - > hide ( ) ;
actionsetAllAsRead - > setEnabled ( false ) ;
2010-10-06 20:17:42 -04:00
}
if ( ci . channelFlags & RS_DISTRIB_PUBLISH ) {
postButton - > setEnabled ( true ) ;
} else {
postButton - > setEnabled ( false ) ;
}
2012-04-03 14:56:29 -04:00
if ( ! ( ci . channelFlags & RS_DISTRIB_ADMIN ) & &
( ci . channelFlags & RS_DISTRIB_SUBSCRIBED ) ) {
actionEnable_Auto_Download - > setEnabled ( true ) ;
} else {
actionEnable_Auto_Download - > setEnabled ( false ) ;
}
2011-04-07 18:10:10 -04:00
2012-04-24 11:34:22 -04:00
# ifdef USE_THREAD
2012-04-21 09:00:17 -04:00
progressLabel - > show ( ) ;
progressBar - > reset ( ) ;
progressBar - > show ( ) ;
2010-10-06 20:17:42 -04:00
2012-04-21 09:00:17 -04:00
// create fill thread
fillThread = new ChannelFillThread ( this , mChannelId ) ;
// connect thread
connect ( fillThread , SIGNAL ( finished ( ) ) , this , SLOT ( fillThreadFinished ( ) ) , Qt : : BlockingQueuedConnection ) ;
2012-04-24 11:34:22 -04:00
connect ( fillThread , SIGNAL ( addMsg ( QString , QString , int , int ) ) , this , SLOT ( fillThreadAddMsg ( QString , QString , int , int ) ) , Qt : : BlockingQueuedConnection ) ;
2012-04-21 09:00:17 -04:00
2012-04-24 11:34:22 -04:00
# ifdef CHAN_DEBUG
2012-04-21 09:00:17 -04:00
std : : cerr < < " ChannelFeed::updateChannelMsgs() Start fill thread " < < std : : endl ;
# endif
// start thread
fillThread - > start ( ) ;
2012-04-24 11:34:22 -04:00
# else
std : : list < ChannelMsgSummary > msgs ;
std : : list < ChannelMsgSummary > : : iterator it ;
rsChannels - > getChannelMsgList ( mChannelId , msgs ) ;
msgs . sort ( sortChannelMsgSummary ) ;
for ( it = msgs . begin ( ) ; it ! = msgs . end ( ) ; it + + ) {
ChanMsgItem * cmi = new ChanMsgItem ( this , 0 , mChannelId , it - > msgId , true ) ;
mChanMsgItems . push_back ( cmi ) ;
verticalLayout_2 - > addWidget ( cmi ) ;
}
# endif
2012-04-21 09:00:17 -04:00
}
2010-10-06 20:17:42 -04:00
2012-04-21 09:00:17 -04:00
void ChannelFeed : : fillThreadFinished ( )
{
2012-04-24 11:34:22 -04:00
# ifdef CHAN_DEBUG
std : : cerr < < " ChannelFeed::fillThreadFinished() " < < std : : endl ;
2012-04-21 09:00:17 -04:00
# endif
// thread has finished
ChannelFillThread * thread = dynamic_cast < ChannelFillThread * > ( sender ( ) ) ;
if ( thread ) {
if ( thread = = fillThread ) {
// current thread has finished, hide progressbar and release thread
progressBar - > hide ( ) ;
progressLabel - > hide ( ) ;
fillThread = NULL ;
}
2012-04-24 11:34:22 -04:00
# ifdef CHAN_DEBUG
2012-04-21 09:00:17 -04:00
if ( thread - > wasStopped ( ) ) {
// thread was stopped
2012-04-24 11:34:22 -04:00
std : : cerr < < " ChannelFeed::fillThreadFinished() Thread was stopped " < < std : : endl ;
2012-04-21 09:00:17 -04:00
}
2012-04-24 11:34:22 -04:00
# endif
2012-04-21 09:00:17 -04:00
2012-04-24 11:34:22 -04:00
# ifdef CHAN_DEBUG
std : : cerr < < " ChannelFeed::fillThreadFinished() Delete thread " < < std : : endl ;
2012-04-21 09:00:17 -04:00
# endif
thread - > deleteLater ( ) ;
thread = NULL ;
}
2010-11-06 20:02:51 -04:00
2012-04-24 11:34:22 -04:00
# ifdef CHAN_DEBUG
std : : cerr < < " ChannelFeed::fillThreadFinished done() " < < std : : endl ;
2012-04-21 09:00:17 -04:00
# endif
}
2010-10-06 20:17:42 -04:00
2012-04-24 11:34:22 -04:00
void ChannelFeed : : fillThreadAddMsg ( const QString & channelId , const QString & channelMsgId , int current , int count )
2012-04-21 09:00:17 -04:00
{
if ( sender ( ) = = fillThread ) {
2012-04-24 11:34:22 -04:00
// show fill progress
if ( count ) {
progressBar - > setValue ( current * progressBar - > maximum ( ) / count ) ;
}
2012-04-21 09:00:17 -04:00
ChanMsgItem * cmi = new ChanMsgItem ( this , 0 , channelId . toStdString ( ) , channelMsgId . toStdString ( ) , true ) ;
2010-10-06 20:17:42 -04:00
mChanMsgItems . push_back ( cmi ) ;
2012-04-24 11:34:22 -04:00
verticalLayout - > addWidget ( cmi ) ;
2010-10-06 20:17:42 -04:00
}
2008-06-24 00:36:45 -04:00
}
2008-06-20 08:43:23 -04:00
2008-07-04 10:41:24 -04:00
void ChannelFeed : : unsubscribeChannel ( )
{
2008-07-09 05:53:47 -04:00
# ifdef CHAN_DEBUG
2010-10-06 20:17:42 -04:00
std : : cerr < < " ChannelFeed::unsubscribeChannel() " ;
std : : cerr < < std : : endl ;
2008-07-04 10:41:24 -04:00
# endif
2010-10-06 20:17:42 -04:00
if ( rsChannels ) {
2011-04-07 18:10:10 -04:00
rsChannels - > channelSubscribe ( mChannelId , false , false ) ;
2010-10-06 20:17:42 -04:00
}
updateChannelMsgs ( ) ;
2008-07-04 10:41:24 -04:00
}
void ChannelFeed : : subscribeChannel ( )
{
2008-07-09 05:53:47 -04:00
# ifdef CHAN_DEBUG
2010-10-06 20:17:42 -04:00
std : : cerr < < " ChannelFeed::subscribeChannel() " ;
std : : cerr < < std : : endl ;
2008-07-04 10:41:24 -04:00
# endif
2010-10-06 20:17:42 -04:00
if ( rsChannels ) {
2012-02-10 15:59:57 -05:00
rsChannels - > channelSubscribe ( mChannelId , true , false ) ;
2010-10-06 20:17:42 -04:00
}
2008-07-04 10:41:24 -04:00
2010-10-06 20:17:42 -04:00
updateChannelMsgs ( ) ;
2009-09-12 06:55:40 -04:00
}
2010-01-04 20:49:58 -05:00
2010-01-19 19:29:40 -05:00
void ChannelFeed : : showChannelDetails ( )
{
2010-10-06 20:17:42 -04:00
if ( mChannelId . empty ( ) ) {
2010-01-19 19:29:40 -05:00
return ;
2010-10-06 20:17:42 -04:00
}
if ( ! rsChannels ) {
return ;
}
ChannelDetails channelui ( this ) ;
channelui . showDetails ( mChannelId ) ;
channelui . exec ( ) ;
}
void ChannelFeed : : setAllAsReadClicked ( )
{
if ( mChannelId . empty ( ) ) {
return ;
}
2010-01-19 19:29:40 -05:00
2010-10-06 20:17:42 -04:00
if ( ! rsChannels ) {
return ;
}
2010-01-19 19:29:40 -05:00
2010-10-06 20:17:42 -04:00
ChannelInfo ci ;
if ( rsChannels - > getChannelInfo ( mChannelId , ci ) = = false ) {
return ;
}
2010-01-19 19:29:40 -05:00
2010-10-06 20:17:42 -04:00
if ( ci . channelFlags & RS_DISTRIB_SUBSCRIBED ) {
std : : list < ChannelMsgSummary > msgs ;
std : : list < ChannelMsgSummary > : : iterator it ;
rsChannels - > getChannelMsgList ( mChannelId , msgs ) ;
for ( it = msgs . begin ( ) ; it ! = msgs . end ( ) ; it + + ) {
rsChannels - > setMessageStatus ( mChannelId , it - > msgId , CHANNEL_MSG_STATUS_READ , CHANNEL_MSG_STATUS_READ | CHANNEL_MSG_STATUS_UNREAD_BY_USER ) ;
}
}
2010-01-19 19:29:40 -05:00
}
2011-04-07 18:10:10 -04:00
void ChannelFeed : : toggleAutoDownload ( ) {
if ( mChannelId . empty ( ) )
return ;
bool autoDl = true ;
if ( rsChannels - > channelGetAutoDl ( mChannelId , autoDl ) ) {
// if auto dl is set true, then set false
if ( autoDl ) {
rsChannels - > channelSetAutoDl ( mChannelId , false ) ;
} else {
rsChannels - > channelSetAutoDl ( mChannelId , true ) ;
}
2011-04-19 15:42:44 -04:00
setAutoDownloadButton ( ! autoDl ) ;
2011-04-07 18:10:10 -04:00
}
else {
std : : cerr < < " Auto Download failed to set "
< < std : : endl ;
}
}
2011-04-19 15:42:44 -04:00
bool ChannelFeed : : navigate ( const std : : string & channelId , const std : : string & msgId )
{
if ( channelId . empty ( ) ) {
return false ;
}
if ( treeWidget - > activateId ( QString : : fromStdString ( channelId ) , msgId . empty ( ) ) = = NULL ) {
return false ;
}
/* Messages are filled in selectChannel */
if ( mChannelId ! = channelId ) {
return false ;
}
if ( msgId . empty ( ) ) {
return true ;
}
/* Search exisiting item */
2012-04-21 09:00:17 -04:00
QList < ChanMsgItem * > : : iterator mit ;
2011-04-19 15:42:44 -04:00
for ( mit = mChanMsgItems . begin ( ) ; mit ! = mChanMsgItems . end ( ) ; mit + + ) {
ChanMsgItem * item = * mit ;
if ( item - > msgId ( ) = = msgId ) {
// the next two lines are necessary to calculate the layout of the widgets in the scroll area (maybe there is a better solution)
item - > show ( ) ;
QCoreApplication : : processEvents ( ) ;
scrollArea - > ensureWidgetVisible ( item , 0 , 0 ) ;
return true ;
}
}
return false ;
}
void ChannelFeed : : setAutoDownloadButton ( bool autoDl )
{
if ( autoDl ) {
2012-01-13 11:26:24 -05:00
actionEnable_Auto_Download - > setText ( tr ( " Disable Auto-Download " ) ) ;
2011-04-19 15:42:44 -04:00
} else {
2012-01-13 11:26:24 -05:00
actionEnable_Auto_Download - > setText ( tr ( " Enable Auto-Download " ) ) ;
2011-04-19 15:42:44 -04:00
}
}
2012-04-21 09:00:17 -04:00
void ChannelFeed : : generateMassData ( )
{
# ifdef CHAN_DEBUG
if ( mChannelId . empty ( ) ) {
return ;
}
if ( QMessageBox : : question ( this , " Generate mass data " , " Do you really want to generate mass data ? " , QMessageBox : : Yes | QMessageBox : : No , QMessageBox : : No ) = = QMessageBox : : No ) {
return ;
}
for ( int thread = 1 ; thread < 1000 ; thread + + ) {
ChannelMsgInfo msgInfo ;
msgInfo . channelId = mChannelId ;
msgInfo . subject = QString ( " Test %1 " ) . arg ( thread , 3 , 10 , QChar ( ' 0 ' ) ) . toStdWString ( ) ;
msgInfo . msg = QString ( " That is only a test " ) . toStdWString ( ) ;
if ( rsChannels - > ChannelMessageSend ( msgInfo ) = = false ) {
return ;
}
}
# endif
}
// ForumsFillThread
ChannelFillThread : : ChannelFillThread ( ChannelFeed * parent , const std : : string & channelId )
: QThread ( parent )
{
stopped = false ;
this - > channelId = channelId ;
}
ChannelFillThread : : ~ ChannelFillThread ( )
{
# ifdef CHAN_DEBUG
std : : cerr < < " ChannelFillThread::~ChannelFillThread " < < std : : endl ;
# endif
}
void ChannelFillThread : : stop ( )
{
disconnect ( ) ;
stopped = true ;
QApplication : : processEvents ( ) ;
wait ( ) ;
}
void ChannelFillThread : : run ( )
{
# ifdef CHAN_DEBUG
std : : cerr < < " ChannelFillThread::run() " < < std : : endl ;
# endif
std : : list < ChannelMsgSummary > msgs ;
std : : list < ChannelMsgSummary > : : iterator it ;
rsChannels - > getChannelMsgList ( channelId , msgs ) ;
msgs . sort ( sortChannelMsgSummary ) ;
int count = msgs . size ( ) ;
int pos = 0 ;
for ( it = msgs . begin ( ) ; it ! = msgs . end ( ) ; it + + ) {
if ( stopped ) {
break ;
}
2012-04-24 11:34:22 -04:00
emit addMsg ( QString : : fromStdString ( channelId ) , QString : : fromStdString ( it - > msgId ) , + + pos , count ) ;
2012-04-21 09:00:17 -04:00
}
# ifdef CHAN_DEBUG
std : : cerr < < " ChannelFillThread::run() stopped: " < < ( wasStopped ( ) ? " yes " : " no " ) < < std : : endl ;
# endif
}