00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
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 }