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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
/*
* Copyright (C) 2003 by Unai Garro <[email protected]>
* Copyright (C) 2004 by Enrico Ros <[email protected]>
* Copyright (C) 2004 by Stephan Kulow <[email protected]>
* Copyright (C) 2004 by Oswald Buddenhagen <[email protected]>
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "kdmlabel.h"
#include <kgreeter.h>
#include <kglobal.h>
#include <klocale.h>
#include <kmacroexpander.h>
#include <kdebug.h>
#include <qdatetime.h>
#include <qpainter.h>
#include <qfontmetrics.h>
#include <qtimer.h>
#include <unistd.h>
#include <sys/utsname.h>
#if !defined(HAVE_GETDOMAINNAME) && defined(HAVE_SYSINFO)
# include <sys/systeminfo.h>
#endif
KdmLabel::KdmLabel( KdmItem *parent, const QDomNode &node, const char *name )
: KdmItem( parent, node, name )
{
itemType = "label";
// Set default values for label (note: strings are already Null)
label.active.color.setRgb( 0xFFFFFF );
label.active.present = false;
label.prelight.present = false;
label.maximumWidth = -1;
const QString locale = KGlobal::locale()->language();
// Read LABEL ID
QDomNode n = node;
QDomElement elLab = n.toElement();
// ID types: clock, pam-error, pam-message, pam-prompt,
// pam-warning, timed-label
label.id = elLab.attribute( "id", "" );
label.hasId = !(label.id).isEmpty();
// Read LABEL TAGS
QDomNodeList childList = node.childNodes();
bool stockUsed = false;
for (uint nod = 0; nod < childList.count(); nod++) {
QDomNode child = childList.item( nod );
QDomElement el = child.toElement();
QString tagName = el.tagName();
if (tagName == "pos")
label.maximumWidth = el.attribute( "max-width", "-1" ).toInt();
else if (tagName == "normal") {
parseColor( el.attribute( "color", "#ffffff" ), label.normal.color );
parseFont( el.attribute( "font", "Sans 14" ), label.normal.font );
} else if (tagName == "active") {
label.active.present = true;
parseColor( el.attribute( "color", "#ffffff" ), label.active.color );
parseFont( el.attribute( "font", "Sans 14" ), label.active.font );
} else if (tagName == "prelight") {
label.prelight.present = true;
parseColor( el.attribute( "color", "#ffffff" ), label.prelight.color );
parseFont( el.attribute( "font", "Sans 14" ), label.prelight.font );
} else if (tagName == "text" && el.attributes().count() == 0 && !stockUsed) {
label.text = el.text();
} else if (tagName == "text" && !stockUsed) {
QString lang = el.attribute( "xml:lang", "" );
if (lang == locale)
label.text = el.text();
} else if (tagName == "stock") {
label.text = lookupStock( el.attribute( "type", "" ) );
stockUsed = true;
}
}
// Check if this is a timer label
label.isTimer = label.text.find( "%c" ) >= 0;
if (label.isTimer) {
timer = new QTimer( this );
timer->start( 1000 );
connect( timer, SIGNAL(timeout()), SLOT(update()) );
}
cText = lookupText( label.text );
}
void
KdmLabel::setText( const QString &txt )
{
label.text = txt;
update();
}
QSize
KdmLabel::sizeHint()
{
// choose the correct label class
struct LabelStruct::LabelClass *l = &label.normal;
if (state == Sactive && label.active.present)
l = &label.active;
else if (state == Sprelight && label.prelight.present)
l = &label.prelight;
// get the hint from font metrics
QSize hint = QFontMetrics( l->font ).size( AlignLeft | SingleLine, cText );
// clip the result using the max-width label(pos) parameter
if (label.maximumWidth > 0 && hint.width() > label.maximumWidth)
hint.setWidth( label.maximumWidth );
return hint;
}
void
KdmLabel::drawContents( QPainter *p, const QRect &/*r*/ )
{
// choose the correct label class
struct LabelStruct::LabelClass *l = &label.normal;
if (state == Sactive && label.active.present)
l = &label.active;
else if (state == Sprelight && label.prelight.present)
l = &label.prelight;
// draw the label
p->setFont( l->font );
p->setPen( l->color );
//TODO paint clipped (tested but not working..)
p->drawText( area, AlignLeft | SingleLine, cText );
}
void
KdmLabel::statusChanged()
{
KdmItem::statusChanged();
if (!label.active.present && !label.prelight.present)
return;
if ((state == Sprelight && !label.prelight.present) ||
(state == Sactive && !label.active.present))
return;
needUpdate();
}
void
KdmLabel::update()
{
QString text = lookupText( label.text );
if (text != cText) {
cText = text;
needUpdate();
}
}
static const struct {
const char *type, *text;
} stocks[] = {
{ "language", I18N_NOOP("Language") },
{ "session", I18N_NOOP("Session Type") },
{ "system", I18N_NOOP("Menu") }, // i18n("Actions");
{ "disconnect", I18N_NOOP("Disconnect") },
{ "quit", I18N_NOOP("Quit") },
{ "halt", I18N_NOOP("Power off") },
{ "suspend", I18N_NOOP("Suspend") },
{ "reboot", I18N_NOOP("Reboot") },
{ "chooser", I18N_NOOP("XDMCP Chooser") },
{ "config", I18N_NOOP("Configure") },
{ "caps-lock-warning", I18N_NOOP("You have got caps lock on.") },
{ "timed-label", I18N_NOOP("User %s will login in %d seconds") },
{ "welcome-label", I18N_NOOP("Welcome to %h") }, // _greetString
{ "username-label", I18N_NOOP("Username:") },
{ "password-label", I18N_NOOP("Password:") },
{ "login", I18N_NOOP("Login") }
};
QString
KdmLabel::lookupStock( const QString &stock )
{
//FIXME add key accels!
QString type( stock.lower() );
for (uint i = 0; i < sizeof(stocks)/sizeof(stocks[0]); i++)
if (type == stocks[i].type)
return i18n(stocks[i].text);
kdDebug() << "Invalid <stock> element. Check your theme!" << endl;
return stock;
}
QString
KdmLabel::lookupText( const QString &t )
{
QString text = t;
text.replace( '_', '&' );
// text.remove( '_' ); // FIXME add key accels, remove underscores for now
QMap<QChar,QString> m;
struct utsname uts;
uname( &uts );
m['n'] = QString::fromLocal8Bit( uts.nodename );
char buf[256];
buf[sizeof(buf) - 1] = '\0';
m['h'] = gethostname( buf, sizeof(buf) - 1 ) ? "localhost" : QString::fromLocal8Bit( buf );
#ifdef HAVE_GETDOMAINNAME
m['o'] = getdomainname( buf, sizeof(buf) - 1 ) ? "localdomain" : QString::fromLocal8Bit( buf );
#elif defined(HAVE_SYSINFO)
m['o'] = (unsigned)sysinfo( SI_SRPC_DOMAIN, buf, sizeof(buf) ) > sizeof(buf) ? "localdomain" : QString::fromLocal8Bit( buf );
#endif
m['d'] = QString::number( KThemedGreeter::timedDelay );
m['s'] = KThemedGreeter::timedUser;
// xgettext:no-c-format
KGlobal::locale()->setDateFormat( i18n("date format", "%a %d %B") );
m['c'] = KGlobal::locale()->formatDateTime( QDateTime::currentDateTime(), false, false );
return KMacroExpander::expandMacros( text, m );
}
#include "kdmlabel.moc"
|