diff options
| -rw-r--r-- | pica-client/contactlistwidget.cpp | 5 | ||||
| -rw-r--r-- | pica-client/contactlistwidget.h | 3 | ||||
| -rw-r--r-- | pica-client/contacts.cpp | 40 | ||||
| -rw-r--r-- | pica-client/contacts.h | 16 | ||||
| -rw-r--r-- | pica-client/main.cpp | 4 | ||||
| -rw-r--r-- | pica-client/mainwindow.cpp | 1 | ||||
| -rw-r--r-- | pica-client/skynet.cpp | 7 | ||||
| -rw-r--r-- | pica-client/skynet.h | 1 |
8 files changed, 69 insertions, 8 deletions
diff --git a/pica-client/contactlistwidget.cpp b/pica-client/contactlistwidget.cpp index 4331b89..0fe79e6 100644 --- a/pica-client/contactlistwidget.cpp +++ b/pica-client/contactlistwidget.cpp @@ -182,3 +182,8 @@ void ContactListWidget::view_cert() mbx.exec(); } } + +void ContactListWidget::Reload() +{ + setContactsStorage(storage); +} diff --git a/pica-client/contactlistwidget.h b/pica-client/contactlistwidget.h index d884bc4..4fe936a 100644 --- a/pica-client/contactlistwidget.h +++ b/pica-client/contactlistwidget.h @@ -31,6 +31,9 @@ private: signals: +public slots: + void Reload(); + private slots: void add_contact(); void del_contact(); diff --git a/pica-client/contacts.cpp b/pica-client/contacts.cpp index fb482d2..657ab12 100644 --- a/pica-client/contacts.cpp +++ b/pica-client/contacts.cpp @@ -35,14 +35,43 @@ QString Contacts::GetLastError() return lasterr.text(); } -void Contacts::Add(QByteArray id) +bool Contacts::Exists(QByteArray id) { QSqlQuery query; - query.prepare("insert into contacts (id, name, cert_pem, account_id) values (:id, NULL, NULL, :account_id)"); - query.bindValue(":id",id); + query.setForwardOnly(true); + query.prepare("select count(*) from contacts where account_id=:account_id and id=:id"); query.bindValue(":account_id",account_id_); + query.bindValue(":id", id); query.exec(); + lasterr=query.lastError(); + + query.next(); + if (query.value(0).toInt() == 1) + return true; + + return false; +} + +void Contacts::Add(QByteArray id, ContactType type) +{ + QSqlQuery query; + + if (!Exists(id)) + { + query.prepare("insert into contacts (id, name, cert_pem, account_id) values (:id, NULL, NULL, :account_id)"); + query.bindValue(":id",id); + query.bindValue(":account_id",account_id_); + query.exec(); + } + else + { + query.prepare("update contacts set type=:type where id=:id and account_id=:account_id"); + query.bindValue(":type", type); + query.bindValue(":id", id); + query.bindValue(":account_id", account_id_); + query.exec(); + } lasterr=query.lastError(); } @@ -59,15 +88,16 @@ void Contacts::Delete(QByteArray id) lasterr=query.lastError(); } -QList<Contacts::ContactRecord> Contacts::GetContacts() +QList<Contacts::ContactRecord> Contacts::GetContacts(ContactType type) { QList<ContactRecord> L; ContactRecord r; QSqlQuery query; query.setForwardOnly(true); - query.prepare("select id, name from contacts where account_id=:account_id"); + query.prepare("select id, name from contacts where account_id=:account_id and type=:type"); query.bindValue(":account_id",account_id_); + query.bindValue(":type", type); query.exec(); lasterr=query.lastError(); diff --git a/pica-client/contacts.h b/pica-client/contacts.h index f3f7663..34c7742 100644 --- a/pica-client/contacts.h +++ b/pica-client/contacts.h @@ -9,23 +9,35 @@ class Contacts { public: + + enum ContactType + { + temporary = 0, + regular = 1, + blacklisted = 2 + }; + struct ContactRecord { QByteArray id; QString name; + + ContactType type; }; Contacts(QString storage, QByteArray user_account_id); - void Add(QByteArray id); + bool Exists(QByteArray id); + void Add(QByteArray id, ContactType type = regular); void Delete(QByteArray id); - QList<ContactRecord> GetContacts(); + QList<ContactRecord> GetContacts(ContactType type = regular); QString GetContactCert(QByteArray id); QString GetContactName(QByteArray id); void SetContactCert(QByteArray id, QString &cert_pem); void SetContactName(QByteArray id, QString name); QString GetLastError(); bool isOK(); + private: QSqlDatabase dbconn; QSqlError lasterr; diff --git a/pica-client/main.cpp b/pica-client/main.cpp index 5fbd0af..79b80c9 100644 --- a/pica-client/main.cpp +++ b/pica-client/main.cpp @@ -78,6 +78,7 @@ static bool create_database() name varchar(64), \ cert_pem text(2048), \ account_id blob not null, \ + type int not null default 1, \ primary key(id, account_id),\ foreign key(account_id) references accounts(id) on delete cascade\ );"); @@ -254,6 +255,7 @@ static bool update_database() name varchar(64), \ cert_pem text(2048), \ account_id blob not null, \ + type int not null default 1, \ primary key(id, account_id),\ foreign key(account_id) references accounts(id) on delete cascade\ );" @@ -339,7 +341,7 @@ static bool update_database() if (query.lastError().isValid()) goto showerror; - query.exec("insert into contacts select c.new_id, c.name, c.cert_pem, a.new_id from accounts_old_schemav1 as a, contacts_old_schemav1 as c on c.account_id = a.id where c.new_id is not null and a.new_id is not null"); + query.exec("insert into contacts select c.new_id, c.name, c.cert_pem, a.new_id, 1 from accounts_old_schemav1 as a, contacts_old_schemav1 as c on c.account_id = a.id where c.new_id is not null and a.new_id is not null"); if (query.lastError().isValid()) goto showerror; diff --git a/pica-client/mainwindow.cpp b/pica-client/mainwindow.cpp index 1d789e4..b030919 100644 --- a/pica-client/mainwindow.cpp +++ b/pica-client/mainwindow.cpp @@ -47,6 +47,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(skynet, SIGNAL(LostSelfAwareness()), this, SLOT(set_offline())); connect(status, SIGNAL(currentIndexChanged(int)), this, SLOT(status_changed(int))); connect(bt_showhide_notifications, SIGNAL(clicked()), this, SLOT(showhide_click())); + connect(skynet, SIGNAL(ContactsUpdated()), contact_list, SLOT(Reload())); } MainWindow::~MainWindow() diff --git a/pica-client/skynet.cpp b/pica-client/skynet.cpp index 974c1a8..2d2336f 100644 --- a/pica-client/skynet.cpp +++ b/pica-client/skynet.cpp @@ -103,6 +103,11 @@ void SkyNet::verify_peer_cert(QByteArray peer_id, QString cert_pem, bool *verifi Contacts cnt(config_dbname, Accounts::GetCurrentAccount().id); QString stored_cert; + + if (!cnt.Exists(peer_id) && cnt.isOK()) + { + cnt.Add(peer_id, Contacts::temporary); + } if ((stored_cert = cnt.GetContactCert(peer_id)).isEmpty()) { cnt.SetContactCert(peer_id, cert_pem); @@ -110,6 +115,8 @@ void SkyNet::verify_peer_cert(QByteArray peer_id, QString cert_pem, bool *verifi QString name = OpenSSLTool::NameFromCertString(cert_pem); cnt.SetContactName(peer_id, name); + + emit ContactsUpdated(); } else {//compare certificates diff --git a/pica-client/skynet.h b/pica-client/skynet.h index b9029a1..b6cdfb8 100644 --- a/pica-client/skynet.h +++ b/pica-client/skynet.h @@ -31,6 +31,7 @@ signals: void PeerCertificateReceived(QByteArray peer_id, QString cert_pem, bool *verified); void CertificateForged(QByteArray peer_id, QString received_cert, QString stored_cert); void ErrMsgFromNode(QString msg); + void ContactsUpdated(); private: Nodes nodes; |
