summaryrefslogtreecommitdiff
path: root/plugins/Quotes/src/QuoteInfoDlg.cpp
blob: fdb60d15ab40c646e5cabbc7672a940c95751237 (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
#include "StdAfx.h"

// extern HANDLE g_hWindowListEditSettings;
extern HGENMENU g_hMenuEditSettings;
extern HGENMENU g_hMenuOpenLogFile;
#ifdef CHART_IMPLEMENT
extern HGENMENU g_hMenuChart;
#endif
extern HGENMENU g_hMenuRefresh, g_hMenuRoot;

#define WINDOW_PREFIX_INFO "Quote Info"

MCONTACT g_hContact;

inline bool IsMyContact(MCONTACT hContact)
{
	CQuotesProviders::TQuotesProviderPtr pProvider = CModuleInfo::GetQuoteProvidersPtr()->GetContactProviderPtr(hContact);
	return (NULL != pProvider);
}

inline MCONTACT get_contact(HWND hWnd)
{
	return MCONTACT(GetWindowLongPtr(hWnd, GWLP_USERDATA));
}

static bool get_fetch_time(time_t& rTime, MCONTACT hContact)
{
	rTime = db_get_dw(hContact, QUOTES_PROTOCOL_NAME, DB_STR_QUOTE_FETCH_TIME, -1);
	return (rTime != -1);
}

INT_PTR CALLBACK QuoteInfoDlgProcImpl(MCONTACT hContact, HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_INITDIALOG:
		assert(hContact);

		TranslateDialogDefault(hdlg);
		{
			tstring sDescription = GetContactName(hContact);
			::SetDlgItemText(hdlg, IDC_STATIC_QUOTE_NAME, sDescription.c_str());

			double dRate = 0.0;
			if (true == Quotes_DBReadDouble(hContact, QUOTES_PROTOCOL_NAME, DB_STR_QUOTE_PREV_VALUE, dRate)) {
				tostringstream o;
				o.imbue(GetSystemLocale());
				o << dRate;

				::SetDlgItemText(hdlg, IDC_EDIT_PREVIOUS_RATE, o.str().c_str());
			}

			dRate = 0.0;
			if (true == Quotes_DBReadDouble(hContact, QUOTES_PROTOCOL_NAME, DB_STR_QUOTE_CURR_VALUE, dRate)) {
				tostringstream o;
				o.imbue(GetSystemLocale());
				o << dRate;

				::SetDlgItemText(hdlg, IDC_EDIT_RATE, o.str().c_str());
			}

			time_t nFetchTime;
			if (true == get_fetch_time(nFetchTime, hContact)) {
				TCHAR szTime[50] = { 0 };
				if (0 == _tctime_s(szTime, 50, &nFetchTime)) {
					::SetDlgItemText(hdlg, IDC_EDIT_RATE_FETCH_TIME, szTime);
				}
			}

			CQuotesProviders::TQuotesProviderPtr pProvider = CModuleInfo::GetQuoteProvidersPtr()->GetContactProviderPtr(hContact);

			const IQuotesProvider::CProviderInfo& pi = pProvider->GetInfo();
			tostringstream o;
			o << TranslateT("Info provided by") << _T(" <a href=\"") << pi.m_sURL << _T("\">") << pi.m_sName << _T("</a>");

			::SetDlgItemText(hdlg, IDC_SYSLINK_PROVIDER, o.str().c_str());
		}
		return TRUE;

	case WM_NOTIFY:
		LPNMHDR pNMHDR = reinterpret_cast<LPNMHDR>(lParam);
		switch (pNMHDR->code) {
		case NM_CLICK:
			if (IDC_SYSLINK_PROVIDER == wParam) {
				PNMLINK pNMLink = reinterpret_cast<PNMLINK>(pNMHDR);
				::ShellExecute(hdlg, _T("open"), pNMLink->item.szUrl, NULL, NULL, SW_SHOWNORMAL);
			}
			break;
		}
		break;
	}
	return FALSE;
}

INT_PTR CALLBACK QuoteInfoDlgProc(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	return QuoteInfoDlgProcImpl(g_hContact, hdlg, msg, wParam, lParam);
}

int QuotesEventFunc_OnUserInfoInit(WPARAM wp, LPARAM lp)
{
	MCONTACT hContact = MCONTACT(lp);
	if (NULL == hContact)
		return 0;

	if (false == IsMyContact(hContact))
		return 0;

	g_hContact = hContact;

	OPTIONSDIALOGPAGE odp = { 0 };
	odp.hInstance = g_hInstance;
	odp.hIcon = Quotes_LoadIconEx(IDI_ICON_MAIN);
	odp.pfnDlgProc = QuoteInfoDlgProc;
	odp.position = -2000000000;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_DIALOG_QUOTE_INFO);
	odp.pszTitle = LPGEN("Quote");
	UserInfo_AddPage(wp, &odp);
	return 0;
}


INT_PTR QuotesMenu_EditSettings(WPARAM wp, LPARAM)
{
	MCONTACT hContact = MCONTACT(wp);
	if (NULL != hContact)
		ShowSettingsDlg(hContact);
	return 0;
}

namespace
{
	bool get_log_file(MCONTACT hContact, tstring& rsLogfile)
	{
		rsLogfile = GetContactLogFileName(hContact);
		return ((rsLogfile.empty()) ? false : true);
	}
}

INT_PTR QuotesMenu_OpenLogFile(WPARAM wp, LPARAM)
{
	MCONTACT hContact = MCONTACT(wp);
	if (NULL == hContact)
		return 0;

	tstring sLogFileName;
	if ((true == get_log_file(hContact, sLogFileName)) && (false == sLogFileName.empty()))
		::ShellExecute(NULL, _T("open"), sLogFileName.c_str(), NULL, NULL, SW_SHOWNORMAL);

	return 0;
}

INT_PTR QuotesMenu_RefreshContact(WPARAM wp, LPARAM)
{
	MCONTACT hContact = MCONTACT(wp);
	if (NULL == hContact)
		return 0;

	CQuotesProviders::TQuotesProviderPtr pProvider = CModuleInfo::GetQuoteProvidersPtr()->GetContactProviderPtr(hContact);
	if (!pProvider)
		return 0;

	pProvider->RefreshContact(hContact);
	return 0;
}

static INT_PTR CALLBACK QuoteInfoDlgProc1(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	MCONTACT hContact = NULL;
	MWindowList hWL;

	switch (msg) {
	case WM_INITDIALOG:
		hContact = MCONTACT(lParam);
		hWL = CModuleInfo::GetInstance().GetWindowList(WINDOW_PREFIX_INFO, false);
		assert(hWL);
		WindowList_Add(hWL, hdlg, hContact);

		::SetWindowLongPtr(hdlg, GWLP_USERDATA, hContact);
		Utils_RestoreWindowPositionNoSize(hdlg, hContact, QUOTES_MODULE_NAME, WINDOW_PREFIX_INFO);
		::ShowWindow(hdlg, SW_SHOW);
		break;

	case WM_CLOSE:
		DestroyWindow(hdlg);
		return FALSE;

	case WM_DESTROY:
		hContact = get_contact(hdlg);
		if (hContact) {
			SetWindowLongPtr(hdlg, GWLP_USERDATA, 0);

			hWL = CModuleInfo::GetInstance().GetWindowList(WINDOW_PREFIX_INFO, false);
			assert(hWL);
			WindowList_Remove(hWL, hdlg);
			Utils_SaveWindowPosition(hdlg, hContact, QUOTES_MODULE_NAME, WINDOW_PREFIX_INFO);
		}
		return FALSE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK) {
			::DestroyWindow(hdlg);
			return FALSE;
		}

	default:
		hContact = get_contact(hdlg);
		break;
	}

	return QuoteInfoDlgProcImpl(hContact, hdlg, msg, wParam, lParam);
}

int Quotes_OnContactDoubleClick(WPARAM wp, LPARAM/* lp*/)
{
	MCONTACT hContact = MCONTACT(wp);
	if (CModuleInfo::GetQuoteProvidersPtr()->GetContactProviderPtr(hContact)) {
		MWindowList hWL = CModuleInfo::GetInstance().GetWindowList(WINDOW_PREFIX_INFO, true);
		assert(hWL);
		HWND hWnd = WindowList_Find(hWL, hContact);
		if (NULL != hWnd) {
			SetForegroundWindow(hWnd);
			SetFocus(hWnd);
		}
		else if (true == IsMyContact(hContact))
			CreateDialogParam(g_hInstance, MAKEINTRESOURCE(IDD_DIALOG_QUOTE_INFO_1), NULL, QuoteInfoDlgProc1, LPARAM(hContact));

		return 1;
	}

	return 0;
}

int Quotes_PrebuildContactMenu(WPARAM wp, LPARAM)
{
	Menu_EnableItem(g_hMenuEditSettings, false);
	Menu_EnableItem(g_hMenuOpenLogFile, false);
	#ifdef CHART_IMPLEMENT
	Menu_EnableItem(g_hMenuChart, false);
	#endif
	Menu_EnableItem(g_hMenuRefresh, false);

	MCONTACT hContact = MCONTACT(wp);
	char *szProto = GetContactProto(hContact);
	if (mir_strcmp(szProto, QUOTES_MODULE_NAME)) {
		Menu_ShowItem(g_hMenuRoot, false);
		return 0;
	}

	Menu_ShowItem(g_hMenuRoot, true);
	Menu_EnableItem(g_hMenuEditSettings, true);

	Menu_EnableItem(g_hMenuRefresh, true);

	tstring sLogFileName;
	bool bThereIsLogFile = (true == get_log_file(hContact, sLogFileName))
		&& (false == sLogFileName.empty()) && (0 == _taccess(sLogFileName.c_str(), 04));
	if (true == bThereIsLogFile) {
		#ifdef CHART_IMPLEMENT
		Menu_EnableItem(g_hMenuChart, true);
		#endif
		Menu_EnableItem(g_hMenuOpenLogFile, true);
	}

	return 0;
}