yapet/passworddialog.cc

Go to the documentation of this file.
00001 // $Id: passworddialog.cc,v 1.7 2008-02-13 00:20:24 rafi Exp $
00002 //
00003 // YAPET -- Yet Another Password Encryption Tool
00004 // Copyright (C) 2008  Rafael Ostertag
00005 //
00006 // This program is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation, either version 3 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 //
00019 
00020 #include "passworddialog.h"
00021 #include "messagebox.h"
00022 
00023 void
00024 PasswordDialog::createWindow() throw(YAPETUI::UIException) {
00025     if (window != NULL)
00026     throw YAPETUI::UIException("May you consider deleting the window before reallocating");
00027 
00028     window = newwin(getHeight(), getWidth(), getStartY(), getStartX());
00029     if (window == NULL)
00030     throw YAPETUI::UIException("Error creating password dialog");
00031 
00032     pwidget1 = new YAPETUI::PasswordWidget(getStartX() + 1,
00033                      getStartY() + 3,
00034                      getWidth() - 2);
00035     if (pwtype == NEW_PW)
00036     pwidget2 = new YAPETUI::PasswordWidget(getStartX() + 1,
00037                          getStartY() + 5,
00038                          getWidth()-2);
00039 
00040     okbutton = new YAPETUI::Button("Ok",
00041                  getStartX() + 1,
00042                  getStartY() + getHeight() - 2);
00043 
00044     cancelbutton = new YAPETUI::Button("Cancel",
00045                      getStartX() + okbutton->getLength() + 2,
00046                      getStartY() + getHeight() - 2);
00047 
00048 }
00049 
00050 PasswordDialog::PasswordDialog(PWTYPE pt, std::string fn)
00051     throw(YAPETUI::UIException) : window(NULL),
00052              pwidget1(NULL),
00053              pwidget2(NULL),
00054              okbutton(NULL),
00055              cancelbutton(NULL),
00056              pwtype(pt),
00057              key(NULL),
00058              filename(fn){
00059     createWindow();
00060              }
00061 
00062 PasswordDialog::~PasswordDialog() {
00063     delete pwidget1;
00064     if (pwtype == NEW_PW)
00065     delete pwidget2;
00066     delete okbutton;
00067     delete cancelbutton;
00068     wclear(window);
00069     delwin(window);
00070 }
00071 
00072 void
00073 PasswordDialog::run() throw(YAPETUI::UIException) {
00074     refresh();
00075     while (true) {
00076     int ch = 0;
00077 #ifdef HAVE_WRESIZE
00078     while ( (ch = pwidget1->focus()) == KEY_RESIZE)
00079         YAPETUI::BaseWindow::resizeAll();
00080 #else // HAVE_WRESIZE
00081     pwidget1->focus();
00082 #endif // HAVE_WRESIZE
00083 
00084     // Password input widget 2 only if we prompt for a new password
00085     if (pwtype == NEW_PW) {
00086 #ifdef HAVE_WRESIZE
00087         while ( (ch = pwidget2->focus()) == KEY_RESIZE)
00088         YAPETUI::BaseWindow::resizeAll();
00089 #else // HAVE_WRESIZE
00090         pwidget2->focus();
00091 #endif // HAVE_WRESIZE
00092     }
00093 
00094 #ifdef HAVE_WRESIZE
00095     while ( (ch = okbutton->focus()) == KEY_RESIZE)
00096         YAPETUI::BaseWindow::resizeAll();
00097 #else // HAVE_WRESIZE
00098     ch = okbutton->focus();
00099 #endif // HAVE_WRESIZE
00100     if (ch == '\n') {
00101         if (pwtype == NEW_PW) {
00102         if (pwidget1->getText() == pwidget2->getText()) {
00103             key = new YAPET::Key(pwidget1->getText().c_str());
00104             return;
00105         } else {
00106             YAPETUI::MessageBox* errmsg = NULL;
00107             try {
00108             errmsg = new YAPETUI::MessageBox("Error", "Passwords do not match");
00109             errmsg->run();
00110             delete errmsg;
00111             } catch(YAPETUI::UIException&) {
00112             if (errmsg == NULL)
00113                 delete errmsg;
00114             }
00115             pwidget1->setText("");
00116             pwidget2->setText("");
00117             refresh();
00118             continue;
00119         }
00120         } else {
00121         key = new YAPET::Key(pwidget1->getText().c_str());
00122         pwidget1->clearText();
00123         return;
00124         }
00125     }
00126 #ifdef HAVE_WRESIZE
00127     while ( (ch = cancelbutton->focus()) == KEY_RESIZE)
00128         YAPETUI::BaseWindow::resizeAll();
00129 #else // HAVE_WRESIZE
00130     ch = cancelbutton->focus();
00131 #endif // HAVE_WRESIZE
00132     if (ch == '\n')
00133         return;
00134     }
00135 
00136 }
00137 
00138 void
00139 PasswordDialog::resize() throw(YAPETUI::UIException) {
00140     int retval = delwin(window);
00141     if (retval == ERR)
00142     throw YAPETUI::UIException("Error deleting password dialog window");
00143 
00144     pwidget1->clearText();
00145     delete pwidget1;
00146     if (pwtype == NEW_PW) {
00147     pwidget2->clearText();
00148     delete pwidget2;
00149     }
00150     delete okbutton;
00151     delete cancelbutton;
00152 
00153     window = NULL;
00154     pwidget1 = NULL;
00155     pwidget2 = NULL;
00156     okbutton = NULL;
00157     cancelbutton = NULL;
00158 
00159     createWindow();
00160 }
00161 
00162 void
00163 PasswordDialog::refresh() throw(YAPETUI::UIException) {
00164     YAPETUI::Colors::setcolor(window, YAPETUI::MESSAGEBOX);
00165     int retval = werase(window);
00166     if (retval == ERR)
00167     throw YAPETUI::UIException("Error clearing password dialog");
00168 
00169     retval = box(window, 0, 0);
00170     if (retval == ERR)
00171     throw YAPETUI::UIException("Error adding box");
00172 
00173     retval = mymvwaddstr(window, 0, 2, "P A S S W O R D");
00174     if (retval == ERR)
00175     throw YAPETUI::UIException("Error setting title");
00176 
00177     // The label holding the file name
00178     retval = mymvwaddstr(window, 2, 1, filename.c_str());
00179     if (retval == ERR)
00180     throw YAPETUI::UIException("Error setting label");
00181 
00182     if (pwtype == NEW_PW) {
00183     retval = mymvwaddstr(window, 1, 1, "Enter new password for");
00184     if (retval == ERR)
00185         throw YAPETUI::UIException("Error setting label");
00186 
00187     retval = mymvwaddstr(window, 4, 1, "Confirm password");
00188     if (retval == ERR)
00189         throw YAPETUI::UIException("Error setting label");
00190     } else {
00191     retval = mymvwaddstr(window, 1, 1, "Enter password for");
00192     if (retval == ERR)
00193         throw YAPETUI::UIException("Error setting label");
00194     }
00195 
00196     retval = wrefresh(window);
00197     if (retval == ERR)
00198     throw YAPETUI::UIException("Error refreshing password dialog");
00199 
00200     pwidget1->refresh();
00201     if (pwtype == NEW_PW)
00202     pwidget2->refresh();
00203     okbutton->refresh();
00204     cancelbutton->refresh();
00205 }

Generated on Wed Feb 27 16:15:42 2008 for YAPET by  doxygen 1.5.4