| 1 | // -*- c-basic-offset: 2 -*- |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of Licq, an instant messaging client for UNIX. |
|---|
| 4 | * Copyright (C) 1999-2006 Licq developers |
|---|
| 5 | * |
|---|
| 6 | * Licq is free software; you can redistribute it and/or modify |
|---|
| 7 | * it under the terms of the GNU General Public License as published by |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | * (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * Licq is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with Licq; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #include "messagelist.h" |
|---|
| 22 | |
|---|
| 23 | #include "config.h" |
|---|
| 24 | |
|---|
| 25 | #include <QDateTime> |
|---|
| 26 | #include <QHeaderView> |
|---|
| 27 | #include <QPainter> |
|---|
| 28 | #include <QResizeEvent> |
|---|
| 29 | #include <QScrollBar> |
|---|
| 30 | #include <QTextCodec> |
|---|
| 31 | |
|---|
| 32 | #include <licq_message.h> |
|---|
| 33 | #include <licq_icq.h> |
|---|
| 34 | |
|---|
| 35 | #include "helpers/eventdesc.h" |
|---|
| 36 | |
|---|
| 37 | using namespace LicqQtGui; |
|---|
| 38 | /* TRANSLATOR LicqQtGui::MessageList */ |
|---|
| 39 | /* TRANSLATOR LicqQtGui::MessageListItem */ |
|---|
| 40 | |
|---|
| 41 | MessageListItem::MessageListItem(const CUserEvent* theMsg, QTextCodec* codec, QTreeWidget* parent) |
|---|
| 42 | : QTreeWidgetItem(parent) |
|---|
| 43 | { |
|---|
| 44 | // Keep a copy of the event |
|---|
| 45 | myMsg = theMsg->Copy(); |
|---|
| 46 | |
|---|
| 47 | myCodec = codec; |
|---|
| 48 | |
|---|
| 49 | myUnread = (myMsg->Direction() == D_RECEIVER); |
|---|
| 50 | |
|---|
| 51 | setText(0, myMsg->Direction() == D_SENDER ? "S" : "*R"); |
|---|
| 52 | setTextAlignment(0, Qt::AlignHCenter); |
|---|
| 53 | SetEventLine(); |
|---|
| 54 | QString t = "-----"; |
|---|
| 55 | |
|---|
| 56 | if (myMsg->IsDirect()) t[0] = 'D'; |
|---|
| 57 | if (myMsg->IsUrgent()) t[1] = 'U'; |
|---|
| 58 | if (myMsg->IsMultiRec()) t[2] = 'M'; |
|---|
| 59 | //if (myMsg->IsCancelled()) t[3] = 'C'; |
|---|
| 60 | if (myMsg->IsLicq()) t[3] = 'L'; |
|---|
| 61 | if (myMsg->IsEncrypted()) t[4] = 'E'; |
|---|
| 62 | |
|---|
| 63 | setText(2, t); |
|---|
| 64 | setTextAlignment(2, Qt::AlignHCenter); |
|---|
| 65 | |
|---|
| 66 | QDateTime d; |
|---|
| 67 | d.setTime_t(myMsg->Time()); |
|---|
| 68 | QString sd = d.toString(); |
|---|
| 69 | sd.truncate(sd.length() - 5); |
|---|
| 70 | setText(3, sd); |
|---|
| 71 | |
|---|
| 72 | QColor foreColor; |
|---|
| 73 | if (myMsg->Direction() == D_SENDER) |
|---|
| 74 | foreColor = QColor("blue"); |
|---|
| 75 | else |
|---|
| 76 | foreColor = QColor("red"); |
|---|
| 77 | setForeground(0, foreColor); |
|---|
| 78 | setForeground(1, foreColor); |
|---|
| 79 | setForeground(2, foreColor); |
|---|
| 80 | setForeground(3, foreColor); |
|---|
| 81 | |
|---|
| 82 | QFont f(font(0)); |
|---|
| 83 | f.setBold(myUnread); |
|---|
| 84 | f.setItalic(myMsg->IsUrgent()); |
|---|
| 85 | setFont(0, f); |
|---|
| 86 | setFont(1, f); |
|---|
| 87 | setFont(2, f); |
|---|
| 88 | setFont(3, f); |
|---|
| 89 | |
|---|
| 90 | // Add ourselves first in the list instead of last |
|---|
| 91 | int index = parent->indexOfTopLevelItem(this); |
|---|
| 92 | if (index > -1) |
|---|
| 93 | parent->takeTopLevelItem(index); |
|---|
| 94 | parent->insertTopLevelItem(0, this); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | MessageListItem::~MessageListItem(void) |
|---|
| 98 | { |
|---|
| 99 | delete myMsg; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | void MessageListItem::SetEventLine() |
|---|
| 103 | { |
|---|
| 104 | QString s = EventDescription(myMsg); |
|---|
| 105 | QString text; |
|---|
| 106 | |
|---|
| 107 | switch(myMsg->SubCommand()) |
|---|
| 108 | { |
|---|
| 109 | case ICQ_CMDxSUB_MSG: |
|---|
| 110 | text = myCodec->toUnicode(myMsg->Text()); |
|---|
| 111 | break; |
|---|
| 112 | |
|---|
| 113 | case ICQ_CMDxSUB_URL: |
|---|
| 114 | text = myCodec->toUnicode(dynamic_cast<CEventUrl*>(myMsg)->Url()); |
|---|
| 115 | break; |
|---|
| 116 | |
|---|
| 117 | case ICQ_CMDxSUB_CHAT: |
|---|
| 118 | text = myCodec->toUnicode(dynamic_cast<CEventChat*>(myMsg)->Reason()); |
|---|
| 119 | break; |
|---|
| 120 | |
|---|
| 121 | case ICQ_CMDxSUB_FILE: |
|---|
| 122 | text = myCodec->toUnicode(dynamic_cast<CEventFile*>(myMsg)->Filename()); |
|---|
| 123 | break; |
|---|
| 124 | |
|---|
| 125 | case ICQ_CMDxSUB_EMAILxALERT: |
|---|
| 126 | text = myCodec->toUnicode(dynamic_cast<CEventEmailAlert*>(myMsg)->From()); |
|---|
| 127 | break; |
|---|
| 128 | |
|---|
| 129 | default: |
|---|
| 130 | break; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | if (!text.trimmed().isEmpty()) |
|---|
| 134 | s += " [" + text.trimmed().replace('\n', " ") + "]"; |
|---|
| 135 | setText(1, s); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | void MessageListItem::MarkRead() |
|---|
| 139 | { |
|---|
| 140 | myUnread = false; |
|---|
| 141 | QFont f(font(0)); |
|---|
| 142 | f.setBold(false); |
|---|
| 143 | f.setItalic(myMsg->IsUrgent()); |
|---|
| 144 | setFont(0, f); |
|---|
| 145 | setFont(1, f); |
|---|
| 146 | setFont(2, f); |
|---|
| 147 | setFont(3, f); |
|---|
| 148 | |
|---|
| 149 | setText(0, myMsg->Direction() == D_SENDER ? "S" : "R"); |
|---|
| 150 | SetEventLine(); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | //-----MessageList::constructor------------------------------------------------------------------------ |
|---|
| 154 | MessageList::MessageList(QWidget* parent) |
|---|
| 155 | : QTreeWidget(parent) |
|---|
| 156 | { |
|---|
| 157 | setColumnCount(4); |
|---|
| 158 | QStringList headers; |
|---|
| 159 | headers << tr("D") << tr("Event Type") << tr("Options") << tr("Time"); |
|---|
| 160 | setHeaderLabels(headers); |
|---|
| 161 | setAllColumnsShowFocus(true); |
|---|
| 162 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); |
|---|
| 163 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|---|
| 164 | setSortingEnabled(false); |
|---|
| 165 | setIndentation(0); |
|---|
| 166 | header()->hide(); |
|---|
| 167 | |
|---|
| 168 | QPalette pal(palette()); |
|---|
| 169 | QColor c = pal.color(QPalette::Active, QPalette::Window); |
|---|
| 170 | pal.setColor(QPalette::Active, QPalette::Base, c); |
|---|
| 171 | pal.setColor(QPalette::Inactive, QPalette::Base, c); |
|---|
| 172 | pal.setColor(QPalette::Highlight, pal.color(QPalette::Mid)); |
|---|
| 173 | setPalette(pal); |
|---|
| 174 | |
|---|
| 175 | setFrameStyle(Panel | Sunken); |
|---|
| 176 | setMinimumHeight(40); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | CUserEvent* MessageList::currentMsg() |
|---|
| 180 | { |
|---|
| 181 | if (currentItem() == NULL) |
|---|
| 182 | return NULL; |
|---|
| 183 | return (dynamic_cast<MessageListItem*>(currentItem())->msg()); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | QSize MessageList::sizeHint() const |
|---|
| 187 | { |
|---|
| 188 | QSize s = QTreeWidget::sizeHint(); |
|---|
| 189 | s.setHeight(minimumHeight()); |
|---|
| 190 | |
|---|
| 191 | return s; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | int MessageList::getNumUnread() const |
|---|
| 195 | { |
|---|
| 196 | int num = 0; |
|---|
| 197 | |
|---|
| 198 | for (int i = 0; i < topLevelItemCount(); ++i) |
|---|
| 199 | { |
|---|
| 200 | MessageListItem* item = dynamic_cast<MessageListItem*>(topLevelItem(i)); |
|---|
| 201 | if (item->isUnread()) |
|---|
| 202 | num++; |
|---|
| 203 | } |
|---|
| 204 | return num; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | MessageListItem* MessageList::getNextUnread() |
|---|
| 208 | { |
|---|
| 209 | MessageListItem* next = NULL; |
|---|
| 210 | for (int i = 0; i < topLevelItemCount(); ++i) |
|---|
| 211 | { |
|---|
| 212 | MessageListItem* item = dynamic_cast<MessageListItem*>(topLevelItem(i)); |
|---|
| 213 | if (item->isUnread()) |
|---|
| 214 | next = item; |
|---|
| 215 | } |
|---|
| 216 | return next; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | void MessageList::resizeEvent(QResizeEvent* e) |
|---|
| 220 | { |
|---|
| 221 | QScrollBar* s = verticalScrollBar(); |
|---|
| 222 | int ow = header()->sectionSize(1); |
|---|
| 223 | int nw = width() - 200 - s->width(); |
|---|
| 224 | QTreeWidget::resizeEvent(e); |
|---|
| 225 | if (ow != nw) |
|---|
| 226 | { |
|---|
| 227 | header()->resizeSection(1, nw); |
|---|
| 228 | emit sizeChange(1, ow, nw); |
|---|
| 229 | } |
|---|
| 230 | SetEventLines(); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | void MessageList::SetEventLines() |
|---|
| 234 | { |
|---|
| 235 | for (int i = 0; i < topLevelItemCount(); ++i) |
|---|
| 236 | { |
|---|
| 237 | MessageListItem* item = dynamic_cast<MessageListItem*>(topLevelItem(i)); |
|---|
| 238 | item->SetEventLine(); |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | bool MessageList::event(QEvent* event) |
|---|
| 243 | { |
|---|
| 244 | if (event->type() == QEvent::ToolTip) |
|---|
| 245 | { |
|---|
| 246 | QHelpEvent* helpEvent = dynamic_cast<QHelpEvent*>(event); |
|---|
| 247 | MessageListItem* item = dynamic_cast<MessageListItem*>(itemAt(helpEvent->pos())); |
|---|
| 248 | |
|---|
| 249 | if (item != NULL) |
|---|
| 250 | { |
|---|
| 251 | QString s(item->msg()->IsDirect() ? tr("Direct") : tr("Server")); |
|---|
| 252 | if (item->msg()->IsUrgent()) |
|---|
| 253 | s += QString(" / ") + tr("Urgent"); |
|---|
| 254 | if (item->msg()->IsMultiRec()) |
|---|
| 255 | s += QString(" / ") + tr("Multiple Recipients"); |
|---|
| 256 | if (item->msg()->IsCancelled()) |
|---|
| 257 | s += QString(" / ") + tr("Cancelled Event"); |
|---|
| 258 | if (item->msg()->IsLicq()) |
|---|
| 259 | s += QString(" / Licq ") + QString::fromLocal8Bit(item->msg()->LicqVersionStr()); |
|---|
| 260 | |
|---|
| 261 | setToolTip(s); |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | return QTreeWidget::event(event); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | void MessageList::drawRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const |
|---|
| 269 | { |
|---|
| 270 | QStyleOptionViewItem option2(option); |
|---|
| 271 | // Make highlighted row have same text color as when it is not highlighted |
|---|
| 272 | option2.palette.setBrush(QPalette::HighlightedText, itemFromIndex(index)->foreground(0)); |
|---|
| 273 | |
|---|
| 274 | QTreeWidget::drawRow(painter, option2, index); |
|---|
| 275 | QRect r = visualRect(index); |
|---|
| 276 | |
|---|
| 277 | painter->save(); |
|---|
| 278 | painter->setPen(QPen(option2.palette.dark(), 1)); |
|---|
| 279 | int right = 0; |
|---|
| 280 | for (int i = 0; i < columnCount(); ++i) |
|---|
| 281 | { |
|---|
| 282 | right += columnWidth(i); |
|---|
| 283 | painter->drawLine(right, r.top(), right, r.bottom()); |
|---|
| 284 | } |
|---|
| 285 | painter->drawLine(r.left(), r.bottom(), right, r.bottom()); |
|---|
| 286 | painter->restore(); |
|---|
| 287 | } |
|---|