diff options
| author | me_desk <me_desk@fb96df88-216d-11de-830c-f3023f12d8ac> | 2013-01-12 22:02:09 +0000 |
|---|---|---|
| committer | me_desk <me_desk@fb96df88-216d-11de-830c-f3023f12d8ac> | 2013-01-12 22:02:09 +0000 |
| commit | da68f9e85db0f232656542564bd770240e28110e (patch) | |
| tree | f61c18c2a6ef1b612c8469be10ea61c3141b227a /pica-client | |
| parent | 3eb742498edba38d42773818e949331b9664ee80 (diff) | |
| download | pica-pica-da68f9e85db0f232656542564bd770240e28110e.tar.gz pica-pica-da68f9e85db0f232656542564bd770240e28110e.zip | |
new class History for storing message history and undelivered messages between restarts
git-svn-id: svn://localhost/project_picapica/trunk@220 fb96df88-216d-11de-830c-f3023f12d8ac
Diffstat (limited to 'pica-client')
| -rw-r--r-- | pica-client/history.cpp | 126 | ||||
| -rw-r--r-- | pica-client/history.h | 35 | ||||
| -rw-r--r-- | pica-client/main.cpp | 16 | ||||
| -rw-r--r-- | pica-client/pica-client.pro | 6 |
4 files changed, 181 insertions, 2 deletions
diff --git a/pica-client/history.cpp b/pica-client/history.cpp new file mode 100644 index 0000000..1f57bc4 --- /dev/null +++ b/pica-client/history.cpp @@ -0,0 +1,126 @@ +#include "history.h" + +History::History(QString storage, quint32 my_id) + : me_(my_id) +{ + 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 History::Add(quint32 peer_id, QString message, bool is_me) +{ + QSqlQuery query; + + query.prepare("insert into history (contact_id, account_id, timestamp, is_me, is_delivered, message) \ + values (:peer_id, :me, strftime('%s','now'), :is_me, :is_delivered, :message)"); + + query.bindValue(":peer_id", peer_id); + query.bindValue(":me", me_); + + if (is_me) + { + query.bindValue(":is_me", 1); + query.bindValue(":is_delivered", 0); + } + else + { + query.bindValue(":is_me", 0); + query.bindValue(":is_delivered", 1); + } + + query.bindValue(":message", message); + + query.exec(); + + lasterr=query.lastError(); +} + +void History::SetDelivered(quint32 peer_id) +{ + QSqlQuery query; + + query.prepare("update history set is_delivered=1 where id in \ + (select id from history where \ + is_delivered=0 and is_me=1 and contact_id=:peer_id and account_id=:me order by id asc limit 1)" + ); + + query.bindValue(":peer_id", peer_id); + query.bindValue(":me", me_); + + query.exec(); + lasterr=query.lastError(); +} + +QList<History::HistoryRecord> History::GetMessages(quint32 peer_id, quint32 start_timestamp, quint32 end_timestamp) +{ + QList<HistoryRecord> L; + HistoryRecord r; + QSqlQuery query; + + query.setForwardOnly(true); + query.prepare("select contact_id, is_me, is_delivered, timestamp, message from history \ + where contact_id=:peer_id and account_id=:me and timestamp >= :start and timestamp <=:end order by id" + ); + + query.bindValue(":peer_id", peer_id); + query.bindValue(":me", me_); + query.bindValue(":start", start_timestamp); + query.bindValue(":end", end_timestamp); + query.exec(); + + lasterr=query.lastError(); + + if (lasterr.isValid()) + return L; + + while(query.next()) + { + r.peer_id = query.value(0).toUInt(); + r.is_me = query.value(1).toBool(); + r.is_delivered = query.value(2).toBool(); + r.timestamp = query.value(3).toUInt(); + r.message = query.value(4).toString(); + + L<<r; + } + + return L; +} + +QMap<quint32, QList<QString> > History::GetUndeliveredMessages() +{ + QMap<quint32, QList<QString> > M; + QSqlQuery query; + + query.setForwardOnly(true); + query.prepare("select contact_id, message from history \ + where account_id=:me and is_delivered=0 and is_me=1 order by id" + ); + + query.bindValue(":me", me_); + query.exec(); + + lasterr=query.lastError(); + + if (lasterr.isValid()) + return M; + + while(query.next()) + { + M[query.value(0).toUInt()] << query.value(1).toString(); + } + + return M; +} diff --git a/pica-client/history.h b/pica-client/history.h new file mode 100644 index 0000000..f7a6168 --- /dev/null +++ b/pica-client/history.h @@ -0,0 +1,35 @@ +#ifndef HISTORY_H +#define HISTORY_H + +#include <QString> +#include <QtSql> +#include <QSqlDatabase> +#include <QList> +#include <QMap> + +class History +{ +public: + struct HistoryRecord + { + quint32 peer_id; + bool is_me; + bool is_delivered; + quint32 timestamp; + QString message; + }; + + History(QString storage, quint32 my_id); + void Add(quint32 peer_id, QString message, bool is_me);//is_me - true - message from me to peer, false - from peer to me + void SetDelivered(quint32 peer_id); //mark first undelivered message from me to peer as delivered + QList<HistoryRecord> GetMessages(quint32 peer_id, quint32 start_timestamp, quint32 end_timestamp); + QMap<quint32, QList<QString> > GetUndeliveredMessages(); + +private: + QSqlDatabase dbconn; + QSqlError lasterr; + + quint32 me_; +}; + +#endif // HISTORY_H diff --git a/pica-client/main.cpp b/pica-client/main.cpp index 9a71a43..5d04501 100644 --- a/pica-client/main.cpp +++ b/pica-client/main.cpp @@ -115,6 +115,22 @@ static bool create_config_dir() query.exec("insert into nodes values(\"picapica.im\", 2299, 0, 0);"); query.exec("insert into nodes values(\"picapica.ge\", 2299, 0, 0);"); + if (query.lastError().isValid()) + goto showerror; + + query.exec("create table history \ + (\ + id integer primary key autoincrement, \ + contact_id int not null, \ + account_id int not null, \ + timestamp int not null, \ + is_me int not null, \ + is_delivered int not null, \ + message text(65536) not null, \ + foreign key(contact_id) references contacts(id) on delete cascade\ + foreign key(account_id) references accounts(id) on delete cascade\ + );" + ); if (query.lastError().isValid()) showerror: diff --git a/pica-client/pica-client.pro b/pica-client/pica-client.pro index f9549d6..73beea4 100644 --- a/pica-client/pica-client.pro +++ b/pica-client/pica-client.pro @@ -30,7 +30,8 @@ SOURCES += main.cpp\ dialogs/viewcertdialog.cpp \ dialogs/forgedcertdialog.cpp \ dialogs/registeraccountdialog.cpp \ - openssltool.cpp + openssltool.cpp \ + history.cpp HEADERS += mainwindow.h \ chatwindow.h \ @@ -54,7 +55,8 @@ HEADERS += mainwindow.h \ ../PICA_common.h \ ../PICA_proto.h \ dialogs/registeraccountdialog.h \ - openssltool.h + openssltool.h \ + history.h DEFINES = PICA_MULTITHREADED |
