-
Notifications
You must be signed in to change notification settings - Fork 50
/
uicontactlistdialog.cpp
124 lines (101 loc) · 3.31 KB
/
uicontactlistdialog.cpp
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
// uicontactlistdialog.cpp
//
// Copyright (c) 2019-2024 Kristofer Berggren
// All rights reserved.
//
// nchat is distributed under the MIT license, see LICENSE for details.
#include "uicontactlistdialog.h"
#include <algorithm>
#include "apputil.h"
#include "log.h"
#include "uimodel.h"
#include "strutil.h"
UiContactListDialog::UiContactListDialog(const UiDialogParams& p_Params)
: UiListDialog(p_Params, false /*p_ShadeHidden*/)
{
static bool s_ContactsRequested = false;
if (!s_ContactsRequested)
{
m_Model->RequestContacts();
s_ContactsRequested = true;
}
UpdateList();
}
UiContactListDialog::~UiContactListDialog()
{
}
UiContactListItem UiContactListDialog::GetSelectedContactItem()
{
return m_SelectedContactItem;
}
void UiContactListDialog::OnSelect()
{
if (m_ContactListItemVec.empty()) return;
m_SelectedContactItem = m_ContactListItemVec[m_Index];
m_Result = true;
m_Running = false;
}
void UiContactListDialog::OnBack()
{
}
bool UiContactListDialog::OnTimer()
{
int64_t modelContactInfosUpdateTime = m_Model->GetContactInfosUpdateTime();
if (m_DialogContactInfosUpdateTime != modelContactInfosUpdateTime)
{
UpdateList();
return true;
}
return false;
}
void UiContactListDialog::UpdateList()
{
int64_t modelContactInfosUpdateTime = m_Model->GetContactInfosUpdateTime();
if (m_DialogContactInfosUpdateTime != modelContactInfosUpdateTime)
{
m_DialogContactInfosUpdateTime = modelContactInfosUpdateTime;
m_DialogContactInfos = m_Model->GetContactInfos();
}
m_Index = 0;
m_Items.clear();
m_ContactListItemVec.clear();
// Use a local vector which is sorted before populating dialog members, which need to be in sync
std::vector<UiContactListItem> localContactListItemVec;
for (const auto& profileContactInfos : m_DialogContactInfos)
{
for (const auto& idContactInfo : profileContactInfos.second)
{
const std::string& profileId = profileContactInfos.first;
const std::string& contactId = idContactInfo.first;
const std::string& name = m_Model->GetContactListNameLock(profileId, contactId, false /*p_AllowId*/);;
if (name.empty()) continue;
if (m_FilterStr.empty() ||
(StrUtil::ToLower(name).find(StrUtil::ToLower(StrUtil::ToString(m_FilterStr))) != std::string::npos))
{
static const bool isMultipleProfiles = m_Model->IsMultipleProfiles();
std::string displayName = name +
(isMultipleProfiles ? " @ " + m_Model->GetProfileDisplayName(profileId) : "");
static const bool developerMode = AppUtil::GetDeveloperMode();
if (developerMode)
{
displayName += " [" + contactId + "]";
}
UiContactListItem contactListItem;
contactListItem.profileId = profileId;
contactListItem.contactId = contactId;
contactListItem.name = displayName;
localContactListItemVec.push_back(contactListItem);
}
}
}
std::sort(localContactListItemVec.begin(), localContactListItemVec.end(),
[&](const UiContactListItem& lhs, const UiContactListItem& rhs) -> bool
{
return lhs.name < rhs.name;
});
for (const auto& contactListItem : localContactListItemVec)
{
m_Items.push_back(StrUtil::TrimPadWString(StrUtil::ToWString(contactListItem.name), m_W));
m_ContactListItemVec.push_back(contactListItem);
}
}