summaryrefslogtreecommitdiff
path: root/plugins/Quotes/src/QuotesProviderFinance.cpp
blob: 0dcfe6db54ea2ad5c2f1b0d30b0d12dd1421bf7a (plain)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "stdafx.h"

void CQuotesProviderFinance::GetWatchedQuotes(TQuotes& raQuotes)const
{
	raQuotes.clear();
	BOOST_FOREACH(HANDLE hContact,m_aContacts)
	{
		tstring sID = Quotes_DBGetStringT(hContact,QUOTES_MODULE_NAME,DB_STR_QUOTE_ID);
		tstring sSymbol = Quotes_DBGetStringT(hContact,QUOTES_MODULE_NAME,DB_STR_QUOTE_SYMBOL,sID.c_str());
		tstring sDescr = Quotes_DBGetStringT(hContact,QUOTES_MODULE_NAME,DB_STR_QUOTE_DESCRIPTION);
		CQuotesProviderBase::CQuote quote(sID,sSymbol,sDescr);

		raQuotes.push_back(quote);
	}
}

namespace
{
	inline tstring get_quote_id(HANDLE hContact)
	{
		return Quotes_DBGetStringT(hContact,QUOTES_MODULE_NAME,DB_STR_QUOTE_ID);
	}

	inline bool is_quote_id_equal(HANDLE hContact,const tstring& sID)
	{
		return sID == get_quote_id(hContact);
	}
}

bool CQuotesProviderFinance::WatchForQuote(const CQuote& rQuote,bool bWatch)
{
	const tstring& sQuoteID = rQuote.GetID();
	TContracts::iterator i = std::find_if(m_aContacts.begin(),m_aContacts.end(),
		boost::bind(is_quote_id_equal,_1,sQuoteID));

	if ((false == bWatch) && (i != m_aContacts.end()))
	{
		HANDLE hContact = *i;
		{// for CCritSection
			CGuard<CLightMutex> cs(m_cs);
			m_aContacts.erase(i);
		}

		CallService(MS_DB_CONTACT_DELETE,reinterpret_cast<WPARAM>(hContact),0);
		return true;
	}
	else if ((true == bWatch) && (i == m_aContacts.end()))
	{
		HANDLE hContact = CreateNewContact(rQuote.GetSymbol());
		if(hContact)
		{
			DBWriteContactSettingTString(hContact,QUOTES_PROTOCOL_NAME,DB_STR_QUOTE_ID,sQuoteID.c_str());
			if(false == rQuote.GetName().empty())
			{
				DBWriteContactSettingTString(hContact,QUOTES_PROTOCOL_NAME,DB_STR_QUOTE_DESCRIPTION,rQuote.GetName().c_str());
			}

			return true;
		}
	}

	return false;
}

HANDLE CQuotesProviderFinance::GetContactByQuoteID(const tstring& rsQuoteID)const
{
	CGuard<CLightMutex> cs(m_cs);

	TContracts::const_iterator i = std::find_if(m_aContacts.begin(),m_aContacts.end(),
		boost::bind(std::equal_to<tstring>(),rsQuoteID,boost::bind(get_quote_id,_1)));
	if(i != m_aContacts.end())
	{
		return *i;
	}
	else
	{
		return NULL;
	}
}

void CQuotesProviderFinance::Accept(CQuotesProviderVisitor& visitor)const
{
	CQuotesProviderBase::Accept(visitor);
	visitor.Visit(*this);
}

namespace
{
	inline tstring make_quote_name(const CQuotesProviderBase::CQuote& rQuote)
	{
		const tstring& rsDesc = rQuote.GetName();
		return((false == rsDesc.empty()) ? rsDesc : rQuote.GetSymbol());
	}
	
	int add_quote_to_wnd(const CQuotesProviderBase::CQuote& rQuote,HWND hwnd)
	{
		tstring sName = make_quote_name(rQuote);
		int nIndex = ::SendMessage(hwnd,LB_ADDSTRING,0,reinterpret_cast<LPARAM>(sName.c_str()));
		if(nIndex >= 0)
		{
			CQuotesProviderBase::CQuote* pQuote = new CQuotesProviderBase::CQuote(rQuote);
			if(LB_ERR == ::SendMessage(hwnd,LB_SETITEMDATA,nIndex,reinterpret_cast<LPARAM>(pQuote)))
			{
				delete pQuote;
			}
		}
		return nIndex;
	}

// 	typedef CQuotesProviderFinance::TQuotes TQuotes;
// 	TQuotes g_aWatchedQuotes;

// 	inline bool cmp_quotes(const tstring& rsQuoteId,const CQuotesProviderBase::CQuote& quote)
// 	{
// 		return (0 == quotes_stricmp(rsQuoteId.c_str(),quote.GetID().c_str()));
// 	}

	CQuotesProviderBase::CQuote* get_quote_ptr_from_lb_index(HWND hwndListBox,int nIndex)
	{
		LRESULT lResult = ::SendMessage(hwndListBox,LB_GETITEMDATA,nIndex,0);
		return (((LB_ERR != lResult) && (0 != lResult)) ? (reinterpret_cast<CQuotesProviderBase::CQuote*>(lResult)) : nullptr);
	}

	int is_quote_added(HWND hwndList,const tstring& rsQuoteID)
	{
		int cItems = ::SendMessage(hwndList,LB_GETCOUNT,0,0);
		for(int i = 0;i < cItems;++i)
		{
			const CQuotesProviderBase::CQuote* pQuote = get_quote_ptr_from_lb_index(hwndList,i);
			if ((nullptr != pQuote) 
				&& ((0 == quotes_stricmp(rsQuoteID.c_str(),pQuote->GetID().c_str()))
				|| (0 == quotes_stricmp(rsQuoteID.c_str(),pQuote->GetName().c_str()))
				|| (0 == quotes_stricmp(rsQuoteID.c_str(),pQuote->GetSymbol().c_str()))))
			{
				return i;
			}
		}
		return LB_ERR;
	}

	INT_PTR CALLBACK  GoogleFinanceOptDlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
	{
		CQuotesProviderFinance* pProvider = nullptr;
		if(WM_INITDIALOG == message)
		{
			pProvider = reinterpret_cast<CQuotesProviderFinance*>(lParam);
			SetWindowLongPtr(hDlg,GWLP_USERDATA,lParam);
		}
		else
		{
			pProvider = reinterpret_cast<CQuotesProviderFinance*>(GetWindowLongPtr(hDlg,GWLP_USERDATA));
		}

		CCommonDlgProcData d(pProvider);
		CommonOptionDlgProc(hDlg,message,wParam,lParam,d);

		switch(message)
		{
		case WM_INITDIALOG:
			{
				TranslateDialogDefault(hDlg);

				CQuotesProviderFinance::TQuotes aQuotes;
				pProvider->GetWatchedQuotes(aQuotes);

				HWND hwndList = GetDlgItem(hDlg,IDC_LIST_RATES);
				std::for_each(aQuotes.begin(),aQuotes.end(),
					boost::bind(add_quote_to_wnd,_1,hwndList));

				::EnableWindow(::GetDlgItem(hDlg,IDC_BUTTON_ADD),FALSE);
				::EnableWindow(::GetDlgItem(hDlg,IDC_BUTTON_REMOVE),FALSE);
			}
			return (TRUE);
		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
			case IDC_EDIT_QUOTE:
				if(EN_CHANGE == HIWORD(wParam))
				{
					::EnableWindow(::GetDlgItem(hDlg,IDC_BUTTON_ADD),GetWindowTextLength(GetDlgItem(hDlg,IDC_EDIT_QUOTE)) > 0);
				}
				return (TRUE);
			case IDC_BUTTON_ADD:
				if(BN_CLICKED == HIWORD(wParam))
				{
					HWND hEdit = GetDlgItem(hDlg,IDC_EDIT_QUOTE);
					tstring sQuoteSymbol = get_window_text(hEdit);
					assert(false == sQuoteSymbol.empty());
					HWND hwndList = GetDlgItem(hDlg,IDC_LIST_RATES);
					if(LB_ERR == is_quote_added(hwndList,sQuoteSymbol))
					{
						CQuotesProviderBase::CQuote quote(sQuoteSymbol,sQuoteSymbol);
						if(add_quote_to_wnd(quote,hwndList) >= 0)
						{
							SetDlgItemText(hDlg,IDC_EDIT_QUOTE,_T(""));
							SetFocus(hEdit);
							PropSheet_Changed(::GetParent(hDlg),hDlg);
						}
						else
						{
							::MessageBeep(MB_ICONERROR);
						}
					}
				}
				return (TRUE);
			case IDC_BUTTON_REMOVE:
				if(BN_CLICKED == HIWORD(wParam))
				{
					HWND hWnd = ::GetDlgItem(hDlg,IDC_LIST_RATES);
					int nSel = ::SendMessage(hWnd,LB_GETCURSEL,0,0);
					if(LB_ERR != nSel)
					{
						CQuotesProviderBase::CQuote* pQuote = get_quote_ptr_from_lb_index(hWnd,nSel);
						delete pQuote;
						if(LB_ERR != ::SendMessage(hWnd,LB_DELETESTRING,nSel,0))
						{
							PropSheet_Changed(::GetParent(hDlg),hDlg);
						}
					}

					nSel = ::SendMessage(hWnd,LB_GETCURSEL,0,0);
					::EnableWindow(::GetDlgItem(hDlg,IDC_BUTTON_REMOVE),(LB_ERR != nSel));
				}
				return (TRUE);
			case IDC_LIST_RATES:
				if(CBN_SELCHANGE == HIWORD(wParam))
				{
					int nSel = ::SendMessage(::GetDlgItem(hDlg,IDC_LIST_RATES),LB_GETCURSEL,0,0);
					::EnableWindow(::GetDlgItem(hDlg,IDC_BUTTON_REMOVE),(LB_ERR != nSel));
				}
				return (TRUE);
			}
			return (FALSE);

		case WM_NOTIFY:
			{
				LPNMHDR pNMHDR = reinterpret_cast<LPNMHDR>(lParam);
				switch(pNMHDR->code)
				{
				case PSN_APPLY:
					if(pProvider)
					{
						CQuotesProviderFinance::TQuotes aTemp;
						pProvider->GetWatchedQuotes(aTemp);

						typedef std::vector<const CQuotesProviderBase::CQuote*> TQuotesPtr;
						TQuotesPtr apCurrent;
						HWND hwndListBox = GetDlgItem(hDlg,IDC_LIST_RATES);
						int cItems = ::SendMessage(hwndListBox,LB_GETCOUNT,0,0);
						for(int i = 0;i < cItems;++i)
						{
							const CQuotesProviderBase::CQuote* pQuote = get_quote_ptr_from_lb_index(hwndListBox,i);
							if(pQuote)
							{
								apCurrent.push_back(pQuote);
							}
						}
						
						std::for_each(aTemp.begin(),aTemp.end(),
							[&apCurrent,pProvider](const CQuotesProviderBase::CQuote& quote)
							{
								if(apCurrent.end() == std::find_if(apCurrent.begin(),apCurrent.end(),
									[&quote](const CQuotesProviderBase::CQuote* pQuote){return 0 == quotes_stricmp(pQuote->GetID().c_str(),quote.GetID().c_str());}))
								{
									pProvider->WatchForQuote(quote,false);
								}
							});

						std::for_each(apCurrent.begin(),apCurrent.end(),
							[&aTemp,pProvider](const CQuotesProviderBase::CQuote* pQuote)
							{
								if(aTemp.end() == 
									std::find_if(aTemp.begin(),aTemp.end(),
									[pQuote](const CQuotesProviderBase::CQuote& quote){return 0 == quotes_stricmp(pQuote->GetID().c_str(),quote.GetID().c_str());}))
								{
									pProvider->WatchForQuote(*pQuote,true);
								}

							});

						pProvider->RefreshAll();
					}

					return (TRUE);
				}
			}
			return (FALSE);
		case WM_DESTROY:
			HWND hwndListBox = GetDlgItem(hDlg,IDC_LIST_RATES);
			int cItems = ::SendMessage(hwndListBox,LB_GETCOUNT,0,0);
			for(int i = 0;i < cItems;++i)
			{
				const CQuotesProviderBase::CQuote* pQuote = get_quote_ptr_from_lb_index(hwndListBox,i);
				delete pQuote;
			}
			return (FALSE);
		}
		return (FALSE);
	}
}

void CQuotesProviderFinance::ShowPropertyPage(WPARAM wp,OPTIONSDIALOGPAGE& odp)
{
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_DIALOG_OPT_FINANCE);
	odp.pfnDlgProc = GoogleFinanceOptDlgProc;
	odp.dwInitParam = reinterpret_cast<DWORD>(static_cast<CQuotesProviderFinance*>(this));
	odp.ptszTab = const_cast<LPTSTR>(GetInfo().m_sName.c_str());
	Options_AddPage(wp, &odp);	
}