~igor-cali/bilancio/stable

« back to all changes in this revision

Viewing changes to src/omb33core.cpp

  • Committer: Igor Calì
  • Date: 2019-04-07 16:24:48 UTC
  • Revision ID: igor.cali0@gmail.com-20190407162448-5mfwr7c39y3kzz6u
 v3.3.1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************
 
2
 * Name:      omb33core.cpp
 
3
 * Purpose:   Core Code for OpenMoneyBox Application Class
 
4
 * Author:    Igor Calì (igor.cali0@gmail.com)
 
5
 * Created:   2019-04-02
 
6
 * Copyright: Igor Calì (igor.cali0@gmail.com)
 
7
 * License:             GNU
 
8
 **************************************************************/
 
9
 
 
10
#ifndef OMB33CORE_CPP_INCLUDED
 
11
#define OMB33CORE_CPP_INCLUDED
 
12
 
 
13
#ifdef _OMB_USE_CIPHER
 
14
        #include <exception>
 
15
        using namespace std;
 
16
        #ifndef __OMBCONVERT_BIN__
 
17
                #include "ui/password.h"
 
18
        #endif // __OMBCONVERT_BIN__
 
19
        #include "wxsqlite3.h"
 
20
#endif // _OMB_USE_CIPHER
 
21
 
 
22
#include <wx/filename.h>
 
23
#include <wx/textfile.h>
 
24
 
 
25
#ifndef __OMBCONVERT_BIN__
 
26
        #include <wx/gauge.h>
 
27
#endif
 
28
 
 
29
#include "productversion.h"
 
30
#include "omb33core.h"
 
31
 
 
32
#ifdef __OPENMONEYBOX_EXE__
 
33
        #include "openmoneybox/ui/main_wx.h"
 
34
#endif
 
35
 
 
36
#ifndef __WXMSW__
 
37
        extern wxLanguage Lan;
 
38
#endif // __WXMSW__
 
39
 
 
40
#ifdef __OPENMONEYBOX_EXE__
 
41
        extern "C" ombMainFrame *frame;
 
42
#endif // __OPENMONEYBOX_EXE__
 
43
 
 
44
// db structure
 
45
extern wxString cs_information;
 
46
extern wxString cs_funds;
 
47
extern wxString cs_credits;
 
48
extern wxString cs_debts;
 
49
extern wxString cs_loans;
 
50
extern wxString cs_borrows;
 
51
//extern wxString cs_shoplist;
 
52
extern wxString cs_transactions;
 
53
extern wxString cs_categories;
 
54
extern wxString cs_funds_master;
 
55
extern wxString cs_credits_master;
 
56
extern wxString cs_debts_master;
 
57
extern wxString cs_loans_master;
 
58
extern wxString cs_borrows_master;
 
59
extern wxString cs_categories_master;
 
60
 
 
61
#if (! wxCHECK_VERSION(3, 0, 0) )
 
62
        extern wxLanguage Lan;
 
63
#endif
 
64
 
 
65
#ifdef _OMB_USE_CIPHER
 
66
        extern bool TableExists(const wxString& tableName, const wxString& databaseName, sqlite3 *m_db);
 
67
        extern int ExecuteUpdate(sqlite3 *m_db, const char* sql/*, bool saveRC = false*/);
 
68
        extern wxSQLite3Table GetTable(sqlite3 *m_db, const char* sql);
 
69
#endif // _OMB_USE_CIPHER
 
70
 
 
71
#ifndef __OMBCONVERT_BIN__
 
72
        TData_v32::TData_v32(wxGauge *ProgressBar):TData(ProgressBar){
 
73
#else
 
74
        TData_v32::TData_v32(void){
 
75
#endif // __OMBCONVERT_BIN__
 
76
 
 
77
        #ifdef _OMB_USE_CIPHER
 
78
                IsEncrypted = IsEncrypted_master = false;
 
79
        #endif // _OMB_USE_CIPHER
 
80
 
 
81
        Credits = new TVal_v32[MAX_CREDITS];
 
82
        Debts = new TVal_v32[MAX_DEBTS];
 
83
        Lent = new TObj_v32[MAX_LENT];
 
84
        Borrowed = new TObj_v32[MAX_BORROWED];
 
85
        Lines = new TLine_v32[MAX_LINES];
 
86
 
 
87
        Initialize(true);
 
88
}
 
89
 
 
90
void TData_v32::Initialize(bool Creating){
 
91
  // Report initialization
 
92
  Lines[0].IsDate=false;
 
93
  Lines[0].Date=wxInvalidDateTime;
 
94
  Lines[0].Time=wxInvalidDateTime;
 
95
  Lines[0].Type=toNULL;
 
96
  Lines[0].Value=wxEmptyString;
 
97
  Lines[0].Reason=wxEmptyString;
 
98
        Lines[0].CategoryIndex = -1;
 
99
        Lines[0].ContactIndex = -1;
 
100
}
 
101
 
 
102
void TData_v32::ParseDatabase(void){
 
103
        Parsing = true;
 
104
 
 
105
        #ifdef _OMB_USE_CIPHER
 
106
                bool is_date;
 
107
                int Rows, id, i;        // Rows: number of rows in the query table
 
108
                                                                                        // id: id of table item
 
109
                long ind,                                       // ind: category index of transaction
 
110
                        contact_in;     // contact_in: contact index of transaction or object
 
111
 
 
112
                double val,
 
113
                        lat, lon;
 
114
                TOpType type;
 
115
                wxString str, reason;
 
116
                wxDateTime date, time;
 
117
                wxSQLite3Table Table;
 
118
 
 
119
                // Read Funds
 
120
                if(TableExists(L"Funds", wxEmptyString, database)){
 
121
                        Table = GetTable(database, "select * from Funds order by name;");
 
122
                        NFun = 0;
 
123
                        Tot_Funds = 0;
 
124
                        Rows = Table.GetRowCount();
 
125
                        for (i = 0; i < Rows; i++){
 
126
                                Table.SetRow(i);
 
127
                                id = Table.GetInt(0, 0);
 
128
                                str = Table.GetString(1, wxEmptyString);
 
129
                                val = Table.GetDouble(2, 0);
 
130
                                if(AddValue(tvFou, id, str, val)){
 
131
                                        NFun++;
 
132
                                        Tot_Funds += val;}}}
 
133
 
 
134
                // Read default fund
 
135
                #ifdef __OPENSUSE__
 
136
      wxString Sql = L"select data from Information where id = " +
 
137
                                                                                                                                wxString::Format(L"%d", dbMeta_default_fund) +
 
138
                                                                                                                                L";";
 
139
      Table = GetTable(database, Sql.c_str());
 
140
                #else
 
141
      Table = GetTable(database, L"select data from Information where id = " +
 
142
                                                                                                                                wxString::Format(L"%d", dbMeta_default_fund) +
 
143
                                                                                                                                L";");
 
144
                #endif // __OPENSUSE__
 
145
                Table.SetRow(0);
 
146
                FileData.DefFund = Table.GetString(0, wxEmptyString);
 
147
 
 
148
                // Read Credits
 
149
                if(TableExists(L"Credits", wxEmptyString, database)){
 
150
                        Table = GetTable(database, "select * from Credits order by name;");
 
151
                        NCre = 0;
 
152
                        Tot_Credits = 0;
 
153
                        Rows = Table.GetRowCount();
 
154
                        for (i = 0; i < Rows; i++){
 
155
                                Table.SetRow(i);
 
156
                                id = Table.GetInt(0, 0);
 
157
                                str = Table.GetString(1, wxEmptyString);
 
158
                                val = Table.GetDouble(2, 0);
 
159
                                contact_in = Table.GetInt(3, -1);
 
160
                                if(AddValue(tvCre, id, str, val, contact_in)){
 
161
                                        NCre++;
 
162
                                        Tot_Credits += val;}}}
 
163
 
 
164
                // Read Debts
 
165
                if(TableExists(L"Debts", wxEmptyString, database)){
 
166
                        Table = GetTable(database, "select * from Debts order by name;");
 
167
                        NDeb = 0;
 
168
                        Tot_Debts = 0;
 
169
                        Rows = Table.GetRowCount();
 
170
                        for (i = 0; i < Rows; i++){
 
171
                                Table.SetRow(i);
 
172
                                id = Table.GetInt(0, 0);
 
173
                                str = Table.GetString(1, wxEmptyString);
 
174
                                val = Table.GetDouble(2, 0);
 
175
                                contact_in = Table.GetInt(3, -1);
 
176
                                if(AddValue(tvDeb, id, str, val, contact_in)){
 
177
                                        NDeb++;
 
178
                                        Tot_Debts += val;}}}
 
179
 
 
180
                // Read Loans
 
181
                if(TableExists(L"Loans", wxEmptyString, database)){
 
182
                        Table = GetTable(database, "select * from Loans order by name;");
 
183
                        NLen = 0;
 
184
                        Rows = Table.GetRowCount();
 
185
                        for (i = 0; i < Rows; i++){
 
186
                                Table.SetRow(i);
 
187
                                id = Table.GetInt(0, 0);
 
188
                                str = Table.GetString(1, wxEmptyString);
 
189
                                reason = Table.GetString(2, wxEmptyString);
 
190
                                ind = Table.GetInt(3, 0);
 
191
                                if(ind == -1) date = wxDateTime::Today().Add(wxDateSpan(100, 0, 0, 0));
 
192
                                else date = wxDateTime((time_t) ind);
 
193
                                contact_in = Table.GetInt(4, -1);
 
194
                                if(AddObject(toPre, id, str, reason, date, contact_in)) NLen++;}}
 
195
 
 
196
                // Read Borrows
 
197
                if(TableExists(L"Borrows", wxEmptyString, database)){
 
198
                        Table = GetTable(database, "select * from Borrows order by name;");
 
199
                        NBor = 0;
 
200
                        Rows = Table.GetRowCount();
 
201
                        for (i = 0; i < Rows; i++){
 
202
                                Table.SetRow(i);
 
203
                                id = Table.GetInt(0, 0);
 
204
                                str = Table.GetString(1, wxEmptyString);
 
205
                                reason = Table.GetString(2, wxEmptyString);
 
206
                                ind = Table.GetInt(3, 0);
 
207
                                if(ind == -1) date = wxDateTime::Today().Add(wxDateSpan(100, 0, 0, 0));
 
208
                                else date = wxDateTime((time_t) ind);
 
209
                                contact_in = Table.GetInt(4, -1);
 
210
                                if(AddObject(toInP, id, str, reason, date, contact_in)) NBor++;}}
 
211
 
 
212
                // Read Categories
 
213
                Table = /*database->*/GetTable(database, "select * from Categories order by name;");
 
214
                NCat = 0;
 
215
                Rows = Table.GetRowCount();
 
216
                for (i = 0; i < Rows; i++){
 
217
                        Table.SetRow(i) ;
 
218
                        id = Table.GetInt(0, 0);
 
219
                        str = Table.GetString(1, wxEmptyString);
 
220
                        is_date = Table.GetInt(2, 0);
 
221
                        if(is_date)AddCategory(id, str);}
 
222
 
 
223
                // Read Transactions
 
224
                wxDateTime LastDate = wxDateTime(1, wxDateTime::Jan, 1970);
 
225
                Table = /*database->*/GetTable(database, "select * from Transactions order by date;");
 
226
                NLin = 0;
 
227
                Rows = Table.GetRowCount();
 
228
                if(Rows){
 
229
                        for (i = 0; i < Rows; i++){
 
230
                                Table.SetRow(i);
 
231
                                id = Table.GetInt(0, 0);
 
232
                                is_date = Table.GetInt(1, 0);
 
233
                                date = wxDateTime((time_t) Table.GetInt(2, 0));
 
234
                                if(date.IsLaterThan(LastDate)) LastDate = date;
 
235
                                time = wxDateTime((time_t) Table.GetInt(3, 0));
 
236
                                type = (TOpType) Table.GetInt(4, 0);
 
237
                                str = Table.GetString(5, wxEmptyString);
 
238
                                reason = Table.GetString(6, wxEmptyString);
 
239
                                ind = Table.GetInt(7, 0);
 
240
 
 
241
                                contact_in = Table.GetInt(8, -1);
 
242
                                if(is_date){
 
243
                                        #if (wxCHECK_VERSION(3, 0, 0) )
 
244
                                                str.ToCDouble(&val);
 
245
                                        #else
 
246
                                                str.ToDouble(&val);
 
247
                                        #endif
 
248
                                        if(AddDate(id, date, val)) NLin++;}
 
249
                                else{
 
250
                                        #if (wxCHECK_VERSION(3, 0, 0) )
 
251
                                                Table.GetAsString(9).ToCDouble(&lat);
 
252
                                                Table.GetAsString(10).ToCDouble(&lon);
 
253
                                        #else
 
254
                                                Table.GetAsString(9).ToDouble(&lat);
 
255
                                                Table.GetAsString(10).ToDouble(&lon);
 
256
                                        #endif
 
257
                                        if((int) type < 9){
 
258
                                                #if (wxCHECK_VERSION(3, 0, 0) )
 
259
                                                        str.ToCDouble(&val);
 
260
                                                #else
 
261
                                                        str.ToDouble(&val);
 
262
                                                #endif
 
263
                                                str = FormDigits(val);}
 
264
                                        if(AddOper(id, date, time, type, str, reason, ind, contact_in, true, lat, lon)) NLin++;}}}
 
265
                else LastDate = wxDateTime::Today();
 
266
 
 
267
                FileData.Year = LastDate.GetYear();
 
268
                // Following code is a workaround for GetMonth() bug in Linux
 
269
                // http://forums.wxwidgets.org/viewtopic.php?t=25154&highlight=getmonth
 
270
                wxString temp;
 
271
                #ifdef __WXMSW__
 
272
                        temp = LastDate.Format("%m", wxDateTime::Local).c_str();
 
273
                #else
 
274
                        temp = LastDate.Format(L"%m", wxDateTime::Local).c_str();
 
275
                #endif // __WXMSW__
 
276
                temp.ToLong(&FileData.Month, 10);
 
277
 
 
278
                Day = LastDate;
 
279
 
 
280
                // Read shopping list
 
281
                if(TableExists(L"Shoplist", wxEmptyString, database)){
 
282
                        Table = GetTable(database, "select * from Shoplist order by item collate nocase;");
 
283
                        NSho = 0;
 
284
                        Rows = Table.GetRowCount();
 
285
                        for (i = 0; i < Rows; i++){
 
286
                                Table.SetRow(i);
 
287
                                id = Table.GetInt(0, 0);
 
288
                                str = Table.GetString(1, wxEmptyString);
 
289
                                ind = Table.GetInt(2, 0);
 
290
                                if(ind == -1) date = wxDateTime::Today().Add(wxDateSpan(100, 0, 0, 0));
 
291
                                else date = wxDateTime((time_t) ind);
 
292
                                if(AddShopItem(id, str, date)) NSho++;}}
 
293
        #else
 
294
                bool is_date;
 
295
                int Rows, id, i;        // Rows: number of rows in the query table
 
296
                                                                                        // id: id of table item
 
297
                long ind,                                       // ind: category index of transaction
 
298
                        contact_in;     // contact_in: contact index of transaction or object
 
299
 
 
300
                double val,
 
301
                        lat, lon;
 
302
                TOpType type;
 
303
                wxString str, reason;
 
304
                wxDateTime date, time;
 
305
                wxSQLite3Table Table;
 
306
 
 
307
                // Read Funds
 
308
                if(database->TableExists(L"Funds")){
 
309
                        Table = database->GetTable("select * from Funds order by name;");
 
310
                        NFun = 0;
 
311
                        Tot_Funds = 0;
 
312
                        Rows = Table.GetRowCount();
 
313
                        for (i = 0; i < Rows; i++){
 
314
                                Table.SetRow(i);
 
315
                                id = Table.GetInt(0, 0);
 
316
                                str = Table.GetString(1, wxEmptyString);
 
317
                                val = Table.GetDouble(2, 0);
 
318
                                if(AddValue(tvFou, id, str, val)){
 
319
                                        NFun++;
 
320
                                        Tot_Funds += val;}}}
 
321
 
 
322
                // Read default fund
 
323
                Table = database->GetTable(L"select data from Information where id = " +
 
324
                                                                                                                                wxString::Format(L"%d", dbMeta_default_fund) +
 
325
                                                                                                                                L";");
 
326
                Table.SetRow(0);
 
327
                FileData.DefFund = Table.GetString(0, wxEmptyString);
 
328
 
 
329
                // Read Credits
 
330
                if(database->TableExists(L"Credits")){
 
331
                        Table = database->GetTable("select * from Credits order by name;");
 
332
                        NCre = 0;
 
333
                        Tot_Credits = 0;
 
334
                        Rows = Table.GetRowCount();
 
335
                        for (i = 0; i < Rows; i++){
 
336
                                Table.SetRow(i);
 
337
                                id = Table.GetInt(0, 0);
 
338
                                str = Table.GetString(1, wxEmptyString);
 
339
                                val = Table.GetDouble(2, 0);
 
340
                                contact_in = Table.GetInt(3, -1);
 
341
                                if(AddValue(tvCre, id, str, val, contact_in)){
 
342
                                        NCre++;
 
343
                                        Tot_Credits += val;}}}
 
344
 
 
345
                // Read Debts
 
346
                if(database->TableExists(L"Debts")){
 
347
                        Table = database->GetTable("select * from Debts order by name;");
 
348
                        NDeb = 0;
 
349
                        Tot_Debts = 0;
 
350
                        Rows = Table.GetRowCount();
 
351
                        for (i = 0; i < Rows; i++){
 
352
                                Table.SetRow(i);
 
353
                                id = Table.GetInt(0, 0);
 
354
                                str = Table.GetString(1, wxEmptyString);
 
355
                                val = Table.GetDouble(2, 0);
 
356
                                contact_in = Table.GetInt(3, -1);
 
357
                                if(AddValue(tvDeb, id, str, val, contact_in)){
 
358
                                        NDeb++;
 
359
                                        Tot_Debts += val;}}}
 
360
 
 
361
                // Read Loans
 
362
                if(database->TableExists(L"Loans")){
 
363
                        Table = database->GetTable("select * from Loans order by name;");
 
364
                        NLen = 0;
 
365
                        Rows = Table.GetRowCount();
 
366
                        for (i = 0; i < Rows; i++){
 
367
                                Table.SetRow(i);
 
368
                                id = Table.GetInt(0, 0);
 
369
                                str = Table.GetString(1, wxEmptyString);
 
370
                                reason = Table.GetString(2, wxEmptyString);
 
371
                                ind = Table.GetInt(3, 0);
 
372
                                if(ind == -1) date = wxDateTime::Today().Add(wxDateSpan(100, 0, 0, 0));
 
373
                                else date = wxDateTime((time_t) ind);
 
374
                                contact_in = Table.GetInt(4, -1);
 
375
                                if(AddObject(toPre, id, str, reason, date, contact_in)) NLen++;}}
 
376
 
 
377
                // Read Borrows
 
378
                if(database->TableExists(L"Borrows")){
 
379
                        Table = database->GetTable("select * from Borrows order by name;");
 
380
                        NBor = 0;
 
381
                        Rows = Table.GetRowCount();
 
382
                        for (i = 0; i < Rows; i++){
 
383
                                Table.SetRow(i);
 
384
                                id = Table.GetInt(0, 0);
 
385
                                str = Table.GetString(1, wxEmptyString);
 
386
                                reason = Table.GetString(2, wxEmptyString);
 
387
                                ind = Table.GetInt(3, 0);
 
388
                                if(ind == -1) date = wxDateTime::Today().Add(wxDateSpan(100, 0, 0, 0));
 
389
                                else date = wxDateTime((time_t) ind);
 
390
                                contact_in = Table.GetInt(4, -1);
 
391
                                if(AddObject(toInP, id, str, reason, date, contact_in)) NBor++;}}
 
392
 
 
393
                // Read Categories
 
394
                Table = database->GetTable("select * from Categories order by name;");
 
395
                NCat = 0;
 
396
                Rows = Table.GetRowCount();
 
397
                for (i = 0; i < Rows; i++){
 
398
                        Table.SetRow(i) ;
 
399
                        id = Table.GetInt(0, 0);
 
400
                        str = Table.GetString(1, wxEmptyString);
 
401
                        is_date = Table.GetInt(2, 0);
 
402
                        if(is_date)AddCategory(id, str);}
 
403
 
 
404
                // Read Transactions
 
405
                wxDateTime LastDate = wxDateTime(1, wxDateTime::Jan, 1970);
 
406
                Table = database->GetTable("select * from Transactions order by date;");
 
407
                NLin = 0;
 
408
                Rows = Table.GetRowCount();
 
409
                if(Rows){
 
410
                        for (i = 0; i < Rows; i++){
 
411
                                Table.SetRow(i);
 
412
                                id = Table.GetInt(0, 0);
 
413
                                is_date = Table.GetInt(1, 0);
 
414
                                date = wxDateTime((time_t) Table.GetInt(2, 0));
 
415
                                if(date.IsLaterThan(LastDate)) LastDate = date;
 
416
                                time = wxDateTime((time_t) Table.GetInt(3, 0));
 
417
                                type = (TOpType) Table.GetInt(4, 0);
 
418
                                str = Table.GetString(5, wxEmptyString);
 
419
                                reason = Table.GetString(6, wxEmptyString);
 
420
                                ind = Table.GetInt(7, 0);
 
421
 
 
422
                                contact_in = Table.GetInt(8, -1);
 
423
                                if(is_date){
 
424
                                        #if (wxCHECK_VERSION(3, 0, 0) )
 
425
                                                str.ToCDouble(&val);
 
426
                                        #else
 
427
                                                str.ToDouble(&val);
 
428
                                        #endif
 
429
                                        if(AddDate(id, date, val)) NLin++;}
 
430
                                else{
 
431
                                        #if (wxCHECK_VERSION(3, 0, 0) )
 
432
                                                Table.GetAsString(9).ToCDouble(&lat);
 
433
                                                Table.GetAsString(10).ToCDouble(&lon);
 
434
                                        #else
 
435
                                                Table.GetAsString(9).ToDouble(&lat);
 
436
                                                Table.GetAsString(10).ToDouble(&lon);
 
437
                                        #endif
 
438
                                        if((int) type < 9){
 
439
                                                #if (wxCHECK_VERSION(3, 0, 0) )
 
440
                                                        str.ToCDouble(&val);
 
441
                                                #else
 
442
                                                        str.ToDouble(&val);
 
443
                                                #endif
 
444
                                                str = FormDigits(val);}
 
445
                                        if(AddOper(id, date, time, type, str, reason, ind, contact_in, true, lat, lon)) NLin++;}}}
 
446
                else LastDate = wxDateTime::Today();
 
447
 
 
448
                FileData.Year = LastDate.GetYear();
 
449
                // Following code is a workaround for GetMonth() bug in Linux
 
450
                // http://forums.wxwidgets.org/viewtopic.php?t=25154&highlight=getmonth
 
451
                wxString temp;
 
452
                #ifdef __WXMSW__
 
453
                        temp = LastDate.Format("%m", wxDateTime::Local).c_str();
 
454
                #else
 
455
                        temp = LastDate.Format(L"%m", wxDateTime::Local).c_str();
 
456
                #endif // __WXMSW__
 
457
                temp.ToLong(&FileData.Month, 10);
 
458
 
 
459
                Day = LastDate;
 
460
 
 
461
                // Read shopping list
 
462
                if(database->TableExists(L"Shoplist")){
 
463
                        Table = database->GetTable("select * from Shoplist order by item collate nocase;");
 
464
                        NSho = 0;
 
465
                        Rows = Table.GetRowCount();
 
466
                        for (i = 0; i < Rows; i++){
 
467
                                Table.SetRow(i);
 
468
                                id = Table.GetInt(0, 0);
 
469
                                str = Table.GetString(1, wxEmptyString);
 
470
                                ind = Table.GetInt(2, 0);
 
471
                                if(ind == -1) date = wxDateTime::Today().Add(wxDateSpan(100, 0, 0, 0));
 
472
                                else date = wxDateTime((time_t) ind);
 
473
                                if(AddShopItem(id, str, date)) NSho++;}}
 
474
        #endif // _OMB_USE_CIPHER
 
475
 
 
476
        Parsing = false;
 
477
}
 
478
 
 
479
bool TData_v32::AddOper(int id, wxDateTime D, wxDateTime O, TOpType T, wxString V, wxString M, long N, long c_index,
 
480
                                                                                                bool hasLocation, double lat, double lon){
 
481
        if(T==toNULL||V.IsEmpty()||M.IsEmpty()) return false;
 
482
        //wxString Tot=FormDigits(Tot_Funds);
 
483
 
 
484
        if(Parsing){
 
485
                Lines[NLin].Id = id;
 
486
                Lines[NLin].IsDate = false;
 
487
                Lines[NLin].Date = D;
 
488
                Lines[NLin].Time = O;
 
489
                Lines[NLin].Type = T;
 
490
                Lines[NLin].Value = V;
 
491
                Lines[NLin].Reason = M;
 
492
                Lines[NLin].CategoryIndex = N;
 
493
                Lines[NLin].ContactIndex = c_index;
 
494
                Lines[NLin].Latitude = lat;
 
495
                Lines[NLin].Longitude = lon;
 
496
                }
 
497
        else{
 
498
                M = DoubleQuote(M);
 
499
                if(T > 8) V = DoubleQuote(V);
 
500
 
 
501
                // Necessry to make sure the operation is appended to other entries of same day
 
502
                D.Set(D.GetDay(), D.GetMonth(), D.GetYear(), O.GetHour(), O.GetMinute());
 
503
 
 
504
                // Last date check
 
505
                bool found = false;
 
506
                if(NLin < 1){
 
507
                        AddDate(-1, D, Tot_Funds);
 
508
                        found = true;}
 
509
                else for(int i = NLin - 1; i >= 0; i--)if(IsDate(i)){
 
510
                        if( Lines[i].Date.IsSameDate(D)){
 
511
                                found = true;
 
512
                                break;}}
 
513
                if (! found) AddDate(-1, D, Tot_Funds);
 
514
 
 
515
                if(! hasLocation){
 
516
                        lat = ombInvalidLatitude;
 
517
                        lon = ombInvalidLongitude;
 
518
                }
 
519
 
 
520
                #ifdef _OMB_USE_CIPHER
 
521
                        wxString Sql = L"insert into Transactions values (NULL, 0, " +
 
522
                                                                                                                                                        wxString::Format(L"%d", (int) D.GetTicks()) +
 
523
                                                                                                                                                        L", " +
 
524
                                                                                                                                                        wxString::Format(L"%d", (int) O.GetTicks()) +
 
525
                                                                                                                                                        L", " +
 
526
                                                                                                                                                        wxString::Format(L"%d", T) +
 
527
                                                                                                                                                        L", '" +
 
528
                                                                                                                                                        V +
 
529
                                                                                                                                                        L"', '" +
 
530
                                                                                                                                                        M +
 
531
                                                                                                                                                        L"', " +
 
532
                                                                                                                                                        wxString::Format(L"%ld", N) +
 
533
 
 
534
                                                                                                                                                        L", " +
 
535
                                                                                                                                                        wxString::Format(L"%ld", c_index) +
 
536
 
 
537
                                                                                                                                                        L", " +
 
538
                                                                                                                                                        #if( wxCHECK_VERSION(3,0,0) )
 
539
                                                                                                                                                                ombFromCDouble(lat) +
 
540
                                                                                                                                                        #else
 
541
                                                                                                                                                                FormDigits(lat) +
 
542
                                                                                                                                                        #endif
 
543
                                                                                                                                                        L", " +
 
544
                                                                                                                                                        #if( wxCHECK_VERSION(3,0,0) )
 
545
                                                                                                                                                                ombFromCDouble(lon) +
 
546
                                                                                                                                                        #else
 
547
                                                                                                                                                                FormDigits(lon) +
 
548
                                                                                                                                                        #endif
 
549
 
 
550
                                                                                                                                                        L");";
 
551
                        ExecuteUpdate(database, Sql.c_str()/*, false*/);
 
552
                #else
 
553
                        database->ExecuteUpdate(L"insert into Transactions values (NULL, 0, " +
 
554
                                                                                                                                                wxString::Format(L"%d", (int) D.GetTicks()) +
 
555
                                                                                                                                                L", " +
 
556
                                                                                                                                                wxString::Format(L"%d", (int) O.GetTicks()) +
 
557
                                                                                                                                                L", " +
 
558
                                                                                                                                                wxString::Format(L"%d", T) +
 
559
                                                                                                                                                L", '" +
 
560
                                                                                                                                                V +
 
561
                                                                                                                                                L"', '" +
 
562
                                                                                                                                                M +
 
563
                                                                                                                                                L"', " +
 
564
                                                                                                                                                wxString::Format(L"%ld", N) +
 
565
 
 
566
                                                                                                                                                L", " +
 
567
                                                                                                                                                wxString::Format(L"%ld", c_index) +
 
568
 
 
569
                                                                                                                                                L", " +
 
570
                                                                                                                                                #if( wxCHECK_VERSION(3,0,0) )
 
571
                                                                                                                                                        ombFromCDouble(lat) +
 
572
                                                                                                                                                #else
 
573
                                                                                                                                                        FormDigits(lat) +
 
574
                                                                                                                                                #endif
 
575
                                                                                                                                                L", " +
 
576
                                                                                                                                                #if( wxCHECK_VERSION(3,0,0) )
 
577
                                                                                                                                                        ombFromCDouble(lon) +
 
578
                                                                                                                                                #else
 
579
                                                                                                                                                        FormDigits(lon) +
 
580
                                                                                                                                                #endif
 
581
 
 
582
                                                                                                                                                L");");
 
583
                #endif // _OMB_USE_CIPHER
 
584
 
 
585
                FileData.Modified=true;
 
586
                ParseDatabase();
 
587
                //Sort();
 
588
        }
 
589
        return true;}
 
590
 
 
591
bool TData_v32::AddDate(int id, wxDateTime D, double T){
 
592
        if(!D.IsValid())return false;
 
593
 
 
594
        if(! Parsing){
 
595
                D.ResetTime(); // New dates are with time set to midnight
 
596
 
 
597
                #ifdef __OPENMONEYBOX_EXE__
 
598
                        int M, A;
 
599
                        A = Day.GetYear();
 
600
                        M = Day.GetMonth() + 1;
 
601
                        if((A != FileData.Year) || (M != FileData.Month)){
 
602
                                //#ifdef __OPENMONEYBOX_EXE__
 
603
                                        if(AutoConvert()){
 
604
                                                wxCommandEvent evt(wxEVT_NULL,0);
 
605
                                                frame->TextConvClick(evt);}
 
606
                                //#endif // __OPENMONEYBOX_EXE__
 
607
                                wxString Path=::wxPathOnly(FileData.FileName);
 
608
                                #ifdef __WXMSW__
 
609
                                        wxString month=::wxString::Format("%02d",FileData.Month);
 
610
                                #else
 
611
                                        #if(wxCHECK_VERSION(3,0,0))
 
612
                                                wxString month=::wxString::Format(L"%02d",int(FileData.Month));
 
613
                                        #else
 
614
                                                wxString month=::wxString::Format(L"%02d",FileData.Month);
 
615
                                        #endif
 
616
                                #endif // __WXMSW__
 
617
                                wxString File = GetDocPrefix();
 
618
                                File+=L"_";
 
619
                                #ifdef __WXMSW__
 
620
                                        File+=::wxString::Format("%d",FileData.Year);
 
621
                                #else
 
622
                                        File+=::wxString::Format(L"%d",FileData.Year);
 
623
                                #endif // __WXMSW__
 
624
                                File += L"-" + month + L".omb";
 
625
                                FileData.Modified = true;
 
626
 
 
627
                                //#ifdef __OPENMONEYBOX_EXE__
 
628
                                        wxString master = Get_MasterDB();
 
629
                                        wxString trailname = ::wxString::Format(L"_%d_%02d", FileData.Year, int(FileData.Month));
 
630
 
 
631
                                        bool has_cat = Prepare_MasterDB(master, trailname);
 
632
 
 
633
                                        #ifdef _OMB_USE_CIPHER
 
634
                                                sqlite3_exec(database, "RELEASE rollback;", NULL, NULL, NULL);
 
635
 
 
636
                                                sqlite3 *pBk;
 
637
                                                sqlite3_open((Path + L"/" + File).c_str(), &pBk);
 
638
                                                sqlite3_backup *pBackup;
 
639
                                                pBackup = sqlite3_backup_init(pBk, "main", database, "main");
 
640
                                                if( pBackup ){
 
641
                                                        (void)sqlite3_backup_step(pBackup, -1);
 
642
                                                        (void)sqlite3_backup_finish(pBackup);
 
643
                                                }
 
644
                                        #else
 
645
                                                database->ReleaseSavepoint(L"rollback");
 
646
                                                database->Backup(Path + L"/" + File);
 
647
                                        #endif // _OMB_USE_CIPHER
 
648
 
 
649
                                        Archive_inMaster(master, trailname, ! has_cat);
 
650
 
 
651
                                        #ifdef _OMB_USE_CIPHER
 
652
                                                ExecuteUpdate(database, "DELETE FROM Transactions");
 
653
                                                ExecuteUpdate(database, "VACUUM");
 
654
                                                ExecuteUpdate(database, "SAVEPOINT rollback;");
 
655
                                        #else
 
656
                                                database->ExecuteUpdate("DELETE FROM Transactions");
 
657
                                                database->Vacuum();
 
658
                                                database->Savepoint(L"rollback");
 
659
                                        #endif // _OMB_USE_CIPHER
 
660
 
 
661
                                //#endif // __OPENMONEYBOX_EXE__
 
662
 
 
663
                                //CategoryDB->Sort(false);
 
664
 
 
665
                                NLin = 0;
 
666
                                FileData.Month = int (D.GetMonth()) + 1;
 
667
                                FileData.Year = D.GetYear();}
 
668
                        #endif // __OPENMONEYBOX_EXE__
 
669
        }
 
670
        else    // Robustness code to avoid creation of duplicate date entries
 
671
                for(int i = NLin - 1; i >= 0; i--)if(IsDate(i))
 
672
                        if(Lines[i].Date == D)
 
673
                                #ifndef __OMBCONVERT_BIN__
 
674
                                        return true;
 
675
                                #else
 
676
                                        return false;
 
677
                                #endif // __OMBCONVERT_BIN__
 
678
 
 
679
        if(Parsing){
 
680
                Lines[NLin].Id= id;
 
681
                Lines[NLin].IsDate = true;
 
682
                Lines[NLin].Date = D;
 
683
                Lines[NLin].Time = wxInvalidDateTime;
 
684
                Lines[NLin].Type = toNULL;
 
685
                Lines[NLin].Value = FormDigits(T);
 
686
                Lines[NLin].Reason = wxEmptyString;
 
687
                Lines[NLin].CategoryIndex = -1;
 
688
                Lines[NLin].ContactIndex = -1;
 
689
                Lines[NLin].Latitude = ombInvalidLatitude;
 
690
                Lines[NLin].Longitude = ombInvalidLongitude;
 
691
        }
 
692
        else
 
693
                #ifdef _OMB_USE_CIPHER
 
694
      #ifdef __OPENSUSE__
 
695
        {
 
696
          wxString Sql = L"insert into Transactions values (NULL, 1, " +
 
697
                                                                                                                                                wxString::Format(L"%d", (int) D.GetTicks()) +
 
698
                                                                                                                                                L", 0, 0, '" +
 
699
                                                                                                                                                #if( wxCHECK_VERSION(3,0,0) )
 
700
                                                                                                                                                        ombFromCDouble(T) +
 
701
                                                                                                                                                #else
 
702
                                                                                                                                                        FormDigits(T) +
 
703
                                                                                                                                                #endif
 
704
                                                                                                                                                L"', 0, -1, -1, " +
 
705
                                                                                                                                                wxString::Format(L"%d", (int) ombInvalidLatitude) +
 
706
                                                                                                                                                L", " +
 
707
                                                                                                                                                wxString::Format(L"%d", (int) ombInvalidLongitude) +
 
708
                                                                                                                                                L");";
 
709
          ExecuteUpdate(database, Sql.c_str());
 
710
        }
 
711
      #else
 
712
        ExecuteUpdate(database, L"insert into Transactions values (NULL, 1, " +
 
713
                                                                                                                                                wxString::Format(L"%d", (int) D.GetTicks()) +
 
714
                                                                                                                                                L", 0, 0, '" +
 
715
                                                                                                                                                #if( wxCHECK_VERSION(3,0,0) )
 
716
                                                                                                                                                        ombFromCDouble(T) +
 
717
                                                                                                                                                #else
 
718
                                                                                                                                                        FormDigits(T) +
 
719
                                                                                                                                                #endif
 
720
                                                                                                                                                L"', 0, -1, -1, " +
 
721
                                                                                                                                                wxString::Format(L"%d", (int) ombInvalidLatitude) +
 
722
                                                                                                                                                L", " +
 
723
                                                                                                                                                wxString::Format(L"%d", (int) ombInvalidLongitude) +
 
724
                                                                                                                                                L");");
 
725
      #endif // __OPENSUSE__
 
726
                #else
 
727
                        database->ExecuteUpdate(L"insert into Transactions values (NULL, 1, " +
 
728
                                                                                                                                                wxString::Format(L"%d", (int) D.GetTicks()) +
 
729
                                                                                                                                                L", 0, 0, '" +
 
730
                                                                                                                                                #if( wxCHECK_VERSION(3,0,0) )
 
731
                                                                                                                                                        ombFromCDouble(T) +
 
732
                                                                                                                                                #else
 
733
                                                                                                                                                        FormDigits(T) +
 
734
                                                                                                                                                #endif
 
735
                                                                                                                                                L"', 0, -1, -1, " +
 
736
                                                                                                                                                wxString::Format(L"%d", (int) ombInvalidLatitude) +
 
737
                                                                                                                                                L", " +
 
738
                                                                                                                                                wxString::Format(L"%d", (int) ombInvalidLongitude) +
 
739
                                                                                                                                                L");");
 
740
        #endif // _OMB_USE_CIPHER
 
741
 
 
742
  FileData.Modified=true;
 
743
  return true;}
 
744
 
 
745
bool TData_v32::FindContact(long contact_id){
 
746
                bool contact_exists = false;
 
747
                #ifdef _OMB_USE_CIPHER
 
748
                        bool table_existed = TableExists(L"Contacts", wxEmptyString, database);
 
749
                #else
 
750
                        bool table_existed = database->TableExists(L"Contacts");
 
751
                #endif // _OMB_USE_CIPHER
 
752
                if(table_existed){
 
753
                        wxSQLite3Table ContactsTable;
 
754
                        #ifdef _OMB_USE_CIPHER
 
755
        #ifdef __OPENSUSE__
 
756
          wxString Sql = L"select status from Contacts where mobile_id = " +
 
757
                                                                                                                        wxString::Format(L"%ld", contact_id) +
 
758
                                                                                                                        L";";
 
759
          ContactsTable = GetTable(database, Sql.c_str());
 
760
        #else
 
761
          ContactsTable = GetTable(database, L"select status from Contacts where mobile_id = " +
 
762
                                                                                                                        wxString::Format(L"%ld", contact_id) +
 
763
                                                                                                                        L";");
 
764
        #endif // __OPENSUSE__
 
765
                        #else
 
766
                                ContactsTable = database->GetTable(L"select status from Contacts where mobile_id = " +
 
767
                                                                                                                        wxString::Format(L"%ld", contact_id) +
 
768
                                                                                                                        L";");
 
769
                        #endif // _OMB_USE_CIPHER
 
770
                        ContactsTable.SetRow(0);
 
771
 
 
772
                        contact_exists = (ContactsTable.GetInt(0) > 0);
 
773
                }
 
774
 
 
775
                return contact_exists;
 
776
}
 
777
 
 
778
bool TData_v32::IsDate(int R){
 
779
  return Lines[R].IsDate;
 
780
}
 
781
 
 
782
bool TData_v32::OpenDatabase(wxString File){
 
783
 
 
784
        bool file_exist = ::wxFileExists(File);
 
785
        wxString Name;
 
786
 
 
787
        #ifdef _OMB_USE_CIPHER
 
788
        #ifdef __OMBCONVERT_BIN__
 
789
                wxString pwd = wxEmptyString;
 
790
        #else
 
791
                wxString pwd = GetKey();
 
792
                wxString pwd_archive = GetKey(true);
 
793
        #endif // __OMBCONVERT_BIN__
 
794
        #endif // _OMB_USE_CIPHER
 
795
 
 
796
        #ifndef __OMBCONVERT_BIN__
 
797
                if(file_exist){
 
798
                        #ifdef _OMB_USE_CIPHER
 
799
                                if(! CheckAndPromptForConversion(File, false, pwd)){
 
800
                        #else
 
801
                                if(! CheckAndPromptForConversion(File, false)){
 
802
                        #endif // _OMB_USE_CIPHER
 
803
                                Parsing = false;
 
804
                                return false;}}
 
805
 
 
806
                wxString ArchiveName = Get_MasterDB();
 
807
                bool master_exist = ::wxFileExists(ArchiveName);
 
808
 
 
809
    if(master_exist){
 
810
        #ifdef _OMB_USE_CIPHER
 
811
                if(! CheckAndPromptForConversion(ArchiveName, true, pwd_archive)){
 
812
                        #else
 
813
                if(! CheckAndPromptForConversion(ArchiveName, true)){
 
814
                        #endif // _OMB_USE_CIPHER
 
815
                        Parsing = false;
 
816
                        return false;}}
 
817
        #endif // __OMBCONVERT_BIN__
 
818
 
 
819
        #ifdef __OPENMONEYBOX_EXE__
 
820
                wxLogMessage(L"Opening database...");
 
821
        #endif // __OPENMONEYBOX_EXE__
 
822
 
 
823
        #ifdef _OMB_USE_CIPHER
 
824
                int result = -1;
 
825
                int tryno = 1;
 
826
 
 
827
                IsEncrypted = IsEncryptedDB(File, pwd, wxEmptyString, true);
 
828
                #ifndef __OMBCONVERT_BIN__
 
829
                        if(master_exist) IsEncrypted_master = IsEncryptedDB(ArchiveName, pwd_archive, wxEmptyString, true);
 
830
                #endif // __OMBCONVERT_BIN__
 
831
 
 
832
                #ifdef __OPENSUSE__
 
833
      sqlite3_open(File.c_str(), &database);
 
834
                #else
 
835
      sqlite3_open(File, &database);
 
836
    #endif // __OPENSUSE__
 
837
 
 
838
                while (tryno < 4)
 
839
                {
 
840
                        try
 
841
                        {
 
842
                                if(IsEncrypted){
 
843
          #ifdef __OPENSUSE__
 
844
            sqlite3_key(database, pwd.c_str(), pwd.length());
 
845
          #else
 
846
            sqlite3_key(database, pwd, pwd.length());
 
847
          #endif // __OPENSUSE__
 
848
                                        int rc = sqlite3_exec(database, "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL);
 
849
                                        switch(rc){
 
850
                                                case SQLITE_OK:
 
851
                                                result = 1;
 
852
                                                break;
 
853
 
 
854
                                        case SQLITE_NOTADB:
 
855
                                                #ifndef __OMBCONVERT_BIN__
 
856
                                                        PasswordPrompt(database, false);
 
857
                                                #endif // __OMBCONVERT_BIN__
 
858
                                                break;
 
859
 
 
860
                                                //default:
 
861
                                        }
 
862
 
 
863
                                }
 
864
 
 
865
                        }
 
866
                        catch(exception& e)
 
867
                        {
 
868
                                //
 
869
                        }
 
870
 
 
871
                        if(result == 1) break;
 
872
                        tryno++;
 
873
                }
 
874
        #else
 
875
                database->Open(File, wxEmptyString, WXSQLITE_OPEN_READWRITE | WXSQLITE_OPEN_CREATE );
 
876
        #endif // _OMB_USE_CIPHER
 
877
 
 
878
        #ifdef __OPENMONEYBOX_EXE__
 
879
                wxLogMessage(L"Database loaded.");
 
880
        #endif // __OPENMONEYBOX_EXE__
 
881
 
 
882
        if(file_exist){
 
883
                FileData.FileName = File;
 
884
                ::wxFileName::SplitPath(File, NULL, NULL, &Name, NULL, wxPATH_NATIVE);
 
885
                FileData.FileView = Name;
 
886
 
 
887
                #ifndef __OMBCONVERT_BIN__
 
888
                        // Check if present Android backup to be archived in master db
 
889
                        // Read backup entry
 
890
                        wxSQLite3Table Table;
 
891
                        #ifdef _OMB_USE_CIPHER
 
892
                                wxString Sql;
 
893
 
 
894
                                #ifdef __OPENSUSE__
 
895
                                        Sql = L"select data from Information where id = " +
 
896
                                                                                                                                                wxString::Format(L"%d", dbMeta_mobile_export) +
 
897
                                                                                                                                                L";";
 
898
                                        Table = GetTable(database, Sql.c_str());
 
899
                                #else
 
900
                                        Table = GetTable(database, L"select data from Information where id = " +
 
901
                                                                                                                                                wxString::Format(L"%d", dbMeta_mobile_export) +
 
902
                                                                                                                                                L";");
 
903
                                #endif // __OPENSUSE__
 
904
                        #else
 
905
                                Table = database->GetTable(L"select data from Information where id = " +
 
906
                                                                                                                                        wxString::Format(L"%d", dbMeta_mobile_export) +
 
907
                                                                                                                                        L";");
 
908
                        #endif // _OMB_USE_CIPHER
 
909
                        Table.SetRow(0);
 
910
                        wxString archive = Table.GetString(0, wxEmptyString);
 
911
 
 
912
                        if(! archive.IsEmpty()){
 
913
                                wxString Path, backup_file;
 
914
                                ::wxFileName::SplitPath(File, &Path, NULL, NULL, wxPATH_NATIVE);
 
915
 
 
916
                                #ifdef __WXGTK__
 
917
                                        backup_file = Path + L"/_" + archive;
 
918
                                #elif defined ( __WXMSW__ )
 
919
                                        backup_file = Path + "\\_" + archive;
 
920
                                #endif // __WXGTK__
 
921
 
 
922
                                if(::wxFileExists(backup_file)){
 
923
                                        wxString master = Get_MasterDB();
 
924
                                        wxString trailname = L"_" + archive;
 
925
                                        bool has_cat = Prepare_MasterDB(master, trailname);
 
926
 
 
927
                                        #if ( wxCHECK_VERSION(3, 0, 0) )
 
928
                                                #ifdef _OMB_USE_CIPHER
 
929
                                                        AttachMaster();
 
930
                                                        #ifdef __OPENSUSE__
 
931
                                                                Sql = wxString::Format(L"ATTACH DATABASE '%s' AS ext KEY '%s';", backup_file, pwd);
 
932
                                                                ExecuteUpdate(database, Sql.c_str());
 
933
                                                        #else
 
934
                                                                ExecuteUpdate(database, wxString::Format(L"ATTACH DATABASE '%s' AS ext KEY '%s';", backup_file, pwd));
 
935
                                                        #endif // __OPENSUSE__
 
936
 
 
937
                                                        // Archive data in master database from external archive
 
938
                                                        Sql = L"INSERT INTO master.Transactions (isdate, date, time, type, value, reason, cat_index, contact_index, latitude, longitude) SELECT isdate, date, time, type, value, reason, cat_index, contact_index, latitude, longitude FROM ext.Transactions;";
 
939
                                                        ExecuteUpdate(database, Sql.c_str());
 
940
                                                        #ifdef __OPENSUSE__
 
941
                                                                Sql = wxString::Format(L"INSERT INTO master.Funds%s SELECT * FROM ext.Funds%s;", trailname, trailname);
 
942
                                                                ExecuteUpdate(database, Sql.c_str());
 
943
                                                                Sql = wxString::Format(L"INSERT INTO master.Credits%s SELECT * FROM ext.Credits%s;", trailname, trailname);
 
944
                                                                ExecuteUpdate(database, Sql.c_str());
 
945
                                                                Sql = wxString::Format(L"INSERT INTO master.Debts%s SELECT * FROM ext.Debts%s;", trailname, trailname);
 
946
                                                                ExecuteUpdate(database, Sql.c_str());
 
947
                                                                Sql = wxString::Format(L"INSERT INTO master.Loans%s SELECT * FROM ext.Loans%s;", trailname, trailname);
 
948
                                                                ExecuteUpdate(database, Sql.c_str());
 
949
                                                                Sql = wxString::Format(L"INSERT INTO master.Borrows%s SELECT * FROM ext.Borrows%s;", trailname, trailname);
 
950
                                                                ExecuteUpdate(database, Sql.c_str());
 
951
                                                        #else
 
952
                                                                ExecuteUpdate(database, wxString::Format(L"INSERT INTO master.Funds%s SELECT * FROM ext.Funds%s;", trailname, trailname));
 
953
                                                                ExecuteUpdate(database, wxString::Format(L"INSERT INTO master.Credits%s SELECT * FROM ext.Credits%s;", trailname, trailname));
 
954
                                                                ExecuteUpdate(database, wxString::Format(L"INSERT INTO master.Debts%s SELECT * FROM ext.Debts%s;", trailname, trailname));
 
955
                                                                ExecuteUpdate(database, wxString::Format(L"INSERT INTO master.Loans%s SELECT * FROM ext.Loans%s;", trailname, trailname));
 
956
                                                                ExecuteUpdate(database, wxString::Format(L"INSERT INTO master.Borrows%s SELECT * FROM ext.Borrows%s;", trailname, trailname));
 
957
                                                        #endif // __OPENSUSE__
 
958
                                                #else
 
959
                                                        database->ExecuteUpdate(wxString::Format(L"ATTACH DATABASE '%s' AS master;", master));
 
960
                                                        database->ExecuteUpdate(wxString::Format(L"ATTACH DATABASE '%s' AS ext;", backup_file));
 
961
 
 
962
                                                        // Archive data in master database from external archive
 
963
                                                        database->ExecuteUpdate(L"INSERT INTO master.Transactions (isdate, date, time, type, value, reason, cat_index, contact_index, latitude, longitude) SELECT isdate, date, time, type, value, reason, cat_index, contact_index, latitude, longitude FROM ext.Transactions;");
 
964
                                                        database->ExecuteUpdate(wxString::Format(L"INSERT INTO master.Funds%s SELECT * FROM ext.Funds%s;", trailname, trailname));
 
965
                                                        database->ExecuteUpdate(wxString::Format(L"INSERT INTO master.Credits%s SELECT * FROM ext.Credits%s;", trailname, trailname));
 
966
                                                        database->ExecuteUpdate(wxString::Format(L"INSERT INTO master.Debts%s SELECT * FROM ext.Debts%s;", trailname, trailname));
 
967
                                                        database->ExecuteUpdate(wxString::Format(L"INSERT INTO master.Loans%s SELECT * FROM ext.Loans%s;", trailname, trailname));
 
968
                                                        database->ExecuteUpdate(wxString::Format(L"INSERT INTO master.Borrows%s SELECT * FROM ext.Borrows%s;", trailname, trailname));
 
969
                                                #endif // _OMB_USE_CIPHER
 
970
                                        #else
 
971
                                                database->ExecuteUpdate(wxString::Format(L"ATTACH DATABASE '%s' AS master;", master.c_str()));
 
972
                                                database->ExecuteUpdate(wxString::Format(L"ATTACH DATABASE '%s' AS ext;", backup_file.c_str()));
 
973
 
 
974
                                                // Archive data in master database from external archive
 
975
                                                database->ExecuteUpdate(L"INSERT INTO master.Transactions (isdate, date, time, type, value, reason, cat_index, contact_index, latitude, longitude) SELECT isdate, date, time, type, value, reason, cat_index, contact_index, latitude, longitude FROM ext.Transactions;");
 
976
                                                database->ExecuteUpdate(wxString::Format(L"INSERT INTO master.Funds%s SELECT * FROM ext.Funds%s;", trailname.c_str(), trailname.c_str()));
 
977
                                                database->ExecuteUpdate(wxString::Format(L"INSERT INTO master.Credits%s SELECT * FROM ext.Credits%s;", trailname.c_str(), trailname.c_str()));
 
978
                                                database->ExecuteUpdate(wxString::Format(L"INSERT INTO master.Debts%s SELECT * FROM ext.Debts%s;", trailname.c_str(), trailname.c_str()));
 
979
                                                database->ExecuteUpdate(wxString::Format(L"INSERT INTO master.Loans%s SELECT * FROM ext.Loans%s;", trailname.c_str(), trailname.c_str()));
 
980
                                                database->ExecuteUpdate(wxString::Format(L"INSERT INTO master.Borrows%s SELECT * FROM ext.Borrows%s;", trailname.c_str(), trailname.c_str()));
 
981
                                        #endif
 
982
 
 
983
                                        #ifdef _OMB_USE_CIPHER
 
984
                                                if(! has_cat) {
 
985
                                                        Sql = L"INSERT INTO master.Categories SELECT * FROM ext.Categories;";
 
986
                                                        ExecuteUpdate(database, Sql.c_str()/*, false*/);
 
987
                                                }
 
988
                                                Sql = L"DETACH DATABASE 'master';";
 
989
                                                ExecuteUpdate(database, Sql.c_str()/*, false*/);
 
990
                                                Sql = L"DETACH DATABASE 'ext';";
 
991
                                                ExecuteUpdate(database, Sql.c_str());
 
992
 
 
993
                                                #ifdef __OPENSUSE__
 
994
                                                        Sql = L"update Information set data = \"\" where id = " +
 
995
                                                                                                                                        wxString::Format(L"%d", dbMeta_mobile_export) +
 
996
                                                                                                                                        L";";
 
997
                                                        ExecuteUpdate(database, Sql.c_str());
 
998
                                                #else
 
999
                                                        ExecuteUpdate(database, L"update Information set data = \"\" where id = " +
 
1000
                                                                                                                                        wxString::Format(L"%d", dbMeta_mobile_export) +
 
1001
                                                                                                                                        L";");
 
1002
                                                #endif // __OPENSUSE__
 
1003
                                        #else
 
1004
                                                if(! has_cat) database->ExecuteUpdate(L"INSERT INTO master.Categories SELECT * FROM ext.Categories;");
 
1005
 
 
1006
                                                database->ExecuteUpdate(L"DETACH DATABASE 'master';");
 
1007
                                                database->ExecuteUpdate(L"DETACH DATABASE 'ext';");
 
1008
 
 
1009
                                                database->ExecuteUpdate(L"update Information set data = \"\" where id = " +
 
1010
                                                                                                                                        wxString::Format(L"%d", dbMeta_mobile_export) +
 
1011
                                                                                                                                        L";");
 
1012
                                        #endif // _OMB_USE_CIPHER
 
1013
 
 
1014
                                        ::wxRemoveFile(backup_file);
 
1015
                                }
 
1016
                        }
 
1017
                #endif // __OMBCONVERT_BIN__
 
1018
        }
 
1019
 
 
1020
        #ifdef _OMB_USE_CIPHER
 
1021
                else
 
1022
      #ifdef __OPENSUSE__
 
1023
        {
 
1024
          wxString Sql = L"pragma user_version = " +
 
1025
                                                                                                                                                                                ::wxString::Format(L"%d", dbVersion) +
 
1026
                                                                                                                                                                                L";";
 
1027
          ExecuteUpdate(database, Sql.c_str());
 
1028
        }
 
1029
      #else
 
1030
        ExecuteUpdate(database, L"pragma user_version = " +
 
1031
                                                                                                                                                                                ::wxString::Format(L"%d", dbVersion) +
 
1032
                                                                                                                                                                                L";");
 
1033
      #endif // __OPENSUSE__
 
1034
                // Set restore savepoint
 
1035
                ExecuteUpdate(database, "SAVEPOINT rollback;");
 
1036
 
 
1037
        #else
 
1038
                else database->ExecuteUpdate(L"pragma user_version = " +
 
1039
                                                                                                                                                                                        ::wxString::Format(L"%d", dbVersion) +
 
1040
                                                                                                                                                                                        L";");
 
1041
 
 
1042
                // Set restore savepoint
 
1043
                database->Savepoint(L"rollback");
 
1044
        #endif // _OMB_USE_CIPHER
 
1045
 
 
1046
        #ifdef __OPENMONEYBOX_EXE__
 
1047
                wxLogMessage(L"Rollback savepoint set in database");
 
1048
        #endif // __OPENMONEYBOX_EXE__
 
1049
 
 
1050
        #if wxCHECK_VERSION(3, 0, 0)
 
1051
                // Write file metadata
 
1052
                wxString metadata;
 
1053
                metadata = L"OS: ";
 
1054
                #ifdef __WXGTK__
 
1055
                        metadata += wxGetLinuxDistributionInfo().Description;
 
1056
                #elif defined ( __WXMSW__)
 
1057
                        metadata += wxGetOsDescription();
 
1058
                #else
 
1059
                        ,etadata += L"unknown";
 
1060
                #endif // __WXGTK__
 
1061
                metadata += L"\n\n";
 
1062
                #ifndef __OMBCONVERT_BIN__
 
1063
                        metadata += wxGetLibraryVersionInfo().ToString();
 
1064
                #else
 
1065
                        metadata += _("Converted with ombconvert");
 
1066
                #endif // __OMBCONVERT_BIN__
 
1067
        #endif // wxCHECK_VERSION
 
1068
 
 
1069
        #ifdef _OMB_USE_CIPHER
 
1070
                if(! TableExists(L"Information", wxEmptyString, database)){
 
1071
      #ifdef __OPENSUSE__
 
1072
        ExecuteUpdate(database, cs_information.c_str());
 
1073
      #else
 
1074
        ExecuteUpdate(database, cs_information);
 
1075
      #endif // __OPENSUSE__
 
1076
 
 
1077
                        #if wxCHECK_VERSION(3, 0, 0)
 
1078
        #ifdef __OPENSUSE__
 
1079
          wxString Sql = L"insert into Information values (" +
 
1080
                                                                                                                                        wxString::Format(L"%d", dbMeta_application_info) +
 
1081
                                                                                                                                        L", '" +
 
1082
                                                                                                                                        metadata +
 
1083
                                                                                                                                        L"');";
 
1084
          ExecuteUpdate(database, Sql.c_str());
 
1085
        #else
 
1086
          ExecuteUpdate(database, L"insert into Information values (" +
 
1087
                                                                                                                                        wxString::Format(L"%d", dbMeta_application_info) +
 
1088
                                                                                                                                        L", '" +
 
1089
                                                                                                                                        metadata +
 
1090
                                                                                                                                        L"');");
 
1091
        #endif // __OPENSUSE__
 
1092
                        #endif // wxCHECK_VERSION
 
1093
 
 
1094
                        #ifdef __OPENSUSE__
 
1095
        Sql = L"insert into Information values (" +
 
1096
                                                                                                                                wxString::Format(L"%d", dbMeta_default_fund) +
 
1097
                                                                                                                                L", 'default');";
 
1098
        ExecuteUpdate(database, Sql.c_str());
 
1099
        Sql = L"insert into Information values (" +
 
1100
                                                                                                                                wxString::Format(L"%d", dbMeta_mobile_export) +
 
1101
                                                                                                                                L", '');";
 
1102
        ExecuteUpdate(database, Sql.c_str());
 
1103
                        #else
 
1104
        ExecuteUpdate(database, L"insert into Information values (" +
 
1105
                                                                                                                                wxString::Format(L"%d", dbMeta_default_fund) +
 
1106
                                                                                                                                L", 'default');");
 
1107
        ExecuteUpdate(database, L"insert into Information values (" +
 
1108
                                                                                                                                wxString::Format(L"%d", dbMeta_mobile_export) +
 
1109
                                                                                                                                L", '');");
 
1110
      #endif // __OPENSUSE__
 
1111
                }
 
1112
                #if wxCHECK_VERSION(3, 0, 0)
 
1113
                        else
 
1114
        #ifdef __OPENSUSE__
 
1115
          {
 
1116
            wxString Sql = L"update Information set data = \"" +
 
1117
                                                                                                                                        metadata +
 
1118
                                                                                                                                        L"\" where id = " +
 
1119
                                                                                                                                        wxString::Format(L"%d", dbMeta_application_info) +
 
1120
                                                                                                                                        L";";
 
1121
            ExecuteUpdate(database, Sql.c_str());
 
1122
          }
 
1123
        #else
 
1124
          ExecuteUpdate(database, L"update Information set data = \"" +
 
1125
                                                                                                                                        metadata +
 
1126
                                                                                                                                        L"\" where id = " +
 
1127
                                                                                                                                        wxString::Format(L"%d", dbMeta_application_info) +
 
1128
                                                                                                                                        L";");
 
1129
        #endif // __OPENSUSE__
 
1130
                #endif // wxCHECK_VERSION
 
1131
        #else
 
1132
                if(! database->TableExists(L"Information")){
 
1133
                        database->ExecuteUpdate(cs_information);
 
1134
 
 
1135
                        #if wxCHECK_VERSION(3, 0, 0)
 
1136
                                database->ExecuteUpdate(L"insert into Information values (" +
 
1137
                                                                                                                                        wxString::Format(L"%d", dbMeta_application_info) +
 
1138
                                                                                                                                        L", '" +
 
1139
                                                                                                                                        metadata +
 
1140
                                                                                                                                        L"');");
 
1141
                        #endif // wxCHECK_VERSION
 
1142
                        database->ExecuteUpdate(L"insert into Information values (" +
 
1143
                                                                                                                                wxString::Format(L"%d", dbMeta_default_fund) +
 
1144
                                                                                                                                L", 'default');");
 
1145
                        database->ExecuteUpdate(L"insert into Information values (" +
 
1146
                                                                                                                                wxString::Format(L"%d", dbMeta_mobile_export) +
 
1147
                                                                                                                                L", '');");
 
1148
                }
 
1149
                #if wxCHECK_VERSION(3, 0, 0)
 
1150
                        else database->ExecuteUpdate(L"update Information set data = \"" +
 
1151
                                                                                                                                        metadata +
 
1152
                                                                                                                                        L"\" where id = " +
 
1153
                                                                                                                                        wxString::Format(L"%d", dbMeta_application_info) +
 
1154
                                                                                                                                        L";");
 
1155
                #endif // wxCHECK_VERSION
 
1156
        #endif // _OMB_USE_CIPHER
 
1157
 
 
1158
        // Set currency when if not present (added later in dbVersion 31)
 
1159
        wxSQLite3Table currencyTable;
 
1160
        #ifdef _OMB_USE_CIPHER
 
1161
    #ifdef __OPENSUSE__
 
1162
      wxString Sql = L"select data from Information where id = " +
 
1163
                                                                                                                                wxString::Format(L"%d", dbMeta_currency) +
 
1164
                                                                                                                                L";";
 
1165
      currencyTable = GetTable(database, Sql.c_str());
 
1166
    #else
 
1167
      currencyTable = GetTable(database, L"select data from Information where id = " +
 
1168
                                                                                                                                wxString::Format(L"%d", dbMeta_currency) +
 
1169
                                                                                                                                L";");
 
1170
    #endif // __OPENSUSE__
 
1171
        #else
 
1172
                currencyTable = database->GetTable(L"select data from Information where id = " +
 
1173
                                                                                                                        wxString::Format(L"%d", dbMeta_currency) +
 
1174
                                                                                                                        L";");
 
1175
        #endif // _OMB_USE_CIPHER
 
1176
        if(currencyTable.GetRowCount() == 0){
 
1177
                wxString curr = GetCurrencySymbol();
 
1178
                #ifdef _OMB_USE_CIPHER
 
1179
      #ifdef __OPENSUSE__
 
1180
        Sql = L"insert into Information values (" +
 
1181
                                                                                                                                wxString::Format(L"%d", dbMeta_currency) +
 
1182
                                                                                                                                L", '" +
 
1183
                                                                                                                                curr +
 
1184
                                                                                                                                L"');";
 
1185
        ExecuteUpdate(database, Sql.c_str());
 
1186
      #else
 
1187
        ExecuteUpdate(database, L"insert into Information values (" +
 
1188
                                                                                                                                wxString::Format(L"%d", dbMeta_currency) +
 
1189
                                                                                                                                L", '" +
 
1190
                                                                                                                                curr +
 
1191
                                                                                                                                L"');");
 
1192
      #endif // __OPENSUSE__
 
1193
                #else
 
1194
                        database->ExecuteUpdate(L"insert into Information values (" +
 
1195
                                                                                                                        wxString::Format(L"%d", dbMeta_currency) +
 
1196
                                                                                                                        L", '" +
 
1197
                                                                                                                        curr +
 
1198
                                                                                                                        L"');");
 
1199
                #endif // _OMB_USE_CIPHER
 
1200
        }
 
1201
 
 
1202
        #ifdef __OPENMONEYBOX_EXE__
 
1203
                wxLogMessage(L"Database metadata updated");
 
1204
        #endif // __OPENMONEYBOX_EXE__
 
1205
 
 
1206
        #ifdef _OMB_USE_CIPHER
 
1207
    #ifdef __OPENSUSE__
 
1208
      // Transaction table init
 
1209
      if(! TableExists(L"Transactions", wxEmptyString, database)) ExecuteUpdate(database, cs_transactions.c_str());
 
1210
 
 
1211
      // Category table init
 
1212
      if(! TableExists(L"Categories", wxEmptyString, database)) ExecuteUpdate(database, cs_categories.c_str());
 
1213
    #else
 
1214
      // Transaction table init
 
1215
      if(! TableExists(L"Transactions", wxEmptyString, database)) ExecuteUpdate(database, cs_transactions);
 
1216
 
 
1217
      // Category table init
 
1218
      if(! TableExists(L"Categories", wxEmptyString, database)) ExecuteUpdate(database, cs_categories);
 
1219
    #endif // __OPENSUSE__
 
1220
        #else
 
1221
                // Transaction table init
 
1222
                if(! database->TableExists(L"Transactions")) database->ExecuteUpdate(cs_transactions);
 
1223
 
 
1224
                // Category table init
 
1225
                if(! database->TableExists(L"Categories")) database->ExecuteUpdate(cs_categories);
 
1226
        #endif // _OMB_USE_CIPHER
 
1227
 
 
1228
        ParseDatabase();
 
1229
        FileData.Modified = false;
 
1230
 
 
1231
        return true;}
 
1232
 
 
1233
bool TData_v32::AddObject(TObjType T, int id, wxString N, wxString O, wxDateTime D, long c_index){
 
1234
        if(N.IsEmpty() || O.IsEmpty())return false;
 
1235
        if (D == invalidDate) return false;
 
1236
 
 
1237
        if(! Parsing){
 
1238
                N = DoubleQuote(N);
 
1239
                O = DoubleQuote(O);}
 
1240
 
 
1241
        switch(T){
 
1242
                case toPre:
 
1243
                        if(Parsing){
 
1244
                                Lent[NLen].Id = id;
 
1245
                                Lent[NLen].Name = N;
 
1246
                                Lent[NLen].Object = O;
 
1247
                                Lent[NLen].Alarm = D;
 
1248
                                Lent[NLen].ContactIndex = c_index;
 
1249
                        }
 
1250
                        else{
 
1251
                                #ifdef _OMB_USE_CIPHER
 
1252
          #ifdef __OPENSUSE__
 
1253
            if(! TableExists(L"Loans", wxEmptyString, database)) ExecuteUpdate(database, cs_loans.c_str());
 
1254
            wxString Sql = L"insert into Loans values (NULL, '" +
 
1255
                                                                                                                                                        N +
 
1256
                                                                                                                                                        L"', '" +
 
1257
                                                                                                                                                        O +
 
1258
                                                                                                                                                        L"', " +
 
1259
                                                                                                                                                        wxString::Format(L"%d", (int) D.GetTicks()) +
 
1260
 
 
1261
                                                                                                                                                        L", " +
 
1262
                                                                                                                                                        wxString::Format(L"%ld", c_index) +
 
1263
 
 
1264
                                                                                                                                                        L");";
 
1265
            ExecuteUpdate(database, Sql.c_str());
 
1266
          #else
 
1267
            if(! TableExists(L"Loans", wxEmptyString, database)) ExecuteUpdate(database, cs_loans);
 
1268
            ExecuteUpdate(database, L"insert into Loans values (NULL, '" +
 
1269
                                                                                                                                                        N +
 
1270
                                                                                                                                                        L"', '" +
 
1271
                                                                                                                                                        O +
 
1272
                                                                                                                                                        L"', " +
 
1273
                                                                                                                                                        wxString::Format(L"%d", (int) D.GetTicks()) +
 
1274
 
 
1275
                                                                                                                                                        L", " +
 
1276
                                                                                                                                                        wxString::Format(L"%ld", c_index) +
 
1277
 
 
1278
                                                                                                                                                        L");");
 
1279
          #endif // __OPENSUSE__
 
1280
                                #else
 
1281
                                        if(! database->TableExists(L"Loans")) database->ExecuteUpdate(cs_loans);
 
1282
                                        database->ExecuteUpdate(L"insert into Loans values (NULL, '" +
 
1283
                                                                                                                                                N +
 
1284
                                                                                                                                                L"', '" +
 
1285
                                                                                                                                                O +
 
1286
                                                                                                                                                L"', " +
 
1287
                                                                                                                                                wxString::Format(L"%d", (int) D.GetTicks()) +
 
1288
 
 
1289
                                                                                                                                                L", " +
 
1290
                                                                                                                                                wxString::Format(L"%ld", c_index) +
 
1291
 
 
1292
                                                                                                                                                L");");
 
1293
                                #endif // _OMB_USE_CIPHER
 
1294
                        }
 
1295
                        break;
 
1296
                case toInP:
 
1297
                        if(Parsing){
 
1298
                                Borrowed[NBor].Id = id;
 
1299
                                Borrowed[NBor].Name = N;
 
1300
                                Borrowed[NBor].Object = O;
 
1301
                                Borrowed[NBor].Alarm = D;
 
1302
                                Borrowed[NBor].ContactIndex = c_index;
 
1303
                        }
 
1304
                        else{
 
1305
                                #ifdef _OMB_USE_CIPHER
 
1306
          #ifdef __OPENSUSE__
 
1307
            if(! TableExists(L"Borrows", wxEmptyString, database)) ExecuteUpdate(database, cs_borrows.c_str());
 
1308
            wxString Sql = L"insert into Borrows values (NULL, '" +
 
1309
                                                                                                                                                        N +
 
1310
                                                                                                                                                        L"', '" +
 
1311
                                                                                                                                                        O +
 
1312
                                                                                                                                                        L"', " +
 
1313
                                                                                                                                                        wxString::Format(L"%d", (int) D.GetTicks()) +
 
1314
 
 
1315
                                                                                                                                                        L", " +
 
1316
                                                                                                                                                        wxString::Format(L"%ld", c_index) +
 
1317
 
 
1318
                                                                                                                                                        L");";
 
1319
            ExecuteUpdate(database, Sql.c_str());
 
1320
          #else
 
1321
            if(! TableExists(L"Borrows", wxEmptyString, database)) ExecuteUpdate(database, cs_borrows);
 
1322
            ExecuteUpdate(database, L"insert into Borrows values (NULL, '" +
 
1323
                                                                                                                                                        N +
 
1324
                                                                                                                                                        L"', '" +
 
1325
                                                                                                                                                        O +
 
1326
                                                                                                                                                        L"', " +
 
1327
                                                                                                                                                        wxString::Format(L"%d", (int) D.GetTicks()) +
 
1328
 
 
1329
                                                                                                                                                        L", " +
 
1330
                                                                                                                                                        wxString::Format(L"%ld", c_index) +
 
1331
 
 
1332
                                                                                                                                                        L");");
 
1333
          #endif // __OPENSUSE__
 
1334
                                #else
 
1335
                                        if(! database->TableExists(L"Borrows")) database->ExecuteUpdate(cs_borrows);
 
1336
                                        database->ExecuteUpdate(L"insert into Borrows values (NULL, '" +
 
1337
                                                                                                                                                N +
 
1338
                                                                                                                                                L"', '" +
 
1339
                                                                                                                                                O +
 
1340
                                                                                                                                                L"', " +
 
1341
                                                                                                                                                wxString::Format(L"%d", (int) D.GetTicks()) +
 
1342
 
 
1343
                                                                                                                                                L", " +
 
1344
                                                                                                                                                wxString::Format(L"%ld", c_index) +
 
1345
 
 
1346
                                                                                                                                                L");");
 
1347
                                #endif // _OMB_USE_CIPHER
 
1348
                        }
 
1349
                }
 
1350
        if(! Parsing){
 
1351
                FileData.Modified = true;
 
1352
                ParseDatabase();}
 
1353
  return true;}
 
1354
 
 
1355
bool TData_v32::AddValue(TTypeVal T, int id, wxString N, double V, long c_index){
 
1356
        if(! Parsing){
 
1357
                if(N.IsEmpty() || V == 0) return false;
 
1358
                N = DoubleQuote(N);
 
1359
        }
 
1360
 
 
1361
        bool E=false;
 
1362
        int x;
 
1363
        wxString curstr;
 
1364
 
 
1365
        #if( wxCHECK_VERSION(3,0,0) )
 
1366
                curstr = ::wxString::FromCDouble(V, 2);
 
1367
        #else
 
1368
                curstr = FormDigits(V);
 
1369
        #endif
 
1370
 
 
1371
        switch(T){
 
1372
                case tvFou:
 
1373
                        for(x = 0; x < NFun; x++)if(Funds[x].Name == N){
 
1374
                                Error(11,N);
 
1375
                                return false;}
 
1376
 
 
1377
                        if(Parsing){
 
1378
                                Funds[NFun].Id = id;
 
1379
                                Funds[NFun].Name = N;
 
1380
                                Funds[NFun].Value = V;}
 
1381
                        else{
 
1382
                                #ifdef _OMB_USE_CIPHER
 
1383
          #ifdef __OPENSUSE__
 
1384
            if(! TableExists(L"Funds", wxEmptyString, database)) ExecuteUpdate(database, cs_funds.c_str());
 
1385
            wxString Sql = L"insert into Funds values (NULL, '" +
 
1386
                                                                                                                                                        N +
 
1387
                                                                                                                                                        L"', " +
 
1388
                                                                                                                                                        curstr +
 
1389
                                                                                                                                                        L");";
 
1390
            ExecuteUpdate(database, Sql.c_str());
 
1391
          #else
 
1392
            if(! TableExists(L"Funds", wxEmptyString, database)) ExecuteUpdate(database, cs_funds);
 
1393
            ExecuteUpdate(database, L"insert into Funds values (NULL, '" +
 
1394
                                                                                                                                                        N +
 
1395
                                                                                                                                                        L"', " +
 
1396
                                                                                                                                                        curstr +
 
1397
                                                                                                                                                        L");");
 
1398
          #endif // __OPENSUSE__
 
1399
                                #else
 
1400
                                        if(! database->TableExists(L"Funds")) database->ExecuteUpdate(cs_funds);
 
1401
 
 
1402
                                        database->ExecuteUpdate(L"insert into Funds values (NULL, '" +
 
1403
                                                                                                                                                N +
 
1404
                                                                                                                                                L"', " +
 
1405
                                                                                                                                                curstr +
 
1406
                                                                                                                                                L");");
 
1407
                        #endif // _OMB_USE_CIPHER
 
1408
                        }
 
1409
 
 
1410
                        break;
 
1411
                case tvCre:
 
1412
                        for(x = 0; x < NCre; x++) if(Credits[x].Name == N){
 
1413
                                Credits[x].Value += V;
 
1414
                                ChangeFundValue(tvCre, Credits[x].Id, Credits[x].Value);
 
1415
                                E = true;
 
1416
                                break;}
 
1417
                        if(!E){
 
1418
                                if(Parsing){
 
1419
                                        Credits[NCre].Id = id;
 
1420
                                        Credits[NCre].Name = N;
 
1421
                                        Credits[NCre].Value = V;
 
1422
                                        Credits[NCre].ContactIndex = c_index;
 
1423
                                }
 
1424
                                else{
 
1425
                                        #ifdef _OMB_USE_CIPHER
 
1426
            #ifdef __OPENSUSE__
 
1427
              if(! TableExists(L"Credits", wxEmptyString, database)) ExecuteUpdate(database, cs_credits.c_str());
 
1428
              wxString Sql = L"insert into Credits values (NULL, '" +
 
1429
                                                                                                                                                                N +
 
1430
                                                                                                                                                                L"', " +
 
1431
                                                                                                                                                                curstr +
 
1432
 
 
1433
                                                                                                                                                                L", " +
 
1434
                                                                                                                                                                wxString::Format(L"%ld", c_index) +
 
1435
 
 
1436
                                                                                                                                                                L");";
 
1437
              ExecuteUpdate(database, Sql.c_str());
 
1438
            #else
 
1439
              if(! TableExists(L"Credits", wxEmptyString, database)) ExecuteUpdate(database, cs_credits);
 
1440
              ExecuteUpdate(database, L"insert into Credits values (NULL, '" +
 
1441
                                                                                                                                                                N +
 
1442
                                                                                                                                                                L"', " +
 
1443
                                                                                                                                                                curstr +
 
1444
 
 
1445
                                                                                                                                                                L", " +
 
1446
                                                                                                                                                                wxString::Format(L"%ld", c_index) +
 
1447
 
 
1448
                                                                                                                                                                L");");
 
1449
            #endif // __OPENSUSE__
 
1450
                                        #else
 
1451
                                                if(! database->TableExists(L"Credits")) database->ExecuteUpdate(cs_credits);
 
1452
 
 
1453
                                                database->ExecuteUpdate(L"insert into Credits values (NULL, '" +
 
1454
                                                                                                                                                        N +
 
1455
                                                                                                                                                        L"', " +
 
1456
                                                                                                                                                        curstr +
 
1457
 
 
1458
                                                                                                                                                        L", " +
 
1459
                                                                                                                                                        wxString::Format(L"%ld", c_index) +
 
1460
 
 
1461
                                                                                                                                                        L");");
 
1462
                                        #endif // _OMB_USE_CIPHER
 
1463
                                }
 
1464
                        }
 
1465
                        break;
 
1466
                case tvDeb:
 
1467
                        for(x = 0; x < NDeb; x++) if(Debts[x].Name == N){
 
1468
                                Debts[x].Value += V;
 
1469
                                ChangeFundValue(tvDeb, Debts[x].Id, Debts[x].Value);
 
1470
                                E = true;
 
1471
                                break;}
 
1472
                        if(!E){
 
1473
                                if(Parsing){
 
1474
                                        Debts[NDeb].Id = id;
 
1475
                                        Debts[NDeb].Name = N;
 
1476
                                        Debts[NDeb].Value = V;
 
1477
                                        Debts[NDeb].ContactIndex = c_index;
 
1478
                                }
 
1479
                                else{
 
1480
                                        #ifdef _OMB_USE_CIPHER
 
1481
            #ifdef __OPENSUSE__
 
1482
              if(! TableExists(L"Debts", wxEmptyString, database)) ExecuteUpdate(database, cs_debts.c_str());
 
1483
              wxString Sql = L"insert into Debts values (NULL, '" +
 
1484
                                                                                                                                                                N +
 
1485
                                                                                                                                                                L"', " +
 
1486
                                                                                                                                                                curstr +
 
1487
 
 
1488
                                                                                                                                                                L", " +
 
1489
                                                                                                                                                                wxString::Format(L"%ld", c_index) +
 
1490
 
 
1491
                                                                                                                                                                L");";
 
1492
              ExecuteUpdate(database, Sql.c_str());
 
1493
            #else
 
1494
              if(! TableExists(L"Debts", wxEmptyString, database)) ExecuteUpdate(database, cs_debts);
 
1495
              ExecuteUpdate(database, L"insert into Debts values (NULL, '" +
 
1496
                                                                                                                                                                N +
 
1497
                                                                                                                                                                L"', " +
 
1498
                                                                                                                                                                curstr +
 
1499
 
 
1500
                                                                                                                                                                L", " +
 
1501
                                                                                                                                                                wxString::Format(L"%ld", c_index) +
 
1502
 
 
1503
                                                                                                                                                                L");");
 
1504
            #endif // __OPENSUSE__
 
1505
                                        #else
 
1506
                                                if(! database->TableExists(L"Debts")) database->ExecuteUpdate(cs_debts);
 
1507
                                                database->ExecuteUpdate(L"insert into Debts values (NULL, '" +
 
1508
                                                                                                                                                        N +
 
1509
                                                                                                                                                        L"', " +
 
1510
                                                                                                                                                        curstr +
 
1511
 
 
1512
                                                                                                                                                        L", " +
 
1513
                                                                                                                                                        wxString::Format(L"%ld", c_index) +
 
1514
 
 
1515
                                                                                                                                                        L");");
 
1516
                                        #endif // _OMB_USE_CIPHER
 
1517
                                }
 
1518
                        }
 
1519
                        break;
 
1520
                default:
 
1521
                        return false;}
 
1522
 
 
1523
        if(! Parsing){
 
1524
                ParseDatabase();
 
1525
                FileData.Modified=true;}
 
1526
        return true;}
 
1527
 
 
1528
/*
 
1529
void TData_v32::CleanMaster(void){
 
1530
        // TODO (igor#1#): To be implemented: ...
 
1531
        //  - clean empty Credits tables
 
1532
        //  - clean empty Debts tables
 
1533
        //  - clean empty Loans tables
 
1534
        //  - clean empty Borros tables
 
1535
        //  - vacuum
 
1536
 
 
1537
}
 
1538
*/
 
1539
 
 
1540
bool TData_v32::ChangeFundValue(TTypeVal type, int id, double V){
 
1541
        wxString table;
 
1542
        switch(type){
 
1543
                case tvFou:
 
1544
                        table = L"Funds";
 
1545
                        break;
 
1546
                case tvCre:
 
1547
                        table = L"Credits";
 
1548
                        break;
 
1549
                case tvDeb:
 
1550
                        table = L"Debts";
 
1551
                        break;
 
1552
                default:
 
1553
                        table = wxEmptyString;}
 
1554
 
 
1555
        #if (wxCHECK_VERSION(3, 0, 0) )
 
1556
                wxString curstr = ::wxString::FromCDouble(V, 2);
 
1557
        #else
 
1558
                wxString curstr = ::wxString::Format(L"%f", V);
 
1559
 
 
1560
                #ifndef __OMBCONVERT_BIN__
 
1561
                        switch(Lan){
 
1562
                                case wxLANGUAGE_ITALIAN:
 
1563
                                        SubstLocaleDecimalSeparator(curstr, ',');
 
1564
                                        break;
 
1565
                                default:
 
1566
                                        ;
 
1567
                        }
 
1568
                #else
 
1569
                        SubstLocaleDecimalSeparator(curstr, ',');
 
1570
                #endif // __OMBCONVERT_BIN__
 
1571
 
 
1572
        #endif  // wxCHECK_VERSION
 
1573
 
 
1574
        #ifdef _OMB_USE_CIPHER
 
1575
    #ifdef __OPENSUSE__
 
1576
      wxString Sql = L"update " + table + L" set value = " +
 
1577
                                                                                                                                curstr +
 
1578
                                                                                                                                L" where id = " +
 
1579
                                                                                                                                wxString::Format(L"%d", id) +
 
1580
                                                                                                                                L";";
 
1581
      ExecuteUpdate(database, Sql.c_str());
 
1582
    #else
 
1583
      ExecuteUpdate(database, L"update " + table + L" set value = " +
 
1584
                                                                                                                                curstr +
 
1585
                                                                                                                                L" where id = " +
 
1586
                                                                                                                                wxString::Format(L"%d", id) +
 
1587
                                                                                                                                L";");
 
1588
    #endif // __OPENSUSE__
 
1589
        #else
 
1590
                database->ExecuteUpdate(L"update " + table + L" set value = " +
 
1591
                                                                                                                        curstr +
 
1592
                                                                                                                        L" where id = " +
 
1593
                                                                                                                        wxString::Format(L"%d", id) +
 
1594
                                                                                                                        L";");
 
1595
        #endif // _OMB_USE_CIPHER
 
1596
 
 
1597
        ParseDatabase();
 
1598
        FileData.Modified=true;
 
1599
        return true;}
 
1600
 
 
1601
#ifndef __OMBCONVERT_BIN__
 
1602
        void TData_v32::XMLExport(wxString F1, wxString F2){
 
1603
                bool FirstDate=false;   // set when first date is inserted
 
1604
                wxString App;   // line to append
 
1605
                double cur; // value storage
 
1606
                //wxArrayString RemarkTokens;
 
1607
                // path selection
 
1608
                wxString dir = wxDirSelector(_("Select a folder"),GetBilDocPath());
 
1609
                if(dir.IsEmpty())return;
 
1610
                ::wxBeginBusyCursor(wxHOURGLASS_CURSOR);
 
1611
                // Folder creation
 
1612
                #ifdef __WXMSW__
 
1613
                        dir = dir + L"\\" + GetDocPrefix() + L"_" + ::wxString::Format("%d", FileData.Year) + L"-"
 
1614
                                + ::wxString::Format("%02d", FileData.Month);
 
1615
                #else
 
1616
                        dir = dir + L"/" + GetDocPrefix() + L"_" + ::wxString::Format(L"%d", FileData.Year);
 
1617
                        #if(wxCHECK_VERSION(3,0,0))
 
1618
                                dir = dir + L"-" + ::wxString::Format(L"%02d", int(FileData.Month));
 
1619
                        #else
 
1620
                                dir = dir + L"-" + ::wxString::Format(L"%02d", FileData.Month);
 
1621
                        #endif
 
1622
                #endif // __WXMSW__
 
1623
                if(!wxDirExists(dir))if(!wxMkdir(dir,0777))return;
 
1624
                // xsl template copy
 
1625
                #ifdef __WXMSW__
 
1626
                        wxString xsltempl=GetInstallationPath()+L"\\data";
 
1627
                #else
 
1628
                        wxString xsltempl=GetDataDir();
 
1629
                #endif // __WXMSW__
 
1630
                #ifdef __WXMSW__
 
1631
                        wxLanguage Lan=FindLang();
 
1632
                #endif
 
1633
                switch(Lan){
 
1634
                        case wxLANGUAGE_ITALIAN:
 
1635
                                #ifndef __WXMSW__
 
1636
                                        xsltempl+=L"/it/";
 
1637
                                #else
 
1638
                                        xsltempl+=L"\\it\\";
 
1639
                                #endif // __WXMSW__
 
1640
                                break;
 
1641
                        default:
 
1642
                                #ifndef __WXMSW__
 
1643
                                        xsltempl+=L"/en/";
 
1644
                                #else
 
1645
                                        xsltempl+=L"\\en\\";
 
1646
                                #endif // __WXMSW__
 
1647
                }
 
1648
                xsltempl += L"ombexport.xsl";
 
1649
                wxCopyFile(xsltempl, dir + L"/ombexport.xsl");
 
1650
                // xml document creation
 
1651
                wxString xmlfile = dir + L"/" + GetDocPrefix() + L".xml";
 
1652
                wxTextFile *file = new wxTextFile(xmlfile);
 
1653
                file->Create();
 
1654
                file->AddLine(L"<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>");
 
1655
                #ifdef __WXMSW__
 
1656
                        file->AddLine(L"<!--File Created By OpenMoneyBox 3.3 on " + wxDateTime::Today().Format(L"%d/%m/%Y") + L" " + wxDateTime::Now().Format(L"%H:%M") + L"-->");
 
1657
                #else
 
1658
                        file->AddLine(L"<!--File Created By OpenMoneyBox 3.3 on " + wxDateTime::Today().Format(L"%d/%02m/%Y") + L" "+wxDateTime::Now().Format(L"%H:%M") + L"-->");
 
1659
                #endif // __WXMSW__
 
1660
                file->AddLine(L"<?xml-stylesheet type=\"text/xsl\" href=\"ombexport.xsl\"?>");
 
1661
                file->AddLine(L"<groups><headers>");
 
1662
                wxString Header=_("Monthly report of");
 
1663
                #ifdef __WXMSW__
 
1664
                        file->AddLine(L"<title heading=\""+Header+L" \" month=\""+IntToMonth(FileData.Month)+L" "+::wxString::Format("%d",FileData.Year)+L"\" />");
 
1665
                #else
 
1666
                        file->AddLine(L"<title heading=\""+Header+L" \" month=\""+IntToMonth(FileData.Month)+L" "+::wxString::Format(L"%d",FileData.Year)+L"\" />");
 
1667
                #endif // __WXMSW__
 
1668
                file->AddLine(L"</headers><days>");
 
1669
                // line parsing
 
1670
                for(int i = 0; i < NLin; i++){
 
1671
                        App=wxEmptyString;
 
1672
                        if(IsDate(i)){
 
1673
                                if(!FirstDate)FirstDate=true;
 
1674
                                else    file->AddLine(L"</day>");
 
1675
                                App+=L"<day date=\"";
 
1676
                                #ifdef __WXMSW__
 
1677
                                        App+=Lines[i].Date.Format("%a %d/%m/%Y",wxDateTime::Local);
 
1678
                                #else
 
1679
                                        App+=Lines[i].Date.Format(L"%a %d/%m/%Y",wxDateTime::Local);
 
1680
                                #endif // __WXMSW__
 
1681
                                App+=L"\" total=\"";
 
1682
                                Lines[i].Value.ToDouble(&cur);
 
1683
                                App+=FormDigits(cur);
 
1684
                                App+=L"\">";
 
1685
                                file->AddLine(App);}
 
1686
                        else{
 
1687
                                App+=L"<item time=\"";
 
1688
                                #ifdef __WXMSW__
 
1689
                                        App+=Lines[i].Time.Format("%H:%M",wxDateTime::Local);
 
1690
                                #else
 
1691
                                        App+=Lines[i].Time.Format(L"%H:%M",wxDateTime::Local);
 
1692
                                #endif // __WXMSW__
 
1693
                                App+=L"\" type=\"";
 
1694
                                switch(Lines[i].Type){
 
1695
                                        case toGain:
 
1696
                                        App+=L"1";
 
1697
                                        break;
 
1698
                                        case toExpe:
 
1699
                                        App+=L"2";
 
1700
                                        break;
 
1701
                                        case toSetCre:
 
1702
                                        App+=L"3";
 
1703
                                        break;
 
1704
                                        case toRemCre:
 
1705
                                        App+=L"4";
 
1706
                                        break;
 
1707
                                        case toConCre:
 
1708
                                        App+=L"5";
 
1709
                                        break;
 
1710
                                        case toSetDeb:
 
1711
                                        App+=L"6";
 
1712
                                        break;
 
1713
                                        case toRemDeb:
 
1714
                                        App+=L"7";
 
1715
                                        break;
 
1716
                                        case toConDeb:
 
1717
                                        App+=L"8";
 
1718
                                        break;
 
1719
                                        case toGetObj:
 
1720
                                        App+=L"9";
 
1721
                                        break;
 
1722
                                        case toGivObj:
 
1723
                                        App+=L"10";
 
1724
                                        break;
 
1725
                                        case toLenObj:
 
1726
                                        App+=L"11";
 
1727
                                        break;
 
1728
                                        case toBakObj:
 
1729
                                        App+=L"12";
 
1730
                                        break;
 
1731
                                        case toBorObj:
 
1732
                                        App+=L"13";
 
1733
                                        break;
 
1734
                                        case toRetObj:
 
1735
                                        App+=L"14";
 
1736
                                        break;
 
1737
                                        default:
 
1738
                                        App+=L"0";
 
1739
                                        break;}
 
1740
                                App += L"\" value=\"";
 
1741
                                if(Lines[i].Type < 9){
 
1742
                                        Lines[i].Value.ToDouble(&cur);
 
1743
                                        App += FormDigits(cur);}
 
1744
                                else{
 
1745
                                        if(Lines[i].Value.Find(L"\"") != wxNOT_FOUND){
 
1746
                                                wxString vals = Lines[i].Value;
 
1747
                                                vals.Replace(L"\"", L"[OMB_ESCAPEQUOTES]", true);       // intermediate string to be processed by SubstSpecialChars
 
1748
                                                App += vals;
 
1749
                                        }
 
1750
                                        else App += Lines[i].Value;
 
1751
                                }
 
1752
 
 
1753
                                App += L"\" reason=\"";
 
1754
 
 
1755
                                if(Lines[i].Reason.Find(L"\"") != wxNOT_FOUND){
 
1756
                                        wxString reas = Lines[i].Reason;
 
1757
                                        reas.Replace(L"\"", L"[OMB_ESCAPEQUOTES]", true);       // intermediate string to be processed by SubstSpecialChars
 
1758
                                        App += reas;
 
1759
                                }
 
1760
                                else App+=Lines[i].Reason;
 
1761
 
 
1762
                                App+=L"\" category=\"";
 
1763
                                App=SubstSpecialChars(App);
 
1764
                                file->AddLine(App);
 
1765
                                if(Lines[i].CategoryIndex != -1)
 
1766
                                        for(int j = 0; j < NCat; j++)if(Categories[j].Id == Lines[i].CategoryIndex){
 
1767
                                                file->AddLine(Categories[j].Name);
 
1768
                                                break;}
 
1769
                                file->AddLine(L"\"/>");}}
 
1770
                // file completion
 
1771
                file->AddLine(L"</day></days></groups>");
 
1772
                file->Write(wxTextFileType_Unix,wxConvUTF8);
 
1773
                // file closure
 
1774
                file->Close();
 
1775
                delete file;
 
1776
                // Logo picture copy
 
1777
                #ifndef __WXMSW__
 
1778
                        xsltempl=GetDataDir()+L"logo.png";
 
1779
                #else
 
1780
                        xsltempl=GetInstallationPath()+L"\\data\\logo.png";
 
1781
                #endif
 
1782
                wxCopyFile(xsltempl,dir+L"/logo.png");
 
1783
                // Chart copy
 
1784
                wxCopyFile(F1, dir+L"/chart1.png");
 
1785
                wxCopyFile(F2, dir+L"/chart2.png");
 
1786
                ::wxEndBusyCursor();}
 
1787
#endif // __OMBCONVERT_BIN__
 
1788
 
 
1789
#ifndef __OMBCONVERT_BIN__
 
1790
void TData_v32::Archive_inMaster(wxString master, wxString trailname, bool archive_categories){
 
1791
        // master:              database to attach and archive data in
 
1792
        // trailname:   suffix for tables
 
1793
        // archive_categories:  true to archive also categories
 
1794
 
 
1795
        #if(wxCHECK_VERSION(3, 0, 0))
 
1796
                #ifdef _OMB_USE_CIPHER
 
1797
                        AttachMaster();
 
1798
 
 
1799
                        // Archive data in master database
 
1800
                        ExecuteUpdate(database, "INSERT INTO master.Transactions (isdate, date, time, type, value, reason, cat_index, contact_index, latitude, longitude) SELECT isdate, date, time, type, value, reason, cat_index, contact_index, latitude, longitude FROM Transactions;");
 
1801
                        #ifdef __OPENSUSE__
 
1802
        wxString Sql = wxString::Format("INSERT INTO master.Funds%s SELECT * FROM Funds;", trailname);
 
1803
        ExecuteUpdate(database, Sql.c_str());
 
1804
        Sql = wxString::Format("INSERT INTO master.Credits%s SELECT * FROM Credits;", trailname);
 
1805
        ExecuteUpdate(database, Sql.c_str());
 
1806
        Sql = wxString::Format("INSERT INTO master.Debts%s SELECT * FROM Debts;", trailname);
 
1807
        ExecuteUpdate(database, Sql.c_str());
 
1808
        Sql = wxString::Format("INSERT INTO master.Loans%s SELECT * FROM Loans;", trailname);
 
1809
        ExecuteUpdate(database, Sql.c_str());
 
1810
        Sql = wxString::Format("INSERT INTO master.Borrows%s SELECT * FROM Borrows;", trailname);
 
1811
        ExecuteUpdate(database, Sql.c_str());
 
1812
                        #else
 
1813
        ExecuteUpdate(database, wxString::Format("INSERT INTO master.Funds%s SELECT * FROM Funds;", trailname));
 
1814
        ExecuteUpdate(database, wxString::Format("INSERT INTO master.Credits%s SELECT * FROM Credits;", trailname));
 
1815
        ExecuteUpdate(database, wxString::Format("INSERT INTO master.Debts%s SELECT * FROM Debts;", trailname));
 
1816
        ExecuteUpdate(database, wxString::Format("INSERT INTO master.Loans%s SELECT * FROM Loans;", trailname));
 
1817
        ExecuteUpdate(database, wxString::Format("INSERT INTO master.Borrows%s SELECT * FROM Borrows;", trailname));
 
1818
      #endif // __OPENSUSE__
 
1819
                #else
 
1820
                        database->ExecuteUpdate(wxString::Format("ATTACH DATABASE '%s' AS master;", master));
 
1821
 
 
1822
                        // Archive data in master database
 
1823
                        database->ExecuteUpdate("INSERT INTO master.Transactions (isdate, date, time, type, value, reason, cat_index, contact_index, latitude, longitude) SELECT isdate, date, time, type, value, reason, cat_index, contact_index, latitude, longitude FROM Transactions;");
 
1824
                        database->ExecuteUpdate(wxString::Format("INSERT INTO master.Funds%s SELECT * FROM Funds;", trailname));
 
1825
                        database->ExecuteUpdate(wxString::Format("INSERT INTO master.Credits%s SELECT * FROM Credits;", trailname));
 
1826
                        database->ExecuteUpdate(wxString::Format("INSERT INTO master.Debts%s SELECT * FROM Debts;", trailname));
 
1827
                        database->ExecuteUpdate(wxString::Format("INSERT INTO master.Loans%s SELECT * FROM Loans;", trailname));
 
1828
                        database->ExecuteUpdate(wxString::Format("INSERT INTO master.Borrows%s SELECT * FROM Borrows;", trailname));
 
1829
                #endif // _OMB_USE_CIPHER
 
1830
        #else
 
1831
                database->ExecuteUpdate(wxString::Format(L"ATTACH DATABASE '%s' AS master;", master.c_str()));
 
1832
 
 
1833
                // Archive data in master database
 
1834
                database->ExecuteUpdate("INSERT INTO master.Transactions (isdate, date, time, type, value, reason, cat_index, contact_index, latitude, longitude) SELECT isdate, date, time, type, value, reason, cat_index, contact_index, latitude, longitude FROM Transactions;");
 
1835
                database->ExecuteUpdate(wxString::Format(L"INSERT INTO master.Funds%s SELECT * FROM Funds;", trailname.c_str()));
 
1836
                database->ExecuteUpdate(wxString::Format(L"INSERT INTO master.Credits%s SELECT * FROM Credits;", trailname.c_str()));
 
1837
                database->ExecuteUpdate(wxString::Format(L"INSERT INTO master.Debts%s SELECT * FROM Debts;", trailname.c_str()));
 
1838
                database->ExecuteUpdate(wxString::Format(L"INSERT INTO master.Loans%s SELECT * FROM Loans;", trailname.c_str()));
 
1839
                database->ExecuteUpdate(wxString::Format(L"INSERT INTO master.Borrows%s SELECT * FROM Borrows;", trailname.c_str()));
 
1840
        #endif  // wxCHECK_VERSION
 
1841
 
 
1842
        #ifdef _OMB_USE_CIPHER
 
1843
                if(archive_categories) ExecuteUpdate(database, "INSERT INTO master.Categories SELECT * FROM Categories;");
 
1844
                ExecuteUpdate(database, "DETACH DATABASE 'master';");
 
1845
        #else
 
1846
                if(archive_categories) database->ExecuteUpdate("INSERT INTO master.Categories SELECT * FROM Categories;");
 
1847
                database->ExecuteUpdate("DETACH DATABASE 'master';");
 
1848
        #endif // _OMB_USE_CIPHER
 
1849
}
 
1850
#endif // __OMBCONVERT_BIN__
 
1851
 
 
1852
#ifndef __OMBCONVERT_BIN__
 
1853
bool TData_v32::Prepare_MasterDB(wxString dbPath, wxString trailname){
 
1854
        // master:              database to attach and archive data in
 
1855
        // trailname:   suffix for tables
 
1856
 
 
1857
        bool file_exist = ::wxFileExists(dbPath);
 
1858
 
 
1859
        #ifdef _OMB_USE_CIPHER
 
1860
                wxString pwd_archive = GetKey(true);
 
1861
 
 
1862
                sqlite3 *db;
 
1863
                #ifdef __OPENSUSE__
 
1864
      sqlite3_open(dbPath.c_str(), &db);
 
1865
      sqlite3_key(db, pwd_archive.c_str(), pwd_archive.length());
 
1866
                #else
 
1867
      sqlite3_open(dbPath, &db);
 
1868
      sqlite3_key(db, pwd_archive, pwd_archive.length());
 
1869
    #endif // __OPENSUSE__
 
1870
 
 
1871
                int rc = sqlite3_exec(db, "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL);
 
1872
                switch(rc){
 
1873
                        case SQLITE_OK:
 
1874
                                //result = 1;
 
1875
                                break;
 
1876
 
 
1877
                        case SQLITE_NOTADB:
 
1878
                                PasswordPrompt(db, true);
 
1879
                                break;
 
1880
 
 
1881
                        //default:
 
1882
                }
 
1883
 
 
1884
        #else
 
1885
                wxSQLite3Database *db = new wxSQLite3Database();
 
1886
                db->Open(dbPath, wxEmptyString, WXSQLITE_OPEN_READWRITE | WXSQLITE_OPEN_CREATE );
 
1887
        #endif // _OMB_USE_CIPHER
 
1888
 
 
1889
        #ifdef _OMB_USE_CIPHER
 
1890
    #ifdef __OPENSUSE__
 
1891
      if(! file_exist){
 
1892
        wxString Sql = L"pragma user_version = " +
 
1893
                                                                                                                                                                                ::wxString::Format(L"%d", dbVersion) +
 
1894
                                                                                                                                                                                L";";
 
1895
        ExecuteUpdate(db, Sql.c_str());
 
1896
      }
 
1897
    #else
 
1898
      if(! file_exist) ExecuteUpdate(db, L"pragma user_version = " +
 
1899
                                                                                                                                                                                ::wxString::Format(L"%d", dbVersion) +
 
1900
                                                                                                                                                                                L";");
 
1901
    #endif // __OPENSUSE__
 
1902
        #else
 
1903
                if(! file_exist) db->ExecuteUpdate(L"pragma user_version = " +
 
1904
                                                                                                                                                                                ::wxString::Format(L"%d", dbVersion) +
 
1905
                                                                                                                                                                                L";");
 
1906
        #endif // _OMB_USE_CIPHER
 
1907
 
 
1908
        wxString metadata;
 
1909
        #if wxCHECK_VERSION(3, 0, 0)
 
1910
                // Write file metadata
 
1911
                metadata = L"OS: ";
 
1912
                #ifdef __WXGTK__
 
1913
                        metadata += wxGetLinuxDistributionInfo().Description;
 
1914
                #elif defined ( __WXMSW__)
 
1915
                        metadata += wxGetOsDescription();
 
1916
                #else
 
1917
                        ,etadata += L"unknown";
 
1918
                #endif // __WXGTK__
 
1919
                metadata += L"\n\n";
 
1920
                #ifndef __OMBCONVERT_BIN__
 
1921
                        metadata += wxGetLibraryVersionInfo().ToString();
 
1922
                #else
 
1923
                        metadata += _("Converted with ombconvert");
 
1924
                #endif // __OMBCONVERT_BIN__
 
1925
        #else
 
1926
                metadata = L"wxWidgets 2.8.0";
 
1927
        #endif // wxCHECK_VERSION
 
1928
 
 
1929
        #ifdef _OMB_USE_CIPHER
 
1930
                if(! TableExists(L"Information", "master", db)){
 
1931
      #ifdef __OPENSUSE__
 
1932
        ExecuteUpdate(db, cs_information.c_str());
 
1933
        wxString Sql = L"insert into Information values (" +
 
1934
                                                                                                                                wxString::Format(L"%d", dbMeta_application_info) +
 
1935
                                                                                                                                L", '" +
 
1936
                                                                                                                                metadata +
 
1937
                                                                                                                                L"');";
 
1938
        ExecuteUpdate(db, Sql.c_str());
 
1939
      #else
 
1940
        ExecuteUpdate(db, cs_information);
 
1941
        ExecuteUpdate(db, L"insert into Information values (" +
 
1942
                                                                                                                                wxString::Format(L"%d", dbMeta_application_info) +
 
1943
                                                                                                                                L", '" +
 
1944
                                                                                                                                metadata +
 
1945
                                                                                                                                L"');");
 
1946
      #endif // __OPENSUSE__
 
1947
                }
 
1948
                else
 
1949
      #ifdef __OPENSUSE__
 
1950
        {
 
1951
          wxString Sql = L"update Information set data = \"" +
 
1952
                                                                                                                                metadata +
 
1953
                                                                                                                                L"\" where id = " +
 
1954
                                                                                                                                wxString::Format(L"%d", dbMeta_application_info) +
 
1955
                                                                                                                                L";";
 
1956
          ExecuteUpdate(db, Sql.c_str());
 
1957
        }
 
1958
      #else
 
1959
        ExecuteUpdate(db, L"update Information set data = \"" +
 
1960
                                                                                                                                metadata +
 
1961
                                                                                                                                L"\" where id = " +
 
1962
                                                                                                                                wxString::Format(L"%d", dbMeta_application_info) +
 
1963
                                                                                                                                L";");
 
1964
      #endif // __OPENSUSE__
 
1965
        #else
 
1966
                if(! db->TableExists(L"Information")){
 
1967
                        db->ExecuteUpdate(cs_information);
 
1968
                        db->ExecuteUpdate(L"insert into Information values (" +
 
1969
                                                                                                                        wxString::Format(L"%d", dbMeta_application_info) +
 
1970
                                                                                                                        L", '" +
 
1971
                                                                                                                        metadata +
 
1972
                                                                                                                        L"');");
 
1973
                }
 
1974
                else db->ExecuteUpdate(L"update Information set data = \"" +
 
1975
                                                                                                                        metadata +
 
1976
                                                                                                                        L"\" where id = " +
 
1977
                                                                                                                        wxString::Format(L"%d", dbMeta_application_info) +
 
1978
                                                                                                                        L";");
 
1979
        #endif // _OMB_USE_CIPHER
 
1980
 
 
1981
        #ifdef _OMB_USE_CIPHER
 
1982
    #ifdef __OPENSUSE__
 
1983
      // Transaction table init
 
1984
      if(! TableExists(L"Transactions", "master", db)) ExecuteUpdate(db, cs_transactions.c_str());
 
1985
    #else
 
1986
      // Transaction table init
 
1987
      if(! TableExists(L"Transactions", "master", db)) ExecuteUpdate(db, cs_transactions);
 
1988
    #endif // __OPENSUSE__
 
1989
        #else
 
1990
                // Transaction table init
 
1991
                if(! db->TableExists(L"Transactions")) db->ExecuteUpdate(cs_transactions);
 
1992
        #endif // _OMB_USE_CIPHER
 
1993
 
 
1994
        #ifdef _OMB_USE_CIPHER
 
1995
                // Category table init
 
1996
                bool master_had_categories = TableExists(L"Categories", "master", db);
 
1997
        #else
 
1998
                // Category table init
 
1999
                bool master_had_categories = db->TableExists(L"Categories");
 
2000
        #endif // _OMB_USE_CIPHER
 
2001
 
 
2002
        if(master_had_categories){
 
2003
                #if(wxCHECK_VERSION(3, 0, 0))
 
2004
                        #ifdef _OMB_USE_CIPHER
 
2005
                                if(! TableExists("Categories" + trailname, "master", db))
 
2006
          #ifdef __OPENSUSE__
 
2007
            {
 
2008
              wxString Sql = wxString::Format(cs_categories_master, trailname);
 
2009
              ExecuteUpdate(db, Sql.c_str());
 
2010
            }
 
2011
          #else
 
2012
            ExecuteUpdate(db, wxString::Format(cs_categories_master, trailname));
 
2013
          #endif // __OPENSUSE__
 
2014
                        #else
 
2015
                                if(! db->TableExists("Categories" + trailname)) db->ExecuteUpdate(wxString::Format(cs_categories_master, trailname));
 
2016
                        #endif // _OMB_USE_CIPHER
 
2017
                #else
 
2018
                        if(! db->TableExists(L"Categories" + trailname)) db->ExecuteUpdate(wxString::Format(cs_categories_master, trailname.c_str()));
 
2019
                #endif  // wxCHECK_VERSION
 
2020
        }
 
2021
        #ifdef _OMB_USE_CIPHER
 
2022
    #ifdef __OPENSUSE__
 
2023
      else ExecuteUpdate(db, cs_categories.c_str());
 
2024
    #else
 
2025
      else ExecuteUpdate(db, cs_categories);
 
2026
    #endif // __OPENSUSE__
 
2027
        #else
 
2028
                else db->ExecuteUpdate(cs_categories);
 
2029
        #endif // _OMB_USE_CIPHER
 
2030
 
 
2031
        #if(wxCHECK_VERSION(3, 0, 0))
 
2032
                #ifdef _OMB_USE_CIPHER
 
2033
      #ifdef __OPENSUSE__
 
2034
        wxString Sql;
 
2035
        if(! TableExists("Funds" + trailname, "master", db)){
 
2036
          Sql = wxString::Format(cs_funds_master, trailname);
 
2037
          ExecuteUpdate(db, Sql.c_str());
 
2038
        }
 
2039
        if(! TableExists("Credits" + trailname, "master", db)){
 
2040
          Sql = wxString::Format(cs_credits_master, trailname);
 
2041
          ExecuteUpdate(db, Sql.c_str());
 
2042
        }
 
2043
        if(! TableExists("Debts" + trailname, "master", db)){
 
2044
          Sql = wxString::Format(cs_debts_master, trailname);
 
2045
          ExecuteUpdate(db, Sql.c_str());
 
2046
        }
 
2047
        if(! TableExists("Loans" + trailname, "master", db)){
 
2048
          Sql = wxString::Format(cs_loans_master, trailname);
 
2049
          ExecuteUpdate(db, Sql.c_str());
 
2050
        }
 
2051
        if(! TableExists("Borrows" + trailname, "master", db)){
 
2052
          Sql = wxString::Format(cs_borrows_master, trailname);
 
2053
          ExecuteUpdate(db, Sql.c_str());
 
2054
        }
 
2055
      #else
 
2056
        if(! TableExists("Funds" + trailname, "master", db)) ExecuteUpdate(db, wxString::Format(cs_funds_master, trailname));
 
2057
        if(! TableExists("Credits" + trailname, "master", db)) ExecuteUpdate(db, wxString::Format(cs_credits_master, trailname));
 
2058
        if(! TableExists("Debts" + trailname, "master", db)) ExecuteUpdate(db, wxString::Format(cs_debts_master, trailname));
 
2059
        if(! TableExists("Loans" + trailname, "master", db)) ExecuteUpdate(db, wxString::Format(cs_loans_master, trailname));
 
2060
        if(! TableExists("Borrows" + trailname, "master", db)) ExecuteUpdate(db, wxString::Format(cs_borrows_master, trailname));
 
2061
      #endif // __OPENSUSE__
 
2062
                #else
 
2063
                        if(! db->TableExists("Funds" + trailname)) db->ExecuteUpdate(wxString::Format(cs_funds_master, trailname));
 
2064
                        if(! db->TableExists("Credits" + trailname)) db->ExecuteUpdate(wxString::Format(cs_credits_master, trailname));
 
2065
                        if(! db->TableExists("Debts" + trailname)) db->ExecuteUpdate(wxString::Format(cs_debts_master, trailname));
 
2066
                        if(! db->TableExists("Loans" + trailname)) db->ExecuteUpdate(wxString::Format(cs_loans_master, trailname));
 
2067
                        if(! db->TableExists("Borrows" + trailname)) db->ExecuteUpdate(wxString::Format(cs_borrows_master, trailname));
 
2068
                #endif // _OMB_USE_CIPHER
 
2069
        #else
 
2070
                if(! db->TableExists(L"Funds" + trailname)) db->ExecuteUpdate(wxString::Format(cs_funds_master, trailname.c_str()));
 
2071
                if(! db->TableExists(L"Credits" + trailname)) db->ExecuteUpdate(wxString::Format(cs_credits_master, trailname.c_str()));
 
2072
                if(! db->TableExists(L"Debts" + trailname)) db->ExecuteUpdate(wxString::Format(cs_debts_master, trailname.c_str()));
 
2073
                if(! db->TableExists(L"Loans" + trailname)) db->ExecuteUpdate(wxString::Format(cs_loans_master, trailname.c_str()));
 
2074
                if(! db->TableExists(L"Borrows" + trailname)) db->ExecuteUpdate(wxString::Format(cs_borrows_master, trailname.c_str()));
 
2075
        #endif  // wxCHECK_VERSION
 
2076
 
 
2077
        #ifdef _OMB_USE_CIPHER
 
2078
                sqlite3_close(db);
 
2079
        #else
 
2080
                db->Close();
 
2081
        #endif // _OMB_USE_CIPHER
 
2082
 
 
2083
        return master_had_categories;
 
2084
}
 
2085
#endif // __OMBCONVERT_BIN__
 
2086
 
 
2087
#ifndef __OMBCONVERT_BIN__
 
2088
        void TData_v32::XMLExport_archive(wxString F1, wxString F2, wxDateTime date){
 
2089
                bool FirstDate=false;   // set when first date is inserted
 
2090
                wxString App;   // line to append
 
2091
                double cur; // value storage
 
2092
                // path selection
 
2093
                wxString dir = wxDirSelector(_("Select a folder"),GetBilDocPath());
 
2094
                if(dir.IsEmpty())return;
 
2095
                ::wxBeginBusyCursor(wxHOURGLASS_CURSOR);
 
2096
                // Folder creation
 
2097
                #ifdef __WXMSW__
 
2098
                        dir = dir + L"\\" + GetDocPrefix() + L"_" + date.Format(L"%Y-%m");
 
2099
                #else
 
2100
                        dir = dir + L"/" + GetDocPrefix() + L"_" + date.Format(L"%Y-%m");
 
2101
                #endif // __WXMSW__
 
2102
                if(!wxDirExists(dir))if(!wxMkdir(dir,0777))return;
 
2103
                // xsl template copy
 
2104
                #ifdef __WXMSW__
 
2105
                        wxString xsltempl=GetInstallationPath()+L"\\data";
 
2106
                #else
 
2107
                        wxString xsltempl=GetDataDir();
 
2108
                #endif // __WXMSW__
 
2109
                #ifdef __WXMSW__
 
2110
                        wxLanguage Lan=FindLang();
 
2111
                #endif
 
2112
                switch(Lan){
 
2113
                        case wxLANGUAGE_ITALIAN:
 
2114
                                #ifndef __WXMSW__
 
2115
                                        xsltempl+=L"/it/";
 
2116
                                #else
 
2117
                                        xsltempl+=L"\\it\\";
 
2118
                                #endif // __WXMSW__
 
2119
                                break;
 
2120
                        default:
 
2121
                                #ifndef __WXMSW__
 
2122
                                        xsltempl+=L"/en/";
 
2123
                                #else
 
2124
                                        xsltempl+=L"\\en\\";
 
2125
                                #endif // __WXMSW__
 
2126
                }
 
2127
                xsltempl += L"ombexport.xsl";
 
2128
                wxCopyFile(xsltempl, dir + L"/ombexport.xsl");
 
2129
                // xml document creation
 
2130
                wxString xmlfile = dir + L"/" + GetDocPrefix() + L".xml";
 
2131
                wxTextFile *file=new wxTextFile(xmlfile);
 
2132
                file->Create();
 
2133
                file->AddLine(L"<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>");
 
2134
                #ifdef __WXMSW__
 
2135
                        file->AddLine(L"<!--File Created By OpenMoneyBox 3.3 on " + wxDateTime::Today().Format(L"%d/%m/%Y") + L" " + wxDateTime::Now().Format(L"%H:%M") + L"-->");
 
2136
                #else
 
2137
                        file->AddLine(L"<!--File Created By OpenMoneyBox 3.3 on " + wxDateTime::Today().Format(L"%d/%02m/%Y") + L" " + wxDateTime::Now().Format(L"%H:%M") + L"-->");
 
2138
                #endif // __WXMSW__
 
2139
                file->AddLine(L"<?xml-stylesheet type=\"text/xsl\" href=\"ombexport.xsl\"?>");
 
2140
                file->AddLine(L"<groups><headers>");
 
2141
                wxString Header=_("Monthly report of");
 
2142
                #ifdef __WXMSW__
 
2143
                        file->AddLine(L"<title heading=\""+Header+L" \" month=\""+IntToMonth(FileData.Month)+L" "+::wxString::Format("%d",FileData.Year)+L"\" />");
 
2144
                #else
 
2145
                        file->AddLine(L"<title heading=\"" + Header + L" \" month=\"" + IntToMonth(/*FileData.Month*/date.GetMonth() + 1) + L" " +
 
2146
                                                                                        ::wxString::Format(L"%d", /*FileData.Year*/date.GetYear()) + L"\" />");
 
2147
                #endif // __WXMSW__
 
2148
                file->AddLine(L"</headers><days>");
 
2149
 
 
2150
                int min_date, max_date;
 
2151
                wxDateTime min_datetime, max_datetime, d_date, time;
 
2152
                wxSQLite3Table transaction_table;
 
2153
                wxString value_string, reas;
 
2154
 
 
2155
                min_datetime = date;
 
2156
                min_datetime.SetDay(1);
 
2157
                min_datetime.SetHour(0);
 
2158
                min_datetime.SetMinute(0);
 
2159
                min_datetime.SetSecond(0);
 
2160
                min_datetime.SetMillisecond(0);
 
2161
                min_date = min_datetime.GetTicks();
 
2162
 
 
2163
                max_datetime = date;
 
2164
                max_datetime.SetToLastMonthDay(max_datetime.GetMonth(), max_datetime.GetYear());
 
2165
                max_datetime.SetHour(23);
 
2166
                max_datetime.SetMinute(59);
 
2167
                max_datetime.SetSecond(59);
 
2168
                max_datetime.SetMillisecond(999);
 
2169
                max_date = max_datetime.GetTicks();
 
2170
 
 
2171
                #ifdef _OMB_USE_CIPHER
 
2172
      #ifdef __OPENSUSE__
 
2173
        wxString Sql = ::wxString::Format(L"SELECT * FROM master.Transactions WHERE date >= %d and date <= %d;", min_date, max_date);
 
2174
        transaction_table = GetTable(database, Sql.c_str());
 
2175
      #else
 
2176
        transaction_table = GetTable(database, ::wxString::Format(L"SELECT * FROM master.Transactions WHERE date >= %d and date <= %d;", min_date, max_date));
 
2177
      #endif // __OPENSUSE__
 
2178
                #else
 
2179
                        transaction_table = database->GetTable(::wxString::Format(L"SELECT * FROM master.Transactions WHERE date >= %d and date <= %d;", min_date, max_date));
 
2180
                #endif // _OMB_USE_CIPHER
 
2181
 
 
2182
                // line parsing
 
2183
                for(int i = 0; i < transaction_table.GetRowCount(); i++){
 
2184
                        transaction_table.SetRow(i);
 
2185
                        App = wxEmptyString;
 
2186
                        if(transaction_table.GetInt(L"isdate", false)){
 
2187
                                if(! FirstDate) FirstDate = true;
 
2188
                                else    file->AddLine(L"</day>");
 
2189
                                App += L"<day date=\"";
 
2190
                                d_date = wxDateTime((time_t) transaction_table.GetInt(2, 0));
 
2191
                                #ifdef __WXMSW__
 
2192
                                        App += d_date.Format("%a %d/%m/%Y", wxDateTime::Local);
 
2193
                                #else
 
2194
                                        App += d_date.Format(L"%a %d/%m/%Y", wxDateTime::Local);
 
2195
                                #endif // __WXMSW__
 
2196
                                App += L"\" total=\"";
 
2197
                                value_string = transaction_table.GetString(5, wxEmptyString);
 
2198
                                #if( wxCHECK_VERSION(3,0,0) )
 
2199
                                        value_string.ToCDouble(&cur);
 
2200
                                #else
 
2201
                                        value_string.ToDouble(&cur);
 
2202
                                #endif
 
2203
                                App += FormDigits(cur);
 
2204
                                App += L"\">";
 
2205
                                file->AddLine(App);}
 
2206
                        else{
 
2207
                                App+=L"<item time=\"";
 
2208
                                #ifdef __WXMSW__
 
2209
                                        App+=Lines[i].Time.Format("%H:%M",wxDateTime::Local);
 
2210
                                #else
 
2211
                                        time = wxDateTime((time_t) transaction_table.GetInt(3, 0));
 
2212
                                        App += time.Format(L"%H:%M",wxDateTime::Local);
 
2213
                                #endif // __WXMSW__
 
2214
                                App += L"\" type=\"";
 
2215
                                switch(transaction_table.GetInt(4, 0)){
 
2216
                                        case toGain:
 
2217
                                                App += L"1";
 
2218
                                                break;
 
2219
                                        case toExpe:
 
2220
                                                App += L"2";
 
2221
                                                break;
 
2222
                                        case toSetCre:
 
2223
                                                App += L"3";
 
2224
                                                break;
 
2225
                                        case toRemCre:
 
2226
                                                App += L"4";
 
2227
                                                break;
 
2228
                                        case toConCre:
 
2229
                                                App += L"5";
 
2230
                                                break;
 
2231
                                        case toSetDeb:
 
2232
                                                App += L"6";
 
2233
                                                break;
 
2234
                                        case toRemDeb:
 
2235
                                                App += L"7";
 
2236
                                                break;
 
2237
                                        case toConDeb:
 
2238
                                                App += L"8";
 
2239
                                                break;
 
2240
                                        case toGetObj:
 
2241
                                                App += L"9";
 
2242
                                                break;
 
2243
                                        case toGivObj:
 
2244
                                                App += L"10";
 
2245
                                                break;
 
2246
                                        case toLenObj:
 
2247
                                                App += L"11";
 
2248
                                                break;
 
2249
                                        case toBakObj:
 
2250
                                                App += L"12";
 
2251
                                                break;
 
2252
                                        case toBorObj:
 
2253
                                                App += L"13";
 
2254
                                                break;
 
2255
                                        case toRetObj:
 
2256
                                                App += L"14";
 
2257
                                                break;
 
2258
                                        default:
 
2259
                                                App += L"0";
 
2260
                                                break;}
 
2261
                                App += L"\" value=\"";
 
2262
                                value_string = transaction_table.GetString(5, wxEmptyString);
 
2263
                                if(transaction_table.GetInt(4, 0) < 9){
 
2264
                                        #if( wxCHECK_VERSION(3,0,0) )
 
2265
                                                value_string.ToCDouble(&cur);
 
2266
                                        #else
 
2267
                                                value_string.ToDouble(&cur);
 
2268
                                        #endif
 
2269
                                        App += FormDigits(cur);}
 
2270
                                else App += value_string;
 
2271
                                App += L"\" reason=\"";
 
2272
 
 
2273
                                reas = transaction_table.GetString(6, wxEmptyString);
 
2274
                                if(reas.Find(L"\"") != wxNOT_FOUND)
 
2275
                                        reas.Replace(L"\"", L"[OMB_ESCAPEQUOTES]", true);       // intermediate string to be processed by SubstSpecialChars
 
2276
                                App += reas;
 
2277
 
 
2278
                                App += L"\" category=\"";
 
2279
                                App = SubstSpecialChars(App);
 
2280
                                file->AddLine(App);
 
2281
                                if(transaction_table.GetInt(7, 0) != -1)
 
2282
                                        for(int j = 0; j < NCat; j++)if(Categories[j].Id == transaction_table.GetInt(7, 0)){
 
2283
                                                file->AddLine(Categories[j].Name);
 
2284
                                                break;}
 
2285
                                file->AddLine(L"\"/>");}}
 
2286
                // file completion
 
2287
                file->AddLine(L"</day></days></groups>");
 
2288
                file->Write(wxTextFileType_Unix,wxConvUTF8);
 
2289
                // file closure
 
2290
                file->Close();
 
2291
                delete file;
 
2292
                // Logo picture copy
 
2293
                #ifndef __WXMSW__
 
2294
                        xsltempl=GetDataDir()+L"logo.png";
 
2295
                #else
 
2296
                        xsltempl=GetInstallationPath()+L"\\data\\logo.png";
 
2297
                #endif
 
2298
                wxCopyFile(xsltempl,dir+L"/logo.png");
 
2299
                // Chart copy
 
2300
                wxCopyFile(F1, dir+L"/chart1.png");
 
2301
                wxCopyFile(F2, dir+L"/chart2.png");
 
2302
                ::wxEndBusyCursor();}
 
2303
#endif // __OMBCONVERT_BIN__
 
2304
 
 
2305
bool TData_v32::UpdateMatters(TOpType T){
 
2306
        bool Ex;
 
2307
  int i;
 
2308
  unsigned int j;
 
2309
  MattersBuffer->Clear();
 
2310
  for(i = 0; i < NLin; i++)if(Lines[i].Type == T){
 
2311
                Ex = false;
 
2312
                for(j = 0; j < MattersBuffer->Count();j ++)if(MattersBuffer->Item(j).CmpNoCase(Lines[i].Reason) == 0){
 
2313
                        Ex = true;
 
2314
                        break;}
 
2315
                if(! Ex)MattersBuffer->Add(Lines[i].Reason);}
 
2316
        MattersBuffer->Sort();
 
2317
        return true;}
 
2318
 
 
2319
void TData_v32::ChangeTransactionCategory(int id, int new_index){
 
2320
        #ifdef _OMB_USE_CIPHER
 
2321
                wxString Sql = L"update Transactions set cat_index = " +
 
2322
                                                                                                                        wxString::Format(L"%d", new_index) +
 
2323
                                                                                                                        L" where id = " +
 
2324
                                                                                                                        wxString::Format(L"%d", id) +
 
2325
                                                                                                                        L";";
 
2326
                ExecuteUpdate(database, Sql.c_str()/*, false*/);
 
2327
        #else
 
2328
                database->ExecuteUpdate(L"update Transactions set cat_index = " +
 
2329
                                                                                                                wxString::Format(L"%d", new_index) +
 
2330
                                                                                                                L" where id = " +
 
2331
                                                                                                                wxString::Format(L"%d", id) +
 
2332
                                                                                                                L";");
 
2333
        #endif // _OMB_USE_CIPHER
 
2334
        ParseDatabase();
 
2335
}
 
2336
 
 
2337
#ifdef _OMB_USE_CIPHER
 
2338
#ifndef __OMBCONVERT_BIN__
 
2339
bool TData_v32::PasswordPrompt(sqlite3 *db, bool Archive = false){
 
2340
        int Response, rc, return_value = false;
 
2341
        wxString Key;
 
2342
 
 
2343
        TPassF *PassF = new TPassF(wxTheApp->GetTopWindow());
 
2344
 
 
2345
        if(Archive)
 
2346
                PassF->PassLab->SetLabel(_("Insert the archive password:"));
 
2347
 
 
2348
        do{
 
2349
                Response = PassF->ShowModal();}
 
2350
        while(Response == wxID_RETRY);
 
2351
 
 
2352
        switch(Response){
 
2353
                case wxID_OK:
 
2354
                        Key = PassF->Pass->GetValue();
 
2355
                        if(SetKey(Key, Archive)){
 
2356
        #ifdef __OPENSUSE__
 
2357
          sqlite3_key(db, Key.c_str(), Key.length());
 
2358
        #else
 
2359
          sqlite3_key(db, Key, Key.length());
 
2360
        #endif // __OPENSUSE__
 
2361
                                rc = sqlite3_exec(db, "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL);
 
2362
                                if (rc == SQLITE_OK)
 
2363
                                        return_value = true;
 
2364
                        }
 
2365
                        break;
 
2366
                case wxID_CANCEL:
 
2367
                default:
 
2368
                        //Error(5, wxEmptyString); // This condition should never happen
 
2369
                        return_value = false;
 
2370
        }
 
2371
        delete PassF;
 
2372
        return return_value;
 
2373
}
 
2374
#endif // __OMBCONVERT_BIN__
 
2375
#endif // _OMB_USE_CIPHER
 
2376
 
 
2377
#ifndef __OMBCONVERT_BIN__
 
2378
        #ifdef _OMB_USE_CIPHER
 
2379
                void TData_v32::ModPass(void){
 
2380
                        int Response;
 
2381
                        wxString Key;
 
2382
 
 
2383
                        TPassF *PassF = new TPassF(wxTheApp->GetTopWindow());
 
2384
                        PassF->Pass2->Show(true);
 
2385
                        PassF->ConfirmLab->Show(true);
 
2386
                        do{
 
2387
                                Response = PassF->ShowModal();}
 
2388
                        while(Response == wxID_RETRY);
 
2389
 
 
2390
                        wxString ArchiveName = Get_MasterDB();
 
2391
 
 
2392
                        switch(Response){
 
2393
                                case wxID_OK:
 
2394
 
 
2395
                                        // Change main database password
 
2396
                                        Key = PassF->Pass->GetValue();
 
2397
                                        if(IsEncrypted){
 
2398
            #ifdef __OPENSUSE__
 
2399
              if(sqlite3_rekey(database, Key.c_str(), Key.length()) == SQLITE_OK)
 
2400
            #else
 
2401
              if(sqlite3_rekey(database, Key, Key.length()) == SQLITE_OK)
 
2402
            #endif // __OPENSUSE__
 
2403
                                                        SetKey(Key);
 
2404
                                        }
 
2405
                                        else{
 
2406
                                                sqlite3_close(database);
 
2407
                                                IsEncrypted = IsEncryptedDB(FileData.FileName, Key);
 
2408
            #ifdef __OPENSUSE__
 
2409
              if(sqlite3_open(FileData.FileName.c_str(), &database) == SQLITE_OK){
 
2410
                sqlite3_key(database, Key.c_str(), Key.length());
 
2411
            #else
 
2412
              if(sqlite3_open(FileData.FileName, &database) == SQLITE_OK){
 
2413
                sqlite3_key(database, Key, Key.length());
 
2414
            #endif // __OPENSUSE__
 
2415
                                                        if(sqlite3_exec(database, "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK)
 
2416
                                                                SetKey(Key);
 
2417
                                                }
 
2418
                                        }
 
2419
 
 
2420
                                        // change archive password
 
2421
                                        Key = GetKey(true);
 
2422
 
 
2423
                                        sqlite3 *Master;
 
2424
                                        #ifdef __OPENSUSE__
 
2425
            sqlite3_open(ArchiveName.c_str(), &Master);
 
2426
            sqlite3_key(Master, Key.c_str(), Key.length());
 
2427
                                        #else
 
2428
            sqlite3_open(ArchiveName, &Master);
 
2429
            sqlite3_key(Master, Key, Key.length());
 
2430
          #endif // __OPENSUSE__
 
2431
 
 
2432
                                        Key = PassF->Pass->GetValue();
 
2433
                                        if(IsEncrypted_master){
 
2434
            #ifdef __OPENSUSE__
 
2435
              if(sqlite3_rekey(Master, Key.c_str(), Key.length()) == SQLITE_OK)
 
2436
            #else
 
2437
              if(sqlite3_rekey(Master, Key, Key.length()) == SQLITE_OK)
 
2438
                                                #endif // __OPENSUSE__
 
2439
                                                        SetKey(Key, true);
 
2440
                                        }
 
2441
                                        else{
 
2442
                                                sqlite3_close(Master);
 
2443
                                                IsEncrypted_master = IsEncryptedDB(ArchiveName, Key);
 
2444
                                                #ifdef __OPENSUSE__
 
2445
              if(sqlite3_open(ArchiveName.c_str(), &Master) == SQLITE_OK){
 
2446
                sqlite3_key(Master, Key.c_str(), Key.length());
 
2447
                                                #else
 
2448
              if(sqlite3_open(ArchiveName, &Master) == SQLITE_OK){
 
2449
                sqlite3_key(Master, Key, Key.length());
 
2450
            #endif // __OPENSUSE__
 
2451
                                                        if(sqlite3_exec(Master, "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK)
 
2452
                                                                SetKey(Key, true);
 
2453
                                                }
 
2454
                                        }
 
2455
 
 
2456
                                        break;
 
2457
                                case wxID_CANCEL:
 
2458
                                        break;
 
2459
                                /*
 
2460
                                default:
 
2461
                                        Error(5,wxEmptyString); // This condition should never happen
 
2462
                                */
 
2463
                        }
 
2464
                        delete PassF;}
 
2465
        #endif // _OMB_USE_CIPHER
 
2466
#endif // __OMBCONVERT_BIN__
 
2467
 
 
2468
#ifndef __OMBCONVERT_BIN__
 
2469
        #ifdef _OMB_USE_CIPHER
 
2470
                bool TData_v32::AttachMaster(void){
 
2471
                        wxString ArchiveName = Get_MasterDB();
 
2472
 
 
2473
                        if(::wxFileExists(ArchiveName)){
 
2474
                                wxString pwd_archive = GetKey(true);
 
2475
                                if(! CheckAndPromptForConversion(ArchiveName, true, pwd_archive))
 
2476
                                        return false;
 
2477
 
 
2478
                                int tryno = 1;
 
2479
                                int result = -1;
 
2480
                                while (tryno < 4)
 
2481
                                {
 
2482
                                        try
 
2483
                                        {
 
2484
                                                if(IsEncrypted_master){
 
2485
                                                        sqlite3 *Master;
 
2486
                                                        #ifdef __OPENSUSE__
 
2487
                if(sqlite3_open(ArchiveName.c_str(), &Master) != SQLITE_OK) return false;
 
2488
                sqlite3_key(Master, pwd_archive.c_str(), pwd_archive.length());
 
2489
                                                        #else
 
2490
                if(sqlite3_open(ArchiveName, &Master) != SQLITE_OK) return false;
 
2491
                sqlite3_key(Master, pwd_archive, pwd_archive.length());
 
2492
              #endif // __OPENSUSE__
 
2493
                                                        int rc = sqlite3_exec(Master, "SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL);
 
2494
                                                        switch(rc){
 
2495
                                                                case SQLITE_OK:
 
2496
                                                                result = 1;
 
2497
                                                                break;
 
2498
 
 
2499
                                                        case SQLITE_NOTADB:
 
2500
                                                                #ifndef __OMBCONVERT_BIN__
 
2501
                                                                        PasswordPrompt(database, true);
 
2502
                                                                        pwd_archive = GetKey(true);
 
2503
                                                                #endif // __OMBCONVERT_BIN__
 
2504
                                                                break;
 
2505
 
 
2506
                                                                //default:
 
2507
                                                        }
 
2508
 
 
2509
                                                }
 
2510
                                        }
 
2511
                                        catch(exception& e)
 
2512
                                        {
 
2513
                                                //
 
2514
                                        }
 
2515
 
 
2516
                                        if(result == 1) break;
 
2517
                                        tryno++;
 
2518
                                }
 
2519
 
 
2520
                                #ifdef __OPENSUSE__
 
2521
          wxString Sql = wxString::Format(L"ATTACH DATABASE '%s' AS master KEY '%s';", ArchiveName, pwd_archive);
 
2522
          ExecuteUpdate(database, Sql.c_str());
 
2523
                                #else
 
2524
          ExecuteUpdate(database, wxString::Format(L"ATTACH DATABASE '%s' AS master KEY '%s';", ArchiveName, pwd_archive));
 
2525
        #endif // __OPENSUSE__
 
2526
                                return true;
 
2527
                        }
 
2528
 
 
2529
                        return false;
 
2530
                }
 
2531
        #endif // _OMB_USE_CIPHER
 
2532
#endif // __OMBCONVERT_BIN__
 
2533
 
 
2534
#endif  // OMB33CORE_CPP_INCLUDED