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

yapet/pwgendialog.cc

Go to the documentation of this file.
00001 // $Id: pwgendialog.cc 3343 2010-09-17 18:36:31Z java $
00002 //
00003 // Copyright (C) 2009-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 #ifdef HAVE_CONFIG_H
00022 # include <config.h>
00023 #endif
00024 
00025 #ifdef HAVE_ASSERT_H
00026 # include <assert.h>
00027 #endif
00028 
00029 #include "../intl.h"
00030 #include "cfg.h"
00031 #include "messagebox.h"
00032 #include "colors.h"
00033 #include "pwgendialog.h"
00034 #include "globals.h"
00035 #include "consts.h"
00036 #include "fileopen.h"
00037 #include "pwgen/pwgen.h"
00038 
00039 static const char* ckbx_strings[] = {
00040     "Letters",
00041     "Digits",
00042     "Punctuation",
00043     "Special",
00044     "Other",
00045     0
00046 };
00047 
00048 std::string
00049 PWGenDialog::getNameOfRNG() const {
00050     switch (pwgen.getRNGUsed() ) {
00051         case YAPET::PWGEN::DEVRANDOM:
00052             return "/dev/random";
00053         case YAPET::PWGEN::DEVURANDOM:
00054             return "/dev/urandom";
00055         case YAPET::PWGEN::LRAND48:
00056             return "lrand48()";
00057         case YAPET::PWGEN::RAND:
00058             return _ ("rand() (not good)");
00059         case YAPET::PWGEN::NONE:
00060             return "none!";
00061         case YAPET::PWGEN::AUTO:
00062         return "auto";
00063     }
00064 
00065     assert (0);
00066     return "oops!";
00067 }
00068 
00069 int
00070 PWGenDialog::CheckBoxOptions2Charpools (uint16_t o) const {
00071     int charpools = 0;
00072     // Refer to the ckbx_strings got see the order of the options
00073     charpools |= o & (1 << 0) ? YAPET::PWGEN::LETTERS : 0;
00074     charpools |= o & (1 << 1) ? YAPET::PWGEN::DIGITS : 0;
00075     charpools |= o & (1 << 2) ? YAPET::PWGEN::PUNCT : 0;
00076     charpools |= o & (1 << 3) ? YAPET::PWGEN::SPECIAL : 0;
00077     charpools |= o & (1 << 4) ? YAPET::PWGEN::OTHER : 0;
00078     return charpools;
00079 }
00080 
00081 
00082 void
00083 PWGenDialog::initCheckBoxItems() {
00084     ckbxitems.clear();
00085     const char** tmp = ckbx_strings;
00086 
00087     while (*tmp != 0) {
00088         ckbxitems.push_back (*tmp);
00089         tmp++;
00090     }
00091 }
00092 
00093 void
00094 PWGenDialog::createWindow() throw (YAPET::UI::UIException) {
00095     if (window != NULL)
00096         throw YAPET::UI::UIException (_ ("May you consider deleting the window before reallocating") );
00097 
00098     window = newwin (windowHeight(), windowWidth(), startY(), startX() );
00099 
00100     if (window == NULL)
00101         throw YAPET::UI::UIException (_ ("Error creating password generator window") );
00102 
00103     initCheckBoxItems();
00104     pwdisplay = new YAPET::UI::InputWidget (startX() + 1,
00105                         startY() + 2,
00106                         windowWidth() - 3,
00107                         true);
00108     pwleninput = new YAPET::UI::IntInWidget (startX() + 1,
00109             startY() + 4,
00110             windowWidth() - 3);
00111     ckbxgroup = new YAPET::UI::CheckBoxGroup (_ ("Character Pools"),
00112             ckbxitems,
00113             ckbox_options,
00114             startX() + 1,
00115             startY() + 5,
00116             windowWidth() - 2,
00117             7);
00118     regenbutton = new YAPET::UI::Button (_ ("Regenerate"),
00119                                          startX() + 2,
00120                                          startY() + windowHeight() - 2);
00121     okbutton = new YAPET::UI::Button (_ ("OK"),
00122                                       startX() + regenbutton->getLength() + 3,
00123                                       startY() + windowHeight() - 2);
00124     cancelbutton = new YAPET::UI::Button (_ ("Cancel"),
00125                                           startX() + regenbutton->getLength() + okbutton->getLength() + 4,
00126                                           startY() + windowHeight() - 2);
00127     //
00128     // The initial password will be computed here.
00129     //
00130     // Do not move this into refresh(). That might be tempting, but we don't
00131     // want to alter the password everytime the window is refreshed...
00132     //
00133     pwgen.generatePassword (pwlen);
00134     pwdisplay->setText (pwgen.getPassword() );
00135 }
00136 
00137 void
00138 PWGenDialog::printTitles() throw (YAPET::UI::UIException) {
00139     int retval = mymvwaddstr (window, 0, 2, _ ("P A S S W O R D  G E N E R A T O R") );
00140 
00141     if (retval == ERR)
00142         throw YAPET::UI::UIException (_ ("Error printing title") );
00143 
00144     char tmpbuff[1024];
00145     snprintf (tmpbuff, 1024, _ ("Generated Password (using %s):"), getNameOfRNG().c_str() );
00146     retval = mymvwaddstr (window, 1, 1, tmpbuff);
00147 
00148     if (retval == ERR)
00149         throw YAPET::UI::UIException (_ ("Error printing label") );
00150 
00151     retval = mymvwaddstr (window, 3,
00152                           1,
00153                           _ ("Password Length") );
00154 
00155     if (retval == ERR)
00156         throw YAPET::UI::UIException (_ ("Error printing label") );
00157 }
00158 
00159 PWGenDialog::PWGenDialog() throw (YAPET::UI::UIException) :
00160         window (NULL),
00161         ckbxgroup (NULL),
00162         pwdisplay (NULL),
00163         pwleninput (NULL),
00164         regenbutton (NULL),
00165         okbutton (NULL),
00166         cancelbutton (NULL),
00167         password (""),
00168         pwgen (YAPET::GLOBALS::Globals::getCharacterPools() ),
00169         pwlen (YAPET::GLOBALS::Globals::getPasswordLength() ),
00170         ckbox_options (YAPET::GLOBALS::Globals::getCharacterPools() ),
00171         canceled (true) {
00172 
00173     YAPET::PWGEN::RNGENGINE requested_rng = YAPET::GLOBALS::Globals::getPWGenRNG();
00174     int available_rngs = YAPET::PWGEN::RNG::getAvailableRNGs();
00175     if (available_rngs & requested_rng) {
00176     pwgen.setNewRNG(requested_rng);
00177     }
00178     
00179     createWindow();
00180 }
00181 
00182 PWGenDialog::~PWGenDialog() {
00183     assert (window != NULL);
00184     assert (pwdisplay != NULL);
00185     assert (pwleninput != NULL);
00186     assert (ckbxgroup != NULL);
00187     assert (regenbutton != NULL);
00188     assert (okbutton != NULL);
00189     assert (cancelbutton != NULL);
00190     wclear (window);
00191     // To be sure we don't have any sensitive information on the screen and
00192     // buffers (hopefully)
00193     wrefresh (window);
00194     delwin (window);
00195     // Save the values used. If they are unreasonable, adjust to the bare
00196     // minimum.
00197     YAPET::GLOBALS::Globals::setCharacterPools(
00198                            CheckBoxOptions2Charpools(ckbxgroup->getOptions()) == 0 ?
00199                            1 :
00200                            CheckBoxOptions2Charpools(ckbxgroup->getOptions())
00201                            );
00202     YAPET::GLOBALS::Globals::setPasswordLength(
00203                            pwleninput->getInt() == 0 ?
00204                            1 :
00205                            pwleninput->getInt()
00206                            );
00207     // Crush the widgets
00208     delete ckbxgroup;
00209     delete pwdisplay;
00210     delete pwleninput;
00211     delete regenbutton;
00212     delete okbutton;
00213     delete cancelbutton;
00214 }
00215 
00216 void
00217 PWGenDialog::run() throw (YAPET::UI::UIException) {
00218     refresh();
00219     int ch;
00220 
00221     while (true) {
00222         // The event handler for the check box group
00223         while ( (ch = pwdisplay->focus() ) != '\t') {
00224             switch (ch) {
00225 #ifdef HAVE_WRESIZE
00226                 case KEY_RESIZE:
00227                     YAPET::UI::BaseWindow::resizeAll();
00228                     break;
00229 #endif // HAVE_WRESIZE
00230                 case KEY_ESC:
00231                     canceled = true;
00232                     return;
00233                 default:
00234                     break;
00235             }
00236         }
00237 
00238 #ifdef HAVE_WRESIZE
00239 
00240         while ( (ch = pwleninput->focus() ) == KEY_RESIZE)
00241             YAPET::UI::BaseWindow::resizeAll();
00242 
00243 #else // HAVE_WRESIZE
00244         ch = pwleninput->focus();
00245 #endif // HAVE_WRESIZE
00246 
00247         if (ch == KEY_ESC) {
00248             canceled = true;
00249             return;
00250         }
00251 
00252         pwlen = pwleninput->getInt() > YAPET::CONSTS::Consts::getMaxPWLength() ? 
00253         pwleninput->setInt(YAPET::CONFIG::Config::getDefPWLength()),
00254         YAPET::CONFIG::Config::getDefPWLength() :
00255         pwleninput->getInt();
00256 
00257         // The event handler for the check box group
00258         while ( (ch = ckbxgroup->focus() ) != '\t') {
00259             switch (ch) {
00260 #ifdef HAVE_WRESIZE
00261                 case KEY_RESIZE:
00262                     YAPET::UI::BaseWindow::resizeAll();
00263                     break;
00264 #endif // HAVE_WRESIZE
00265                 case KEY_ESC:
00266                     canceled = true;
00267                     return;
00268                 default:
00269                     break;
00270             }
00271         }
00272 
00273 RESTART_GENBUTTON:
00274         // The regenerate button
00275 #ifdef HAVE_WRESIZE
00276 
00277         while ( (ch = regenbutton->focus() ) == KEY_RESIZE)
00278             YAPET::UI::BaseWindow::resizeAll();
00279 
00280 #else // HAVE_WRESIZE
00281         ch = regenbutton->focus();
00282 #endif // HAVE_WRESIZE
00283 
00284         switch (ch) {
00285             case KEY_ESC:
00286                 canceled = true;
00287                 return;
00288             case '\t':
00289                 break;
00290             case '\n':
00291             case KEY_ENTER:
00292 
00293                 if (ckbxgroup->getOptions() == 0) {
00294                     YAPET::UI::MessageBox* msgbox = NULL;
00295 
00296                     try {
00297                         msgbox = new YAPET::UI::MessageBox (_ ("E R R O R"), _ ("You need to select at least one Character Pool")  );
00298                         msgbox->run();
00299                         delete msgbox;
00300                     } catch (YAPET::UI::UIException&) {
00301                         if (msgbox != NULL)
00302                             delete msgbox;
00303                     }
00304 
00305                     refresh();
00306                     goto RESTART_GENBUTTON;
00307                 }
00308 
00309                 if (pwleninput->getInt() == 0) {
00310                     YAPET::UI::MessageBox* msgbox = NULL;
00311 
00312                     try {
00313                         msgbox = new YAPET::UI::MessageBox (_ ("E R R O R"), _ ("Password must have at least one character")  );
00314                         msgbox->run();
00315                         delete msgbox;
00316                     } catch (YAPET::UI::UIException&) {
00317                         if (msgbox != NULL)
00318                             delete msgbox;
00319                     }
00320 
00321                     refresh();
00322                     goto RESTART_GENBUTTON;
00323                 }
00324 
00325                 pwgen.setNewPool (CheckBoxOptions2Charpools (ckbxgroup->getOptions() ) );
00326                 pwgen.generatePassword (pwlen);
00327                 pwdisplay->clearText();
00328                 pwdisplay->setText (pwgen.getPassword() );
00329                 // Fall thru
00330             default:
00331                 // We want to stay on the button unless the user presses a tab key
00332                 goto RESTART_GENBUTTON;
00333         }
00334 
00335         // The ok button
00336 #ifdef HAVE_WRESIZE
00337 
00338         while ( (ch = okbutton->focus() ) == KEY_RESIZE)
00339             YAPET::UI::BaseWindow::resizeAll();
00340 
00341 #else // HAVE_WRESIZE
00342         ch = okbutton->focus();
00343 #endif // HAVE_WRESIZE
00344 
00345         switch (ch) {
00346             case KEY_ESC:
00347                 canceled = true;
00348                 return;
00349             case '\n':
00350             case KEY_ENTER:
00351                 canceled = false;
00352 
00353                 // Store the password now
00354                 if (pwdisplay->hasText() )
00355                     password = pwdisplay->getText();
00356                 else
00357                     canceled = true;
00358 
00359                 return;
00360         }
00361 
00362         // The cancel button
00363 #ifdef HAVE_WRESIZE
00364 
00365         while ( (ch = cancelbutton->focus() ) == KEY_RESIZE)
00366             YAPET::UI::BaseWindow::resizeAll();
00367 
00368 #else // HAVE_WRESIZE
00369         ch = cancelbutton->focus();
00370 #endif // HAVE_WRESIZE
00371 
00372         if (ch == '\n' || ch == KEY_ENTER || ch == KEY_ESC) {
00373             canceled = true;
00374             return;
00375         }
00376     }
00377 }
00378 
00379 void
00380 PWGenDialog::refresh() throw (YAPET::UI::UIException) {
00381     YAPET::UI::Colors::setcolor (window, YAPET::UI::MESSAGEBOX);
00382     int retval = box (window, 0, 0);
00383 
00384     if (retval == ERR)
00385         throw YAPET::UI::UIException (_ ("Error drawing box") );
00386 
00387     printTitles();
00388     retval = wrefresh (window);
00389 
00390     if (retval == ERR)
00391         throw YAPET::UI::UIException (_ ("Error refreshing window") );
00392 
00393     ckbxgroup->refresh();
00394     pwdisplay->refresh();
00395     pwleninput->setInt (pwlen);
00396     pwleninput->refresh();
00397     regenbutton->refresh();
00398     okbutton->refresh();
00399     cancelbutton->refresh();
00400 }
00401 
00402 void
00403 PWGenDialog::resize() throw (YAPET::UI::UIException) {
00404     wclear (window);
00405     // To be sure we don't have any sensitive information on the screen and
00406     // buffers (hopefully)
00407     wrefresh (window);
00408     delwin (window);
00409     delete pwdisplay;
00410     delete pwleninput;
00411     delete ckbxgroup;
00412     delete regenbutton;
00413     delete okbutton;
00414     delete cancelbutton;
00415     window = NULL;
00416     pwdisplay = NULL;
00417     pwleninput = NULL;
00418     ckbxgroup = NULL;
00419     regenbutton = NULL;
00420     okbutton = NULL;
00421     cancelbutton = NULL;
00422     createWindow();
00423 }

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