2018-12-25 21:34:59 +01:00
/*******************************************************************************
* gui / HomePage . cpp *
* *
* Copyright ( C ) 2016 Defnax < 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/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2016-12-12 21:16:56 +01:00
# include "HomePage.h"
# include "ui_HomePage.h"
2018-09-18 21:20:55 +02:00
# include "retroshare/rsinit.h"
2020-11-09 22:42:28 +01:00
# include "util/qtthreadsutils.h"
2016-12-12 21:16:56 +01:00
# include "gui/notifyqt.h"
# include "gui/msgs/MessageComposer.h"
2016-12-14 15:20:03 +01:00
# include "gui/connect/ConnectFriendWizard.h"
2018-09-17 23:03:11 +02:00
# include "gui/connect/ConfCertDialog.h"
2017-03-01 22:36:36 +03:00
# include <gui/QuickStartWizard.h>
2017-01-22 21:06:11 +01:00
# include "gui/connect/FriendRecommendDialog.h"
2016-12-12 21:16:56 +01:00
# if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
# include <QUrlQuery>
# endif
# include <iostream>
# include <string>
# include <QTime>
# include <QMenu>
# include <QDesktopServices>
# include <QMessageBox>
# include <QFileDialog>
# include <QClipboard>
# include <QTextStream>
# include <QTextCodec>
HomePage : : HomePage ( QWidget * parent ) :
2020-10-18 21:20:22 +02:00
MainPage ( parent ) ,
ui ( new Ui : : HomePage ) ,
2019-06-06 21:41:38 +02:00
mIncludeAllIPs ( false ) ,
2020-11-09 22:42:28 +01:00
mUseShortFormat ( true )
2016-12-12 21:16:56 +01:00
{
ui - > setupUi ( this ) ;
2020-10-18 21:20:22 +02:00
updateCertificate ( ) ;
connect ( ui - > addButton , SIGNAL ( clicked ( ) ) , this , SLOT ( addFriend ( ) ) ) ;
2021-02-04 21:50:30 +01:00
connect ( ui - > copyIDButton , SIGNAL ( clicked ( ) ) , this , SLOT ( copyId ( ) ) ) ;
2020-10-18 21:20:22 +02:00
2017-03-03 22:31:23 +03:00
QAction * WebMailAction = new QAction ( QIcon ( ) , tr ( " Invite via WebMail " ) , this ) ;
connect ( WebMailAction , SIGNAL ( triggered ( ) ) , this , SLOT ( webMail ( ) ) ) ;
2020-10-18 21:20:22 +02:00
2017-01-22 21:06:11 +01:00
QAction * RecAction = new QAction ( QIcon ( ) , tr ( " Recommend friends to each others " ) , this ) ;
connect ( RecAction , SIGNAL ( triggered ( ) ) , this , SLOT ( recommendFriends ( ) ) ) ;
2018-09-18 21:20:55 +02:00
QAction * SendAction = new QAction ( QIcon ( ) , tr ( " Send via Email " ) , this ) ;
connect ( SendAction , SIGNAL ( triggered ( ) ) , this , SLOT ( runEmailClient ( ) ) ) ;
2020-10-18 21:20:22 +02:00
QAction * CopyIdAction = new QAction ( QIcon ( ) , tr ( " Copy your Retroshare ID to Clipboard " ) , this ) ;
2019-09-27 20:21:40 +02:00
connect ( CopyIdAction , SIGNAL ( triggered ( ) ) , this , SLOT ( copyId ( ) ) ) ;
2018-09-18 21:20:55 +02:00
2020-10-18 21:20:22 +02:00
QMenu * menu = new QMenu ( ) ;
menu - > addAction ( CopyIdAction ) ;
if ( ! RsAccounts : : isHiddenNode ( ) )
{
QAction * includeIPsAct = new QAction ( QIcon ( ) , tr ( " Include all your known IPs " ) , this ) ;
connect ( includeIPsAct , SIGNAL ( triggered ( ) ) , this , SLOT ( toggleIncludeAllIPs ( ) ) ) ;
includeIPsAct - > setCheckable ( true ) ;
includeIPsAct - > setChecked ( mIncludeAllIPs ) ;
menu - > addAction ( includeIPsAct ) ;
}
QAction * useOldFormatAct = new QAction ( QIcon ( ) , tr ( " Use old certificate format " ) , this ) ;
useOldFormatAct - > setToolTip ( tr ( " Displays the certificate format used up to version 0.6.5 \n Old Retroshare nodes will not understand the \n new short format " ) ) ;
connect ( useOldFormatAct , SIGNAL ( triggered ( ) ) , this , SLOT ( toggleUseOldFormat ( ) ) ) ;
useOldFormatAct - > setCheckable ( true ) ;
2020-11-09 22:42:28 +01:00
useOldFormatAct - > setChecked ( ! mUseShortFormat ) ;
2020-10-18 21:20:22 +02:00
menu - > addAction ( useOldFormatAct ) ;
menu - > addSeparator ( ) ;
menu - > addAction ( SendAction ) ;
menu - > addAction ( WebMailAction ) ;
2017-01-22 21:06:11 +01:00
menu - > addAction ( RecAction ) ;
2016-12-12 21:16:56 +01:00
ui - > shareButton - > setMenu ( menu ) ;
2020-10-18 21:20:22 +02:00
connect ( ui - > openwebhelp , SIGNAL ( clicked ( ) ) , this , SLOT ( openWebHelp ( ) ) ) ;
2017-03-03 22:41:32 +01:00
2017-02-22 23:55:29 +01:00
int S = QFontMetricsF ( font ( ) ) . height ( ) ;
2020-11-09 22:42:28 +01:00
QString help_str = tr (
" <h1><img width= \" %1 \" src= \" :/icons/help_64.png \" > Welcome to Retroshare!</h1> \
< p > You need to < b > make friends < / b > ! After you create a network of friends or join an existing network , \
you ' ll be able to exchange files , chat , talk in forums , etc . < / p > \
< div align = center > \
< IMG align = \ " center \" width= \" %2 \" src= \" :/images/network_map.png \" /> \
< / div > \
2021-02-06 23:08:38 +01:00
< p > To do so , copy your Retroshare ID on this page and send it to friends , and add your friends ' Retroshare ID . < / p > \
2020-11-09 22:42:28 +01:00
< p > Another option is to search the internet for \ " Retroshare chat servers \" (independently administrated). These servers allow you to exchange \
2021-02-06 23:08:38 +01:00
Retroshare ID with a dedicated Retroshare node , through which \
2020-11-09 22:42:28 +01:00
you will be able to anonymously meet other people . < / p > " ).arg(QString::number(2*S)).arg(width()*0.5);
registerHelpButton ( ui - > helpButton , help_str , " HomePage " ) ;
// register a event handler to catch IP updates
mEventHandlerId = 0 ;
rsEvents - > registerEventsHandler ( [ this ] ( std : : shared_ptr < const RsEvent > event ) { handleEvent ( event ) ; } , mEventHandlerId , RsEventType : : NETWORK ) ;
2016-12-12 21:16:56 +01:00
}
2020-11-09 22:42:28 +01:00
void HomePage : : handleEvent ( std : : shared_ptr < const RsEvent > e )
{
if ( e - > mType ! = RsEventType : : NETWORK )
return ;
const RsNetworkEvent * ne = dynamic_cast < const RsNetworkEvent * > ( e . get ( ) ) ;
2020-11-11 22:43:07 +01:00
if ( ! ne )
2020-11-09 22:42:28 +01:00
return ;
// in any case we update the IPs
switch ( ne - > mNetworkEventCode )
{
case RsNetworkEventCode : : LOCAL_IP_UPDATED : // [fallthrough]
case RsNetworkEventCode : : EXTERNAL_IP_UPDATED : // [fallthrough]
RsQThreadUtils : : postToObject ( [ = ] ( )
{
updateCertificate ( ) ;
} , this ) ;
break ;
default :
break ;
}
}
2018-09-18 21:20:55 +02:00
void HomePage : : certContextMenu ( QPoint point )
{
QMenu menu ( this ) ;
2020-10-18 21:20:22 +02:00
QAction * CopyAction = new QAction ( QIcon ( ) , tr ( " Copy your Cert to Clipboard " ) , this ) ;
2018-09-18 21:20:55 +02:00
connect ( CopyAction , SIGNAL ( triggered ( ) ) , this , SLOT ( copyCert ( ) ) ) ;
QAction * SaveAction = new QAction ( QIcon ( ) , tr ( " Save your Cert into a File " ) , this ) ;
connect ( SaveAction , SIGNAL ( triggered ( ) ) , this , SLOT ( saveCert ( ) ) ) ;
menu . addAction ( CopyAction ) ;
menu . addAction ( SaveAction ) ;
2020-10-18 21:20:22 +02:00
QAction * shortFormatAct = new QAction ( QIcon ( ) , tr ( " Use new (short) certificate format " ) , this ) ;
connect ( shortFormatAct , SIGNAL ( triggered ( ) ) , this , SLOT ( toggleUseShortFormat ( ) ) ) ;
shortFormatAct - > setCheckable ( true ) ;
shortFormatAct - > setChecked ( mUseShortFormat ) ;
2019-06-06 21:41:38 +02:00
2020-10-18 21:20:22 +02:00
menu . addAction ( shortFormatAct ) ;
2019-06-06 21:41:38 +02:00
2018-09-18 21:20:55 +02:00
if ( ! RsAccounts : : isHiddenNode ( ) )
2020-10-18 21:20:22 +02:00
{
QAction * includeIPsAct = new QAction ( QIcon ( ) , tr ( " Include all your known IPs " ) , this ) ;
connect ( includeIPsAct , SIGNAL ( triggered ( ) ) , this , SLOT ( toggleIncludeAllIPs ( ) ) ) ;
includeIPsAct - > setCheckable ( true ) ;
includeIPsAct - > setChecked ( mIncludeAllIPs ) ;
2018-09-18 21:20:55 +02:00
2020-10-18 21:20:22 +02:00
menu . addAction ( includeIPsAct ) ;
}
2018-09-18 21:20:55 +02:00
menu . exec ( QCursor : : pos ( ) ) ;
}
2019-06-06 21:41:38 +02:00
void HomePage : : toggleUseShortFormat ( )
{
mUseShortFormat = ! mUseShortFormat ;
2020-10-18 21:20:22 +02:00
updateCertificate ( ) ;
2019-06-06 21:41:38 +02:00
}
2018-09-18 21:20:55 +02:00
void HomePage : : toggleIncludeAllIPs ( )
{
mIncludeAllIPs = ! mIncludeAllIPs ;
2020-10-18 21:20:22 +02:00
updateCertificate ( ) ;
2018-09-18 21:20:55 +02:00
}
2016-12-12 21:16:56 +01:00
HomePage : : ~ HomePage ( )
{
2021-02-08 22:35:32 +01:00
rsEvents - > unregisterEventsHandler ( mEventHandlerId ) ;
2020-10-18 21:20:22 +02:00
delete ui ;
}
void HomePage : : updateCertificate ( )
{
2020-11-09 22:42:28 +01:00
updateOwnCert ( ) ;
2016-12-12 21:16:56 +01:00
}
void HomePage : : updateOwnCert ( )
{
2018-09-18 21:20:55 +02:00
bool include_extra_locators = mIncludeAllIPs ;
2018-09-17 23:03:11 +02:00
RsPeerDetails detail ;
if ( ! rsPeers - > getPeerDetails ( rsPeers - > getOwnId ( ) , detail ) )
{
std : : cerr < < " (EE) Cannot retrieve information about own certificate. That is a real problem!! " < < std : : endl ;
return ;
}
2020-11-09 22:42:28 +01:00
QString invite ;
RetroshareInviteFlags invite_flags = RetroshareInviteFlags : : CURRENT_IP ;
2019-06-06 21:41:38 +02:00
2020-11-09 22:42:28 +01:00
if ( mIncludeAllIPs )
invite_flags | = RetroshareInviteFlags : : FULL_IP_HISTORY ;
2016-12-12 21:16:56 +01:00
2020-11-09 22:42:28 +01:00
if ( mUseShortFormat )
{
std : : string short_invite ;
rsPeers - > getShortInvite ( short_invite , rsPeers - > getOwnId ( ) , invite_flags | RetroshareInviteFlags : : RADIX_FORMAT ) ;
2018-09-17 23:03:11 +02:00
2020-11-09 22:42:28 +01:00
QString S ;
QString txt ;
2018-09-17 23:03:11 +02:00
2020-11-09 22:42:28 +01:00
for ( uint32_t i = 0 ; i < short_invite . size ( ) ; )
if ( S . length ( ) < 100 )
S + = short_invite [ i + + ] ;
else
{
txt + = S + " \n " ;
S . clear ( ) ;
}
2016-12-12 21:16:56 +01:00
2020-11-09 22:42:28 +01:00
txt + = S ;
2019-09-27 20:21:40 +02:00
2020-11-09 22:42:28 +01:00
invite = txt ; // the "\n" is here to make some space
2019-09-27 20:21:40 +02:00
}
2020-11-09 22:42:28 +01:00
else
invite = QString : : fromStdString ( rsPeers - > GetRetroshareInvite ( detail . id , invite_flags ) ) ;
2019-09-27 20:21:40 +02:00
2020-11-09 22:42:28 +01:00
ui - > retroshareid - > setText ( " \n " + invite + " \n " ) ;
2020-10-10 20:19:05 +02:00
2020-11-09 22:42:28 +01:00
QString description = ConfCertDialog : : getCertificateDescription ( detail , false , mUseShortFormat , include_extra_locators ) ;
2020-10-10 20:19:05 +02:00
2020-11-09 22:42:28 +01:00
ui - > retroshareid - > setToolTip ( description ) ;
2019-09-27 20:21:40 +02:00
}
2020-11-09 22:42:28 +01:00
2016-12-12 21:16:56 +01:00
static void sendMail ( QString sAddress , QString sSubject , QString sBody )
{
# ifdef Q_OS_WIN
2020-10-18 21:20:22 +02:00
/* search and replace the end of lines with: "%0D%0A" */
sBody . replace ( " \n " , " %0D%0A " ) ;
2016-12-12 21:16:56 +01:00
# endif
2020-10-18 21:20:22 +02:00
QUrl url = QUrl ( " mailto: " + sAddress ) ;
2016-12-12 21:16:56 +01:00
# if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
2020-10-18 21:20:22 +02:00
QUrlQuery urlQuery ;
2016-12-12 21:16:56 +01:00
# else
2020-10-18 21:20:22 +02:00
QUrl & urlQuery ( url ) ;
2016-12-12 21:16:56 +01:00
# endif
2020-10-18 21:20:22 +02:00
urlQuery . addQueryItem ( " subject " , sSubject ) ;
urlQuery . addQueryItem ( " body " , sBody ) ;
2016-12-12 21:16:56 +01:00
# if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
2020-10-18 21:20:22 +02:00
url . setQuery ( urlQuery ) ;
2016-12-12 21:16:56 +01:00
# endif
2020-10-18 21:20:22 +02:00
std : : cerr < < " MAIL STRING: " < < ( std : : string ) url . toEncoded ( ) . constData ( ) < < std : : endl ;
2016-12-12 21:16:56 +01:00
2020-10-18 21:20:22 +02:00
/* pass the url directly to QDesktopServices::openUrl */
QDesktopServices : : openUrl ( url ) ;
2016-12-12 21:16:56 +01:00
}
2017-01-22 21:06:11 +01:00
void HomePage : : recommendFriends ( )
{
FriendRecommendDialog : : showIt ( ) ;
}
2016-12-12 21:16:56 +01:00
void HomePage : : runEmailClient ( )
{
2020-10-18 21:20:22 +02:00
sendMail ( " " , tr ( " RetroShare Invite " ) , ui - > retroshareid - > text ( ) ) ;
2016-12-12 21:16:56 +01:00
}
void HomePage : : copyCert ( )
{
2020-10-18 21:20:22 +02:00
QClipboard * clipboard = QApplication : : clipboard ( ) ;
clipboard - > setText ( ui - > retroshareid - > text ( ) ) ;
QMessageBox : : information ( this , " RetroShare " , tr ( " Your Retroshare certificate is copied to Clipboard, paste and send it to your friend via email or some other way " ) ) ;
2019-09-27 20:21:40 +02:00
}
void HomePage : : copyId ( )
{
2020-10-18 21:20:22 +02:00
QClipboard * clipboard = QApplication : : clipboard ( ) ;
clipboard - > setText ( ui - > retroshareid - > text ( ) ) ;
QMessageBox : : information ( this , " RetroShare " , tr ( " Your Retroshare ID is copied to Clipboard, paste and send it to your friend via email or some other way " ) ) ;
2016-12-12 21:16:56 +01:00
}
void HomePage : : saveCert ( )
{
2020-10-18 21:20:22 +02:00
QString fileName = QFileDialog : : getSaveFileName ( this , tr ( " Save as... " ) , " " , tr ( " RetroShare Certificate (*.rsc );;All Files (*) " ) ) ;
if ( fileName . isEmpty ( ) )
return ;
2016-12-12 21:16:56 +01:00
2020-10-18 21:20:22 +02:00
QFile file ( fileName ) ;
if ( ! file . open ( QFile : : WriteOnly ) )
return ;
2016-12-12 21:16:56 +01:00
2020-10-18 21:20:22 +02:00
//Todo: move save to file to p3Peers::SaveCertificateToFile
2016-12-12 21:16:56 +01:00
2020-10-18 21:20:22 +02:00
QTextStream ts ( & file ) ;
ts . setCodec ( QTextCodec : : codecForName ( " UTF-8 " ) ) ;
ts < < ui - > retroshareid - > text ( ) ;
2016-12-12 21:16:56 +01:00
}
2016-12-14 15:20:03 +01:00
/** Add a Friends Text Certificate */
void HomePage : : addFriend ( )
{
ConnectFriendWizard connwiz ( this ) ;
connwiz . setStartId ( ConnectFriendWizard : : Page_Text ) ;
connwiz . exec ( ) ;
}
2017-03-01 22:36:36 +03:00
2017-03-03 22:31:23 +03:00
void HomePage : : webMail ( )
{
ConnectFriendWizard connwiz ( this ) ;
connwiz . setStartId ( ConnectFriendWizard : : Page_WebMail ) ;
connwiz . exec ( ) ;
}
2019-06-10 00:03:07 +02:00
// void HomePage::loadCert()
// {
// ConnectFriendWizard connwiz (this);
//
// connwiz.setStartId(ConnectFriendWizard::Page_Cert);
// connwiz.exec ();
// }
2017-03-01 22:36:36 +03:00
void HomePage : : openWebHelp ( )
{
2021-02-15 21:17:27 +01:00
QDesktopServices : : openUrl ( QUrl ( QString ( " https://retrosharedocs.readthedocs.io/en/latest/ " ) ) ) ;
2017-03-01 22:36:36 +03:00
}
2019-09-27 20:21:40 +02:00
2020-10-18 21:20:22 +02:00
void HomePage : : toggleUseOldFormat ( )
2019-09-27 20:21:40 +02:00
{
2020-11-09 22:42:28 +01:00
mUseShortFormat = ! mUseShortFormat ;
2020-10-18 21:20:22 +02:00
updateCertificate ( ) ;
2019-09-27 20:21:40 +02:00
}