~igor-cali/bilancio/stable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/***************************************************************
 * Name:      datawithalarms.cpp
 * Purpose:   Alarm extension for TData
 * Author:    Igor Calì (igor.cali0@gmail.com)
 * Created:   2019-02-10
 * Copyright: Igor Calì (igor.cali0@gmail.com)
 * License:		GNU
 **************************************************************/

#include <wx/sound.h>

#include "datawithalarms.h"
#include "ui/Alarm.h"
#include "../constants.h"

#ifdef _OMB_USE_CIPHER
	extern int ExecuteUpdate(sqlite3 *m_db, const char* sql/*, bool saveRC = false*/);
#endif // _OMB_USE_CIPHER

TDataWithAlarms::TDataWithAlarms(void):TData_v32(NULL){
	SoundFile = GetDataDir() + L"alarm_clock.wav";}

bool TDataWithAlarms::HasAlarms(void){
	bool result=false;
	int i;
	wxDateTime checkdate=wxDateTime::Today().Add(wxDateSpan(90,0,0,0));
	for(i = 0; i < NSho; i++)if(ShopItems[i].Alarm < checkdate){
			result=true;
			break;}
	if(result)return true;
	for(i = 0; i < NLen; i++)if(Lent[i].Alarm<checkdate){
			result=true;
			break;}
	if(result)return true;
	for(i = 0; i < NBor; i++)if(Borrowed[i].Alarm<checkdate){
			result=true;
			break;}
	return result;}

void TDataWithAlarms::CheckShopList(void){
	bool PostAll=false;
	wxDateTime Now = wxDateTime::Now();
	TAlarmF *AlarmF = new TAlarmF(wxTheApp->GetTopWindow());
	wxString Msg;
	AlarmF->DatePicker->SetValue(Now);
	AlarmF->DatePicker->SetRange(Now,wxInvalidDateTime);
	for(int i = 0; i < NSho; i++)if(ShopItems[i].Alarm <= Now){
		Msg=::wxString::Format(_("The alarm for the shopping list item \"%s\" is expired!"),ShopItems[i].Name.c_str());
		AlarmF->Text->SetLabel(Msg);
    if(wxFileExists(SoundFile)){
	    wxSound *sound;
    	sound=new wxSound(SoundFile,false);
    	if(!PostAll)if(sound->IsOk()) sound->Play(wxSOUND_ASYNC);
 	   	delete sound;}
		if(PostAll){
			ShopItems[i].Alarm = AlarmF->DatePicker->GetValue();
			#ifdef _OMB_USE_CIPHER
				wxString update = L"update Shoplist set alarm = " +
																wxString::Format(L"%d", (int) ShopItems[i].Alarm.GetTicks()) +
																L" where id = " +
																wxString::Format(L"%d", ShopItems[i].Id) +
																L";";
        #ifdef __OPENSUSE__
          ExecuteUpdate(database, update.c_str());
        #else
          ExecuteUpdate(database, update);
        #endif
			#else
				database->ExecuteUpdate(L"update Shoplist set alarm = " +
																wxString::Format(L"%d", (int) ShopItems[i].Alarm.GetTicks()) +
																L" where id = " +
																wxString::Format(L"%d", ShopItems[i].Id) +
																L";");
			#endif // _OMB_USE_CIPHER
		}
		else{
			#ifdef _OMB_USE_CIPHER
				wxString Sql;
			#endif // _OMB_USE_CIPHER
			switch(AlarmF->ShowModal()){
				case wxID_OK:
					ShopItems[i].Alarm=AlarmF->DatePicker->GetValue();
					#ifdef _OMB_USE_CIPHER
						Sql = L"update Shoplist set alarm = " +
																		wxString::Format(L"%d", (int) ShopItems[i].Alarm.GetTicks()) +
																		L" where id = " +
																		wxString::Format(L"%d", ShopItems[i].Id) +
																		L";";
            #ifdef __OPENSUSE__
              ExecuteUpdate(database, Sql.c_str());
            #else
              ExecuteUpdate(database, Sql);
            #endif
					#else
						database->ExecuteUpdate(L"update Shoplist set alarm = " +
																		wxString::Format(L"%d", (int) ShopItems[i].Alarm.GetTicks()) +
																		L" where id = " +
																		wxString::Format(L"%d", ShopItems[i].Id) +
																		L";");
					#endif // _OMB_USE_CIPHER
					if(AlarmF->cb_PostponeAll->IsChecked())PostAll=true;
					break;
			case wxID_CANCEL:
					ShopItems[i].Alarm.Add(wxDateSpan(100,0,0,0));}
			FileData.Modified=true;}}
		delete AlarmF;}

void TDataWithAlarms::CheckLentObjects(void){
	bool PostAll = false;
	wxDateTime Now = wxDateTime::Now();
	TAlarmF *AlarmF=new TAlarmF(wxTheApp->GetTopWindow());
	wxString Msg;
	AlarmF->DatePicker->SetValue(Now);
	AlarmF->DatePicker->SetRange(Now, wxInvalidDateTime);
	for(int i = 0; i < NLen; i++)if(Now.IsLaterThan(Lent[i].Alarm)){
		Msg=::wxString::Format(_("The alarm for the item \"%s\" is expired!"),Lent[i].Object.c_str());
		AlarmF->Text->SetLabel(Msg);
		if(wxFileExists(SoundFile)){
			wxSound *sound;
			sound=new wxSound(SoundFile,false);
			if(!PostAll)if(sound->IsOk()) sound->Play(wxSOUND_ASYNC);
			delete sound;}
		if(PostAll){
		Lent[i].Alarm=AlarmF->DatePicker->GetValue();
		#ifdef _OMB_USE_CIPHER
			wxString update = L"update Loans set alarm = " +
															wxString::Format(L"%d", (int) Lent[i].Alarm.GetTicks()) +
															L" where id = " +
															wxString::Format(L"%d", Lent[i].Id) +
															L";";
			#ifdef __OPENSUSE__
        ExecuteUpdate(database, update.c_str());
			#else
        ExecuteUpdate(database, update);
      #endif
		#else
			database->ExecuteUpdate(L"update Loans set alarm = " +
															wxString::Format(L"%d", (int) Lent[i].Alarm.GetTicks()) +
															L" where id = " +
															wxString::Format(L"%d", Lent[i].Id) +
															L";");
		#endif // _OMB_USE_CIPHER
		}
		else{
			#ifdef _OMB_USE_CIPHER
				wxString Sql;
			#endif // _OMB_USE_CIPHER
			switch(AlarmF->ShowModal()){
				case wxID_OK:
					Lent[i].Alarm=AlarmF->DatePicker->GetValue();
					#ifdef _OMB_USE_CIPHER
						Sql = L"update Loans set alarm = " +
																		wxString::Format(L"%d", (int) Lent[i].Alarm.GetTicks()) +
																		L" where id = " +
																		wxString::Format(L"%d", Lent[i].Id) +
																		L";";
						#ifdef __OPENSUSE__
              ExecuteUpdate(database, Sql.c_str());
						#else
              ExecuteUpdate(database, Sql);
            #endif
					#else
						database->ExecuteUpdate(L"update Loans set alarm = " +
																			wxString::Format(L"%d", (int) Lent[i].Alarm.GetTicks()) +
																			L" where id = " +
																			wxString::Format(L"%d", Lent[i].Id) +
																			L";");
					#endif // _OMB_USE_CIPHER
					if(AlarmF->cb_PostponeAll->IsChecked())PostAll=true;
					break;
				case wxID_CANCEL:
					Lent[i].Alarm.Add(wxDateSpan(100,0,0,0));}
				FileData.Modified=true;}}
  delete AlarmF;}

void TDataWithAlarms::CheckBorrowedObjects(void){
	bool PostAll=false;
	wxDateTime Now = wxDateTime::Now();
	TAlarmF *AlarmF=new TAlarmF(wxTheApp->GetTopWindow());
	wxString Msg;
	AlarmF->DatePicker->SetValue(Now);
	AlarmF->DatePicker->SetRange(Now, wxInvalidDateTime);
	for(int i = 0; i < NBor; i++)if(Borrowed[i].Alarm<=Now){
		Msg=::wxString::Format(_("The alarm for the item \"%s\" is expired!"),Lent[i].Object.c_str());
		AlarmF->Text->SetLabel(Msg);
    if(wxFileExists(SoundFile)){
	    wxSound *sound;
    	sound=new wxSound(SoundFile,false);
    	if(!PostAll)if(sound->IsOk())sound->Play(wxSOUND_ASYNC);
  	  delete sound;}
		if(PostAll){
			Borrowed[i].Alarm=AlarmF->DatePicker->GetValue();
			#ifdef _OMB_USE_CIPHER
				wxString update = L"update Borrows set alarm = " +
																wxString::Format(L"%d", (int) Borrowed[i].Alarm.GetTicks()) +
																L" where id = " +
																wxString::Format(L"%d", Borrowed[i].Id) +
																L";";
        #ifdef __OPENSUSE__
          ExecuteUpdate(database, update.c_str());
        #else
          ExecuteUpdate(database, update);
        #endif
			#else
				database->ExecuteUpdate(L"update Borrows set alarm = " +
																wxString::Format(L"%d", (int) Borrowed[i].Alarm.GetTicks()) +
																L" where id = " +
																wxString::Format(L"%d", Borrowed[i].Id) +
																L";");
			#endif // _OMB_USE_CIPHER
		}
		else{
			switch(AlarmF->ShowModal()){
				case wxID_OK:
						Borrowed[i].Alarm=AlarmF->DatePicker->GetValue();
						if(AlarmF->cb_PostponeAll->IsChecked())PostAll=true;
						break;
				case wxID_CANCEL:
						Borrowed[i].Alarm.Add(wxDateSpan(100,0,0,0));}
			FileData.Modified=true;}}
   delete AlarmF;}