File: kbsmessageview.cpp

package info (click to toggle)
kboincspy 0.9.1-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,880 kB
  • ctags: 3,964
  • sloc: cpp: 29,018; sh: 9,736; perl: 2,793; makefile: 472; xml: 119
file content (156 lines) | stat: -rw-r--r-- 4,965 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/***************************************************************************
 *   Copyright (C) 2004 by Roberto Virga                                   *
 *   rvirga@users.sourceforge.net                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <qlayout.h>
#include <qpopupmenu.h>

#include <kapplication.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kstatusbar.h>

#include <kbsboincmonitor.h>
#include <kbsrpcmonitor.h>

#include "kbsmessageview.h"

KBSMessageView::KBSMessageView(KBSBOINCMonitor *monitor, QWidget *parent, const char *name)
              : KBSStandardWindow(parent, name),
                m_view(new KListView(this)), m_monitor(monitor)
{
  KBSRPCMonitor *rpcMonitor = monitor->rpcMonitor();
  
  connect(rpcMonitor, SIGNAL(messagesUpdated()), this, SLOT(updateMessages()));
  
  const QString host = rpcMonitor->host();
  
  setCaption(i18n("Messages - %1").arg(host));
  
  setCentralWidget(m_view);
  setupView();
  
  setAutoSaveGeometry(QString("%1 - Messages").arg(host));
  
  setupActions();
  
  updateMessages();
}

KBSMessageView::~KBSMessageView()
{
  const QString group = autoSaveGroup();
  if(!group.isEmpty()) m_view->saveLayout(kapp->config(), group);
}

KBSBOINCMonitor *KBSMessageView::monitor()
{
  return m_monitor;
}

QString KBSMessageView::text()
{
  QString text = "";
  
  QListViewItem *item = m_view->firstChild();
  while(NULL != item)
  {
    QStringList fields;
    for(int column = 0; column < m_view->columns(); ++column)
      fields << item->text(column);
    text.append(fields.join("\t") + "\n");
    
    item = item->nextSibling();
  }
  
  return text;
}

void KBSMessageView::setAutoSaveGeometry(const QString &group)
{
  KBSStandardWindow::setAutoSaveGeometry(group);
  m_view->restoreLayout(kapp->config(), group);
}

void KBSMessageView::updateMessages()
{
  const QValueList<KBSBOINCMsg> messages = m_monitor->rpcMonitor()->messages()->msg;
  
  if(m_view->childCount() >= int(messages.count()))
    m_view->clear();
  
  QListViewItem *last = NULL;
  for(int i = m_view->childCount(); i < int(messages.count()); ++i)
    last = new Item(m_view, messages[i]);
  
  if(NULL != last)
    m_view->ensureItemVisible(last);
}

void KBSMessageView::slotContextMenu(KListView *, QListViewItem *, const QPoint &pos)
{
  QPopupMenu *context = static_cast<QPopupMenu*>(guiFactory()->container("context", this));
  context->popup(pos);
}

void KBSMessageView::setupView()
{
  m_view->addColumn(i18n("Project"));
  m_view->setColumnAlignment(0, AlignLeft);
  m_view->addColumn(i18n("Message"));  
  m_view->setColumnAlignment(1, AlignLeft);
  m_view->addColumn(i18n("Time"));
  m_view->setColumnAlignment(2, AlignLeft);
  
  m_view->setFocusPolicy(QWidget::NoFocus);
  m_view->setSelectionMode(QListView::NoSelection);
  
  m_view->setShowSortIndicator(true);
  m_view->setSorting(2);
  m_view->sort();
 
  connect(m_view, SIGNAL(contextMenu(KListView *, QListViewItem *, const QPoint &)),
          this, SLOT(slotContextMenu(KListView *, QListViewItem *, const QPoint &))); 
}

KBSMessageView::Item::Item(QListView *parent, const KBSBOINCMsg &msg)
                    : KListViewItem(parent), m_msg(msg)
{
  setText(0, msg.project);
  setPixmap(1, msg.pri > 1 ? SmallIcon("messagebox_critical") : SmallIcon("messagebox_info"));
  setText(1, msg.body);
  setText(2, KGlobal::locale()->formatDateTime(msg.time));
}

QString KBSMessageView::Item::key(int column, bool ascending) const
{
  switch(column) {
    case 0:
      return m_msg.project;
    case 1:
      return m_msg.body;
    case 2:
      return QString().sprintf("%09u", m_msg.seqno);
    default:
      return QListViewItem::key(column, ascending);
  }
}

#include "kbsmessageview.moc"