summaryrefslogtreecommitdiff
path: root/plugins/HistoryStats/src/mirandahistory.cpp
blob: 867b8756f2f7ff657148035e4b3e23cadf259c73 (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
#include "_globals.h"
#include "mirandahistory.h"

#include "mirandasettings.h"

/*
 * MirandaHistory
 */

void MirandaHistory::populateProtocols()
{
	m_Protocols.clear();

	PROTOACCOUNT **protoList;
	int protoCount;

	if (mu::proto::enumProtocols(&protoCount, &protoList) == 0) {				
		upto_each_(i, protoCount) 
		{
			ext::a::string protoName = protoList[i]->szModuleName;

			Protocol& curProto = m_Protocols[protoName];

			curProto.displayName = Protocol::getDisplayName(protoName);
		}
	}

	m_DefaultProtocol.displayName = TranslateT("(Unknown)");
}

const Protocol& MirandaHistory::getProtocol(const ext::a::string& protocol) const
{
	std::map<ext::a::string, Protocol>::const_iterator i = m_Protocols.find(protocol);

	return (i != m_Protocols.end()) ? i->second : m_DefaultProtocol;
}

void MirandaHistory::makeContactsAvailable()
{
	if (m_bContactsAvailable)
		return;

	// make protocols available
	populateProtocols();

	// first run:
	// - enum all contacts
	// - skip for ignored protocol
	// - handle meta contacts (if enabled and available)
	// - skip manually excluded
	readContacts();

	// second run:
	// - merge contacts with similar names
	mergeContacts();

	m_bContactsAvailable = true;
}

void MirandaHistory::readContacts()
{
	bool bHandleMeta = mu::metacontacts::_available() && m_Settings.m_MetaContactsMode != Settings::mcmIgnoreMeta;
	ext::a::string strMetaProto = bHandleMeta ? META_PROTO : "";
	MirandaSettings db;

	std::vector<MCONTACT> sources;

	MCONTACT hContact = db_find_first();
	while (hContact) {
		db.setContact(hContact);

		const char* pProtoName = GetContactProto(hContact);

		// if something leads to ignorance of conact jump to end of
		// processing this contact via 'break'
		do {
			// ignore because of bad or not loaded protocol?
			if (!pProtoName)
				pProtoName = con::ProtoUnknown; // MEMO: alternative would be "break;"

			ext::string curNick = mu::clist::getContactDisplayName(hContact);

			// retrieve protocol
			const ext::a::string curProtoName = pProtoName;
			const Protocol& curProto = getProtocol(curProtoName);

			// retrieve group
			db.setModule(con::ModCList);
			ext::string curGroup = db.readStrDirect(con::SettGroup, TranslateT("(none)"));

			// ignore because of filtered protocol?
			if (m_Settings.m_ProtosIgnore.find(curProtoName) != m_Settings.m_ProtosIgnore.end())
				break;

			// init list of event sources
			sources.clear();
			sources.push_back(hContact);

			// handle meta-contacts
			if (bHandleMeta) {
				if (curProtoName == strMetaProto) {
					// don't include meta-contact history
					if (m_Settings.m_MetaContactsMode == Settings::mcmSubOnly)
						sources.clear();

					// include meta-contact's subcontact
					if (m_Settings.m_MetaContactsMode != Settings::mcmMetaOnly) {
						// find subcontacts to read history from
						int numSubs = mu::metacontacts::getNumContacts(hContact);
						if (numSubs > 0) {
							for (int i = 0; i < numSubs; ++i) {
								MCONTACT hSubContact = mu::metacontacts::getSubContact(hContact, i);
								if (hSubContact)
									sources.push_back(hSubContact);
							}
						}
					}
				}
				else {
					// ignore because of meta-contact?
					if (db_mc_getMeta(hContact))
						break;
				}
			}

			// ignore because of exclude?
			db.setModule(con::ModHistoryStats);

			if (db.readBool(con::SettExclude, false))
				break;

			// finally add to list
			MirandaContact* pContact = MirandaContactFactory::makeMirandaContact(m_Settings.m_MergeMode, curNick, curProto.displayName, curGroup, sources);
			m_Contacts.push_back(pContact);
		}
			while (false);

		hContact = db_find_next(hContact);
	}
}

void MirandaHistory::mergeContacts()
{
	if (!m_Settings.m_MergeContacts)
		return;

	for (ContactList::size_type i = 0; i < m_Contacts.size(); ++i) {
		MirandaContact& cur = *m_Contacts[i];

		for (ContactList::size_type j = i + 1; j < m_Contacts.size(); ++j) {
			if (m_Contacts[j]->getNick() == cur.getNick()) {
				if (!m_Settings.m_MergeContactsGroups || m_Contacts[j]->getGroup() == cur.getGroup()) {
					cur.merge(*m_Contacts[j]);
					delete m_Contacts[j];

					m_Contacts.erase(m_Contacts.begin() + j);
					--j;
				}
			}
		}
	}
}

MirandaHistory::MirandaHistory(const Settings& settings) :
	m_Settings(settings), m_bContactsAvailable(false)
{
}

MirandaHistory::~MirandaHistory()
{
	citer_each_(ContactList, i, m_Contacts)
	{
		delete *i;
	}

	m_Contacts.clear();
}

int MirandaHistory::getContactCount()
{
	if (!m_bContactsAvailable)
		makeContactsAvailable();

	return m_Contacts.size();
}

MirandaContact& MirandaHistory::getContact(int index)
{
	if (!m_bContactsAvailable)
		makeContactsAvailable();

	assert(index >= 0 && index < m_Contacts.size());

	return *m_Contacts[index];
}