00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifdef HAVE_CONFIG_H
00021 # include <config.h>
00022 #endif
00023
00024 #ifdef HAVE_FCNTL_H
00025 # include <fcntl.h>
00026 #endif
00027
00028 #ifdef HAVE_SYS_TYPES_H
00029 # include <sys/types.h>
00030 #endif
00031
00032 #ifdef HAVE_SYS_STAT_H
00033 # include <sys/stat.h>
00034 #endif
00035
00036 #ifdef HAVE_SIGNAL_H
00037 # include <signal.h>
00038 #endif
00039
00040 #ifdef HAVE_ERRNO_H
00041 # include <errno.h>
00042 #endif
00043
00044 #ifdef HAVE_STRING_H
00045 # include <string.h>
00046 #endif
00047
00048 #ifdef TIME_WITH_SYS_TIME
00049 # include <sys/time.h>
00050 # include <time.h>
00051 #else
00052 # ifdef HAVE_SYS_TIME_H
00053 # include <sys/time.h>
00054 # else
00055 # include <time.h>
00056 # endif
00057 #endif // TIME_WITH_SYS_TIME
00058
00059 #include <button.h>
00060 #include <dialogbox.h>
00061 #include <colors.h>
00062
00063 #include "mainwindow.h"
00064 #include "fileopen.h"
00065 #include "passworddialog.h"
00066 #include "passwordrecord.h"
00067 #include "searchdialog.h"
00068
00075 struct KeyDesc {
00081 int y;
00087 int x;
00093 const char* key;
00099 const char* desc;
00100 };
00101
00107 KeyDesc keys[] = { {3, 2, "S", "Save File"},
00108 {4, 2, "R", "Load File"},
00109 {5, 2, "L", "Lock Screen"},
00110 {6, 2, "A", "Add Entry"},
00111 {7, 2, "D", "Delete Entry"},
00112 {8, 2, "O", "Sort Order"},
00113 {9, 2, "/", "Search"},
00114 {10, 2, "N", "Search Next"},
00115 {11, 2, "C", "Change Password"},
00116 {12, 2, "^L", "Redraw Screen"},
00117 {13, 2, "Q", "Quit"},
00118 {0, 0, NULL, NULL}
00119 };
00120
00121 #if defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
00122
00128 class Alarm : public YAPETUI::BaseWindow::AlarmFunction {
00129 private:
00130 MainWindow& ref;
00131 public:
00132 inline Alarm(MainWindow& r) : ref(r) {}
00133 inline void process(int signo) {
00134 ref.handle_signal(signo);
00135 }
00136 };
00137 #endif // defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
00138
00139 void
00140 MainWindow::printTitle() throw(YAPETUI::UIException) {
00141 YAPETUI::Colors::setcolor(stdscr, YAPETUI::DEFAULT);
00142
00143
00144 int retval = wmove(stdscr, 0,0);
00145 if (retval == ERR)
00146 throw YAPETUI::UIException("Error moving cursor");
00147 retval = wclrtoeol(stdscr);
00148 if (retval == ERR)
00149 throw YAPETUI::UIException("Error clearing line");
00150
00151
00152 char title[100];
00153 snprintf(title,100, "..::|| %s ||::..", PACKAGE_STRING);
00154
00155
00156 retval = wmove(stdscr, 0, maxX()/2 - strlen(title)/2);
00157 if (retval == ERR)
00158 throw YAPETUI::UIException("Error moving cursor");
00159 retval = mywaddstr(stdscr, title);
00160 if (retval == ERR)
00161 throw YAPETUI::UIException("Error printing title");
00162 }
00163
00164 void
00165 MainWindow::topRightWinContent() throw (YAPETUI::UIException) {
00166 int max_y, max_x;
00167 getmaxyx (toprightwin, max_y, max_x);
00168
00169 char win_title[] = "K E Y S";
00170 int start_title_x = max_x / 2 - strlen (win_title) / 2;
00171
00172 int retval = mymvwaddstr (toprightwin, 1, start_title_x, win_title);
00173 if (retval == ERR)
00174 throw YAPETUI::UIException ("mvwaddstr() blew it");
00175 retval = wmove (toprightwin, 2, 1);
00176 if (retval == ERR)
00177 throw YAPETUI::UIException ("wmove() blew it");
00178
00179 #if defined(_XOPEN_CURSES) && !defined(__NCURSES_H)
00180 retval = whline (toprightwin, '-', max_x - 2);
00181 #else
00182 retval = whline (toprightwin, 0, max_x - 2);
00183 #endif
00184 if (retval == ERR)
00185 throw YAPETUI::UIException ("whline() blew it");
00186
00187
00188 KeyDesc* ptr = keys;
00189 while (ptr->key != NULL
00190 && ptr->desc != NULL) {
00191
00192 wattron (toprightwin, A_REVERSE);
00193 retval = mvwprintw (toprightwin, ptr->y, ptr->x, " %0-2s ", ptr->key);
00194 if (retval == ERR)
00195 throw YAPETUI::UIException ("mvprintw() blew it");
00196 wattroff (toprightwin, A_REVERSE);
00197 retval = mymvwaddstr (toprightwin, ptr->y, ptr->x + 8, ptr->desc);
00198 if (retval == ERR)
00199 throw YAPETUI::UIException ("waddstr() blew it");
00200
00201 ptr++;
00202 }
00203
00204 }
00205
00206 void
00207 MainWindow::bottomRightWinContent() throw(YAPETUI::UIException) {
00208 if (key == NULL || recordlist == NULL) return;
00209
00210 int retval = mymvwaddstr(bottomrightwin, 1, 2, "Cipher: Blowfish");
00211 if (retval == ERR)
00212 throw YAPETUI::UIException ("waddstr() blew it");
00213 retval = mvwprintw(bottomrightwin, 2, 2, "Key: %d bytes (%d bits)", key->size(), key->size()*8);
00214 if (retval == ERR)
00215 throw YAPETUI::UIException ("mvprintw() blew it");
00216 retval = mvwprintw(bottomrightwin, 3, 2, "%d entries ", recordlist->size());
00217 if (retval == ERR)
00218 throw YAPETUI::UIException ("mvprintw() blew it");
00219
00220 #if defined(HAVE_ASCTIME) && defined(HAVE_LOCALTIME)
00221 if (file != NULL) {
00222 try {
00223 time_t t = file->getMasterPWSet(*key);
00224 retval = mvwprintw(bottomrightwin, 4, 2, "PW set: %s",
00225 asctime(localtime(&t)) );
00226 if (retval == ERR)
00227 throw YAPETUI::UIException ("mvprintw() blew it");
00228 } catch (YAPET::YAPETException& ex) {
00229 statusbar.putMsg(ex.what());
00230 }
00231 }
00232 #endif
00233
00234 }
00235
00236 void
00237 MainWindow::createWindow() throw(YAPETUI::UIException) {
00238 if (toprightwin != NULL || bottomrightwin != NULL)
00239 throw YAPETUI::UIException("May you consider deleting the window before reallocating");
00240 int middleX = maxX() / 2;
00241 int thirdY = maxY() / 3 - 1;
00242
00243 printTitle();
00244
00245
00246
00247
00248 toprightwin = newwin (maxY() - thirdY - 1 , maxX() - middleX, 1, middleX);
00249 if (toprightwin == NULL)
00250 throw YAPETUI::UIException ("newwin() returned NULL");
00251
00252 YAPETUI::Colors::setcolor(toprightwin, YAPETUI::DEFAULT);
00253 int retval = box (toprightwin, 0, 0);
00254 if (retval == ERR)
00255 throw YAPETUI::UIException ("box() blew it");
00256
00257 topRightWinContent();
00258
00259
00260
00261
00262 bottomrightwin = newwin (thirdY - 1 , maxX() - middleX, maxY()-thirdY, middleX);
00263 if (bottomrightwin == NULL)
00264 throw YAPETUI::UIException ("newwin() returned NULL");
00265
00266 YAPETUI::Colors::setcolor(bottomrightwin, YAPETUI::DEFAULT);
00267 retval = werase (bottomrightwin);
00268 if (retval == ERR)
00269 throw YAPETUI::UIException("werase() blew it");
00270 retval = box (bottomrightwin, 0, 0);
00271 if (retval == ERR)
00272 throw YAPETUI::UIException ("box() blew it");
00273
00274
00275
00276
00277
00278 if (recordlist == NULL) {
00279 std::list<YAPET::PartDec> emptylist;
00280 recordlist = new YAPETUI::ListWidget<YAPET::PartDec> (emptylist,
00281 0,
00282 1,
00283 maxX() / 2,
00284 maxY() - 2);
00285 }
00286
00287 bottomRightWinContent();
00288 }
00289
00290 void
00291 MainWindow::resize() throw (YAPETUI::UIException) {
00292 int retval = delwin(toprightwin);
00293 if (retval == ERR)
00294 throw YAPETUI::UIException("delwin() blew it");
00295
00296 retval = delwin(bottomrightwin);
00297 if (retval == ERR)
00298 throw YAPETUI::UIException("delwin() blew it");
00299
00300 toprightwin = NULL;
00301 bottomrightwin = NULL;
00302
00303
00304 createWindow();
00305
00306 recordlist->resize (0, 1, maxX()/2, maxY() - 2);
00307 }
00308
00309 void
00310 MainWindow::refresh() throw (YAPETUI::UIException) {
00311 printTitle();
00312
00313 int retval = wrefresh(stdscr);
00314 if (retval == ERR)
00315 throw YAPETUI::UIException("Error refreshing stdscr");
00316
00317 topRightWinContent();
00318 bottomRightWinContent();
00319
00320 retval = box(toprightwin, 0, 0);
00321 if (retval == ERR)
00322 throw YAPETUI::UIException ("Error setting border");
00323
00324 retval = box(bottomrightwin, 0, 0);
00325 if (retval == ERR)
00326 throw YAPETUI::UIException ("Error setting border");
00327
00328
00329 retval = wrefresh (toprightwin);
00330 if (retval == ERR)
00331 throw YAPETUI::UIException ("Error refreshing top right window");
00332
00333 retval = wrefresh (bottomrightwin);
00334 if (retval == ERR)
00335 throw YAPETUI::UIException ("Error refreshing bottom right window");
00336
00337 recordlist->refresh();
00338 statusbar.refresh();
00339 }
00340
00341 void
00342 MainWindow::createFile(std::string& filename) throw(YAPETUI::UIException) {
00343 closeFile();
00344
00345 PasswordDialog* pwdia = NULL;
00346 try {
00347 pwdia = new PasswordDialog(NEW_PW, filename);
00348 pwdia->run();
00349 key = pwdia->getKey();
00350 delete pwdia;
00351 } catch(YAPETUI::UIException&) {
00352 if (pwdia != NULL)
00353 delete pwdia;
00354
00355 statusbar.putMsg("Error while asking for password");
00356 return;
00357 }
00358
00359 if (key == NULL) {
00360 statusbar.putMsg("Creation of file canceled");
00361 return;
00362 }
00363
00364 try {
00365 file = new YAPET::File(filename, *key, true);
00366 statusbar.putMsg(filename + " created");
00367 records_changed = false;
00368 } catch(YAPET::YAPETException& ex) {
00369 YAPETUI::MessageBox* msgbox = NULL;
00370 try{
00371 msgbox = new YAPETUI::MessageBox("E R R O R", ex.what());
00372 msgbox->run();
00373 delete msgbox;
00374 } catch (YAPETUI::UIException&) {
00375 if (msgbox != NULL)
00376 delete msgbox;
00377
00378 statusbar.putMsg("Error showing error message");
00379 }
00380 closeFile();
00381 }
00382 }
00383
00384 void
00385 MainWindow::openFile(std::string filename) throw(YAPETUI::UIException) {
00386 struct stat st;
00387 int retval = stat(filename.c_str(), &st);
00388 if (retval == -1 && errno == ENOENT) {
00389
00390 YAPETUI::DialogBox* question =
00391 new YAPETUI::DialogBox("Question",
00392 "The file does not exist. Do you want to create it?");
00393 question->run();
00394 YAPETUI::ANSWER a = question->getAnswer();
00395 delete question;
00396 if ( a == YAPETUI::ANSWER_OK) {
00397 createFile(filename);
00398 return;
00399 } else {
00400 statusbar.putMsg("File creation canceled");
00401 return;
00402 }
00403 } else if (retval == -1) {
00404
00405 YAPETUI::MessageBox* errmsg = NULL;
00406 try {
00407 errmsg = new YAPETUI::MessageBox("Error", strerror(errno));
00408 errmsg->run();
00409 delete errmsg;
00410 } catch (YAPETUI::UIException&) {
00411 if (errmsg != NULL)
00412 delete errmsg;
00413 }
00414 refresh();
00415 return;
00416 }
00417
00418
00419
00420 if (!S_ISREG(st.st_mode)) {
00421 YAPETUI::MessageBox* errmsg = NULL;
00422 try {
00423 errmsg = new YAPETUI::MessageBox("Error",
00424 "The specified file is not a regular file");
00425 errmsg->run();
00426 delete errmsg;
00427 } catch (YAPETUI::UIException&) {
00428 if (errmsg != NULL)
00429 delete errmsg;
00430 }
00431 return;
00432 }
00433
00434 closeFile();
00435
00436
00437 PasswordDialog* pwdia = NULL;
00438 try {
00439 pwdia = new PasswordDialog(EXISTING_PW, filename);
00440 pwdia->run();
00441 key = pwdia->getKey();
00442 delete pwdia;
00443 } catch (YAPETUI::UIException&) {
00444 if (pwdia != NULL)
00445 delete pwdia;
00446 statusbar.putMsg("UI error while asking for password");
00447 }
00448
00449
00450 if (key != NULL) {
00451 try {
00452 file = new YAPET::File(filename, *key, false);
00453 std::list<YAPET::PartDec> tmp_list = file->read(*key);
00454 recordlist->setList(tmp_list);
00455 statusbar.putMsg(filename + " opened");
00456 return;
00457 } catch(YAPET::YAPETException& e) {
00458 if (file != NULL)
00459 delete file;
00460
00461 YAPETUI::MessageBox* msgbox = NULL;
00462 try {
00463 msgbox = new YAPETUI::MessageBox("Error", e.what());
00464 msgbox->run();
00465 delete msgbox;
00466 } catch (YAPETUI::UIException&) {
00467 if (msgbox != NULL)
00468 delete msgbox;
00469 statusbar.putMsg("Error while trying to show error");
00470 }
00471 delete key;
00472 key = NULL;
00473 file = NULL;
00474 statusbar.putMsg("Error opening file");
00475 return;
00476 }
00477 } else {
00478 statusbar.putMsg("Opening of " + filename + " canceled");
00479 }
00480 }
00481
00482 void
00483 MainWindow::saveFile() {
00484 if (key == NULL || file == NULL) return;
00485 try {
00486 file->save(recordlist->getList());
00487 records_changed = false;
00488 statusbar.putMsg(file->getFilename() + " saved");
00489 } catch (YAPET::YAPETException& ex) {
00490 YAPETUI::MessageBox* msgbox = NULL;
00491 try {
00492 msgbox = new YAPETUI::MessageBox("Error", ex.what());
00493 msgbox->run();
00494 delete msgbox;
00495 } catch (YAPETUI::UIException) {
00496 if (msgbox != NULL)
00497 delete msgbox;
00498 statusbar.putMsg("Error showing error message");
00499 }
00500 }
00501 }
00502
00503 void
00504 MainWindow::closeFile() {
00505
00506 if (key != NULL) {
00507 delete key;
00508 key = NULL;
00509 }
00510 if (file != NULL) {
00511 delete file;
00512 file = NULL;
00513 }
00514
00515
00516 recordlist->getList().clear();
00517 records_changed = false;
00518 }
00519
00520 void
00521 MainWindow::addNewRecord() {
00522 if (key == NULL || file == NULL) return;
00523 PasswordRecord* pwentry = NULL;
00524 try {
00525 pwentry = new PasswordRecord(*key, NULL);
00526 pwentry->run();
00527 if (pwentry->entryChanged() &&
00528 pwentry->getEncEntry() != NULL) {
00529 recordlist->getList().push_back(*pwentry->getEncEntry());
00530 recordlist->setSortOrder();
00531 delete pwentry->getEncEntry();
00532 records_changed = true;
00533 statusbar.putMsg("New record added");
00534 } else {
00535 statusbar.putMsg("Record addition canceled");
00536 }
00537 delete pwentry;
00538 } catch (YAPETUI::UIException& ex) {
00539 if (pwentry != NULL) {
00540 if (pwentry->getEncEntry() != NULL)
00541 delete pwentry->getEncEntry();
00542 delete pwentry;
00543 }
00544
00545 YAPETUI::MessageBox* msgbox = NULL;
00546 try {
00547 msgbox = new YAPETUI::MessageBox("Error", "Error adding password entry");
00548 msgbox->run();
00549 delete msgbox;
00550 } catch (YAPETUI::UIException&) {
00551 if (msgbox != NULL)
00552 delete msgbox;
00553
00554 statusbar.putMsg("Error showing error message");
00555 }
00556
00557 }
00558 ::refresh();
00559 refresh();
00560 }
00561
00562 void
00563 MainWindow::editSelectedRecord() {
00564 if (key == NULL ||
00565 file == NULL ||
00566 recordlist->size() == 0) return;
00567 PasswordRecord* pwentry = NULL;
00568 try {
00569 YAPET::PartDec pd = recordlist->getSelectedItem();
00570 pwentry = new PasswordRecord(*key, &pd);
00571 pwentry->run();
00572 if (pwentry->entryChanged() &&
00573 pwentry->getEncEntry() != NULL) {
00574 recordlist->replaceCurrentItem(*pwentry->getEncEntry());
00575 recordlist->setSortOrder();
00576 records_changed = true;
00577 statusbar.putMsg("Record edited");
00578 delete pwentry->getEncEntry();
00579 } else {
00580 statusbar.putMsg("Record edition canceled");
00581 }
00582 delete pwentry;
00583 } catch (YAPETUI::UIException& ex) {
00584 if (pwentry != NULL) {
00585 if (pwentry->getEncEntry() != NULL)
00586 delete pwentry->getEncEntry();
00587 delete pwentry;
00588 }
00589
00590 YAPETUI::MessageBox* msgbox = NULL;
00591 try {
00592 msgbox = new YAPETUI::MessageBox("Error", "Error adding password entry");
00593 msgbox->run();
00594 delete msgbox;
00595 } catch (YAPETUI::UIException&) {
00596 if (msgbox != NULL)
00597 delete msgbox;
00598
00599 statusbar.putMsg("Error showing error message");
00600 }
00601 }
00602 ::refresh();
00603 refresh();
00604 }
00605
00606 void
00607 MainWindow::deleteSelectedRecord() throw(YAPETUI::UIException){
00608 if (recordlist->size() < 1) return;
00609
00610 YAPETUI::DialogBox* dialog = NULL;
00611 try {
00612 dialog = new YAPETUI::DialogBox("Question", "Delete selected record?");
00613 dialog->run();
00614 YAPETUI::ANSWER a = dialog->getAnswer();
00615 if (a == YAPETUI::ANSWER_OK) {
00616 recordlist->deleteSelectedItem();
00617 records_changed = true;
00618 recordlist->refresh();
00619 statusbar.putMsg("Record deleted");
00620 records_changed = true;
00621 } else {
00622 statusbar.putMsg("");
00623 }
00624 delete dialog;
00625 } catch(YAPETUI::UIException&) {
00626 if (dialog != NULL)
00627 delete dialog;
00628
00629 YAPETUI::MessageBox* msgbox = NULL;
00630 try {
00631 msgbox = new YAPETUI::MessageBox("Error", "Error showing dialog");
00632 msgbox->run();
00633 delete msgbox;
00634 } catch (YAPETUI::UIException&) {
00635 if (msgbox != NULL)
00636 delete msgbox;
00637
00638 statusbar.putMsg("Error showing error message");
00639 }
00640 }
00641 refresh();
00642 }
00643
00644 void
00645 MainWindow::setSortOrder() {
00646 try {
00647 switch (recordlist->getSortOrder()) {
00648 case(YAPETUI::ListWidget<YAPET::PartDec>::ASCENDING):
00649 recordlist->setSortOrder(YAPETUI::ListWidget<YAPET::PartDec>::DESCENDING);
00650 statusbar.putMsg("Set sort order descending");
00651 break;
00652 case(YAPETUI::ListWidget<YAPET::PartDec>::DESCENDING):
00653 recordlist->setSortOrder(YAPETUI::ListWidget<YAPET::PartDec>::ASCENDING);
00654 statusbar.putMsg("Set sort order ascending");
00655 break;
00656 };
00657 recordlist->refresh();
00658 } catch(std::exception& ex) {
00659 YAPETUI::MessageBox* msgbox = NULL;
00660 try {
00661 msgbox = new YAPETUI::MessageBox("Error", ex.what());
00662 msgbox->run();
00663 delete msgbox;
00664 } catch (YAPETUI::UIException&) {
00665 if (msgbox != NULL)
00666 delete msgbox;
00667 statusbar.putMsg("Error showing error message");
00668 }
00669 }
00670 }
00671
00672 void
00673 MainWindow::searchTerm() {
00674 if (key == NULL ||
00675 file == NULL ||
00676 recordlist->size() == 0) return;
00677 SearchDialog* searchdialog = NULL;
00678 try {
00679 searchdialog = new SearchDialog();
00680 searchdialog->run();
00681 if (!searchdialog->isCanceled()) {
00682 if (recordlist->searchTerm(searchdialog->getSearchTerm()) ) {
00683
00684 statusbar.putMsg("");
00685 } else {
00686 statusbar.putMsg("Search term not found");
00687 }
00688 } else {
00689 statusbar.putMsg("Search canceled");
00690 }
00691 delete searchdialog;
00692 } catch (YAPETUI::UIException& ex) {
00693 if (searchdialog != NULL) {
00694 delete searchdialog;
00695 }
00696
00697 YAPETUI::MessageBox* msgbox = NULL;
00698 try {
00699 msgbox = new YAPETUI::MessageBox("Error", ex.what());
00700 msgbox->run();
00701 delete msgbox;
00702 } catch (YAPETUI::UIException&) {
00703 if (msgbox != NULL)
00704 delete msgbox;
00705
00706 statusbar.putMsg("Error showing error message");
00707 }
00708 }
00709 ::refresh();
00710 refresh();
00711 }
00712
00713 void
00714 MainWindow::searchNext() {
00715 if (key == NULL ||
00716 file == NULL ||
00717 recordlist->size() == 0) return;
00718
00719 if (recordlist->searchNext() ) {
00720
00721 statusbar.putMsg("");
00722 } else {
00723 statusbar.putMsg("Search term not found");
00724 }
00725 }
00726
00727 bool
00728 MainWindow::quit() {
00729 if (!records_changed) return true;
00730
00731 YAPETUI::DialogBox* dialogbox = NULL;
00732 try {
00733 dialogbox = new YAPETUI::DialogBox("Question", "Save before quitting?");
00734 dialogbox->run();
00735 YAPETUI::ANSWER a = dialogbox->getAnswer();
00736 delete dialogbox;
00737 if (a == YAPETUI::ANSWER_OK) {
00738 saveFile();
00739 return true;
00740 }
00741
00742 return true;
00743 } catch (YAPETUI::UIException&) {
00744 if (dialogbox != NULL)
00745 delete dialogbox;
00746 statusbar.putMsg("Error showing error message");
00747 refresh();
00748 return false;
00749 }
00750 }
00751
00752 void
00753 MainWindow::lockScreen() const throw(YAPETUI::UIException){
00754 if (key == NULL) return;
00755 int ch;
00756 while (true) {
00757 WINDOW* lockwin = newwin(0,0,0,0);
00758 if (lockwin == NULL)
00759 throw YAPETUI::UIException("Error creating lock window");
00760
00761 int retval = werase(lockwin);
00762 if (retval == ERR) {
00763 delwin(lockwin);
00764 throw YAPETUI::UIException("Error erasing window");
00765 }
00766
00767
00768 retval = wrefresh(lockwin);
00769 if (retval == ERR) {
00770 delwin(lockwin);
00771 throw YAPETUI::UIException("Error refreshing window");
00772 }
00773
00774 ch = wgetch(lockwin);
00775 #ifdef HAVE_WRESIZE
00776 if (ch == KEY_RESIZE) {
00777 delwin(lockwin);
00778 YAPETUI::BaseWindow::resizeAll();
00779 continue;
00780 }
00781 #endif
00782 PasswordDialog* pwdia = NULL;
00783 YAPET::Key* testkey = NULL;
00784 try {
00785 pwdia = new PasswordDialog(EXISTING_PW, file->getFilename());
00786 pwdia->run();
00787 testkey = pwdia->getKey();
00788 delete pwdia;
00789 } catch(YAPETUI::UIException&) {
00790 if (pwdia != NULL)
00791 delete pwdia;
00792 if (testkey != NULL)
00793 delete testkey;
00794 delwin(lockwin);
00795 continue;
00796 }
00797
00798 if (testkey == NULL) {
00799 delwin(lockwin);
00800 continue;
00801 }
00802
00803 if (*testkey != *key) {
00804 YAPETUI::MessageBox* msgbox = NULL;
00805 try {
00806 msgbox = new YAPETUI::MessageBox("Error", "Wrong password");
00807 msgbox->run();
00808 delete msgbox;
00809 } catch (YAPETUI::UIException&) {
00810 if (msgbox != NULL)
00811 delete msgbox;
00812 }
00813 } else {
00814 delete testkey;
00815 delwin(lockwin);
00816 return;
00817 }
00818
00819 delete testkey;
00820 delwin(lockwin);
00821 }
00822 }
00823
00824 void
00825 MainWindow::changePassword() throw(YAPETUI::UIException) {
00826 if (file == NULL || key == NULL) return;
00827
00828
00829 if (records_changed) {
00830 YAPETUI::DialogBox* dialogbox = NULL;
00831 try {
00832 dialogbox = new YAPETUI::DialogBox("Question", "Save before changing password?");
00833 dialogbox->run();
00834 YAPETUI::ANSWER a = dialogbox->getAnswer();
00835 delete dialogbox;
00836 if (a == YAPETUI::ANSWER_OK) {
00837 saveFile();
00838 } else {
00839 statusbar.putMsg("Password change aborted");
00840 return;
00841 }
00842 } catch (YAPETUI::UIException&) {
00843 if (dialogbox != NULL)
00844 delete dialogbox;
00845 statusbar.putMsg("Error showing error message");
00846 refresh();
00847 return;
00848 }
00849 }
00850
00851
00852 PasswordDialog* pwdia = NULL;
00853 YAPET::Key* newkey;
00854 try {
00855 pwdia = new PasswordDialog(NEW_PW, file->getFilename());
00856 pwdia->run();
00857 newkey = pwdia->getKey();
00858 delete pwdia;
00859 } catch(YAPETUI::UIException&) {
00860 if (pwdia != NULL)
00861 delete pwdia;
00862
00863 statusbar.putMsg("Error while asking for password");
00864 return;
00865 }
00866
00867
00868 if (newkey == NULL) {
00869 statusbar.putMsg("Password change canceled");
00870 return;
00871 }
00872
00873
00874 try {
00875 file->setNewKey(*key, *newkey);
00876 } catch (std::exception& ex) {
00877 delete newkey;
00878 YAPETUI::MessageBox* msgbox = NULL;
00879 try {
00880 msgbox = new YAPETUI::MessageBox("Error", ex.what());
00881 msgbox->run();
00882 delete msgbox;
00883 } catch (YAPETUI::UIException&) {
00884 if (msgbox != NULL)
00885 delete msgbox;
00886 }
00887 return;
00888 }
00889
00890 delete key;
00891 key = newkey;
00892
00893
00894 try {
00895 std::list<YAPET::PartDec> tmp_list = file->read(*key);
00896 recordlist->setList(tmp_list);
00897 } catch(YAPET::YAPETException& e) {
00898 if (file != NULL)
00899 delete file;
00900
00901 YAPETUI::MessageBox* msgbox = NULL;
00902 try {
00903 msgbox = new YAPETUI::MessageBox("Error", e.what());
00904 msgbox->run();
00905 delete msgbox;
00906 } catch (YAPETUI::UIException&) {
00907 if (msgbox != NULL)
00908 delete msgbox;
00909 statusbar.putMsg("Error while trying to show error");
00910 }
00911 delete key;
00912 key = NULL;
00913 file = NULL;
00914 statusbar.putMsg("Error reading from file");
00915 return;
00916 }
00917
00918 statusbar.putMsg("Password successfully changed");
00919 }
00920
00921 MainWindow::MainWindow() throw (YAPETUI::UIException) : BaseWindow(),
00922 toprightwin (NULL),
00923 bottomrightwin (NULL),
00924 recordlist (NULL),
00925 statusbar(),
00926 records_changed(false),
00927 key (NULL),
00928 file (NULL) {
00929 createWindow();
00930 }
00931
00932 MainWindow::~MainWindow() {
00933 delete recordlist;
00934 wclear(toprightwin);
00935 wclear(bottomrightwin);
00936 delwin (toprightwin);
00937 delwin (bottomrightwin);
00938 if (key != NULL)
00939 delete key;
00940 if (file != NULL)
00941 delete file;
00942
00943
00944 }
00945
00946 void
00947 MainWindow::run() throw (YAPETUI::UIException) {
00948
00949 if (file == NULL || key == NULL)
00950 statusbar.putMsg ("No file loaded");
00951
00952 if (file != NULL && key != NULL)
00953 statusbar.putMsg(file->getFilename() + " loaded");
00954
00955 refresh();
00956
00957 Alarm alrm(*this);
00958 int ch;
00959 while(true) {
00960 try {
00961 #if defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
00962 BaseWindow::setTimeout(&alrm,600);
00963 #endif // defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
00964 while ( (ch=recordlist->focus()) ) {
00965 #if defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
00966 YAPETUI::BaseWindow::suspendTimeout();
00967 #endif // defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
00968 switch (ch) {
00969 case '\n':
00970 editSelectedRecord();
00971 break;
00972 case 3:
00973 case 'Q':
00974 case 'q':
00975 if (quit()) return;
00976 break;
00977 #ifdef HAVE_WRESIZE
00978 case KEY_RESIZE:
00979 YAPETUI::BaseWindow::resizeAll();
00980 break;
00981 #endif // HAVE_WRESIZE
00982 case KEY_REFRESH:
00983 #ifdef HAVE_WRESIZE
00984 YAPETUI::BaseWindow::resizeAll();
00985 #endif // HAVE_WRESIZE
00986 YAPETUI::BaseWindow::refreshAll();
00987 break;
00988 case 'S':
00989 case 's':
00990 saveFile();
00991 break;
00992 case 'R':
00993 case 'r': {
00994 FileOpen* tmp = NULL;
00995 try {
00996 tmp = new FileOpen("O P E N F I L E");
00997 tmp->run();
00998 if (!tmp->isCanceled()) {
00999 openFile(tmp->getFilepath());
01000 }
01001 } catch (std::exception& ex2) {
01002 statusbar.putMsg(ex2.what());
01003 if (file != NULL)
01004 delete file;
01005 if (key != NULL)
01006 delete key;
01007 file = NULL;
01008 key = NULL;
01009 }
01010 delete tmp;
01011 ::refresh();
01012 YAPETUI::BaseWindow::refreshAll();
01013 }
01014 break;
01015 case 'L':
01016 case 'l':
01017 lockScreen();
01018 ::refresh();
01019 YAPETUI::BaseWindow::refreshAll();
01020 break;
01021
01022 case 'A':
01023 case 'a':
01024 addNewRecord();
01025 break;
01026
01027 case 'D':
01028 case 'd':
01029 deleteSelectedRecord();
01030 break;
01031
01032 case 'O':
01033 case 'o':
01034 setSortOrder();
01035 break;
01036
01037 case '/':
01038 searchTerm();
01039 break;
01040
01041 case 'N':
01042 case 'n':
01043 searchNext();
01044 break;
01045
01046 case 'c':
01047 case 'C':
01048 changePassword();
01049 ::refresh();
01050 YAPETUI::BaseWindow::refreshAll();
01051 break;
01052 }
01053 #if defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
01054 YAPETUI::BaseWindow::setTimeout(&alrm,600);
01055 #endif // defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
01056 }
01057 } catch(std::exception& ex) {
01058 statusbar.putMsg(ex.what());
01059 }
01060 }
01061 }
01062
01063 void
01064 MainWindow::run(std::string fn) {
01065 if (fn.empty()) {
01066 run();
01067 return;
01068 }
01069
01070 refresh();
01071
01072 try {
01073 openFile(fn);
01074 } catch (std::exception& ex2) {
01075 statusbar.putMsg(ex2.what());
01076 if (file != NULL)
01077 delete file;
01078 if (key != NULL)
01079 delete key;
01080 file = NULL;
01081 key = NULL;
01082 }
01083 ::refresh();
01084
01085 run();
01086 }
01087
01088 #if defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
01089 void
01090 MainWindow::handle_signal(int signo) {
01091 if (signo == SIGALRM) {
01092 lockScreen();
01093 ::refresh();
01094 YAPETUI::BaseWindow::refreshAll();
01095 }
01096 }
01097 #endif // defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)