diff options
Diffstat (limited to 'pica-client/chatwindow.cpp')
| -rw-r--r-- | pica-client/chatwindow.cpp | 162 |
1 files changed, 155 insertions, 7 deletions
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> (%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 = " "; + 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> (%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())); +} |
