yapet/mainwindow.cc

Go to the documentation of this file.
00001 // $Id: mainwindow.cc,v 1.12 2008-02-25 15:16:12 rafi Exp $
00002 //
00003 // YAPET -- Yet Another Password Encryption Tool
00004 // Copyright (C) 2008  Rafael Ostertag
00005 //
00006 // This program is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation, either version 3 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 
00074 struct KeyDesc {
00080     int y;
00086     int x;
00092     const char* key;
00098     const char* desc;
00099 };
00100 
00106 KeyDesc keys[] = { {4, 2, "S", "Save File"},
00107            {5, 2, "R", "Load File"},
00108            {6, 2, "L", "Lock Screen"},
00109            {7, 2, "A", "Add Entry"},
00110            {8, 2, "D", "Delete Entry"},
00111            {9, 2, "C", "Change Password"},
00112            {10, 2, "^L", "Redraw Screen"},
00113            {11, 2, "Q", "Quit"},
00114            {0, 0, NULL, NULL}
00115 };
00116 
00117 #if defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
00118 
00124 class Alarm : public YAPETUI::BaseWindow::AlarmFunction {
00125     private:
00126     MainWindow& ref;
00127     public:
00128     inline Alarm(MainWindow& r) : ref(r) {}
00129     inline void process(int signo) {
00130         ref.handle_signal(signo);
00131     }
00132 };
00133 #endif // defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
00134 
00135 void
00136 MainWindow::printTitle() throw(YAPETUI::UIException) {
00137     YAPETUI::Colors::setcolor(stdscr, YAPETUI::DEFAULT);
00138     // Title
00139     char title[100];
00140     snprintf(title,100, "..::|| %s ||::..", PACKAGE_STRING);
00141     int retval = wmove(stdscr, 0, maxX()/2 - strlen(title)/2);
00142     if (retval == ERR)
00143     throw YAPETUI::UIException("Error moving cursor");
00144     retval = mywaddstr(stdscr, title);
00145     if (retval == ERR)
00146     throw YAPETUI::UIException("Error printing title");
00147 }
00148 
00149 void
00150 MainWindow::topRightWinContent() throw (YAPETUI::UIException) {
00151     int max_y, max_x;
00152     getmaxyx (toprightwin, max_y, max_x);
00153 
00154     char win_title[] = "K E Y S";
00155     int start_title_x = max_x / 2 - strlen (win_title) / 2;
00156 
00157     int retval = mymvwaddstr (toprightwin, 1, start_title_x, win_title);
00158     if (retval == ERR)
00159     throw YAPETUI::UIException ("mvwaddstr() blew it");
00160     retval = wmove (toprightwin, 2, 1);
00161     if (retval == ERR)
00162     throw YAPETUI::UIException ("wmove() blew it");
00163 
00164 #if defined(_XOPEN_CURSES) && !defined(__NCURSES_H)
00165     retval = whline (toprightwin, '-', max_x - 2);
00166 #else
00167     retval = whline (toprightwin, 0, max_x - 2);
00168 #endif
00169     if (retval == ERR)
00170     throw YAPETUI::UIException ("whline() blew it");
00171 
00172 
00173     KeyDesc* ptr = keys;
00174     while (ptr->key != NULL
00175         && ptr->desc != NULL) {
00176 
00177     wattron (toprightwin, A_REVERSE);
00178     retval = mvwprintw (toprightwin, ptr->y, ptr->x, "  %0-2s  ", ptr->key);
00179     if (retval == ERR)
00180         throw YAPETUI::UIException ("mvprintw() blew it");
00181     wattroff (toprightwin, A_REVERSE);
00182     retval = mymvwaddstr (toprightwin, ptr->y, ptr->x + 8, ptr->desc);
00183     if (retval == ERR)
00184         throw YAPETUI::UIException ("waddstr() blew it");
00185 
00186     ptr++;
00187     }
00188 
00189 }
00190 
00191 void
00192 MainWindow::bottomRightWinContent() throw(YAPETUI::UIException) {
00193     if (key == NULL || recordlist == NULL) return;
00194 
00195     int retval = mymvwaddstr(bottomrightwin, 1, 2, "Cipher: Blowfish");
00196     if (retval == ERR)
00197     throw YAPETUI::UIException ("waddstr() blew it");
00198     retval = mvwprintw(bottomrightwin, 2, 2, "Key: %d bytes (%d bits)", key->size(), key->size()*8);
00199     if (retval == ERR)
00200     throw YAPETUI::UIException ("mvprintw() blew it");
00201     retval = mvwprintw(bottomrightwin, 3, 2, "%d entries ", recordlist->size());
00202     if (retval == ERR)
00203     throw YAPETUI::UIException ("mvprintw() blew it");
00204 
00205 #if defined(HAVE_ASCTIME) && defined(HAVE_LOCALTIME)
00206     if (file != NULL) {
00207     try {
00208         time_t t = file->getMasterPWSet(*key);
00209         retval = mvwprintw(bottomrightwin, 4, 2, "PW set: %s",
00210                    asctime(localtime(&t)) );
00211         if (retval == ERR)
00212         throw YAPETUI::UIException ("mvprintw() blew it");
00213     } catch (YAPET::YAPETException& ex) {
00214         statusbar.putMsg(ex.what());
00215     }
00216     }
00217 #endif
00218 
00219 }
00220 
00221 void
00222 MainWindow::createWindow() throw(YAPETUI::UIException) {
00223     if (toprightwin != NULL || bottomrightwin != NULL)
00224     throw YAPETUI::UIException("May you consider deleting the window before reallocating");
00225     int middleX = maxX() / 2;
00226     int thirdY = maxY() / 3 - 1;
00227 
00228     printTitle();
00229 
00230     //
00231     // Top right window
00232     //
00233     toprightwin = newwin (maxY() - thirdY - 1 , maxX() - middleX, 1, middleX);
00234     if (toprightwin == NULL)
00235     throw YAPETUI::UIException ("newwin() returned NULL");
00236 
00237     YAPETUI::Colors::setcolor(toprightwin, YAPETUI::DEFAULT);
00238     int retval = box (toprightwin, 0, 0);
00239     if (retval == ERR)
00240     throw YAPETUI::UIException ("box() blew it");
00241 
00242     topRightWinContent();
00243 
00244     //
00245     // Bottom right window
00246     //
00247     bottomrightwin = newwin (thirdY - 1 , maxX() - middleX, maxY()-thirdY, middleX);
00248     if (bottomrightwin == NULL)
00249     throw YAPETUI::UIException ("newwin() returned NULL");
00250 
00251     YAPETUI::Colors::setcolor(bottomrightwin, YAPETUI::DEFAULT);
00252     retval = werase (bottomrightwin);
00253     if (retval == ERR)
00254     throw YAPETUI::UIException("werase() blew it");
00255     retval = box (bottomrightwin, 0, 0);
00256     if (retval == ERR)
00257     throw YAPETUI::UIException ("box() blew it");
00258 
00259 
00260     //
00261     // List widget on the left
00262     //
00263     if (recordlist == NULL) {
00264     std::list<YAPET::PartDec> emptylist;
00265     recordlist = new YAPETUI::ListWidget<YAPET::PartDec> (emptylist,
00266                                  0,
00267                                  1,
00268                                  maxX() / 2,
00269                                  maxY() - 2);
00270     }
00271 
00272     bottomRightWinContent();
00273 }
00274 
00275 void
00276 MainWindow::resize() throw (YAPETUI::UIException) {
00277     int retval = delwin(toprightwin);
00278     if (retval == ERR)
00279     throw YAPETUI::UIException("delwin() blew it");
00280 
00281     retval = delwin(bottomrightwin);
00282     if (retval == ERR)
00283     throw YAPETUI::UIException("delwin() blew it");
00284 
00285     toprightwin = NULL;
00286     bottomrightwin = NULL;
00287 
00288 
00289     createWindow();
00290 
00291     recordlist->resize (0, 1, maxX()/2, maxY() - 2);
00292 }
00293 
00294 void
00295 MainWindow::refresh() throw (YAPETUI::UIException) {
00296     printTitle();
00297 
00298     int retval = wrefresh(stdscr);
00299     if (retval == ERR)
00300     throw YAPETUI::UIException("Error refreshing stdscr");
00301 
00302     topRightWinContent();
00303     bottomRightWinContent();
00304 
00305     retval = box(toprightwin, 0, 0);
00306     if (retval == ERR)
00307     throw YAPETUI::UIException ("Error setting border");
00308 
00309     retval = box(bottomrightwin, 0, 0);
00310     if (retval == ERR)
00311     throw YAPETUI::UIException ("Error setting border");
00312 
00313 
00314     retval = wrefresh (toprightwin);
00315     if (retval == ERR)
00316     throw YAPETUI::UIException ("Error refreshing top right window");
00317 
00318     retval = wrefresh (bottomrightwin);
00319     if (retval == ERR)
00320     throw YAPETUI::UIException ("Error refreshing bottom right window");
00321 
00322     recordlist->refresh();
00323     statusbar.refresh();
00324 }
00325 
00326 void
00327 MainWindow::createFile(std::string& filename) throw(YAPETUI::UIException) {
00328     closeFile();
00329 
00330     PasswordDialog* pwdia = NULL;
00331     try {
00332     pwdia = new PasswordDialog(NEW_PW, filename);
00333     pwdia->run();
00334     key = pwdia->getKey();
00335     delete pwdia;
00336     } catch(YAPETUI::UIException&) {
00337     if (pwdia != NULL)
00338         delete pwdia;
00339 
00340     statusbar.putMsg("Error while asking for password");
00341     return;
00342     }
00343 
00344     if (key == NULL) {
00345     statusbar.putMsg("Creation of file canceled");
00346     return;
00347     }
00348 
00349     try {
00350     file = new YAPET::File(filename, *key, true);
00351     statusbar.putMsg(filename + " created");
00352     records_changed = false;
00353     } catch(YAPET::YAPETException& ex) {
00354     YAPETUI::MessageBox* msgbox = NULL;
00355     try{
00356         msgbox = new YAPETUI::MessageBox("E R R O R", ex.what());
00357         msgbox->run();
00358         delete msgbox;
00359     } catch (YAPETUI::UIException&) {
00360         if (msgbox != NULL)
00361         delete msgbox;
00362 
00363         statusbar.putMsg("Error showing error message");
00364     }
00365     closeFile();
00366     }
00367 }
00368 
00369 void
00370 MainWindow::openFile(std::string filename) throw(YAPETUI::UIException) {
00371     struct stat st;
00372     int retval = stat(filename.c_str(), &st);
00373     if (retval == -1 && errno == ENOENT) {
00374     // Ask user whether or not he wants to create a new file
00375     YAPETUI::DialogBox* question =
00376         new YAPETUI::DialogBox("Question",
00377                    "The file does not exist. Do you want to create it?");
00378     question->run();
00379     YAPETUI::ANSWER a = question->getAnswer();
00380     delete question;
00381     if ( a == YAPETUI::ANSWER_OK) {
00382         createFile(filename);
00383         return;
00384     } else {
00385         statusbar.putMsg("File creation canceled");
00386         return;
00387     }
00388     } else if (retval == -1) {
00389     // Inform user about other error
00390     YAPETUI::MessageBox* errmsg = NULL;
00391     try {
00392         errmsg = new YAPETUI::MessageBox("Error", strerror(errno));
00393         errmsg->run();
00394         delete errmsg;
00395     } catch (YAPETUI::UIException&) {
00396         if (errmsg != NULL)
00397         delete errmsg;
00398     }
00399     refresh();
00400     return;
00401     }
00402 
00403     // We were able to stat the file, no make sure it is a file and open it
00404     // using the password
00405     if (!S_ISREG(st.st_mode)) {
00406     YAPETUI::MessageBox* errmsg = NULL;
00407     try {
00408         errmsg = new YAPETUI::MessageBox("Error",
00409                          "The specified file is not a regular file");
00410         errmsg->run();
00411         delete errmsg;
00412     } catch (YAPETUI::UIException&) {
00413         if (errmsg != NULL)
00414         delete errmsg;
00415     }
00416     return;
00417     }
00418 
00419     closeFile();
00420 
00421     // Ask for password
00422     PasswordDialog* pwdia = NULL;
00423     try {
00424     pwdia = new PasswordDialog(EXISTING_PW, filename);
00425     pwdia->run();
00426     key = pwdia->getKey();
00427     delete pwdia;
00428     } catch (YAPETUI::UIException&) {
00429     if (pwdia != NULL)
00430         delete pwdia;
00431     statusbar.putMsg("UI error while asking for password");
00432     }
00433 
00434     // Open file
00435     if (key != NULL) {
00436     try {
00437         file = new YAPET::File(filename, *key, false);
00438         std::list<YAPET::PartDec> tmp_list = file->read(*key);
00439         recordlist->setList(tmp_list);
00440         statusbar.putMsg(filename + " opened");
00441         return;
00442     } catch(YAPET::YAPETException& e) {
00443         if (file != NULL)
00444         delete file;
00445 
00446         YAPETUI::MessageBox* msgbox = NULL;
00447         try {
00448         msgbox = new YAPETUI::MessageBox("Error", e.what());
00449         msgbox->run();
00450         delete msgbox;
00451         } catch (YAPETUI::UIException&) {
00452         if (msgbox != NULL)
00453             delete msgbox;
00454         statusbar.putMsg("Error while trying to show error");
00455         }
00456         delete key;
00457         key = NULL;
00458         file = NULL;
00459         statusbar.putMsg("Error opening file");
00460         return;
00461     }
00462     } else {
00463     statusbar.putMsg("Opening of " + filename + " canceled");
00464     }
00465 }
00466 
00467 void
00468 MainWindow::saveFile() {
00469     if (key == NULL || file == NULL) return;
00470     try {
00471     file->save(recordlist->getList());
00472     records_changed = false;
00473     statusbar.putMsg(file->getFilename() + " saved");
00474     } catch (YAPET::YAPETException& ex) {
00475     YAPETUI::MessageBox* msgbox = NULL;
00476     try {
00477         msgbox = new YAPETUI::MessageBox("Error", ex.what());
00478         msgbox->run();
00479         delete msgbox;
00480     } catch (YAPETUI::UIException) {
00481         if (msgbox != NULL)
00482         delete msgbox;
00483         statusbar.putMsg("Error showing error message");
00484     }
00485     }
00486 }
00487 
00488 void
00489 MainWindow::closeFile() {
00490     // Remove key and close file
00491     if (key != NULL) {
00492     delete key;
00493     key = NULL;
00494     }
00495     if (file != NULL) {
00496     delete file;
00497     file = NULL;
00498     }
00499 
00500     // Clear list
00501     recordlist->getList().clear();
00502     records_changed = false;
00503 }
00504 
00505 void
00506 MainWindow::addNewRecord() {
00507     if (key == NULL || file == NULL) return;
00508     PasswordRecord* pwentry = NULL;
00509     try {
00510     pwentry = new PasswordRecord(*key, NULL);
00511     pwentry->run();
00512     if (pwentry->entryChanged() &&
00513         pwentry->getEncEntry() != NULL) {
00514         recordlist->getList().push_back(*pwentry->getEncEntry());
00515         delete pwentry->getEncEntry();
00516         records_changed = true;
00517         statusbar.putMsg("New record added");
00518     } else {
00519         statusbar.putMsg("Record addition canceled");
00520     }
00521     delete pwentry;
00522     } catch (YAPETUI::UIException& ex) {
00523     if (pwentry != NULL) {
00524         if (pwentry->getEncEntry() != NULL)
00525         delete pwentry->getEncEntry();
00526         delete pwentry;
00527     }
00528 
00529     YAPETUI::MessageBox* msgbox = NULL;
00530     try {
00531         msgbox = new YAPETUI::MessageBox("Error", "Error adding password entry");
00532         msgbox->run();
00533         delete msgbox;
00534     } catch (YAPETUI::UIException&) {
00535         if (msgbox != NULL)
00536         delete msgbox;
00537 
00538         statusbar.putMsg("Error showing error message");
00539     }
00540 
00541     }
00542     ::refresh();
00543     refresh();
00544 }
00545 
00546 void
00547 MainWindow::editSelectedRecord() {
00548     if (key == NULL ||
00549     file == NULL ||
00550     recordlist->size() == 0) return;
00551     PasswordRecord* pwentry = NULL;
00552     try {
00553     YAPET::PartDec pd = recordlist->getSelectedItem();
00554     pwentry = new PasswordRecord(*key, &pd);
00555     pwentry->run();
00556     if (pwentry->entryChanged() &&
00557         pwentry->getEncEntry() != NULL) {
00558         recordlist->replaceCurrentItem(*pwentry->getEncEntry());
00559         records_changed = true;
00560         statusbar.putMsg("Record edited");
00561         delete pwentry->getEncEntry();
00562     } else {
00563         statusbar.putMsg("Record edition canceled");
00564     }
00565     delete pwentry;
00566     } catch (YAPETUI::UIException& ex) {
00567     if (pwentry != NULL) {
00568         if (pwentry->getEncEntry() != NULL)
00569         delete pwentry->getEncEntry();
00570         delete pwentry;
00571     }
00572 
00573     YAPETUI::MessageBox* msgbox = NULL;
00574     try {
00575         msgbox = new YAPETUI::MessageBox("Error", "Error adding password entry");
00576         msgbox->run();
00577         delete msgbox;
00578     } catch (YAPETUI::UIException&) {
00579         if (msgbox != NULL)
00580         delete msgbox;
00581 
00582         statusbar.putMsg("Error showing error message");
00583     }
00584     }
00585     ::refresh();
00586     refresh();
00587 }
00588 
00589 void
00590 MainWindow::deleteSelectedRecord() throw(YAPETUI::UIException){
00591     YAPETUI::DialogBox* dialog = NULL;
00592     try {
00593     dialog = new YAPETUI::DialogBox("Question", "Delete selected record?");
00594     dialog->run();
00595     YAPETUI::ANSWER a = dialog->getAnswer();
00596     if (a == YAPETUI::ANSWER_OK) {
00597         recordlist->deleteSelectedItem();
00598         records_changed = true;
00599         recordlist->refresh();
00600         records_changed = true;
00601     }
00602     delete dialog;
00603     } catch(YAPETUI::UIException&) {
00604     if (dialog != NULL)
00605         delete dialog;
00606 
00607     YAPETUI::MessageBox* msgbox = NULL;
00608     try {
00609         msgbox = new YAPETUI::MessageBox("Error", "Error showing dialog");
00610         msgbox->run();
00611         delete msgbox;
00612     } catch (YAPETUI::UIException&) {
00613         if (msgbox != NULL)
00614         delete msgbox;
00615 
00616         statusbar.putMsg("Error showing error message");
00617     }
00618     }
00619     refresh();
00620 }
00621 
00622 bool
00623 MainWindow::quit() {
00624     if (!records_changed) return true;
00625 
00626     YAPETUI::DialogBox* dialogbox = NULL;
00627     try {
00628     dialogbox = new YAPETUI::DialogBox("Question", "Save before quitting?");
00629     dialogbox->run();
00630     YAPETUI::ANSWER a = dialogbox->getAnswer();
00631     delete dialogbox;
00632     if (a == YAPETUI::ANSWER_OK) {
00633         saveFile();
00634         return true;
00635     }
00636 
00637     return true;
00638     } catch (YAPETUI::UIException&) {
00639     if (dialogbox != NULL)
00640         delete dialogbox;
00641     statusbar.putMsg("Error showing error message");
00642     refresh();
00643     return false;
00644     }
00645 }
00646 
00647 void
00648 MainWindow::lockScreen() const throw(YAPETUI::UIException){
00649     if (key == NULL) return;
00650     int ch;
00651     while (true) {
00652     WINDOW* lockwin = newwin(0,0,0,0);
00653     if (lockwin == NULL)
00654         throw YAPETUI::UIException("Error creating lock window");
00655 
00656     int retval = werase(lockwin);
00657     if (retval == ERR) {
00658         delwin(lockwin);
00659         throw YAPETUI::UIException("Error erasing window");
00660     }
00661 
00662 
00663     retval = wrefresh(lockwin);
00664     if (retval == ERR) {
00665         delwin(lockwin);
00666         throw YAPETUI::UIException("Error refreshing window");
00667     }
00668 
00669     ch = wgetch(lockwin);
00670 #ifdef HAVE_WRESIZE
00671     if (ch == KEY_RESIZE) {
00672         delwin(lockwin);
00673         YAPETUI::BaseWindow::resizeAll();
00674         continue;
00675     }
00676 #endif
00677     PasswordDialog* pwdia = NULL;
00678     YAPET::Key* testkey = NULL;
00679     try {
00680         pwdia = new PasswordDialog(EXISTING_PW, file->getFilename());
00681         pwdia->run();
00682         testkey = pwdia->getKey();
00683         delete pwdia;
00684     } catch(YAPETUI::UIException&) {
00685         if (pwdia != NULL)
00686         delete pwdia;
00687         if (testkey != NULL)
00688         delete testkey;
00689         delwin(lockwin);
00690         continue;
00691     }
00692 
00693     if (testkey == NULL) {
00694         delwin(lockwin);
00695         continue;
00696     }
00697 
00698     if (*testkey != *key) {
00699         YAPETUI::MessageBox* msgbox = NULL;
00700         try {
00701         msgbox = new YAPETUI::MessageBox("Error", "Wrong password");
00702         msgbox->run();
00703         delete msgbox;
00704         } catch (YAPETUI::UIException&) {
00705         if (msgbox != NULL)
00706             delete msgbox;
00707         }
00708     } else {
00709         delete testkey;
00710         delwin(lockwin);
00711         return;
00712     }
00713 
00714     delete testkey;
00715     delwin(lockwin);
00716     }
00717 }
00718 
00719 void
00720 MainWindow::changePassword() throw(YAPETUI::UIException) {
00721     if (file == NULL || key == NULL) return;
00722 
00723     // Make sure there are no unsaved entries
00724     if (records_changed) {
00725     YAPETUI::DialogBox* dialogbox = NULL;
00726     try {
00727         dialogbox = new YAPETUI::DialogBox("Question", "Save before changing password?");
00728         dialogbox->run();
00729         YAPETUI::ANSWER a = dialogbox->getAnswer();
00730         delete dialogbox;
00731         if (a == YAPETUI::ANSWER_OK) {
00732         saveFile();
00733         } else {
00734         statusbar.putMsg("Password change aborted");
00735         return;
00736         }
00737     } catch (YAPETUI::UIException&) {
00738         if (dialogbox != NULL)
00739         delete dialogbox;
00740         statusbar.putMsg("Error showing error message");
00741         refresh();
00742         return;
00743     }
00744     }
00745 
00746     // Prompt for the new password
00747     PasswordDialog* pwdia = NULL;
00748     YAPET::Key* newkey;
00749     try {
00750     pwdia = new PasswordDialog(NEW_PW, file->getFilename());
00751     pwdia->run();
00752     newkey = pwdia->getKey();
00753     delete pwdia;
00754     } catch(YAPETUI::UIException&) {
00755     if (pwdia != NULL)
00756         delete pwdia;
00757 
00758     statusbar.putMsg("Error while asking for password");
00759     return;
00760     }
00761 
00762     // Make sure the key has been generated
00763     if (newkey == NULL) {
00764     statusbar.putMsg("Password change canceled");
00765     return;
00766     }
00767 
00768     // Change the password
00769     try {
00770     file->setNewKey(*key, *newkey);
00771     } catch (std::exception& ex) {
00772     delete newkey;
00773     YAPETUI::MessageBox* msgbox = NULL;
00774     try {
00775         msgbox = new YAPETUI::MessageBox("Error", ex.what());
00776         msgbox->run();
00777         delete msgbox;
00778     } catch (YAPETUI::UIException&) {
00779         if (msgbox != NULL)
00780         delete msgbox;
00781     }
00782     return;
00783     }
00784 
00785     delete key;
00786     key = newkey;
00787 
00788     // Read records from file
00789     try {
00790     std::list<YAPET::PartDec> tmp_list = file->read(*key);
00791     recordlist->setList(tmp_list);
00792     } catch(YAPET::YAPETException& e) {
00793         if (file != NULL)
00794         delete file;
00795 
00796         YAPETUI::MessageBox* msgbox = NULL;
00797         try {
00798         msgbox = new YAPETUI::MessageBox("Error", e.what());
00799         msgbox->run();
00800         delete msgbox;
00801         } catch (YAPETUI::UIException&) {
00802         if (msgbox != NULL)
00803             delete msgbox;
00804         statusbar.putMsg("Error while trying to show error");
00805         }
00806         delete key;
00807         key = NULL;
00808         file = NULL;
00809         statusbar.putMsg("Error reading from file");
00810         return;
00811     }
00812 
00813     statusbar.putMsg("Password successfully changed");
00814 }
00815 
00816 MainWindow::MainWindow() throw (YAPETUI::UIException) : BaseWindow(),
00817                             toprightwin (NULL),
00818                             bottomrightwin (NULL),
00819                             recordlist (NULL),
00820                             statusbar(),
00821                             records_changed(false),
00822                             key (NULL),
00823                             file (NULL) {
00824     createWindow();
00825 }
00826 
00827 MainWindow::~MainWindow() {
00828     delete recordlist;
00829     wclear(toprightwin);
00830     wclear(bottomrightwin);
00831     delwin (toprightwin);
00832     delwin (bottomrightwin);
00833     if (key != NULL)
00834     delete key;
00835     if (file != NULL)
00836     delete file;
00837 
00838 
00839 }
00840 
00841 void
00842 MainWindow::run() throw (YAPETUI::UIException) {
00843 
00844     if (file == NULL || key == NULL)
00845     statusbar.putMsg ("No file loaded");
00846 
00847     if (file != NULL && key != NULL)
00848     statusbar.putMsg(file->getFilename() + " loaded");
00849 
00850     refresh();
00851 
00852     Alarm alrm(*this);
00853     int ch;
00854     while(true) {
00855     try {
00856 #if defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
00857         BaseWindow::setTimeout(&alrm,600);
00858 #endif // defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
00859         while ( (ch=recordlist->focus()) ) {
00860 #if defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
00861         YAPETUI::BaseWindow::suspendTimeout();
00862 #endif // defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
00863         switch (ch) {
00864         case '\n':
00865             editSelectedRecord();
00866             break;
00867         case 3: // ^C
00868         case 'Q':
00869         case 'q':
00870             if (quit()) return;
00871             break;
00872 #ifdef HAVE_WRESIZE
00873         case KEY_RESIZE:
00874             YAPETUI::BaseWindow::resizeAll();
00875             break;
00876 #endif // HAVE_WRESIZE
00877         case KEY_REFRESH:
00878 #ifdef HAVE_WRESIZE
00879             YAPETUI::BaseWindow::resizeAll();
00880 #endif // HAVE_WRESIZE
00881             YAPETUI::BaseWindow::refreshAll();
00882             break;
00883         case 'S':
00884         case 's':
00885             saveFile();
00886             break;
00887         case 'R':
00888         case 'r': {
00889             FileOpen* tmp = NULL;
00890             try {
00891             tmp = new FileOpen("O P E N  F I L E");
00892             tmp->run();
00893             if (!tmp->isCanceled()) {
00894                 openFile(tmp->getFilepath());
00895             }
00896             } catch (std::exception& ex2) {
00897             statusbar.putMsg(ex2.what());
00898             if (file != NULL)
00899                 delete file;
00900             if (key != NULL)
00901                 delete key;
00902             file = NULL;
00903             key = NULL;
00904             }
00905             delete tmp;
00906 		    ::refresh();
00907             YAPETUI::BaseWindow::refreshAll();
00908         }
00909             break;
00910         case 'L':
00911         case 'l':
00912             lockScreen();
00913 		    ::refresh();
00914             YAPETUI::BaseWindow::refreshAll();
00915             break;
00916 
00917         case 'A':
00918         case 'a':
00919             addNewRecord();
00920             break;
00921 
00922         case 'D':
00923         case 'd':
00924             deleteSelectedRecord();
00925             break;
00926 
00927         case 'c':
00928         case 'C':
00929             changePassword();
00930 		    ::refresh();
00931             YAPETUI::BaseWindow::refreshAll();
00932             break;
00933         }
00934 #if defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
00935         YAPETUI::BaseWindow::setTimeout(&alrm,600);
00936 #endif // defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
00937         }
00938     } catch(std::exception& ex) {
00939         statusbar.putMsg(ex.what());
00940     }
00941     }
00942 }
00943 
00944 void
00945 MainWindow::run(std::string fn) {
00946     if (fn.empty()) {
00947     run();
00948     return;
00949     }
00950 
00951     refresh();
00952 
00953     try {
00954     openFile(fn);
00955     } catch (std::exception& ex2) {
00956     statusbar.putMsg(ex2.what());
00957     if (file != NULL)
00958         delete file;
00959     if (key != NULL)
00960         delete key;
00961     file = NULL;
00962     key = NULL;
00963     }
00964     ::refresh();
00965 
00966     run();
00967 }
00968 
00969 #if defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)
00970 void
00971 MainWindow::handle_signal(int signo) {
00972     if (signo == SIGALRM) {
00973     lockScreen();
00974 	::refresh();
00975     YAPETUI::BaseWindow::refreshAll();
00976     }
00977 }
00978 #endif // defined(HAVE_SIGACTION) && defined(HAVE_SIGNAL_H)

Generated on Wed Feb 27 16:15:42 2008 for YAPET by  doxygen 1.5.4