summaryrefslogtreecommitdiffstats
path: root/pica-client/accounts.cpp
blob: eb7b035fd83efee265a772ded8f721779119f788 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "accounts.h"
#include <QFile>

Accounts::Accounts(QString storage)
{
    dbconn=QSqlDatabase::addDatabase("QSQLITE");
    dbconn.setDatabaseName(storage);

    if (!dbconn.open())
    {
    lasterr=dbconn.lastError();
    return;
    }

    QSqlQuery query;

    query.exec("PRAGMA foreign_keys=ON;");

    //check result, if foreign keys support is on
}

void Accounts::Add(AccountRecord &acc)
{
    QSqlQuery query;

    query.prepare("insert into accounts (id, name, cert_file, pkey_file, ca_file) values (:id, :name, :cert_file, :pkey_file, :ca_file);");
    query.bindValue(":id",acc.id);
    query.bindValue(":name",acc.name);
    query.bindValue(":cert_file",acc.cert_file);
    query.bindValue(":pkey_file",acc.pkey_file);
    query.bindValue(":ca_file",acc.CA_file);
    query.exec();

    lasterr=query.lastError();
}

void Accounts::Delete(quint32 id)
{
    QSqlQuery query;

    query.prepare("delete from accounts where id=:id");
    query.bindValue(":id",id);
    query.exec();

    lasterr=query.lastError();
}

QList<Accounts::AccountRecord> Accounts::GetAccounts()
{
    AccountRecord r;
    QList<Accounts::AccountRecord> L;
    QSqlQuery query;

    //
    query.setForwardOnly(true);
    query.exec("select id, name, cert_file, pkey_file, ca_file from accounts");
    lasterr=query.lastError();

    if (lasterr.isValid())
        return L;

    while(query.next())
    {
        r.id = query.value(0).toUInt();
        r.name = query.value(1).toString();
        r.cert_file = query.value(2).toString();
        r.pkey_file = query.value(3).toString();
        r.CA_file = query.value(4).toString();

        L<<r;
    }

    return L;
}

QString Accounts::GetName(quint32 id)
{
    QSqlQuery query;

    query.prepare("select name from accounts where id=:id");
    query.bindValue(":id", id);
    query.exec();

    lasterr=query.lastError();

    if (lasterr.isValid())
        return QString::null;

    query.next();

    return query.value(0).toString();
}

bool Accounts::isOK()
{
  return  !lasterr.isValid();
}

QString Accounts::GetLastError()
{
    return lasterr.text();
}

bool Accounts::CheckFiles(AccountRecord &acc, QString &error_string)
{
  if (!QFile::exists(acc.cert_file))
  {
    error_string = QObject::tr("Certificate file %1 does not exist").arg(acc.cert_file);
    return false;
  }

  if (!QFile::exists(acc.pkey_file))
  {
    error_string = QObject::tr("Private key file %1 does not exist").arg(acc.pkey_file);
    return false;
  }

  if (!QFile::exists(acc.CA_file))
  {
    error_string = QObject::tr("Certificate Authority file %1 does not exist").arg(acc.CA_file);
    return false;
  }

  return true;
}