Qtopia library API Documentation

password.cpp

00001 /**********************************************************************
00002 ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of the Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 #include "password.h"
00021 #include "config.h"
00022 #include "global.h"
00023 #include "backend/contact.h"
00024 #include <qlabel.h>
00025 #include <qlineedit.h>
00026 #include <qtextview.h>
00027 #include <qstring.h>
00028 #include <qapplication.h>
00029 #include <qfile.h>
00030 //#include <qwindowsystem_qws.h>
00031 
00032 #include <qdialog.h>
00033 
00034 #include <unistd.h> //for sleep
00035 #include "passwordbase_p.h"
00036 
00037 class PasswordDialog : public PasswordBase
00038 {
00039     Q_OBJECT
00040 
00041 public:
00042     PasswordDialog( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
00043     ~PasswordDialog();
00044 
00045     void clear();
00046     void setPrompt( const QString& );
00047 
00048 signals:
00049     void passwordEntered( const QString& );
00050 
00051 protected:
00052     bool eventFilter( QObject*, QEvent* );
00053     void keyPressEvent( QKeyEvent * );
00054 
00055 private:
00056     void input( QString );
00057     friend class Password;
00058     QString text;
00059 };
00060 
00061 
00062 extern "C" char *crypt(const char *key, const char *salt);
00063 static QString qcrypt(const QString& k, const char *salt)
00064 {
00065     return QString::fromUtf8(crypt(k.utf8(),salt));
00066 }
00067 
00068 /*
00069  *  Constructs a PasswordDialog which is a child of 'parent', with the
00070  *  name 'name' and widget flags set to 'f'
00071  */
00072 PasswordDialog::PasswordDialog( QWidget* parent,  const char* name, WFlags fl )
00073     : PasswordBase( parent, name, fl )
00074 {
00075     QRect desk = qApp->desktop()->geometry();
00076 
00077     if ( desk.width() < 220 ) {
00078     QFont f( font() );
00079     f.setPointSize( 18 );
00080     setFont( f );
00081     f.setPointSize( 12 );
00082     prompt->setFont( f );
00083     }
00084 
00085     button_0->installEventFilter( this );
00086     button_1->installEventFilter( this );
00087     button_2->installEventFilter( this );
00088     button_3->installEventFilter( this );
00089     button_4->installEventFilter( this );
00090     button_5->installEventFilter( this );
00091     button_6->installEventFilter( this );
00092     button_7->installEventFilter( this );
00093     button_8->installEventFilter( this );
00094     button_9->installEventFilter( this );
00095     button_OK->installEventFilter( this );
00096     setFocus();
00097 }
00098 
00099 /*
00100  *  Destroys the object and frees any allocated resources
00101  */
00102 PasswordDialog::~PasswordDialog()
00103 {
00104     // no need to delete child widgets, Qt does it all for us
00105 }
00106 
00107 
00108 
00113 bool PasswordDialog::eventFilter( QObject*o, QEvent*e )
00114 {
00115     if ( e->type() == QEvent::MouseButtonRelease ) {
00116     if ( o == button_OK ) {
00117         emit passwordEntered( text );
00118     } else {
00119         QLabel *l = (QLabel*)o;
00120         input(l->text());
00121     }
00122     }
00123     return FALSE;
00124 }
00125 
00126 
00131 void PasswordDialog::keyPressEvent( QKeyEvent * )
00132 {
00133 #if 0
00134     if ( e->key() == Key_Enter || e->key() == Key_Return )
00135     emit passwordEntered( text );
00136     else
00137     input( e->text() );
00138 #endif
00139 }
00140 
00141 
00146 void PasswordDialog::input( QString c )
00147 {
00148     text += c;
00149     display->setText( text );
00150 }
00151 
00156 void PasswordDialog::setPrompt( const QString& s )
00157 {
00158     prompt->setText( s );
00159 }
00160 
00161 void PasswordDialog::clear()
00162 {
00163     text = "";
00164     input("");
00165 }
00166 
00167 class PasswdDlg : public QDialog
00168 {
00169 public:
00170     PasswdDlg( QWidget *parent, const char * name, bool modal, bool fullscreen = FALSE )
00171     : QDialog( parent, name, modal, fullscreen ? WStyle_NoBorder | WStyle_Customize | WStyle_StaysOnTop : 0 ),
00172         modl(modal)
00173     {
00174     passw = new PasswordDialog( this );
00175 
00176     if ( fullscreen ) {
00177         QRect desk = qApp->desktop()->geometry();
00178         setGeometry( 0, 0, desk.width(), desk.height() );
00179     }
00180 
00181     connect( passw, SIGNAL(passwordEntered(const QString&)),
00182          this, SLOT(accept()) );
00183     }
00184 
00185     void resizeEvent( QResizeEvent * )
00186     {
00187     passw->resize( size() );
00188     }
00189 
00190     void reset()
00191     {
00192     passw->clear();
00193     }
00194 
00195     void execNonModal()
00196     {
00197     if ( !modl ) {
00198         reset();
00199         showFullScreen();
00200         do {
00201         qApp->enter_loop();
00202         } while (result()!=1);
00203     }
00204     }
00205 
00206     void accept()
00207     {
00208     if ( !modl )
00209         qApp->exit_loop();
00210     QDialog::accept();
00211     }
00212 
00213     PasswordDialog *passw;
00214     bool modl;
00215 };
00216 
00217 class OwnerDlg : public QDialog
00218 {
00219     Q_OBJECT
00220 public:
00221 
00222     OwnerDlg( QWidget *parent, const char * name, Contact c,
00223         bool modal, bool fullscreen = FALSE )
00224     : QDialog( parent, name, modal,
00225         fullscreen ?
00226         WStyle_NoBorder | WStyle_Customize | WStyle_StaysOnTop : 0 )
00227     {
00228     if ( fullscreen ) {
00229         QRect desk = qApp->desktop()->geometry();
00230         setGeometry( 0, 0, desk.width(), desk.height() );
00231     }
00232 
00233     // set up contents.
00234     QString text = "<H1>" + tr("Owner Information") + "</H1>";
00235     text += c.toRichText();
00236     tv = new QTextView(this);
00237     tv->setText(text);
00238 
00239     tv->viewport()->installEventFilter(this);
00240     }
00241 
00242     void resizeEvent( QResizeEvent * )
00243     {
00244     tv->resize( size() );
00245     }
00246 
00247     bool eventFilter(QObject *o, QEvent *e)
00248     {
00249     if (e->type() == QEvent::KeyPress || e->type() == QEvent::MouseButtonPress ) {
00250         accept();
00251         return TRUE;
00252     }
00253     return QWidget::eventFilter(o, e);
00254     }
00255 
00256     void mousePressEvent( QMouseEvent * ) { accept(); }
00257 
00258 private:
00259     QTextView *tv;
00260 };
00261 
00269 QString Password::getPassword( const QString& prompt )
00270 {
00271     PasswdDlg pd(0,0,TRUE);
00272     pd.passw->setPrompt( prompt );
00273 
00274     pd.showMaximized();
00275     int r = pd.exec();
00276 
00277     if ( r == QDialog::Accepted ) {
00278     if (pd.passw->text.isEmpty())
00279         return "";
00280     else
00281         return qcrypt(pd.passw->text,"a0");
00282     } else {
00283     return QString::null;
00284     }
00285 }
00286 
00287 
00297 bool Password::needToAuthenticate(bool at_poweron)
00298 {
00299     Config cfg("Security");
00300     cfg.setGroup("Passcode");
00301     QString passcode = cfg.readEntry("passcode");
00302 
00303     return ( !passcode.isEmpty()
00304         && (!at_poweron || cfg.readNumEntry("passcode_poweron",0)) );
00305 }
00306 
00315 void Password::authenticate(bool at_poweron)
00316 {
00317     Config cfg("Security");
00318     cfg.setGroup("Passcode");
00319     QString passcode = cfg.readEntry("passcode");
00320     if ( !passcode.isEmpty()
00321         && (!at_poweron || cfg.readNumEntry("passcode_poweron",0)) )
00322     {
00323     // Do it as a fullscreen modal dialog
00324     PasswdDlg pd(0,0,TRUE,TRUE);
00325 
00326     // see if there is contact information.
00327     OwnerDlg *oi = 0;
00328     QString vfilename = Global::applicationFileName("addressbook",
00329         "businesscard.vcf");
00330     if (QFile::exists(vfilename)) {
00331         Contact c;
00332         c = Contact::readVCard( vfilename )[0];
00333 
00334         oi = new OwnerDlg(0, 0, c, TRUE, TRUE);
00335     }
00336 
00337     pd.reset();
00338     pd.exec();
00339     while (qcrypt(pd.passw->text, "a0") != passcode) {
00340         if (oi)
00341         oi->exec();
00342         pd.reset();
00343         pd.exec();
00344     }
00345     } else if ( at_poweron ) {
00346     // refresh screen   #### should probably be in caller
00347     // Not needed (we took away the screen blacking) TT
00348     // Not needed (we have intelligent screen blacking) sandman
00349     //if ( qwsServer )
00350         //qwsServer->refresh();
00351     }
00352 }
00353 
00354 #include "password.moc"
KDE Logo
This file is part of the documentation for OPIE Version 1.5.5.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:24:06 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001