-
Notifications
You must be signed in to change notification settings - Fork 1
/
Notifier.cpp
117 lines (99 loc) · 2.49 KB
/
Notifier.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
#include "StdAfx.h"
#include "Notifier.h"
CNotifier::CNotifier(void)
{
try
{
m_psnTalkingCallerNumber = new CStateNotifyByNameNoMask(SN_PHONETALKINGCALLERNUMBER);
m_psnTalkingCallerNumber->KeepStateData(true);
m_psnTalkingCallerName = new CStateNotifyByNameNoMask(SN_PHONETALKINGCALLERNAME);
m_psnTalkingCallerName->KeepStateData(true);
m_psnTalkingCallerContact = new CStateNotifyByNameNoMask(SN_PHONETALKINGCALLERCONTACT);
m_psnTalkingCallerContact->KeepStateData(true);
m_psnCallTalking = new CStateNotifyByName(SN_PHONECALLTALKING);
m_psnCallTalking->SetCallback(CallTalkingCallback, reinterpret_cast<DWORD>(this));
}
catch(...)
{
CancelNotifications();
throw;
}
}
CNotifier::~CNotifier(void)
{
CancelNotifications();
}
void CNotifier::CancelNotifications()
{
delete m_psnTalkingCallerNumber;
delete m_psnTalkingCallerName;
delete m_psnTalkingCallerContact;
delete m_psnCallTalking;
}
void CNotifier::OnPhoneTalkStarted()
{
foreach(ListenersListIter, i, m_pListeners)
{
(*i)->OnPhoneTalkStarted();
}
}
void CNotifier::OnPhoneTalkFinished()
{
foreach(ListenersListIter, i, m_pListeners)
{
(*i)->OnPhoneTalkFinished(
m_psnTalkingCallerName->GetAsString(),
m_psnTalkingCallerNumber->GetAsString() );
}
m_psnTalkingCallerNumber->ClearStateData();
m_psnTalkingCallerName->ClearStateData();
m_psnTalkingCallerContact->ClearStateData();
}
void CNotifier::CallTalkingCallback(HREGNOTIFY hNotify, DWORD dwUserData, const PBYTE pData, const UINT cbData)
{
DWORD state;
CNotifier *pobj;
// get the changed value
state = *((DWORD*)pData);
pobj = reinterpret_cast<CNotifier*>(dwUserData);
if (state & SN_PHONECALLTALKING_BITMASK)
{
pobj->OnPhoneTalkStarted();
}
else
{
pobj->OnPhoneTalkFinished();
}
}
bool CNotifier::FindListener(INotifListener *pListener, ListenersListIter *pIter)
{
foreach(ListenersListIter, i, m_pListeners)
{
if (*i == pListener)
{
if(pIter)
{
*pIter = i;
}
return true;
}
}
return false;
}
void CNotifier::AddListener(INotifListener *pListener)
{
if(HasListener(pListener))
throw CArgumentException(_T("pListener"), "Listener is already registered.");
m_pListeners.push_back(pListener);
}
bool CNotifier::HasListener(INotifListener *pListener)
{
return FindListener(pListener, NULL);
}
void CNotifier::RemoveListener(INotifListener *pListener)
{
ListenersListIter i;
if(!FindListener(pListener, &i))
throw CArgumentException(_T("pListener"), "Listener is not registered.");
m_pListeners.erase(i);
}