2018-12-25 15:34:59 -05:00
/*******************************************************************************
* gui / NetworkDialog . cpp *
* *
* Copyright ( c ) 2006 Crypton < retroshare . project @ gmail . com > *
* *
* This program is free software : you can redistribute it and / or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation , either version 3 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 Affero General Public License for more details . *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program . If not , see < https : //www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-09-06 16:44:22 -04:00
# include <QTreeWidget>
# include <QDebug>
# include <QTimer>
# include <QTime>
# include <QMenu>
2017-03-01 16:25:57 -05:00
# include <QSortFilterProxyModel>
2011-09-06 16:44:22 -04:00
# include <retroshare/rsiface.h>
# include <retroshare/rspeers.h>
# include <retroshare/rsdisc.h>
2013-04-10 17:21:52 -04:00
# include <retroshare/rsmsgs.h>
2011-09-06 16:44:22 -04:00
# include "common/vmessagebox.h"
# include "common/RSTreeWidgetItem.h"
2013-05-29 17:32:02 -04:00
# include <gui/common/FriendSelectionDialog.h>
2013-08-28 11:37:11 -04:00
# include "gui/msgs/MessageComposer.h"
2014-09-25 16:14:21 -04:00
# include "gui/profile/ProfileManager.h"
2011-09-06 16:44:22 -04:00
# include "NetworkDialog.h"
2012-07-17 16:40:51 -04:00
//#include "TrustView.h"
2011-09-06 16:44:22 -04:00
# include "NetworkView.h"
# include "GenCertDialog.h"
2015-05-12 16:30:44 -04:00
# include "connect/PGPKeyDialog.h"
2011-09-06 16:44:22 -04:00
# include "settings/rsharesettings.h"
# include "RetroShareLink.h"
2013-10-19 09:25:06 -04:00
# include "util/QtVersion.h"
2011-09-06 16:44:22 -04:00
# include <time.h>
/* Images for context menu icons */
# define IMAGE_LOADCERT ": / images / loadcert16.png"
2013-08-28 11:37:11 -04:00
# define IMAGE_PEERDETAILS ": / images / info16.png"
2011-09-06 16:44:22 -04:00
# define IMAGE_AUTH ": / images / encrypted16.png"
2013-05-29 17:32:02 -04:00
# define IMAGE_CLEAN_UNUSED ": / images / deletemail24.png"
2011-09-06 16:44:22 -04:00
# define IMAGE_MAKEFRIEND ": / images / user / add_user16.png"
# define IMAGE_EXPORT ": / images / exportpeers_16x16.png"
# define IMAGE_COPYLINK ": / images / copyrslink.png"
2013-08-28 11:37:11 -04:00
# define IMAGE_MESSAGE ": / images / mail_new.png"
2011-09-06 16:44:22 -04:00
/******
* # define NET_DEBUG 1
* * * * */
/** Constructor */
2017-12-08 13:47:00 -05:00
NetworkDialog : : NetworkDialog ( QWidget */ * parent */ )
2011-09-06 16:44:22 -04:00
{
/* Invoke the Qt Designer generated object setup routine */
ui . setupUi ( this ) ;
2012-04-05 17:03:03 -04:00
connect ( ui . filterLineEdit , SIGNAL ( textChanged ( const QString & ) ) , this , SLOT ( filterItems ( QString ) ) ) ;
2012-10-29 06:59:26 -04:00
connect ( ui . filterLineEdit , SIGNAL ( filterChanged ( int ) ) , this , SLOT ( filterColumnChanged ( int ) ) ) ;
2011-09-06 16:44:22 -04:00
2017-10-08 15:00:15 -04:00
//list data model
float f = QFontMetricsF ( font ( ) ) . height ( ) / 14.0 ;
2017-12-16 11:46:44 -05:00
2017-10-08 15:00:15 -04:00
PGPIdItemModel = new pgpid_item_model ( neighs , f , this ) ;
PGPIdItemProxy = new pgpid_item_proxy ( this ) ;
2017-10-12 15:33:39 -04:00
connect ( ui . onlyTrustedKeys , SIGNAL ( toggled ( bool ) ) , PGPIdItemProxy , SLOT ( use_only_trusted_keys ( bool ) ) ) ;
2017-10-08 15:00:15 -04:00
PGPIdItemProxy - > setSourceModel ( PGPIdItemModel ) ;
PGPIdItemProxy - > setFilterKeyColumn ( COLUMN_PEERNAME ) ;
PGPIdItemProxy - > setFilterCaseSensitivity ( Qt : : CaseInsensitive ) ;
2017-10-12 15:33:39 -04:00
PGPIdItemProxy - > setSortRole ( Qt : : EditRole ) ; //use edit role to get raw data since we do not have edit for this model.
2017-10-08 15:00:15 -04:00
ui . connectTreeWidget - > setModel ( PGPIdItemProxy ) ;
ui . connectTreeWidget - > setContextMenuPolicy ( Qt : : CustomContextMenu ) ;
ui . connectTreeWidget - > verticalHeader ( ) - > hide ( ) ;
ui . connectTreeWidget - > setShowGrid ( false ) ;
ui . connectTreeWidget - > setUpdatesEnabled ( true ) ;
ui . connectTreeWidget - > setSortingEnabled ( true ) ;
ui . connectTreeWidget - > setSelectionBehavior ( QAbstractItemView : : SelectRows ) ;
2017-12-16 11:46:44 -05:00
ui . connectTreeWidget - > setSelectionMode ( QAbstractItemView : : SingleSelection ) ;
2017-10-08 15:00:15 -04:00
connect ( ui . connectTreeWidget , SIGNAL ( customContextMenuRequested ( QPoint ) ) , this , SLOT ( connectTreeWidgetCostumPopupMenu ( QPoint ) ) ) ;
connect ( ui . connectTreeWidget , SIGNAL ( doubleClicked ( QModelIndex ) ) , this , SLOT ( peerdetails ( ) ) ) ;
ui . onlyTrustedKeys - > setMinimumWidth ( 20 * f ) ;
2011-09-06 16:44:22 -04:00
2012-10-29 06:59:26 -04:00
/* add filter actions */
2015-06-01 14:46:33 -04:00
ui . filterLineEdit - > addFilter ( QIcon ( ) , tr ( " Name " ) , COLUMN_PEERNAME , tr ( " Search name " ) ) ;
ui . filterLineEdit - > addFilter ( QIcon ( ) , tr ( " Peer ID " ) , COLUMN_PEERID , tr ( " Search peer ID " ) ) ;
2012-10-29 06:59:26 -04:00
ui . filterLineEdit - > setCurrentFilter ( COLUMN_PEERNAME ) ;
2017-10-08 15:00:15 -04:00
connect ( ui . filterLineEdit , SIGNAL ( textChanged ( QString ) ) , PGPIdItemProxy , SLOT ( setFilterWildcard ( QString ) ) ) ;
2011-09-06 16:44:22 -04:00
}
2012-11-19 11:01:21 -05:00
void NetworkDialog : : changeEvent ( QEvent * e )
{
QWidget : : changeEvent ( e ) ;
switch ( e - > type ( ) ) {
case QEvent : : StyleChange :
2013-05-21 08:55:03 -04:00
securedUpdateDisplay ( ) ;
2012-11-19 11:01:21 -05:00
break ;
default :
// remove compiler warnings
break ;
}
}
2013-07-04 16:09:46 -04:00
void NetworkDialog : : connectTreeWidgetCostumPopupMenu ( QPoint /*point*/ )
2011-09-06 16:44:22 -04:00
{
2017-10-08 15:00:15 -04:00
2017-12-08 13:47:00 -05:00
QModelIndexList l = ui . connectTreeWidget - > selectionModel ( ) - > selection ( ) . indexes ( ) ;
if ( l . empty ( ) )
{
return ;
}
2011-09-06 16:44:22 -04:00
2012-08-15 16:08:18 -04:00
QMenu * contextMnu = new QMenu ;
2011-09-06 16:44:22 -04:00
2017-12-08 13:47:00 -05:00
RsPgpId peer_id ( ui . connectTreeWidget - > model ( ) - > data ( ui . connectTreeWidget - > model ( ) - > index ( l . begin ( ) - > row ( ) , COLUMN_PEERID ) ) . toString ( ) . toStdString ( ) ) ;
2011-09-06 16:44:22 -04:00
2012-08-15 16:08:18 -04:00
// That's what context menus are made for
RsPeerDetails detail ;
if ( ! rsPeers - > getGPGDetails ( peer_id , detail ) ) // that is not suppose to fail.
return ;
2011-09-06 16:44:22 -04:00
2012-08-15 16:08:18 -04:00
if ( peer_id = = rsPeers - > getGPGOwnId ( ) )
2014-12-30 06:11:08 -05:00
contextMnu - > addAction ( QIcon ( IMAGE_EXPORT ) , tr ( " Export/create a new node " ) , this , SLOT ( on_actionExportKey_activated ( ) ) ) ;
2013-05-29 17:32:02 -04:00
2017-03-01 16:25:57 -05:00
contextMnu - > addAction ( QIcon ( IMAGE_PEERDETAILS ) , tr ( " Profile details... " ) , this , SLOT ( peerdetails ( ) ) ) ;
2013-05-29 17:32:02 -04:00
contextMnu - > addSeparator ( ) ;
contextMnu - > addAction ( QIcon ( IMAGE_CLEAN_UNUSED ) , tr ( " Remove unused keys... " ) , this , SLOT ( removeUnusedKeys ( ) ) ) ;
contextMnu - > exec ( QCursor : : pos ( ) ) ;
}
void NetworkDialog : : removeUnusedKeys ( )
{
2015-04-17 17:36:22 -04:00
std : : set < RsPgpId > pre_selected ;
2014-03-17 16:56:06 -04:00
std : : list < RsPgpId > ids ;
2013-05-29 17:32:02 -04:00
rsPeers - > getGPGAllList ( ids ) ;
RsPeerDetails details ;
time_t now = time ( NULL ) ;
2013-09-07 10:06:08 -04:00
time_t THREE_MONTHS = 3 * 31 * 24 * 60 * 60 ; //3*DayPerMonth*HoursPerDay*MinPerHour*SecPerMin
2013-05-29 17:32:02 -04:00
2014-03-17 16:56:06 -04:00
for ( std : : list < RsPgpId > : : const_iterator it ( ids . begin ( ) ) ; it ! = ids . end ( ) ; + + it )
2012-08-15 16:08:18 -04:00
{
2014-03-17 16:56:06 -04:00
rsPeers - > getGPGDetails ( * it , details ) ;
2013-05-29 17:32:02 -04:00
2013-05-31 15:16:04 -04:00
if ( rsPeers - > haveSecretKey ( * it ) )
{
std : : cerr < < " Skipping public/secret key pair " < < * it < < std : : endl ;
continue ;
}
2017-07-27 14:43:08 -04:00
if ( now > ( time_t ) ( THREE_MONTHS + details . lastUsed ) & & ! details . accept_connection )
2013-05-29 17:32:02 -04:00
{
std : : cerr < < " Adding " < < * it < < " to pre-selection. " < < std : : endl ;
2015-04-17 17:36:22 -04:00
pre_selected . insert ( * it ) ;
2013-05-29 17:32:02 -04:00
}
2012-08-15 16:08:18 -04:00
}
2011-09-06 16:44:22 -04:00
2015-04-17 17:36:22 -04:00
std : : set < RsPgpId > selected = FriendSelectionDialog : : selectFriends_PGP ( NULL ,
2013-05-29 17:32:02 -04:00
tr ( " Clean keyring " ) ,
tr ( " The selected keys below haven't been used in the last 3 months. \n Do you want to delete them permanently ? \n \n Notes: Your old keyring will be backed up. \n The removal may fail when running multiple Retroshare instances on the same machine. " ) , FriendSelectionWidget : : MODUS_CHECK , FriendSelectionWidget : : SHOW_GPG | FriendSelectionWidget : : SHOW_NON_FRIEND_GPG ,
2014-03-17 16:56:06 -04:00
pre_selected ) ;
2013-05-29 17:32:02 -04:00
std : : cerr < < " Removing these keys from the keyring: " < < std : : endl ;
2015-04-17 17:36:22 -04:00
for ( std : : set < RsPgpId > : : const_iterator it ( selected . begin ( ) ) ; it ! = selected . end ( ) ; + + it )
2013-05-29 17:32:02 -04:00
std : : cerr < < " " < < * it < < std : : endl ;
std : : string backup_file ;
uint32_t error_code ;
2013-05-31 15:16:04 -04:00
if ( selected . empty ( ) )
return ;
2013-05-29 17:32:02 -04:00
if ( rsPeers - > removeKeysFromPGPKeyring ( selected , backup_file , error_code ) )
QMessageBox : : information ( NULL , tr ( " Keyring info " ) , tr ( " %1 keys have been deleted from your keyring. \n For security, your keyring was previously backed-up to file \n \n " ) . arg ( selected . size ( ) ) + QString : : fromStdString ( backup_file ) ) ;
else
{
QString error_string ;
2011-09-06 16:44:22 -04:00
2013-05-29 17:32:02 -04:00
switch ( error_code )
{
default :
case PGP_KEYRING_REMOVAL_ERROR_NO_ERROR : error_string = tr ( " Unknown error " ) ;
break ;
case PGP_KEYRING_REMOVAL_ERROR_CANT_REMOVE_SECRET_KEYS : error_string = tr ( " Cannot delete secret keys " ) ;
break ;
case PGP_KEYRING_REMOVAL_ERROR_CANNOT_WRITE_BACKUP :
case PGP_KEYRING_REMOVAL_ERROR_CANNOT_CREATE_BACKUP : error_string = tr ( " Cannot create backup file. Check for permissions in pgp directory, disk space, etc. " ) ;
break ;
2013-06-10 13:18:51 -04:00
case PGP_KEYRING_REMOVAL_ERROR_DATA_INCONSISTENCY : error_string = tr ( " Data inconsistency in the keyring. This is most probably a bug. Please contact the developers. " ) ;
2013-05-31 15:16:04 -04:00
break ;
2011-09-06 16:44:22 -04:00
2013-05-29 17:32:02 -04:00
}
2015-06-12 04:27:26 -04:00
QMessageBox : : warning ( NULL , tr ( " Keyring info " ) , tr ( " Key removal has failed. Your keyring remains intact. \n \n Reported error: " ) + " " + error_string ) ;
2013-05-29 17:32:02 -04:00
}
2017-10-12 15:33:39 -04:00
updateDisplay ( ) ;
// insertConnect() ;
2011-09-06 16:44:22 -04:00
}
void NetworkDialog : : denyFriend ( )
{
2017-10-08 15:00:15 -04:00
QModelIndexList l = ui . connectTreeWidget - > selectionModel ( ) - > selection ( ) . indexes ( ) ;
if ( l . empty ( ) )
return ;
RsPgpId peer_id ( ui . connectTreeWidget - > model ( ) - > data ( ui . connectTreeWidget - > model ( ) - > index ( l . begin ( ) - > row ( ) , COLUMN_PEERID ) ) . toString ( ) . toStdString ( ) ) ;
2011-09-06 16:44:22 -04:00
rsPeers - > removeFriend ( peer_id ) ;
2013-05-21 08:55:03 -04:00
securedUpdateDisplay ( ) ;
2011-09-06 16:44:22 -04:00
}
2013-09-07 10:06:08 -04:00
2011-09-06 16:44:22 -04:00
void NetworkDialog : : makeFriend ( )
{
2017-10-08 15:00:15 -04:00
QModelIndexList l = ui . connectTreeWidget - > selectionModel ( ) - > selection ( ) . indexes ( ) ;
if ( l . empty ( ) )
return ;
PGPKeyDialog : : showIt ( RsPgpId ( ui . connectTreeWidget - > model ( ) - > data ( ui . connectTreeWidget - > model ( ) - > index ( l . begin ( ) - > row ( ) , COLUMN_PEERID ) ) . toString ( ) . toStdString ( ) ) , PGPKeyDialog : : PageDetails ) ;
2011-09-06 16:44:22 -04:00
}
/** Shows Peer Information/Auth Dialog */
void NetworkDialog : : peerdetails ( )
{
2017-10-08 15:00:15 -04:00
QModelIndexList l = ui . connectTreeWidget - > selectionModel ( ) - > selection ( ) . indexes ( ) ;
if ( l . empty ( ) )
return ;
PGPKeyDialog : : showIt ( RsPgpId ( ui . connectTreeWidget - > model ( ) - > data ( ui . connectTreeWidget - > model ( ) - > index ( l . begin ( ) - > row ( ) , COLUMN_PEERID ) ) . toString ( ) . toStdString ( ) ) , PGPKeyDialog : : PageDetails ) ;
2011-09-06 16:44:22 -04:00
}
void NetworkDialog : : copyLink ( )
{
2017-10-08 15:00:15 -04:00
QModelIndexList l = ui . connectTreeWidget - > selectionModel ( ) - > selection ( ) . indexes ( ) ;
if ( l . empty ( ) )
return ;
2011-09-06 16:44:22 -04:00
2017-10-08 15:00:15 -04:00
RsPgpId peer_id ( ui . connectTreeWidget - > model ( ) - > data ( ui . connectTreeWidget - > model ( ) - > index ( l . begin ( ) - > row ( ) , COLUMN_PEERID ) ) . toString ( ) . toStdString ( ) ) ;
2011-09-06 16:44:22 -04:00
2011-09-11 18:07:24 -04:00
QList < RetroShareLink > urls ;
2017-07-16 07:11:47 -04:00
RetroShareLink link = RetroShareLink : : createPerson ( peer_id ) ;
if ( link . valid ( ) ) {
2011-09-06 16:44:22 -04:00
urls . push_back ( link ) ;
}
RSLinkClipboard : : copyLinks ( urls ) ;
}
2019-01-06 14:58:32 -05:00
// void NetworkDialog::on_actionExportKey_activated()
2013-02-22 16:42:27 -05:00
// {
2019-01-06 14:58:32 -05:00
// ProfileManager prof ;
// prof.exec() ;
2013-02-22 16:42:27 -05:00
// }
2011-09-06 16:44:22 -04:00
2017-10-08 15:00:15 -04:00
void NetworkDialog : : filterColumnChanged ( int col )
2011-09-06 16:44:22 -04:00
{
2017-10-08 15:00:15 -04:00
if ( PGPIdItemProxy )
PGPIdItemProxy - > setFilterKeyColumn ( col ) ;
//filterItems(ui.filterLineEdit->text());
2011-09-06 16:44:22 -04:00
}
2017-10-08 15:00:15 -04:00
void NetworkDialog : : updateDisplay ( )
{
if ( ! rsPeers )
return ;
//update ids list
std : : list < RsPgpId > new_neighs ;
rsPeers - > getGPGAllList ( new_neighs ) ;
//refresh model
PGPIdItemModel - > data_updated ( new_neighs ) ;
}