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

#include "QuotesProviderDukasCopy.h"
#include "EconomicRateInfo.h"
#include "QuotesProviderGoogle.h"
#include "DBUtils.h"
#include "QuotesProviderGoogleFinance.h"
#include "QuotesProviderYahoo.h"

#define LAST_RUN_VERSION "LastRunVersion"

CQuotesProviders::CQuotesProviders()
{
	InitProviders();
}

CQuotesProviders::~CQuotesProviders()
{
	ClearProviders();
}

const CQuotesProviders::TQuotesProviders& CQuotesProviders::GetProviders()const
{
	return m_apProviders;
}

namespace
{
	template<class T>void create_provider(CQuotesProviders::TQuotesProviders& apProviders)
	{
		CQuotesProviders::TQuotesProviderPtr pProvider(new T);
		if(pProvider->Init())
		{
			apProviders.push_back(pProvider);
		}
	}
}

void CQuotesProviders::CreateProviders()
{
	create_provider<CQuotesProviderDukasCopy>(m_apProviders);
	create_provider<CQuotesProviderGoogle>(m_apProviders);
	create_provider<CQuotesProviderGoogleFinance>(m_apProviders);
	create_provider<CQuotesProviderYahoo>(m_apProviders);
}

void CQuotesProviders::ClearProviders()
{
	m_apProviders.clear();
}

namespace
{
	void convert_contact_settings(HANDLE hContact) 
	{
		WORD dwLogMode = DBGetContactSettingWord(hContact,QUOTES_PROTOCOL_NAME,DB_STR_QUOTE_LOG,static_cast<WORD>(lmDisabled));
		if ((dwLogMode&lmInternalHistory) || (dwLogMode&lmExternalFile))
		{
			DBWriteContactSettingByte(hContact,QUOTES_PROTOCOL_NAME,DB_STR_CONTACT_SPEC_SETTINGS,1);
		}
	}
}
void CQuotesProviders::InitProviders()
{
	CreateProviders();

	const WORD nCurrentVersion = 17;
	WORD nVersion = DBGetContactSettingWord(NULL,QUOTES_MODULE_NAME,LAST_RUN_VERSION,1);

	for(HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact))
	{
		TQuotesProviderPtr pProvider = GetContactProviderPtr(hContact);
		if(pProvider)
		{
			pProvider->AddContact(hContact);
			if(nVersion < nCurrentVersion)
			{
				convert_contact_settings(hContact);
			}
		}
	}

	DBWriteContactSettingWord(NULL,QUOTES_MODULE_NAME,LAST_RUN_VERSION,nCurrentVersion);
}

CQuotesProviders::TQuotesProviderPtr CQuotesProviders::GetContactProviderPtr(HANDLE hContact)const
{
	char* szProto = reinterpret_cast<char*>(CallService(MS_PROTO_GETCONTACTBASEPROTO,
											reinterpret_cast<WPARAM>(hContact),0));
	if(NULL == szProto || 0 != ::_stricmp(szProto,QUOTES_PROTOCOL_NAME))
	{
		return TQuotesProviderPtr();
	}

	tstring sProvider = Quotes_DBGetStringT(hContact,QUOTES_MODULE_NAME,DB_STR_QUOTE_PROVIDER);
	if(true == sProvider.empty())
	{
		return TQuotesProviderPtr();
	}

	return FindProvider(sProvider);
}

CQuotesProviders::TQuotesProviderPtr CQuotesProviders::FindProvider(const tstring& rsName)const
{
	TQuotesProviderPtr pResult;
	for(TQuotesProviders::const_iterator i = m_apProviders.begin();i != m_apProviders.end();++i)
	{
		const TQuotesProviderPtr& pProvider = *i;
		const IQuotesProvider::CProviderInfo& rInfo = pProvider->GetInfo();
		if(0 == ::quotes_stricmp(rsName.c_str(),rInfo.m_sName.c_str()))
		{
			pResult = pProvider;
			break;
		}
	}

	return pResult;
}