2008-02-03 01:31:11 -05: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-08-16 15:29:20 -04:00
* Foundation , Inc . , 51 Franklin Street , Fifth Floor ,
2008-02-03 01:31:11 -05:00
* Boston , MA 02110 - 1301 , USA .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2010-07-23 14:52:58 -04:00
# include <QMenu>
# include <QTimer>
# include <QMessageBox>
2008-02-03 01:31:11 -05:00
# include <QDateTime>
# include <QDesktopServices>
# include "LinksDialog.h"
2010-03-10 16:38:26 -05:00
# include "RetroShareLink.h"
2010-08-06 05:40:23 -04:00
# include <retroshare/rspeers.h>
# include <retroshare/rsrank.h>
# include <retroshare/rsfiles.h>
2008-02-03 01:31:11 -05:00
# include <sstream>
/* Images for context menu icons */
2008-05-13 11:45:09 -04:00
# define IMAGE_EXPORTFRIEND ": / images / exportpeers_16x16.png"
2008-06-15 06:11:13 -04:00
# define IMAGE_GREAT ": / images / filerating5.png"
# define IMAGE_GOOD ": / images / filerating4.png"
# define IMAGE_OK ": / images / filerating3.png"
# define IMAGE_SUX ": / images / filerating2.png"
2009-08-17 16:19:02 -04:00
# define IMAGE_BADLINK ": / images / filerating1.png"
# define IMAGE_NOCOMMENTRATING ": / images / filerating0.png"
# define IMAGE_DOWNLOAD ": / images / download16.png"
2008-07-09 05:53:47 -04:00
/******
* # define LINKS_DEBUG 1
* * * * */
2009-08-17 16:19:02 -04:00
/** Constructor */
LinksDialog : : LinksDialog ( QWidget * parent )
2008-02-03 01:31:11 -05:00
: MainPage ( parent )
2009-08-17 16:19:02 -04:00
{
/* Invoke the Qt Designer generated object setup routine */
ui . setupUi ( this ) ;
connect ( ui . linkTreeWidget , SIGNAL ( customContextMenuRequested ( QPoint ) ) , this , SLOT ( linkTreeWidgetCostumPopupMenu ( QPoint ) ) ) ;
2008-02-03 01:31:11 -05:00
/* link combos */
connect ( ui . rankComboBox , SIGNAL ( currentIndexChanged ( int ) ) , this , SLOT ( changedSortRank ( int ) ) ) ;
connect ( ui . periodComboBox , SIGNAL ( currentIndexChanged ( int ) ) , this , SLOT ( changedSortPeriod ( int ) ) ) ;
connect ( ui . fromComboBox , SIGNAL ( currentIndexChanged ( int ) ) , this , SLOT ( changedSortFrom ( int ) ) ) ;
connect ( ui . topComboBox , SIGNAL ( currentIndexChanged ( int ) ) , this , SLOT ( changedSortTop ( int ) ) ) ;
/* add button */
connect ( ui . addButton , SIGNAL ( clicked ( void ) ) , this , SLOT ( addLinkComment ( void ) ) ) ;
connect ( ui . expandButton , SIGNAL ( clicked ( void ) ) , this , SLOT ( toggleWindows ( void ) ) ) ;
2009-08-16 15:29:20 -04:00
connect ( ui . linkTreeWidget , SIGNAL ( currentItemChanged ( QTreeWidgetItem * , QTreeWidgetItem * ) ) ,
2008-02-03 01:31:11 -05:00
this , SLOT ( changedItem ( QTreeWidgetItem * , QTreeWidgetItem * ) ) ) ;
2009-08-16 15:29:20 -04:00
connect ( ui . linkTreeWidget , SIGNAL ( itemDoubleClicked ( QTreeWidgetItem * , int ) ) ,
2008-02-03 01:31:11 -05:00
this , SLOT ( openLink ( QTreeWidgetItem * , int ) ) ) ;
2008-04-15 12:40:38 -04:00
connect ( ui . anonBox , SIGNAL ( stateChanged ( int ) ) , this , SLOT ( checkAnon ( void ) ) ) ;
2008-02-03 01:31:11 -05:00
2010-02-05 08:44:33 -05:00
connect ( ui . linklabel , SIGNAL ( anchorClicked ( const QUrl & ) ) , SLOT ( anchorClicked ( const QUrl & ) ) ) ;
2008-02-03 01:31:11 -05:00
mStart = 0 ;
/* Set header resize modes and initial section sizes */
QHeaderView * _header = ui . linkTreeWidget - > header ( ) ;
2010-02-05 08:44:33 -05:00
_header - > setResizeMode ( 0 , QHeaderView : : Interactive ) ;
2008-02-03 01:31:11 -05:00
_header - > setResizeMode ( 1 , QHeaderView : : Interactive ) ;
_header - > setResizeMode ( 2 , QHeaderView : : Interactive ) ;
_header - > resizeSection ( 0 , 400 ) ;
2010-02-05 08:44:33 -05:00
_header - > resizeSection ( 1 , 60 ) ;
2008-02-03 01:31:11 -05:00
_header - > resizeSection ( 2 , 150 ) ;
2009-08-16 15:29:20 -04:00
2008-06-15 06:11:13 -04:00
ui . linkTreeWidget - > setSortingEnabled ( true ) ;
2010-02-05 08:44:33 -05:00
ui . linklabel - > setMinimumWidth ( 20 ) ;
2008-02-03 01:31:11 -05:00
2008-03-17 10:03:47 -04:00
/* Set a GUI update timer - much cleaner than
2009-08-16 15:29:20 -04:00
* doing everything through the notify agent
2008-03-17 10:03:47 -04:00
*/
QTimer * timer = new QTimer ( this ) ;
timer - > connect ( timer , SIGNAL ( timeout ( ) ) , this , SLOT ( checkUpdate ( ) ) ) ;
timer - > start ( 1000 ) ;
2008-02-03 01:31:11 -05:00
/* Hide platform specific features */
# ifdef Q_WS_WIN
# endif
2008-03-17 10:03:47 -04:00
}
void LinksDialog : : checkUpdate ( )
{
/* update */
if ( ! rsRanks )
return ;
if ( rsRanks - > updated ( ) )
{
updateLinks ( ) ;
}
return ;
2008-02-03 01:31:11 -05:00
}
void LinksDialog : : linkTreeWidgetCostumPopupMenu ( QPoint point )
{
2009-08-17 16:19:02 -04:00
QMenu contextMnu ( this ) ;
2010-07-15 07:25:34 -04:00
QAction * voteupAct = new QAction ( QIcon ( IMAGE_EXPORTFRIEND ) , tr ( " Share Link Anonymously " ) , & contextMnu ) ;
2008-05-13 11:45:09 -04:00
connect ( voteupAct , SIGNAL ( triggered ( ) ) , this , SLOT ( voteup_anon ( ) ) ) ;
2010-07-15 07:25:34 -04:00
QMenu * voteMenu = new QMenu ( tr ( " Vote on Link " ) , & contextMnu ) ;
2008-05-13 11:45:09 -04:00
voteMenu - > setIcon ( QIcon ( IMAGE_EXPORTFRIEND ) ) ;
2010-07-15 07:25:34 -04:00
QAction * vote_p2 = new QAction ( QIcon ( IMAGE_GREAT ) , " [+2] Great " , & contextMnu ) ;
2008-05-13 11:45:09 -04:00
connect ( vote_p2 , SIGNAL ( triggered ( ) ) , this , SLOT ( voteup_p2 ( ) ) ) ;
voteMenu - > addAction ( vote_p2 ) ;
2010-07-15 07:25:34 -04:00
QAction * vote_p1 = new QAction ( QIcon ( IMAGE_GOOD ) , " [+1] Good " , & contextMnu ) ;
2008-05-13 11:45:09 -04:00
connect ( vote_p1 , SIGNAL ( triggered ( ) ) , this , SLOT ( voteup_p1 ( ) ) ) ;
voteMenu - > addAction ( vote_p1 ) ;
2010-07-15 07:25:34 -04:00
QAction * vote_p0 = new QAction ( QIcon ( IMAGE_OK ) , " [+0] Okay " , & contextMnu ) ;
2008-05-13 11:45:09 -04:00
connect ( vote_p0 , SIGNAL ( triggered ( ) ) , this , SLOT ( voteup_p0 ( ) ) ) ;
voteMenu - > addAction ( vote_p0 ) ;
2010-07-15 07:25:34 -04:00
QAction * vote_m1 = new QAction ( QIcon ( IMAGE_SUX ) , " [-1] Sux " , & contextMnu ) ;
2008-05-13 11:45:09 -04:00
connect ( vote_m1 , SIGNAL ( triggered ( ) ) , this , SLOT ( voteup_m1 ( ) ) ) ;
voteMenu - > addAction ( vote_m1 ) ;
2010-07-15 07:25:34 -04:00
QAction * vote_m2 = new QAction ( QIcon ( IMAGE_BADLINK ) , " [-2] BAD LINK " , & contextMnu ) ;
2008-05-13 11:45:09 -04:00
connect ( vote_m2 , SIGNAL ( triggered ( ) ) , this , SLOT ( voteup_m2 ( ) ) ) ;
voteMenu - > addAction ( vote_m2 ) ;
2008-02-03 01:31:11 -05:00
2010-07-15 07:25:34 -04:00
QAction * downloadAct = new QAction ( QIcon ( IMAGE_DOWNLOAD ) , tr ( " Download " ) , & contextMnu ) ;
2009-08-16 15:29:20 -04:00
connect ( downloadAct , SIGNAL ( triggered ( ) ) , this , SLOT ( downloadSelected ( ) ) ) ;
2008-02-03 01:31:11 -05:00
contextMnu . addAction ( voteupAct ) ;
2009-08-16 15:29:20 -04:00
contextMnu . addSeparator ( ) ;
2008-05-13 11:45:09 -04:00
contextMnu . addMenu ( voteMenu ) ;
2009-08-16 15:29:20 -04:00
contextMnu . addSeparator ( ) ;
contextMnu . addAction ( downloadAct ) ;
2008-05-13 11:45:09 -04:00
2010-05-14 16:55:44 -04:00
contextMnu . exec ( QCursor : : pos ( ) ) ;
2008-02-03 01:31:11 -05:00
}
void LinksDialog : : changedSortRank ( int index )
{
/* update */
if ( ! rsRanks )
return ;
/* translate */
uint32_t type = 0 ;
switch ( index )
{
case 1 :
type = RS_RANK_TIME ;
break ;
case 2 :
type = RS_RANK_SCORE ;
break ;
default :
case 0 :
type = RS_RANK_ALG ;
break ;
}
if ( type )
{
rsRanks - > setSortMethod ( type ) ;
}
updateLinks ( ) ;
}
void LinksDialog : : changedSortPeriod ( int index )
{
/* update */
if ( ! rsRanks )
return ;
/* translate */
uint32_t period = 0 ;
switch ( index )
{
case 1 :
period = 60 * 60 * 24 * 7 ; /* WEEK */
break ;
case 2 :
period = 60 * 60 * 24 ; /* DAY */
break ;
default :
case 0 :
period = 60 * 60 * 24 * 30 ; /* MONTH */
break ;
}
if ( period )
{
rsRanks - > setSortPeriod ( period ) ;
}
updateLinks ( ) ;
}
void LinksDialog : : changedSortFrom ( int index )
{
/* update */
if ( ! rsRanks )
return ;
std : : list < std : : string > peers ;
/* translate */
switch ( index )
{
default :
case 0 :
break ;
case 1 :
peers . push_back ( rsPeers - > getOwnId ( ) ) ;
break ;
}
if ( peers . size ( ) < 1 )
{
rsRanks - > clearPeerFilter ( ) ;
}
else
{
rsRanks - > setPeerFilter ( peers ) ;
}
updateLinks ( ) ;
}
2008-04-15 12:40:38 -04:00
# define ENTRIES_PER_BLOCK 100
2008-02-03 01:31:11 -05:00
void LinksDialog : : changedSortTop ( int index )
{
/* update */
if ( ! rsRanks )
return ;
std : : list < std : : string > peers ;
/* translate */
switch ( index )
{
default :
case 0 :
mStart = 0 ;
break ;
2008-04-15 12:40:38 -04:00
case 1 :
mStart = 1 * ENTRIES_PER_BLOCK ;
break ;
2008-02-03 01:31:11 -05:00
case 2 :
2008-04-15 12:40:38 -04:00
mStart = 2 * ENTRIES_PER_BLOCK ;
2008-02-03 01:31:11 -05:00
break ;
case 3 :
2008-04-15 12:40:38 -04:00
mStart = 3 * ENTRIES_PER_BLOCK ;
2008-02-03 01:31:11 -05:00
break ;
case 4 :
2008-04-15 12:40:38 -04:00
mStart = 4 * ENTRIES_PER_BLOCK ;
2008-02-03 01:31:11 -05:00
break ;
case 5 :
mStart = - 1 ;
break ;
}
updateLinks ( ) ;
}
/* get the list of Links from the RsRanks. */
void LinksDialog : : updateLinks ( )
{
std : : list < std : : string > rids ;
std : : list < std : : string > : : iterator rit ;
std : : list < RsRankComment > : : iterator cit ;
2009-08-16 15:29:20 -04:00
# ifdef LINKS_DEBUG
2008-04-15 12:40:38 -04:00
std : : cerr < < " LinksDialog::updateLinks() " < < std : : endl ;
2008-07-09 05:53:47 -04:00
# endif
2008-04-15 12:40:38 -04:00
2008-02-03 01:31:11 -05:00
/* Work out the number/entries to show */
uint32_t count = rsRanks - > getRankingsCount ( ) ;
uint32_t start ;
2008-04-15 12:40:38 -04:00
uint32_t entries = ENTRIES_PER_BLOCK ;
2008-02-03 01:31:11 -05:00
if ( count < entries )
{
entries = count ;
}
if ( mStart = = - 1 )
{
/* backwards */
start = count - entries ;
}
else
{
start = mStart ;
if ( start + entries > count )
{
start = count - entries ;
}
}
/* get a link to the table */
QTreeWidget * linkWidget = ui . linkTreeWidget ;
QList < QTreeWidgetItem * > items ;
rsRanks - > getRankings ( start , entries , rids ) ;
float maxRank = rsRanks - > getMaxRank ( ) ;
for ( rit = rids . begin ( ) ; rit ! = rids . end ( ) ; rit + + )
{
RsRankDetails detail ;
if ( ! rsRanks - > getRankDetails ( * rit , detail ) )
{
continue ;
}
/* create items */
QTreeWidgetItem * item = new QTreeWidgetItem ( ( QTreeWidget * ) 0 ) ;
2009-08-16 15:29:20 -04:00
/* (0) Title */
2008-02-03 01:31:11 -05:00
{
item - > setText ( 0 , QString : : fromStdWString ( detail . title ) ) ;
2010-05-18 14:09:35 -04:00
item - > setSizeHint ( 0 , QSize ( 20 , 20 ) ) ;
2008-02-03 01:31:11 -05:00
/* Bold and bigger */
2010-02-05 08:44:33 -05:00
/*QFont font = item->font(0);
2008-02-03 01:31:11 -05:00
font . setBold ( true ) ;
font . setPointSize ( font . pointSize ( ) + 2 ) ;
2010-02-05 08:44:33 -05:00
item - > setFont ( 0 , font ) ; */
2008-02-03 01:31:11 -05:00
}
/* (1) Rank */
{
std : : ostringstream out ;
out < < 100 * ( detail . rank / ( maxRank + 0.01 ) ) ;
item - > setText ( 1 , QString : : fromStdString ( out . str ( ) ) ) ;
2010-05-18 14:09:35 -04:00
item - > setSizeHint ( 1 , QSize ( 20 , 20 ) ) ;
2008-02-03 01:31:11 -05:00
/* Bold and bigger */
2010-02-05 08:44:33 -05:00
/*QFont font = item->font(1);
font . setBold ( true ) ;
2008-02-03 01:31:11 -05:00
font . setPointSize ( font . pointSize ( ) + 2 ) ;
2010-02-05 08:44:33 -05:00
item - > setFont ( 1 , font ) ; */
2008-02-03 01:31:11 -05:00
}
2009-08-16 15:29:20 -04:00
/* (2) Link */
2008-02-03 01:31:11 -05:00
{
item - > setText ( 2 , QString : : fromStdWString ( detail . link ) ) ;
2010-05-18 14:09:35 -04:00
item - > setSizeHint ( 2 , QSize ( 20 , 20 ) ) ;
2008-02-03 01:31:11 -05:00
/* Bold and bigger */
2010-02-05 08:44:33 -05:00
/*QFont font = item->font(2);
2008-02-03 01:31:11 -05:00
font . setBold ( true ) ;
font . setPointSize ( font . pointSize ( ) + 2 ) ;
2010-02-05 08:44:33 -05:00
item - > setFont ( 2 , font ) ; */
2008-02-03 01:31:11 -05:00
}
2010-02-05 08:44:33 -05:00
/* (3) Date */
/*{
QDateTime qtime ;
qtime . setTime_t ( it - > lastPost ) ;
QString timestamp = qtime . toString ( " yyyy-MM-dd hh:mm:ss " ) ;
item - > setText ( 3 , timestamp ) ;
} */
2008-02-03 01:31:11 -05:00
2010-02-05 08:44:33 -05:00
2009-08-16 15:29:20 -04:00
/* (4) rid */
2008-02-03 01:31:11 -05:00
item - > setText ( 4 , QString : : fromStdString ( detail . rid ) ) ;
/* add children */
int i = 0 ;
for ( cit = detail . comments . begin ( ) ;
cit ! = detail . comments . end ( ) ; cit + + , i + + )
{
/* create items */
QTreeWidgetItem * child = new QTreeWidgetItem ( ( QTreeWidget * ) 0 ) ;
2008-04-15 12:40:38 -04:00
QString commentText ;
QString peerScore ;
if ( cit - > score > 1 )
{
peerScore = " [+2] " ;
2008-06-15 11:34:56 -04:00
child - > setIcon ( 0 , ( QIcon ( IMAGE_GREAT ) ) ) ;
2009-08-17 16:19:02 -04:00
item - > setIcon ( 0 , ( QIcon ( IMAGE_GREAT ) ) ) ;
2008-04-15 12:40:38 -04:00
//peerScore = "[+2 Great Link] ";
}
else if ( cit - > score = = 1 )
{
peerScore = " [+1] " ;
2008-06-15 11:34:56 -04:00
child - > setIcon ( 0 , ( QIcon ( IMAGE_GOOD ) ) ) ;
2009-08-17 16:19:02 -04:00
item - > setIcon ( 0 , ( QIcon ( IMAGE_GOOD ) ) ) ;
2008-04-15 12:40:38 -04:00
//peerScore = "[+1 Good] ";
}
else if ( cit - > score = = 0 )
{
peerScore = " [+0] " ;
2008-06-15 11:34:56 -04:00
child - > setIcon ( 0 , ( QIcon ( IMAGE_OK ) ) ) ;
2009-08-17 16:19:02 -04:00
item - > setIcon ( 0 , ( QIcon ( IMAGE_OK ) ) ) ;
2008-04-15 12:40:38 -04:00
//peerScore = "[+0 Okay] ";
}
else if ( cit - > score = = - 1 )
{
peerScore = " [-1] " ;
2008-06-15 11:34:56 -04:00
child - > setIcon ( 0 , ( QIcon ( IMAGE_SUX ) ) ) ;
2009-08-17 16:19:02 -04:00
item - > setIcon ( 0 , ( QIcon ( IMAGE_SUX ) ) ) ;
2008-04-15 12:40:38 -04:00
//peerScore = "[-1 Not Worth It] ";
}
else //if (cit->score < -1)
{
peerScore = " [-2 BAD] " ;
2008-06-15 11:34:56 -04:00
child - > setIcon ( 0 , ( QIcon ( IMAGE_BADLINK ) ) ) ;
2009-08-17 16:19:02 -04:00
item - > setIcon ( 0 , ( QIcon ( IMAGE_BADLINK ) ) ) ;
2008-04-15 12:40:38 -04:00
//peerScore = "[-2 BAD Link] ";
}
2009-08-16 15:29:20 -04:00
/* (0) Comment */
2008-02-03 11:43:03 -05:00
if ( cit - > comment ! = L " " )
{
2008-04-15 12:40:38 -04:00
commentText = peerScore + QString : : fromStdWString ( cit - > comment ) ;
2008-02-03 11:43:03 -05:00
}
else
{
2008-04-15 12:40:38 -04:00
commentText = peerScore + " No Comment " ;
2008-02-03 11:43:03 -05:00
}
2008-04-15 12:40:38 -04:00
child - > setText ( 0 , commentText ) ;
2008-02-03 01:31:11 -05:00
/* (2) Peer / Date */
{
QDateTime qtime ;
qtime . setTime_t ( cit - > timestamp ) ;
QString timestamp = qtime . toString ( " yyyy-MM-dd hh:mm:ss " ) ;
QString peerLabel = QString : : fromStdString ( rsPeers - > getPeerName ( cit - > id ) ) ;
2008-03-31 14:37:50 -04:00
if ( peerLabel = = " " )
{
peerLabel = " < " ;
peerLabel + = QString : : fromStdString ( cit - > id ) ;
2008-04-15 12:40:38 -04:00
peerLabel + = " > " ;
2008-03-31 14:37:50 -04:00
}
2008-04-15 12:40:38 -04:00
peerLabel + = " " ;
2008-02-03 01:31:11 -05:00
peerLabel + = timestamp ;
child - > setText ( 2 , peerLabel ) ;
}
2009-08-16 15:29:20 -04:00
/* (4) Id */
2008-02-03 01:31:11 -05:00
child - > setText ( 4 , QString : : fromStdString ( cit - > id ) ) ;
if ( i % 2 = = 1 )
{
/* set to light gray background */
child - > setBackground ( 0 , QBrush ( Qt : : lightGray ) ) ;
child - > setBackground ( 1 , QBrush ( Qt : : lightGray ) ) ;
child - > setBackground ( 2 , QBrush ( Qt : : lightGray ) ) ;
}
/* push to items */
item - > addChild ( child ) ;
}
/* add to the list */
items . append ( item ) ;
}
/* remove old items */
linkWidget - > clear ( ) ;
linkWidget - > setColumnCount ( 3 ) ;
/* add the items in! */
linkWidget - > insertTopLevelItems ( 0 , items ) ;
linkWidget - > update ( ) ; /* update display */
}
2010-03-10 16:38:26 -05:00
void LinksDialog : : openLink ( QTreeWidgetItem * item , int )
2008-02-03 01:31:11 -05:00
{
2009-08-16 15:29:20 -04:00
# ifdef LINKS_DEBUG
2008-02-03 01:31:11 -05:00
std : : cerr < < " LinksDialog::openLink() " < < std : : endl ;
2008-07-09 05:53:47 -04:00
# endif
2008-02-03 01:31:11 -05:00
/* work out the ids */
if ( ! item )
{
2008-07-09 05:53:47 -04:00
2009-08-16 15:29:20 -04:00
# ifdef LINKS_DEBUG
2008-02-03 01:31:11 -05:00
std : : cerr < < " LinksDialog::openLink() Failed Item " < < std : : endl ;
2008-07-09 05:53:47 -04:00
# endif
2008-02-03 01:31:11 -05:00
return ;
}
2009-08-16 15:29:20 -04:00
std : : string rid ;
std : : string pid ;
2008-02-03 01:31:11 -05:00
QTreeWidgetItem * parent = item - > parent ( ) ;
if ( parent )
{
/* a child comment -> ignore double click */
2009-08-16 15:29:20 -04:00
# ifdef LINKS_DEBUG
2008-02-03 01:31:11 -05:00
std : : cerr < < " LinksDialog::openLink() Failed Child " < < std : : endl ;
2008-07-09 05:53:47 -04:00
# endif
2008-02-03 01:31:11 -05:00
return ;
}
2009-08-16 15:29:20 -04:00
# ifdef LINKS_DEBUG
2008-02-03 11:43:03 -05:00
std : : cerr < < " LinksDialog::openLink() " < < ( item - > text ( 2 ) ) . toStdString ( ) < < std : : endl ;
2008-07-09 05:53:47 -04:00
# endif
2008-02-03 01:31:11 -05:00
/* open a browser */
2008-02-03 11:43:03 -05:00
QUrl url ( item - > text ( 2 ) ) ;
2008-02-03 01:31:11 -05:00
QDesktopServices : : openUrl ( url ) ;
/* close expansion */
bool state = item - > isExpanded ( ) ;
item - > setExpanded ( ! state ) ;
}
2010-03-10 16:38:26 -05:00
void LinksDialog : : changedItem ( QTreeWidgetItem * curr , QTreeWidgetItem * )
2008-02-03 01:31:11 -05:00
{
/* work out the ids */
if ( ! curr )
{
updateComments ( " " , " " ) ;
return ;
}
2009-08-16 15:29:20 -04:00
std : : string rid ;
std : : string pid ;
2008-02-03 01:31:11 -05:00
QTreeWidgetItem * parent = curr - > parent ( ) ;
if ( parent )
{
rid = ( parent - > text ( 4 ) ) . toStdString ( ) ;
pid = ( curr - > text ( 4 ) ) . toStdString ( ) ;
2009-08-16 15:29:20 -04:00
# ifdef LINKS_DEBUG
2008-02-03 01:31:11 -05:00
std : : cerr < < " LinksDialog::changedItem() Rid: " < < rid < < " Pid: " < < pid ;
std : : cerr < < std : : endl ;
2008-07-09 05:53:47 -04:00
# endif
2008-02-03 01:31:11 -05:00
updateComments ( rid , pid ) ;
}
else
{
rid = ( curr - > text ( 4 ) ) . toStdString ( ) ;
2009-08-16 15:29:20 -04:00
# ifdef LINKS_DEBUG
2008-02-03 01:31:11 -05:00
std : : cerr < < " LinksDialog::changedItem() Rid: " < < rid < < " Pid: NULL " ;
std : : cerr < < std : : endl ;
2008-07-09 05:53:47 -04:00
# endif
2008-02-03 01:31:11 -05:00
updateComments ( rid , " " ) ;
}
}
2008-04-15 12:40:38 -04:00
void LinksDialog : : checkAnon ( )
{
changedItem ( ui . linkTreeWidget - > currentItem ( ) , NULL ) ;
}
int IndexToScore ( int index )
{
if ( ( index = = - 1 ) | | ( index > 4 ) )
return 0 ;
int score = 2 - index ;
return score ;
}
int ScoreToIndex ( int score )
{
if ( ( score < - 2 ) | | ( score > 2 ) )
return 2 ;
int index = 2 - score ;
return index ;
}
2008-02-03 01:31:11 -05:00
/* get the list of Links from the RsRanks. */
2010-03-10 16:38:26 -05:00
void LinksDialog : : updateComments ( std : : string rid , std : : string )
2008-02-03 01:31:11 -05:00
{
std : : list < RsRankComment > : : iterator cit ;
2008-04-15 12:40:38 -04:00
if ( ui . anonBox - > isChecked ( ) )
2008-02-03 01:31:11 -05:00
{
2008-04-15 12:40:38 -04:00
/* empty everything */
2008-02-03 01:31:11 -05:00
ui . titleLineEdit - > setText ( " " ) ;
ui . linkLineEdit - > setText ( " " ) ;
ui . linkTextEdit - > setText ( " " ) ;
2008-04-15 12:40:38 -04:00
ui . scoreBox - > setCurrentIndex ( ScoreToIndex ( 0 ) ) ;
2008-05-24 16:23:15 -04:00
mLinkId = rid ; /* must be set for Context Menu */
2008-04-15 12:40:38 -04:00
/* disable comment + score */
ui . scoreBox - > setEnabled ( false ) ;
ui . linkTextEdit - > setEnabled ( false ) ;
/* done! */
2008-02-03 01:31:11 -05:00
return ;
}
2008-04-15 12:40:38 -04:00
else
{
/* enable comment + score */
ui . scoreBox - > setEnabled ( true ) ;
ui . linkTextEdit - > setEnabled ( true ) ;
}
2008-02-03 01:31:11 -05:00
RsRankDetails detail ;
2008-04-15 12:40:38 -04:00
if ( ( rid = = " " ) | | ( ! rsRanks - > getRankDetails ( rid , detail ) ) )
2008-02-03 01:31:11 -05:00
{
/* clear it up */
ui . titleLineEdit - > setText ( " " ) ;
ui . linkLineEdit - > setText ( " " ) ;
ui . linkTextEdit - > setText ( " " ) ;
2008-04-15 12:40:38 -04:00
ui . scoreBox - > setCurrentIndex ( ScoreToIndex ( 0 ) ) ;
mLinkId = rid ;
2008-02-03 01:31:11 -05:00
return ;
}
2008-04-15 12:40:38 -04:00
2008-02-03 01:31:11 -05:00
/* set Link details */
ui . titleLineEdit - > setText ( QString : : fromStdWString ( detail . title ) ) ;
ui . linkLineEdit - > setText ( QString : : fromStdWString ( detail . link ) ) ;
2010-02-05 08:44:33 -05:00
ui . linklabel - > setHtml ( " <a href=' " + QString : : fromStdWString ( detail . link ) + " '> " + QString : : fromStdWString ( detail . link ) + " </a> " ) ;
2008-02-03 01:31:11 -05:00
2008-02-05 08:45:04 -05:00
if ( mLinkId = = rid )
2008-02-03 01:31:11 -05:00
{
2008-02-05 08:45:04 -05:00
/* leave comments */
//ui.linkTextEdit->setText("");
return ;
2008-02-03 01:31:11 -05:00
}
2008-02-05 08:45:04 -05:00
2008-02-03 01:31:11 -05:00
mLinkId = rid ;
2008-02-05 08:45:04 -05:00
/* Add your text to the comment */
std : : string ownId = rsPeers - > getOwnId ( ) ;
for ( cit = detail . comments . begin ( ) ; cit ! = detail . comments . end ( ) ; cit + + )
{
if ( cit - > id = = ownId )
break ;
}
if ( cit ! = detail . comments . end ( ) )
{
QString comment = QString : : fromStdWString ( cit - > comment ) ;
ui . linkTextEdit - > setText ( comment ) ;
2008-04-15 12:40:38 -04:00
ui . scoreBox - > setCurrentIndex ( ScoreToIndex ( cit - > score ) ) ;
2008-02-05 08:45:04 -05:00
}
else
{
ui . linkTextEdit - > setText ( " " ) ;
2008-04-15 12:40:38 -04:00
ui . scoreBox - > setCurrentIndex ( ScoreToIndex ( 0 ) ) ;
2008-02-05 08:45:04 -05:00
}
return ;
2008-02-03 01:31:11 -05:00
}
2009-08-16 15:29:20 -04:00
void LinksDialog : : addLinkComment ( void )
2008-02-03 01:31:11 -05:00
{
/* get the title / link / comment */
QString title = ui . titleLineEdit - > text ( ) ;
QString link = ui . linkLineEdit - > text ( ) ;
QString comment = ui . linkTextEdit - > toPlainText ( ) ;
2008-04-15 12:40:38 -04:00
int32_t score = IndexToScore ( ui . scoreBox - > currentIndex ( ) ) ;
2008-02-03 01:31:11 -05:00
2008-05-24 16:23:15 -04:00
if ( ( mLinkId = = " " ) | | ( ui . anonBox - > isChecked ( ) ) )
2008-02-03 01:31:11 -05:00
{
if ( ( link = = " " ) | | ( title = = " " ) )
{
2010-03-10 16:38:26 -05:00
QMessageBox : : warning ( NULL , " Add Link Failure " , " Missing Link and/or Title " , QMessageBox : : Ok ) ;
2008-02-03 01:31:11 -05:00
/* can't do anything */
return ;
}
2008-04-15 12:40:38 -04:00
/* add it either way */
if ( ui . anonBox - > isChecked ( ) )
{
2008-05-24 16:23:15 -04:00
rsRanks - > anonRankMsg ( " " , link . toStdWString ( ) , title . toStdWString ( ) ) ;
2008-04-15 12:40:38 -04:00
}
else
{
rsRanks - > newRankMsg (
link . toStdWString ( ) ,
title . toStdWString ( ) ,
comment . toStdWString ( ) , score ) ;
}
2009-08-16 15:29:20 -04:00
2008-02-05 08:45:04 -05:00
updateLinks ( ) ;
return ;
2008-02-03 01:31:11 -05:00
}
/* get existing details */
RsRankDetails detail ;
if ( ! rsRanks - > getRankDetails ( mLinkId , detail ) )
{
/* strange error! */
2010-03-10 16:38:26 -05:00
QMessageBox : : warning ( NULL , " Add Link Failure " , " Missing Link Data " , QMessageBox : : Ok ) ;
2008-02-03 01:31:11 -05:00
return ;
}
if ( link . toStdWString ( ) = = detail . link ) /* same link! - we can add a comment */
{
if ( comment = = " " ) /* no comment! */
{
2010-03-10 16:38:26 -05:00
QMessageBox : : warning ( NULL , " Add Link Failure " , " Missing Comment " , QMessageBox : : Ok ) ;
2008-02-03 01:31:11 -05:00
return ;
}
2009-08-16 15:29:20 -04:00
rsRanks - > updateComment ( mLinkId ,
comment . toStdWString ( ) ,
2008-04-15 12:40:38 -04:00
score ) ;
2008-02-03 01:31:11 -05:00
}
else
{
QMessageBox : : StandardButton sb = QMessageBox : : Yes ;
if ( ( title . toStdWString ( ) = = detail . title ) /* same title! - wrong */
| | ( title = = " " ) )
{
2009-08-16 15:29:20 -04:00
sb = QMessageBox : : question ( NULL , " Link Title Not Changed " ,
2008-02-03 01:31:11 -05:00
" Do you want to continue? " ,
( QMessageBox : : Yes | QMessageBox : : No ) ) ;
}
/* add Link! */
if ( sb = = QMessageBox : : Yes )
{
rsRanks - > newRankMsg (
link . toStdWString ( ) ,
title . toStdWString ( ) ,
2009-08-16 15:29:20 -04:00
comment . toStdWString ( ) ,
2008-04-15 12:40:38 -04:00
score ) ;
2008-02-03 01:31:11 -05:00
}
}
updateLinks ( ) ;
return ;
}
void LinksDialog : : toggleWindows ( void )
{
2009-08-16 15:29:20 -04:00
/* if msg header visible -> hide by changing splitter
2008-02-03 01:31:11 -05:00
*/
QList < int > sizeList = ui . msgSplitter - > sizes ( ) ;
QList < int > : : iterator it ;
int listSize = 0 ;
int msgSize = 0 ;
int i = 0 ;
for ( it = sizeList . begin ( ) ; it ! = sizeList . end ( ) ; it + + , i + + )
{
if ( i = = 0 )
{
listSize = ( * it ) ;
}
else if ( i = = 1 )
{
msgSize = ( * it ) ;
}
}
int totalSize = listSize + msgSize ;
bool toShrink = true ;
if ( msgSize < ( int ) totalSize / 10 )
{
toShrink = false ;
}
QList < int > newSizeList ;
if ( toShrink )
{
newSizeList . push_back ( totalSize ) ;
newSizeList . push_back ( 0 ) ;
2008-08-12 14:41:08 -04:00
ui . expandButton - > setIcon ( QIcon ( QString ( " :/images/edit_add24.png " ) ) ) ;
2010-09-13 16:05:55 -04:00
ui . expandButton - > setToolTip ( tr ( " Expand " ) ) ;
2008-02-03 01:31:11 -05:00
}
else
{
newSizeList . push_back ( totalSize * 3 / 4 ) ;
newSizeList . push_back ( totalSize * 1 / 4 ) ;
2008-08-12 14:41:08 -04:00
ui . expandButton - > setIcon ( QIcon ( QString ( " :/images/edit_remove24.png " ) ) ) ;
2010-09-13 16:05:55 -04:00
ui . expandButton - > setToolTip ( tr ( " Hide " ) ) ;
2008-02-03 01:31:11 -05:00
}
ui . msgSplitter - > setSizes ( newSizeList ) ;
return ;
}
QTreeWidgetItem * LinksDialog : : getCurrentLine ( )
{
/* get the current, and extract the Id */
/* get a link to the table */
QTreeWidget * peerWidget = ui . linkTreeWidget ;
QTreeWidgetItem * item = peerWidget - > currentItem ( ) ;
if ( ! item )
{
2009-08-16 15:29:20 -04:00
# ifdef LINKS_DEBUG
2008-02-03 01:31:11 -05:00
std : : cerr < < " Invalid Current Item " < < std : : endl ;
2008-07-09 05:53:47 -04:00
# endif
2008-02-03 01:31:11 -05:00
return NULL ;
}
2009-08-16 15:29:20 -04:00
# ifdef LINKS_DEBUG
2008-02-03 01:31:11 -05:00
/* Display the columns of this item. */
std : : ostringstream out ;
out < < " CurrentPeerItem: " < < std : : endl ;
for ( int i = 1 ; i < 6 ; i + + )
{
QString txt = item - > text ( i ) ;
out < < " \t " < < i < < " : " < < txt . toStdString ( ) < < std : : endl ;
}
std : : cerr < < out . str ( ) ;
2008-07-09 05:53:47 -04:00
# endif
2008-02-03 01:31:11 -05:00
return item ;
}
2008-05-13 11:45:09 -04:00
void LinksDialog : : voteup_anon ( )
2008-02-03 01:31:11 -05:00
{
//QTreeWidgetItem *c = getCurrentLine();
if ( mLinkId = = " " )
{
return ;
}
RsRankDetails detail ;
if ( ! rsRanks - > getRankDetails ( mLinkId , detail ) )
{
/* not there! */
return ;
}
QString link = QString : : fromStdWString ( detail . link ) ;
2009-08-16 15:29:20 -04:00
# ifdef LINKS_DEBUG
2008-05-13 11:45:09 -04:00
std : : cerr < < " LinksDialog::voteup_anon() : " < < link . toStdString ( ) < < std : : endl ;
2008-07-09 05:53:47 -04:00
# endif
2008-05-13 11:45:09 -04:00
// need a proper anon sharing option.
rsRanks - > anonRankMsg ( mLinkId , detail . link , detail . title ) ;
2008-02-03 01:31:11 -05:00
}
2008-05-13 11:45:09 -04:00
void LinksDialog : : voteup_score ( int score )
2008-02-03 01:31:11 -05:00
{
2008-05-13 11:45:09 -04:00
if ( mLinkId = = " " )
{
return ;
}
RsRankDetails detail ;
if ( ! rsRanks - > getRankDetails ( mLinkId , detail ) )
{
/* not there! */
return ;
}
QString link = QString : : fromStdWString ( detail . link ) ;
std : : wstring comment ;
2009-08-16 15:29:20 -04:00
# ifdef LINKS_DEBUG
2008-05-13 11:45:09 -04:00
std : : cerr < < " LinksDialog::voteup_score() : " < < link . toStdString ( ) < < std : : endl ;
2008-07-09 05:53:47 -04:00
# endif
2008-05-13 11:45:09 -04:00
std : : list < RsRankComment > : : iterator cit ;
/* Add your text to the comment */
std : : string ownId = rsPeers - > getOwnId ( ) ;
for ( cit = detail . comments . begin ( ) ; cit ! = detail . comments . end ( ) ; cit + + )
{
if ( cit - > id = = ownId )
break ;
}
if ( cit ! = detail . comments . end ( ) )
{
comment = cit - > comment ;
}
rsRanks - > updateComment ( mLinkId , comment , score ) ;
2008-02-03 01:31:11 -05:00
}
2008-05-13 11:45:09 -04:00
void LinksDialog : : voteup_p2 ( )
{
voteup_score ( 2 ) ;
}
void LinksDialog : : voteup_p1 ( )
{
voteup_score ( 1 ) ;
}
void LinksDialog : : voteup_p0 ( )
{
voteup_score ( 0 ) ;
}
void LinksDialog : : voteup_m1 ( )
{
voteup_score ( - 1 ) ;
}
void LinksDialog : : voteup_m2 ( )
{
voteup_score ( - 2 ) ;
}
2008-02-03 01:31:11 -05:00
2009-08-16 15:29:20 -04:00
void LinksDialog : : downloadSelected ( )
{
if ( mLinkId = = " " )
{
return ;
}
RsRankDetails detail ;
if ( ! rsRanks - > getRankDetails ( mLinkId , detail ) )
{
/* not there! */
return ;
}
QString link = QString : : fromStdWString ( detail . link ) ;
std : : wstring comment ;
# ifdef LINKS_DEBUG
std : : cerr < < " LinksDialog::downloadSelected() : " < < link . toStdString ( ) < < std : : endl ;
# endif
2010-03-10 16:38:26 -05:00
RetroShareLink rslink ( QString : : fromStdWString ( detail . link ) ) ;
2010-07-15 07:25:34 -04:00
if ( ! rslink . valid ( ) | | rslink . type ( ) ! = RetroShareLink : : TYPE_FILE )
2009-08-16 15:29:20 -04:00
{
2010-03-10 16:38:26 -05:00
QMessageBox : : critical ( NULL , " Badly formed link " , " This link is badly formed. Can't parse/use it. This is a bug. Please contact the developers. " ) ;
return ;
}
2009-08-16 15:29:20 -04:00
2010-03-10 16:38:26 -05:00
/* retrieve all peers id for this file */
FileInfo info ;
rsFiles - > FileDetails ( rslink . hash ( ) . toStdString ( ) , 0 , info ) ;
2009-08-16 15:29:20 -04:00
2010-03-10 16:38:26 -05:00
std : : list < std : : string > srcIds ;
std : : list < TransferInfo > : : iterator pit ;
for ( pit = info . peers . begin ( ) ; pit ! = info . peers . end ( ) ; pit + + )
srcIds . push_back ( pit - > peerId ) ;
2009-08-16 15:29:20 -04:00
2010-03-10 16:38:26 -05:00
rsFiles - > FileRequest ( rslink . name ( ) . toStdString ( ) , rslink . hash ( ) . toStdString ( ) , rslink . size ( ) , " " , 0 , srcIds ) ;
2009-08-16 15:29:20 -04:00
}
2010-02-05 08:44:33 -05:00
void LinksDialog : : anchorClicked ( const QUrl & link )
{
# ifdef LINK_DEBUG
std : : cerr < < " LinksDialog::anchorClicked link.scheme() : " < < link . scheme ( ) . toStdString ( ) < < std : : endl ;
# endif
if ( link . scheme ( ) = = " retroshare " )
{
QStringList L = link . toString ( ) . split ( " | " ) ;
std : : string fileName = L . at ( 1 ) . toStdString ( ) ;
uint64_t fileSize = L . at ( 2 ) . toULongLong ( ) ;
std : : string fileHash = L . at ( 3 ) . toStdString ( ) ;
# ifdef LINK_DEBUG
std : : cerr < < " LinksDialog::anchorClicked FileRequest : fileName : " < < fileName < < " . fileHash : " < < fileHash < < " . fileSize : " < < fileSize < < std : : endl ;
# endif
if ( fileName ! = " " & & fileHash ! = " " )
{
std : : list < std : : string > srcIds ;
if ( rsFiles - > FileRequest ( fileName , fileHash , fileSize , " " , RS_FILE_HINTS_NETWORK_WIDE , srcIds ) )
{
QMessageBox mb ( tr ( " File Request Confirmation " ) , tr ( " The file has been added to your download list. " ) , QMessageBox : : Information , QMessageBox : : Ok , 0 , 0 ) ;
mb . setButtonText ( QMessageBox : : Ok , " OK " ) ;
mb . setWindowIcon ( QIcon ( QString : : fromUtf8 ( " :/images/rstray3.png " ) ) ) ;
mb . exec ( ) ;
}
else
{
QMessageBox mb ( tr ( " File Request canceled " ) , tr ( " The file has not been added to your download list, because you already have it. " ) , QMessageBox : : Information , QMessageBox : : Ok , 0 , 0 ) ;
mb . setButtonText ( QMessageBox : : Ok , " OK " ) ;
mb . exec ( ) ;
}
}
else
{
QMessageBox mb ( tr ( " File Request Error " ) , tr ( " The file link is malformed. " ) , QMessageBox : : Information , QMessageBox : : Ok , 0 , 0 ) ;
mb . setButtonText ( QMessageBox : : Ok , " OK " ) ;
mb . setWindowIcon ( QIcon ( QString : : fromUtf8 ( " :/images/rstray3.png " ) ) ) ;
mb . exec ( ) ;
}
}
else if ( link . scheme ( ) = = " http " )
{
QDesktopServices : : openUrl ( link ) ;
}
else if ( link . scheme ( ) = = " " )
{
//it's probably a web adress, let's add http:// at the beginning of the link
QString newAddress = link . toString ( ) ;
newAddress . prepend ( " http:// " ) ;
QDesktopServices : : openUrl ( QUrl ( newAddress ) ) ;
}
}