summaryrefslogtreecommitdiffstats
path: root/kdm/kfrontend/themer/kdmlabel.cpp
blob: b36b7e5ad9a9e88ab0ecccc764d3d2784870e608 (plain)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 *  Copyright (C) 2003 by Unai Garro <ugarro@users.sourceforge.net>
 *  Copyright (C) 2004 by Enrico Ros <rosenric@dei.unipd.it>
 *  Copyright (C) 2004 by Stephan Kulow <coolo@kde.org>
 *  Copyright (C) 2004 by Oswald Buddenhagen <ossi@kde.org>
 *
 *  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 <config.h>
#include "kdmlabel.h"
#include "kdmconfig.h"
#include "../kgreeter.h"

#include <kglobal.h>
#include <klocale.h>
#include <kmacroexpander.h>
#include <kdebug.h>

#include <tqdatetime.h>
#include <tqpainter.h>
#include <tqfontmetrics.h>
#include <tqtimer.h>
#include <tqaccel.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 TQDomNode &node, const char *name )
       : KdmItem( parent, node, name ), myAccel(0)
{
	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 TQString locale = KGlobal::locale()->language();

	// Read LABEL ID
	TQDomNode n = node;
	TQDomElement 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
	TQDomNodeList childList = node.childNodes();
	bool stockUsed = false;
	for (uint nod = 0; nod < childList.count(); nod++) {
		TQDomNode child = childList.item( nod );
		TQDomElement el = child.toElement();
		TQString 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) {
			TQString 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.tqfind( "%c" ) >= 0;
	if (label.isTimer) {
		timer = new TQTimer( this );
		timer->start( 1000 );
		connect( timer, TQT_SIGNAL(timeout()), TQT_SLOT(update()) );
	}
	setTextInt( lookupText( label.text ) );
}

void
KdmLabel::setTextInt( const TQString &txt)
{
  // TODO: catch &&
        cText = txt;
	cAccel = txt.tqfind('&');
	delete myAccel;
	myAccel = 0;
	if (cAccel != -1) {
	  cText.remove('&');
	  myAccel = new TQAccel(tqparentWidget());
	  myAccel->insertItem(ALT + UNICODE_ACCEL + cText.tqat(cAccel).lower().tqunicode());
	  connect(myAccel, TQT_SIGNAL(activated(int)), TQT_SLOT(slotAccel()));
	}
}

void
KdmLabel::slotAccel()
{
  if (buttonParent)
    emit activated(buttonParent->getId());
  else
    emit activated(id);
}

void
KdmLabel::setText( const TQString &txt )
{
	label.text = txt;
	setTextInt( lookupText( label.text ) );
}

TQSize
KdmLabel::tqsizeHint()
{
	// 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
	TQSize hint = TQFontMetrics( 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( TQPainter *p, const TQRect &/*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..)
        if (cAccel != -1 && (!id.isEmpty() || buttonParent) ) {
	  TQString left = cText.left(cAccel);
	  TQString right = cText.mid(cAccel + 1);
	  p->drawText( area, AlignLeft | SingleLine, left );
	  TQRect tarea = area;
	  TQFontMetrics fm(l->font);
	  tarea.rLeft() += fm.width(left);
	  TQFont f(l->font);
	  f.setUnderline(true);
	  p->setFont ( f );
	  p->drawText( tarea, AlignLeft | SingleLine, TQString(cText.tqat(cAccel)));
	  tarea.rLeft() += fm.width(cText.tqat(cAccel));
	  p->setFont( l->font );
	  p->drawText( tarea, AlignLeft | SingleLine, right);
        } else {
	  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()
{
	TQString text = lookupText( label.text );
	if (text != cText) {
	        setTextInt(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");
        { "admin",              I18N_NOOP("&Administration") },
	{ "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("Caps Lock is enabled.") },
	{ "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:") },
        { "domain-label",       I18N_NOOP("Domain:") },
	{ "login",              I18N_NOOP("Login") }
};

TQString
KdmLabel::lookupStock( const TQString &stock )
{
	//FIXME add key accels!
	TQString 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() << timestamp() << " Invalid <stock> element. Check your theme!" << endl;
	return stock;
}

TQString
KdmLabel::lookupText( const TQString &t )
{
	TQString text = t;

	text.tqreplace( '_', '&' );

	TQMap<TQChar,TQString> m;
	struct utsname uts;
	uname( &uts );
	m['n'] = TQString::fromLocal8Bit( uts.nodename );
	char buf[256];
	buf[sizeof(buf) - 1] = '\0';
	m['h'] = gethostname( buf, sizeof(buf) - 1 ) ? "localhost" : TQString::fromLocal8Bit( buf );
#ifdef HAVE_GETDOMAINNAME
	m['o'] = getdomainname( buf, sizeof(buf) - 1 ) ? "localdomain" : TQString::fromLocal8Bit( buf );
#elif defined(HAVE_SYSINFO)
	m['o'] = (unsigned)sysinfo( SI_SRPC_DOMAIN, buf, sizeof(buf) ) > sizeof(buf) ? "localdomain" : TQString::fromLocal8Bit( buf );
#endif
	m['d'] = TQString::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( TQDateTime::tqcurrentDateTime(), false, false );

	return KMacroExpander::expandMacros( text, m );
}

#include "kdmlabel.moc"