2018-12-25 15:34:59 -05: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 15:16:56 -05:00
# include "HomePage.h"
# include "ui_HomePage.h"
2018-09-18 15:20:55 -04:00
# include "retroshare/rsinit.h"
2016-12-12 15:16:56 -05:00
# include "gui/notifyqt.h"
# include "gui/msgs/MessageComposer.h"
2016-12-14 09:20:03 -05:00
# include "gui/connect/ConnectFriendWizard.h"
2018-09-17 17:03:11 -04:00
# include "gui/connect/ConfCertDialog.h"
2017-03-01 14:36:36 -05:00
# include <gui/QuickStartWizard.h>
2017-01-22 15:06:11 -05:00
# include "gui/connect/FriendRecommendDialog.h"
2016-12-12 15:16:56 -05: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 ) :
MainPage ( parent ) ,
2018-11-02 15:26:49 -04:00
ui ( new Ui : : HomePage ) ,
2019-06-06 15:41:38 -04:00
mIncludeAllIPs ( false ) ,
2019-09-27 14:21:40 -04:00
mUseShortFormat ( false )
2016-12-12 15:16:56 -05:00
{
ui - > setupUi ( this ) ;
2018-11-01 15:28:09 -04:00
updateOwnCert ( ) ;
2019-09-27 14:21:40 -04:00
updateOwnId ( ) ;
2016-12-12 15:16:56 -05:00
2016-12-14 09:20:03 -05:00
connect ( ui - > addButton , SIGNAL ( clicked ( ) ) , this , SLOT ( addFriend ( ) ) ) ;
2019-09-27 14:21:40 -04:00
connect ( ui - > expandButton , SIGNAL ( clicked ( ) ) , this , SLOT ( doExpand ( ) ) ) ;
2017-03-03 16:12:36 -05:00
2017-03-03 14:31:23 -05:00
QAction * WebMailAction = new QAction ( QIcon ( ) , tr ( " Invite via WebMail " ) , this ) ;
connect ( WebMailAction , SIGNAL ( triggered ( ) ) , this , SLOT ( webMail ( ) ) ) ;
2017-01-22 15:06:11 -05:00
QAction * RecAction = new QAction ( QIcon ( ) , tr ( " Recommend friends to each others " ) , this ) ;
connect ( RecAction , SIGNAL ( triggered ( ) ) , this , SLOT ( recommendFriends ( ) ) ) ;
2018-09-18 15:20:55 -04:00
QAction * SendAction = new QAction ( QIcon ( ) , tr ( " Send via Email " ) , this ) ;
connect ( SendAction , SIGNAL ( triggered ( ) ) , this , SLOT ( runEmailClient ( ) ) ) ;
2019-09-27 14:21:40 -04:00
QAction * CopyIdAction = new QAction ( QIcon ( ) , tr ( " Copy your Retroshare ID to Clipboard " ) , this ) ;
connect ( CopyIdAction , SIGNAL ( triggered ( ) ) , this , SLOT ( copyId ( ) ) ) ;
2018-09-18 15:20:55 -04:00
2016-12-12 15:16:56 -05:00
QMenu * menu = new QMenu ( ) ;
2019-09-27 14:21:40 -04:00
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 ) ;
}
menu - > addSeparator ( ) ;
menu - > addAction ( SendAction ) ;
2017-03-03 14:31:23 -05:00
menu - > addAction ( WebMailAction ) ;
2017-01-22 15:06:11 -05:00
menu - > addAction ( RecAction ) ;
2016-12-12 15:16:56 -05:00
ui - > shareButton - > setMenu ( menu ) ;
2018-09-18 15:20:55 -04:00
QObject : : connect ( ui - > userCertEdit , SIGNAL ( customContextMenuRequested ( QPoint ) ) , this , SLOT ( certContextMenu ( QPoint ) ) ) ;
2017-03-01 14:36:36 -05:00
connect ( ui - > openwebhelp , SIGNAL ( clicked ( ) ) , this , SLOT ( openWebHelp ( ) ) ) ;
2017-03-03 16:41:32 -05:00
2019-09-27 14:21:40 -04:00
ui - > userCertEdit - > hide ( ) ;
2017-03-01 14:36:36 -05:00
2017-02-22 17:55:29 -05:00
int S = QFontMetricsF ( font ( ) ) . height ( ) ;
QString help_str = tr (
" <h1><img width= \" %1 \" src= \" :/icons/help_64.png \" > Welcome to Retroshare!</h1> \
2017-07-30 08:38:17 -04:00
< p > You need to < b > make friends < / b > ! After you create a network of friends or join an existing network , \
2017-02-22 17:55:29 -05:00
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 > \
2017-07-30 08:38:17 -04:00
< p > To do so , copy your certificate on this page and send it to friends , and add your friends ' certificate . < / p > \
2017-02-22 17:55:29 -05:00
< p > Another option is to search the internet for \ " Retroshare chat servers \" (independently administrated). These servers allow you to exchange \
certificates with a dedicated Retroshare node , through which \
2017-07-30 08:38:17 -04:00
you will be able to anonymously meet other people . < / p > " ).arg(QString::number(2*S)).arg(width()*0.5);
2017-02-25 17:52:57 -05:00
registerHelpButton ( ui - > helpButton , help_str , " HomePage " ) ;
2016-12-12 15:16:56 -05:00
}
2018-09-18 15:20:55 -04:00
void HomePage : : certContextMenu ( QPoint point )
{
QMenu menu ( this ) ;
QAction * CopyAction = new QAction ( QIcon ( ) , tr ( " Copy your Cert to Clipboard " ) , this ) ;
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 ) ;
2019-06-06 15:41:38 -04: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 ) ;
menu . addAction ( shortFormatAct ) ;
2018-09-18 15:20:55 -04:00
if ( ! RsAccounts : : isHiddenNode ( ) )
{
2019-06-06 15:41:38 -04:00
QAction * includeIPsAct = new QAction ( QIcon ( ) , tr ( " Include all your known IPs " ) , this ) ;
2018-09-18 15:20:55 -04:00
connect ( includeIPsAct , SIGNAL ( triggered ( ) ) , this , SLOT ( toggleIncludeAllIPs ( ) ) ) ;
includeIPsAct - > setCheckable ( true ) ;
2019-06-06 15:41:38 -04:00
includeIPsAct - > setChecked ( mIncludeAllIPs ) ;
2018-09-18 15:20:55 -04:00
menu . addAction ( includeIPsAct ) ;
}
menu . exec ( QCursor : : pos ( ) ) ;
}
2019-06-06 15:41:38 -04:00
void HomePage : : toggleUseShortFormat ( )
{
mUseShortFormat = ! mUseShortFormat ;
updateOwnCert ( ) ;
}
2018-09-18 15:20:55 -04:00
void HomePage : : toggleIncludeAllIPs ( )
{
mIncludeAllIPs = ! mIncludeAllIPs ;
updateOwnCert ( ) ;
2019-09-27 14:21:40 -04:00
updateOwnId ( ) ;
2018-09-18 15:20:55 -04:00
}
2016-12-12 15:16:56 -05:00
HomePage : : ~ HomePage ( )
{
delete ui ;
}
void HomePage : : updateOwnCert ( )
{
2018-09-18 15:20:55 -04:00
bool include_extra_locators = mIncludeAllIPs ;
2018-09-17 17:03:11 -04: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 ;
}
2019-06-06 15:41:38 -04:00
std : : string invite ;
if ( mUseShortFormat )
rsPeers - > getShortInvite ( invite , rsPeers - > getOwnId ( ) , true , ! mIncludeAllIPs ) ;
else
invite = rsPeers - > GetRetroshareInvite ( detail . id , false , include_extra_locators ) ;
2016-12-12 15:16:56 -05:00
ui - > userCertEdit - > setPlainText ( QString : : fromUtf8 ( invite . c_str ( ) ) ) ;
2018-09-17 17:03:11 -04:00
2019-06-06 15:41:38 -04:00
QString description = ConfCertDialog : : getCertificateDescription ( detail , false , mUseShortFormat , include_extra_locators ) ;
2018-09-17 17:03:11 -04:00
ui - > userCertEdit - > setToolTip ( description ) ;
2016-12-12 15:16:56 -05:00
}
2019-09-27 14:21:40 -04:00
void HomePage : : updateOwnId ( )
{
bool include_extra_locators = mIncludeAllIPs ;
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 ;
}
std : : string invite ;
rsPeers - > getShortInvite ( invite , rsPeers - > getOwnId ( ) , true , ! mIncludeAllIPs ) ;
ui - > retroshareid - > setText ( QString : : fromUtf8 ( invite . c_str ( ) ) ) ;
}
2016-12-12 15:16:56 -05:00
static void sendMail ( QString sAddress , QString sSubject , QString sBody )
{
# ifdef Q_OS_WIN
/* search and replace the end of lines with: "%0D%0A" */
sBody . replace ( " \n " , " %0D%0A " ) ;
# endif
QUrl url = QUrl ( " mailto: " + sAddress ) ;
# if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
QUrlQuery urlQuery ;
# else
QUrl & urlQuery ( url ) ;
# endif
urlQuery . addQueryItem ( " subject " , sSubject ) ;
urlQuery . addQueryItem ( " body " , sBody ) ;
# if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
url . setQuery ( urlQuery ) ;
# endif
std : : cerr < < " MAIL STRING: " < < ( std : : string ) url . toEncoded ( ) . constData ( ) < < std : : endl ;
/* pass the url directly to QDesktopServices::openUrl */
QDesktopServices : : openUrl ( url ) ;
}
2017-01-22 15:06:11 -05:00
void HomePage : : recommendFriends ( )
{
FriendRecommendDialog : : showIt ( ) ;
}
2016-12-12 15:16:56 -05:00
void HomePage : : runEmailClient ( )
{
sendMail ( " " , tr ( " RetroShare Invite " ) , ui - > userCertEdit - > toPlainText ( ) ) ;
}
void HomePage : : copyCert ( )
{
QClipboard * clipboard = QApplication : : clipboard ( ) ;
clipboard - > setText ( ui - > userCertEdit - > toPlainText ( ) ) ;
2019-09-27 14:21:40 -04:00
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 " ) ) ;
}
void HomePage : : copyId ( )
{
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 15:16:56 -05:00
}
void HomePage : : saveCert ( )
{
QString fileName = QFileDialog : : getSaveFileName ( this , tr ( " Save as... " ) , " " , tr ( " RetroShare Certificate (*.rsc );;All Files (*) " ) ) ;
if ( fileName . isEmpty ( ) )
return ;
QFile file ( fileName ) ;
if ( ! file . open ( QFile : : WriteOnly ) )
return ;
//Todo: move save to file to p3Peers::SaveCertificateToFile
QTextStream ts ( & file ) ;
ts . setCodec ( QTextCodec : : codecForName ( " UTF-8 " ) ) ;
ts < < ui - > userCertEdit - > document ( ) - > toPlainText ( ) ;
}
2016-12-14 09:20:03 -05:00
/** Add a Friends Text Certificate */
void HomePage : : addFriend ( )
{
ConnectFriendWizard connwiz ( this ) ;
connwiz . setStartId ( ConnectFriendWizard : : Page_Text ) ;
connwiz . exec ( ) ;
}
2017-03-01 14:36:36 -05:00
2017-03-03 14:31:23 -05:00
void HomePage : : webMail ( )
{
ConnectFriendWizard connwiz ( this ) ;
connwiz . setStartId ( ConnectFriendWizard : : Page_WebMail ) ;
connwiz . exec ( ) ;
}
2019-06-09 18:03:07 -04:00
// void HomePage::loadCert()
// {
// ConnectFriendWizard connwiz (this);
//
// connwiz.setStartId(ConnectFriendWizard::Page_Cert);
// connwiz.exec ();
// }
2017-03-01 14:36:36 -05:00
void HomePage : : openWebHelp ( )
{
QDesktopServices : : openUrl ( QUrl ( QString ( " https://retroshare.readthedocs.io " ) ) ) ;
}
2019-09-27 14:21:40 -04:00
void HomePage : : doExpand ( )
{
if ( ui - > expandButton - > isChecked ( ) )
{
ui - > userCertEdit - > show ( ) ;
ui - > expandButton - > setToolTip ( tr ( " Hide " ) ) ;
}
else
{
ui - > userCertEdit - > hide ( ) ;
2019-09-28 06:18:46 -04:00
ui - > expandButton - > setToolTip ( tr ( " Show full certificate (old format) " ) ) ;
2019-09-27 14:21:40 -04:00
}
}