2013-04-13 09:12:13 -04:00
/****************************************************************
* RetroShare is distributed under the following license :
*
2013-04-29 16:44:48 -04:00
* Copyright ( C ) 2013 Cyril Soler
2013-04-13 09:12:13 -04:00
*
* This program is free software ; you can redistribute it and / or
* 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 .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include <iostream>
2013-06-06 17:42:15 -04:00
# include <time.h>
2013-04-13 18:40:17 -04:00
# include <QMessageBox>
2013-04-14 17:45:02 -04:00
# include <QTimer>
2013-04-13 18:40:17 -04:00
# include <retroshare/rsmsgs.h>
2013-04-29 16:44:48 -04:00
# include <retroshare/rspeers.h>
2013-04-13 09:12:13 -04:00
# include "CreateMsgLinkDialog.h"
# include <gui/common/FriendSelectionWidget.h>
2013-04-13 18:40:17 -04:00
# include <gui/RetroShareLink.h>
2013-04-13 09:12:13 -04:00
2013-06-10 09:27:24 -04:00
CreateMsgLinkDialog : : CreateMsgLinkDialog ( )
2013-04-13 09:12:13 -04:00
: QDialog ( NULL , Qt : : WindowSystemMenuHint | Qt : : WindowTitleHint | Qt : : WindowMinMaxButtonsHint | Qt : : WindowCloseButtonHint )
{
/* Invoke the Qt Designer generated object setup routine */
setupUi ( this ) ;
2013-04-13 18:40:17 -04:00
setAttribute ( Qt : : WA_DeleteOnClose , false ) ;
2013-04-13 09:12:13 -04:00
_info_GB - > layout ( ) - > addWidget ( _gpg_selection = new FriendSelectionWidget ( this ) ) ;
QObject : : connect ( _link_type_CB , SIGNAL ( currentIndexChanged ( int ) ) , this , SLOT ( update ( ) ) ) ;
2013-04-13 18:40:17 -04:00
QObject : : connect ( _create_link_PB , SIGNAL ( clicked ( ) ) , this , SLOT ( createLink ( ) ) ) ;
QObject : : connect ( _create_new_PB , SIGNAL ( toggled ( bool ) ) , this , SLOT ( toggleCreateLink ( bool ) ) ) ;
2013-04-14 17:45:02 -04:00
QObject : : connect ( _existing_links_LW , SIGNAL ( currentRowChanged ( int ) ) , this , SLOT ( updateCurrentRow ( int ) ) ) ;
2013-04-13 09:12:13 -04:00
2013-04-13 18:40:17 -04:00
_gpg_selection - > setModus ( FriendSelectionWidget : : MODUS_SINGLE ) ;
_gpg_selection - > setShowType ( FriendSelectionWidget : : SHOW_NON_FRIEND_GPG | FriendSelectionWidget : : SHOW_GPG ) ;
_gpg_selection - > setHeaderText ( QObject : : tr ( " Select who can contact you: " ) ) ;
_gpg_selection - > start ( ) ;
toggleCreateLink ( false ) ;
2013-04-13 09:12:13 -04:00
update ( ) ;
2013-04-14 17:45:02 -04:00
updateCurrentRow ( - 1 ) ;
}
void CreateMsgLinkDialog : : updateCurrentRow ( int r )
{
if ( r < 0 )
{
_current_link_type_LE - > setText ( " " ) ;
_current_link_dst_LE - > setText ( " " ) ;
2013-06-08 08:54:24 -04:00
_current_link_date_DE - > setDateTime ( QDateTime : : fromTime_t ( 0 ) ) ;
2013-04-14 17:45:02 -04:00
return ;
}
QUrl text = _existing_links_LW - > item ( r ) - > data ( Qt : : UserRole ) . toUrl ( ) ;
std : : cerr < < " Parsing link : " < < text . toString ( ) . toStdString ( ) < < std : : endl ;
RetroShareLink link ( text ) ;
2013-06-11 17:26:12 -04:00
RsPeerDetails detail ;
rsPeers - > getPeerDetails ( link . GPGId ( ) . toStdString ( ) , detail ) ;
2013-04-14 17:45:02 -04:00
if ( link . type ( ) = = RetroShareLink : : TYPE_PRIVATE_CHAT )
2013-06-11 17:26:12 -04:00
{
2013-04-14 17:45:02 -04:00
_current_link_type_LE - > setText ( tr ( " Private chat invite " ) ) ;
2013-06-11 17:26:12 -04:00
_usable_LB - > setText ( tr ( " Usable only by : " ) ) ;
}
2013-04-14 17:45:02 -04:00
else
2013-06-11 17:26:12 -04:00
{
2013-04-14 17:45:02 -04:00
_current_link_type_LE - > setText ( tr ( " Public message invite " ) ) ;
2013-06-11 17:26:12 -04:00
_usable_LB - > setText ( tr ( " Usable to contact : " ) ) ;
}
2013-04-14 17:45:02 -04:00
2013-06-11 17:26:12 -04:00
_current_link_dst_LE - > setText ( QString : : fromStdString ( detail . name ) + " ( " + link . GPGId ( ) + " ) " ) ;
2013-06-08 08:54:24 -04:00
_current_link_date_DE - > setDateTime ( QDateTime : : fromTime_t ( link . timeStamp ( ) ) ) ;
2013-04-13 09:12:13 -04:00
}
2013-04-13 18:40:17 -04:00
void CreateMsgLinkDialog : : toggleCreateLink ( bool b )
{
_new_link_F - > setHidden ( ! b ) ;
}
2013-04-13 09:12:13 -04:00
void CreateMsgLinkDialog : : update ( )
{
if ( _link_type_CB - > currentIndex ( ) = = 0 )
{
QString s ;
2013-04-13 18:40:17 -04:00
s + = " A private chat invite allows a specific peer to contact you using encrypted private chat. You need to select a destination peer from your PGP keyring before creating the link. The link contains the encryption code and your PGP signature, so that the peer can authenticate you. " ;
2013-04-13 09:12:13 -04:00
2013-04-13 18:40:17 -04:00
_info_TB - > setHtml ( s ) ;
2013-04-13 09:12:13 -04:00
_gpg_selection - > setHidden ( false ) ;
}
else
{
QString s ;
2013-04-13 18:40:17 -04:00
s + = " A public message link allows any peer in the nearby network to send a private message to you. The message is encrypted and only you can read it. " ;
2013-04-13 09:12:13 -04:00
2013-04-13 18:40:17 -04:00
_info_TB - > setHtml ( s ) ;
2013-04-13 09:12:13 -04:00
_gpg_selection - > setHidden ( true ) ;
}
2013-04-14 11:05:24 -04:00
std : : vector < DistantChatInviteInfo > invites ;
rsMsgs - > getDistantChatInviteList ( invites ) ;
_existing_links_LW - > clear ( ) ;
for ( uint32_t i = 0 ; i < invites . size ( ) ; + + i )
{
RetroShareLink link ;
2013-06-10 09:27:24 -04:00
if ( ! link . createPrivateChatInvite ( invites [ i ] . time_of_validity , QString : : fromStdString ( invites [ i ] . destination_pgp_id ) , QString : : fromStdString ( invites [ i ] . encrypted_radix64_string ) ) ) {
2013-04-14 11:05:24 -04:00
std : : cerr < < " Cannot create link. " < < std : : endl ;
2013-06-10 09:27:24 -04:00
continue ;
}
2013-04-14 11:05:24 -04:00
2013-06-11 17:26:12 -04:00
RsPeerDetails detail ;
rsPeers - > getPeerDetails ( link . GPGId ( ) . toStdString ( ) , detail ) ;
2013-04-14 17:45:02 -04:00
QListWidgetItem * item = new QListWidgetItem ;
2013-06-11 17:26:12 -04:00
item - > setData ( Qt : : DisplayRole , tr ( " Private chat invite to " ) + QString : : fromStdString ( detail . name ) + " ( " + QString : : fromStdString ( invites [ i ] . destination_pgp_id ) + " ) " ) ;
2013-04-14 17:45:02 -04:00
item - > setData ( Qt : : UserRole , link . toString ( ) ) ;
2013-04-14 11:05:24 -04:00
_existing_links_LW - > insertItem ( 0 , item ) ;
}
2013-04-29 16:44:48 -04:00
std : : vector < DistantOfflineMessengingInvite > invites2 ;
rsMsgs - > getDistantOfflineMessengingInvites ( invites2 ) ;
for ( uint32_t i = 0 ; i < invites2 . size ( ) ; + + i )
{
RetroShareLink link ;
if ( ! link . createPublicMsgInvite ( invites2 [ i ] . time_of_validity , QString : : fromStdString ( invites2 [ i ] . issuer_pgp_id ) , QString : : fromStdString ( invites2 [ i ] . hash ) ) )
std : : cerr < < " Cannot create link. " < < std : : endl ;
else
{
QListWidgetItem * item = new QListWidgetItem ;
2013-05-05 15:13:27 -04:00
item - > setData ( Qt : : DisplayRole , tr ( " Public message link " ) ) ;
2013-04-29 16:44:48 -04:00
item - > setData ( Qt : : UserRole , link . toString ( ) ) ;
_existing_links_LW - > insertItem ( 0 , item ) ;
}
}
2013-04-13 09:12:13 -04:00
}
2013-04-13 18:40:17 -04:00
time_t CreateMsgLinkDialog : : computeValidityDuration ( ) const
{
time_t unit ;
switch ( _validity_time_CB - > currentIndex ( ) )
{
default :
case 0 : unit = 3600 ;
break ;
case 1 : unit = 3600 * 24 ;
break ;
case 2 : unit = 3600 * 24 * 7 ;
break ;
case 3 : unit = 3600 * 24 * 30 ;
break ;
case 4 : unit = 3600 * 24 * 365 ;
break ;
}
return unit * _validity_time_SB - > value ( ) ;
}
2013-04-13 09:12:13 -04:00
void CreateMsgLinkDialog : : createLink ( )
{
std : : cerr < < " Creating link! " < < std : : endl ;
2013-04-13 18:40:17 -04:00
if ( _link_type_CB - > currentIndex ( ) = = 0 )
{
time_t validity_duration = computeValidityDuration ( ) ;
FriendSelectionWidget : : IdType type ;
std : : string current_pgp_id = _gpg_selection - > selectedId ( type ) ;
std : : string encrypted_string ;
bool res = rsMsgs - > createDistantChatInvite ( current_pgp_id , validity_duration , encrypted_string ) ;
RetroShareLink link ;
if ( ! link . createPrivateChatInvite ( validity_duration + time ( NULL ) , QString : : fromStdString ( current_pgp_id ) , QString : : fromStdString ( encrypted_string ) ) )
std : : cerr < < " Cannot create link. " < < std : : endl ;
QList < RetroShareLink > links ;
links . push_back ( link ) ;
RSLinkClipboard : : copyLinks ( links ) ;
if ( ! res )
QMessageBox : : critical ( NULL , tr ( " Private chat invite creation failed " ) , tr ( " The creation of the chat invite failed " ) ) ;
else
QMessageBox : : information ( NULL , tr ( " Private chat invite created " ) , tr ( " Your new chat invite has been copied to clipboard. You can now paste it as a Retroshare link. " ) ) ;
}
else
{
2013-04-29 16:44:48 -04:00
time_t validity_duration = computeValidityDuration ( ) ;
std : : string hash ;
std : : string issuer_pgp_id = rsPeers - > getGPGOwnId ( ) ;
bool res = rsMsgs - > createDistantOfflineMessengingInvite ( validity_duration , hash ) ;
RetroShareLink link ;
if ( ! link . createPublicMsgInvite ( validity_duration + time ( NULL ) , QString : : fromStdString ( issuer_pgp_id ) , QString : : fromStdString ( hash ) ) )
{
std : : cerr < < " Cannot create link. " < < std : : endl ;
return ;
}
QList < RetroShareLink > links ;
links . push_back ( link ) ;
RSLinkClipboard : : copyLinks ( links ) ;
if ( ! res )
QMessageBox : : critical ( NULL , tr ( " Messenging invite creation failed " ) , tr ( " The creation of the messenging invite failed " ) ) ;
else
QMessageBox : : information ( NULL , tr ( " Messenging invite created " ) , tr ( " Your new messenging chat invite has been copied to clipboard. You can now paste it as a Retroshare link. " ) ) ;
2013-04-13 18:40:17 -04:00
}
2013-04-14 17:45:02 -04:00
2013-04-29 16:44:48 -04:00
QTimer : : singleShot ( 100 , this , SLOT ( update ( ) ) ) ;
2013-04-13 09:12:13 -04:00
}
2013-04-13 18:40:17 -04:00