summaryrefslogtreecommitdiff
path: root/plugins/Clist_blind/src/contact.cpp
blob: a6b983e7322bd681dcb5b213420587c48d2b79e7 (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
/*

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

Copyright (C) 2012-24 Miranda NG team (https://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 "stdafx.h"

int g_bSortByStatus, g_bSortByProto;

const struct
{
	int status, order;
}
static statusModeOrder[] = {
	{ ID_STATUS_OFFLINE, 500 },
	{ ID_STATUS_ONLINE, 10 },
	{ ID_STATUS_AWAY, 200 },
	{ ID_STATUS_DND, 110 },
	{ ID_STATUS_NA, 450 },
	{ ID_STATUS_OCCUPIED, 100 },
	{ ID_STATUS_FREECHAT, 0 },
	{ ID_STATUS_INVISIBLE, 20 }
};

static int GetStatusModeOrdering(int statusMode)
{
	for (int i = 0; i < _countof(statusModeOrder); i++)
		if (statusModeOrder[i].status == statusMode)
			return statusModeOrder[i].order;
	return 1000;
}

int CompareContacts(const ClcContact* c1, const ClcContact* c2)
{
	MCONTACT a = c1->hContact, b = c2->hContact;

	int statusa = db_get_w(a, c1->pce->szProto, "Status", ID_STATUS_OFFLINE);
	int statusb = db_get_w(b, c2->pce->szProto, "Status", ID_STATUS_OFFLINE);

	if (g_bSortByProto) {
		/* deal with statuses, online contacts have to go above offline */
		if ((statusa == ID_STATUS_OFFLINE) != (statusb == ID_STATUS_OFFLINE)) {
			return 2 * (statusa == ID_STATUS_OFFLINE) - 1;
		}
		/* both are online, now check protocols */
		if (c1->pce->szProto != nullptr && c2->pce->szProto != nullptr) {
			int rc = mir_strcmp(c1->pce->szProto, c2->pce->szProto);
			if (rc != 0)
				return rc;
		}
		/* protocols are the same, order by display name */
	}

	if (g_bSortByStatus) {
		int ordera = GetStatusModeOrdering(statusa);
		int orderb = GetStatusModeOrdering(statusb);
		if (ordera != orderb)
			return ordera - orderb;
	}
	else {
		//one is offline: offline goes below online
		if ((statusa == ID_STATUS_OFFLINE) != (statusb == ID_STATUS_OFFLINE))
			return 2 * (statusa == ID_STATUS_OFFLINE) - 1;
	}

	wchar_t namea[128];
	wchar_t *nameb = Clist_GetContactDisplayName(a);
	wcsncpy_s(namea, nameb, _TRUNCATE);
	namea[ _countof(namea)-1 ] = 0;
	nameb = Clist_GetContactDisplayName(b);

	//otherwise just compare names
	return mir_wstrcmpi(namea, nameb);
}