summaryrefslogtreecommitdiff
path: root/plugins/BuddyPounce/src/dialog.cpp
blob: 19bb83e04818644119cb52b522874758548afc11 (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
196
197
198
#include "stdafx.h"

void getDefaultMessage(HWND hwnd, UINT control, MCONTACT hContact)
{
	DBVARIANT dbv;
	if (!g_plugin.getWString(hContact, "PounceMsg", &dbv)) {
		SetDlgItemText(hwnd, control, dbv.pwszVal);
		db_free(&dbv);
	}
	else if (!g_plugin.getWString("PounceMsg", &dbv)) {
		SetDlgItemText(hwnd, control, dbv.pwszVal);
		db_free(&dbv);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////

CBuddyPounceBasicDlg::CBuddyPounceBasicDlg(int dlgId) :
	CDlgBase(g_plugin, dlgId),
	edtMessage(this, IDC_MESSAGE)
{
	edtMessage.OnChange = Callback(this, &CBuddyPounceBasicDlg::onChanged_Message);
}

bool CBuddyPounceBasicDlg::OnInitDialog()
{
	getDefaultMessage(m_hwnd, IDC_MESSAGE, hContact);
	onChanged_Message(0);
	return true;
}

bool CBuddyPounceBasicDlg::OnApply()
{
	ptrW wszMessage(edtMessage.GetText());
	if (mir_wstrlen(wszMessage))
		g_plugin.setWString(hContact, "PounceMsg", wszMessage);
	else
		g_plugin.delSetting(hContact, "PounceMsg");
	return true;
}

void CBuddyPounceBasicDlg::onChanged_Message(CCtrlEdit *)
{
	int length = GetWindowTextLength(edtMessage.GetHwnd());
	CMStringW str(FORMAT, TranslateT("The Message    (%d Characters)"), length);
	SetDlgItemTextW(m_hwnd, GRP_MSG, str);
}

/////////////////////////////////////////////////////////////////////////////////////////
// CBuddyPounceSimpleDlg class implementation

CBuddyPounceSimpleDlg::CBuddyPounceSimpleDlg(MCONTACT _1) :
	CSuper(IDD_POUNCE_SIMPLE),
	btnAdvanced(this, IDC_ADVANCED)
{
	hContact = _1;

	btnAdvanced.OnClick = Callback(this, &CBuddyPounceSimpleDlg::onClick_Advanced);
}

void CBuddyPounceSimpleDlg::onClick_Advanced(CCtrlButton *)
{
	(new CBuddyPounceDlg(hContact))->Create();

	OnApply();
	Close();
}

/////////////////////////////////////////////////////////////////////////////////////////

CBuddyPounceDlg::CBuddyPounceDlg(MCONTACT _1) :
	CSuper(IDD_POUNCE),
	m_contacts(this, IDC_CONTACTS),
	chkSimple(this, IDC_SIMPLE),
	btnDelete(this, IDC_DELETE),
	btnDefault(this, IDC_DEFAULT)
{
	hContact = _1;

	btnDelete.OnClick = Callback(this, &CBuddyPounceDlg::onClick_Delete);
	btnDefault.OnClick = Callback(this, &CBuddyPounceDlg::onClick_Default);

	chkSimple.OnChange = Callback(this, &CBuddyPounceDlg::onChange_Simple);
}

bool CBuddyPounceDlg::OnInitDialog()
{
	CSuper::OnInitDialog();

	// populate contacts
	for (auto &cc: Contacts()) {
		char *szProto = Proto_GetBaseAccountName(cc);
		if (szProto && (CallProtoService(szProto, PS_GETCAPS, PFLAGNUM_1, 0) & PF1_IM)) {
			wchar_t name[300];
			mir_snwprintf(name, L"%s (%S)", Clist_GetContactDisplayName(cc), szProto);
			int index = m_contacts.AddString(name, cc);
			if (hContact == cc)
				m_contacts.SetCurSel(index);
		}
	}

	return true;
}

void CBuddyPounceDlg::onChange_Simple(CCtrlCheck *)
{
	if (!m_bInitialized) return;

	(new CBuddyPounceSimpleDlg(hContact))->Create();

	OnApply();
	Close();
}

void CBuddyPounceDlg::onClick_Delete(CCtrlButton *)
{
	edtMessage.SetText(L"");
	onChanged_Message(0);

	g_plugin.delSetting(hContact, "PounceMsg");
	g_plugin.delSetting(hContact, "SendIfMyStatusIsFLAG");
	g_plugin.delSetting(hContact, "SendIfTheirStatusIsFLAG");
	g_plugin.delSetting(hContact, "Reuse");
	g_plugin.delSetting(hContact, "GiveUpDays");
	g_plugin.delSetting(hContact, "GiveUpDate");
	g_plugin.delSetting(hContact, "ConfirmTimeout");
	g_plugin.delSetting(hContact, "FileToSend");
}

void CBuddyPounceDlg::onClick_Default(CCtrlButton *)
{
	getDefaultMessage(m_hwnd, IDC_MESSAGE, hContact);
	onChanged_Message(0);

	g_plugin.setWord(hContact, "SendIfMyStatusIsFLAG", g_plugin.getWord("SendIfMyStatusIsFLAG", 0));
	g_plugin.setWord(hContact, "SendIfTheirStatusIsFLAG", g_plugin.getWord("SendIfTheirStatusIsFLAG", 0));
	g_plugin.setByte(hContact, "Reuse", g_plugin.getByte("Reuse", 0));
	g_plugin.setByte(hContact, "GiveUpDays", g_plugin.getByte("GiveUpDays", 0));
	g_plugin.setDword(hContact, "GiveUpDate", g_plugin.getDword("GiveUpDate", 0));
	g_plugin.setWord(hContact, "ConfirmTimeout", g_plugin.getWord("ConfirmTimeout", 0));
}

/////////////////////////////////////////////////////////////////////////////////////////
// Pounce send confirmation dialog

class CPounceSentDlg : public CDlgBase
{
	bool m_bSuccess;
	MCONTACT m_hContact;

	CCtrlEdit edtMessage;

public:
	CPounceSentDlg(MCONTACT hContact, bool bSuccess) :
		CDlgBase(g_plugin, IDD_CONFIRMSEND),
		m_bSuccess(bSuccess),
		m_hContact(hContact),
		edtMessage(this, IDC_MESSAGE)
	{}

	bool OnInitDialog() override
	{
		ptrW wszMessage(g_plugin.getWStringA(m_hContact, "PounceMsg"));
		if (wszMessage == nullptr)
			return false;

		edtMessage.SetText(wszMessage);

		wchar_t msg[256];
		if (m_bSuccess) {
			mir_snwprintf(msg, TranslateT("Message successfully sent to %s"), Clist_GetContactDisplayName(m_hContact));
			SetDlgItemText(m_hwnd, IDOK, TranslateT("OK"));
			ShowWindow(GetDlgItem(m_hwnd, IDCANCEL), 0);
		}
		else {
			mir_snwprintf(msg, TranslateT("Message failed to send to %s"), Clist_GetContactDisplayName(m_hContact));
			SetDlgItemText(m_hwnd, IDOK, TranslateT("Retry"));
		}
		SetDlgItemText(m_hwnd, LBL_CONTACT, msg);
		SetWindowText(m_hwnd, TranslateT(modFullname));
		return true;
	}

	bool OnApply() override
	{
		if (!m_bSuccess) {
			ptrW wszMessage(edtMessage.GetText());
			if (wszMessage)
				SendPounce(wszMessage, m_hContact);
		}
		return true;
	}
};

void CreateMessageAcknowlegedWindow(MCONTACT hContact, int SentSuccess)
{
	(new CPounceSentDlg(hContact, SentSuccess != 0))->Create();
}