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