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

yapet/passwordrecord.cc

Go to the documentation of this file.
00001 // $Id: passwordrecord.cc 3355 2010-09-18 21:04:48Z rafi $
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 #ifdef HAVE_CONFIG
00022 # include <config.h>
00023 #endif
00024 
00025 #ifdef HAVE_STRING_H
00026 # include <string.h>
00027 #endif
00028 
00029 #include <structs.h>
00030 #include <partdec.h>
00031 #include <crypt.h>
00032 
00033 #include "../intl.h"
00034 #include "colors.h"
00035 #include "messagebox.h"
00036 #include "dialogbox.h"
00037 #include "passwordrecord.h"
00038 
00039 void
00040 PasswordRecord::createWindow() throw (YAPET::UI::UIException) {
00041     if (window != NULL)
00042         throw YAPET::UI::UIException (_ ("May you consider deleting the window before reallocating") );
00043 
00044     window = newwin (getHeight(), getWidth(), getStartY(), getStartX() );
00045 
00046     if (window == NULL)
00047         throw YAPET::UI::UIException (_ ("Error creating password entry") );
00048 
00049     name = new YAPET::UI::InputWidget (getStartX() + 1,
00050                                        getStartY() + 2,
00051                                        getWidth() - 2,
00052                                        YAPET::NAME_SIZE,
00053                        readonly);
00054     host = new YAPET::UI::InputWidget (getStartX() + 1,
00055                                        getStartY() + 4,
00056                                        getWidth() - 2,
00057                                        YAPET::HOST_SIZE,
00058                        readonly);
00059     username = new YAPET::UI::InputWidget (getStartX() + 1,
00060                                            getStartY() + 6,
00061                                            getWidth() - 2,
00062                                            YAPET::USERNAME_SIZE,
00063                        readonly);
00064     password = new YAPET::UI::InputWidget (getStartX() + 1,
00065                                            getStartY() + 8,
00066                                            getWidth() - 2,
00067                                            YAPET::PASSWORD_SIZE,
00068                        readonly);
00069     comment = new YAPET::UI::InputWidget (getStartX() + 1,
00070                                           getStartY() + 10,
00071                                           getWidth() - 2,
00072                                           YAPET::COMMENT_SIZE,
00073                       readonly);
00074     okbutton = new YAPET::UI::Button (_ ("OK"),
00075                                       getStartX() + 1,
00076                                       getStartY() + 12);
00077     cancelbutton = new YAPET::UI::Button (_ ("Cancel"),
00078                                           getStartX() + okbutton->getLength() + 2,
00079                                           getStartY() + 12);
00080 #ifdef ENABLE_PWGEN
00081     pwgenbutton = new YAPET::UI::Button (_ ("Generate Password"),
00082                                          getStartX() + okbutton->getLength() + cancelbutton->getLength() + 3,
00083                                          getStartY() + 12,
00084                      readonly);
00085 #endif
00086     refresh();
00087 }
00088 
00093 bool
00094 PasswordRecord::sureToCancel() throw (YAPET::UI::UIException) {
00095     bool suretoexit = true;
00096 
00097     if (entryChanged() ) {
00098         YAPET::UI::DialogBox* question = NULL;
00099 
00100         try {
00101             question = new YAPET::UI::DialogBox (_ ("Q U E S T I O N"),
00102                                                  _ ("Entries modified. Really cancel?") );
00103             question->run();
00104             YAPET::UI::ANSWER a = question->getAnswer();
00105             suretoexit = a == YAPET::UI::ANSWER_OK ? true : false;
00106             delete question;
00107         } catch (YAPET::UI::UIException&) {
00108             if (question != NULL)
00109                 delete question;
00110         }
00111 
00112         refresh();
00113     }
00114 
00115     if (suretoexit) {
00116         encentry = NULL;
00117         return true;
00118     } else {
00119         return false;
00120     }
00121 }
00122 
00123 PasswordRecord::PasswordRecord (YAPET::Key& k, YAPET::PartDec* pe, bool ro)
00124 throw (YAPET::UI::UIException) : window (NULL),
00125                  name (NULL),
00126                  host (NULL),
00127                  username (NULL),
00128                  password (NULL),
00129                  comment (NULL),
00130                  okbutton (NULL),
00131                  cancelbutton (NULL),
00132 #ifdef ENABLE_PWGEN
00133                  pwgenbutton (NULL),
00134 #endif
00135                  key (&k),
00136                  encentry (pe),
00137                  s_name (""),
00138                  s_host (""),
00139                  s_username (""),
00140                  s_password (""),
00141                  s_comment (""),
00142                  namechanged (false),
00143                  hostchanged (false),
00144                  usernamechanged (false),
00145                  passwordchanged (false),
00146                  commentchanged (false),
00147                  readonly (ro) {
00148     if (encentry != NULL) {
00149         YAPET::Record<YAPET::PasswordRecord>* dec_rec = NULL;
00150 
00151         try {
00152             YAPET::Crypt crypt (*key);
00153             dec_rec = crypt.decrypt<YAPET::PasswordRecord> (encentry->getEncRecord() );
00154             YAPET::PasswordRecord* ptr_rec = *dec_rec;
00155             s_name = (char*) ptr_rec->name;
00156             s_host = (char*) ptr_rec->host;
00157             s_username = (char*) ptr_rec->username;
00158             s_password = (char*) ptr_rec->password;
00159             s_comment = (char*) ptr_rec->comment;
00160             delete dec_rec;
00161         } catch (YAPET::YAPETException& ex) {
00162             if (dec_rec != NULL)
00163                 delete dec_rec;
00164 
00165             YAPET::UI::MessageBox* msgbox = NULL;
00166 
00167             try {
00168                 msgbox = new YAPET::UI::MessageBox (_ ("E R R O R"), ex.what() );
00169                 msgbox->run();
00170                 delete msgbox;
00171             } catch (YAPET::UI::UIException&) {
00172                 if (msgbox != NULL)
00173                     delete msgbox;
00174 
00175                 // What should I do else, looks pretty screwed up??
00176             }
00177         }
00178     }
00179 
00180     createWindow();
00181 }
00182 
00183 PasswordRecord::~PasswordRecord() {
00184     wclear (window);
00185     delwin (window);
00186     delete name;
00187     delete host;
00188     delete username;
00189     delete password;
00190     delete comment;
00191     delete okbutton;
00192     delete cancelbutton;
00193 #ifdef ENABLE_PWGEN
00194     delete pwgenbutton;
00195 #endif
00196 }
00197 
00198 void
00199 PasswordRecord::run() throw (YAPET::UI::UIException) {
00200     while (true) {
00201         int ch = 0;
00202 
00203 
00204         while ( (ch = name->focus()) ) {
00205         switch (ch) {
00206 #ifdef HAVE_WRESIZE
00207         case KEY_RESIZE:
00208         YAPET::UI::BaseWindow::resizeAll();
00209         continue;
00210         break;
00211 #endif // HAVE_WRESIZE
00212         case KEY_CTRL_E: 
00213         setReadonly(false);
00214         // For getting the title right
00215         refresh();
00216         continue;
00217         break;
00218         }
00219         break;
00220     }
00221         
00222         s_name = name->getText();
00223         namechanged = name->isTextChanged();
00224 
00225         if (ch == KEY_ESC && sureToCancel() ) {
00226             return;
00227         }
00228 
00229         while ( (ch = host->focus()) ) {
00230         switch (ch) {
00231 #ifdef HAVE_WRESIZE
00232         case KEY_RESIZE:
00233         YAPET::UI::BaseWindow::resizeAll();
00234         continue;
00235         break;
00236 #endif // HAVE_WRESIZE
00237         case KEY_CTRL_E: 
00238         setReadonly(false);
00239         // For getting the title right
00240         refresh();
00241         continue;
00242         break;
00243         }
00244         break;
00245     }
00246 
00247         s_host = host->getText();
00248         hostchanged = host->isTextChanged();
00249 
00250         if (ch == KEY_ESC && sureToCancel() ) {
00251             return;
00252         }
00253 
00254         while ( (ch = username->focus()) ) {
00255         switch (ch) {
00256 #ifdef HAVE_WRESIZE
00257         case KEY_RESIZE:
00258         YAPET::UI::BaseWindow::resizeAll();
00259         continue;
00260         break;
00261 #endif // HAVE_WRESIZE
00262         case KEY_CTRL_E: 
00263         setReadonly(false);
00264         // For getting the title right
00265         refresh();
00266         continue;
00267         break;
00268         }
00269         break;
00270     }
00271 
00272         s_username = username->getText();
00273         usernamechanged = username->isTextChanged();
00274 
00275         if (ch == KEY_ESC && sureToCancel() ) {
00276             return;
00277         }
00278 
00279         while ( (ch = password->focus()) )  {
00280         switch (ch) {
00281 #ifdef HAVE_WRESIZE
00282         case KEY_RESIZE:
00283         YAPET::UI::BaseWindow::resizeAll();
00284         continue;
00285         break;
00286 #endif // HAVE_WRESIZE
00287         case KEY_CTRL_E: 
00288         setReadonly(false);
00289         // For getting the title right
00290         refresh();
00291         continue;
00292         break;
00293         }
00294         break;
00295     }
00296 
00297         s_password = password->getText();
00298         passwordchanged = password->isTextChanged();
00299 
00300         if (ch == KEY_ESC && sureToCancel() ) {
00301             return;
00302         }
00303 
00304         while ( (ch = comment->focus()) )  {
00305         switch (ch) {
00306 #ifdef HAVE_WRESIZE
00307         case KEY_RESIZE:
00308         YAPET::UI::BaseWindow::resizeAll();
00309         continue;
00310         break;
00311 #endif // HAVE_WRESIZE
00312         case KEY_CTRL_E: 
00313         setReadonly(false);
00314         // For getting the title right
00315         refresh();
00316         continue;
00317         break;
00318         }
00319         break;
00320     }
00321 
00322         s_comment = comment->getText();
00323         commentchanged = comment->isTextChanged();
00324 
00325         if (ch == KEY_ESC && sureToCancel() ) {
00326             return;
00327         }
00328 
00329 #ifdef HAVE_WRESIZE
00330 
00331         while ( (ch = okbutton->focus() ) == KEY_RESIZE)
00332             YAPET::UI::BaseWindow::resizeAll();
00333 
00334 #else // HAVE_WRESIZE
00335         ch = okbutton->focus();
00336 #endif // HAVE_WRESIZE
00337 
00338         if (ch == KEY_ESC && sureToCancel() ) {
00339             return;
00340         }
00341 
00342         if (ch == '\n') {
00343             if (!entryChanged() ) {
00344                 encentry = NULL;
00345                 return;
00346             }
00347 
00348             YAPET::Record<YAPET::PasswordRecord> unenc_rec;
00349             YAPET::PasswordRecord* ptr_rec = unenc_rec;
00350             strncpy ( (char*) ptr_rec->name, s_name.c_str(), YAPET::NAME_SIZE);
00351             strncpy ( (char*) ptr_rec->host, s_host.c_str(), YAPET::HOST_SIZE);
00352             strncpy ( (char*) ptr_rec->username, s_username.c_str(), YAPET::USERNAME_SIZE);
00353             strncpy ( (char*) ptr_rec->password, s_password.c_str(), YAPET::PASSWORD_SIZE);
00354             strncpy ( (char*) ptr_rec->comment, s_comment.c_str(), YAPET::COMMENT_SIZE);
00355             YAPET::BDBuffer* enc_rec = NULL;
00356 
00357             try {
00358                 encentry = new YAPET::PartDec (unenc_rec, *key);
00359             } catch (YAPET::YAPETException& ex) {
00360                 if (enc_rec != NULL)
00361                     delete enc_rec;
00362 
00363                 encentry = NULL;
00364                 YAPET::UI::MessageBox* msgbox = NULL;
00365 
00366                 try {
00367                     msgbox = new YAPET::UI::MessageBox (_ ("E R R O R"), ex.what() );
00368                     msgbox->run();
00369                     delete msgbox;
00370                 } catch (YAPET::UI::UIException&) {
00371                     if (msgbox != NULL)
00372                         delete msgbox;
00373 
00374                     // What should I do else, looks pretty screwed up??
00375                 }
00376             }
00377 
00378             return;
00379         }
00380 
00381 #ifdef HAVE_WRESIZE
00382 
00383         while ( (ch = cancelbutton->focus() ) == KEY_RESIZE)
00384             YAPET::UI::BaseWindow::resizeAll();
00385 
00386 #else // HAVE_WRESIZE
00387         ch = cancelbutton->focus();
00388 #endif // HAVE_WRESIZE
00389 
00390         if ( (ch == '\n' || ch == KEY_ESC) &&
00391                 sureToCancel() ) {
00392             return;
00393         }
00394 
00395 #ifdef ENABLE_PWGEN
00396 #  ifdef HAVE_WRESIZE
00397 
00398         while ( (ch = pwgenbutton->focus() ) == KEY_RESIZE)
00399             YAPET::UI::BaseWindow::resizeAll();
00400 
00401 #  else // HAVE_WRESIZE
00402         ch = pwgenbutton->focus();
00403 #  endif // HAVE_WRESIZE
00404 
00405         if (ch == '\n' ) {
00406             PWGenDialog* tmp = NULL;
00407 
00408             try {
00409                 tmp = new PWGenDialog ();
00410                 tmp->run();
00411 
00412                 if (!tmp->isCanceled() ) {
00413                     s_password = tmp->getPassword();
00414                     passwordchanged = true;
00415                     password->setText (s_password);
00416                     password->setTextChanged (true);
00417                 }
00418 
00419                 delete tmp;
00420             } catch (std::exception& ex2) {
00421                 if (tmp != NULL)
00422                     delete tmp;
00423             }
00424 
00425             YAPET::UI::BaseWindow::refreshAll();
00426         }
00427 
00428         if (ch == KEY_ESC && sureToCancel() ) {
00429             return;
00430         }
00431 
00432 #endif // ENABLE_PWGEN
00433     } // while (true)
00434 }
00435 
00436 void
00437 PasswordRecord::resize() throw (YAPET::UI::UIException) {
00438     int retval = delwin (window);
00439 
00440     if (retval == ERR)
00441         throw YAPET::UI::UIException (_ ("Error deleting password entry window") );
00442 
00443     delete name;
00444     delete host;
00445     delete username;
00446     delete password;
00447     delete comment;
00448     delete okbutton;
00449     delete cancelbutton;
00450 #ifdef ENABLE_PWGEN
00451     delete pwgenbutton;
00452 #endif
00453     window = NULL;
00454     name = NULL;
00455     username = NULL;
00456     password = NULL;
00457     comment = NULL;
00458     okbutton = NULL;
00459     cancelbutton = NULL;
00460 #ifdef ENABLE_PWGEN
00461     pwgenbutton = NULL;
00462 #endif
00463     createWindow();
00464 }
00465 
00466 void
00467 PasswordRecord::refresh() throw (YAPET::UI::UIException) {
00468     YAPET::UI::Colors::setcolor (window, YAPET::UI::MESSAGEBOX);
00469     int retval = wclear (window);
00470 
00471     if (retval == ERR)
00472         throw YAPET::UI::UIException (_ ("Error clearing window") );
00473 
00474     retval = box (window, 0, 0);
00475 
00476     if (retval == ERR)
00477         throw YAPET::UI::UIException (_ ("Error adding box") );
00478 
00479     if ( readonly )
00480     retval = mymvwaddstr (window, 0, 2, _ ("P A S S W O R D  R E C O R D (Read-Only)") );
00481     else
00482     retval = mymvwaddstr (window, 0, 2, _ ("P A S S W O R D  R E C O R D") );
00483 
00484     if (retval == ERR)
00485         throw YAPET::UI::UIException (_ ("Error setting label") );
00486 
00487     retval = mymvwaddstr (window, 1, 1, _ ("Name") );
00488 
00489     if (retval == ERR)
00490         throw YAPET::UI::UIException (_ ("Error adding label") );
00491 
00492     retval = mymvwaddstr (window, 3, 1, _ ("Host") );
00493 
00494     if (retval == ERR)
00495         throw YAPET::UI::UIException (_ ("Error adding label") );
00496 
00497     retval = mymvwaddstr (window, 5, 1, _ ("Username") );
00498 
00499     if (retval == ERR)
00500         throw YAPET::UI::UIException (_ ("Error adding label") );
00501 
00502     retval = mymvwaddstr (window, 7, 1, _ ("Password") );
00503 
00504     if (retval == ERR)
00505         throw YAPET::UI::UIException (_ ("Error adding label") );
00506 
00507     retval = mymvwaddstr (window, 9, 1, _ ("Comment") );
00508 
00509     if (retval == ERR)
00510         throw YAPET::UI::UIException (_ ("Error adding label") );
00511 
00512     retval = wrefresh (window);
00513 
00514     if (retval == ERR)
00515         throw YAPET::UI::UIException (_ ("Error refreshing window") );
00516 
00517     name->setText (s_name);
00518     name->setTextChanged (namechanged);
00519     name->refresh();
00520     host->setText (s_host);
00521     host->setTextChanged (hostchanged);
00522     host->refresh();
00523     username->setText (s_username);
00524     username->setTextChanged (usernamechanged);
00525     username->refresh();
00526     password->setText (s_password);
00527     password->setTextChanged (passwordchanged);
00528     password->refresh();
00529     comment->setText (s_comment);
00530     comment->setTextChanged (commentchanged);
00531     comment->refresh();
00532     okbutton->refresh();
00533     cancelbutton->refresh();
00534 #ifdef ENABLE_PWGEN
00535     pwgenbutton->refresh();
00536 #endif
00537 }
00538 
00539 bool
00540 PasswordRecord::entryChanged() const {
00541     return name->isTextChanged() ||
00542            host->isTextChanged() ||
00543            username->isTextChanged() ||
00544            password->isTextChanged() ||
00545            comment->isTextChanged();
00546 }
00547 
00548 void
00549 PasswordRecord::setReadonly(bool ro) {
00550     readonly = ro;
00551     name->setReadonly(readonly);
00552     host->setReadonly(readonly);
00553     username->setReadonly(readonly);
00554     password->setReadonly(readonly);
00555     comment->setReadonly(readonly);
00556 #ifdef ENABLE_PWGEN
00557     pwgenbutton->setReadonly(readonly);
00558 #endif
00559 
00560 }

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