summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pica-client/accounts.cpp18
-rw-r--r--pica-client/accounts.h1
-rw-r--r--pica-client/chatwindow.cpp162
-rw-r--r--pica-client/chatwindow.h18
-rw-r--r--pica-client/contacts.cpp19
-rw-r--r--pica-client/contacts.h1
-rw-r--r--pica-client/history.cpp21
-rw-r--r--pica-client/history.h2
-rw-r--r--pica-client/main.cpp1
9 files changed, 230 insertions, 13 deletions
diff --git a/pica-client/accounts.cpp b/pica-client/accounts.cpp
index e9cf0e9..eb7b035 100644
--- a/pica-client/accounts.cpp
+++ b/pica-client/accounts.cpp
@@ -73,6 +73,24 @@ QList<Accounts::AccountRecord> Accounts::GetAccounts()
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();
diff --git a/pica-client/accounts.h b/pica-client/accounts.h
index 1205583..f51eace 100644
--- a/pica-client/accounts.h
+++ b/pica-client/accounts.h
@@ -22,6 +22,7 @@ public:
void Add(AccountRecord& acc);
void Delete(quint32 id);
QList<AccountRecord> GetAccounts();
+ QString GetName(quint32 id);
bool isOK();
QString GetLastError();
diff --git a/pica-client/chatwindow.cpp b/pica-client/chatwindow.cpp
index 7273c08..946e355 100644
--- a/pica-client/chatwindow.cpp
+++ b/pica-client/chatwindow.cpp
@@ -1,13 +1,17 @@
#include "chatwindow.h"
#include "globals.h"
+#include "contacts.h"
+#include "accounts.h"
#include <QVBoxLayout>
#include <QDateTime>
#include <QAction>
#include <QKeyEvent>
#include <QMessageBox>
+#include <QDateTime>
+#include <QMessageBox>
ChatWindow::ChatWindow(quint32 peer_id) :
- QWidget(0), peer_id_(peer_id)
+ QWidget(0), peer_id_(peer_id), hist(config_dbname, account_id)
{
QVBoxLayout *layout;
@@ -34,35 +38,154 @@ ChatWindow::ChatWindow(quint32 peer_id) :
QWidget::setAttribute(Qt::WA_DeleteOnClose);
QWidget::setAttribute(Qt::WA_QuitOnClose, false);
+
+ menu = new QMenuBar(this);
+ hist24h = new QAction(tr("Last 24 &Hours"), this);
+ hist1week = new QAction(tr("Last &Week"), this);
+ histAll = new QAction(tr("&All"), this);
+
+ connect(hist24h, SIGNAL(triggered()), this, SLOT(show_history24h()));
+ connect(hist1week, SIGNAL(triggered()), this, SLOT(show_history1w()));
+ connect(histAll,SIGNAL(triggered()), this, SLOT(show_historyAll()));
+
+ {
+ QMenu *m;
+ m = menu->addMenu(tr("&History"));
+ m = m->addMenu(tr("Show History"));
+ m->addAction(hist24h);
+ m->addAction(hist1week);
+ m->addAction(histAll);
+ }
+
+ layout->insertSpacing(0, menu->size().height());
+
+ {
+ Contacts ct(config_dbname, account_id);
+ peer_name_ = ct.GetContactName(peer_id_);
+
+ qDebug()<<"peer_name ="<<peer_name_;
+ if (peer_name_.isEmpty())
+ peer_name_ = QString::number(peer_id_);
+
+ }
+ {
+ Accounts ac(config_dbname);
+ my_name_ = ac.GetName(account_id);
+
+ if (my_name_.isEmpty())
+ my_name_ = QString::number(account_id);
+ }
+
}
void ChatWindow::put_message(QString msg, quint32 id, bool is_me)
{
- QString color;
+ QString color, name;
QTextCharFormat fmt;
+ int pos;
if (is_me)
+ {
color = "#0000ff";
+ name = my_name_;
+ }
else
+ {
color = "#ff0000";
+ if (id == peer_id_)
+ name = peer_name_;
+ else
+ name = QString::number(id);
+ }
+
+ pos = draw_message(msg, name, QDateTime::currentDateTime().toString(), color, false);
+
+ if (is_me)
+ undelivered_msgs.append(pos);
+
+// QTextCursor c(chatw->document());
+// c.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
+// if (is_me)
+// undelivered_msgs.append(c.position());
+
+// chatw->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
+// fmt = chatw->currentCharFormat();
+// chatw->insertHtml(
+// QString("<b><font color=%1>&nbsp;(%2) %3 : </font></b>").
+// arg(color).
+// arg(QDateTime::currentDateTime().toString()).
+// arg(QString::number(id))
+// );
+// chatw->setCurrentCharFormat(fmt);
+// chatw->insertPlainText(msg + '\n');
+
+// chatw->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
+
+}
+
+int ChatWindow::draw_message(QString msg, QString nickname, QString datetime, QString color, bool is_delivered)
+{
+ QTextCharFormat fmt;
+ QString delivered_mark;
+ int ret;
+
+ if (is_delivered)
+ delivered_mark = "+";
+ else
+ delivered_mark = "&nbsp;";
+
QTextCursor c(chatw->document());
c.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
- if (is_me)
- undelivered_msgs.append(c.position());
+
+ ret = c.position();
chatw->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
fmt = chatw->currentCharFormat();
chatw->insertHtml(
- QString("<b><font color=%1>&nbsp;(%2) %3 : </font></b>").
+ QString("<b><font color=%1>%2(%3) %4 : </font></b>").
arg(color).
- arg(QDateTime::currentDateTime().toString()).
- arg(QString::number(id))
+ arg(delivered_mark).
+ arg(datetime).
+ arg(nickname)
);
chatw->setCurrentCharFormat(fmt);
chatw->insertPlainText(msg + '\n');
chatw->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
+
+ return ret;
+}
+
+void ChatWindow::print_history(QList<History::HistoryRecord> H)
+{
+ chatw->clear();
+
+ while(!H.isEmpty())
+ {
+ History::HistoryRecord r = H.takeFirst();
+ QString color, name;
+ QDateTime dt;
+
+ if (r.is_me)
+ {
+ color = "#0000ff";
+ name = my_name_;
+ }
+ else
+ {
+ color = "#ff0000";
+
+ if (r.peer_id == peer_id_)
+ name = peer_name_;
+ else
+ name = QString::number(r.peer_id);
+ }
+
+ dt.setTime_t(r.timestamp);
+ //put_message(r.message, r.peer_id, r.is_me);
+ draw_message(r.message, name, dt.toString(), color, r.is_delivered);
+ }
}
void ChatWindow::msg_from_peer(QString msg)
@@ -71,6 +194,7 @@ void ChatWindow::msg_from_peer(QString msg)
showNormal();
put_message(msg, peer_id_, false);
+ hist.Add(peer_id_, msg, false);
}
void ChatWindow::msg_delivered()
@@ -80,6 +204,8 @@ void ChatWindow::msg_delivered()
c.deleteChar();
c.insertText("+");
undelivered_msgs.removeFirst();
+
+ hist.SetDelivered(peer_id_);
}
void ChatWindow::msg_informational(QString text)
@@ -105,6 +231,14 @@ void ChatWindow::msg_informational(QString text)
void ChatWindow::send_message()
{
put_message(sendtextw->toPlainText(), account_id, true);
+ hist.Add(peer_id_, sendtextw->toPlainText(), true);
+
+ if (!hist.isOK())
+ {
+ QMessageBox mbx;
+ mbx.setText(hist.GetLastError());
+ mbx.exec();
+ }
emit msg_input(sendtextw->toPlainText(), this);
@@ -135,6 +269,20 @@ void TextSend::keyPressEvent(QKeyEvent *e)
QTextEdit::keyPressEvent(e);
}
+void ChatWindow::show_history24h()
+{
+ print_history(hist.GetMessages(peer_id_, QDateTime::currentDateTime().addDays(-1).toTime_t(),
+ QDateTime::currentDateTime().toTime_t()));
+}
+void ChatWindow::show_history1w()
+{
+ print_history(hist.GetMessages(peer_id_, QDateTime::currentDateTime().addDays(-7).toTime_t(),
+ QDateTime::currentDateTime().toTime_t()));
+}
+void ChatWindow::show_historyAll()
+{
+ print_history(hist.GetMessages(peer_id_, 0, QDateTime::currentDateTime().toTime_t()));
+}
diff --git a/pica-client/chatwindow.h b/pica-client/chatwindow.h
index 1babd31..4036e43 100644
--- a/pica-client/chatwindow.h
+++ b/pica-client/chatwindow.h
@@ -4,6 +4,10 @@
#include <QWidget>
#include <QTextEdit>
#include <QList>
+#include <QMenuBar>
+#include <QAction>
+#include <QMenu>
+#include "history.h"
class TextSend : public QTextEdit
{
@@ -40,14 +44,26 @@ private:
TextSend *sendtextw;
quint32 peer_id_;
QList<int> undelivered_msgs;
-
+ History hist;
+ QMenuBar *menu;
+ QAction *hist24h;
+ QAction *hist1week;
+ QAction *histAll;
+ QString peer_name_;
+ QString my_name_;
void put_message(QString msg, quint32 id, bool is_me);
+ int draw_message(QString msg, QString nickname, QString datetime, QString color, bool is_delivered);
+ void print_history(QList<History::HistoryRecord> H);
void closeEvent(QCloseEvent *e);
private slots:
void send_message();
+
+ void show_history24h();
+ void show_history1w();
+ void show_historyAll();
};
#endif // CHATWINDOW_H
diff --git a/pica-client/contacts.cpp b/pica-client/contacts.cpp
index 7b66c4f..9e87298 100644
--- a/pica-client/contacts.cpp
+++ b/pica-client/contacts.cpp
@@ -104,6 +104,25 @@ QString Contacts::GetContactCert(quint32 id)
return query.value(0).toString();
}
+QString Contacts::GetContactName(quint32 id)
+{
+ QSqlQuery query;
+
+ query.prepare("select name from contacts where id=:id and account_id=:account_id");
+ query.bindValue(":id",id);
+ query.bindValue(":account_id",account_id_);
+ query.exec();
+
+ lasterr=query.lastError();
+
+ if (lasterr.isValid())
+ return QString::null;
+
+ query.next();
+
+ return query.value(0).toString();
+}
+
void Contacts::SetContactCert(quint32 id, QString &cert_pem)
{
QSqlQuery query;
diff --git a/pica-client/contacts.h b/pica-client/contacts.h
index 54042b5..47f6cd8 100644
--- a/pica-client/contacts.h
+++ b/pica-client/contacts.h
@@ -21,6 +21,7 @@ public:
void Delete(quint32 id);
QList<ContactRecord> GetContacts();
QString GetContactCert(quint32 id);
+ QString GetContactName(quint32 id);
void SetContactCert(quint32 id, QString &cert_pem);
void SetContactName(quint32 id, QString name);
QString GetLastError();
diff --git a/pica-client/history.cpp b/pica-client/history.cpp
index 1f57bc4..31a4e22 100644
--- a/pica-client/history.cpp
+++ b/pica-client/history.cpp
@@ -1,4 +1,5 @@
#include "history.h"
+#include <QDebug>
History::History(QString storage, quint32 my_id)
: me_(my_id)
@@ -23,11 +24,13 @@ 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.prepare(
+ "insert into \"history\" (\"contact_id\", \"account_id\", \"is_me\", \"is_delivered\", \"message\", \"id\", \"timestamp\") \
+ values (:contact_id, :account_id, :is_me, :is_delivered, :message, null, strftime('%s','now'));"
+ );
- query.bindValue(":peer_id", peer_id);
- query.bindValue(":me", me_);
+ query.bindValue(":contact_id", peer_id);
+ query.bindValue(":account_id", me_);
if (is_me)
{
@@ -124,3 +127,13 @@ QMap<quint32, QList<QString> > History::GetUndeliveredMessages()
return M;
}
+
+bool History::isOK()
+{
+ return !lasterr.isValid();
+}
+
+QString History::GetLastError()
+{
+ return lasterr.text();
+}
diff --git a/pica-client/history.h b/pica-client/history.h
index f7a6168..ebf6dbd 100644
--- a/pica-client/history.h
+++ b/pica-client/history.h
@@ -24,6 +24,8 @@ public:
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();
+ bool isOK();
+ QString GetLastError();
private:
QSqlDatabase dbconn;
diff --git a/pica-client/main.cpp b/pica-client/main.cpp
index 5d04501..b0f2ddd 100644
--- a/pica-client/main.cpp
+++ b/pica-client/main.cpp
@@ -127,7 +127,6 @@ static bool create_config_dir()
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\
);"
);