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