summaryrefslogtreecommitdiff
path: root/src/mir_core/mc.cpp
blob: 8fdc9fda4ab000d23cef58ba59280486618caa63 (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
/*

Miranda NG: the free IM client for Microsoft* Windows*

Copyright (c) 2012-14 Miranda NG project (http://miranda-ng.org),
Copyright (c) 2000-12 Miranda IM project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "commonheaders.h"

static HANDLE hEventDefaultChanged;
static bool g_bEnabled;

void InitMetaContacts()
{
	hEventDefaultChanged = CreateHookableEvent(ME_MC_DEFAULTTCHANGED);
}

DBCachedContact* CheckMeta(MCONTACT hMeta)
{
	if (!g_bEnabled)
		return NULL;

	DBCachedContact *cc = currDb->m_cache->GetCachedContact(hMeta);
	return (cc == NULL || cc->nSubs == -1) ? NULL : cc;
}

int Meta_GetContactNumber(DBCachedContact *cc, MCONTACT hContact)
{
	if (g_bEnabled)
		for (int i = 0; i < cc->nSubs; i++)
			if (cc->pSubs[i] == hContact)
				return i;

	return -1;
}

MCONTACT Meta_GetContactHandle(DBCachedContact *cc, int contact_number)
{
	if (contact_number >= cc->nSubs || contact_number < 0 || !g_bEnabled)
		return 0;

	return cc->pSubs[contact_number];
}

/////////////////////////////////////////////////////////////////////////////////////////
// metacontacts

MIR_CORE_DLL(BOOL) db_mc_isEnabled(void)
{
	return g_bEnabled;
}

MIR_CORE_DLL(void) db_mc_enable(BOOL bEnabled)
{
	g_bEnabled = bEnabled != 0;
}

MIR_CORE_DLL(BOOL) db_mc_isMeta(MCONTACT hContact)
{
	if (currDb == NULL || !g_bEnabled) return FALSE;

	DBCachedContact *cc = currDb->m_cache->GetCachedContact(hContact);
	return (cc == NULL) ? FALSE : cc->nSubs != -1;
}

MIR_CORE_DLL(BOOL) db_mc_isSub(MCONTACT hContact)
{
	if (currDb == NULL || !g_bEnabled) return FALSE;

	DBCachedContact *cc = currDb->m_cache->GetCachedContact(hContact);
	return (cc == NULL) ? FALSE : cc->parentID != 0;
}

//returns a handle to the default contact, or null on failure
MIR_CORE_DLL(MCONTACT) db_mc_getDefault(MCONTACT hMetaContact)
{
	DBCachedContact *cc = CheckMeta(hMetaContact);
	if (cc == NULL)
		return 0;

	return (cc->nDefault != -1) ? Meta_GetContactHandle(cc, cc->nDefault) : 0;
}

//returns the default contact number, or -1 on failure
MIR_CORE_DLL(int) db_mc_getDefaultNum(MCONTACT hMetaContact)
{
	DBCachedContact *cc = CheckMeta(hMetaContact);
	return (cc == NULL) ? -1 : cc->nDefault;
}

//returns the number of subcontacts, or -1 on failure
MIR_CORE_DLL(int) db_mc_getSubCount(MCONTACT hMetaContact)
{
	DBCachedContact *cc = CheckMeta(hMetaContact);
	return (cc == NULL) ? -1 : cc->nSubs;
}

// returns parent hContact for a subcontact or NULL if it's not a sub
MIR_CORE_DLL(MCONTACT) db_mc_getMeta(MCONTACT hSubContact)
{
	if (currDb == NULL) return false;

	DBCachedContact *cc = currDb->m_cache->GetCachedContact(hSubContact);
	return (cc == NULL) ? NULL : cc->parentID;
}

// returns parent hContact for a subcontact or hContact itself if it's not a sub
MIR_CORE_DLL(MCONTACT) db_mc_tryMeta(MCONTACT hContact)
{
	if (currDb == NULL) return hContact;

	DBCachedContact *cc = currDb->m_cache->GetCachedContact(hContact);
	if (cc == NULL) return hContact;

	return (cc->IsSub()) ? cc->parentID : hContact;
}

// returns a subcontact with the given index
MIR_CORE_DLL(MCONTACT) db_mc_getSub(MCONTACT hMetaContact, int iNum)
{
	DBCachedContact *cc = CheckMeta(hMetaContact);
	return (cc == NULL) ? 0 : Meta_GetContactHandle(cc, iNum);
}

//sets the default contact, using the subcontact's handle
MIR_CORE_DLL(int) db_mc_setDefault(MCONTACT hMetaContact, MCONTACT hSub, BOOL bWriteDb)
{
	DBCachedContact *cc = CheckMeta(hMetaContact);
	if (cc == NULL)
		return 1;

	int contact_number = Meta_GetContactNumber(cc, hSub);
	if (contact_number == -1) 
		return 1;

	cc->nDefault = contact_number;
	if (bWriteDb)
		currDb->MetaSetDefault(cc);
	
	NotifyEventHooks(hEventDefaultChanged, hMetaContact, hSub);
	return 0;
}

//sets the default contact, using the subcontact's number
MIR_CORE_DLL(int) db_mc_setDefaultNum(MCONTACT hMetaContact, int iNum, BOOL bWriteDb)
{
	DBCachedContact *cc = CheckMeta(hMetaContact);
	if (cc == NULL)
		return 1;
	if (iNum >= cc->nSubs || iNum < 0)
		return 1;

	cc->nDefault = iNum;
	if (bWriteDb)
		currDb->MetaSetDefault(cc);

	NotifyEventHooks(hEventDefaultChanged, hMetaContact, Meta_GetContactHandle(cc, iNum));
	return 0;
}

extern "C" MIR_CORE_DLL(void) db_mc_notifyDefChange(WPARAM wParam, LPARAM lParam)
{
	NotifyEventHooks(hEventDefaultChanged, wParam, lParam);
}