2007-11-14 22:18:48 -05:00
/****************************************************************
* RetroShare is distributed under the following license :
*
* Copyright ( C ) 2006 , crypton
*
* 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
* Foundation , Inc . , 51 Franklin Street , Fifth Floor ,
* Boston , MA 02110 - 1301 , USA .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include <rshare.h>
2011-01-29 09:27:16 -05:00
# include <util/rsrandom.h>
2010-08-06 05:40:23 -04:00
# include <retroshare/rsinit.h>
2007-11-14 22:18:48 -05:00
# include "GenCertDialog.h"
2009-09-22 13:23:41 -04:00
# include "InfoDialog.h"
2010-05-01 11:17:23 -04:00
# include <QAbstractEventDispatcher>
2007-11-14 22:18:48 -05:00
# include <QFileDialog>
# include <QMessageBox>
2010-01-18 17:02:40 -05:00
# include <QMovie>
2010-01-20 17:02:43 -05:00
# include <time.h>
2010-01-18 17:02:40 -05:00
2007-11-14 22:18:48 -05:00
/* Define the format used for displaying the date and time */
# define DATETIME_FMT "MMM dd hh:mm:ss"
/** Default constructor */
2009-02-08 09:30:28 -05:00
GenCertDialog : : GenCertDialog ( QWidget * parent , Qt : : WFlags flags )
: QDialog ( parent , flags )
2007-11-14 22:18:48 -05:00
{
/* Invoke Qt Designer generated QObject setup routine */
ui . setupUi ( this ) ;
2010-01-19 16:44:13 -05:00
connect ( ui . new_gpg_key_checkbox , SIGNAL ( clicked ( ) ) , this , SLOT ( newGPGKeyGenUiSetup ( ) ) ) ;
2007-11-14 22:18:48 -05:00
connect ( ui . genButton , SIGNAL ( clicked ( ) ) , this , SLOT ( genPerson ( ) ) ) ;
2009-09-22 13:23:41 -04:00
connect ( ui . infopushButton , SIGNAL ( clicked ( ) ) , this , SLOT ( infodlg ( ) ) ) ;
2009-08-04 19:37:01 -04:00
//connect(ui.selectButton, SIGNAL(clicked()), this, SLOT(selectFriend()));
//connect(ui.friendBox, SIGNAL(stateChanged(int)), this, SLOT(checkChanged(int)));
2009-08-18 08:44:17 -04:00
//ui.genName->setFocus(Qt::OtherFocusReason);
2010-06-04 20:44:43 -04:00
QString titleString ( " <span style= \" font-size:17pt; font-weight:500; "
" color:white; \" >%1</span> " ) ;
2009-08-04 19:37:01 -04:00
2012-03-18 08:26:15 -04:00
# if QT_VERSION >= 0x040700
ui . email_input - > setPlaceholderText ( tr ( " [Optional] Visible to your friends, and friends of friends. " ) ) ;
ui . location_input - > setPlaceholderText ( tr ( " [Required] Examples: Home, Laptop,... " ) ) ;
ui . name_input - > setPlaceholderText ( tr ( " [Required] Visible to your friends, and friends of friends. " ) ) ;
ui . password_input - > setPlaceholderText ( tr ( " [Required] This password protects your PGP key. " ) ) ;
# endif
2010-01-13 16:08:46 -05:00
/* get all available pgp private certificates....
* mark last one as default .
*/
std : : cerr < < " Finding PGPUsers " < < std : : endl ;
std : : list < std : : string > pgpIds ;
std : : list < std : : string > : : iterator it ;
2010-01-19 16:44:13 -05:00
bool foundGPGKeys = false ;
2010-01-18 07:30:54 -05:00
if ( RsInit : : GetPGPLogins ( pgpIds ) ) {
2010-01-13 16:08:46 -05:00
for ( it = pgpIds . begin ( ) ; it ! = pgpIds . end ( ) ; it + + )
{
const QVariant & userData = QVariant ( QString : : fromStdString ( * it ) ) ;
std : : string name , email ;
RsInit : : GetPGPLoginDetails ( * it , name , email ) ;
std : : cerr < < " Adding PGPUser: " < < name < < " id: " < < * it < < std : : endl ;
2011-08-27 20:21:34 -04:00
QString gid = QString : : fromStdString ( * it ) . right ( 8 ) ;
ui . genPGPuser - > addItem ( QString : : fromUtf8 ( name . c_str ( ) ) + " < " + QString : : fromUtf8 ( email . c_str ( ) ) + " > ( " + gid + " ) " , userData ) ;
2010-01-18 07:30:54 -05:00
foundGPGKeys = true ;
2010-01-13 16:08:46 -05:00
}
}
2009-08-04 19:37:01 -04:00
2010-01-18 07:30:54 -05:00
if ( foundGPGKeys ) {
ui . no_gpg_key_label - > hide ( ) ;
2010-06-14 09:35:38 -04:00
ui . progressBar - > hide ( ) ;
2010-01-19 16:44:13 -05:00
ui . new_gpg_key_checkbox - > setChecked ( false ) ;
2010-06-04 20:44:43 -04:00
setWindowTitle ( tr ( " Create new Location " ) ) ;
ui . genButton - > setText ( tr ( " Generate new Location " ) ) ;
ui . label_3 - > setText ( titleString . arg ( tr ( " Create a new Location " ) ) ) ;
2010-01-19 16:44:13 -05:00
genNewGPGKey = false ;
2010-01-18 07:30:54 -05:00
} else {
2010-01-19 16:44:13 -05:00
ui . no_gpg_key_label - > show ( ) ;
ui . new_gpg_key_checkbox - > setChecked ( true ) ;
ui . new_gpg_key_checkbox - > hide ( ) ;
2010-06-19 08:47:50 -04:00
ui . progressBar - > hide ( ) ;
2010-06-04 20:44:43 -04:00
setWindowTitle ( tr ( " Create new Profile " ) ) ;
ui . genButton - > setText ( tr ( " Generate new Profile " ) ) ;
ui . label_3 - > setText ( titleString . arg ( tr ( " Create a new Profile " ) ) ) ;
2010-01-19 16:44:13 -05:00
genNewGPGKey = true ;
2010-01-18 07:30:54 -05:00
}
2010-01-19 16:44:13 -05:00
newGPGKeyGenUiSetup ( ) ;
2007-11-14 22:18:48 -05:00
}
2010-01-19 16:44:13 -05:00
void GenCertDialog : : newGPGKeyGenUiSetup ( ) {
2010-06-04 20:44:43 -04:00
QString titleStr ( " <span style= \" font-size:17pt; font-weight:500; "
" color:white; \" >%1</span> " ) ;
2010-01-19 16:44:13 -05:00
if ( ui . new_gpg_key_checkbox - > isChecked ( ) ) {
genNewGPGKey = true ;
ui . name_label - > show ( ) ;
ui . name_input - > show ( ) ;
ui . email_label - > show ( ) ;
ui . email_input - > show ( ) ;
ui . password_label - > show ( ) ;
ui . password_input - > show ( ) ;
ui . genPGPuserlabel - > hide ( ) ;
ui . genPGPuser - > hide ( ) ;
2010-06-04 20:44:43 -04:00
setWindowTitle ( tr ( " Create new Profile " ) ) ;
ui . genButton - > setText ( tr ( " Generate new Profile " ) ) ;
ui . label_3 - > setText ( titleStr . arg ( tr ( " Create a new Profile " ) ) ) ;
2010-01-19 16:44:13 -05:00
} else {
genNewGPGKey = false ;
ui . name_label - > hide ( ) ;
ui . name_input - > hide ( ) ;
ui . email_label - > hide ( ) ;
ui . email_input - > hide ( ) ;
ui . password_label - > hide ( ) ;
ui . password_input - > hide ( ) ;
ui . genPGPuserlabel - > show ( ) ;
ui . genPGPuser - > show ( ) ;
2010-06-04 20:44:43 -04:00
setWindowTitle ( tr ( " Create new Location " ) ) ;
ui . genButton - > setText ( tr ( " Generate new Location " ) ) ;
ui . label_3 - > setText ( titleStr . arg ( tr ( " Create a new Location " ) ) ) ;
2010-01-19 16:44:13 -05:00
}
}
2011-08-12 16:02:00 -04:00
2007-11-14 22:18:48 -05:00
void GenCertDialog : : genPerson ( )
{
/* Check the data from the GUI. */
2011-07-17 19:07:29 -04:00
std : : string genLoc = ui . location_input - > text ( ) . toUtf8 ( ) . constData ( ) ;
2010-01-18 07:30:54 -05:00
std : : string PGPId ;
2009-08-04 19:37:01 -04:00
2010-01-19 16:44:13 -05:00
if ( ! genNewGPGKey ) {
2011-07-17 19:07:29 -04:00
if ( genLoc . length ( ) < 3 ) {
2010-02-14 07:04:44 -05:00
/* Message Dialog */
2012-02-21 14:17:38 -05:00
QMessageBox : : warning ( this ,
2010-02-14 07:04:44 -05:00
tr ( " Generate GPG key Failure " ) ,
tr ( " Location field is required with a minimum of 3 characters " ) ,
QMessageBox : : Ok ) ;
return ;
}
2010-01-18 07:30:54 -05:00
int pgpidx = ui . genPGPuser - > currentIndex ( ) ;
if ( pgpidx < 0 )
{
/* Message Dialog */
2012-02-21 14:17:38 -05:00
QMessageBox : : warning ( this ,
2010-01-18 07:30:54 -05:00
" Generate ID Failure " ,
" Missing PGP Certificate " ,
QMessageBox : : Ok ) ;
return ;
}
QVariant data = ui . genPGPuser - > itemData ( pgpidx ) ;
PGPId = ( data . toString ( ) ) . toStdString ( ) ;
} else {
2010-02-14 07:04:44 -05:00
if ( ui . password_input - > text ( ) . length ( ) < 3 | | ui . name_input - > text ( ) . length ( ) < 3
2012-02-21 14:17:38 -05:00
| | ui . email_input - > text ( ) . length ( ) < 3 | | ui . location_label - > text ( ) . length ( ) < 3 | |
genLoc . length ( ) < 3 ) {
2010-01-24 17:15:02 -05:00
/* Message Dialog */
2012-02-21 14:17:38 -05:00
QMessageBox : : warning ( this ,
2010-01-24 17:15:02 -05:00
tr ( " Generate GPG key Failure " ) ,
2010-02-14 07:04:44 -05:00
tr ( " All fields are required with a minimum of 3 characters " ) ,
2010-01-24 17:15:02 -05:00
QMessageBox : : Ok ) ;
return ;
}
2010-01-18 07:30:54 -05:00
//generate a new gpg key
std : : string err_string ;
2010-05-01 11:17:23 -04:00
ui . no_gpg_key_label - > setText ( tr ( " Generating new GPG key, please be patient: this process needs generating large prime numbers, and can take some minutes on slow computers. \n \n Fill in your GPG password when asked, to sign your new key. " ) ) ;
2010-01-18 07:30:54 -05:00
ui . no_gpg_key_label - > show ( ) ;
2010-06-14 09:35:38 -04:00
ui . progressBar - > show ( ) ;
2010-01-19 16:44:13 -05:00
ui . new_gpg_key_checkbox - > hide ( ) ;
2010-01-18 07:30:54 -05:00
ui . name_label - > hide ( ) ;
ui . name_input - > hide ( ) ;
ui . email_label - > hide ( ) ;
ui . email_input - > hide ( ) ;
ui . password_label - > hide ( ) ;
ui . password_input - > hide ( ) ;
ui . genPGPuserlabel - > hide ( ) ;
ui . genPGPuser - > hide ( ) ;
ui . location_label - > hide ( ) ;
2010-02-14 07:04:44 -05:00
ui . location_input - > hide ( ) ;
2010-01-18 07:30:54 -05:00
ui . infopushButton - > hide ( ) ;
ui . genButton - > hide ( ) ;
2010-02-26 18:43:55 -05:00
ui . label_location2 - > hide ( ) ;
2012-02-21 14:17:38 -05:00
// QMessageBox::StandardButton info = QMessageBox::information(this,
2010-05-01 11:17:23 -04:00
// "Generating GPG key",
// "This process can take some time (approximately one minute), please be patient after pressing the OK button",
// QMessageBox::Ok);
2010-01-18 07:30:54 -05:00
//info->
2010-05-01 11:17:23 -04:00
setCursor ( Qt : : WaitCursor ) ;
QCoreApplication : : processEvents ( ) ;
while ( QAbstractEventDispatcher : : instance ( ) - > processEvents ( QEventLoop : : AllEvents ) ) ;
2011-08-27 18:54:05 -04:00
RsInit : : GeneratePGPCertificate ( ui . name_input - > text ( ) . toUtf8 ( ) . constData ( ) , ui . email_input - > text ( ) . toUtf8 ( ) . constData ( ) , ui . password_input - > text ( ) . toUtf8 ( ) . constData ( ) , PGPId , err_string ) ;
2010-05-01 11:17:23 -04:00
setCursor ( Qt : : ArrowCursor ) ;
2010-01-18 07:30:54 -05:00
}
2009-08-04 19:37:01 -04:00
2009-08-18 08:43:48 -04:00
//generate a random ssl password
2011-01-29 09:27:16 -05:00
std : : string sslPasswd = RSRandom : : random_alphaNumericString ( RsInit : : getSslPwdLen ( ) ) ;
2011-01-29 09:31:44 -05:00
// std::cerr << "Generated sslPasswd: " << sslPasswd << std::endl;
2011-01-29 09:27:16 -05:00
// const int PWD_LEN = RsInit::getSslPwdLen();
//
// for( int i = 0 ; i < PWD_LEN ; ++i )
// {
// int iNumber;
// iNumber = qrand()%(127-33) + 33;
// sslPasswd += (char)iNumber;
// }
2009-08-04 19:37:01 -04:00
/* Initialise the PGP user first */
RsInit : : SelectGPGAccount ( PGPId ) ;
2011-08-12 16:02:00 -04:00
//RsInit::LoadGPGPassword(PGPpasswd);
2009-08-04 19:37:01 -04:00
std : : string sslId ;
2011-08-12 16:02:00 -04:00
std : : cerr < < " GenCertDialog::genPerson() Generating SSL cert with gpg id : " < < PGPId < < std : : endl ;
std : : string err ;
bool okGen = RsInit : : GenerateSSLCertificate ( PGPId , " " , genLoc , " " , sslPasswd , sslId , err ) ;
2007-11-14 22:18:48 -05:00
if ( okGen )
{
/* complete the process */
2009-08-18 08:43:48 -04:00
RsInit : : LoadPassword ( sslId , sslPasswd ) ;
2007-11-14 22:18:48 -05:00
loadCertificates ( ) ;
}
else
{
/* Message Dialog */
2012-02-21 14:17:38 -05:00
QMessageBox : : warning ( this ,
2011-08-12 16:02:00 -04:00
" Generate ID Failure " ,
2009-08-18 08:44:54 -04:00
" Failed to Generate your new Certificate, maybe PGP password is wrong ! " ,
2011-08-12 16:02:00 -04:00
QMessageBox : : Ok ) ;
2007-11-14 22:18:48 -05:00
}
}
void GenCertDialog : : selectFriend ( )
{
2009-08-04 19:37:01 -04:00
#if 0
2007-11-14 22:18:48 -05:00
/* still need to find home (first) */
QString fileName = QFileDialog : : getOpenFileName ( this , tr ( " Select Trusted Friend " ) , " " ,
tr ( " Certificates (*.pqi *.pem) " ) ) ;
std : : string fname , userName ;
fname = fileName . toStdString ( ) ;
2009-02-08 09:30:28 -05:00
if ( RsInit : : ValidateTrustedUser ( fname , userName ) )
2007-11-14 22:18:48 -05:00
{
ui . genFriend - > setText ( QString : : fromStdString ( userName ) ) ;
}
else
{
ui . genFriend - > setText ( " <Invalid Selected> " ) ;
}
2009-08-04 19:37:01 -04:00
# endif
2007-11-14 22:18:48 -05:00
}
2011-08-12 10:06:29 -04:00
void GenCertDialog : : checkChanged ( int /*i*/ )
2007-11-14 22:18:48 -05:00
{
2009-08-04 19:37:01 -04:00
#if 0
2007-11-14 22:18:48 -05:00
if ( i )
{
selectFriend ( ) ;
}
else
{
/* invalidate selection */
std : : string fname = " " ;
std : : string userName = " " ;
2009-02-08 09:30:28 -05:00
RsInit : : ValidateTrustedUser ( fname , userName ) ;
2007-11-14 22:18:48 -05:00
ui . genFriend - > setText ( " <None Selected> " ) ;
}
2009-08-04 19:37:01 -04:00
# endif
}
void GenCertDialog : : loadCertificates ( )
{
2011-03-16 19:25:57 -04:00
std : : string lockFile ;
2011-08-12 16:02:00 -04:00
int retVal = RsInit : : LockAndLoadCertificates ( false , lockFile ) ;
2010-07-01 16:30:36 -04:00
switch ( retVal )
2009-08-04 19:37:01 -04:00
{
2010-07-01 16:30:36 -04:00
case 0 : close ( ) ;
break ;
case 1 : QMessageBox : : warning ( this ,
tr ( " Multiple instances " ) ,
tr ( " Another RetroShare using the same profile is "
" already running on your system. Please close "
" that instance first " ) ) ;
break ;
case 2 : QMessageBox : : warning ( this ,
tr ( " Multiple instances " ) ,
tr ( " An unexpected error occurred when Retroshare "
" tried to acquire the single instance lock " ) ) ;
break ;
case 3 : QMessageBox : : warning ( this ,
tr ( " Generate ID Failure " ) ,
tr ( " Failed to Load your new Certificate! " ) ) ;
break ;
default : std : : cerr < < " StartDialog::loadCertificates() unexpected switch value " < < retVal < < std : : endl ;
2009-08-04 19:37:01 -04:00
}
}
2009-09-22 13:23:41 -04:00
void GenCertDialog : : infodlg ( )
{
2010-05-02 09:12:46 -04:00
InfoDialog infodialog ( this ) ;
infodialog . exec ( ) ;
2009-09-22 13:23:41 -04:00
}