3 Conversation widget for kdm greeter
5 Copyright (C) 2008 Dirk Mueller <mueller@kde.org>
6 Copyright (C) 2008 Oswald Buddenhagen <ossi@kde.org>
8 based on classic kdm greeter:
10 Copyright (C) 1997, 1998, 2000 Steffen Hansen <hansen@kde.org>
11 Copyright (C) 2000-2003 Oswald Buddenhagen <ossi@kde.org>
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include "kgreet_generic.h"
33 #include <klineedit.h>
38 #include <QTextDocument>
40 extern KDE_EXPORT KGreeterPluginInfo kgreeterplugin_info
; // defined at bottom
44 class KDMPasswordEdit
: public KLineEdit
{
46 KDMPasswordEdit( QWidget
*parent
= 0 ) : KLineEdit( parent
)
49 setPasswordMode( true );
51 setEchoMode( ::echoMode
? Password
: NoEcho
);
52 setContextMenuPolicy( Qt::NoContextMenu
);
56 KGenericGreeter::KGenericGreeter( KGreeterPluginHandler
*_handler
,
58 const QString
&_fixedEntity
,
59 Function _func
, Context _ctx
) :
61 KGreeterPlugin( _handler
),
63 fixedUser( _fixedEntity
),
69 m_parentWidget
= new QWidget( parent
);
70 m_parentWidget
->setObjectName( "talker" );
71 // XXX set some minimal size
72 widgetList
<< m_parentWidget
;
73 m_grid
= new QGridLayout( m_parentWidget
);
74 m_grid
->setMargin( 0 );
76 if (ctx
== ExUnlock
|| ctx
== ExChangeTok
)
77 fixedUser
= KUser().loginName();
81 KGenericGreeter::~KGenericGreeter()
84 delete m_parentWidget
;
88 KGenericGreeter::loadUsers( const QStringList
&users
)
94 KGenericGreeter::presetEntity( const QString
&entity
, int /* field */ )
96 // assert( !running );
101 KGenericGreeter::getEntity() const
103 return fixedUser
.isEmpty() ? curUser
: fixedUser
;
107 KGenericGreeter::setUser( const QString
&user
)
109 // assert( running );
110 // assert( fixedUser.isEmpty() );
111 if (!(kgreeterplugin_info
.flags
& KGreeterPluginInfo::Presettable
))
112 return; // Not interested in PAM telling us who logged in
118 if (m_lineEdit
) { // could be null if plugin is misconfigured
119 m_lineEdit
->setText( user
);
120 m_lineEdit
->selectAll();
121 m_lineEdit
->setFocus();
126 KGenericGreeter::setEnabled( bool enable
)
128 // assert( func == Authenticate && ctx == Shutdown );
129 // XXX this is likely to bear some bogosity
130 foreach (QWidget
*w
, m_children
)
131 w
->setEnabled( enable
);
132 if (enable
&& m_lineEdit
)
133 m_lineEdit
->setFocus();
137 KGenericGreeter::textMessage( const char *text
, bool err
)
142 if (m_infoMsgs
.isEmpty())
144 QString qtext
= QString::fromUtf8( text
);
145 m_infoMsgs
.append( qtext
);
146 QLabel
*label
= new QLabel( qtext
, m_parentWidget
);
147 m_grid
->addWidget( label
, m_line
++, 0, 1, 2 );
148 m_children
.append( label
);
154 KGenericGreeter::textPrompt( const char *prompt
, bool echo
, bool /* nonBlocking */ )
158 func
!= Authenticate
||
159 !(kgreeterplugin_info
.flags
& KGreeterPluginInfo::Presettable
);
161 if (!exp
&& !fixedUser
.isEmpty()) {
162 handler
->gplugReturnText( fixedUser
.toLocal8Bit(),
163 KGreeterPluginHandler::IsUser
);
167 if (m_infoMsgs
.isEmpty())
172 QLabel
*label
= new QLabel( QString::fromUtf8( prompt
).trimmed() );
173 m_grid
->addWidget( label
, m_line
, 0 );
174 m_children
.append( label
);
177 m_lineEdit
= new KLineEdit
;
178 m_lineEdit
->setContextMenuPolicy( Qt::NoContextMenu
);
180 if (!m_users
.isEmpty()) {
181 KCompletion
*userNamesCompletion
= new KCompletion
;
182 userNamesCompletion
->setItems( m_users
);
183 m_lineEdit
->setCompletionObject( userNamesCompletion
);
184 m_lineEdit
->setAutoDeleteCompletionObject( true );
185 m_lineEdit
->setCompletionMode( KGlobalSettings::CompletionAuto
);
187 if (!curUser
.isEmpty()) {
188 m_lineEdit
->setText( curUser
);
189 m_lineEdit
->selectAll();
190 connect( m_lineEdit
, SIGNAL(selectionChanged()), SLOT(slotChanged()) );
192 connect( m_lineEdit
, SIGNAL(editingFinished()), SLOT(slotLoginLostFocus()) );
194 connect( m_lineEdit
, SIGNAL(editingFinished()), SLOT(slotChanged()) );
195 connect( m_lineEdit
, SIGNAL(textChanged( const QString
& )), SLOT(slotChanged()) );
197 m_lineEdit
= new KDMPasswordEdit
;
199 m_lineEdit
->setMinimumWidth(
200 m_lineEdit
->fontMetrics().width( "This is a long password" ) );
201 m_grid
->addWidget( m_lineEdit
, m_line
, 1 );
202 m_children
.append( m_lineEdit
);
204 m_lineEdit
->setFocus();
208 KGenericGreeter::binaryPrompt( const char *, bool )
215 KGenericGreeter::start()
219 handler
->gplugStart();
223 KGenericGreeter::suspend()
228 KGenericGreeter::resume()
233 KGenericGreeter::next()
236 m_lineEdit
->setEnabled( false );
237 QString text
= m_lineEdit
->text();
239 handler
->gplugReturnText( text
.toLocal8Bit(),
241 KGreeterPluginHandler::IsSecret
:
243 KGreeterPluginHandler::IsUser
: 0 );
248 KGenericGreeter::abort()
253 handler
->gplugReturnText( 0, 0 );
258 KGenericGreeter::succeeded()
260 failed(); // redefining terms :-D
264 KGenericGreeter::failed()
266 // assert( running || timed_login );
267 if (!m_infoMsgs
.isEmpty()) {
268 QString text
= "<qt>";
269 foreach (const QString
&msg
, m_infoMsgs
)
270 text
+= "<p>" + Qt::escape( msg
) + "</p>";
273 handler
->gplugMsgBox( QMessageBox::Information
, text
);
275 foreach (QWidget
*w
, m_children
)
276 w
->setEnabled( false );
283 KGenericGreeter::revive()
285 // assert( !running );
286 foreach (QWidget
*w
, m_children
)
295 KGenericGreeter::clear()
297 // assert( !running && !passwd1Edit );
299 curUser
= QString::null
;
305 KGenericGreeter::slotLoginLostFocus()
307 m_lineEdit
->setText( m_lineEdit
->text().trimmed() );
308 if (curUser
!= m_lineEdit
->text()) {
309 curUser
= m_lineEdit
->text();
310 handler
->gplugSetUser( curUser
);
315 KGenericGreeter::slotChanged()
317 handler
->gplugChanged();
321 static bool init( const QString
&,
322 QVariant (*getConf
)( void *, const char *, const QVariant
& ),
325 echoMode
= getConf( ctx
, "EchoMode", QVariant( -1 ) ).toInt();
326 // Fielded entities are not supported per se.
327 // This implies that the first field is the presettable entity, if any.
328 if (getConf( ctx
, "generic.Presettable", QVariant( false ) ).toBool())
329 kgreeterplugin_info
.flags
|= KGreeterPluginInfo::Presettable
;
330 KGlobal::locale()->insertCatalog( "kgreet_generic" );
334 static void done( void )
336 KGlobal::locale()->removeCatalog( "kgreet_generic" );
339 static KGreeterPlugin
*
340 create( KGreeterPluginHandler
*handler
,
342 const QString
&fixedEntity
,
343 KGreeterPlugin::Function func
,
344 KGreeterPlugin::Context ctx
)
346 return new KGenericGreeter( handler
, parent
, fixedEntity
, func
, ctx
);
349 KDE_EXPORT KGreeterPluginInfo kgreeterplugin_info
= {
350 I18N_NOOP2("@item:inmenu authentication method", "Generic"), "generic",
351 KGreeterPluginInfo::Local
,
355 #include "kgreet_generic.moc"