00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "cbinfo.h"
00030 #include "traninfo.h"
00031
00032 #include <qpe/config.h>
00033
00034 #include <qfile.h>
00035
00036
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
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
00082 float CBInfo::balance()
00083 {
00084 calcBalance();
00085 return b;
00086 }
00087
00088
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
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
00109 for ( TranInfo *tran = tl->first(); tran; tran = tl->next() ) {
00110 tran->write(config);
00111 }
00112
00113
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
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
00164
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
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 }