~ubuntu-branches/ubuntu/maverick/icecc-monitor/maverick

« back to all changes in this revision

Viewing changes to icemon-kde3/src/hostinfo.cc

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2006-09-11 23:15:00 UTC
  • Revision ID: james.westby@ubuntu.com-20060911231500-l02zw6t5uphenr76
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This file is part of Icecream.
 
3
 
 
4
    Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
 
5
 
 
6
    This program 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
    This program 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 this program; if not, write to the Free Software
 
18
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
*/
 
20
 
 
21
#include "hostinfo.h"
 
22
 
 
23
#include <kdebug.h>
 
24
#include <klocale.h>
 
25
#include <assert.h>
 
26
 
 
27
QValueVector<QColor> HostInfo::mColorTable;
 
28
QMap<int,QString> HostInfo::mColorNameMap;
 
29
 
 
30
void HostInfo::initColorTable()
 
31
{
 
32
    initColor( "#A5080B", i18n("cherry") );
 
33
    initColor( "#76d26f", i18n("pistachio"));
 
34
    initColor( "#664a08", i18n("chocolate"));
 
35
    initColor( "#4c9dff", i18n("smurf"));
 
36
    initColor( "#6c2ca8", i18n("blueberry"));
 
37
    initColor( "#fa8344", i18n("orange"));
 
38
    initColor( "#55CFBD", i18n("mint"));
 
39
    initColor( "#db1230", i18n("strawberry"));
 
40
    initColor( "#a6ea5e", i18n("apple"));
 
41
    initColor( "#D6A3D8", i18n("bubblegum"));
 
42
    initColor( "#f2aa4d", i18n("peach"));
 
43
    initColor( "#aa1387", i18n("plum"));
 
44
    initColor( "#26c3f7", i18n("polar sea"));
 
45
    initColor( "#b8850e", i18n("nut"));
 
46
    initColor( "#6a188d", i18n("blackberry"));
 
47
    initColor( "#24b063", i18n("woodruff"));
 
48
    initColor( "#ffff0f", i18n("banana"));
 
49
    initColor( "#1e1407", i18n("mocha"));
 
50
    initColor( "#29B450", i18n("kiwi"));
 
51
    initColor( "#F8DD31", i18n("lemon"));
 
52
    initColor( "#fa7e91", i18n("raspberry"));
 
53
    initColor( "#c5a243", i18n("caramel"));
 
54
    initColor( "#b8bcff", i18n("blueberry"));
 
55
    // try to make the count a prime number (reminder: 19, 23, 29, 31)
 
56
    // initColor( "#ffb8c0", i18n("blackcurrant"));
 
57
    // initColor( "#f7d36f", i18n("passionfruit"));
 
58
    // initColor( "#d51013", i18n("pomegranate"));
 
59
    // initColor( "#C2C032", i18n("pumpkin" ) );
 
60
}
 
61
 
 
62
void HostInfo::initColor( const QString &value , const QString &name )
 
63
{
 
64
    QColor c( value );
 
65
    mColorTable.append( c );
 
66
 
 
67
    mColorNameMap.insert( c.red() + c.green() * 256 + c.blue() * 65536, name );
 
68
}
 
69
 
 
70
QString HostInfo::colorName( const QColor &c )
 
71
{
 
72
  int key = c.red() + c.green() * 256 + c.blue() * 65536;
 
73
 
 
74
  QMap<int,QString>::ConstIterator it = mColorNameMap.find( key );
 
75
  if ( it == mColorNameMap.end() ) return i18n("<unknown>");
 
76
  else return *it;
 
77
}
 
78
 
 
79
HostInfo::HostInfo( unsigned int id )
 
80
  : mId( id )
 
81
{
 
82
}
 
83
 
 
84
unsigned int HostInfo::id() const
 
85
{
 
86
  return mId;
 
87
}
 
88
 
 
89
QColor HostInfo::color() const
 
90
{
 
91
  return mColor;
 
92
}
 
93
 
 
94
QString HostInfo::name() const
 
95
{
 
96
  return mName;
 
97
}
 
98
 
 
99
QString HostInfo::ip() const
 
100
{
 
101
  return mIp;
 
102
}
 
103
 
 
104
QString HostInfo::platform() const
 
105
{
 
106
    return mPlatform;
 
107
}
 
108
 
 
109
unsigned int HostInfo::maxJobs() const
 
110
{
 
111
  return mMaxJobs;
 
112
}
 
113
 
 
114
bool HostInfo::isOffline() const
 
115
{
 
116
  return mOffline;
 
117
}
 
118
 
 
119
float HostInfo::serverSpeed() const
 
120
{
 
121
    return mServerSpeed;
 
122
}
 
123
 
 
124
unsigned int HostInfo::serverLoad() const
 
125
{
 
126
    return mServerLoad;
 
127
}
 
128
 
 
129
void HostInfo::updateFromStatsMap( const StatsMap &stats )
 
130
{
 
131
#if 0
 
132
  kdDebug() << "HostInfo::updateFromStatsMap():" << endl;
 
133
  StatsMap::ConstIterator it;
 
134
  for( it = stats.begin(); it != stats.end(); it++ ) {
 
135
    kdDebug() << "  STAT: " << it.key() << ": " << it.data() << endl;
 
136
  }
 
137
#endif
 
138
 
 
139
  QString name = stats["Name"];
 
140
 
 
141
  if ( name != mName ) {
 
142
    mName = name;
 
143
    mColor = createColor( mName );
 
144
    mIp = stats["IP"];
 
145
    mPlatform = stats["Platform"];
 
146
  }
 
147
 
 
148
  mMaxJobs = stats["MaxJobs"].toUInt();
 
149
  mOffline = ( stats["State"] == "Offline" );
 
150
 
 
151
  mServerSpeed = stats["Speed"].toFloat();
 
152
 
 
153
  mServerLoad = stats["Load"].toUInt();
 
154
}
 
155
 
 
156
QColor HostInfo::createColor( const QString &name )
 
157
{
 
158
    unsigned long h = 0;
 
159
    unsigned long g;
 
160
    int ch;
 
161
 
 
162
    for( uint i = 0; i < name.length(); ++i ) {
 
163
        ch = name[i].unicode();
 
164
        h = (h << 4) + ch;
 
165
        if ((g = (h & 0xf0000000)) != 0)
 
166
        {
 
167
            h ^= g >> 24;
 
168
            h ^= g;
 
169
        }
 
170
    }
 
171
 
 
172
    h += name.length() + ( name.length() << 17 );
 
173
    h ^= h >> 2;
 
174
 
 
175
    // kdDebug() << "HostInfo::createColor: " << h % mColorTable.count() << ": " << name << endl;
 
176
 
 
177
    return mColorTable[ h % mColorTable.count() ];
 
178
}
 
179
 
 
180
QColor HostInfo::createColor()
 
181
{
 
182
  static int num = 0;
 
183
 
 
184
  return mColorTable.at( num++ % mColorTable.count() );
 
185
 
 
186
#if 0
 
187
  QColor color( num, 255 - num, ( num * 3 ) % 255 );
 
188
 
 
189
  num += 48;
 
190
  num %= 255;
 
191
 
 
192
  return color;
 
193
#endif
 
194
}
 
195
 
 
196
HostInfoManager::HostInfoManager()
 
197
{
 
198
  HostInfo::initColorTable();
 
199
}
 
200
 
 
201
HostInfoManager::~HostInfoManager()
 
202
{
 
203
  HostMap::ConstIterator it;
 
204
  for( it = mHostMap.begin(); it != mHostMap.end(); ++it ) {
 
205
    delete *it;
 
206
  }
 
207
}
 
208
 
 
209
HostInfo *HostInfoManager::find( unsigned int hostid ) const
 
210
{
 
211
  HostMap::ConstIterator it = mHostMap.find( hostid );
 
212
  if ( it == mHostMap.end() ) return 0;
 
213
  else return *it;
 
214
}
 
215
 
 
216
HostInfo *HostInfoManager::checkNode( unsigned int hostid,
 
217
                                      const HostInfo::StatsMap &stats )
 
218
{
 
219
  HostMap::ConstIterator it = mHostMap.find( hostid );
 
220
  HostInfo *hostInfo;
 
221
  if ( it == mHostMap.end() ) {
 
222
    hostInfo = new HostInfo( hostid );
 
223
    mHostMap.insert( hostid, hostInfo );
 
224
  } else {
 
225
    hostInfo = *it;
 
226
  }
 
227
 
 
228
  hostInfo->updateFromStatsMap( stats );
 
229
 
 
230
  return hostInfo;
 
231
}
 
232
 
 
233
QString HostInfoManager::nameForHost( unsigned int id ) const
 
234
{
 
235
  if ( !id ) {
 
236
    kdError() << "Unknown host" << endl;
 
237
  } else {
 
238
    HostInfo *hostInfo = find( id );
 
239
    if ( hostInfo ) return hostInfo->name();
 
240
  }
 
241
 
 
242
  return i18n("<unknown>");
 
243
}
 
244
 
 
245
QColor HostInfoManager::hostColor( unsigned int id ) const
 
246
{
 
247
  if ( id ) {
 
248
    HostInfo *hostInfo = find( id );
 
249
    if ( hostInfo ) {
 
250
        QColor tmp = hostInfo->color();
 
251
        assert( tmp.isValid() && ( tmp.red() + tmp.green() + tmp.blue() ));
 
252
        return tmp;
 
253
    }
 
254
  }
 
255
 
 
256
  kdDebug() << "id " << id << " got no color\n";
 
257
  assert( false );
 
258
 
 
259
  return QColor( 0, 0, 0 );
 
260
}
 
261
 
 
262
unsigned int HostInfoManager::maxJobs( unsigned int id ) const
 
263
{
 
264
  if ( id ) {
 
265
    HostInfo *hostInfo = find( id );
 
266
    if ( hostInfo ) return hostInfo->maxJobs();
 
267
  }
 
268
 
 
269
  return 0;
 
270
}
 
271
 
 
272
HostInfoManager::HostMap HostInfoManager::hostMap() const
 
273
{
 
274
  return mHostMap;
 
275
}