00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "../intl.h"
00021 #include "passwordrecord.h"
00022 #include <messagebox.h>
00023
00024 #include <string.h>
00025
00026
00027 #include <structs.h>
00028 #include <partdec.h>
00029 #include <crypt.h>
00030
00031 void
00032 PasswordRecord::createWindow() throw(YAPETUI::UIException) {
00033 if (window != NULL)
00034 throw YAPETUI::UIException(_("May you consider deleting the window before reallocating"));
00035
00036 window = newwin(getHeight(), getWidth(), getStartY(), getStartX());
00037 if (window == NULL)
00038 throw YAPETUI::UIException(_("Error creating password entry"));
00039
00040 name = new YAPETUI::InputWidget(getStartX() + 1,
00041 getStartY() + 2,
00042 getWidth() - 2,
00043 YAPET::NAME_SIZE);
00044
00045 host = new YAPETUI::InputWidget(getStartX() + 1,
00046 getStartY() + 4,
00047 getWidth() - 2,
00048 YAPET::HOST_SIZE);
00049
00050 username = new YAPETUI::InputWidget(getStartX() + 1,
00051 getStartY() + 6,
00052 getWidth() - 2,
00053 YAPET::USERNAME_SIZE);
00054
00055 password = new YAPETUI::InputWidget(getStartX() + 1,
00056 getStartY() + 8,
00057 getWidth() - 2,
00058 YAPET::PASSWORD_SIZE);
00059
00060 comment = new YAPETUI::InputWidget(getStartX() + 1,
00061 getStartY() + 10,
00062 getWidth() - 2,
00063 YAPET::COMMENT_SIZE);
00064
00065 okbutton = new YAPETUI::Button(_("OK"),
00066 getStartX() + 1,
00067 getStartY() + 12);
00068
00069 cancelbutton = new YAPETUI::Button(_("Cancel"),
00070 getStartX() + okbutton->getLength() + 2,
00071 getStartY() + 12);
00072
00073 refresh();
00074 }
00075
00076 PasswordRecord::PasswordRecord(YAPET::Key& k, YAPET::PartDec* pe)
00077 throw(YAPETUI::UIException) : window(NULL),
00078 name(NULL),
00079 host(NULL),
00080 username(NULL),
00081 password(NULL),
00082 comment(NULL),
00083 okbutton(NULL),
00084 cancelbutton(NULL),
00085 key(&k),
00086 encentry(pe) {
00087 createWindow();
00088 }
00089
00090 PasswordRecord::~PasswordRecord() {
00091 wclear(window);
00092 delwin(window);
00093 delete name;
00094 delete host;
00095 delete username;
00096 delete password;
00097 delete comment;
00098 delete okbutton;
00099 delete cancelbutton;
00100 }
00101
00102 void
00103 PasswordRecord::run() throw(YAPETUI::UIException) {
00104 while (true) {
00105 int ch = 0;
00106 #ifdef HAVE_WRESIZE
00107 while ( (ch = name->focus()) == KEY_RESIZE)
00108 YAPETUI::BaseWindow::resizeAll();
00109 #else // HAVE_WRESIZE
00110 ch = name->focus();
00111 #endif // HAVE_WRESIZE
00112
00113
00114 #ifdef HAVE_WRESIZE
00115 while ( (ch = host->focus()) == KEY_RESIZE)
00116 YAPETUI::BaseWindow::resizeAll();
00117 #else // HAVE_WRESIZE
00118 ch = host->focus();
00119 #endif // HAVE_WRESIZE
00120
00121
00122 #ifdef HAVE_WRESIZE
00123 while ( (ch = username->focus()) == KEY_RESIZE)
00124 YAPETUI::BaseWindow::resizeAll();
00125 #else // HAVE_WRESIZE
00126 ch = username->focus();
00127 #endif // HAVE_WRESIZE
00128
00129
00130 #ifdef HAVE_WRESIZE
00131 while ( (ch = password->focus()) == KEY_RESIZE)
00132 YAPETUI::BaseWindow::resizeAll();
00133 #else // HAVE_WRESIZE
00134 ch = password->focus();
00135 #endif // HAVE_WRESIZE
00136
00137
00138 #ifdef HAVE_WRESIZE
00139 while ( (ch = comment->focus()) == KEY_RESIZE)
00140 YAPETUI::BaseWindow::resizeAll();
00141 #else // HAVE_WRESIZE
00142 ch = comment->focus();
00143 #endif // HAVE_WRESIZE
00144
00145
00146 #ifdef HAVE_WRESIZE
00147 while ( (ch = okbutton->focus()) == KEY_RESIZE)
00148 YAPETUI::BaseWindow::resizeAll();
00149 #else // HAVE_WRESIZE
00150 ch = okbutton->focus();
00151 #endif // HAVE_WRESIZE
00152
00153 if (ch == '\n') {
00154 if (!entryChanged()) {
00155 encentry = NULL;
00156 return;
00157 }
00158
00159 YAPET::Record<YAPET::PasswordRecord> unenc_rec;
00160 YAPET::PasswordRecord* ptr_rec = unenc_rec;
00161 strncpy((char*)ptr_rec->name, name->getText().c_str(), YAPET::NAME_SIZE);
00162 strncpy((char*)ptr_rec->host, host->getText().c_str(), YAPET::HOST_SIZE);
00163 strncpy((char*)ptr_rec->username, username->getText().c_str(), YAPET::USERNAME_SIZE);
00164 strncpy((char*)ptr_rec->password, password->getText().c_str(), YAPET::PASSWORD_SIZE);
00165 strncpy((char*)ptr_rec->comment, comment->getText().c_str(), YAPET::COMMENT_SIZE);
00166
00167 YAPET::BDBuffer* enc_rec = NULL;
00168 try {
00169 encentry = new YAPET::PartDec(unenc_rec, *key);
00170 } catch (YAPET::YAPETException& ex) {
00171 if (enc_rec != NULL)
00172 delete enc_rec;
00173 encentry = NULL;
00174
00175 YAPETUI::MessageBox* msgbox = NULL;
00176 try {
00177 msgbox = new YAPETUI::MessageBox(_("E R R O R"), ex.what());
00178 msgbox->run();
00179 delete msgbox;
00180 } catch (YAPETUI::UIException&) {
00181 if (msgbox != NULL)
00182 delete msgbox;
00183
00184 }
00185 }
00186 return;
00187 }
00188
00189 #ifdef HAVE_WRESIZE
00190 while ( (ch = cancelbutton->focus()) == KEY_RESIZE)
00191 YAPETUI::BaseWindow::resizeAll();
00192 #else // HAVE_WRESIZE
00193 ch = cancelbutton->focus();
00194 #endif // HAVE_WRESIZE
00195 if (ch == '\n') {
00196 encentry = NULL;
00197 return;
00198 }
00199 }
00200 }
00201
00202 void
00203 PasswordRecord::resize() throw(YAPETUI::UIException) {
00204 int retval = delwin(window);
00205 if (retval == ERR)
00206 throw YAPETUI::UIException(_("Error deleting password entry window"));
00207
00208 delete name;
00209 delete host;
00210 delete username;
00211 delete password;
00212 delete comment;
00213 delete okbutton;
00214 delete cancelbutton;
00215
00216 window = NULL;
00217 name = NULL;
00218 username = NULL;
00219 password = NULL;
00220 comment = NULL;
00221 okbutton = NULL;
00222 cancelbutton = NULL;
00223
00224 createWindow();
00225 }
00226
00227 void
00228 PasswordRecord::refresh() throw(YAPETUI::UIException) {
00229 YAPETUI::Colors::setcolor(window, YAPETUI::MESSAGEBOX);
00230
00231 int retval = wclear(window);
00232 if (retval == ERR)
00233 throw YAPETUI::UIException(_("Error clearing window"));
00234
00235 retval = box(window, 0, 0);
00236 if (retval == ERR)
00237 throw YAPETUI::UIException(_("Error adding box"));
00238
00239 retval = mymvwaddstr(window, 0, 2, _("P A S S W O R D R E C O R D"));
00240 if (retval == ERR)
00241 throw YAPETUI::UIException(_("Error setting label"));
00242
00243 retval = mymvwaddstr(window, 1, 1, _("Name"));
00244 if (retval == ERR)
00245 throw YAPETUI::UIException(_("Error adding label"));
00246
00247 retval = mymvwaddstr(window, 3, 1, _("Host"));
00248 if (retval == ERR)
00249 throw YAPETUI::UIException(_("Error adding label"));
00250
00251 retval = mymvwaddstr(window, 5, 1, _("Username"));
00252 if (retval == ERR)
00253 throw YAPETUI::UIException(_("Error adding label"));
00254
00255 retval = mymvwaddstr(window, 7, 1, _("Password"));
00256 if (retval == ERR)
00257 throw YAPETUI::UIException(_("Error adding label"));
00258
00259 retval = mymvwaddstr(window, 9, 1, _("Comment"));
00260 if (retval == ERR)
00261 throw YAPETUI::UIException(_("Error adding label"));
00262
00263 retval = wrefresh(window);
00264 if (retval == ERR)
00265 throw YAPETUI::UIException(_("Error refreshing window"));
00266
00267
00268 if (encentry != NULL) {
00269 YAPET::Record<YAPET::PasswordRecord>* dec_rec = NULL;
00270 try {
00271 YAPET::Crypt crypt(*key);
00272 dec_rec = crypt.decrypt<YAPET::PasswordRecord>(encentry->getEncRecord());
00273 YAPET::PasswordRecord* ptr_rec = *dec_rec;
00274 name->setText((char*)ptr_rec->name);
00275 host->setText((char*)ptr_rec->host);
00276 username->setText((char*)ptr_rec->username);
00277 password->setText((char*)ptr_rec->password);
00278 comment->setText((char*)ptr_rec->comment);
00279 delete dec_rec;
00280 } catch (YAPET::YAPETException& ex) {
00281 if (dec_rec != NULL)
00282 delete dec_rec;
00283 YAPETUI::MessageBox* msgbox = NULL;
00284 try {
00285 msgbox = new YAPETUI::MessageBox(_("E R R O R"), ex.what());
00286 msgbox->run();
00287 delete msgbox;
00288 } catch (YAPETUI::UIException&) {
00289 if (msgbox != NULL)
00290 delete msgbox;
00291
00292 }
00293 }
00294 }
00295 name->refresh();
00296 host->refresh();
00297 username->refresh();
00298 password->refresh();
00299 comment->refresh();
00300 okbutton->refresh();
00301 cancelbutton->refresh();
00302 }