00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "../intl.h"
00022 #include "passworddialog.h"
00023 #include "messagebox.h"
00024
00025 void
00026 PasswordDialog::createWindow() throw(YAPETUI::UIException) {
00027 if (window != NULL)
00028 throw YAPETUI::UIException(_("May you consider deleting the window before reallocating"));
00029
00030 window = newwin(getHeight(), getWidth(), getStartY(), getStartX());
00031 if (window == NULL)
00032 throw YAPETUI::UIException(_("Error creating password dialog"));
00033
00034 pwidget1 = new YAPETUI::PasswordWidget(getStartX() + 1,
00035 getStartY() + 3,
00036 getWidth() - 2);
00037 if (pwtype == NEW_PW)
00038 pwidget2 = new YAPETUI::PasswordWidget(getStartX() + 1,
00039 getStartY() + 5,
00040 getWidth()-2);
00041
00042 okbutton = new YAPETUI::Button(_("OK"),
00043 getStartX() + 1,
00044 getStartY() + getHeight() - 2);
00045
00046 cancelbutton = new YAPETUI::Button(_("Cancel"),
00047 getStartX() + okbutton->getLength() + 2,
00048 getStartY() + getHeight() - 2);
00049
00050 }
00051
00052 PasswordDialog::PasswordDialog(PWTYPE pt, std::string fn)
00053 throw(YAPETUI::UIException) : window(NULL),
00054 pwidget1(NULL),
00055 pwidget2(NULL),
00056 okbutton(NULL),
00057 cancelbutton(NULL),
00058 pwtype(pt),
00059 key(NULL),
00060 filename(fn){
00061 createWindow();
00062 }
00063
00064 PasswordDialog::~PasswordDialog() {
00065 delete pwidget1;
00066 if (pwtype == NEW_PW)
00067 delete pwidget2;
00068 delete okbutton;
00069 delete cancelbutton;
00070 wclear(window);
00071 delwin(window);
00072 }
00073
00074 void
00075 PasswordDialog::run() throw(YAPETUI::UIException) {
00076 refresh();
00077 while (true) {
00078 int ch = 0;
00079 #ifdef HAVE_WRESIZE
00080 while ( (ch = pwidget1->focus()) == KEY_RESIZE)
00081 YAPETUI::BaseWindow::resizeAll();
00082 #else // HAVE_WRESIZE
00083 pwidget1->focus();
00084 #endif // HAVE_WRESIZE
00085
00086
00087 if (pwtype == NEW_PW) {
00088 #ifdef HAVE_WRESIZE
00089 while ( (ch = pwidget2->focus()) == KEY_RESIZE)
00090 YAPETUI::BaseWindow::resizeAll();
00091 #else // HAVE_WRESIZE
00092 pwidget2->focus();
00093 #endif // HAVE_WRESIZE
00094 }
00095
00096 #ifdef HAVE_WRESIZE
00097 while ( (ch = okbutton->focus()) == KEY_RESIZE)
00098 YAPETUI::BaseWindow::resizeAll();
00099 #else // HAVE_WRESIZE
00100 ch = okbutton->focus();
00101 #endif // HAVE_WRESIZE
00102 if (ch == '\n') {
00103 if (pwtype == NEW_PW) {
00104 if (pwidget1->getText() == pwidget2->getText()) {
00105 key = new YAPET::Key(pwidget1->getText().c_str());
00106 return;
00107 } else {
00108 YAPETUI::MessageBox* errmsg = NULL;
00109 try {
00110 errmsg = new YAPETUI::MessageBox(_("E R R O R"), _("Passwords do not match"));
00111 errmsg->run();
00112 delete errmsg;
00113 } catch(YAPETUI::UIException&) {
00114 if (errmsg == NULL)
00115 delete errmsg;
00116 }
00117 pwidget1->setText("");
00118 pwidget2->setText("");
00119 refresh();
00120 continue;
00121 }
00122 } else {
00123 key = new YAPET::Key(pwidget1->getText().c_str());
00124 pwidget1->clearText();
00125 return;
00126 }
00127 }
00128 #ifdef HAVE_WRESIZE
00129 while ( (ch = cancelbutton->focus()) == KEY_RESIZE)
00130 YAPETUI::BaseWindow::resizeAll();
00131 #else // HAVE_WRESIZE
00132 ch = cancelbutton->focus();
00133 #endif // HAVE_WRESIZE
00134 if (ch == '\n')
00135 return;
00136 }
00137
00138 }
00139
00140 void
00141 PasswordDialog::resize() throw(YAPETUI::UIException) {
00142 int retval = delwin(window);
00143 if (retval == ERR)
00144 throw YAPETUI::UIException(_("Error deleting password dialog window"));
00145
00146 pwidget1->clearText();
00147 delete pwidget1;
00148 if (pwtype == NEW_PW) {
00149 pwidget2->clearText();
00150 delete pwidget2;
00151 }
00152 delete okbutton;
00153 delete cancelbutton;
00154
00155 window = NULL;
00156 pwidget1 = NULL;
00157 pwidget2 = NULL;
00158 okbutton = NULL;
00159 cancelbutton = NULL;
00160
00161 createWindow();
00162 }
00163
00164 void
00165 PasswordDialog::refresh() throw(YAPETUI::UIException) {
00166 YAPETUI::Colors::setcolor(window, YAPETUI::MESSAGEBOX);
00167 int retval = werase(window);
00168 if (retval == ERR)
00169 throw YAPETUI::UIException(_("Error clearing password dialog"));
00170
00171 retval = box(window, 0, 0);
00172 if (retval == ERR)
00173 throw YAPETUI::UIException(_("Error adding box"));
00174
00175 retval = mymvwaddstr(window, 0, 2, _("P A S S W O R D"));
00176 if (retval == ERR)
00177 throw YAPETUI::UIException(_("Error setting title"));
00178
00179
00180 retval = mymvwaddstr(window, 2, 1, filename.c_str());
00181 if (retval == ERR)
00182 throw YAPETUI::UIException(_("Error setting label"));
00183
00184 if (pwtype == NEW_PW) {
00185 retval = mymvwaddstr(window, 1, 1, _("Enter new password for"));
00186 if (retval == ERR)
00187 throw YAPETUI::UIException(_("Error setting label"));
00188
00189 retval = mymvwaddstr(window, 4, 1, _("Confirm password"));
00190 if (retval == ERR)
00191 throw YAPETUI::UIException(_("Error setting label"));
00192 } else {
00193 retval = mymvwaddstr(window, 1, 1, _("Enter password for"));
00194 if (retval == ERR)
00195 throw YAPETUI::UIException(_("Error setting label"));
00196 }
00197
00198 retval = wrefresh(window);
00199 if (retval == ERR)
00200 throw YAPETUI::UIException(_("Error refreshing password dialog"));
00201
00202 pwidget1->refresh();
00203 if (pwtype == NEW_PW)
00204 pwidget2->refresh();
00205 okbutton->refresh();
00206 cancelbutton->refresh();
00207 }