• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

yapet/passworddialog.cc

Go to the documentation of this file.
00001 // $Id: passworddialog.cc 3350 2010-09-17 21:54:13Z java $
00002 //
00003 // Copyright (C) 2008-2010  Rafael Ostertag
00004 //
00005 // This file is part of YAPET.
00006 //
00007 // YAPET is free software: you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the Free Software
00009 // Foundation, either version 3 of the License, or (at your option) any later
00010 // version.
00011 //
00012 // YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
00013 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 // FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00015 // details.
00016 //
00017 // You should have received a copy of the GNU General Public License along with
00018 // YAPET.  If not, see <http://www.gnu.org/licenses/>.
00019 //
00020 
00021 #include "../intl.h"
00022 #include "colors.h"
00023 #include "passworddialog.h"
00024 #include "messagebox.h"
00025 
00026 #ifdef HAVE_ASSERT_H
00027 # include <assert.h>
00028 #endif
00029 
00030 #ifdef HAVE_SETJMP_H
00031 # include <setjmp.h>
00032 #endif
00033 
00034 sigjmp_buf password_dialog_sig_jmp_buf;
00035 
00036 extern "C" void __alarm_handler(int) {
00037     siglongjmp(password_dialog_sig_jmp_buf, 1);
00038 }
00039 
00040 void
00041 PasswordDialog::createWindow() throw (YAPET::UI::UIException) {
00042     if (window != NULL)
00043     throw YAPET::UI::UIException (_ ("May you consider deleting the window before reallocating") );
00044 
00045     window = newwin (getHeight(), getWidth(), getStartY(), getStartX() );
00046 
00047     if (window == NULL)
00048     throw YAPET::UI::UIException (_ ("Error creating password dialog") );
00049 
00050     pwidget1 = new YAPET::UI::PasswordWidget (getStartX() + 1,
00051         getStartY() + 3,
00052         getWidth() - 2);
00053 
00054     if (pwtype == NEW_PW)
00055     pwidget2 = new YAPET::UI::PasswordWidget (getStartX() + 1,
00056         getStartY() + 5,
00057         getWidth() - 2);
00058 
00059     okbutton = new YAPET::UI::Button (_ ("OK"),
00060                       getStartX() + 1,
00061                       getStartY() + getHeight() - 2);
00062     cancelbutton = new YAPET::UI::Button (_ ("Cancel"),
00063                       getStartX() + okbutton->getLength() + 2,
00064                       getStartY() + getHeight() - 2);
00065     if (has_quitbutton) {
00066     quitbutton = new YAPET::UI::Button (_("Quit"),
00067                         getStartX() + 
00068                         okbutton->getLength() + 1 +
00069                         cancelbutton->getLength() + 2,
00070                         getStartY() + getHeight() - 2);
00071     }
00072 }
00073 
00074 PasswordDialog::PasswordDialog (PWTYPE pt, std::string fn, unsigned int tout, bool qb)
00075     throw (YAPET::UI::UIException) : window (NULL),
00076                      pwidget1 (NULL),
00077                      pwidget2 (NULL),
00078                      okbutton (NULL),
00079                      cancelbutton (NULL),
00080                      quitbutton (NULL),
00081                      pwtype (pt),
00082                      key (NULL),
00083                      filename (fn),
00084                      input_timeout (tout),
00085                      has_quitbutton (qb),
00086                      quit_pressed (false) {
00087     createWindow();
00088 }
00089 
00090 PasswordDialog::~PasswordDialog() {
00091     delete pwidget1;
00092 
00093     if (pwtype == NEW_PW)
00094     delete pwidget2;
00095 
00096     delete okbutton;
00097     delete cancelbutton;
00098     if (has_quitbutton) {
00099     assert(quitbutton != NULL);
00100     delete quitbutton;
00101     }
00102     wclear (window);
00103     delwin (window);
00104 }
00105 
00106 void
00107 PasswordDialog::run() throw (YAPET::UI::UIException) {
00108     refresh();
00109 
00110     if (pwtype == EXISTING_PW || input_timeout > 0) {
00111     int retval;
00112 
00113     retval = sigemptyset(&my_sigset);
00114     if ( retval == -1 ) {
00115         throw YAPET::UI::UIException("Error calling sigemptyset()");
00116     }
00117     retval = sigaddset(&my_sigset, SIGALRM);
00118     if ( retval == -1 ) {
00119         throw YAPET::UI::UIException("Error calling sigaddset()");
00120     }
00121     retval = sigprocmask(SIG_UNBLOCK, &my_sigset, &old_sigset);
00122     if ( retval == -1 ) {
00123         throw YAPET::UI::UIException("Error calling sigprocmask()");
00124     }
00125 
00126     my_sigaction.sa_handler=__alarm_handler;
00127     retval = sigfillset(&(my_sigaction.sa_mask));
00128     if ( retval == -1 ) {
00129         throw YAPET::UI::UIException("Error calling sigfillset()");
00130     }
00131     my_sigaction.sa_flags=0;
00132 
00133     if ( sigsetjmp(password_dialog_sig_jmp_buf,1) == 1) {
00134         sigaction(SIGALRM, &old_sigaction, NULL);
00135         sigprocmask(SIG_SETMASK, &old_sigset, NULL);
00136 
00137         if ( key != NULL ) {
00138         delete key;
00139         key = NULL;
00140         }
00141         if ( pwidget1 != NULL ) {
00142         pwidget1->clearText();
00143         }
00144         return;
00145     }
00146 
00147     retval = sigaction(SIGALRM, &my_sigaction, &old_sigaction);
00148     if ( retval == -1 ) {
00149         alarm(0);
00150         sigaction(SIGALRM, &old_sigaction, NULL);
00151         sigprocmask(SIG_SETMASK, &old_sigset, NULL);
00152         throw YAPET::UI::UIException("Error calling sigprocmask()");
00153     }
00154     alarm(0);
00155     }
00156 
00157     while (true) {
00158     int ch = 0;
00159     if (pwtype == EXISTING_PW || input_timeout > 0)
00160         alarm(input_timeout);
00161 
00162 #ifdef HAVE_WRESIZE
00163 
00164     while ( (ch = pwidget1->focus() ) == KEY_RESIZE)
00165         YAPET::UI::BaseWindow::resizeAll();
00166 
00167 #else // HAVE_WRESIZE
00168     ch = pwidget1->focus();
00169 #endif // HAVE_WRESIZE
00170 
00171     if (ch == KEY_ESC)
00172         goto BAILOUT;
00173 
00174     // Password input widget 2 only if we prompt for a new password
00175     if (pwtype == NEW_PW) {
00176 #ifdef HAVE_WRESIZE
00177 
00178         while ( (ch = pwidget2->focus() ) == KEY_RESIZE)
00179         YAPET::UI::BaseWindow::resizeAll();
00180 
00181 #else // HAVE_WRESIZE
00182         ch = pwidget2->focus();
00183 #endif // HAVE_WRESIZE
00184 
00185         if (ch == KEY_ESC)
00186         goto BAILOUT;
00187     }
00188 
00189 #ifdef HAVE_WRESIZE
00190 
00191     while ( (ch = okbutton->focus() ) == KEY_RESIZE)
00192         YAPET::UI::BaseWindow::resizeAll();
00193 
00194 #else // HAVE_WRESIZE
00195     ch = okbutton->focus();
00196 #endif // HAVE_WRESIZE
00197 
00198     if (ch == KEY_ESC)
00199         goto BAILOUT;
00200 
00201     if (ch == '\n') {
00202         if (pwtype == NEW_PW) {
00203         if (pwidget1->getText() == pwidget2->getText() ) {
00204             key = new YAPET::Key (pwidget1->getText().c_str() );
00205             goto BAILOUT;
00206         } else {
00207             YAPET::UI::MessageBox* errmsg = NULL;
00208 
00209             try {
00210             errmsg = new YAPET::UI::MessageBox (_ ("E R R O R"), _ ("Passwords do not match") );
00211             errmsg->run();
00212             delete errmsg;
00213             } catch (YAPET::UI::UIException&) {
00214             if (errmsg == NULL)
00215                 delete errmsg;
00216             }
00217 
00218             pwidget1->setText ("");
00219             pwidget2->setText ("");
00220             refresh();
00221             continue;
00222         }
00223         } else {
00224         key = new YAPET::Key (pwidget1->getText().c_str() );
00225         pwidget1->clearText();
00226         goto BAILOUT;
00227         }
00228     }
00229 
00230 #ifdef HAVE_WRESIZE
00231     while ( (ch = cancelbutton->focus() ) == KEY_RESIZE)
00232         YAPET::UI::BaseWindow::resizeAll();
00233 #else // HAVE_WRESIZE
00234     ch = cancelbutton->focus();
00235 #endif // HAVE_WRESIZE
00236     if (ch == '\n' || ch == KEY_ENTER || ch == KEY_ESC)
00237         goto BAILOUT;
00238 
00239     if (has_quitbutton) {
00240 #ifdef HAVE_WRESIZE
00241         while ( (ch = quitbutton->focus() ) == KEY_RESIZE)
00242         YAPET::UI::BaseWindow::resizeAll();
00243 #else // HAVE_WRESIZE
00244         ch = quitbutton->focus();
00245 #endif // HAVE_WRESIZE
00246         if (ch == '\n' || ch == KEY_ENTER) {
00247         quitPressed(true);
00248         goto BAILOUT;
00249         }
00250         if (ch == KEY_ESC) {
00251         quitPressed(false);
00252         goto BAILOUT;
00253         }
00254     }
00255     }
00256 
00257  BAILOUT:
00258     if (pwtype == EXISTING_PW || input_timeout > 0) {
00259     alarm(0);
00260     sigaction(SIGALRM, &old_sigaction, NULL);
00261     sigprocmask(SIG_SETMASK, &old_sigset, NULL);
00262     }
00263 }
00264 
00265 void
00266 PasswordDialog::resize() throw (YAPET::UI::UIException) {
00267     int retval = delwin (window);
00268 
00269     if (retval == ERR)
00270     throw YAPET::UI::UIException (_ ("Error deleting password dialog window") );
00271 
00272     pwidget1->clearText();
00273     delete pwidget1;
00274 
00275     if (pwtype == NEW_PW) {
00276     pwidget2->clearText();
00277     delete pwidget2;
00278     }
00279 
00280     delete okbutton;
00281     delete cancelbutton;
00282     if (has_quitbutton) {
00283     delete quitbutton;
00284     }
00285     window = NULL;
00286     pwidget1 = NULL;
00287     pwidget2 = NULL;
00288     okbutton = NULL;
00289     cancelbutton = NULL;
00290     quitbutton = NULL;
00291     createWindow();
00292 }
00293 
00294 void
00295 PasswordDialog::refresh() throw (YAPET::UI::UIException) {
00296     YAPET::UI::Colors::setcolor (window, YAPET::UI::MESSAGEBOX);
00297     int retval = werase (window);
00298 
00299     if (retval == ERR)
00300     throw YAPET::UI::UIException (_ ("Error clearing password dialog") );
00301 
00302     retval = box (window, 0, 0);
00303 
00304     if (retval == ERR)
00305     throw YAPET::UI::UIException (_ ("Error adding box") );
00306 
00307     retval = mymvwaddstr (window, 0, 2, _ ("P A S S W O R D") );
00308 
00309     if (retval == ERR)
00310     throw YAPET::UI::UIException (_ ("Error setting title") );
00311 
00312     // The label holding the file name
00313     retval = mymvwaddstr (window, 2, 1, filename.c_str() );
00314 
00315     if (retval == ERR)
00316     throw YAPET::UI::UIException (_ ("Error setting label") );
00317 
00318     if (pwtype == NEW_PW) {
00319     retval = mymvwaddstr (window, 1, 1, _ ("Enter new password for") );
00320 
00321     if (retval == ERR)
00322         throw YAPET::UI::UIException (_ ("Error setting label") );
00323 
00324     retval = mymvwaddstr (window, 4, 1, _ ("Confirm password") );
00325 
00326     if (retval == ERR)
00327         throw YAPET::UI::UIException (_ ("Error setting label") );
00328     } else {
00329     retval = mymvwaddstr (window, 1, 1, _ ("Enter password for") );
00330 
00331     if (retval == ERR)
00332         throw YAPET::UI::UIException (_ ("Error setting label") );
00333     }
00334 
00335     retval = wrefresh (window);
00336 
00337     if (retval == ERR)
00338     throw YAPET::UI::UIException (_ ("Error refreshing password dialog") );
00339 
00340     pwidget1->refresh();
00341 
00342     if (pwtype == NEW_PW)
00343     pwidget2->refresh();
00344 
00345     okbutton->refresh();
00346     cancelbutton->refresh();
00347     if (has_quitbutton)
00348     quitbutton->refresh();
00349 }

Generated on Sun Sep 19 2010 15:37:14 for YAPET by  doxygen 1.7.1