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

cbinfo.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 "cbinfo.h"
00030 #include "traninfo.h"
00031 
00032 #include <qpe/config.h>
00033 
00034 #include <qfile.h>
00035 
00036 // --- CBInfo -----------------------------------------------------------------
00037 CBInfo::CBInfo()
00038 {
00039         n = "";
00040         fn = "";
00041         pw = QString::null;
00042         t = "";
00043         bn = "";
00044         a = "";
00045         p = "";
00046         nt = "";
00047         sb = 0.0;
00048     _sLastTab="";
00049     _first=-1;
00050     _last=-1;
00051 
00052         tl = new TranInfoList();
00053 }
00054 
00055 
00056 // --- CBInfo -----------------------------------------------------------------
00057 CBInfo::CBInfo( const QString &name, const QString &filename )
00058 {
00059         Config config( filename, Config::File );
00060         config.setGroup( "Account" );
00061 
00062         n = name;
00063         fn = filename;
00064         pw = config.readEntryCrypt( "Password", QString::null );
00065 
00066         t = config.readEntry( "Type" );
00067         bn = config.readEntry( "Bank", "" );
00068         a = config.readEntryCrypt( "Number", "" );
00069         p = config.readEntryCrypt( "PINNumber", "" );
00070         nt = config.readEntry( "Notes", "" );
00071     _sLastTab = config.readEntry("LastTab", "");
00072     _first=config.readNumEntry("First", -1);
00073     _sSortOrder = config.readEntry( "SortOrder", QWidget::tr("Date") );
00074 
00075         bool ok;
00076         sb = config.readEntry( "Balance", "0.0" ).toFloat( &ok );
00077 
00078         loadTransactions();
00079 }
00080 
00081 // --- balance ----------------------------------------------------------------
00082 float CBInfo::balance()
00083 {
00084         calcBalance();
00085         return b;
00086 }
00087 
00088 // --- write ------------------------------------------------------------------
00089 void CBInfo::write()
00090 {
00091         QFile f( fn );
00092         if ( f.exists() )
00093                 f.remove();
00094 
00095         Config *config = new Config(fn, Config::File);
00096 
00097 
00098     // fix transaction numbers
00099     _first=-1;
00100     TranInfo *prev=NULL;
00101     for ( TranInfo *tran = tl->first(); tran; tran = tl->next() ) {
00102         if( _first<0 ) _first=tran->id();
00103         if( prev ) prev->setNext( tran->id() );
00104         tran->setNext(-1);
00105         prev=tran;
00106     }
00107 
00108         // Save transactions
00109     for ( TranInfo *tran = tl->first(); tran; tran = tl->next() ) {
00110         tran->write(config);
00111     }
00112 
00113     // Save info
00114     if( _first<0 && _last>=0 ) _first=_last;
00115         config->setGroup( "Account" );
00116         config->writeEntryCrypt( "Password", pw );
00117         config->writeEntry( "Type", t );
00118         config->writeEntry( "Bank", bn );
00119         config->writeEntryCrypt( "Number", a );
00120         config->writeEntryCrypt( "PINNumber", p );
00121         config->writeEntry( "Notes", nt );
00122     config->writeEntry( "LastTab", _sLastTab );
00123         QString balstr;
00124         balstr.setNum( sb, 'f', 2 );
00125         config->writeEntry( "Balance", balstr );
00126     config->writeEntry( "First", _first );
00127     config->writeEntry( "SortOrder", _sSortOrder );
00128 
00129     config->write();
00130         delete config;
00131 }
00132 
00133 
00134 // --- findTransaction --------------------------------------------------------
00135 TranInfo *CBInfo::findTransaction( const QString &sId )
00136 {
00137     bool bOk;
00138     int id=sId.toInt( &bOk );
00139     if( !bOk )
00140         return(false);
00141     TranInfo *traninfo;
00142         for(traninfo=tl->first(); traninfo; traninfo=tl->next()) {
00143         if( traninfo->id() == id )
00144             break;
00145     }
00146     return(traninfo);
00147 }
00148 
00149 void CBInfo::addTransaction( TranInfo *tran )
00150 {
00151     tl->append( tran );
00152         calcBalance();
00153 }
00154 
00155 void CBInfo::removeTransaction( TranInfo *tran )
00156 {
00157     tl->removeRef( tran );
00158     delete tran;
00159         calcBalance();
00160 }
00161 
00162 
00163 // --- loadTransactions -------------------------------------------------------
00164 // Reads the transactions. Either the old way 1-n or as linked list.
00165 void CBInfo::loadTransactions()
00166 {
00167         TranInfo *tran;
00168         QString trandesc = "";
00169 
00170         tl = new TranInfoList();
00171 
00172         Config config( fn, Config::File );
00173     int i=_first;
00174     bool bOld=false;
00175     if( i==-1 ) {
00176         i=1;
00177         bOld=true;
00178     }
00179     while( i>=0 ) {
00180         _last=i;
00181         tran=new TranInfo(&config, i);
00182         trandesc = tran->desc();
00183         if( trandesc==QString::null ) {
00184             delete tran;
00185             break;
00186         }
00187         tl->append(tran);
00188         i= bOld ? i+1 : tran->getNext();
00189     }
00190 
00191     calcBalance();
00192 }
00193 
00194 
00195 // --- calcBalance ------------------------------------------------------------
00196 void CBInfo::calcBalance()
00197 {
00198         float amount;
00199 
00200         b = sb;
00201 
00202         for ( TranInfo *tran = tl->first(); tran; tran = tl->next() )
00203         {
00204                 b -= tran->fee();
00205                 amount = tran->amount();
00206                 if ( tran->withdrawal() )
00207                 {
00208                         amount *= -1;
00209                 }
00210                 b += amount;
00211         }
00212 }
00213 
00214 
00215 int CBInfoList::compareItems( QCollection::Item item1, QCollection::Item item2 )
00216 {
00217         QString n1 = ((CBInfo *)item1)->name();
00218         QString n2 = ((CBInfo *)item2)->name();
00219         int r = -1;
00220 
00221         if ( n1 < n2 )
00222                 r = -1;
00223         else if ( n1 == n2 )
00224                 r = 0;
00225         else if ( n1 > n2 )
00226                 r = 1;
00227         return( r );
00228 }

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