Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

zsafe.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Created: Sat Apr 6 17:57:45 2002
00004 **
00005 ** Author: Carsten Schneider <CarstenSchneider@t-online.de>
00006 **
00007 ** $Id: zsafe.cpp,v 1.28 2005/05/06 21:54:49 drw Exp $
00008 **
00009 ** Homepage: http://home.t-online.de/home/CarstenSchneider/zsafe/index.html
00010 **
00011 **
00012 ****************************************************************************/
00013 #include "zsafe.h"
00014 #include "newdialog.h"
00015 #include "searchdialog.h"
00016 #include "categorydialog.h"
00017 #include "passworddialog.h"
00018 #include "infoform.h"
00019 #include "zlistview.h"
00020 #include "shadedlistitem.h"
00021 
00022 #include <opie2/oresource.h>
00023 #include <opie2/ofiledialog.h>
00024 #include <opie2/odebug.h>
00025 using namespace Opie::Core;
00026 using namespace Opie::Ui;
00027 
00028 #include <qclipboard.h>
00029 
00030 #include <sys/types.h>
00031 #include <sys/stat.h>
00032 #include <fcntl.h>
00033 #include <stdlib.h>
00034 #include <unistd.h>
00035 #include <string.h>
00036 #include <errno.h>
00037 
00038 #include <qmenubar.h>
00039 #include <qpopupmenu.h>
00040 
00041 #include <qfile.h>
00042 #include <qpe/applnk.h>
00043 #include <qpe/global.h>
00044 #include <qpe/qpeapplication.h>
00045 #include <qpe/config.h>
00046 
00047 #include <qaction.h>
00048 #include <qtimer.h>
00049 #include <qlayout.h>
00050 #include <qmessagebox.h>
00051 #include <qfile.h>
00052 #include <qtextstream.h>
00053 #include <qheader.h>
00054 #include <qlistview.h>
00055 #include <qvariant.h>
00056 #include <qtooltip.h>
00057 #include <qwhatsthis.h>
00058 #include <qimage.h>
00059 #include <qpixmap.h>
00060 #include <qlineedit.h>
00061 #include <qmultilineedit.h>
00062 #include <qregexp.h>
00063 #include <qdir.h>
00064 #include <qtextbrowser.h>
00065 #include <qlabel.h>
00066 #include <qcombobox.h>
00067 #include <qtoolbar.h>
00068 
00069 #include "krc2.h"
00070 
00071 #include "wait.h"
00072 
00073 const QString APP_KEY = "";
00074 
00075 // include xmp images
00076 #include "pics/zsafe/expand.xpm"
00077 #include "pics/zsafe/export.xpm"
00078 #include "pics/zsafe/import.xpm"
00079 
00080 static const char* const bank_cards_data[] = {
00081 "14 14 16 1",
00082 ". c None",
00083 "# c #000000",
00084 "b c #0000de",
00085 "a c #0000e6",
00086 "j c #41de83",
00087 "k c #4acecd",
00088 "h c #4aced5",
00089 "g c #5a40cd",
00090 "d c #5a44d5",
00091 "l c #9440d5",
00092 "m c #b4ce4a",
00093 "n c #cd4883",
00094 "e c #d5ae10",
00095 "f c #de3ce6",
00096 "i c #e640e6",
00097 "c c #ffffff",
00098 "..............",
00099 ".###########..",
00100 ".#ababababa#..",
00101 ".#babbbabbb#..",
00102 ".#ccccccccc#..",
00103 ".#cdcefcghc#..",
00104 ".#ccccccccc#..",
00105 ".#cicjkclic#..",
00106 ".#ccccccccc#..",
00107 ".#cmchlcnec#..",
00108 ".#ccccccccc#..",
00109 ".###########..",
00110 "..............",
00111 ".............."};
00112 
00113 
00114 static const char* const passwords_data[] = {
00115 "16 16 20 1",
00116 ". c None",
00117 "# c #000000",
00118 "r c #000083",
00119 "p c #0000c5",
00120 "q c #0000ff",
00121 "n c #008100",
00122 "l c #00c200",
00123 "m c #00ff00",
00124 "j c #838100",
00125 "a c #c55900",
00126 "h c #c5c200",
00127 "o c #c5c2ff",
00128 "k c #c5ffc5",
00129 "f c #ff0000",
00130 "d c #ff8100",
00131 "b c #ffaa5a",
00132 "e c #ffc2c5",
00133 "c c #ffdeac",
00134 "i c #ffff00",
00135 "g c #ffffc5",
00136 "............###.",
00137 "...........#abb#",
00138 "..........#cbab#",
00139 ".........#cbdd#.",
00140 "######..#cbdd#..",
00141 "#eeff#..#add#...",
00142 "#eeff#######....",
00143 "#ccdbdd#........",
00144 "#dddbdd###......",
00145 "#gghihhjj#......",
00146 "#hhhihhjj###....",
00147 "#kklmllnnnn#....",
00148 "#lllmllnnnn#....",
00149 "#oopqpprprr#....",
00150 "#oopqpprprr#....",
00151 "############...."};
00152 
00153 static const char* const software_data[] = {
00154 "16 16 5 1",
00155 ". c None",
00156 "# c #000000",
00157 "b c #838183",
00158 "c c #c5ffff",
00159 "a c #ffffff",
00160 "................",
00161 ".##############.",
00162 "#aaaaaaaaaaaaaa#",
00163 "#abbbbbbbbbbbbb#",
00164 "#ab##########ab#",
00165 "#ab#c########ab#",
00166 "#ab#c#c######ab#",
00167 "#ab##########ab#",
00168 "#ab##########ab#",
00169 "#ab##########ab#",
00170 "#ab##########ab#",
00171 "#ab##########ab#",
00172 "#aaaaaaaaaaaaab#",
00173 "#bbbbbbbbbbbbbb#",
00174 ".##############.",
00175 "................"};
00176 
00177 static const char* const general_data[] = {
00178 "14 14 98 2",
00179 "Qt c None",
00180 ".k c #000000",
00181 "#x c #080808",
00182 "#F c #101008",
00183 "#q c #101010",
00184 "#i c #101410",
00185 "## c #101810",
00186 ".m c #181818",
00187 ".3 c #181c18",
00188 ".I c #182018",
00189 ".T c #202420",
00190 "#D c #202820",
00191 "#y c #292c29",
00192 ".c c #293029",
00193 ".d c #313031",
00194 "#E c #313429",
00195 "#r c #313831",
00196 ".j c #393c31",
00197 "#j c #394039",
00198 "#C c #414841",
00199 ".w c #4a554a",
00200 ".a c #4a594a",
00201 ".# c #525052",
00202 ".l c #52594a",
00203 "#f c #525952",
00204 "#v c #525d52",
00205 ".O c #5a4c4a",
00206 ".9 c #5a595a",
00207 ".A c #5a5d52",
00208 ".B c #624c52",
00209 ".0 c #625552",
00210 "#o c #626562",
00211 ".R c #626962",
00212 "#. c #626d5a",
00213 "#p c #626d62",
00214 ".2 c #627162",
00215 "#h c #6a6d62",
00216 "#z c #6a7562",
00217 "#w c #6a756a",
00218 ".C c #73656a",
00219 ".P c #73696a",
00220 "#a c #737d6a",
00221 ".U c #738573",
00222 ".E c #7b817b",
00223 "#B c #7b857b",
00224 "#s c #7b897b",
00225 "#n c #7b917b",
00226 ".b c #838d83",
00227 ".7 c #839583",
00228 ".n c #8b7d7b",
00229 "#g c #8b8583",
00230 ".g c #8b858b",
00231 ".r c #8b898b",
00232 ".s c #8b8d8b",
00233 ".i c #8b9183",
00234 ".8 c #8b918b",
00235 "#A c #8b9d8b",
00236 ".S c #8ba183",
00237 ".Z c #94918b",
00238 ".N c #949994",
00239 ".F c #949d94",
00240 ".x c #94a18b",
00241 ".v c #94a194",
00242 ".Y c #94aa94",
00243 ".h c #9c999c",
00244 ".Q c #9ca19c",
00245 "#u c #9ca59c",
00246 ".H c #9caa9c",
00247 "#e c #9cb29c",
00248 "#m c #a4b29c",
00249 "#t c #a4b2a4",
00250 ".M c #a4b69c",
00251 "#l c #a4b6a4",
00252 ".z c #a4baa4",
00253 ".f c #aca5ac",
00254 ".q c #acaaac",
00255 "#d c #acbeac",
00256 ".6 c #acc2ac",
00257 ".o c #b4b2b4",
00258 ".t c #b4beb4",
00259 "#k c #b4c2ac",
00260 ".5 c #b4cab4",
00261 ".D c #bdb6bd",
00262 ".G c #bdc6b4",
00263 "#c c #bdceb4",
00264 ".X c #bdd2bd",
00265 ".4 c #bdd6bd",
00266 ".1 c #c5bec5",
00267 ".e c #c5c2c5",
00268 ".u c #c5cac5",
00269 "#b c #c5d6c5",
00270 ".J c #c5dec5",
00271 ".p c #cdcacd",
00272 ".W c #cddecd",
00273 ".L c #cde2cd",
00274 ".K c #d5eacd",
00275 ".V c #d5ead5",
00276 ".y c #d5eed5",
00277 "QtQtQtQtQtQtQtQtQtQtQtQtQtQt",
00278 "QtQtQt.#.a.b.cQtQtQtQtQtQtQt",
00279 "QtQt.d.e.f.g.h.i.c.j.dQt.kQt",
00280 ".a.l.m.n.o.p.q.r.s.t.u.v.wQt",
00281 ".x.y.z.A.B.C.D.p.q.E.F.G.H.I",
00282 ".I.J.K.L.M.N.O.P.o.p.Q.R.S.T",
00283 "Qt.U.V.L.W.X.Y.Z.0.P.1.s.2.3",
00284 "Qt.3.X.W.4.X.5.6.7.8.9.s#.##",
00285 "QtQt#a.X#b#c.5.6#d#e#f#g#h#i",
00286 "QtQtQt#j.7#k.6#d#l#m#n#o#p#q",
00287 "QtQtQtQt.k#r#s#m#t.H#u#v#w#x",
00288 "QtQtQtQtQtQt.k#y#z.v#A#B#C#x",
00289 "QtQtQtQtQtQtQtQt.k#D.w#s#E.k",
00290 "QtQtQtQtQtQtQtQtQtQtQt#x#FQt"};
00291 
00292 // exit ZSafe and clear the clipboard for security reasons
00293   void ZSafe::exitZs (int ec)
00294   {
00295      QClipboard *cb = QApplication::clipboard();
00296      cb->clear();
00297 
00298      exit (ec);
00299   }
00300 
00301 
00302 // save the configuration into the file
00303   void ZSafe::saveConf ()
00304   {
00305     if (conf)
00306     {
00307        delete conf;
00308 
00309        conf = new Config ("zsafe");
00310        conf->setGroup ("zsafe");
00311     }
00312   }
00313 
00314 
00315 /*
00316  *  Constructs a ZSafe which is a child of 'parent', with the
00317  *  name 'name' and widget flags set to 'f'
00318  *
00319  *  The dialog will by default be modeless, unless you set 'modal' to
00320  *  TRUE to construct a modal dialog.
00321  */
00322 ZSafe::ZSafe( QWidget* parent,  const char* name, WFlags fl )
00323     : QMainWindow( parent, name, fl),
00324       ListView(0l)
00325 {
00326     IsCut = false;
00327     IsCopy = false;
00328     modified = false;
00329     showpwd = false;
00330 
00331     // set the config file
00332     cfgFile=QDir::homeDirPath();
00333     cfgFile += "/.zsafe.cfg";
00334     // set the icon path
00335 
00336     QString qpeDir = QPEApplication::qpeDir();
00337 
00338     conf = new Config ("zsafe");
00339     conf->setGroup ("zsafePrefs");
00340 
00341     expandTree = conf->readNumEntry(APP_KEY+"expandTree", 0);
00342     conf->setGroup ("zsafe");
00343 
00344     QPixmap new_img = Opie::Core::OResource::loadPixmap( "new", Opie::Core::OResource::SmallIcon );
00345     QPixmap edit_img = Opie::Core::OResource::loadPixmap( "edit", Opie::Core::OResource::SmallIcon );
00346     QPixmap trash_img = Opie::Core::OResource::loadPixmap( "trash", Opie::Core::OResource::SmallIcon );
00347     QPixmap copy_img = Opie::Core::OResource::loadPixmap( "copy", Opie::Core::OResource::SmallIcon );
00348     QPixmap cut_img = Opie::Core::OResource::loadPixmap( "cut", Opie::Core::OResource::SmallIcon );
00349     QPixmap editdelete_img = Opie::Core::OResource::loadPixmap( "editdelete", Opie::Core::OResource::SmallIcon );
00350     QPixmap folder_open_img = Opie::Core::OResource::loadPixmap( "folder_open", Opie::Core::OResource::SmallIcon );
00351     QPixmap help_icon_img = Opie::Core::OResource::loadPixmap( "help_icon", Opie::Core::OResource::SmallIcon );
00352     QPixmap paste_img = Opie::Core::OResource::loadPixmap( "paste", Opie::Core::OResource::SmallIcon );
00353     QPixmap save_img = Opie::Core::OResource::loadPixmap( "save", Opie::Core::OResource::SmallIcon );
00354     QPixmap expand_img((const char**) expand_xpm);
00355     QPixmap export_img((const char**) export_xpm);
00356     QPixmap import_img((const char**) import_xpm);
00357 
00358     QPixmap bank_cards( ( const char** ) bank_cards_data );
00359     QPixmap passwords( ( const char** ) passwords_data );
00360     QPixmap software( ( const char** ) software_data );
00361     QPixmap general( ( const char** ) general_data );
00362     QPixmap image0 = Opie::Core::OResource::loadPixmap( "zsafe/zsafe", Opie::Core::OResource::SmallIcon );
00363 
00364     if ( !name )
00365         setName( "ZSafe" );
00366 
00367    setCaption( tr( "ZSafe" ) );
00368    QString zsafeAppDirPath = QDir::homeDirPath() + "/Documents/application/zsafe";
00369    
00370    filename = conf->readEntry(APP_KEY+"document");
00371    if ( !QFileInfo(filename).exists() || !QDir(zsafeAppDirPath).exists() )
00372    {
00373     // check if the directory application exists, if not
00374     // create it
00375         QString d1(QDir::homeDirPath() + "/Documents/application");
00376     QDir pd1(d1);
00377     if (!pd1.exists())
00378     {
00379        QDir pd2(QDir::homeDirPath() + "/Documents");
00380            if (!pd2.exists()) {
00381                                 QDir pd3(QDir::homeDirPath());
00382                         if (!pd3.mkdir("Documents", FALSE)) {
00383                    }
00384            }
00385 
00386            if (!pd2.mkdir("application", FALSE))
00387        {
00388           QMessageBox::critical( 0, tr("ZSafe"),
00389                tr("<P>Can't create directory %1</P><P>ZSafe will now exit.</P>").arg(d1));
00390           exitZs (1);
00391        }
00392     }
00393         QString d2(QDir::homeDirPath() + "/Documents/application/zsafe");
00394     QDir pd2(d2);
00395     if (!pd2.exists())
00396     {
00397        if (!pd1.mkdir("zsafe", FALSE))
00398        {
00399           QMessageBox::critical( 0, tr("ZSafe"),
00400                tr("<P>Can't create directory %1</P><P>ZSafe will now exit.</P>").arg(d2));
00401           exitZs (1);
00402        }
00403    }
00404 
00405     filename = zsafeAppDirPath + "/passwords.zsf";
00406 
00407     // save the current filename to the config file
00408     conf->writeEntry(APP_KEY+"document", filename);
00409     saveConf();
00410          }
00411    //if (filename == "INVALIDPWD")
00412       //filename = "";
00413 
00414     QString ti = filename.right (filename.length() - filename.findRev ('/') - 1);
00415     this->setCaption(tr("ZSafe: ") + ti);
00416 
00417     selectedItem = NULL;
00418     lastSearchedCategory = NULL;
00419     lastSearchedItem = NULL;
00420     lastSearchedName = "";
00421     lastSearchedUsername = "";
00422     lastSearchedComment = "";
00423 
00424     infoForm = new InfoForm(this, "show_info", TRUE);
00425     categoryDialog = NULL;
00426     infoForm->setIcon( image0);
00427 
00428     // Create menu and tool bar dock
00429     setToolBarsMovable( false );
00430     QToolBar *dock = new QToolBar( this );
00431     dock->setHorizontalStretchable( true );
00432 
00433     // add a menu bar
00434     QMenuBar *menu = new QMenuBar( dock );
00435     menu->setMargin( 0 );
00436 
00437     // Add a toolbar
00438     QToolBar *toolbar = new QToolBar( this );
00439 
00440     // add file menu
00441     // QPopupMenu *file = new QPopupMenu( this );
00442     file = new QPopupMenu( this );
00443 
00444     // File menu
00445     file->insertItem( new_img, tr("New document"),  this, SLOT(newDocument()) );
00446     file->insertItem( folder_open_img, tr("Open document"),  this, SLOT(loadDocument()) );
00447     file->insertItem( save_img, tr("Save document as"),  this, SLOT(saveDocumentAs()) );
00448     file->insertSeparator();
00449 
00450     file->insertItem( save_img, tr("Save document"),  this, SLOT(saveDocumentWithoutPwd()) );
00451     file->insertItem( save_img, tr("Save document with new Password"),  this,
00452                       SLOT(saveDocumentWithPwd()) );
00453     file->insertSeparator();
00454     file->insertItem( export_img, tr("Export text file"),  this, SLOT(writeAllEntries()) );
00455     file->insertItem( import_img, tr("Import text file"),  this, SLOT(readAllEntries()) );
00456     file->insertItem( trash_img, tr("Remove text file"),  this, SLOT(removeAsciiFile()) );
00457     file->insertSeparator();
00458     file->insertItem( expand_img, tr("Open entries expanded"), this,
00459                       SLOT(setExpandFlag()), 0, 'o');
00460     file->setItemChecked('o', expandTree);
00461     menu->insertItem( tr("File"), file );
00462 
00463     // Category menu
00464     QPopupMenu *cat = new QPopupMenu( this );
00465     cat->insertItem( new_img, tr("New"),  this, SLOT(addCategory()) );
00466     cat->insertItem( edit_img, tr("Edit"),  this, SLOT(editCategory()) );
00467     cat->insertItem( trash_img, tr("Delete"),  this, SLOT(delCategory()) );
00468     menu->insertItem( tr("Category"), cat );
00469 
00470     // Entry menu
00471     QPopupMenu *it = new QPopupMenu( this );
00472     it->insertItem( cut_img, tr("Cut"),  this, SLOT(cutItem()) );
00473     it->insertItem( copy_img, tr("Copy"),  this, SLOT(copyItem()) );
00474     it->insertItem( paste_img, tr("Paste"),  this, SLOT(pasteItem()) );
00475     it->insertSeparator();
00476 
00477     QAction *a = new QAction( tr( "New" ), new_img, QString::null, 0, this, 0 );
00478     connect( a, SIGNAL(activated()), this, SLOT(newPwd()) );
00479     a->addTo( it );
00480     a->addTo( toolbar );
00481     a = new QAction( tr( "Edit" ), edit_img, QString::null, 0, this, 0 );
00482     connect( a, SIGNAL(activated()), this, SLOT(editPwd()) );
00483     a->addTo( it );
00484     a->addTo( toolbar );
00485     a = new QAction( tr( "Delete" ), trash_img, QString::null, 0, this, 0 );
00486     connect( a, SIGNAL(activated()), this, SLOT(deletePwd()) );
00487     a->addTo( it );
00488     a->addTo( toolbar );
00489     a = new QAction( tr( "Search" ), Opie::Core::OResource::loadPixmap( "find", Opie::Core::OResource::SmallIcon ),
00490                      QString::null, 0, this, 0 );
00491     connect( a, SIGNAL(activated()), this, SLOT(findPwd()) );
00492     a->addTo( it );
00493     a->addTo( toolbar );
00494     menu->insertItem( tr("Entry"), it );
00495 
00496     // Add main view
00497     ListView = new ZListView( this, "ListView" );
00498     ListView->addColumn( tr( "Name" ) );
00499     ListView->addColumn( tr( "Field 2" ) );
00500     ListView->addColumn( tr( "Field 3" ) );
00501     ListView->addColumn( tr( "Comment" ) );
00502     ListView->addColumn( tr( "Field 4" ) );
00503     ListView->addColumn( tr( "Field 5" ) );
00504     ListView->setAllColumnsShowFocus(TRUE);
00505 
00506     ListView->setResizePolicy(QScrollView::AutoOneFit);
00507     setCentralWidget( ListView );
00508 
00509     QBoxLayout * l = new QVBoxLayout( this );
00510     l->addWidget (menu);
00511     l->addWidget (ListView);
00512 
00513     // start a timer (100 ms) to load the default document
00514     docuTimer.start( 100, true );
00515     connect( &docuTimer, SIGNAL(timeout()), SLOT( slotLoadDocu() ) );
00516     raiseFlag = true;
00517     connect( &raiseTimer, SIGNAL(timeout()), SLOT( slotRaiseTimer() ) );
00518 
00519     // signals and slots connections for QListView
00520     connect( ListView, SIGNAL( selectionChanged(QListViewItem*) ),
00521              this, SLOT( listViewSelected(QListViewItem*) ) );
00522     connect( ListView, SIGNAL( doubleClicked(QListViewItem*) ),
00523              this, SLOT( showInfo(QListViewItem*) ) );
00524     connect( ListView, SIGNAL( returnPressed(QListViewItem*) ),
00525              this, SLOT( showInfo(QListViewItem*) ) );
00526 
00527     QPEApplication::setStylusOperation( ListView->viewport(),QPEApplication::RightOnHold);
00528     connect( ListView, SIGNAL( mouseButtonPressed( int, QListViewItem *, const QPoint&, int)),
00529            this,SLOT( ListPressed(int, QListViewItem *, const QPoint&, int)) );
00530 
00531            this->setIcon( image0);
00532 }
00533 
00534 const QColor *ZSafe::evenRowColor = &Qt::white;
00535 // const QColor *ZSafe::oddRowColor = &Qt::lightGray;
00536 const QColor *ZSafe::oddRowColor = new QColor(216,240,255);
00537 
00538 /*
00539  *  Destroys the object and frees any allocated resources
00540  */
00541 ZSafe::~ZSafe()
00542 {
00543     // no need to delete child widgets, Qt does it all for us
00544     quitMe();
00545 }
00546 
00547 // load the default document
00548 void ZSafe::slotLoadDocu()
00549 {
00550    openDocument (filename);
00551 }
00552 
00553 void ZSafe::deletePwd()
00554 {
00555 
00556     if (!selectedItem)
00557        return;
00558     if (!isCategory(selectedItem))
00559     {
00560        switch( QMessageBox::information( this, tr("ZSafe"),
00561                                       tr("Do you want to delete?"),
00562                                       tr("&Delete"), tr("D&on't Delete"),
00563                                       0      // Enter == button 0
00564                                       ) ) { // Escape == button 2
00565        case 0: // Delete clicked, Alt-S or Enter pressed.
00566           // Delete
00567           modified = true;
00568           selectedItem->parent()->takeItem(selectedItem);
00569           selectedItem = NULL;
00570           break;
00571        case 1: // Don't delete
00572           break;
00573        }
00574     }
00575     else
00576     {
00577        delCategory();
00578     }
00579 }
00580 
00581 void ZSafe::editPwd()
00582 {
00583      if (!selectedItem)
00584         return;
00585     if (!isCategory(selectedItem))
00586     {
00587        // open the 'New Entry' dialog
00588        NewDialog *dialog = new NewDialog(this, "edit_entry", TRUE);
00589 
00590        // set the labels
00591        dialog->Name->setText(getFieldLabel (selectedItem,     "1", tr("Name")));
00592        dialog->Username->setText(getFieldLabel (selectedItem, "2", tr("Username")));
00593        dialog->Password->setText(getFieldLabel (selectedItem, "3", tr("Password")));
00594        dialog->Comment->setText(getFieldLabel (selectedItem,  "4", tr("Comment")));
00595        dialog->Field5Label->setText(getFieldLabel (selectedItem,"5", tr("Field 4")));
00596        dialog->Field6Label->setText(getFieldLabel (selectedItem,"6", tr("Field 5")));
00597 
00598        // set the fields
00599        dialog->NameField->setText(selectedItem->text (0));
00600        dialog->UsernameField->setText(selectedItem->text (1));
00601        dialog->PasswordField->setText(selectedItem->text (2));
00602        QString comment = selectedItem->text (3);
00603        comment.replace (QRegExp("<br>"), "\n");
00604        dialog->Field5->setText(selectedItem->text (4));
00605        dialog->Field6->setText(selectedItem->text (5));
00606        dialog->CommentField->insertLine(comment);
00607        dialog->CommentField->setCursorPosition(0,0);
00608 
00609        QDialog::DialogCode result = (QDialog::DialogCode) QPEApplication::execDialog( dialog );
00610        if (result == QDialog::Accepted)
00611        {
00612           modified = true;
00613           // edit the selected item
00614           QString name = dialog->NameField->text();
00615           selectedItem->setText (0, tr (name));
00616           QString user = dialog->UsernameField->text();
00617           selectedItem->setText (1, tr (user));
00618           QString pwd = dialog->PasswordField->text();
00619           selectedItem->setText (2, tr (pwd));
00620           QString comment = dialog->CommentField->text();
00621           comment.replace (QRegExp("\n"), "<br>");
00622           selectedItem->setText (3, tr (comment));
00623           QString f5 = dialog->Field5->text();
00624           selectedItem->setText (4, tr (f5));
00625           QString f6 = dialog->Field6->text();
00626           selectedItem->setText (5, tr (f6));
00627        }
00628 
00629        delete dialog;
00630     }
00631     else
00632     {
00633        editCategory();
00634     }
00635 }
00636 
00637 void ZSafe::newPwd()
00638 {
00639      if (!selectedItem)
00640         return;
00641                 qWarning("new item");
00642     if (!isCategory(selectedItem))
00643          selectedItem = selectedItem->parent();
00644 
00645     if (isCategory(selectedItem))
00646     {
00647        QString cat = selectedItem->text(0);
00648                          qWarning(cat);
00649        // open the 'New Entry' dialog
00650        NewDialog *dialog = new NewDialog(this, "new_entry", TRUE);
00651        // set the labels
00652        dialog->Name->setText(getFieldLabel (selectedItem,     "1", tr("Name")));
00653        dialog->Username->setText(getFieldLabel (selectedItem, "2", tr("Username")));
00654        dialog->Password->setText(getFieldLabel (selectedItem, "3", tr("Password")));
00655        dialog->Comment->setText(getFieldLabel (selectedItem,  "4", tr("Comment")));
00656        dialog->Field5Label->setText(getFieldLabel (selectedItem,"5", tr("Field 4")));
00657        dialog->Field6Label->setText(getFieldLabel (selectedItem,"6", tr("Field 5")));
00658 retype:
00659 
00660 #ifdef Q_WS_QWS
00661        QDialog::DialogCode result = (QDialog::DialogCode) QPEApplication::execDialog( dialog );
00662 #endif
00663        if (result == QDialog::Accepted)
00664        {
00665 
00666           QString name = dialog->NameField->text();
00667           if (cat == name)
00668           {
00669              QMessageBox::critical( 0, tr("ZSafe"),
00670                 tr("Entry name must be different\nfrom the category name.") );
00671              goto retype; // it's not a good programming style :-)
00672           }
00673 
00674           modified = true;
00675           // add the new item
00676           QListViewItem *i = new ShadedListItem (0, selectedItem);
00677           i->setOpen (TRUE);
00678 
00679           i->setText (0, tr (name));
00680           QString user = dialog->UsernameField->text();
00681           i->setText (1, tr (user));
00682           QString pwd = dialog->PasswordField->text();
00683           i->setText (2, tr (pwd));
00684           QString comment = dialog->CommentField->text();
00685           comment.replace (QRegExp("\n"), "<br>");
00686           i->setText (3, tr (comment));
00687           QString f5 = dialog->Field5->text();
00688           i->setText (4, tr (f5));
00689           QString f6 = dialog->Field6->text();
00690           i->setText (5, tr (f6));
00691        }
00692 
00693        delete dialog;
00694     }
00695 }
00696 
00697 void ZSafe::findPwd()
00698 {
00699 
00700     // open the 'Search' dialog
00701     SearchDialog *dialog = new SearchDialog(this, tr("Search"), TRUE);
00702 
00703     if (lastSearchedName)
00704        dialog->NameField->setText(lastSearchedName);
00705     else
00706        dialog->NameField->setText("");
00707     if (lastSearchedUsername)
00708        dialog->UsernameField->setText(lastSearchedUsername);
00709     else
00710        dialog->UsernameField->setText("");
00711     if (lastSearchedComment)
00712        dialog->CommentField->setText(lastSearchedComment);
00713     else
00714        dialog->CommentField->setText("");
00715     QDialog::DialogCode result = (QDialog::DialogCode) dialog->exec();
00716 
00717     QString name;
00718     QString username;
00719     QString comment;
00720     if (result == QDialog::Accepted)
00721     {
00722        name = dialog->NameField->text();
00723        username = dialog->UsernameField->text();
00724        comment = dialog->CommentField->text();
00725     }
00726     else
00727     {
00728        delete dialog;
00729        return;
00730     }
00731 
00732     if (!name.isEmpty() && name != lastSearchedName ||
00733         lastSearchedName.isEmpty() && !name.isEmpty())
00734     {
00735        // set search at the beginning if a new name is given
00736        lastSearchedCategory = NULL;
00737        lastSearchedItem = NULL;
00738     }
00739     lastSearchedName = name;
00740     if (!username.isEmpty() && username != lastSearchedUsername ||
00741         lastSearchedUsername.isEmpty() && !username.isEmpty())
00742     {
00743        // set search at the beginning if a new name is given
00744        lastSearchedCategory = NULL;
00745        lastSearchedItem = NULL;
00746     }
00747     lastSearchedUsername = username;
00748     if (!comment.isEmpty() && comment != lastSearchedComment ||
00749         lastSearchedComment.isEmpty() && !comment.isEmpty())
00750     {
00751        // set search at the beginning if a new name is given
00752        lastSearchedCategory = NULL;
00753        lastSearchedItem = NULL;
00754     }
00755     lastSearchedComment = comment;
00756 
00757     ListView->clearSelection();
00758 
00759     bool found=FALSE;
00760     // step through all categories
00761     QListViewItem *i;
00762     if (lastSearchedCategory)
00763        i = lastSearchedCategory;
00764      else
00765        i = ListView->firstChild();
00766     for (;
00767          i != NULL;
00768          i = i->nextSibling())
00769     {
00770        i->setSelected(FALSE);
00771 
00772        // step through all subitems
00773        QListViewItem *si;
00774        if (lastSearchedItem)
00775           si = lastSearchedItem;
00776        else
00777           si = i->firstChild();
00778        // for (si = i->firstChild();
00779        for (;
00780             si != NULL;
00781             si = si->nextSibling())
00782        {
00783           if (si->isSelected())
00784              si->setSelected(FALSE);
00785           // ListView->repaintItem(si);
00786 
00787           bool n=TRUE;
00788           bool u=TRUE;
00789           bool c=TRUE;
00790           if (!name.isEmpty())
00791              n = (si->text(0)).contains (name, FALSE);
00792           if (!username.isEmpty())
00793              u = (si->text(1)).contains (username, FALSE);
00794           if (!comment.isEmpty())
00795              c = (si->text(3)).contains (comment, FALSE);
00796 
00797           if ((n && u && c ) && !found)
00798           {
00799              selectedItem = si;
00800              si->setSelected(TRUE);
00801              ListView->setCurrentItem(si);
00802              ListView->ensureItemVisible(si);
00803              ListView->triggerUpdate();
00804 
00805              lastSearchedCategory = i;
00806              // set to the next item
00807              lastSearchedItem = si->nextSibling();
00808              if (!lastSearchedItem)
00809              {
00810                 // no next item within category -> set next category
00811                 lastSearchedCategory = i->nextSibling();
00812                 if (!lastSearchedCategory)
00813                    lastSearchedItem = NULL; // END
00814              }
00815 
00816              found = TRUE;
00817              delete dialog;
00818              update();
00819              return;
00820           }
00821        }
00822        lastSearchedCategory = i->nextSibling();
00823        lastSearchedItem = NULL;
00824     }
00825     lastSearchedCategory = NULL;
00826     lastSearchedItem = NULL;
00827     delete dialog;
00828     update();
00829     QMessageBox::information( this, tr("ZSafe"),
00830                              tr("Entry not found"), tr("&OK"), 0);
00831 
00832 }
00833 
00834 QString ZSafe::getFieldLabel (QListViewItem *_item, QString field, QString def)
00835 {
00836    QString category;
00837    if (_item)
00838    {
00839       if (isCategory(_item))
00840       {
00841          category = _item->text(0);
00842       }
00843       else
00844       {
00845          QListViewItem *cat = _item->parent();
00846          category =  cat->text(0);
00847       }
00848    }
00849    else
00850    {
00851       return def;
00852    }
00853 
00854    QString app_key = APP_KEY;
00855 
00856    conf->setGroup( "fieldDefs" );
00857    QString label = conf->readEntry(app_key+category+"-field"+field,def);
00858    conf->setGroup ("zsafe");
00859    return label;
00860 }
00861 
00862 QString ZSafe::getFieldLabel (QString category, QString field, QString def)
00863 {
00864    QString app_key = APP_KEY;
00865 // #ifndef Q_WS_WIN
00866    conf->setGroup( "fieldDefs" );
00867    QString label = conf->readEntry(app_key+category+"-field"+field,
00868                                    def);
00869 // #else
00870    // QString label(def);
00871 // #endif
00872    conf->setGroup ("zsafe");
00873    return label;
00874 }
00875 
00876 void ZSafe::showInfo( QListViewItem *_item)
00877 {
00878    if (!_item)
00879       return;
00880    if (selectedItem != NULL)
00881       selectedItem->setSelected(FALSE);
00882 
00883    selectedItem = _item;
00884    selectedItem->setSelected(TRUE);
00885 
00886    if (!isCategory(_item))
00887    {
00888 /*
00889       QString label=selectedItem->text(0);
00890       label+="\n";
00891       label+=selectedItem->text(1);
00892       label+="\n";
00893       label+=selectedItem->text(2);
00894       label+="\n";
00895       label+=selectedItem->text(3);
00896 */
00897 
00898       QString text;
00899       QString entry;
00900 
00901       text = "<html><body><div align=""center""><u><b>";
00902       text += selectedItem->text(0);
00903       text += "</b></u><br></div><br>";
00904 
00905       entry = selectedItem->text(1);
00906       if (!entry.isEmpty() && entry.compare(" "))
00907       {
00908          text += "<u><b>";
00909          text += getFieldLabel (selectedItem, "2", tr("Username"));
00910          text += ":<br></b></u><blockquote>";
00911          text += entry;
00912          text += "</blockquote>";
00913          // text += "<br>";
00914       }
00915 
00916       entry = selectedItem->text(2);
00917       if (!entry.isEmpty() && entry.compare(" "))
00918       {
00919          text += "<u><b>";
00920          text += getFieldLabel (selectedItem, "3", tr("Password"));
00921          text += ":<br> </b></u><blockquote>";
00922          text += entry;
00923          text += "</blockquote>";
00924          // text += "<br>";
00925       }
00926 
00927       entry = selectedItem->text(4);
00928       if (!entry.isEmpty() && entry.compare(" "))
00929       {
00930          text += "<u><b>";
00931          text += getFieldLabel (selectedItem, "5", tr("Field 4"));
00932          text += ":<br> </b></u><blockquote>";
00933          text += entry;
00934          text += "</blockquote>";
00935          // text += "<br>";
00936       }
00937 
00938       entry = selectedItem->text(5);
00939       if (!entry.isEmpty() && entry.compare(" "))
00940       {
00941          text += "<u><b>";
00942          text += getFieldLabel (selectedItem, "6", tr("Field 5"));
00943          text += ":<br> </b></u><blockquote>";
00944          text += entry;
00945          text += "</blockquote>";
00946          // text += "<br>";
00947       }
00948 
00949       entry = selectedItem->text(3);
00950       if (!entry.isEmpty() && entry.compare(" "))
00951       {
00952          text += "<u><b>";
00953          text += getFieldLabel (selectedItem, "4", tr("Comment"));
00954          text += ":<br> </b></u>";
00955          QString comment = selectedItem->text(3);
00956          comment.replace (QRegExp("\n"), "<br>");
00957          text += comment;
00958          // text += "<br>";
00959       }
00960 
00961       text += "</body></html>";
00962 
00963       infoForm->InfoText->setText(text);
00964 //      infoForm->hide();
00965 #ifdef Q_WS_QWS
00966       QPEApplication::showDialog( infoForm );
00967 #endif
00968 
00969    }
00970 }
00971 
00972 void ZSafe::listViewSelected( QListViewItem *_item)
00973 {
00974    if (!_item)
00975       return;
00976    if (selectedItem != NULL)
00977       selectedItem->setSelected(FALSE);
00978 
00979    selectedItem = _item;
00980 
00981    // set the column text dependent on the selected item
00982    ListView->setColumnText(0, getFieldLabel (selectedItem, "1", tr("Name")));
00983    ListView->setColumnText(1, getFieldLabel (selectedItem, "2", tr("Field 2")));
00984    ListView->setColumnText(2, getFieldLabel (selectedItem, "3", tr("Field 3")));
00985    ListView->setColumnText(3, getFieldLabel (selectedItem, "4", tr("Comment")));
00986    ListView->setColumnText(4, getFieldLabel (selectedItem, "5", tr("Field 4")));
00987    ListView->setColumnText(5, getFieldLabel (selectedItem, "6", tr("Field 5")));
00988 }
00989 
00990 bool ZSafe::isCategory(QListViewItem *_item)
00991 {
00992    if (_item == NULL)
00993       return FALSE;
00994 
00995    QString categoryName = _item->text (0);
00996    if (categories.find (categoryName))
00997       return TRUE;
00998    else
00999       return FALSE;
01000 }
01001 
01002 void ZSafe::removeAsciiFile()
01003 {
01004     // QString fn = filename + ".txt";
01005   // open the file dialog
01006    QMap<QString, QStringList> mimeTypes;
01007    mimeTypes.insert(tr("All"), QStringList() );
01008    mimeTypes.insert(tr("Text"), "text/*" );
01009    QString fn = OFileDialog::getOpenFileName( OFileSelector::EXTENDED_ALL,
01010                        QDir::homeDirPath() + "/Documents/application/zsafe",
01011                        QString::null,
01012                        mimeTypes,
01013                        this,
01014                        tr ("Remove text file"));
01015 
01016   if (fn && fn.length() > 0 )
01017   {
01018     QFile f( fn );
01019     if ( !f.remove() )
01020     {
01021         QMessageBox::critical( 0, tr("ZSafe"),
01022                   tr("Could not remove text file.") );
01023         return;
01024     }
01025   }
01026 }
01027 
01028 void ZSafe::writeAllEntries()
01029 {
01030    if (filename.isEmpty())
01031    {
01032        QMessageBox::critical( 0, tr("ZSafe"),
01033          tr("No document defined.\nYou have to create a new document"));
01034        return;
01035    }
01036 
01037    // open the file dialog
01038          QString fn = zsaveDialog();
01039    // open the new document
01040    if (fn && fn.length() > 0 )
01041    {
01042     QFile f( fn );
01043     if ( !f.open( IO_WriteOnly ) ) {
01044         QMessageBox::critical( 0, "ZSafe",
01045                   QString("Could not export to text file.") );
01046         return;
01047     }
01048     QTextStream t( &f );
01049 
01050     QListViewItem *i;
01051     // step through all categories
01052     for (i = ListView->firstChild();
01053          i != NULL;
01054          i = i->nextSibling())
01055     {
01056        // step through all subitems
01057        QListViewItem *si;
01058        for (si = i->firstChild();
01059             si != NULL;
01060             si = si->nextSibling())
01061        {
01062           QString oneEntry;
01063           oneEntry += "\"";
01064           oneEntry += i->text(0);
01065           oneEntry += "\";";
01066           oneEntry += "\"";
01067           oneEntry += si->text(0);
01068           oneEntry += "\";";
01069           oneEntry += "\"";
01070           oneEntry += si->text(1);
01071           oneEntry += "\";";
01072           oneEntry += "\"";
01073           oneEntry += si->text(2);
01074           oneEntry += "\";";
01075           oneEntry += "\"";
01076           QString comment = si->text(3);
01077           comment.replace (QRegExp("\n"), "<br>");
01078           oneEntry += comment;
01079           oneEntry += "\";";
01080           oneEntry += "\"";
01081           oneEntry += si->text(4);
01082           oneEntry += "\";";
01083           oneEntry += "\"";
01084           oneEntry += si->text(5);
01085           oneEntry += "\"";
01086           // owarn << oneEntry << oendl;
01087           t << oneEntry << endl;
01088 
01089           // owarn << si->text(0) << oendl;
01090        }
01091     }
01092     f.close();
01093    }
01094 }
01095 
01096 void ZSafe::readAllEntries()
01097 {
01098    if (filename.isEmpty())
01099    {
01100        QMessageBox::critical( 0, tr("ZSafe"),
01101          tr("No document defined.\nYou have to create a new document"));
01102        return;
01103    }
01104 
01105   // open the file dialog
01106    QMap<QString, QStringList> mimeTypes;
01107    mimeTypes.insert(tr("All"), QStringList() );
01108    mimeTypes.insert(tr("Text"), "text/*" );
01109    QString fn = OFileDialog::getOpenFileName( OFileSelector::EXTENDED_ALL,
01110                        QDir::homeDirPath() + "/Documents/application/zsafe",
01111                        QString::null,
01112                        mimeTypes,
01113                        this,
01114                        tr ("Import text file"));
01115 
01116   if (fn && fn.length() > 0 )
01117   {
01118    QFile f( fn );
01119    if ( !f.open( IO_ReadOnly ) )
01120    {
01121       QMessageBox::critical( 0, "ZSafe",
01122                   QString("Could not import text file.") );
01123       return;
01124    }
01125 
01126    modified = true;
01127 
01128    // clear the password list
01129    selectedItem = NULL;
01130    QListViewItem *i;
01131    // step through all categories
01132    for (i = ListView->firstChild();
01133         i != NULL;
01134         i = i->nextSibling())
01135    {
01136       // step through all subitems
01137       QListViewItem *si;
01138       for (si = i->firstChild();
01139            si != NULL; )
01140            // si = si->nextSibling())
01141       {
01142           QListViewItem *_si = si;
01143           si = si->nextSibling();
01144           i->takeItem(_si); // remove from view list
01145           if (_si) delete _si;
01146       }
01147    }
01148 
01149    QTextStream t(&f);
01150    while ( !t.eof() )
01151    {
01152       QString s = t.readLine();
01153       s.replace (QRegExp("\";\""), "\"|\"");
01154       // char buffer[1024];
01155       char buffer[s.length()+1];
01156 
01157 
01158       /* modify QString -> QCString::utf8 */
01159 
01160       strcpy (buffer, s.utf8());
01161 
01162       QString name;
01163       QString user;
01164       QString password;
01165       QString comment;
01166       QString field5="";
01167       QString field6="";
01168 
01169       // separete the entries
01170       char *i = strtok (buffer, "|");
01171       QString category(QString::fromUtf8(&i[1]));
01172       category.truncate(category.length() -1);
01173 
01174       int idx=0;
01175       while ((i = strtok (NULL, "|")) != NULL)
01176       {
01177          switch (idx)
01178          {
01179             case 0:
01180                name = QString::fromUtf8(&i[1]);
01181                name.truncate(name.length() -1);
01182                // name
01183                break;
01184             case 1:
01185                // user
01186                user = QString::fromUtf8(&i[1]);
01187                user.truncate(user.length() -1);
01188                break;
01189             case 2:
01190                // password
01191                password = QString::fromUtf8(&i[1]);
01192                password.truncate(password.length() -1);
01193                break;
01194             case 3:
01195                // comment
01196                comment = QString::fromUtf8(&i[1]);
01197                comment.truncate(comment.length() -1);
01198                break;
01199             case 4:
01200                // field5
01201                field5 = QString::fromUtf8(&i[1]);
01202                field5.truncate(field5.length() -1);
01203                break;
01204             case 5:
01205                // field6
01206                field6 = QString::fromUtf8(&i[1]);
01207                field6.truncate(field6.length() -1);
01208                break;
01209          }
01210          idx++;
01211       }
01212 
01213       Category *cat= categories.find (category);
01214       if (cat)
01215       {
01216          // use the existend item
01217          QListViewItem *catItem = cat->getListItem();
01218          if (catItem)
01219          {
01220             QListViewItem * item = new ShadedListItem( 0, catItem );
01221             item->setText( 0, tr( name ) );
01222             item->setText( 1, tr( user ) );
01223             item->setText( 2, tr( password ) );
01224             item->setText( 3, tr( comment ) );
01225             item->setText( 4, tr( field5 ) );
01226             item->setText( 5, tr( field6 ) );
01227             catItem->setOpen( TRUE );
01228          }
01229       }
01230       else
01231       {
01232          QListViewItem *catI = new ShadedListItem( 1, ListView );
01233          // create and insert a new item
01234          QListViewItem * item = new ShadedListItem( 0, catI );
01235          item->setText( 0, tr( name ) );
01236          item->setText( 1, tr( user ) );
01237          item->setText( 2, tr( password ) );
01238          item->setText( 3, tr( comment ) );
01239          item->setText( 4, tr( field5 ) );
01240          item->setText( 5, tr( field6 ) );
01241 
01242          catI->setOpen( TRUE );
01243 
01244          Category *c1 = new Category();
01245          c1->setCategoryName(category);
01246 
01247          QString icon;
01248          QString fullIconPath;
01249          QPixmap *pix;
01250 // #ifndef Q_WS_WIN
01251          icon = conf->readEntry(APP_KEY+category);
01252 // #endif
01253          bool isIconAv = false;
01254          if (!icon.isEmpty() && !icon.isNull())
01255          {
01256             // build the full path
01257             fullIconPath = iconPath + icon;
01258             pix = new QPixmap (fullIconPath);
01259             if (!pix->isNull())
01260             {
01261                QImage img = pix->convertToImage();
01262                pix->convertFromImage(img.smoothScale(14,14));
01263                c1->setIconName (icon);
01264                c1->setIcon (*pix);
01265                isIconAv = true;
01266             }
01267          }
01268          if (!isIconAv)
01269          {
01270             c1->setIcon (*getPredefinedIcon(category));
01271          }
01272          c1->setListItem (catI);
01273          c1->initListItem();
01274          categories.insert (c1->getCategoryName(), c1);
01275       }
01276 
01277    }
01278    f.close();
01279 
01280   }
01281   else
01282   {
01283   }
01284 
01285 }
01286 
01287 #ifdef UNUSED
01288 void ZSafe::writeAllEntries()
01289 {
01290    if (filename.isEmpty())
01291    {
01292        QMessageBox::critical( 0, tr("ZSafe"),
01293          tr("<P>No document defined. You have to create a new document</P>"));
01294        return;
01295    }
01296 
01297     // open the file for writing
01298     QString fn = filename + ".txt";
01299     QFile f( fn );
01300     if ( !f.open( IO_WriteOnly ) ) {
01301         QMessageBox::critical( 0, tr("ZSafe"),
01302                   tr("Could not export to text file.") );
01303         return;
01304     }
01305     QTextStream t( &f );
01306 
01307     QListViewItem *i;
01308     // step through all categories
01309     for (i = ListView->firstChild();
01310          i != NULL;
01311          i = i->nextSibling())
01312     {
01313        // step through all subitems
01314        QListViewItem *si;
01315        for (si = i->firstChild();
01316             si != NULL;
01317             si = si->nextSibling())
01318        {
01319           QString oneEntry;
01320           oneEntry += "\"";
01321           oneEntry += i->text(0);
01322           oneEntry += "\";";
01323           oneEntry += "\"";
01324           oneEntry += si->text(0);
01325           oneEntry += "\";";
01326           oneEntry += "\"";
01327           oneEntry += si->text(1);
01328           oneEntry += "\";";
01329           oneEntry += "\"";
01330           oneEntry += si->text(2);
01331           oneEntry += "\";";
01332           oneEntry += "\"";
01333           // oneEntry += si->text(3);
01334           QString comment = si->text(3);
01335           comment.replace (QRegExp("\n"), "<br>");
01336           oneEntry += comment;
01337           oneEntry += "\"";
01338           // owarn << oneEntry << oendl;
01339           t << oneEntry << endl;
01340 
01341           // owarn << si->text(0) << oendl;
01342        }
01343     }
01344     f.close();
01345 }
01346 
01347 void ZSafe::readAllEntries()
01348 {
01349    QString fn = filename + ".txt";
01350    QFile f( fn );
01351    if ( !f.open( IO_ReadOnly ) )
01352    {
01353       QMessageBox::critical( 0, tr("ZSafe"),
01354                   tr("Could not import text file.") );
01355       return;
01356    }
01357 
01358    modified = true;
01359 
01360    // clear the password list
01361    selectedItem = NULL;
01362    QListViewItem *i;
01363    // step through all categories
01364    for (i = ListView->firstChild();
01365         i != NULL;
01366         i = i->nextSibling())
01367    {
01368       // step through all subitems
01369       QListViewItem *si;
01370       for (si = i->firstChild();
01371            si != NULL; )
01372            // si = si->nextSibling())
01373       {
01374           QListViewItem *_si = si;
01375           si = si->nextSibling();
01376           i->takeItem(_si); // remove from view list
01377           if (_si) delete _si;
01378       }
01379    }
01380 
01381    QTextStream t(&f);
01382    while ( !t.eof() )
01383    {
01384       QString s = t.readLine();
01385       s.replace (QRegExp("\";\""), "\"|\"");
01386       // char buffer[1024];
01387       int len=s.length()+1;
01388       char buffer[len];
01389       strcpy (buffer, s);
01390 
01391       QString name;
01392       QString user;
01393       QString password;
01394       QString comment;
01395 
01396       // separete the entries
01397       char *i = strtok (buffer, "|");
01398       QString category(&i[1]);
01399       category.truncate(category.length() -1);
01400 
01401       int idx=0;
01402       while (i = strtok (NULL, "|"))
01403       {
01404          switch (idx)
01405          {
01406             case 0:
01407                name = &i[1];
01408                name.truncate(name.length() -1);
01409                // name
01410                break;
01411             case 1:
01412                // user
01413                user = &i[1];
01414                user.truncate(user.length() -1);
01415                break;
01416             case 2:
01417                // password
01418                password = &i[1];
01419                password.truncate(password.length() -1);
01420                break;
01421             case 3:
01422                // comment
01423                comment = &i[1];
01424                comment.truncate(comment.length() -1);
01425                break;
01426          }
01427          idx++;
01428       }
01429 
01430       Category *cat= categories.find (category);
01431       if (cat)
01432       {
01433          // use the existend item
01434          QListViewItem *catItem = cat->getListItem();
01435          if (catItem)
01436          {
01437             QListViewItem * item = new ShadedListItem( 0, catItem );
01438             item->setText( 0, tr( name ) );
01439             item->setText( 1, tr( user ) );
01440             item->setText( 2, tr( password ) );
01441             item->setText( 3, tr( comment ) );
01442 
01443             catItem->setOpen( TRUE );
01444          }
01445       }
01446       else
01447       {
01448          QListViewItem *catI = new ShadedListItem( 1, ListView );
01449          // create and insert a new item
01450          QListViewItem * item = new ShadedListItem( 0, catI );
01451          item->setText( 0, tr( name ) );
01452          item->setText( 1, tr( user ) );
01453          item->setText( 2, tr( password ) );
01454          item->setText( 3, tr( comment ) );
01455 
01456          catI->setOpen( TRUE );
01457 
01458          Category *c1 = new Category();
01459          c1->setCategoryName(category);
01460 
01461          QString icon;
01462          QString fullIconPath;
01463          QPixmap *pix;
01464 // #ifndef Q_WS_WIN
01465          icon = conf->readEntry(APP_KEY+category);
01466 // #endif
01467          bool isIconAv = false;
01468          if (!icon.isEmpty() && !icon.isNull())
01469          {
01470             // build the full path
01471             fullIconPath = iconPath + icon;
01472             pix = new QPixmap (fullIconPath);
01473             if (!pix->isNull())
01474             {
01475                QImage img = pix->convertToImage();
01476                pix->convertFromImage(img.smoothScale(14,14));
01477                c1->setIconName (icon);
01478                c1->setIcon (*pix);
01479                isIconAv = true;
01480             }
01481          }
01482          if (!isIconAv)
01483          {
01484             c1->setIcon (*getPredefinedIcon(category));
01485          }
01486          c1->setListItem (catI);
01487          c1->initListItem();
01488          categories.insert (c1->getCategoryName(), c1);
01489       }
01490 
01491    }
01492    f.close();
01493 
01494 }
01495 #endif // UNUSED
01496 
01497 void ZSafe::resume(int)
01498 {
01499                  // hide the main window
01500 
01501    if ( !showpwd )
01502    {
01503    infoForm->hide();
01504    // open zsafe again
01505    m_password = "";
01506    selectedItem = NULL;
01507 
01508    // clear the password list
01509    QListViewItem *i;
01510    // step through all categories
01511    for (i = ListView->firstChild();
01512         i != NULL;
01513         i = i->nextSibling())
01514    {
01515       // step through all subitems
01516       QListViewItem *si;
01517       for (si = i->firstChild();
01518            si != NULL; )
01519       {
01520           QListViewItem *_si = si;
01521           si = si->nextSibling();
01522           i->takeItem(_si); // remove from view list
01523           if (_si) delete _si;
01524       }
01525    }
01526 
01527    // ask for password and read again
01528    openDocument(filename);
01529          }
01530 }
01531 
01532 //---------------------------------------------
01533 
01534 
01535 bool ZSafe::openDocument(const char* _filename, const char* )
01536 {
01537         QString name= _filename;
01538         qWarning("openDocument "+name);
01539      int retval;
01540      char* entry[FIELD_SIZE];
01541 // #ifndef Q_WS_WIN
01542      int validationFlag = conf->readNumEntry(APP_KEY+"valzsafe", 1);
01543 // #else
01544      // int validationFlag = 1;
01545 // #endif
01546 
01547      int pwdOk = 0;
01548      int numberOfTries = 3;
01549      for (int i=0; i < numberOfTries; i++)
01550      {
01551         QFile f(_filename);
01552         if (f.exists())
01553         {
01554            // ask with a dialog for the password
01555        if (m_password.isEmpty())
01556           getDocPassword(tr("Enter Password"));
01557        if (m_password.isEmpty() && validationFlag == 0)
01558            {
01559               QMessageBox::critical( 0, tr("ZSafe"),
01560                   tr("Wrong password.\n\nZSafe will now exit.") );
01561               exitZs (1);
01562            }
01563 
01564        retval = loadInit(_filename, m_password);
01565        if (retval != PWERR_GOOD)
01566            {
01567                                                                 return false;
01568        }
01569         }
01570         else
01571         {
01572            this->setCaption(tr("ZSafe"));
01573            filename = "";
01574            switch( QMessageBox::warning( this, tr("ZSafe"),
01575                     tr("<P>You must create a new document first. Ok to create?</P>"),
01576                     tr("&Yes"), tr("&No."),
01577                     0
01578                     ) )
01579            {
01580            case 1: // No
01581            return false;
01582                  break;
01583            case 0:  // Yes
01584                                                          newDocument();
01585                                          return false;
01586            break;
01587            }
01588 
01589         }
01590 
01591 
01592         // load the validation entry
01593         if (validationFlag == 0)
01594         {
01595             pwdOk = 1;
01596             break;
01597         }
01598 
01599     retval = loadEntry(entry);
01600         if (retval == 1 &&
01601             !strcmp (entry[0], "ZSAFECATEGORY") &&
01602             !strcmp (entry[1], "name") &&
01603             !strcmp (entry[2], "username") &&
01604             !strcmp (entry[3], "password") &&
01605             !strcmp (entry[4], "comment") )
01606         {
01607         for (int count = 0; count < FIELD_SIZE; count++) free(entry[count]);
01608             pwdOk = 1;
01609             break;
01610         }
01611         else
01612        // for (int count = 0; count < FIELD_SIZE; count++) free(entry[count]);
01613     fclose (fd);
01614         m_password = "";
01615 
01616         if (i < numberOfTries - 1)
01617         {
01618            switch( QMessageBox::warning( this, tr("ZSafe"),
01619                     tr("Wrong password.\nEnter again?"),
01620                     tr("&Yes"), tr("&No."),
01621                     0
01622                     ) )
01623            {
01624            case 1: // No
01625                  exitZs (1);
01626                  break;
01627            case 0:  // Yes
01628                  continue;
01629            }
01630         }
01631      }
01632      if (pwdOk == 0)
01633      {
01634         // unset the document entry
01635         conf->writeEntry(APP_KEY+"document", "INVALIDPWD");
01636         if (conf)
01637            delete conf;
01638 
01639         exitZs (1);
01640      }
01641 
01642 
01643     retval = loadEntry(entry);
01644 
01645         int numberOfEntries=0;
01646     while (retval == 1) {
01647 
01648                 QString category( QString::fromUtf8(entry[0]) );
01649                 QString name( QString::fromUtf8(entry[1]) );
01650                 QString user( QString::fromUtf8(entry[2]) );
01651                 QString password( QString::fromUtf8(entry[3]) );
01652                 QString comment( QString::fromUtf8(entry[4]) );
01653                 QString field5( QString::fromUtf8(entry[5]) );
01654                 QString field6( QString::fromUtf8(entry[6]) );
01655                 // add the subitems to the categories
01656 
01657                 Category *cat= categories.find (category);
01658                 if (cat)
01659                 {
01660                    // use the existend item
01661                    QListViewItem *catItem = cat->getListItem();
01662                    if (catItem)
01663                    {
01664                       QListViewItem * item = new ShadedListItem( 0, catItem );
01665                       item->setText( 0, tr( name ) );
01666                       item->setText( 1, tr( user ) );
01667                       item->setText( 2, tr( password ) );
01668                       item->setText( 3, tr( comment ) );
01669                       item->setText( 4, tr( field5 ) );
01670                       item->setText( 5, tr( field6 ) );
01671 
01672                       if (expandTree)
01673                          catItem->setOpen( TRUE );
01674                       numberOfEntries++;
01675                    }
01676                 }
01677                 else
01678                 {
01679                    QListViewItem *catI = new ShadedListItem( 1, ListView );
01680                    // create and insert a new item
01681                    QListViewItem * item = new ShadedListItem( 0, catI );
01682 
01683                    item->setText( 0, tr( name ) );
01684                    item->setText( 1, tr( user ) );
01685                    item->setText( 2, tr( password ) );
01686                    item->setText( 3, tr( comment ) );
01687                    item->setText( 4, tr( field5 ) );
01688                    item->setText( 5, tr( field6 ) );
01689 
01690                    if (expandTree)
01691                       catI->setOpen( TRUE );
01692 
01693                    Category *c1 = new Category();
01694                    c1->setCategoryName(category);
01695 
01696                    QString icon;
01697                    QString fullIconPath;
01698                    QPixmap *pix;
01699 // #ifndef Q_WS_WIN
01700                    icon = conf->readEntry(APP_KEY+category);
01701 // #endif
01702                    bool isIconAv = false;
01703                    if (!icon.isEmpty() && !icon.isNull())
01704                    {
01705                       // build the full path
01706                       fullIconPath = iconPath + icon;
01707                       pix = new QPixmap (fullIconPath);
01708                       if (!pix->isNull())
01709                       {
01710                          QImage img = pix->convertToImage();
01711                          pix->convertFromImage(img.smoothScale(14,14));
01712                          c1->setIconName (icon);
01713                          c1->setIcon (*pix);
01714                          isIconAv = true;
01715                       }
01716                    }
01717                    if (!isIconAv)
01718                    {
01719                       c1->setIcon (*getPredefinedIcon(category));
01720                    }
01721 
01722                    c1->setListItem (catI);
01723                    c1->initListItem();
01724                    categories.insert (c1->getCategoryName(), c1);
01725                    numberOfEntries++;
01726                 }
01727 
01728         for (int count = 0; count < FIELD_SIZE; count++) {
01729             free(entry[count]);
01730         }
01731         retval = loadEntry(entry);
01732         if (retval == 2) {
01733             // m_parent->slotStatusHelpMsg("Last entry loaded");
01734         }
01735     } // end while
01736 
01737         if (numberOfEntries == 0)
01738         {
01739 
01740            switch( QMessageBox::warning( this, tr("ZSafe"),
01741                  tr("Empty document or\nwrong password.\nContinue?"),
01742                  tr("&No"), tr("&Yes."),
01743                  0
01744                  ) ) {
01745            case 0: // No
01746           retval = loadFinalize();
01747               exitZs (1);
01748               break;
01749            case 1:  // Yes
01750               break;
01751            }
01752     }
01753 
01754     retval = loadFinalize();
01755 
01756     return true;
01757 }
01758 
01759 int ZSafe::loadInit(const char* _filename, const char *password)
01760 {
01761     unsigned int j = 0;
01762     unsigned int keylength=0;
01763     int count=0, count2=0, count3=0;
01764     unsigned char charbuf[8];
01765     unsigned short ciphertext[4];
01766     char key[128];
01767     Krc2* krc2 = new Krc2();
01768 
01769     fd = fopen (_filename, "rb");
01770 
01771         QFileInfo f (_filename);
01772         load_buffer_length = f.size();
01773         load_buffer_length = ((load_buffer_length / 1024)+1) * 1024 * 2;
01774 
01775     if (fd == NULL)
01776         return PWERR_OPEN;
01777 
01778     buffer = (char *)malloc(load_buffer_length);
01779     for (j = 0; password[j] != '\0'; j++) {
01780         key[j] = password[j];
01781     }
01782     keylength = j;
01783     krc2->rc2_expandkey (key, keylength, 128);
01784 
01785 #ifndef Q_WS_WIN
01786     size = read(fileno (fd), (unsigned char *) (charbuf + count), 8);
01787 #else
01788     size = fread ((unsigned char *) (charbuf + count), sizeof(unsigned char), 8, fd);
01789 #endif
01790 
01791     if (size < 8)
01792         return PWERR_DATA;
01793 
01794     for (count = 0; count < 4; count++) {
01795         count2 = count << 1;
01796         iv[count] = charbuf[count2] << 8;
01797         iv[count] += charbuf[count2 + 1];
01798     }
01799 
01800     size = 0;
01801     bufferIndex = 0;
01802 #ifndef Q_WS_WIN
01803     while ((count = read (fileno (fd), (unsigned char *) charbuf, 8)) > 0) {
01804         while (count < 8) {
01805             count2 = read (fileno (fd), (unsigned char *) (charbuf + count), 8);
01806 #else
01807     while ((count = fread ((unsigned char *) (charbuf), sizeof(unsigned char), 8, fd)) > 0) {
01808         while (count < 8) {
01809             count2 = fread ((unsigned char *) (charbuf + count), sizeof(unsigned char), 8, fd);
01810 #endif
01811             if (count2 == 0) {
01812                 return PWERR_DATA;
01813             }
01814             count += count2;
01815         } /* while (count < 8) */
01816 
01817         size += 8;
01818         for (count2 = 0; count2 < 8; count2 += 2) {
01819             count3 = count2 >> 1;
01820             ciphertext[count3] = charbuf[count2] << 8;
01821             ciphertext[count3] += charbuf[count2 + 1];
01822 
01823             plaintext[count3] = ciphertext[count3] ^ iv[count3];
01824             iv[count3] = plaintext[count3];
01825         } /* for (count2) */
01826 
01827         krc2->rc2_decrypt (plaintext);
01828         memcpy ((unsigned char *) (buffer + bufferIndex), plaintext, 8);
01829         bufferIndex += 8;
01830         buffer[bufferIndex + 1] = '\0';
01831     } /* while ((count = read (fileno (fd), (unsigned char *) charbuf, 8)) > 0) */
01832     size -= buffer[size - 1];
01833     lastcount = 0;
01834 
01835     /* This will point to the starting index */
01836     bufferIndex = 0;
01837     return PWERR_GOOD;
01838 }
01839 
01840 int ZSafe::loadEntry(char *entry[FIELD_SIZE])
01841 {
01842     /* Strip off PKCS 5 padding
01843      * Should check to make sure it's good here
01844      */
01845     int count, count1=0;
01846 
01847     for (count = lastcount; count < size; count++) {
01848         if ((unsigned char) (buffer[count]) == 255) {
01849             if (buffer[bufferIndex] == '\0') {
01850                 bufferIndex++;
01851             }
01852             entry[count1] = (char *) malloc (count - bufferIndex + 1);
01853             memcpy (entry[count1], (unsigned char *) (buffer + bufferIndex), count - bufferIndex);
01854             entry[count1][count - bufferIndex] = '\0';
01855             count++;
01856             bufferIndex = count;
01857             count1++;
01858             if (count1 == FIELD_SIZE) {
01859                 lastcount = count;
01860                 return 1;
01861             }
01862         } /* if ((unsigned char) (buffer[count]) == 255) */
01863     } /* for (count = 0; count < size; count++) */
01864 
01865     return 2;
01866 }
01867 
01868 int ZSafe::loadFinalize(void)
01869 {
01870     fclose (fd);
01871         if (buffer) free(buffer);
01872     return PWERR_GOOD;
01873 }
01874 
01875 bool ZSafe::saveDocument(const char* _filename,
01876                          bool withPwd,
01877                          const char* )
01878 {
01879     if (filename.isEmpty())
01880     {
01881        QMessageBox::critical( 0, tr("ZSafe"),
01882          tr("No document defined.\nYou have to create a new document"));
01883        return false;
01884     }
01885 
01886      // if (m_password.isEmpty())
01887         // withPwd = true; // the document must be saved with a valid password
01888      if (withPwd)
01889      {
01890         bool pwdOk = FALSE;
01891         while (!pwdOk)
01892         {
01893        getDocPassword(tr("Enter Password"));
01894        if (m_password.isEmpty())
01895            {
01896 
01897               QMessageBox::critical( 0, tr("ZSafe"),
01898                       tr("Password is empty.\nPlease enter again."));
01899               continue;
01900            }
01901 
01902            QString firstPasswd = m_password;
01903 
01904        getDocPassword(tr("Reenter Password"));
01905        if (m_password.isEmpty())
01906            {
01907               QMessageBox::critical( 0, tr("ZSafe"),
01908                       tr("Password is empty.\nPlease enter again."));
01909               continue;
01910            }
01911            if (firstPasswd != m_password)
01912            {
01913 
01914               QMessageBox::critical( 0, tr("ZSafe"),
01915                       tr("Passwords must be identical.\nPlease enter again."));
01916               continue;
01917            }
01918            pwdOk = TRUE;
01919            modified = false;
01920         }
01921      }
01922      else if (modified)
01923      {
01924       QString fns(_filename);
01925       fns = fns.right (fns.length() - fns.findRev ('/') - 1);
01926       switch( QMessageBox::information( this, tr("ZSafe"),
01927           tr("Do you want to save ") + fns + tr("\nbefore continuing?"),
01928           tr("&Save"),
01929           tr("&Don't Save"),
01930           0      // Enter == button 0
01931           ) )
01932       { // Escape == button 2
01933       case 0: // Save clicked, Alt-S or Enter pressed.
01934         modified = false;
01935         break;
01936       case 1: // Don't Save clicked or Alt-D pressed
01937         modified = false;
01938         return true;
01939       }
01940      }
01941      modified = false;
01942 
01943      if (m_password.isEmpty())
01944         return false;
01945 
01946      int retval = saveInit(_filename, m_password);
01947     // int retval = saveInit(_filename, "test");
01948     if (retval != PWERR_GOOD) {
01949         return false;
01950      }
01951 
01952     char* entry[FIELD_SIZE];
01953 
01954         // save the validation entry
01955         {
01956            int i=0;
01957        entry[i] = (char*)malloc(strlen("ZSAFECATEGORY")+1);
01958        strcpy(entry[i++], "ZSAFECATEGORY");
01959        entry[i] = (char*)malloc(strlen("name")+1);
01960        strcpy(entry[i++], "name");
01961        entry[i] = (char*)malloc(strlen("username")+1);
01962        strcpy(entry[i++], "username");
01963        entry[i] = (char*)malloc(strlen("password")+1);
01964        strcpy(entry[i++], "password");
01965        entry[i] = (char*)malloc(strlen("comment")+1);
01966        strcpy(entry[i++], "comment");
01967 
01968        entry[i] = (char*)malloc(strlen("field5")+1);
01969        strcpy(entry[i++], "field5");
01970        entry[i] = (char*)malloc(strlen("field6")+1);
01971        strcpy(entry[i++], "field6");
01972 
01973        retval = saveEntry(entry);
01974            for (int z=0; z<i; z++) free(entry[z]);
01975            if (retval == PWERR_DATA) {
01976 #ifndef NO_OPIE
01977               owarn << "1: Error writing file, contents not saved" << oendl;
01978 #else
01979               qWarning("1: Error writing file, contents not saved");
01980 #endif
01981               saveFinalize();
01982               return false;
01983            }
01984 // #ifndef Q_WS_WIN
01985            conf->writeEntry(APP_KEY+"valzsafe", 1);
01986 // #endif
01987            saveConf();
01988         }
01989 
01990         QListViewItem *i;
01991         // step through all categories
01992         for (i = ListView->firstChild();
01993              i != NULL;
01994              i = i->nextSibling())
01995         {
01996            // step through all subitems
01997            QListViewItem *si;
01998            for (si = i->firstChild();
01999                 si != NULL;
02000                 si = si->nextSibling())
02001            {
02002               int j=0;
02003          entry[j] = (char*)malloc(strlen(i->text(0).utf8())+1);
02004          strcpy(entry[j++], i->text(0).utf8());
02005          entry[j] = (char*)malloc(strlen(si->text(0).utf8())+1);
02006          strcpy(entry[j++], si->text(0).utf8());
02007          entry[j] = (char*)malloc(strlen(si->text(1).utf8())+1);
02008          strcpy(entry[j++], si->text(1).utf8());
02009          entry[j] = (char*)malloc(strlen(si->text(2).utf8())+1);
02010          strcpy(entry[j++], si->text(2).utf8());
02011          entry[j] = (char*)malloc(strlen(si->text(3).utf8())+1);
02012          strcpy(entry[j++], si->text(3).utf8());
02013          entry[j] = (char*)malloc(strlen(si->text(4).utf8())+1);
02014          strcpy(entry[j++], si->text(4).utf8());
02015          entry[j] = (char*)malloc(strlen(si->text(5).utf8())+1);
02016          strcpy(entry[j++], si->text(5).utf8());
02017 
02018           retval = saveEntry(entry);
02019               for (int z=0; z<j; z++)
02020               {
02021                   free(entry[z]);
02022               }
02023               if (retval == PWERR_DATA) {
02024 #ifndef NO_OPIE
02025                         owarn << "1: Error writing file, contents not saved" << oendl;
02026 #else
02027                         qWarning("1: Error writing file, contents not saved");
02028 #endif
02029                         saveFinalize();
02030                         return false;
02031               }
02032 
02033            }
02034         }
02035 
02036     if (saveFinalize() == PWERR_DATA) {
02037 #ifndef NO_OPIE
02038                 owarn << "2: Error writing file, contents not saved" << oendl;
02039 #else
02040                 qWarning("2: Error writing file, contents not saved");
02041 #endif
02042                                                                 return false;
02043     } else {
02044 #ifndef DESKTOP
02045                 Global::statusMessage (tr("Password file saved."));
02046 #endif
02047                 modified = false;
02048         return true;
02049     }
02050 }
02051 
02052 PasswordForm *newPwdDialog;
02053 bool newPwdDialogResult = false;
02054 void ZSafe::setPasswordDialogDone()
02055 {
02056    newPwdDialogResult = true;
02057    newPwdDialog->close();
02058 }
02059 
02060 void ZSafe::getDocPassword(QString title)
02061 {
02062                         // open the 'Password' dialog
02063     PasswordForm *dialog = new PasswordForm(this, title, TRUE);
02064     newPwdDialog = dialog;
02065     newPwdDialogResult = false;
02066 
02067 //    QPixmap image0 = Opie::Core::OResource::loadPixmap( "zsafe/zsafe", Opie::Core::OResource::SmallIcon );
02068 //              dialog->setIcon( image0);
02069 
02070     connect( dialog->PasswordField, SIGNAL( returnPressed() ),
02071              this, SLOT( setPasswordDialogDone() ) );
02072 
02073     // CS: !!!
02074     // int pos = filename.findRev ('/');
02075     QString ti = filename.right (filename.length() - filename.findRev ('/') - 1);
02076     dialog->setCaption(ti);
02077     // dialog->setCaption(title);
02078 
02079     dialog->PasswordField->setFocus();
02080     QDialog::DialogCode result = (QDialog::DialogCode) dialog->exec();
02081 
02082     QString password;
02083     if (result == QDialog::Accepted || newPwdDialogResult)
02084     {
02085        m_password = dialog->PasswordField->text();
02086     }
02087     else
02088     {
02089        exitZs (1);
02090     }
02091 }
02092 
02093 int ZSafe::saveInit(const char *_filename, const char *password)
02094 {
02095     char key[128];
02096     unsigned int j = 0;
02097     unsigned int keylength;
02098     // int val;
02099     int count2;
02100     Krc2* krc2 = new Krc2();
02101 
02102     /* first we should check the permissions of the filename */
02103 /*
02104     if (QFile::exists(_filename)) {
02105         val = checkFile(_filename);
02106         if (val != PWERR_GOOD)
02107             return val;
02108     } else
02109         {
02110         val = creat (_filename, (S_IRUSR | S_IWUSR));
02111         if (val == -1)
02112             return PWERR_OPEN;
02113         else
02114             close(val);
02115     }
02116 */
02117         QFileInfo f (_filename);
02118         save_buffer_length = f.size();
02119         save_buffer_length = ((save_buffer_length / 1024)+1) * 1024;
02120 
02121     fd = fopen (_filename, "wb");
02122     if (fd == NULL)
02123         return PWERR_OPEN;
02124 
02125     buffer = (char*)malloc(save_buffer_length);
02126 
02127     /* make the key ready */
02128     for (j = 0; password[j] != '\0'; j++) {
02129         key[j] = password[j];
02130     }
02131     keylength = j;
02132     krc2->rc2_expandkey (key, keylength, 128);
02133 
02134     /* First, we make the IV */
02135     for (count2 = 0; count2 < 4; count2++) {
02136         iv[count2] = rand ();
02137         putc ((unsigned char) (iv[count2] >> 8), fd);
02138         putc ((unsigned char) (iv[count2] & 0xff), fd);
02139     }
02140 
02141     bufferIndex = 0;
02142     return PWERR_GOOD;
02143 }
02144 
02145 
02146 int ZSafe::saveEntry(char *entry[FIELD_SIZE])
02147 {
02148     char *text1;
02149     int count2, count3;
02150     unsigned short ciphertext[4];
02151     Krc2* krc2 = new Krc2();
02152 
02153     buffer = (char*)memset(buffer, '\0', save_buffer_length);
02154 
02155     for (count2 = 0; count2 < FIELD_SIZE; count2++) {
02156         text1 = entry[count2];
02157         if (strlen (text1) == 0) {
02158             strncat(buffer, " ", strlen(" "));
02159         } else {
02160             strncat(buffer, text1, strlen(text1));
02161         }
02162         /* Use 255 as the marker.  \n is too tough to test for */
02163         buffer[strlen (buffer)] = 255;
02164     } /*for (count2 = 0; count2 < 5; count2++)*/
02165     count2 = 0;
02166     /* I'm using CBC mode and encrypting the data straight from top down.
02167      * At the bottom, encrypted, I will append an MD5 hash of the file, eventually.
02168      * PKCS 5 padding (explained at the code section
02169      */
02170     while (count2 < (int)strlen (buffer)) {
02171 #ifndef WORDS_BIGENDIAN
02172         plaintext[bufferIndex] = buffer[count2 + 1] << 8;
02173         plaintext[bufferIndex] += buffer[count2] & 0xff;
02174 #endif
02175 #ifdef WORDS_BIGENDIAN
02176         plaintext[bufferIndex] = buffer[count2] << 8;
02177         plaintext[bufferIndex] += buffer[count2 + 1] & 0xff;
02178 #endif
02179         bufferIndex++;
02180         if (bufferIndex == 4) {
02181             krc2->rc2_encrypt (plaintext);
02182 
02183             for (count3 = 0; count3 < 4; count3++) {
02184                 ciphertext[count3] = iv[count3] ^ plaintext[count3];
02185 
02186                 /* Now store the ciphertext as the iv */
02187                 iv[count3] = plaintext[count3];
02188 
02189                 /* reset the buffer index */
02190                 bufferIndex = 0;
02191                 if (putc ((unsigned char) (ciphertext[count3] >> 8), fd) == EOF) return PWERR_DATA;
02192                 if (putc ((unsigned char) (ciphertext[count3] & 0xff), fd) == EOF) return PWERR_DATA;
02193             } /*for (count3 = 0; count3 < 5; count3++)*/
02194         } /*if (bufferIndex == 5)*/
02195         /* increment a short, not a byte */
02196         count2 += 2;
02197     } /*while (count2 < strlen (buffer))*/
02198         int ret = PWERR_GOOD;
02199     return ret;
02200 }
02201 
02202 int ZSafe::saveFinalize(void)
02203 {
02204     int count1, retval = PWERR_GOOD;
02205     unsigned short ciphertext[4];
02206     Krc2* krc2 = new Krc2();
02207 
02208     /* Tack on the PKCS 5 padding
02209      * How it works is we fill up the last n bytes with the value n
02210      *
02211      * So, if we have, say, 13 bytes, 8 of which are used, we have 5 left
02212      * over, leaving us 3 short, so we fill it in with 3's.
02213      *
02214      * If we come out even, we fill it with 8 8s
02215      *
02216      * um, except that in this instance we are using 4 shorts instead of 8 bytes.
02217      * so, half everything
02218      */
02219     for (count1 = bufferIndex; count1 < 4; count1++) {
02220         plaintext[count1] = (4 - bufferIndex);
02221     }
02222     krc2->rc2_encrypt (plaintext);
02223     for (count1 = 0; count1 < 4; count1++) {
02224         ciphertext[count1] = iv[count1] ^ plaintext[count1];
02225         if (putc ((unsigned char) (ciphertext[count1] >> 8), fd) == EOF) retval = PWERR_DATA;
02226         if (putc ((unsigned char) (ciphertext[count1] & 0xff), fd) == EOF) retval = PWERR_DATA;
02227     }
02228 
02229     fclose (fd);
02230     free(buffer);
02231     return retval;
02232 }
02233 
02234 void ZSafe::quitMe ()
02235 {
02236     if (modified)
02237     {
02238       switch( QMessageBox::information( this, tr("ZSafe"),
02239                                       tr("Do you want to save\nbefore exiting?"),
02240                                       tr("&Save"),
02241                                       tr("S&ave with\nnew\npassword"),
02242                                       tr("&Don't Save"),
02243                                       0      // Enter == button 0
02244                                       ) )
02245       { // Escape == button 2
02246       case 0: // Save clicked, Alt-S or Enter pressed.
02247         // save
02248         modified = false;
02249         saveDocument(filename, FALSE);
02250         exitZs (1);
02251         break;
02252       case 1: //
02253         // Save with new password
02254         modified = false;
02255         saveDocument(filename, TRUE);
02256         exitZs (1);
02257         break;
02258       case 2: // Don't Save clicked or Alt-D pressed
02259         // don't save but exitZs
02260         exitZs (1);
02261         break;
02262       }
02263     }
02264     exitZs (1);
02265 
02266 }
02267 
02268 void ZSafe::categoryFieldActivated( const QString& category)
02269 {
02270    if (categoryDialog)
02271       setCategoryDialogFields(categoryDialog, category);
02272 }
02273 
02274 void ZSafe::addCategory()
02275 {
02276     if (filename.isEmpty())
02277     {
02278        QMessageBox::critical( 0, tr("ZSafe"),
02279          tr("No document defined.\nYou have to create a new document"));
02280        return;
02281     }
02282     else
02283     {
02284        // open the 'Category' dialog
02285         bool initIcons = false;
02286        // open the 'Category' dialog
02287         CategoryDialog *dialog;
02288         if (categoryDialog)
02289         {
02290            dialog = categoryDialog;
02291         }
02292         else
02293         {
02294            categoryDialog = new CategoryDialog(this, tr("Category"), TRUE);
02295            dialog = categoryDialog;
02296            connect( dialog->CategoryField,
02297                     SIGNAL( activated(const QString&)),
02298                     this, SLOT( categoryFieldActivated(const QString&) ) );
02299            initIcons = true;
02300         }
02301 
02302         // read all categories from the config file and store
02303         // into a list
02304         QFile f (cfgFile);
02305         QStringList list;
02306         if ( f.open(IO_ReadOnly) ) {    // file opened successfully
02307            QTextStream t( &f );        // use a text stream
02308            QString s;
02309            while ( !t.eof() ) {        // until end of file...
02310                s = t.readLine();       // line of text excluding '\n'
02311                list.append(s);
02312            }
02313            f.close();
02314         }
02315         QStringList::Iterator it = list.begin();
02316         QString categ;
02317         QString firstCategory;
02318         dialog->CategoryField->clear(); // remove all items
02319         while( it != list.end() )
02320         {
02321            QString *cat = new QString (*it);
02322            if (cat->contains("-field1", FALSE))
02323            {
02324               int pos = cat->find ("-field1");
02325               cat->truncate(pos);
02326               categ = *cat;
02327               if (!categ.isEmpty())
02328               {
02329                  dialog->CategoryField->insertItem (categ, -1);
02330                  if (firstCategory.isEmpty())
02331                     firstCategory = categ;
02332               }
02333            }
02334            ++it;
02335         }
02336 
02337 
02338         if (firstCategory.isEmpty())
02339            setCategoryDialogFields(dialog);
02340         else
02341            setCategoryDialogFields(dialog, firstCategory);
02342 
02343         // CategoryDialog *dialog = new CategoryDialog(this, "Category", TRUE);
02344 
02345            if (initIcons)
02346            {
02347         Wait waitDialog(this, tr("Wait dialog"));
02348         waitDialog.waitLabel->setText(tr("Gathering icons..."));
02349         waitDialog.show();
02350         qApp->processEvents();
02351 
02352         QDir d(QPEApplication::qpeDir() + "pics/");
02353         d.setFilter( QDir::Files);
02354 
02355         const QFileInfoList *list = d.entryInfoList();
02356         QFileInfoListIterator it( *list );      // create list iterator
02357         QFileInfo *fi;                          // pointer for traversing
02358 
02359         dialog->IconField->insertItem("predefined");
02360         while ( (fi=it.current()) ) {           // for each file...
02361             QString fileName = fi->fileName();
02362             if(fileName.right(4) == ".png"){
02363                 fileName = fileName.mid(0,fileName.length()-4);
02364                 QPixmap imageOfFile(Opie::Core::OResource::loadPixmap(fileName,Opie::Core::OResource::SmallIcon));
02365                 dialog->IconField->insertItem(imageOfFile,fileName);
02366             }
02367             ++it;
02368         }
02369         waitDialog.hide();
02370            }
02371 
02372         QDialog::DialogCode result = (QDialog::DialogCode) dialog->exec();
02373         QString category;
02374         QString icon;
02375         QString fullIconPath;
02376         QPixmap *pix;
02377         if (result == QDialog::Accepted)
02378         {
02379            modified = true;
02380            category = dialog->CategoryField->currentText();
02381            icon = dialog->IconField->currentText()+".png";
02382 
02383            QListViewItem *li = new ShadedListItem( 1, ListView );
02384            Category *c1 = new Category();
02385            c1->setCategoryName(category);
02386 
02387            // if (!icon.isEmpty() && !icon.isNull())
02388            if (icon != "predefined.png")
02389            {
02390                // build the full path
02391                fullIconPath = iconPath + icon;
02392                pix = new QPixmap (fullIconPath);
02393                // pix->resize(14, 14);
02394                if (!pix->isNull())
02395                {
02396                   // save the full pixmap name into the config file
02397 // #ifndef Q_WS_WIN
02398                   conf->writeEntry(APP_KEY+category, icon);
02399 // #endif
02400                   saveConf();
02401                   QImage img = pix->convertToImage();
02402                   pix->convertFromImage(img.smoothScale(14,14));
02403                   c1->setIcon (*pix);
02404                   c1->setIconName(icon);
02405                }
02406                else
02407                {
02408                   QPixmap folder( ( const char** ) general_data );
02409                   c1->setIcon (folder);
02410                }
02411            }
02412            else
02413            {
02414               c1->setIcon (*getPredefinedIcon(category));
02415            }
02416 
02417            c1->setListItem (li);
02418            c1->initListItem();
02419            categories.insert (c1->getCategoryName(), c1);
02420 
02421            saveCategoryDialogFields(dialog);
02422         }
02423         else
02424         {
02425            // delete dialog;
02426            dialog->hide();
02427            return;
02428         }
02429 
02430     }
02431 
02432 }
02433 
02434 void ZSafe::delCategory()
02435 {
02436     if (!selectedItem)
02437        return;
02438     if (isCategory(selectedItem))
02439     {
02440        switch( QMessageBox::information( this, tr("ZSafe"),
02441                                       tr("Do you want to delete?"),
02442                                       tr("&Delete"), tr("D&on't Delete"),
02443                                       0      // Enter == button 0
02444                                       ) ) { // Escape == button 2
02445        case 0: // Delete clicked, Alt-S or Enter pressed.
02446           // Delete from the category list
02447           modified = true;
02448           categories.remove (selectedItem->text(0));
02449 // #ifndef Q_WS_WIN
02450           conf->removeEntry (selectedItem->text(0));
02451 // #endif
02452           saveConf();
02453 
02454           // Delete the selected item and all subitems
02455           // step through all subitems
02456           QListViewItem *si;
02457           for (si = selectedItem->firstChild();
02458                si != NULL; )
02459           {
02460               QListViewItem *_si = si;
02461               si = si->nextSibling();
02462               selectedItem->takeItem(_si); // remove from view list
02463               if (_si) delete _si;
02464           }
02465           ListView->takeItem(selectedItem);
02466           delete selectedItem;
02467 
02468           selectedItem = NULL;
02469           break;
02470        case 1: // Don't delete
02471           break;
02472        }
02473 
02474     }
02475 }
02476 
02477 void ZSafe::setCategoryDialogFields(CategoryDialog *dialog)
02478 {
02479    if (!dialog)
02480       return;
02481 
02482    QString icon;
02483    if (selectedItem)
02484    {
02485       dialog->Field1->setText(getFieldLabel (selectedItem, "1", tr("Name")));
02486       dialog->Field2->setText(getFieldLabel (selectedItem, "2", tr("Username")));
02487       dialog->Field3->setText(getFieldLabel (selectedItem, "3", tr("Password")));
02488       dialog->Field4->setText(getFieldLabel (selectedItem, "4", tr("Comment")));
02489       dialog->Field5->setText(getFieldLabel (selectedItem, "5", tr("Field 4")));
02490       dialog->Field6->setText(getFieldLabel (selectedItem, "6", tr("Field 5")));
02491 
02492       Category *cat= categories.find (selectedItem->text(0));
02493       if (cat)
02494       {
02495          icon = cat->getIconName();
02496       }
02497       else
02498          icon = conf->readEntry(APP_KEY+selectedItem->text(0));
02499    }
02500    else
02501    {
02502       dialog->Field1->setText(tr("Name"));
02503       dialog->Field2->setText(tr("Username"));
02504       dialog->Field3->setText(tr("Password"));
02505       dialog->Field4->setText(tr("Comment"));
02506       dialog->Field5->setText(tr("Field 4"));
02507       dialog->Field6->setText(tr("Field 5"));
02508    }
02509 
02510     QDir d(QPEApplication::qpeDir() + "pics/");
02511     d.setFilter( QDir::Files);
02512 
02513     const QFileInfoList *list = d.entryInfoList();
02514     int i=0;
02515     QFileInfoListIterator it( *list );      // create list iterator
02516     QFileInfo *fi;                          // pointer for traversing
02517    if (icon.isEmpty() || icon.isNull())
02518    {
02519       dialog->IconField->setCurrentItem(0);
02520    }
02521    else
02522    {
02523         while ( (fi=it.current()) )
02524       { // for each file...
02525             QString fileName = fi->fileName();
02526             if(fileName.right(4) == ".png")
02527                         {
02528             fileName = fileName.mid(0,fileName.length()-4);
02529 
02530             if(fileName+".png"==icon)
02531          {
02532             dialog->IconField->setCurrentItem(i+1);
02533             break;
02534          }
02535                ++i;
02536             }
02537             ++it;
02538         }
02539     }
02540 }
02541 
02542 void ZSafe::setCategoryDialogFields(CategoryDialog *dialog, QString category)
02543 {
02544    if (!dialog)
02545       return;
02546 
02547    dialog->Field1->setText(getFieldLabel (category, "1", tr("Name")));
02548    dialog->Field2->setText(getFieldLabel (category, "2", tr("Username")));
02549    dialog->Field3->setText(getFieldLabel (category, "3", tr("Password")));
02550    dialog->Field4->setText(getFieldLabel (category, "4", tr("Comment")));
02551    dialog->Field5->setText(getFieldLabel (category, "5", tr("Field 4")));
02552    dialog->Field6->setText(getFieldLabel (category, "6", tr("Field 5")));
02553 
02554    QString icon;
02555    Category *cat= categories.find (category);
02556    if (cat)
02557    {
02558       icon = cat->getIconName();
02559    }
02560    else
02561       icon = conf->readEntry(APP_KEY+category);
02562 
02563     QDir d(QPEApplication::qpeDir() + "pics/");
02564     d.setFilter( QDir::Files);
02565 
02566     const QFileInfoList *list = d.entryInfoList();
02567     int i=0;
02568     QFileInfoListIterator it( *list );      // create list iterator
02569     QFileInfo *fi;                          // pointer for traversing
02570    if (icon.isEmpty() || icon.isNull())
02571    {
02572       dialog->IconField->setCurrentItem(0);
02573    }
02574    else
02575    {
02576         while ( (fi=it.current()) )
02577       { // for each file...
02578             QString fileName = fi->fileName();
02579             if(fileName.right(4) == ".png")
02580                         {
02581             fileName = fileName.mid(0,fileName.length()-4);
02582 
02583             if(fileName+".png"==icon)
02584          {
02585             dialog->IconField->setCurrentItem(i+1);
02586             break;
02587          }
02588                ++i;
02589             }
02590             ++it;
02591         }
02592     }
02593 }
02594 
02595 void ZSafe::saveCategoryDialogFields(CategoryDialog *dialog)
02596 {
02597    QString app_key = APP_KEY;
02598    conf->setGroup( "fieldDefs" );
02599    QString category = dialog->CategoryField->currentText();
02600 // #ifndef Q_WS_WIN
02601    conf->writeEntry(app_key+category+"-field1", dialog->Field1->text());
02602    conf->writeEntry(app_key+category+"-field2", dialog->Field2->text());
02603    conf->writeEntry(app_key+category+"-field3", dialog->Field3->text());
02604    conf->writeEntry(app_key+category+"-field4", dialog->Field4->text());
02605    conf->writeEntry(app_key+category+"-field5", dialog->Field5->text());
02606    conf->writeEntry(app_key+category+"-field6", dialog->Field6->text());
02607 // #endif
02608    saveConf();
02609    conf->setGroup ("zsafe");
02610 }
02611 
02612 void ZSafe::editCategory()
02613 {
02614     if (!selectedItem)
02615        return;
02616     if (isCategory(selectedItem))
02617     {
02618         QString category = selectedItem->text(0);
02619         bool initIcons = false;
02620         // open the 'Category' dialog
02621         CategoryDialog *dialog;
02622         if (categoryDialog)
02623         {
02624            dialog = categoryDialog;
02625         }
02626         else
02627         {
02628            categoryDialog = new CategoryDialog(this, tr("Category"), TRUE);
02629            dialog = categoryDialog;
02630            connect( dialog->CategoryField,
02631                     SIGNAL( activated(const QString&)),
02632                     this, SLOT( categoryFieldActivated(const QString&) ) );
02633            initIcons = true;
02634         }
02635         setCategoryDialogFields(dialog);
02636 
02637         // read all categories from the config file and store
02638         // into a list
02639         QFile f (cfgFile);
02640         QStringList list;
02641         if ( f.open(IO_ReadOnly) ) {    // file opened successfully
02642            QTextStream t( &f );        // use a text stream
02643            QString s;
02644            while ( !t.eof() ) {        // until end of file...
02645                s = t.readLine();       // line of text excluding '\n'
02646                list.append(s);
02647            }
02648            f.close();
02649         }
02650         QStringList::Iterator it = list.begin();
02651         QString categ;
02652         dialog->CategoryField->clear(); // remove all items
02653         int i=0;
02654         bool foundCategory = false;
02655         while( it != list.end() )
02656         {
02657            QString *cat = new QString (*it);
02658            if (cat->contains("-field1", FALSE))
02659            {
02660               int pos = cat->find ("-field1");
02661               cat->truncate(pos);
02662               categ = *cat;
02663               if (!categ.isEmpty())
02664               {
02665                  dialog->CategoryField->insertItem (categ, i);
02666                  if (category.compare(categ) == 0)
02667                  {
02668                      dialog->CategoryField->setCurrentItem(i);
02669                      foundCategory = true;
02670                  }
02671                  i++;
02672               }
02673            }
02674            ++it;
02675         }
02676         if (!foundCategory)
02677         {
02678             dialog->CategoryField->insertItem (category, i);
02679             dialog->CategoryField->setCurrentItem(i);
02680         }
02681 
02682         QString icon;
02683         Category *cat= categories.find (selectedItem->text(0));
02684         if (cat)
02685         {
02686            icon = cat->getIconName();
02687         }
02688 
02689            if (initIcons)
02690            {
02691 
02692         Wait waitDialog(this, tr("Wait dialog"));
02693         waitDialog.waitLabel->setText(tr("Gathering icons..."));
02694         waitDialog.show();
02695         qApp->processEvents();
02696 
02697         QDir d(QPEApplication::qpeDir() + "pics/");
02698         d.setFilter( QDir::Files);
02699 
02700         const QFileInfoList *list = d.entryInfoList();
02701         int i=0;
02702         QFileInfoListIterator it( *list );      // create list iterator
02703         QFileInfo *fi;                          // pointer for traversing
02704                 if (icon.isEmpty() || icon.isNull())
02705                 {
02706                    dialog->IconField->setCurrentItem(0);
02707                 }
02708 
02709         dialog->IconField->insertItem("predefined");
02710         while ( (fi=it.current()) ) {           // for each file...
02711             QString fileName = fi->fileName();
02712             if(fileName.right(4) == ".png")
02713                         {
02714                 fileName = fileName.mid(0,fileName.length()-4);
02715                 QPixmap imageOfFile(Opie::Core::OResource::loadPixmap(fileName,Opie::Core::OResource::SmallIcon));
02716                 dialog->IconField->insertItem(imageOfFile,fileName);
02717                 if(fileName+".png"==icon)
02718                                    dialog->IconField->setCurrentItem(i+1);
02719                     ++i;
02720             }
02721             ++it;
02722         }
02723         waitDialog.hide();
02724            }
02725            else
02726            {
02727         QDir d(QPEApplication::qpeDir() + "pics/");
02728         d.setFilter( QDir::Files);
02729 
02730         const QFileInfoList *list = d.entryInfoList();
02731         int i=0;
02732         QFileInfoListIterator it( *list );      // create list iterator
02733         QFileInfo *fi;                          // pointer for traversing
02734                 if (icon.isEmpty() || icon.isNull())
02735                 {
02736                    dialog->IconField->setCurrentItem(0);
02737                 }
02738                 else
02739                 {
02740 
02741           while ( (fi=it.current()) )
02742                   { // for each file...
02743             QString fileName = fi->fileName();
02744             if(fileName.right(4) == ".png")
02745                         {
02746                 fileName = fileName.mid(0,fileName.length()-4);
02747 
02748 
02749                 if(fileName+".png"==icon)
02750                                 {
02751                                    dialog->IconField->setCurrentItem(i+1);
02752                                    break;
02753                                 }
02754                     ++i;
02755             }
02756             ++it;
02757           }
02758         }
02759            }
02760 
02761         // dialog->show();
02762         QDialog::DialogCode result = (QDialog::DialogCode) dialog->exec();
02763 
02764         QString fullIconPath;
02765         QPixmap *pix;
02766         if (result == QDialog::Accepted)
02767         {
02768            modified = true;
02769            if (category != dialog->CategoryField->currentText())
02770            {
02771               categories.remove (category);
02772 // #ifndef Q_WS_WIN
02773               conf->removeEntry(category);
02774 // #endif
02775               saveConf();
02776            }
02777 
02778            category = dialog->CategoryField->currentText();
02779            icon = dialog->IconField->currentText()+".png";
02780 
02781            if (cat)
02782            {
02783               // if (!icon.isEmpty() && !icon.isNull())
02784               if (icon != "predefined.png")
02785               {
02786                   // build the full path
02787                   fullIconPath = iconPath + icon;
02788                   pix = new QPixmap (fullIconPath);
02789                   if (!pix->isNull())
02790                   {
02791                      // save the full pixmap name into the config file
02792 // #ifndef Q_WS_WIN
02793                      conf->writeEntry(APP_KEY+category, icon);
02794 // #endif
02795                      saveConf();
02796                      QImage img = pix->convertToImage();
02797                      pix->convertFromImage(img.smoothScale(14,14));
02798                      cat->setIconName (icon);
02799                      cat->setIcon (*pix);
02800                   }
02801               }
02802               else
02803               {
02804 // #ifndef Q_WS_WIN
02805                  conf->removeEntry (category);
02806 // #endif
02807                  saveConf();
02808                  cat->setIcon (*getPredefinedIcon(category));
02809               }
02810 
02811               // change the category name of the selected category
02812               QListViewItem *catItem = cat->getListItem();
02813               if (catItem)
02814               {
02815                  catItem->setText( 0, tr( category ) );
02816                  cat->setCategoryName (tr(category));
02817 
02818                  cat->initListItem();
02819                  categories.insert (category, cat);
02820               }
02821            }
02822            saveCategoryDialogFields(dialog);
02823         }
02824         else
02825         {
02826            // delete dialog;
02827            dialog->hide();
02828            return;
02829         }
02830 
02831     }
02832 }
02833 
02834 void ZSafe::cutItem()
02835 {
02836     if (!selectedItem)
02837        return;
02838     if (!isCategory(selectedItem))
02839     {
02840        IsCut = true;
02841        copiedItem = selectedItem;
02842     }
02843 }
02844 
02845 void ZSafe::copyItem()
02846 {
02847     if (!selectedItem)
02848        return;
02849     if (!isCategory(selectedItem))
02850     {
02851        IsCopy = true;
02852        copiedItem = selectedItem;
02853     }
02854 }
02855 
02856 // paste item into category
02857 void ZSafe::pasteItem()
02858 {
02859     if (!selectedItem)
02860        return;
02861     if (isCategory(selectedItem))
02862     {
02863        modified = true;
02864        if (IsCut)
02865        {
02866           if (copiedItem)
02867           {
02868              // add the new item
02869              QListViewItem *i = new ShadedListItem (0, selectedItem);
02870              // i->setOpen (TRUE);
02871              i->setText (0, copiedItem->text(0));
02872              i->setText (1, copiedItem->text(1));
02873              i->setText (2, copiedItem->text(2));
02874              i->setText (3, copiedItem->text(3));
02875              i->setText (4, copiedItem->text(4));
02876              i->setText (5, copiedItem->text(5));
02877              selectedItem->setOpen( TRUE );
02878 
02879              // remove the cutted item
02880              copiedItem->parent()->takeItem(copiedItem);
02881              selectedItem = NULL;
02882           }
02883        }
02884        else if (IsCopy)
02885        {
02886           if (copiedItem)
02887           {
02888               // add the new item
02889               QListViewItem *i = new ShadedListItem (0, selectedItem);
02890               // i->setOpen (TRUE);
02891               i->setText (0, copiedItem->text(0));
02892               i->setText (1, copiedItem->text(1));
02893               i->setText (2, copiedItem->text(2));
02894               i->setText (3, copiedItem->text(3));
02895               i->setText (4, copiedItem->text(4));
02896               i->setText (5, copiedItem->text(5));
02897               selectedItem->setOpen( TRUE );
02898           }
02899        }
02900     }
02901     IsCut = false;
02902     IsCopy = false;
02903 }
02904 
02905 void ZSafe::newDocument()
02906 {
02907 
02908     // open the file dialog
02909          QString newFile = zsaveDialog();
02910 
02911     // open the new document
02912     if (newFile && newFile.length() > 0 )
02913     {
02914        // save the previous opened document
02915        if (!filename.isEmpty())
02916           saveDocument(filename, FALSE);
02917 
02918        modified = true;
02919 
02920        // clear the password list
02921        QListViewItem *i;
02922        QListViewItem *c = NULL;
02923        // step through all categories
02924        for (i = ListView->firstChild();
02925             i != NULL;
02926             i = i->nextSibling())
02927        {
02928           if (c) delete c; // delete the previous category
02929 
02930           c = i;
02931           // step through all subitems
02932           QListViewItem *si;
02933           for (si = i->firstChild();
02934                si != NULL; )
02935           {
02936               QListViewItem *_si = si;
02937               si = si->nextSibling();
02938               i->takeItem(_si); // remove from view list
02939               if (_si) delete _si;
02940           }
02941        }
02942        if (c) delete c; // delete the previous category
02943        categories.clear();
02944 
02945        // m_password = "";
02946        selectedItem = NULL;
02947 
02948        filename = newFile;
02949 
02950        // save the current filename to the config file
02951        conf->setGroup("zsafe");
02952        conf->writeEntry(APP_KEY+"document", filename);
02953        saveConf();
02954        QString ti = filename.right (filename.length() - filename.findRev ('/') - 1);
02955        this->setCaption(tr("ZSafe: ") + ti);
02956 
02957        // openDocument(filename);
02958 
02959        QMessageBox::information( this, tr("ZSafe"),
02960        tr("Now you have to enter\na password twice for your\nnewly created document."),       tr("&OK"), 0);
02961 
02962        saveDocumentWithPwd();
02963     }
02964 }
02965 
02966 void ZSafe::loadDocument()
02967 {
02968 
02969     // open the file dialog
02970    QMap<QString, QStringList> mimeTypes;
02971    mimeTypes.insert(tr("All"), QStringList() );
02972    mimeTypes.insert(tr("ZSafe"), "zsafe/*" );
02973    QString newFile = OFileDialog::getOpenFileName( OFileSelector::EXTENDED_ALL,
02974                        QDir::homeDirPath() + "/Documents/application/zsafe",
02975                        QString::null,
02976                        mimeTypes,
02977                        this,
02978                        tr ("Open ZSafe document"));
02979 
02980     // open the new document
02981     if (newFile && newFile.length() > 0 )
02982     {
02983        // save the previous opened document
02984        if (!filename.isEmpty())
02985           saveDocument(filename, FALSE);
02986 
02987        // clear the password list
02988        QListViewItem *i;
02989        QListViewItem *c = NULL;
02990        // step through all categories
02991        for (i = ListView->firstChild();
02992             i != NULL;
02993             i = i->nextSibling())
02994        {
02995           if (c) delete c; // delete the previous category
02996 
02997           c = i;
02998           // step through all subitems
02999           QListViewItem *si;
03000           for (si = i->firstChild();
03001                si != NULL; )
03002           {
03003               QListViewItem *_si = si;
03004               si = si->nextSibling();
03005               i->takeItem(_si); // remove from view list
03006               if (_si) delete _si;
03007           }
03008        }
03009        if (c) delete c; // delete the previous category
03010        categories.clear();
03011        m_password = "";
03012        selectedItem = NULL;
03013        filename = newFile;
03014 
03015        // save the current filename to the config file
03016        conf->setGroup("zsafe");
03017        conf->writeEntry(APP_KEY+"document", filename);
03018        saveConf();
03019        QString ti = filename.right (filename.length() - filename.findRev ('/') - 1);
03020        this->setCaption(tr("ZSafe: ") + ti);
03021 
03022        openDocument(filename);
03023     }
03024 }
03025 
03026 void ZSafe::saveDocumentAs()
03027 {
03028 
03029 QString newFile = zsaveDialog();
03030     // open the new document
03031     if (newFile && newFile.length() > 0 )
03032     {
03033        // save the previous opened document
03034        if (!filename.isEmpty())
03035           saveDocument(filename, FALSE);
03036 
03037        selectedItem = NULL;
03038        filename = newFile;
03039 
03040        // save the current filename to the config file
03041        conf->setGroup("zsafe");
03042        conf->writeEntry(APP_KEY+"document", filename);
03043        saveConf();
03044        QString ti = filename.right (filename.length() - filename.findRev ('/') - 1);
03045        this->setCaption(tr("ZSafe: ") + ti);
03046 
03047        QMessageBox::information( this, tr("ZSafe"),
03048        tr("Now you have to enter\na password twice for your\nnewly created document."),       tr("&OK"), 0);
03049 
03050        saveDocumentWithPwd();
03051     }
03052 }
03053 
03054 void ZSafe::saveDocumentWithoutPwd()
03055 {
03056    saveDocument(filename, FALSE);
03057 }
03058 
03059 void ZSafe::saveDocumentWithPwd()
03060 {
03061    saveDocument(filename, TRUE);
03062 }
03063 
03064 void ZSafe::setExpandFlag()
03065 {
03066    expandTree = !expandTree;
03067    file->setItemChecked('o', expandTree);
03068    conf->setGroup ("zsafePrefs");
03069 // #ifndef Q_WS_WIN
03070    conf->writeEntry (APP_KEY+"expandTree", expandTree);
03071 // #endif
03072    saveConf();
03073 
03074 }
03075 
03076 void ZSafe::paintEvent( QPaintEvent * )
03077 {
03078    if (raiseFlag)
03079    {
03080       raiseFlag = false;
03081       raiseTimer.start (1, true);
03082       if (infoForm->isVisible())
03083          infoForm->raise();
03084    }
03085 }
03086 
03087 void ZSafe::slotRaiseTimer()
03088 {
03089    if (infoForm->isVisible())
03090       infoForm->raise();
03091    raiseFlag = true;
03092 }
03093 
03094 QPixmap * ZSafe::getPredefinedIcon(QString category)
03095 {
03096    QPixmap *pm;
03097    if (category == "Bank cards")
03098       pm = new QPixmap((const char**)bank_cards_data);
03099    else if (category == "Passwords")
03100       pm = new QPixmap((const char**)passwords_data);
03101    else if (category == "Software")
03102       pm = new QPixmap((const char**)software_data);
03103    else if (category == "General")
03104       pm = new QPixmap((const char**)general_data);
03105    else
03106       pm = new QPixmap((const char**)general_data);
03107    return pm;
03108 }
03109 
03110 void ZSafe::setDocument(const QString& fileref)
03111 {
03112      // stop the timer to prevent loading of the default document
03113      docuTimer.stop();
03114 
03115      DocLnk link(fileref);
03116      if ( link.isValid() )
03117      {
03118          // if (filename != link.file())
03119             // saveDocument(filename, FALSE);
03120          filename = link.file();
03121      }
03122      else
03123      {
03124          // if (filename != fileref)
03125             // saveDocument(filename, FALSE);
03126          filename = fileref;
03127      }
03128      // save the current filename to the config file
03129      conf->setGroup("zsafe");
03130      conf->writeEntry(APP_KEY+"document", filename);
03131      saveConf();
03132      QString ti = filename.right (filename.length() - filename.findRev ('/') - 1);
03133      this->setCaption(tr("ZSafe: ") + ti);
03134 
03135      // clear the password list
03136      QListViewItem *i;
03137      QListViewItem *c = NULL;
03138      // step through all categories
03139      for (i = ListView->firstChild();
03140             i != NULL;
03141             i = i->nextSibling())
03142      {
03143           if (c) delete c; // delete the previous category
03144 
03145           c = i;
03146           // step through all subitems
03147           QListViewItem *si;
03148           for (si = i->firstChild();
03149                si != NULL; )
03150           {
03151               QListViewItem *_si = si;
03152               si = si->nextSibling();
03153               i->takeItem(_si); // remove from view list
03154               if (_si) delete _si;
03155           }
03156      }
03157      if (c) delete c; // delete the previous category
03158      categories.clear();
03159 
03160      m_password = "";
03161      selectedItem = NULL;
03162 
03163      openDocument(filename);
03164 }
03165 
03166 
03167 void ZSafe::ListPressed(int mouse, QListViewItem *item, const QPoint&, int column) {
03168         if(item ==0) return;
03169    switch (mouse) {
03170    case 1:
03171    break;
03172    case 2:
03173            {
03174                         QClipboard *cb = QApplication::clipboard();
03175 
03176             QIconSet copy_img = Opie::Core::OResource::loadPixmap( "copy", Opie::Core::OResource::SmallIcon );
03177             QIconSet edit_img = Opie::Core::OResource::loadPixmap( "edit", Opie::Core::OResource::SmallIcon );
03178             QPixmap folder_open_img = Opie::Core::OResource::loadPixmap( "folder_open", Opie::Core::OResource::SmallIcon );
03179             QPixmap editdelete_img = Opie::Core::OResource::loadPixmap( "editdelete", Opie::Core::OResource::SmallIcon );
03180 
03181                         QPopupMenu *m = new QPopupMenu(this);
03182                         int copyItem =  m->insertItem( copy_img, tr( "Copy to Clipboard" ));
03183                         int editItem = m->insertItem(edit_img, tr( "Edit" ));
03184                         int showItem = m->insertItem(folder_open_img, tr( "Show Info" ));
03185                         int cancelItem = m->insertItem( editdelete_img, tr( "Cancel" ));
03186                         m->setFocus();
03187                         int me=m->exec( QPoint( QCursor::pos().x(), QCursor::pos().y() ) ) ;
03188                          if(me == copyItem) {
03189                                 copyClip( item->text(column) ) ;
03190                         } else if (me == cancelItem) {
03191                              cb->clear();
03192                         } else if (me == editItem) {
03193                                 editPwd();
03194                         } else if (me == showItem) {
03195                                 showInfo(item);
03196                         }
03197            }
03198       break;
03199    };
03200 }
03201 
03202 void  ZSafe::copyClip( const QString &text) {
03203         QClipboard *cb = QApplication::clipboard();
03204         cb->setText( text);
03205 }
03206 
03207 
03208 QString ZSafe::zsaveDialog() {
03209 
03210                 QString fn;
03211    QMap<QString, QStringList> mimeTypes;
03212    mimeTypes.insert(tr("All"), QStringList() );
03213    mimeTypes.insert(tr("Text"), "text/*" );
03214    fn = OFileDialog::getSaveFileName( OFileSelector::EXTENDED_ALL,
03215                        QDir::homeDirPath() + "/Documents/application/zsafe",
03216                        QString::null,
03217                        mimeTypes,
03218                        this,
03219                        tr ("Export text file"));
03220      return fn;
03221 }

Generated on Sat Nov 5 16:17:15 2005 for OPIE by  doxygen 1.4.2