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

mainwindow.cpp

Go to the documentation of this file.
00001 /*
00002                      This file is part of the OPIE Project
00003                =.
00004       .=l.            Copyright (c)  2002 Dan Williams <drw@handhelds.org>
00005      .>+-=
00006 _;:,   .>  :=|.         This file is free software; you can
00007 .> <`_,  > .  <=          redistribute it and/or modify it under
00008 :`=1 )Y*s>-.--  :           the terms of the GNU General Public
00009 .="- .-=="i,   .._         License as published by the Free Software
00010 - .  .-<_>   .<>         Foundation; either version 2 of the License,
00011   ._= =}    :          or (at your option) any later version.
00012   .%`+i>    _;_.
00013   .i_,=:_.   -<s.       This file is distributed in the hope that
00014   + . -:.    =       it will be useful, but WITHOUT ANY WARRANTY;
00015   : ..  .:,   . . .    without even the implied warranty of
00016   =_    +   =;=|`    MERCHANTABILITY or FITNESS FOR A
00017  _.=:.    :  :=>`:     PARTICULAR PURPOSE. See the GNU General
00018 ..}^=.=    =    ;      Public License for more details.
00019 ++=  -.   .`   .:
00020 :   = ...= . :.=-        You should have received a copy of the GNU
00021 -.  .:....=;==+<;          General Public License along with this file;
00022  -_. . .  )=. =           see the file COPYING. If not, write to the
00023   --    :-=`           Free Software Foundation, Inc.,
00024                              59 Temple Place - Suite 330,
00025                              Boston, MA 02111-1307, USA.
00026 
00027 */
00028 
00029 #include "mainwindow.h"
00030 #include "cbinfo.h"
00031 #include "configuration.h"
00032 #include "password.h"
00033 #include "checkbook.h"
00034 
00035 #include <opie2/oresource.h>
00036 
00037 #include <qpe/config.h>
00038 #include <qpe/qpeapplication.h>
00039 #include <qpe/qpemessagebox.h>
00040 
00041 #include <qaction.h>
00042 #include <qdir.h>
00043 #include <qmenubar.h>
00044 #include <qtoolbar.h>
00045 #include <qwhatsthis.h>
00046 
00047 
00048 MainWindow::MainWindow( QWidget* parent, const char* name, WFlags /*fl*/ )
00049     : QMainWindow( parent, name, WStyle_ContextHelp )
00050 {
00051     setCaption( tr( "Checkbook" ) );
00052 
00053     cbDir = Global::applicationFileName( "checkbook", "" );
00054     lockIcon = Opie::Core::OResource::loadPixmap( "locked", Opie::Core::OResource::SmallIcon );
00055 
00056     // Load configuration options
00057     Config config( "checkbook" );
00058     _cfg.readConfig( config );
00059 
00060 
00061     // Build menu and tool bars
00062     setToolBarsMovable( FALSE );
00063 
00064     QToolBar *bar = new QToolBar( this );
00065     bar->setHorizontalStretchable( TRUE );
00066     QMenuBar *mb = new QMenuBar( bar );
00067     mb->setMargin( 0 );
00068     QPopupMenu *popup = new QPopupMenu( this );
00069 
00070     bar = new QToolBar( this );
00071     QAction *a = new QAction( tr( "New" ), Opie::Core::OResource::loadPixmap( "new", Opie::Core::OResource::SmallIcon ),
00072                               QString::null, 0, this, 0 );
00073     a->setWhatsThis( tr( "Click here to create a new checkbook.\n\nYou also can select New from the Checkbook menu." ) );
00074     connect( a, SIGNAL( activated() ), this, SLOT( slotNew() ) );
00075     a->addTo( popup );
00076     a->addTo( bar );
00077 
00078     actionOpen = new QAction( tr( "Edit" ), Opie::Core::OResource::loadPixmap( "edit", Opie::Core::OResource::SmallIcon ),
00079                               QString::null, 0, this, 0 );
00080     actionOpen->setWhatsThis( tr( "Select a checkbook and then click here to edit it.\n\nYou also can select Edit from the Checkbook menu, or click and hold on a checkbook name." ) );
00081     connect( actionOpen, SIGNAL( activated() ), this, SLOT( slotEdit() ) );
00082     actionOpen->addTo( popup );
00083     actionOpen->addTo( bar );
00084 
00085     actionDelete = new QAction( tr( "Delete" ), Opie::Core::OResource::loadPixmap( "trash", Opie::Core::OResource::SmallIcon ),
00086                                 QString::null, 0, this, 0 );
00087     actionDelete->setWhatsThis( tr( "Select a checkbook and then click here delete it.\n\nYou also can select Delete from the Checkbook menu." ) );
00088     connect( actionDelete, SIGNAL( activated() ), this, SLOT( slotDelete() ) );
00089     actionDelete->addTo( popup );
00090     actionDelete->addTo( bar );
00091 
00092     popup->insertSeparator();
00093 
00094     a = new QAction( tr( "Configure" ), Opie::Core::OResource::loadPixmap( "SettingsIcon", Opie::Core::OResource::SmallIcon ),
00095                      QString::null, 0, this, 0 );
00096     a->setWhatsThis( tr( "Click here to configure this app." ) );
00097     connect( a, SIGNAL( activated() ), this, SLOT( slotConfigure() ) );
00098     a->addTo( popup );
00099     a->addTo( bar );
00100 
00101     mb->insertItem( tr( "Checkbook" ), popup );
00102 
00103     // Load Checkbook selection list
00104     checkbooks = new CBInfoList();
00105 
00106     QDir checkdir( cbDir );
00107     if (checkdir.exists() == true)
00108     {
00109         QStringList cblist = checkdir.entryList( "*.qcb", QDir::Files|QDir::Readable|QDir::Writable,
00110                                                      QDir::Time );
00111         CBInfo *cb = 0x0;
00112         QString filename;
00113 
00114         for ( QStringList::Iterator it = cblist.begin(); it != cblist.end(); it++ )
00115         {
00116             filename = cbDir;
00117             filename.append( (*it) );
00118 
00119             cb = new CBInfo( (*it).remove( (*it).find('.'), (*it).length() ), filename );
00120             checkbooks->inSort( cb );
00121         }
00122     }
00123 
00124     // Build Checkbook selection list control
00125     cbList = 0x0;
00126     buildList();
00127 
00128     // open last book?
00129     if( _cfg.isOpenLastBook() ) {
00130         this->show();
00131         this->showMaximized();
00132         QListViewItem *itm=cbList->firstChild();
00133         while( itm ) {
00134             if( itm->text(posName)==_cfg.getLastBook() ) {
00135                 openBook( itm );
00136                 break;
00137             }
00138             itm=itm->nextSibling();
00139         }
00140     }
00141 }
00142 
00143 
00144 // --- ~MainWindow ------------------------------------------------------------
00145 MainWindow::~MainWindow()
00146 {
00147     writeConfig();
00148 }
00149 
00150 
00151 // --- buildList --------------------------------------------------------------
00152 void MainWindow::buildList()
00153 {
00154     if ( cbList )
00155             delete cbList;
00156 
00157     cbList = new QListView( this );
00158     QWhatsThis::add( cbList, tr( "This is a listing of all checkbooks currently available." ) );
00159 
00160     if ( _cfg.getShowLocks() )
00161     {
00162         cbList->addColumn( lockIcon, "", 24 );
00163         posName = 1;
00164     }
00165     else
00166     {
00167         posName = 0;
00168     }
00169     cbList->addColumn( tr( "Checkbook Name" ) );
00170     if ( _cfg.getShowBalances() )
00171     {
00172         int colnum = cbList->addColumn( tr( "Balance" ) );
00173         cbList->setColumnAlignment( colnum, Qt::AlignRight );
00174     }
00175     cbList->setAllColumnsShowFocus( TRUE );
00176     cbList->setSorting( posName );
00177     QPEApplication::setStylusOperation( cbList->viewport(), QPEApplication::RightOnHold );
00178     connect( cbList, SIGNAL( rightButtonPressed(QListViewItem*,const QPoint&,int) ),
00179              this, SLOT( slotEdit() ) );
00180     setCentralWidget( cbList );
00181 
00182     for ( CBInfo *cb = checkbooks->first(); cb; cb = checkbooks->next() )
00183     {
00184         addCheckbook( cb );
00185     }
00186 }
00187 
00188 void MainWindow::addCheckbook( CBInfo *cb )
00189 {
00190     QListViewItem *lvi = new QListViewItem( cbList );
00191     if ( _cfg.getShowLocks() && !cb->password().isNull() )
00192     {
00193         lvi->setPixmap( 0, lockIcon );
00194     }
00195     lvi->setText( posName, cb->name() );
00196     if ( _cfg.getShowBalances() )
00197     {
00198         QString balance;
00199         balance.sprintf( "%.2f", cb->balance() );
00200         balance.prepend( _cfg.getCurrencySymbol() );
00201         lvi->setText( posName + 1, balance );
00202     }
00203 }
00204 
00205 void MainWindow::buildFilename( const QString &name )
00206 {
00207     tempFilename = cbDir;
00208     tempFilename.append( name );
00209     tempFilename.append( ".qcb" );
00210 }
00211 
00212 void MainWindow::slotNew()
00213 {
00214     CBInfo *cb = new CBInfo();
00215 
00216     Checkbook *currcb = new Checkbook( this, cb, &_cfg );
00217     if ( QPEApplication::execDialog( currcb ) == QDialog::Accepted )
00218     {
00219         // Save new checkbook
00220         buildFilename( cb->name() );
00221         _cfg.setLastBook( cb->name() );
00222         cb->setFilename( tempFilename );
00223         cb->write();
00224 
00225         // Add to listbox
00226         checkbooks->inSort( cb );
00227         addCheckbook( cb );
00228     }
00229     delete currcb;
00230 }
00231 
00232 // --- slotEdit ---------------------------------------------------------------
00233 void MainWindow::slotEdit()
00234 {
00235     // get name and open it
00236     QListViewItem *curritem = cbList->currentItem();
00237     if ( !curritem )
00238         return;
00239     openBook( curritem );
00240 }
00241 
00242 
00243 // --- openBook ---------------------------------------------------------------
00244 void MainWindow::openBook(QListViewItem *curritem)
00245 {
00246     // find book in List
00247     QString currname=curritem->text(posName);
00248     CBInfo *cb = checkbooks->first();
00249     while ( cb ) {
00250         if ( cb->name() == currname )
00251             break;
00252         cb = checkbooks->next();
00253     }
00254     if ( !cb ) return;
00255 
00256     //
00257     buildFilename( currname );
00258     float currbalance = cb->balance();
00259     bool currlock = !cb->password().isNull();
00260 
00261     if ( currlock )
00262     {
00263         Password *pw = new Password( this, tr( "Enter password" ), tr( "Please enter your password:" ) );
00264         if ( pw->exec() != QDialog::Accepted || pw->password != cb->password()  )
00265         {
00266             delete pw;
00267             return;
00268         }
00269         delete pw;
00270     }
00271 
00272     _cfg.setLastBook( currname );
00273     Checkbook *currcb = new Checkbook( this, cb, &_cfg );
00274     if ( QPEApplication::execDialog( currcb ) == QDialog::Accepted )
00275     {
00276         QString newname = cb->name();
00277         if ( currname != newname )
00278         {
00279             // Update name if changed
00280             if( curritem ) {
00281                 curritem->setText( posName, newname );
00282                 cbList->sort();
00283             }
00284             _cfg.setLastBook( newname );
00285 
00286             // Remove old file
00287             QFile f( tempFilename );
00288             if ( f.exists() )
00289                 f.remove();
00290 
00291             // Get new filename
00292             buildFilename( newname );
00293             cb->setFilename( tempFilename );
00294         }
00295 
00296         cb->write();
00297 
00298         // Update lock if changed
00299         if ( _cfg.getShowLocks() && !cb->password().isNull() != currlock )
00300         {
00301             if ( !cb->password().isNull() )
00302                 curritem->setPixmap( 0, lockIcon );
00303             else
00304                 curritem->setPixmap( 0, nullIcon );
00305         }
00306 
00307         // Update balance if changed
00308         if ( _cfg.getShowBalances() && cb->balance() != currbalance )
00309         {
00310             QString tempstr;
00311             tempstr.sprintf( "%.2f", cb->balance() );
00312             tempstr.prepend( _cfg.getCurrencySymbol() );
00313             curritem->setText( posName + 1, tempstr );
00314         }
00315 
00316         // write config, if needed
00317         if( _cfg.isDirty() ) {
00318             Config config("checkbook");
00319             _cfg.writeConfig( config );
00320         }
00321     }
00322     delete currcb;
00323 }
00324 
00325 // --- slotDelete -------------------------------------------------------------
00326 void MainWindow::slotDelete()
00327 {
00328     QString currname = cbList->currentItem()->text( posName );
00329 
00330     if ( QPEMessageBox::confirmDelete ( this, tr( "Delete checkbook" ), currname ) )
00331     {
00332         buildFilename( currname );
00333         QFile f( tempFilename );
00334         if ( f.exists() )
00335         {
00336             f.remove();
00337         }
00338 
00339         delete cbList->currentItem();
00340     }
00341 }
00342 
00343 // --- slotConfigure ----------------------------------------------------------
00344 void MainWindow::slotConfigure()
00345 {
00346     Configuration *cfgdlg = new Configuration( this, _cfg );
00347     if ( QPEApplication::execDialog( cfgdlg ) == QDialog::Accepted )
00348     {
00349         // read data from config dialog & save it
00350         cfgdlg->saveConfig( _cfg );
00351         writeConfig();
00352         buildList();
00353     }
00354     delete cfgdlg;
00355 }
00356 
00357 
00358 // --- writeConfig --------------------------------------------------------------
00359 void MainWindow::writeConfig()
00360 {
00361     Config config("checkbook");
00362     _cfg.writeConfig( config );
00363 }

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