summaryrefslogtreecommitdiff
path: root/plugins/WinterSpeak/src/ConfigDatabase.cpp
blob: 0184e7e5d091b26d0a625cf7a231cbb10eccf70f (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
#include "Common.h"
#include "ConfigDatabase.h"


namespace
{
	const char *SPEAK        = "speak_config";
	const char *ACTIVE_FLAGS = "active_flags";
	const char *ACTIVE_STATE = "active_state";
	const char *ACTIVE_MSG   = "active_msg";
	const char *WELCOME_MSG  = "welcome_msg";
	const char *ENGINE       = "engine";
	const char *VOICE        = "voice";
	const char *VOLUME       = "volume";
	const char *RATE         = "rate";
	const char *PITCH        = "pitch";
}

ConfigDatabase::ConfigDatabase() : m_voice_desc(), m_active_flags(0), m_welcome_msg(L""), m_active_users()
{	
	// load the database from miranda
	load();
}


ConfigDatabase::~ConfigDatabase(void)
{
}


bool ConfigDatabase::getActiveFlag(ActiveFlag flag) const
{
	return ((m_active_flags & (1 << flag)) != 0);
}

//------------------------------------------------------------------------------
void ConfigDatabase::setActiveFlag(ActiveFlag flag, bool state)
{ 
	if (state)
	{
		m_active_flags |= (1 << flag);
	}
	else
	{
		m_active_flags &= ~(1 << flag);
	}
}

//------------------------------------------------------------------------------
ConfigDatabase::act ConfigDatabase::getActiveUser(HCONTACT user) const
{
	ActiveUsersMap::const_iterator iter = m_active_users.find(user);

	if (iter == m_active_users.end())
	{
		// get the unknown user status
		iter = m_active_users.find(0);
		
		if (iter == m_active_users.end())
		{
			ConfigDatabase::act ret;
			ret.message = false;
			ret.status = false;
			return ret;
		}
	}

	return iter->second;
}

//------------------------------------------------------------------------------
void ConfigDatabase::setActiveUser(HCONTACT user, act state)
{
	m_active_users[user].status = state.status;
	m_active_users[user].message = state.message;
}

//------------------------------------------------------------------------------
void ConfigDatabase::load()
{
	m_voice_desc.engine = DBGetContactSettingString(SPEAK, ENGINE, L"");
	m_voice_desc.voice  = DBGetContactSettingString(SPEAK, VOICE, L"");
	m_voice_desc.volume = db_get_dw(NULL, SPEAK, VOLUME, 50);
	m_voice_desc.pitch  = db_get_dw(NULL, SPEAK, PITCH, 50);
	m_voice_desc.rate   = db_get_dw(NULL, SPEAK, RATE, 50);

	m_active_flags = db_get_dw(NULL, SPEAK, ACTIVE_FLAGS, 0xffff);

	m_welcome_msg = DBGetContactSettingString(SPEAK, WELCOME_MSG, L"Welcome to Miranda");

	// iterate through all the users and add them to the list if active
	HCONTACT contact = db_find_first();
	while (NULL != contact) {
		m_active_users[contact].status = (db_get_b(contact, SPEAK, ACTIVE_STATE, true) != 0);
		m_active_users[contact].message = (db_get_b(contact, SPEAK, ACTIVE_MSG, true) != 0);

		contact = db_find_next(contact); 
	}

	// load unknown users setting
	m_active_users[0].status = (db_get_b(NULL, SPEAK, ACTIVE_STATE, true) != 0);
	m_active_users[0].message = (db_get_b(NULL, SPEAK, ACTIVE_MSG, true) != 0);
}

//------------------------------------------------------------------------------
void ConfigDatabase::save()
{
	db_set_ts(NULL, SPEAK, ENGINE, m_voice_desc.engine.c_str());
	db_set_ts(NULL, SPEAK, VOICE, m_voice_desc.voice.c_str());
	db_set_dw(NULL, SPEAK, VOLUME, m_voice_desc.volume);
	db_set_dw(NULL, SPEAK, PITCH, m_voice_desc.pitch);
	db_set_dw(NULL, SPEAK, RATE, m_voice_desc.rate);

	db_set_dw(NULL, SPEAK, ACTIVE_FLAGS, m_active_flags);

	db_set_ts(NULL, SPEAK, WELCOME_MSG, m_welcome_msg.c_str());

	for (ActiveUsersMap::iterator i = m_active_users.begin(); i != m_active_users.end(); ++i)
	{
		db_set_b(i->first, SPEAK, ACTIVE_STATE, i->second.status);
		db_set_b(i->first, SPEAK, ACTIVE_MSG, i->second.message);
	}

	// notify the subjects that things have changed
	notify();
}

//------------------------------------------------------------------------------
// private:
//------------------------------------------------------------------------------
std::wstring ConfigDatabase::DBGetContactSettingString(const char *szModule, const char *szSetting, const WCHAR *def)
{
	std::wstring ret = def;
	DBVARIANT dbv;
	
	if (!db_get_ts(NULL, szModule, szSetting, &dbv))
	{
		ret = dbv.pwszVal;
	}
	
	return ret;
}

//==============================================================================