summaryrefslogtreecommitdiffstats
path: root/pica-client/askpassword.cpp
blob: 06295029d3c904609becc39fa677b217a7e6aae0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "askpassword.h"
#include "globals.h"
#include <string.h>
#include <QInputDialog>

QMutex AskPassword::passwd_mutex;
QMap<quint32,QString> AskPassword::idpasswd;

AskPassword::AskPassword(QObject *parent) :
    QObject(parent)
{
    connect(this,SIGNAL(password_requested()),this,SLOT(password_dialog()),Qt::BlockingQueuedConnection);
}

int AskPassword::ask_password_cb(char *buf, int size, int rwflag, void *userdata)
{
    QString pswd;
    quint32 id = *((quint32*)userdata);

    passwd_mutex.lock();

    if (idpasswd.contains(id))
    {
        pswd = idpasswd[id];
    }
    else
    {
        askpassword->emit_sig();
        pswd = idpasswd[id];
    }
    passwd_mutex.unlock();

    strncpy(buf, pswd.toUtf8().constData(), size);
    buf[size - 1] = 0;
    return(strlen(buf));
}

void AskPassword::emit_sig()
{
    emit password_requested();
}

void AskPassword::password_dialog()
{
    bool ok;
    QString title = QObject::tr("Enter private key passphrase");
    QString text = QObject::tr(QString("Passphrase for (%1)").arg(account_id).toUtf8().constData());
    QString psw = QInputDialog::getText((QWidget*)mainwindow,title,text,QLineEdit::Password,QString(),&ok);
    if (ok)
        idpasswd[account_id] = psw;
}