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