Example #1
0
void OtrInternal::create_privkey(const char *accountname,
                                 const char *protocol)
{
    Q_ASSERT(m_userstate);

    if(!m_mutex.tryLock(10))
        return;

    QMessageBox warnBox(QMessageBox::Warning,tr("qutim-otr"),tr("Key generation will be started.<br>Please, make sure that you <b>log out from all your jabber accounts</b>! QutIM may be crashed if they will be online while key is generating."),QMessageBox::Ok,NULL,Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
    warnBox.setModal(false);
    warnBox.setWindowModality(Qt::NonModal);
    warnBox.setDefaultButton(QMessageBox::Ok);
    warnBox.show();
    warnBox.setEscapeButton(QMessageBox::Cancel);
    while(!warnBox.isHidden())
        QCoreApplication::processEvents();

    QMessageBox infoMb(QMessageBox::Information, tr("qutim-otr"),
                       tr("Generating keys for account %1\nThis may take a while.\nPlease, move mouse and use keyoard to decrease generation time.").arg(QString(accountname)),
                       QMessageBox::Ok, NULL,
                       Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
    infoMb.button(QMessageBox::Ok)->setEnabled(false);
    infoMb.button(QMessageBox::Ok)->setText(tr("please wait..."));
    infoMb.setWindowModality(Qt::NonModal);
    infoMb.setModal(false);
    infoMb.show();
    {
        KeyGeneratorThread keyGenerator(m_userstate, m_keysFile,
                                                                  accountname, protocol);
        keyGenerator.start();

        while (!keyGenerator.isFinished())
            QCoreApplication::processEvents();
    }
    m_mutex.unlock();
    infoMb.button(QMessageBox::Ok)->setEnabled(true);
    infoMb.button(QMessageBox::Ok)->setText("Ok");

    char fingerprint[45];
    if (otrl_privkey_fingerprint(m_userstate, fingerprint, accountname,
                                 protocol) == NULL)
    {
        QMessageBox failMb(QMessageBox::Critical, tr("qutim-otr"),
                           tr("Failed to generate key for account %1\nThe OTR Plugin will not work.").arg(QString(accountname)),
                           QMessageBox::Ok, NULL,
                           Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
        failMb.exec();
    }
    else
    {
        infoMb.setText(tr("The fingerprint for account %1 is\n").arg(QString(accountname)) + QString(fingerprint));
    }
    infoMb.exec();

}
void OtrInternal::create_privkey(const char* accountname,
                                 const char* protocol)
{
    if (is_generating)
    {
        return;
    }

    QMessageBox qMB(QMessageBox::Question, QObject::tr("Psi OTR"),
                    QObject::tr("Private keys for account \"%1\" need to be generated. "
                                "This takes quite some time (from a few seconds to a "
                                "couple of minutes), and while you can use Psi+ in the "
                                "meantime, all the messages will be sent unencrypted "
                                "until keys are generated. You will be notified when "
                                "this process finishes.\n"
                                "\n"
                                "Do you want to generate keys now?")
                                .arg(m_callback->humanAccount(
                                            QString::fromUtf8(accountname))),
                       QMessageBox::Yes | QMessageBox::No);

    if (qMB.exec() != QMessageBox::Yes)
    {
        return;
    }

    is_generating = true;

    QByteArray keysfile = QFile::encodeName(m_keysFile);

    QEventLoop loop;
    QFutureWatcher<gcry_error_t> watcher;

    QObject::connect(&watcher, SIGNAL(finished()), &loop, SLOT(quit()));

    QFuture<gcry_error_t> future = QtConcurrent::run(otrl_privkey_generate,
                                              m_userstate,
                                              keysfile.constData(),
                                              accountname,
                                              protocol);
    watcher.setFuture(future);

    loop.exec();

    is_generating = false;

    char fingerprint[OTRL_PRIVKEY_FPRINT_HUMAN_LEN];
    if (otrl_privkey_fingerprint(m_userstate, fingerprint, accountname,
                                 protocol))
    {
        QMessageBox infoMb(QMessageBox::Information, QObject::tr("Psi OTR"),
                           QObject::tr("Keys have been generated. "
                                       "Fingerprint for account \"%1\":\n"
                                       "%2\n"
                                       "\n"
                                       "Thanks for your patience.")
                                   .arg(m_callback->humanAccount(
                                            QString::fromUtf8(accountname)))
                                   .arg(QString(fingerprint)));
        infoMb.exec();
    }
    else
    {
        QMessageBox failMb(QMessageBox::Critical, QObject::tr("Psi OTR"),
                           QObject::tr("Failed to generate keys for account \"%1\"."
                                       "\nThe OTR Plugin will not work.")
                                       .arg(m_callback->humanAccount(
                                                QString::fromUtf8(accountname))),
                           QMessageBox::Ok);
        failMb.exec();
    }
}