Moved loadCertificate from main, StartDialog and GenCertDialog to Rshare.

Switched StartDialog from QMainWindow to QDialog.
Updated english translation file.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@5752 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2012-11-01 01:07:36 +00:00
parent aadb408533
commit afa3248429
11 changed files with 551 additions and 694 deletions

View file

@ -22,139 +22,134 @@
#include <rshare.h>
#include <util/rsrandom.h>
#include <retroshare/rsinit.h>
#include <retroshare/rspeers.h>
#include "GenCertDialog.h"
#include <QAbstractEventDispatcher>
#include <QFileDialog>
#include <QMessageBox>
#include <QMovie>
#include <time.h>
#include <iostream>
/* Define the format used for displaying the date and time */
#define DATETIME_FMT "MMM dd hh:mm:ss"
/** Default constructor */
GenCertDialog::GenCertDialog(bool onlyGenerateIdentity, QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags), mOnlyGenerateIdentity(onlyGenerateIdentity)
: QDialog(parent, flags), mOnlyGenerateIdentity(onlyGenerateIdentity)
{
/* Invoke Qt Designer generated QObject setup routine */
ui.setupUi(this);
/* Invoke Qt Designer generated QObject setup routine */
ui.setupUi(this);
connect(ui.new_gpg_key_checkbox, SIGNAL(clicked()), this, SLOT(newGPGKeyGenUiSetup()));
connect(ui.genButton, SIGNAL(clicked()), this, SLOT(genPerson()));
connect(ui.importIdentity_PB, SIGNAL(clicked()), this, SLOT(importIdentity()));
connect(ui.exportIdentity_PB, SIGNAL(clicked()), this, SLOT(exportIdentity()));
//connect(ui.selectButton, SIGNAL(clicked()), this, SLOT(selectFriend()));
//connect(ui.friendBox, SIGNAL(stateChanged(int)), this, SLOT(checkChanged(int)));
connect(ui.new_gpg_key_checkbox, SIGNAL(clicked()), this, SLOT(newGPGKeyGenUiSetup()));
connect(ui.genButton, SIGNAL(clicked()), this, SLOT(genPerson()));
connect(ui.importIdentity_PB, SIGNAL(clicked()), this, SLOT(importIdentity()));
connect(ui.exportIdentity_PB, SIGNAL(clicked()), this, SLOT(exportIdentity()));
//ui.genName->setFocus(Qt::OtherFocusReason);
//ui.genName->setFocus(Qt::OtherFocusReason);
#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."));
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
/* get all available pgp private certificates....
* mark last one as default.
*/
/* get all available pgp private certificates....
* mark last one as default.
*/
init() ;
init();
}
void GenCertDialog::init()
{
std::cerr << "Finding PGPUsers" << std::endl;
std::cerr << "Finding PGPUsers" << std::endl;
ui.genPGPuser->clear() ;
ui.genPGPuser->clear() ;
std::list<std::string> pgpIds;
std::list<std::string>::iterator it;
bool foundGPGKeys = false;
if (!mOnlyGenerateIdentity) {
if (RsInit::GetPGPLogins(pgpIds)) {
for(it = pgpIds.begin(); it != pgpIds.end(); it++)
{
QVariant userData(QString::fromStdString(*it));
std::string name, email;
RsInit::GetPGPLoginDetails(*it, name, email);
std::cerr << "Adding PGPUser: " << name << " id: " << *it << std::endl;
QString gid = QString::fromStdString(*it).right(8) ;
ui.genPGPuser->addItem(QString::fromUtf8(name.c_str()) + " <" + QString::fromUtf8(email.c_str()) + "> (" + gid + ")", userData);
foundGPGKeys = true;
}
}
}
std::list<std::string> pgpIds;
std::list<std::string>::iterator it;
bool foundGPGKeys = false;
if (!mOnlyGenerateIdentity) {
if (RsInit::GetPGPLogins(pgpIds)) {
for(it = pgpIds.begin(); it != pgpIds.end(); it++)
{
QVariant userData(QString::fromStdString(*it));
std::string name, email;
RsInit::GetPGPLoginDetails(*it, name, email);
std::cerr << "Adding PGPUser: " << name << " id: " << *it << std::endl;
QString gid = QString::fromStdString(*it).right(8) ;
ui.genPGPuser->addItem(QString::fromUtf8(name.c_str()) + " <" + QString::fromUtf8(email.c_str()) + "> (" + gid + ")", userData);
foundGPGKeys = true;
}
}
}
if (foundGPGKeys) {
ui.no_gpg_key_label->hide();
ui.new_gpg_key_checkbox->setChecked(false);
setWindowTitle(tr("Create new Location"));
ui.genButton->setText(tr("Generate new Location"));
ui.headerLabel->setText(tr("Create a new Location"));
genNewGPGKey = false;
} else {
ui.no_gpg_key_label->setVisible(!mOnlyGenerateIdentity);
ui.new_gpg_key_checkbox->setChecked(true);
ui.new_gpg_key_checkbox->setEnabled(false);
setWindowTitle(tr("Create new Identity"));
ui.genButton->setText(tr("Generate new Identity"));
ui.headerLabel->setText(tr("Create a new Identity"));
genNewGPGKey = true;
}
if (foundGPGKeys) {
ui.no_gpg_key_label->hide();
ui.new_gpg_key_checkbox->setChecked(false);
setWindowTitle(tr("Create new Location"));
ui.genButton->setText(tr("Generate new Location"));
ui.headerLabel->setText(tr("Create a new Location"));
genNewGPGKey = false;
} else {
ui.no_gpg_key_label->setVisible(!mOnlyGenerateIdentity);
ui.new_gpg_key_checkbox->setChecked(true);
ui.new_gpg_key_checkbox->setEnabled(false);
setWindowTitle(tr("Create new Identity"));
ui.genButton->setText(tr("Generate new Identity"));
ui.headerLabel->setText(tr("Create a new Identity"));
genNewGPGKey = true;
}
QString text = ui.headerLabel2->text() + "\n";
QString text = ui.headerLabel2->text() + "\n";
if (mOnlyGenerateIdentity) {
ui.new_gpg_key_checkbox->setChecked(true);
ui.new_gpg_key_checkbox->hide();
ui.label->hide();
text += tr("You can create a new identity with this form.");
} else {
text += tr("You can use an existing identity (i.e. a gpg key pair), from the list below, or create a new one with this form.");
}
ui.headerLabel2->setText(text);
if (mOnlyGenerateIdentity) {
ui.new_gpg_key_checkbox->setChecked(true);
ui.new_gpg_key_checkbox->hide();
ui.label->hide();
text += tr("You can create a new identity with this form.");
} else {
text += tr("You can use an existing identity (i.e. a gpg key pair), from the list below, or create a new one with this form.");
}
ui.headerLabel2->setText(text);
newGPGKeyGenUiSetup();
newGPGKeyGenUiSetup();
}
void GenCertDialog::newGPGKeyGenUiSetup() {
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();
ui.importIdentity_PB->hide() ;
ui.exportIdentity_PB->hide();
setWindowTitle(tr("Create new Identity"));
ui.genButton->setText(tr("Generate new Identity"));
ui.headerLabel->setText(tr("Create a new Identity"));
} 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();
ui.importIdentity_PB->setVisible(!mOnlyGenerateIdentity);
ui.exportIdentity_PB->setVisible(!mOnlyGenerateIdentity);
ui.exportIdentity_PB->setEnabled(ui.genPGPuser->count() != 0);
setWindowTitle(tr("Create new Location"));
ui.genButton->setText(tr("Generate new Location"));
ui.headerLabel->setText(tr("Create a new Location"));
}
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();
ui.importIdentity_PB->hide() ;
ui.exportIdentity_PB->hide();
setWindowTitle(tr("Create new Identity"));
ui.genButton->setText(tr("Generate new Identity"));
ui.headerLabel->setText(tr("Create a new Identity"));
} 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();
ui.importIdentity_PB->setVisible(!mOnlyGenerateIdentity);
ui.exportIdentity_PB->setVisible(!mOnlyGenerateIdentity);
ui.exportIdentity_PB->setEnabled(ui.genPGPuser->count() != 0);
setWindowTitle(tr("Create new Location"));
ui.genButton->setText(tr("Generate new Location"));
ui.headerLabel->setText(tr("Create a new Location"));
}
}
void GenCertDialog::exportIdentity()
@ -172,6 +167,7 @@ void GenCertDialog::exportIdentity()
else
QMessageBox::information(this,tr("Identity not saved"),tr("Your identity was not saved. An error occurred.")) ;
}
void GenCertDialog::importIdentity()
{
QString fname = QFileDialog::getOpenFileName(this,tr("Export Identity"), "",tr("RetroShare Identity files (*.asc)")) ;
@ -198,102 +194,79 @@ void GenCertDialog::importIdentity()
}
init() ;
// QVariant userData(QString::fromStdString(gpg_id));
// QString gid = QString::fromStdString(gpg_id).right(8) ;
// ui.genPGPuser->addItem(QString::fromUtf8(name.c_str()) + " <" + QString::fromUtf8(email.c_str()) + "> (" + gid + ")", userData);
}
void GenCertDialog::genPerson()
{
/* Check the data from the GUI. */
std::string genLoc = ui.location_input->text().toUtf8().constData();
std::string PGPId;
std::string genLoc = ui.location_input->text().toUtf8().constData();
std::string PGPId;
if (!genNewGPGKey) {
if (genLoc.length() < 3) {
/* Message Dialog */
QMessageBox::warning(this,
tr("Generate GPG key Failure"),
tr("Location field is required with a minimum of 3 characters"),
QMessageBox::Ok);
return;
}
int pgpidx = ui.genPGPuser->currentIndex();
if (pgpidx < 0)
{
/* Message Dialog */
QMessageBox::warning(this,
"Generate ID Failure",
"Missing PGP Certificate",
QMessageBox::Ok);
return;
}
QVariant data = ui.genPGPuser->itemData(pgpidx);
PGPId = (data.toString()).toStdString();
} else {
if (ui.password_input->text().length() < 3 || ui.name_input->text().length() < 3
|| ui.email_input->text().length() < 3 || ui.location_label->text().length() < 3 ||
genLoc.length() < 3) {
/* Message Dialog */
QMessageBox::warning(this,
tr("Generate GPG key Failure"),
tr("All fields are required with a minimum of 3 characters"),
QMessageBox::Ok);
return;
}
//generate a new gpg key
std::string err_string;
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\nFill in your GPG password when asked, to sign your new key."));
ui.no_gpg_key_label->show();
ui.new_gpg_key_checkbox->hide();
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();
ui.location_input->hide();
ui.genButton->hide();
ui.label_location2->hide();
ui.importIdentity_PB->hide();
// QMessageBox::StandardButton info = QMessageBox::information(this,
// "Generating GPG key",
// "This process can take some time (approximately one minute), please be patient after pressing the OK button",
// QMessageBox::Ok);
//info->
setCursor(Qt::WaitCursor) ;
if (!genNewGPGKey) {
if (genLoc.length() < 3) {
/* Message Dialog */
QMessageBox::warning(this,
tr("Generate GPG key Failure"),
tr("Location field is required with a minimum of 3 characters"),
QMessageBox::Ok);
return;
}
int pgpidx = ui.genPGPuser->currentIndex();
if (pgpidx < 0)
{
/* Message Dialog */
QMessageBox::warning(this,
"Generate ID Failure",
"Missing PGP Certificate",
QMessageBox::Ok);
return;
}
QVariant data = ui.genPGPuser->itemData(pgpidx);
PGPId = (data.toString()).toStdString();
} else {
if (ui.password_input->text().length() < 3 || ui.name_input->text().length() < 3 ||
ui.email_input->text().length() < 3 || genLoc.length() < 3) {
/* Message Dialog */
QMessageBox::warning(this,
tr("Generate GPG key Failure"),
tr("All fields are required with a minimum of 3 characters"),
QMessageBox::Ok);
return;
}
//generate a new gpg key
std::string err_string;
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\nFill in your GPG password when asked, to sign your new key."));
ui.no_gpg_key_label->show();
ui.new_gpg_key_checkbox->hide();
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();
ui.location_input->hide();
ui.genButton->hide();
ui.label_location2->hide();
ui.importIdentity_PB->hide();
QCoreApplication::processEvents();
while(QAbstractEventDispatcher::instance()->processEvents(QEventLoop::AllEvents)) ;
setCursor(Qt::WaitCursor) ;
RsInit::GeneratePGPCertificate(ui.name_input->text().toUtf8().constData(), ui.email_input->text().toUtf8().constData(), ui.password_input->text().toUtf8().constData(), PGPId, err_string);
QCoreApplication::processEvents();
while(QAbstractEventDispatcher::instance()->processEvents(QEventLoop::AllEvents)) ;
setCursor(Qt::ArrowCursor) ;
}
RsInit::GeneratePGPCertificate(ui.name_input->text().toUtf8().constData(), ui.email_input->text().toUtf8().constData(), ui.password_input->text().toUtf8().constData(), PGPId, err_string);
setCursor(Qt::ArrowCursor) ;
}
//generate a random ssl password
std::string sslPasswd = RSRandom::random_alphaNumericString(RsInit::getSslPwdLen()) ;
// std::cerr << "Generated sslPasswd: " << sslPasswd << std::endl;
// const int PWD_LEN = RsInit::getSslPwdLen();
//
// for( int i = 0 ; i < PWD_LEN ; ++i )
// {
// int iNumber;
// iNumber = qrand()%(127-33) + 33;
// sslPasswd += (char)iNumber;
// }
/* Initialise the PGP user first */
RsInit::SelectGPGAccount(PGPId);
//RsInit::LoadGPGPassword(PGPpasswd);
std::string sslId;
std::cerr << "GenCertDialog::genPerson() Generating SSL cert with gpg id : " << PGPId << std::endl;
@ -304,80 +277,16 @@ void GenCertDialog::genPerson()
{
/* complete the process */
RsInit::LoadPassword(sslId, sslPasswd);
loadCertificates();
if (Rshare::loadCertificate(sslId, false, PGPId)) {
accept();
}
}
else
{
/* Message Dialog */
QMessageBox::warning(this,
"Generate ID Failure",
"Failed to Generate your new Certificate, maybe PGP password is wrong !",
tr("Generate ID Failure"),
tr("Failed to Generate your new Certificate, maybe PGP password is wrong!"),
QMessageBox::Ok);
}
}
void GenCertDialog::selectFriend()
{
#if 0
/* 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();
if (RsInit::ValidateTrustedUser(fname, userName))
{
ui.genFriend -> setText(QString::fromStdString(userName));
}
else
{
ui.genFriend -> setText("<Invalid Selected>");
}
#endif
}
void GenCertDialog::checkChanged(int /*i*/)
{
#if 0
if (i)
{
selectFriend();
}
else
{
/* invalidate selection */
std::string fname = "";
std::string userName = "";
RsInit::ValidateTrustedUser(fname, userName);
ui.genFriend -> setText("<None Selected>");
}
#endif
}
void GenCertDialog::loadCertificates()
{
std::string lockFile;
int retVal = RsInit::LockAndLoadCertificates(false, lockFile);
switch(retVal)
{
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;
}
}