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