From 147b3241bde44240de2ea14623a44c6f4207e353 Mon Sep 17 00:00:00 2001 From: me_desk Date: Sun, 13 Oct 2013 11:05:53 +0000 Subject: contacts now have type - temporary, regular, blacklisted. On incoming connections from previously unknown peers new temporary contact record will be created git-svn-id: svn://localhost/project_picapica/trunk@278 fb96df88-216d-11de-830c-f3023f12d8ac --- pica-client/contacts.cpp | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'pica-client/contacts.cpp') 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::GetContacts() +QList Contacts::GetContacts(ContactType type) { QList 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(); -- cgit v1.2.1