#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 "Currency Rate Info"
MCONTACT g_hContact;
inline bool IsMyContact(MCONTACT hContact)
{
CCurrencyRatesProviders::TCurrencyRatesProviderPtr pProvider = CModuleInfo::GetCurrencyRateProvidersPtr()->GetContactProviderPtr(hContact);
return (nullptr != 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, CURRENCYRATES_MODULE_NAME, DB_STR_CURRENCYRATE_FETCH_TIME, -1);
return (rTime != -1);
}
INT_PTR CALLBACK CurrencyRateInfoDlgProcImpl(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_CURRENCYRATE_NAME, sDescription.c_str());
double dRate = 0.0;
if (true == CurrencyRates_DBReadDouble(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_CURRENCYRATE_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 == CurrencyRates_DBReadDouble(hContact, CURRENCYRATES_MODULE_NAME, DB_STR_CURRENCYRATE_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)) {
wchar_t szTime[50] = { 0 };
if (0 == _tctime_s(szTime, 50, &nFetchTime)) {
::SetDlgItemText(hdlg, IDC_EDIT_RATE_FETCH_TIME, szTime);
}
}
CCurrencyRatesProviders::TCurrencyRatesProviderPtr pProvider = CModuleInfo::GetCurrencyRateProvidersPtr()->GetContactProviderPtr(hContact);
const ICurrencyRatesProvider::CProviderInfo& pi = pProvider->GetInfo();
tostringstream o;
o << TranslateT("Info provided by") << L" " << pi.m_sName << L"";
::SetDlgItemText(hdlg, IDC_SYSLINK_PROVIDER, o.str().c_str());
}
return TRUE;
case WM_NOTIFY:
LPNMHDR pNMHDR = reinterpret_cast(lParam);
switch (pNMHDR->code) {
case NM_CLICK:
if (IDC_SYSLINK_PROVIDER == wParam) {
PNMLINK pNMLink = reinterpret_cast(pNMHDR);
::ShellExecute(hdlg, L"open", pNMLink->item.szUrl, nullptr, nullptr, SW_SHOWNORMAL);
}
break;
}
break;
}
return FALSE;
}
INT_PTR CALLBACK CurrencyRateInfoDlgProc(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
return CurrencyRateInfoDlgProcImpl(g_hContact, hdlg, msg, wParam, lParam);
}
int CurrencyRatesEventFunc_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 = {};
odp.pfnDlgProc = CurrencyRateInfoDlgProc;
odp.position = -2000000000;
odp.pszTemplate = MAKEINTRESOURCEA(IDD_DIALOG_CURRENCYRATE_INFO);
odp.szTitle.a = LPGEN("Currency Rate");
g_plugin.addUserInfo(wp, &odp);
return 0;
}
INT_PTR CurrencyRatesMenu_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 CurrencyRatesMenu_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(nullptr, L"open", sLogFileName.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
return 0;
}
INT_PTR CurrencyRatesMenu_RefreshContact(WPARAM wp, LPARAM)
{
MCONTACT hContact = MCONTACT(wp);
if (NULL == hContact)
return 0;
CCurrencyRatesProviders::TCurrencyRatesProviderPtr pProvider = CModuleInfo::GetCurrencyRateProvidersPtr()->GetContactProviderPtr(hContact);
if (!pProvider)
return 0;
pProvider->RefreshContact(hContact);
return 0;
}
static INT_PTR CALLBACK CurrencyRateInfoDlgProc1(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, CURRENCYRATES_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, CURRENCYRATES_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 CurrencyRateInfoDlgProcImpl(hContact, hdlg, msg, wParam, lParam);
}
int CurrencyRates_OnContactDoubleClick(WPARAM wp, LPARAM/* lp*/)
{
MCONTACT hContact = MCONTACT(wp);
if (CModuleInfo::GetCurrencyRateProvidersPtr()->GetContactProviderPtr(hContact)) {
MWindowList hWL = CModuleInfo::GetInstance().GetWindowList(WINDOW_PREFIX_INFO, true);
assert(hWL);
HWND hWnd = WindowList_Find(hWL, hContact);
if (nullptr != hWnd) {
SetForegroundWindow(hWnd);
SetFocus(hWnd);
}
else if (true == IsMyContact(hContact))
CreateDialogParam(g_plugin.getInst(), MAKEINTRESOURCE(IDD_DIALOG_CURRENCYRATE_INFO_1), nullptr, CurrencyRateInfoDlgProc1, LPARAM(hContact));
return 1;
}
return 0;
}
int CurrencyRates_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, CURRENCYRATES_PROTOCOL_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 == _waccess(sLogFileName.c_str(), 04));
if (true == bThereIsLogFile) {
#ifdef CHART_IMPLEMENT
Menu_EnableItem(g_hMenuChart, true);
#endif
Menu_EnableItem(g_hMenuOpenLogFile, true);
}
return 0;
}