• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdeio
 

tdeio/tdeio

  • tdeio
  • tdeio
passdlg.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000 David Faure <faure@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "passdlg.h"
20 
21 #include <tqapplication.h>
22 #include <tqcheckbox.h>
23 #include <tqhbox.h>
24 #include <tqlabel.h>
25 #include <tqlayout.h>
26 #include <tqsimplerichtext.h>
27 #include <tqstylesheet.h>
28 
29 #include <kcombobox.h>
30 #include <tdeconfig.h>
31 #include <kiconloader.h>
32 #include <klineedit.h>
33 #include <tdelocale.h>
34 #include <kstandarddirs.h>
35 
36 using namespace TDEIO;
37 
38 struct PasswordDialog::PasswordDialogPrivate
39 {
40  TQGridLayout *layout;
41  TQLineEdit* userEdit;
42  KLineEdit* passEdit;
43  TQLabel* userNameLabel;
44  TQLabel* prompt;
45  TQCheckBox* keepCheckBox;
46  TQMap<TQString,TQString> knownLogins;
47  KComboBox* userEditCombo;
48  TQHBox* userNameHBox;
49 
50  bool keep;
51  short unsigned int nRow;
52 };
53 
54 PasswordDialog::PasswordDialog( const TQString& prompt, const TQString& user,
55  bool enableKeep, bool modal, TQWidget* parent,
56  const char* name )
57  :KDialogBase( parent, name, modal, i18n("Password"), Ok|Cancel, Ok, true)
58 {
59  init ( prompt, user, enableKeep );
60 }
61 
62 PasswordDialog::~PasswordDialog()
63 {
64  delete d;
65 }
66 
67 void PasswordDialog::init( const TQString& prompt, const TQString& user,
68  bool enableKeep )
69 {
70  TQWidget *main = makeMainWidget();
71 
72  d = new PasswordDialogPrivate;
73  d->keep = false;
74  d->nRow = 0;
75  d->keepCheckBox = 0;
76 
77  TDEConfig* cfg = TDEGlobal::config();
78  TDEConfigGroupSaver saver( cfg, "Passwords" );
79 
80  d->layout = new TQGridLayout( main, 9, 3, spacingHint(), marginHint());
81  d->layout->addColSpacing(1, 5);
82 
83  // Row 0: pixmap prompt
84  TQLabel* lbl;
85  TQPixmap pix( TDEGlobal::iconLoader()->loadIcon( "password", TDEIcon::NoGroup, TDEIcon::SizeHuge, 0, 0, true));
86  if ( !pix.isNull() )
87  {
88  lbl = new TQLabel( main );
89  lbl->setPixmap( pix );
90  lbl->setAlignment( Qt::AlignLeft|Qt::AlignVCenter );
91  lbl->setFixedSize( lbl->sizeHint() );
92  d->layout->addWidget( lbl, 0, 0, Qt::AlignLeft );
93  }
94  d->prompt = new TQLabel( main );
95  d->prompt->setAlignment( Qt::AlignLeft|Qt::AlignVCenter|TQt::WordBreak );
96  d->layout->addWidget( d->prompt, 0, 2, Qt::AlignLeft );
97  if ( prompt.isEmpty() )
98  setPrompt( i18n( "You need to supply a username and a password" ) );
99  else
100  setPrompt( prompt );
101 
102  // Row 1: Row Spacer
103  d->layout->addRowSpacing( 1, 7 );
104 
105  // Row 2-3: Reserved for an additional comment
106 
107  // Row 4: Username field
108  d->userNameLabel = new TQLabel( i18n("&Username:"), main );
109  d->userNameLabel->setAlignment( Qt::AlignVCenter | Qt::AlignLeft );
110  d->userNameLabel->setFixedSize( d->userNameLabel->sizeHint() );
111  d->userNameHBox = new TQHBox( main );
112 
113  d->userEdit = new KLineEdit( d->userNameHBox );
114  TQSize s = d->userEdit->sizeHint();
115  d->userEdit->setFixedHeight( s.height() );
116  d->userEdit->setMinimumWidth( s.width() );
117  d->userNameLabel->setBuddy( d->userEdit );
118  d->layout->addWidget( d->userNameLabel, 4, 0 );
119  d->layout->addWidget( d->userNameHBox, 4, 2 );
120 
121  // Row 5: Row spacer
122  d->layout->addRowSpacing( 5, 4 );
123 
124  // Row 6: Password field
125  lbl = new TQLabel( i18n("&Password:"), main );
126  lbl->setAlignment( Qt::AlignVCenter | Qt::AlignLeft );
127  lbl->setFixedSize( lbl->sizeHint() );
128  TQHBox* hbox = new TQHBox( main );
129  d->passEdit = new KLineEdit( hbox );
130  if ( cfg->readEntry("EchoMode", "OneStar") == "NoEcho" )
131  d->passEdit->setEchoMode( TQLineEdit::NoEcho );
132  else
133  d->passEdit->setEchoMode( TQLineEdit::Password );
134  s = d->passEdit->sizeHint();
135  d->passEdit->setFixedHeight( s.height() );
136  d->passEdit->setMinimumWidth( s.width() );
137  lbl->setBuddy( d->passEdit );
138  d->layout->addWidget( lbl, 6, 0 );
139  d->layout->addWidget( hbox, 6, 2 );
140 
141  if ( enableKeep )
142  {
143  // Row 7: Add spacer
144  d->layout->addRowSpacing( 7, 4 );
145  // Row 8: Keep Password
146  hbox = new TQHBox( main );
147  d->keepCheckBox = new TQCheckBox( i18n("&Keep password"), hbox );
148  d->keepCheckBox->setFixedSize( d->keepCheckBox->sizeHint() );
149  d->keep = cfg->readBoolEntry("Keep", false );
150  d->keepCheckBox->setChecked( d->keep );
151  connect(d->keepCheckBox, TQT_SIGNAL(toggled( bool )), TQT_SLOT(slotKeep( bool )));
152  d->layout->addWidget( hbox, 8, 2 );
153  }
154 
155  // Configure necessary key-bindings and connect necessar slots and signals
156  connect( d->userEdit, TQT_SIGNAL(returnPressed()), d->passEdit, TQT_SLOT(setFocus()) );
157  connect( d->passEdit, TQT_SIGNAL(returnPressed()), TQT_SLOT(slotOk()) );
158 
159  if ( !user.isEmpty() )
160  {
161  d->userEdit->setText( user );
162  d->passEdit->setFocus();
163  }
164  else
165  d->userEdit->setFocus();
166 
167  d->userEditCombo = 0;
168 // setFixedSize( sizeHint() );
169 }
170 
171 TQString PasswordDialog::username() const
172 {
173  return d->userEdit->text();
174 }
175 
176 TQString PasswordDialog::password() const
177 {
178  return d->passEdit->text();
179 }
180 
181 void PasswordDialog::setKeepPassword( bool b )
182 {
183  if ( d->keepCheckBox )
184  d->keepCheckBox->setChecked( b );
185 }
186 
187 bool PasswordDialog::keepPassword() const
188 {
189  return d->keep;
190 }
191 
192 static void calculateLabelSize(TQLabel *label)
193 {
194  TQString qt_text = label->text();
195 
196  int pref_width = 0;
197  int pref_height = 0;
198  // Calculate a proper size for the text.
199  {
200  TQSimpleRichText rt(qt_text, label->font());
201  TQRect d = TDEGlobalSettings::desktopGeometry(label->topLevelWidget());
202 
203  pref_width = d.width() / 4;
204  rt.setWidth(pref_width-10);
205  int used_width = rt.widthUsed();
206  pref_height = rt.height();
207  if (used_width <= pref_width)
208  {
209  while(true)
210  {
211  int new_width = (used_width * 9) / 10;
212  rt.setWidth(new_width-10);
213  int new_height = rt.height();
214  if (new_height > pref_height)
215  break;
216  used_width = rt.widthUsed();
217  if (used_width > new_width)
218  break;
219  }
220  pref_width = used_width;
221  }
222  else
223  {
224  if (used_width > (pref_width *2))
225  pref_width = pref_width *2;
226  else
227  pref_width = used_width;
228  }
229  }
230  label->setFixedSize(TQSize(pref_width+10, pref_height));
231 }
232 
233 void PasswordDialog::addCommentLine( const TQString& label,
234  const TQString comment )
235 {
236  if (d->nRow > 0)
237  return;
238 
239  TQWidget *main = mainWidget();
240 
241  TQLabel* lbl = new TQLabel( label, main);
242  lbl->setAlignment( Qt::AlignVCenter|Qt::AlignRight );
243  lbl->setFixedSize( lbl->sizeHint() );
244  d->layout->addWidget( lbl, d->nRow+2, 0, Qt::AlignLeft );
245  lbl = new TQLabel( comment, main);
246  lbl->setAlignment( Qt::AlignVCenter|Qt::AlignLeft|TQt::WordBreak );
247  calculateLabelSize(lbl);
248  d->layout->addWidget( lbl, d->nRow+2, 2, Qt::AlignLeft );
249  d->layout->addRowSpacing( 3, 10 ); // Add a spacer
250  d->nRow++;
251 }
252 
253 void PasswordDialog::slotKeep( bool keep )
254 {
255  d->keep = keep;
256 }
257 
258 static TQString qrichtextify( const TQString& text )
259 {
260  if ( text.isEmpty() || text[0] == '<' )
261  return text;
262 
263  TQStringList lines = TQStringList::split('\n', text);
264  for(TQStringList::Iterator it = lines.begin(); it != lines.end(); ++it)
265  {
266  *it = TQStyleSheet::convertFromPlainText( *it, TQStyleSheetItem::WhiteSpaceNormal );
267  }
268 
269  return lines.join(TQString::null);
270 }
271 
272 void PasswordDialog::setPrompt(const TQString& prompt)
273 {
274  TQString text = qrichtextify(prompt);
275  d->prompt->setText(text);
276  calculateLabelSize(d->prompt);
277 }
278 
279 void PasswordDialog::setPassword(const TQString &p)
280 {
281  d->passEdit->setText(p);
282 }
283 
284 void PasswordDialog::setUserReadOnly( bool readOnly )
285 {
286  d->userEdit->setReadOnly( readOnly );
287  if ( readOnly && d->userEdit->hasFocus() )
288  d->passEdit->setFocus();
289 }
290 
291 void PasswordDialog::setKnownLogins( const TQMap<TQString, TQString>& knownLogins )
292 {
293  const int nr = knownLogins.count();
294  if ( nr == 0 )
295  return;
296  if ( nr == 1 ) {
297  d->userEdit->setText( knownLogins.begin().key() );
298  setPassword( knownLogins.begin().data() );
299  return;
300  }
301 
302  Q_ASSERT( !d->userEdit->isReadOnly() );
303  if ( !d->userEditCombo ) {
304  delete d->userEdit;
305  d->userEditCombo = new KComboBox( true, d->userNameHBox );
306  d->userEdit = d->userEditCombo->lineEdit();
307  TQSize s = d->userEditCombo->sizeHint();
308  d->userEditCombo->setFixedHeight( s.height() );
309  d->userEditCombo->setMinimumWidth( s.width() );
310  d->userNameLabel->setBuddy( d->userEditCombo );
311  d->layout->addWidget( d->userNameHBox, 4, 2 );
312  }
313 
314  d->knownLogins = knownLogins;
315  d->userEditCombo->insertStringList( knownLogins.keys() );
316  d->userEditCombo->setFocus();
317 
318  connect( d->userEditCombo, TQT_SIGNAL( activated( const TQString& ) ),
319  this, TQT_SLOT( slotActivated( const TQString& ) ) );
320 }
321 
322 void PasswordDialog::slotActivated( const TQString& userName )
323 {
324  TQMap<TQString, TQString>::ConstIterator it = d->knownLogins.find( userName );
325  if ( it != d->knownLogins.end() )
326  setPassword( it.data() );
327 }
328 
329 
330 int PasswordDialog::getNameAndPassword( TQString& user, TQString& pass, bool* keep,
331  const TQString& prompt, bool readOnly,
332  const TQString& caption,
333  const TQString& comment,
334  const TQString& label )
335 {
336  PasswordDialog* dlg;
337  if( keep )
338  dlg = new PasswordDialog( prompt, user, (*keep) );
339  else
340  dlg = new PasswordDialog( prompt, user );
341 
342  if ( !caption.isEmpty() )
343  dlg->setPlainCaption( caption );
344  else
345  dlg->setPlainCaption( i18n("Authorization Dialog") );
346 
347  if ( !comment.isEmpty() )
348  dlg->addCommentLine( label, comment );
349 
350  if ( readOnly )
351  dlg->setUserReadOnly( readOnly );
352 
353  int ret = dlg->exec();
354  if ( ret == Accepted )
355  {
356  user = dlg->username();
357  pass = dlg->password();
358  if ( keep ) { (*keep) = dlg->keepPassword(); }
359  }
360  delete dlg;
361  return ret;
362  }
363 
364 void PasswordDialog::virtual_hook( int id, void* data )
365 { KDialogBase::virtual_hook( id, data ); }
366 
367 #include "passdlg.moc"
TDEIO::PasswordDialog::setPrompt
void setPrompt(const TQString &prompt)
Sets the prompt to show to the user.
Definition: passdlg.cpp:272
TDEIO::PasswordDialog::setKeepPassword
void setKeepPassword(bool b)
Check or uncheck the "keep password" checkbox.
Definition: passdlg.cpp:181
TDEIO::PasswordDialog::username
TQString username() const
Returns the username entered by the user.
Definition: passdlg.cpp:171
TDEIO::PasswordDialog::setPassword
void setPassword(const TQString &password)
Presets the password.
Definition: passdlg.cpp:279
TDEIO
A namespace for TDEIO globals.
Definition: authinfo.h:29
TDEIO::PasswordDialog::setKnownLogins
void setKnownLogins(const TQMap< TQString, TQString > &knownLogins)
Presets a number of login+password pairs that the user can choose from.
Definition: passdlg.cpp:291
TDEIO::PasswordDialog::getNameAndPassword
static int getNameAndPassword(TQString &user, TQString &pass, bool *keep, const TQString &prompt=TQString::null, bool readOnly=false, const TQString &caption=TQString::null, const TQString &comment=TQString::null, const TQString &label=TQString::null)
A convienence static method for obtaining authorization information from the end user.
Definition: passdlg.cpp:330
TDEIO::PasswordDialog::addCommentLine
void addCommentLine(const TQString &label, const TQString comment)
Adds a comment line to the dialog.
Definition: passdlg.cpp:233
TDEIO::PasswordDialog::keepPassword
bool keepPassword() const
Determines whether supplied authorization should persist even after the application has been closed...
Definition: passdlg.cpp:187
TDEIO::PasswordDialog::setUserReadOnly
void setUserReadOnly(bool readOnly)
Sets the username field read-only and sets the focus to the password field.
Definition: passdlg.cpp:284
TDEIO::PasswordDialog::PasswordDialog
PasswordDialog(const TQString &prompt, const TQString &user, bool enableKeep=false, bool modal=true, TQWidget *parent=0, const char *name=0)
Create a password dialog.
Definition: passdlg.cpp:54
TDEIO::PasswordDialog
A dialog for requesting a login and a password from the end user.
Definition: passdlg.h:36
TDEIO::PasswordDialog::password
TQString password() const
Returns the password entered by the user.
Definition: passdlg.cpp:176
TDEIO::PasswordDialog::~PasswordDialog
~PasswordDialog()
Destructor.
Definition: passdlg.cpp:62

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdeio by doxygen 1.8.8
This website is maintained by Timothy Pearson.