summaryrefslogtreecommitdiff
path: root/plugins/StatusPlugins/AdvancedAutoAway/trigger_aaa.cpp
blob: e803c2dab4f9258cd6514dd8e37cea91d41fd57e (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
#include "../commonstatus.h"
#include "advancedautoaway.h"
#include <m_trigger.h>
#include "trigger_aaa.h"
#include "../resource.h"

extern HINSTANCE hInst;
static HANDLE hAAAStateChangedHook;

static int AAAStateChanged(WPARAM wParam, LPARAM lParam) {

	AUTOAWAYSETTING *aas;
	DWORD triggerID;
	REPORTINFO ri;
	TRIGGERDATA td;
	BOOL bReport;

	aas = (AUTOAWAYSETTING *)lParam;
	triggerID = 0;
	do {
		triggerID = (DWORD)CallService(MS_TRIGGER_FINDNEXTTRIGGERID, triggerID, (LPARAM)TRIGGERNAME);
		if (triggerID == 0) {
			continue;
		}
		bReport = FALSE;
		switch (aas->curState) {
		case STATUS1_SET:
			if (DBGetTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_ENTERFIRST, 0)) {
				bReport = TRUE;
			}
			break;
		case STATUS2_SET:
			if (DBGetTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_ENTERSECOND, 0)) {
				bReport = TRUE;
			}
			break;
		case SET_ORGSTATUS:
			if ( (aas->oldState == STATUS1_SET) && (DBGetTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_LEAVEFIRST, 0)) ) {
				bReport = TRUE;;
			}
			if ( (aas->oldState == STATUS2_SET) && (DBGetTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_LEAVESECOND, 0)) ) {
				bReport = TRUE;;
			}
			break;
		case HIDDEN_ACTIVE:
			if (DBGetTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_BECOMEACTIVE, 0)) {
				bReport = TRUE;
			}
			break;
		case ACTIVE:
			if (aas->oldState == HIDDEN_ACTIVE) {
				char setting[128];
				char *szProto;

				szProto = DBGetContactSettingByte(NULL, MODULENAME, SETTING_SAMESETTINGS, 0)?SETTING_ALL:aas->protocolSetting->szName;
				_snprintf(setting, sizeof(setting), "%s_Lv1Status", szProto);
				if ( (aas->protocolSetting->lastStatus == DBGetContactSettingWord(NULL, MODULENAME, setting, ID_STATUS_AWAY)) && DBGetTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_LEAVEFIRST, 0) ) {
					bReport = TRUE;
				}
				
				_snprintf(setting, sizeof(setting), "%s_Lv2Status", szProto);
				if ( (aas->protocolSetting->lastStatus == DBGetContactSettingWord(NULL, MODULENAME, setting, ID_STATUS_NA)) && DBGetTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_LEAVESECOND, 0) ) {
					bReport = TRUE;
				}
			}
			break;
		}
		if (!bReport) {
			continue;
		}
		ZeroMemory(&td, sizeof(td));
		td.cbSize = sizeof(td);
		td.dFlags = DF_PROTO|DF_STATUS|DF_TEXT;
		td.szProto = td.szText = aas->protocolSetting->szName;
		td.status = aas->protocolSetting->status;
		ZeroMemory(&ri, sizeof(REPORTINFO));
		ri.cbSize = sizeof(REPORTINFO);
		ri.triggerID = triggerID;
		ri.pszName = TRIGGERNAME;
		ri.flags = TRG_PERFORM;
		ri.td = &td;
		CallService(MS_TRIGGER_REPORTEVENT, 0, (LPARAM)&ri);
	} while (triggerID != 0);

	return 0;
}

static INT_PTR CALLBACK DlgProcOptsAAAStateChange(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) {

    switch (msg) {
	case WM_INITDIALOG: {
		DWORD triggerID;

		TranslateDialogDefault(hwndDlg);		
		triggerID = (DWORD)lParam;
		CheckDlgButton(hwndDlg, IDC_ENTERFIRST, DBGetTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_ENTERFIRST, 0)?BST_CHECKED:BST_UNCHECKED);
		CheckDlgButton(hwndDlg, IDC_ENTERSECOND, DBGetTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_ENTERSECOND, 0)?BST_CHECKED:BST_UNCHECKED);
		CheckDlgButton(hwndDlg, IDC_LEAVEFIRST, DBGetTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_LEAVEFIRST, 0)?BST_CHECKED:BST_UNCHECKED);
		CheckDlgButton(hwndDlg, IDC_LEAVESECOND, DBGetTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_LEAVESECOND, 0)?BST_CHECKED:BST_UNCHECKED);
		CheckDlgButton(hwndDlg, IDC_BECOMEACTIVE, DBGetTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_BECOMEACTIVE, 0)?BST_CHECKED:BST_UNCHECKED);
		break;
						}

	case TM_ADDTRIGGER: {
		// wParam = trigger ID
		DWORD triggerID;

		triggerID = (DWORD)wParam;
		DBWriteTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_ENTERFIRST, (BYTE)IsDlgButtonChecked(hwndDlg, IDC_ENTERFIRST));
		DBWriteTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_ENTERSECOND, (BYTE)IsDlgButtonChecked(hwndDlg, IDC_ENTERSECOND));
		DBWriteTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_LEAVEFIRST, (BYTE)IsDlgButtonChecked(hwndDlg, IDC_LEAVEFIRST));
		DBWriteTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_LEAVESECOND, (BYTE)IsDlgButtonChecked(hwndDlg, IDC_LEAVESECOND));
		DBWriteTriggerSettingByte(triggerID, NULL, MODULENAME, SETTING_BECOMEACTIVE, (BYTE)IsDlgButtonChecked(hwndDlg, IDC_BECOMEACTIVE));
		break;
						}
	
	case TM_DELTRIGGER: {
		// wParam = triggerID
		DWORD triggerID;
		REMOVETRIGGERSETTINGS rts;
		
		triggerID = (DWORD)wParam;
		rts.cbSize = sizeof(REMOVETRIGGERSETTINGS);
		rts.prefix = PREFIX_TRIGGERID;
		rts.id = triggerID;
		rts.hContact = NULL;
		rts.szModule = MODULENAME;
		CallService(MS_TRIGGER_REMOVESETTINGS, 0, (LPARAM)&rts);
		break;
						}
	}

    return FALSE;
}

int InitTrigger() {

	TRIGGERREGISTER tr;

	if (!ServiceExists(MS_TRIGGER_REGISTERTRIGGER)) {
		return -1;
	}
	ZeroMemory(&tr, sizeof(tr));
	tr.cbSize = sizeof(tr);
	tr.pszName = TRIGGERNAME;
	tr.hInstance = hInst;
	tr.pfnDlgProc = DlgProcOptsAAAStateChange;
	tr.pszTemplate = MAKEINTRESOURCEA(IDD_TRG_AAASTATECHANGE);
	tr.dFlags = DF_STATUS|DF_PROTO;
	CallService(MS_TRIGGER_REGISTERTRIGGER, 0, (LPARAM)&tr);
	hAAAStateChangedHook = HookEvent(ME_AAA_STATECHANGED, AAAStateChanged);

	return 0;
}

int DeInitTrigger() {

	UnhookEvent(hAAAStateChangedHook);

	return 0;
}