summaryrefslogtreecommitdiff
path: root/noncore/apps/qashmoney/preferences.cpp
Unidiff
Diffstat (limited to 'noncore/apps/qashmoney/preferences.cpp') (more/less context) (ignore whitespace changes)
-rwxr-xr-xnoncore/apps/qashmoney/preferences.cpp296
1 files changed, 296 insertions, 0 deletions
diff --git a/noncore/apps/qashmoney/preferences.cpp b/noncore/apps/qashmoney/preferences.cpp
new file mode 100755
index 0000000..8783a47
--- a/dev/null
+++ b/noncore/apps/qashmoney/preferences.cpp
@@ -0,0 +1,296 @@
1#include "preferences.h"
2#include <stdlib.h>
3
4Preferences::Preferences ()
5 {
6 db = sqlite_open ( "qmpreferences.db", 0, NULL );
7 }
8
9Preferences::~Preferences ()
10 {
11 sqlite_close ( db );
12 }
13
14void Preferences::addPreferences ()
15 {
16 // This function checks the preferences database for existing preferences and adds
17 // them if they are not there. First we set up variables. Preferences are always set
18 // to non-zero numbers because when we check to see if a preference has been
19 // added to the database, the result is zero if it hasn't
20 int rows = 0;
21 int columns = 0;
22 char **results;
23
24 sqlite_get_table ( db, "select count() from preferences;", &results, 0, 0, 0 );
25 if ( atoi ( results [ 1 ] ) != 6 )
26 {
27 // dateformat preference 1 = yyyymmdd 2 = yymmdd 3 = mmddyyyy 4 = mmddyy
28 // 5 = yyyyddmm 6 = yyddmm 7 = ddmmyyyy 8 = ddmmyy
29 sqlite_get_table ( db, "select preference from preferences where id = 1;", &results, &rows, &columns, 0 );
30 if ( rows == 0 )
31 sqlite_exec ( db, "insert into preferences values ( 4, 'dateformat', 0, 0, 0, NULL );", 0, 0, 0 );
32
33 // dateseparator preference 1 = / ( forward slash ) 2 = - ( dash ) 3 = . ( period )
34 rows = 0;
35 sqlite_get_table ( db, "select preference from preferences where id = 2;", &results, &rows, &columns, 0 );
36 if ( rows == 0 )
37 sqlite_exec ( db, "insert into preferences values ( 1, 'dateseparator', 0, 0, 0, NULL );", 0, 0, 0 );
38
39 // showclearedtransactions preference 0 = no 1 = yes
40 rows = 0;
41 sqlite_get_table ( db, "select preference from preferences where id = 3;", &results, &rows, &columns, 0 );
42 if ( rows == 0 )
43 sqlite_exec ( db, "insert into preferences values ( 0, 'showclearedtransactions', 0, 0, 0, NULL );", 0, 0, 0 );
44
45 // enable currency support preference 0 = no 1 = yes
46 rows = 0;
47 sqlite_get_table ( db, "select preference from preferences where id = 4;", &results, &rows, &columns, 0 );
48 if ( rows == 0 )
49 sqlite_exec ( db, "insert into preferences values ( 0, 'enablecurrencysupport', 0, 0, 0, NULL );", 0, 0, 0 );
50
51 // one touch account viewing preference 0 = no 1 = yes
52 rows = 0;
53 sqlite_get_table ( db, "select preference from preferences where id = 5;", &results, &rows, &columns, 0 );
54 if ( rows == 0 )
55 sqlite_exec ( db, "insert into preferences values ( 0, 'onetouchviewing', 0, 0, 0, NULL );", 0, 0, 0 );
56
57 // exclude transfers from limit view 0 = no 1 = yes
58 rows = 0;
59 sqlite_get_table ( db, "select preference from preferences where id = 6;", &results, &rows, &columns, 0 );
60 if ( rows == 0 )
61 sqlite_exec ( db, "insert into preferences values ( 0, 'excludetransfersfromlimit', 0, 0, 0, NULL );", 0, 0, 0 );
62 }
63 }
64
65void Preferences::initializeColumnPreferences ()
66 {
67 int rows = 0;
68 int columns = 0;
69 char **results;
70
71 // initialize accountname column width
72 sqlite_get_table ( db, "select width from columns where id = 1;", &results, &rows, &columns, 0 );
73 if ( rows == 0 )
74 sqlite_exec ( db, "insert into columns values ( 'accountname', 90, 0, 0, 0, NULL );", 0, 0, 0 );
75
76 // initialize accountbalance column width
77 sqlite_get_table ( db, "select width from columns where id = 2;", &results, &rows, &columns, 0 );
78 if ( rows == 0 )
79 sqlite_exec ( db, "insert into columns values ( 'accountbalance', 90, 0, 0, 0, NULL );", 0, 0, 0 );
80
81 // initialize transactiondate column width
82 sqlite_get_table ( db, "select width from columns where id = 3;", &results, &rows, &columns, 0 );
83 if ( rows == 0 )
84 sqlite_exec ( db, "insert into columns values ( 'normaltransactiondate', 50, 0, 0, 0, NULL );", 0, 0, 0 );
85
86 // initialize transactionname column width
87 sqlite_get_table ( db, "select width from columns where id = 4;", &results, &rows, &columns, 0 );
88 if ( rows == 0 )
89 sqlite_exec ( db, "insert into columns values ( 'normaltransactionname', 75, 0, 0, 0, NULL );", 0, 0, 0 );
90
91 // initialize transactionamount column width
92 sqlite_get_table ( db, "select width from columns where id = 5;", &results, &rows, &columns, 0 );
93 if ( rows == 0 )
94 sqlite_exec ( db, "insert into columns values ( 'normaltransactionamount', 50, 0, 0, 0, NULL );", 0, 0, 0 );
95
96 // initialize transactiondate column width
97 sqlite_get_table ( db, "select width from columns where id = 6;", &results, &rows, &columns, 0 );
98 if ( rows == 0 )
99 sqlite_exec ( db, "insert into columns values ( 'extendedtransactiondate', 50, 0, 0, 0, NULL );", 0, 0, 0 );
100
101 // initialize transactionname column width
102 sqlite_get_table ( db, "select width from columns where id = 7;", &results, &rows, &columns, 0 );
103 if ( rows == 0 )
104 sqlite_exec ( db, "insert into columns values ( 'extendedtransactionname', 75, 0, 0, 0, NULL );", 0, 0, 0 );
105
106 // initialize transactionamount column width
107 sqlite_get_table ( db, "select width from columns where id = 8;", &results, &rows, &columns, 0 );
108 if ( rows == 0 )
109 sqlite_exec ( db, "insert into columns values ( 'extendedtransactionamount', 50, 0, 0, 0, NULL );", 0, 0, 0 );
110
111 // initialize transactionaccount column width
112 sqlite_get_table ( db, "select width from columns where id = 9;", &results, &rows, &columns, 0 );
113 if ( rows == 0 )
114 sqlite_exec ( db, "insert into columns values ( 'transactionaccount', 50, 0, 0, 0, NULL );", 0, 0, 0 );
115
116 // initialize account name with currency column width
117 sqlite_get_table ( db, "select width from columns where id = 10;", &results, &rows, &columns, 0 );
118 if ( rows == 0 )
119 sqlite_exec ( db, "insert into columns values ( 'accountnamewithcurrency', 100, 0, 0, 0, NULL );", 0, 0, 0 );
120
121 // initialize account currency column width
122 sqlite_get_table ( db, "select width from columns where id = 11;", &results, &rows, &columns, 0 );
123 if ( rows == 0 )
124 sqlite_exec ( db, "insert into columns values ( 'currencycolumn', 10, 0, 0, 0, NULL );", 0, 0, 0 );
125
126 // initialize account balance with currency column width
127 sqlite_get_table ( db, "select width from columns where id = 12;", &results, &rows, &columns, 0 );
128 if ( rows == 0 )
129 sqlite_exec ( db, "insert into columns values ( 'accountbalancewithcurrency', 50, 0, 0, 0, NULL );", 0, 0, 0 );
130
131 // initialize budget line item column
132 sqlite_get_table ( db, "select width from columns where id = 13;", &results, &rows, &columns, 0 );
133 if ( rows == 0 )
134 sqlite_exec ( db, "insert into columns values ( 'budgetlineitem', 50, 0, 0, 0, NULL );", 0, 0, 0 );
135
136 // initialize budget budget column
137 sqlite_get_table ( db, "select width from columns where id = 14;", &results, &rows, &columns, 0 );
138 if ( rows == 0 )
139 sqlite_exec ( db, "insert into columns values ( 'budgetbudget', 50, 0, 0, 0, NULL );", 0, 0, 0 );
140
141 // initialize budget actual column
142 sqlite_get_table ( db, "select width from columns where id = 15;", &results, &rows, &columns, 0 );
143 if ( rows == 0 )
144 sqlite_exec ( db, "insert into columns values ( 'budgetactual', 50, 0, 0, 0, NULL );", 0, 0, 0 );
145 }
146
147void Preferences::changeColumnPreference ( int id, int width )
148 {
149 sqlite_exec_printf ( db, "update columns set width = %i where id = %i;", 0, 0, 0, width, id );
150 }
151
152int Preferences::getColumnPreference ( int id )
153 {
154 char **results;
155 sqlite_get_table_printf ( db, "select width from columns where id = %i;", &results, NULL, NULL, NULL, id );
156 return atoi ( results [ 1 ] );
157 }
158
159int Preferences::getPreference ( int id )
160 {
161 char **results;
162 sqlite_get_table_printf ( db, "select preference from preferences where id = %i;", &results, NULL, NULL, NULL, id );
163 return atoi ( results [ 1 ] );
164 }
165
166QString Preferences::getSeparator ( )
167 {
168 int s = getPreference ( 2 );
169 if ( s == 1 )
170 return "/";
171 if ( s ==2 )
172 return "-";
173 else
174 return ".";
175 }
176
177void Preferences::changePreference ( int id, int newpreference )
178 {
179 sqlite_exec_printf ( db, "update preferences set preference = %i where id = %i;", 0, 0, 0, newpreference, id );
180 }
181
182void Preferences::setDefaultDatePreferences ()
183 {
184 sqlite_exec ( db, "update preferences set preference = 4 where id = 1;", 0, 0, 0 );
185 sqlite_exec ( db, "update preferences set preference = 1 where id = 2;", 0, 0, 0 );
186 }
187
188QString Preferences::getDate ( int y, int m, int d )
189 {
190 QString date;
191 int format = getPreference ( 1 );
192 QString separator = getSeparator();
193
194 // Convert all date integers to QStrings
195 QString year = QString::number ( y );
196 QString month = QString::number ( m );
197 if ( m < 10 )
198 month.prepend ( "0" );
199 QString day = QString::number ( d );
200 if ( d < 10 )
201 day.prepend ( "0" );
202
203 // Truncate four digit year if necessary
204 if ( format == 2 || format == 4 || format == 6 || format == 8 )
205 year.remove ( 0, 2 );
206
207 // Concatenate dates as necessary
208 if ( format == 1 || format == 2 )
209 {
210 date = year;
211 date.append ( separator );
212 date.append ( month );
213 date.append ( separator );
214 date.append ( day );
215 return date;
216 }
217 if ( format == 3 || format == 4 )
218 {
219 date = month;
220 date.append ( separator );
221 date.append ( day );
222 date.append ( separator );
223 date.append ( year );
224 return date;
225 }
226 if ( format == 5 || format == 6 )
227 {
228 date = year;
229 date.append ( separator );
230 date.append ( day );
231 date.append ( separator );
232 date.append ( month );
233 return date;
234 }
235 if ( format == 7 || format == 8 )
236 {
237 date = day;
238 date.append ( separator );
239 date.append ( month );
240 date.append ( separator );
241 date.append ( year );
242 return date;
243 }
244 }
245
246QString Preferences::getDate ( int y, int m )
247 {
248 QString date;
249 int format = getPreference ( 1 );
250 QString separator = getSeparator();
251
252 // Convert all date integers to QStrings
253 QString year = QString::number ( y );
254 QString month = QString::number ( m );
255 if ( m < 10 )
256 month.prepend ( "0" );
257
258 // Truncate four digit year if necessary
259 if ( format == 2 || format == 4 || format == 6 || format == 8 )
260 year.remove ( 0, 2 );
261
262 // Concatenate dates as necessary
263 if ( format == 1 || format == 2 )
264 {
265 date = year;
266 date.append ( separator );
267 date.append ( month );
268 return date;
269 }
270 if ( format == 3 || format == 4 )
271 {
272 date = month;
273 date.append ( separator );
274 date.append ( year );
275 return date;
276 }
277 if ( format == 5 || format == 6 )
278 {
279 date = year;
280 date.append ( separator );
281 date.append ( month );
282 return date;
283 }
284 if ( format == 7 || format == 8 )
285 {
286 date.append ( month );
287 date.append ( separator );
288 date.append ( year );
289 return date;
290 }
291 }
292
293
294
295
296