summaryrefslogtreecommitdiff
path: root/noncore/apps/checkbook/cbinfo.cpp
Unidiff
Diffstat (limited to 'noncore/apps/checkbook/cbinfo.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/checkbook/cbinfo.cpp196
1 files changed, 196 insertions, 0 deletions
diff --git a/noncore/apps/checkbook/cbinfo.cpp b/noncore/apps/checkbook/cbinfo.cpp
new file mode 100644
index 0000000..3a39317
--- a/dev/null
+++ b/noncore/apps/checkbook/cbinfo.cpp
@@ -0,0 +1,196 @@
1/*
2                This file is part of the OPIE Project
3 =.
4             .=l. Copyright (c) 2002 Dan Williams <drw@handhelds.org>
5           .>+-=
6 _;:,     .>    :=|. This file is free software; you can
7.> <`_,   >  .   <= redistribute it and/or modify it under
8:`=1 )Y*s>-.--   : the terms of the GNU General Public
9.="- .-=="i,     .._ License as published by the Free Software
10 - .   .-<_>     .<> Foundation; either version 2 of the License,
11     ._= =}       : or (at your option) any later version.
12    .%`+i>       _;_.
13    .i_,=:_.      -<s. This file is distributed in the hope that
14     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
15    : ..    .:,     . . . without even the implied warranty of
16    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
17  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU General
18..}^=.=       =       ; Public License for more details.
19++=   -.     .`     .:
20 :     =  ...= . :.=- You should have received a copy of the GNU
21 -.   .:....=;==+<; General Public License along with this file;
22  -_. . .   )=.  = see the file COPYING. If not, write to the
23    --        :-=` Free Software Foundation, Inc.,
24 59 Temple Place - Suite 330,
25 Boston, MA 02111-1307, USA.
26
27*/
28
29#include "cbinfo.h"
30#include "traninfo.h"
31
32#include <qpe/config.h>
33
34#include <qfile.h>
35
36CBInfo::CBInfo()
37{
38 n = "";
39 fn = "";
40 pw = QString::null;
41 t = "";
42 bn = "";
43 a = "";
44 p = "";
45 nt = "";
46 sb = 0.0;
47
48 tl = new TranInfoList();
49}
50
51CBInfo::CBInfo( const QString &name, const QString &filename )
52{
53 Config config( filename, Config::File );
54 config.setGroup( "Account" );
55
56 n = name;
57 fn = filename;
58 pw = config.readEntryCrypt( "Password", QString::null );
59
60 t = config.readEntry( "Type" );
61 bn = config.readEntry( "Bank", "" );
62 a = config.readEntryCrypt( "Number", "" );
63 p = config.readEntryCrypt( "PINNumber", "" );
64 nt = config.readEntry( "Notes", "" );
65
66 bool ok;
67 sb = config.readEntry( "Balance", "0.0" ).toFloat( &ok );
68
69 loadTransactions();
70}
71
72float CBInfo::balance()
73{
74 calcBalance();
75 return b;
76}
77
78void CBInfo::write()
79{
80 QFile f( fn );
81 if ( f.exists() )
82 {
83 f.remove();
84 }
85
86 Config *config = new Config(fn, Config::File);
87
88 // Save info
89 config->setGroup( "Account" );
90 config->writeEntryCrypt( "Password", pw );
91 config->writeEntry( "Type", t );
92 config->writeEntry( "Bank", bn );
93 config->writeEntryCrypt( "Number", a );
94 config->writeEntryCrypt( "PINNumber", p );
95 config->writeEntry( "Notes", n );
96 QString balstr;
97 balstr.setNum( sb, 'f', 2 );
98 config->writeEntry( "Balance", balstr );
99
100 // Save transactions
101 int i = 1;
102 for ( TranInfo *tran = tl->first(); tran; tran = tl->next() )
103 {
104 tran->write( config, i );
105 i++;
106 }
107 config->write();
108
109 delete config;
110}
111
112TranInfo *CBInfo::findTransaction( const QString &checknum, const QString &date,
113 const QString &desc )
114{
115 TranInfo *traninfo = tl->first();
116 while ( traninfo )
117 {
118 if ( traninfo->number() == checknum && traninfo->datestr() == date &&
119 traninfo->desc() == desc )
120 break;
121 traninfo = tl->next();
122 }
123 return( traninfo );
124}
125
126void CBInfo::addTransaction( TranInfo *tran )
127{
128 tl->inSort( tran );
129 calcBalance();
130}
131
132void CBInfo::removeTransaction( TranInfo *tran )
133{
134 tl->remove( tran );
135 delete tran;
136 calcBalance();
137}
138
139void CBInfo::loadTransactions()
140{
141 TranInfo *tran;
142 QString trandesc = "";
143
144 tl = new TranInfoList();
145
146 Config config( fn, Config::File );
147
148 for ( int i = 1; trandesc != QString::null; i++ )
149 {
150 tran = new TranInfo( config, i );
151 trandesc = tran->desc();
152 if ( trandesc != QString::null )
153 {
154 tl->inSort( tran );
155 }
156 else
157 {
158 delete tran;
159 }
160 }
161
162 calcBalance();
163}
164
165void CBInfo::calcBalance()
166{
167 float amount;
168
169 b = sb;
170
171 for ( TranInfo *tran = tl->first(); tran; tran = tl->next() )
172 {
173 b -= tran->fee();
174 amount = tran->amount();
175 if ( tran->withdrawal() )
176 {
177 amount *= -1;
178 }
179 b += amount;
180 }
181}
182
183int CBInfoList::compareItems( QCollection::Item item1, QCollection::Item item2 )
184{
185 QString n1 = ((CBInfo *)item1)->name();
186 QString n2 = ((CBInfo *)item2)->name();
187 int r = -1;
188
189 if ( n1 < n2 )
190 r = -1;
191 else if ( n1 == n2 )
192 r = 0;
193 else if ( n1 > n2 )
194 r = 1;
195 return( r );
196}