summaryrefslogtreecommitdiff
path: root/noncore/unsupported/qashmoney/preferences.cpp
Unidiff
Diffstat (limited to 'noncore/unsupported/qashmoney/preferences.cpp') (more/less context) (ignore whitespace changes)
-rwxr-xr-xnoncore/unsupported/qashmoney/preferences.cpp352
1 files changed, 352 insertions, 0 deletions
diff --git a/noncore/unsupported/qashmoney/preferences.cpp b/noncore/unsupported/qashmoney/preferences.cpp
new file mode 100755
index 0000000..880807a
--- a/dev/null
+++ b/noncore/unsupported/qashmoney/preferences.cpp
@@ -0,0 +1,352 @@
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 ] ) != 7 )
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 // limit number of transactions to 0 = 14 days 1 = 30 days, 2 = 90 days, 3 = 180 days, 4 = 365 days 5 = all
64 rows = 0;
65 sqlite_get_table ( db, "select preference from preferences where id = 7;", &results, &rows, &columns, 0 );
66 if ( rows == 0 )
67 sqlite_exec ( db, "insert into preferences values ( 0, 'limittransactions', 0, 0, 0, NULL );", 0, 0, 0 );
68 }
69 }
70
71void Preferences::initializeColumnPreferences ()
72 {
73 int rows = 0;
74 int columns = 0;
75 char **results;
76
77 // initialize accountname column width
78 sqlite_get_table ( db, "select width from columns where id = 1;", &results, &rows, &columns, 0 );
79 if ( rows == 0 )
80 sqlite_exec ( db, "insert into columns values ( 'accountname', 90, 0, 0, 0, NULL );", 0, 0, 0 );
81
82 // initialize accountbalance column width
83 sqlite_get_table ( db, "select width from columns where id = 2;", &results, &rows, &columns, 0 );
84 if ( rows == 0 )
85 sqlite_exec ( db, "insert into columns values ( 'accountbalance', 90, 0, 0, 0, NULL );", 0, 0, 0 );
86
87 // initialize transactiondate column width
88 sqlite_get_table ( db, "select width from columns where id = 3;", &results, &rows, &columns, 0 );
89 if ( rows == 0 )
90 sqlite_exec ( db, "insert into columns values ( 'normaltransactiondate', 50, 0, 0, 0, NULL );", 0, 0, 0 );
91
92 // initialize transactionname column width
93 sqlite_get_table ( db, "select width from columns where id = 4;", &results, &rows, &columns, 0 );
94 if ( rows == 0 )
95 sqlite_exec ( db, "insert into columns values ( 'normaltransactionname', 75, 0, 0, 0, NULL );", 0, 0, 0 );
96
97 // initialize transactionamount column width
98 sqlite_get_table ( db, "select width from columns where id = 5;", &results, &rows, &columns, 0 );
99 if ( rows == 0 )
100 sqlite_exec ( db, "insert into columns values ( 'normaltransactionamount', 50, 0, 0, 0, NULL );", 0, 0, 0 );
101
102 // initialize transactiondate column width
103 sqlite_get_table ( db, "select width from columns where id = 6;", &results, &rows, &columns, 0 );
104 if ( rows == 0 )
105 sqlite_exec ( db, "insert into columns values ( 'extendedtransactiondate', 50, 0, 0, 0, NULL );", 0, 0, 0 );
106
107 // initialize transactionname column width
108 sqlite_get_table ( db, "select width from columns where id = 7;", &results, &rows, &columns, 0 );
109 if ( rows == 0 )
110 sqlite_exec ( db, "insert into columns values ( 'extendedtransactionname', 75, 0, 0, 0, NULL );", 0, 0, 0 );
111
112 // initialize transactionamount column width
113 sqlite_get_table ( db, "select width from columns where id = 8;", &results, &rows, &columns, 0 );
114 if ( rows == 0 )
115 sqlite_exec ( db, "insert into columns values ( 'extendedtransactionamount', 50, 0, 0, 0, NULL );", 0, 0, 0 );
116
117 // initialize transactionaccount column width
118 sqlite_get_table ( db, "select width from columns where id = 9;", &results, &rows, &columns, 0 );
119 if ( rows == 0 )
120 sqlite_exec ( db, "insert into columns values ( 'transactionaccount', 50, 0, 0, 0, NULL );", 0, 0, 0 );
121
122 // initialize account name with currency column width
123 sqlite_get_table ( db, "select width from columns where id = 10;", &results, &rows, &columns, 0 );
124 if ( rows == 0 )
125 sqlite_exec ( db, "insert into columns values ( 'accountnamewithcurrency', 100, 0, 0, 0, NULL );", 0, 0, 0 );
126
127 // initialize account currency column width
128 sqlite_get_table ( db, "select width from columns where id = 11;", &results, &rows, &columns, 0 );
129 if ( rows == 0 )
130 sqlite_exec ( db, "insert into columns values ( 'currencycolumn', 10, 0, 0, 0, NULL );", 0, 0, 0 );
131
132 // initialize account balance with currency column width
133 sqlite_get_table ( db, "select width from columns where id = 12;", &results, &rows, &columns, 0 );
134 if ( rows == 0 )
135 sqlite_exec ( db, "insert into columns values ( 'accountbalancewithcurrency', 50, 0, 0, 0, NULL );", 0, 0, 0 );
136
137 // initialize budget line item column
138 sqlite_get_table ( db, "select width from columns where id = 13;", &results, &rows, &columns, 0 );
139 if ( rows == 0 )
140 sqlite_exec ( db, "insert into columns values ( 'budgetlineitem', 50, 0, 0, 0, NULL );", 0, 0, 0 );
141
142 // initialize budget budget column
143 sqlite_get_table ( db, "select width from columns where id = 14;", &results, &rows, &columns, 0 );
144 if ( rows == 0 )
145 sqlite_exec ( db, "insert into columns values ( 'budgetbudget', 50, 0, 0, 0, NULL );", 0, 0, 0 );
146
147 // initialize budget actual column
148 sqlite_get_table ( db, "select width from columns where id = 15;", &results, &rows, &columns, 0 );
149 if ( rows == 0 )
150 sqlite_exec ( db, "insert into columns values ( 'budgetactual', 50, 0, 0, 0, NULL );", 0, 0, 0 );
151 }
152
153void Preferences::initializeSortingPreferences ()
154 {
155 int rows = 0;
156 int columns = 0;
157 char **results;
158
159 if ( sqlite_get_table ( db, "select count() from sorting;", 0, 0, 0, 0 ) != 0 )
160 sqlite_exec ( db, "create table sorting ( listbox, column, direction, id integer primary key );", 0, 0, 0 );
161
162 // initialize account listbox sorting. Set direction = 1 here so ascending sort is default.
163 sqlite_get_table ( db, "select column, direction from sorting where id = 1;", &results, &rows, &columns, 0 );
164 if ( rows == 0 )
165 sqlite_exec ( db, "insert into sorting values ( 'accounts', 0, 1, NULL );", 0, 0, 0 );
166
167 // initialize transaction listbox sorting
168 sqlite_get_table ( db, "select column, direction from sorting where id = 2;", &results, &rows, &columns, 0);
169 if ( rows == 0 )
170 sqlite_exec ( db, "insert into sorting values ( 'transactions', 0, 1, NULL );", 0, 0, 0 );
171
172 // initialize budgets listbox sorting
173 sqlite_get_table ( db, "select column, direction from sorting where id = 3;", & results, &rows, &columns, 0 );
174 if ( rows == 0 )
175 sqlite_exec ( db, "insert into sorting values ( 'budgets', 0, 1, NULL );", 0, 0, 0 );
176 }
177
178void Preferences::changeColumnPreference ( int id, int width )
179 {
180 sqlite_exec_printf ( db, "update columns set width = %i where id = %i;", 0, 0, 0, width, id );
181 }
182
183int Preferences::getColumnPreference ( int id )
184 {
185 char **results;
186 sqlite_get_table_printf ( db, "select width from columns where id = %i;", &results, NULL, NULL, NULL, id );
187 return atoi ( results [ 1 ] );
188 }
189
190void Preferences::changeSortingPreference ( int id, int column )
191 {
192 int pColumn = 0; // column setting coming from the prefs object
193 int pDirection = 0; // direction setting coming from the prefs object
194
195 // because there appears to be no way to query the QT header object directly for it's current sort settings, we have
196 // to maintain track of them ourselves. So start by pulling the current saved setting for this view.
197 getSortingPreference ( id, &pColumn, &pDirection );
198
199 // if the current saved column == the new column, then the user wants to toggle the sort order.
200 // otherwise we behave like QT does by default, which is to select the new column and default to an ascending sort.
201 if ( column == pColumn )
202 sqlite_exec_printf ( db, "update sorting set direction = %i where id = %i;", 0, 0, 0, !pDirection, id );
203 else
204 sqlite_exec_printf ( db, "update sorting set column = %i, direction = 1 where id = %i;", 0, 0, 0, column, id );
205 }
206
207void Preferences::getSortingPreference ( int id, int *column, int *direction )
208 {
209 char **results;
210 sqlite_get_table_printf ( db, "select column, direction from sorting where id = %i;", &results, NULL, NULL, NULL, id );
211 *column = atoi ( results [ 2 ] );
212 *direction = atoi ( results [ 3 ] );
213 }
214
215int Preferences::getPreference ( int id )
216 {
217 char **results;
218 sqlite_get_table_printf ( db, "select preference from preferences where id = %i;", &results, NULL, NULL, NULL, id );
219 return atoi ( results [ 1 ] );
220 }
221
222QString Preferences::getSeparator ( )
223 {
224 int s = getPreference ( 2 );
225 if ( s == 1 )
226 return "/";
227 if ( s ==2 )
228 return "-";
229 else
230 return ".";
231 }
232
233void Preferences::changePreference ( int id, int newpreference )
234 {
235 sqlite_exec_printf ( db, "update preferences set preference = %i where id = %i;", 0, 0, 0, newpreference, id );
236 }
237
238void Preferences::setDefaultDatePreferences ()
239 {
240 sqlite_exec ( db, "update preferences set preference = 4 where id = 1;", 0, 0, 0 );
241 sqlite_exec ( db, "update preferences set preference = 1 where id = 2;", 0, 0, 0 );
242 }
243
244QString Preferences::getDate ( int y, int m, int d )
245 {
246 QString date;
247 int format = getPreference ( 1 );
248 QString separator = getSeparator();
249
250 // Convert all date integers to QStrings
251 QString year = QString::number ( y );
252 QString month = QString::number ( m );
253 if ( m < 10 )
254 month.prepend ( "0" );
255 QString day = QString::number ( d );
256 if ( d < 10 )
257 day.prepend ( "0" );
258
259 // Truncate four digit year if necessary
260 if ( format == 2 || format == 4 || format == 6 || format == 8 )
261 year.remove ( 0, 2 );
262
263 // Concatenate dates as necessary
264 if ( format == 1 || format == 2 )
265 {
266 date = year;
267 date.append ( separator );
268 date.append ( month );
269 date.append ( separator );
270 date.append ( day );
271 return date;
272 }
273 if ( format == 3 || format == 4 )
274 {
275 date = month;
276 date.append ( separator );
277 date.append ( day );
278 date.append ( separator );
279 date.append ( year );
280 return date;
281 }
282 if ( format == 5 || format == 6 )
283 {
284 date = year;
285 date.append ( separator );
286 date.append ( day );
287 date.append ( separator );
288 date.append ( month );
289 return date;
290 }
291 if ( format == 7 || format == 8 )
292 {
293 date = day;
294 date.append ( separator );
295 date.append ( month );
296 date.append ( separator );
297 date.append ( year );
298 return date;
299 }
300 }
301
302QString Preferences::getDate ( int y, int m )
303 {
304 QString date;
305 int format = getPreference ( 1 );
306 QString separator = getSeparator();
307
308 // Convert all date integers to QStrings
309 QString year = QString::number ( y );
310 QString month = QString::number ( m );
311 if ( m < 10 )
312 month.prepend ( "0" );
313
314 // Truncate four digit year if necessary
315 if ( format == 2 || format == 4 || format == 6 || format == 8 )
316 year.remove ( 0, 2 );
317
318 // Concatenate dates as necessary
319 if ( format == 1 || format == 2 )
320 {
321 date = year;
322 date.append ( separator );
323 date.append ( month );
324 return date;
325 }
326 if ( format == 3 || format == 4 )
327 {
328 date = month;
329 date.append ( separator );
330 date.append ( year );
331 return date;
332 }
333 if ( format == 5 || format == 6 )
334 {
335 date = year;
336 date.append ( separator );
337 date.append ( month );
338 return date;
339 }
340 if ( format == 7 || format == 8 )
341 {
342 date.append ( month );
343 date.append ( separator );
344 date.append ( year );
345 return date;
346 }
347 }
348
349
350
351
352