summaryrefslogtreecommitdiff
path: root/plugins/NewAwaySysMod/Properties.cpp
blob: eb14d799501fd2a04d1d4b4793d97216897e33fe (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
	New Away System - plugin for Miranda IM
	Copyright (C) 2005-2007 Chervov Dmitry

    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 "Common.h"
#include "Properties.h"

CProtoStates g_ProtoStates;


/*char *mystrdup(const char *szStr)
{
	char *szNew = (char *)malloc(lstrlen(szStr) + 1);
	lstrcpy(szNew, szStr);
	return szNew;
}


void *mymalloc(size_t size)
{
	return HeapAlloc(GetProcessHeap(), 0, size);//GlobalAlloc(GPTR, size);
}


void myfree(void *p)
{
	//GlobalFree(p);
	HeapFree(GetProcessHeap(), 0, p);
}


void *__cdecl operator new(unsigned int size)
{
	return (void *)HeapAlloc(GetProcessHeap(), 0, size);//GlobalAlloc(GPTR, size);
}


void __cdecl operator delete(void *p)
{
//	GlobalFree((HGLOBAL)p);
	HeapFree(GetProcessHeap(), 0, p);
}
*/


void ResetContactSettingsOnStatusChange(HANDLE hContact)
{
	DBDeleteContactSetting(hContact, MOD_NAME, DB_REQUESTCOUNT);
	DBDeleteContactSetting(hContact, MOD_NAME, DB_SENDCOUNT);
	DBDeleteContactSetting(hContact, MOD_NAME, DB_MESSAGECOUNT);
}


void ResetSettingsOnStatusChange(const char *szProto = NULL, int bResetPersonalMsgs = false, int Status = 0)
{
	if (bResetPersonalMsgs)
	{ // bResetPersonalMsgs &&= g_MoreOptPage.GetDBValueCopy(IDC_MOREOPTDLG_SAVEPERSONALMSGS);
		bResetPersonalMsgs = !g_MoreOptPage.GetDBValueCopy(IDC_MOREOPTDLG_SAVEPERSONALMSGS);
	}
	HANDLE hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0);
	while (hContact)
	{
		const char *szCurProto;
		if (!szProto || ((szCurProto = (const char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0)) && !lstrcmpA(szProto, szCurProto)))
		{
			ResetContactSettingsOnStatusChange(hContact);
			if (bResetPersonalMsgs)
			{
				CContactSettings(Status, hContact).SetMsgFormat(SMF_PERSONAL, NULL); // TODO: delete only when SAM dialog opens?
			}
		}
		hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM)hContact, 0);
	}
}


CProtoState::CStatus& CProtoState::CStatus::operator = (int Status)
{
	_ASSERT(Status >= ID_STATUS_OFFLINE && Status <= ID_STATUS_OUTTOLUNCH);
	if (Status < ID_STATUS_OFFLINE || Status > ID_STATUS_OUTTOLUNCH)
	{
		return *this; // ignore status change if the new status is unknown
	}
	bool bModified = false;
	if (szProto)
	{
		if (this->Status != Status)
		{
			this->Status = Status;
			(*GrandParent)[szProto].AwaySince.Reset();
			ResetSettingsOnStatusChange(szProto, true, Status);
			bModified = true;
		}
		if ((*GrandParent)[szProto].TempMsg.IsSet())
		{
			(*GrandParent)[szProto].TempMsg.Unset();
			bModified = true;
		}
	} else // global status change
	{
		int I;
		int bStatusModified = false;
		for (I = 0; I < GrandParent->GetSize(); I++)
		{
			CProtoState &State = (*GrandParent)[I];
			if (!DBGetContactSettingByte(NULL, State.GetProto(), "LockMainStatus", 0)) // if the protocol isn't locked
			{
				if (State.Status != Status)
				{
					State.Status.Status = Status; // "Status.Status" - changing Status directly to prevent recursive calls to the function
					State.AwaySince.Reset();
					bModified = true;
					bStatusModified = true;
				}
				if (State.TempMsg.IsSet())
				{
					State.TempMsg.Unset();
					bModified = true;
				}
				if (g_MoreOptPage.GetDBValueCopy(IDC_MOREOPTDLG_RESETPROTOMSGS))
				{
					CProtoSettings(State.szProto, Status).SetMsgFormat(SMF_PERSONAL, NULL);
				}
			}
		}
		if (bStatusModified)
		{
			ResetSettingsOnStatusChange(NULL, true, Status);
		}
	}
	if (bModified && g_SetAwayMsgPage.GetWnd())
	{
		SendMessage(g_SetAwayMsgPage.GetWnd(), UM_SAM_PROTOSTATUSCHANGED, (WPARAM)(char*)szProto, 0);
	}
	return *this;
}


void CProtoState::CAwaySince::Reset()
{
	GetLocalTime(&AwaySince);
	if (GrandParent && !szProto)
	{
		int I;
		for (I = 0; I < GrandParent->GetSize(); I++)
		{
			GetLocalTime((*GrandParent)[I].AwaySince);
		}
	}
}


void CContactSettings::SetMsgFormat(int Flags, TCString Message)
{
	if (Flags & SMF_PERSONAL)
	{ // set a personal message for a contact. also it's used to set global status message (hContact = NULL).
	// if Message == NULL, then the function deletes the message.
		CString DBSetting(StatusToDBSetting(Status, DB_STATUSMSG, IDC_MOREOPTDLG_PERSTATUSPERSONAL));
		if (g_AutoreplyOptPage.GetDBValueCopy(IDC_REPLYDLG_RESETCOUNTERWHENSAMEICON) && GetMsgFormat(SMF_PERSONAL) != (const TCHAR*)Message)
		{
			ResetContactSettingsOnStatusChange(hContact);
		}
		if (Message != NULL)
		{
			DBWriteContactSettingTString(hContact, MOD_NAME, DBSetting, Message);
		} else
		{
			DBDeleteContactSetting(hContact, MOD_NAME, DBSetting);
		}
	}
	if (Flags & (SMF_LAST | SMF_TEMPORARY))
	{
		_ASSERT(!hContact);
		CProtoSettings().SetMsgFormat(Flags & (SMF_LAST | SMF_TEMPORARY), Message);
	}
}


TCString CContactSettings::GetMsgFormat(int Flags, int *pOrder, char *szProtoOverride)
// returns the requested message; sets Order to the order of the message in the message tree, if available; or to -1 otherwise.
// szProtoOverride is needed only to get status message of the right protocol when the ICQ contact is on list, but not with the same protocol on which it requests the message - this way we can still get contact details.
{
	TCString Message = NULL;
	if (pOrder)
	{
		*pOrder = -1;
	}
	if (Flags & GMF_PERSONAL)
	{ // try getting personal message (it overrides global)
		Message = DBGetContactSettingString(hContact, MOD_NAME, StatusToDBSetting(Status, DB_STATUSMSG, IDC_MOREOPTDLG_PERSTATUSPERSONAL), (TCHAR*)NULL);
	}
	if (Flags & (GMF_LASTORDEFAULT | GMF_PROTOORGLOBAL | GMF_TEMPORARY) && Message.IsEmpty())
	{
		char *szProto = szProtoOverride ? szProtoOverride : (hContact ? (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0) : NULL);
		if (Flags & (GMF_LASTORDEFAULT | GMF_PROTOORGLOBAL))
		{ // we mustn't pass here by GMF_TEMPORARY flag, as otherwise we'll handle GMF_TEMPORARY | GMF_PERSONAL combination incorrectly, which is supposed to get only per-contact messages, and at the same time also may be used with NULL contact to get the global status message
			Message = CProtoSettings(szProto).GetMsgFormat(Flags, pOrder);
		} else if (!hContact)
		{ // handle global temporary message too
			if (g_ProtoStates[szProto].TempMsg.IsSet())
			{
				Message = g_ProtoStates[szProto].TempMsg;
			}
		}
	}
	return Message;
}


void CProtoSettings::SetMsgFormat(int Flags, TCString Message)
{
	if (Flags & (SMF_TEMPORARY | SMF_PERSONAL) && g_AutoreplyOptPage.GetDBValueCopy(IDC_REPLYDLG_RESETCOUNTERWHENSAMEICON) && GetMsgFormat(Flags & (SMF_TEMPORARY | SMF_PERSONAL)) != (const TCHAR*)Message)
	{
		ResetSettingsOnStatusChange(szProto);
	}
	if (Flags & SMF_TEMPORARY)
	{
		_ASSERT(!Status || Status == g_ProtoStates[szProto].Status);
		g_ProtoStates[szProto].TempMsg = (szProto || Message != NULL) ? Message : CProtoSettings(NULL, Status).GetMsgFormat(GMF_LASTORDEFAULT);
	}
	if (Flags & SMF_PERSONAL)
	{ // set a "personal" message for a protocol. also it's used to set global status message (hContact = NULL).
	// if Message == NULL, then we'll use the "default" message - i.e. it's either the global message for szProto != NULL (we delete the per-proto DB setting), or it's just a default message for a given status for szProto == NULL.
		g_ProtoStates[szProto].TempMsg.Unset();
		CString DBSetting(ProtoStatusToDBSetting(DB_STATUSMSG, IDC_MOREOPTDLG_PERSTATUSPROTOMSGS));
		if (Message != NULL)
		{
			DBWriteContactSettingTString(NULL, MOD_NAME, DBSetting, Message);
		} else
		{
			if (!szProto)
			{
				DBWriteContactSettingTString(NULL, MOD_NAME, DBSetting, CProtoSettings(NULL, Status).GetMsgFormat(GMF_LASTORDEFAULT)); // global message can't be NULL; we can use an empty string instead if it's really necessary
			} else
			{
				DBDeleteContactSetting(NULL, MOD_NAME, DBSetting);
			}
		}
	}
	if (Flags & SMF_LAST)
	{
		COptPage MsgTreeData(g_MsgTreePage);
		COptItem_TreeCtrl *TreeCtrl = (COptItem_TreeCtrl*)MsgTreeData.Find(IDV_MSGTREE);
		TreeCtrl->DBToMem(CString(MOD_NAME));
		int RecentGroupID = GetRecentGroupID(Status);
		if (RecentGroupID == -1)
		{ // we didn't find the group, it also means that we're using per status messages; so we need to create it
			TreeCtrl->Value.AddElem(CTreeItem(Status ? (const TCHAR*)CallService(MS_CLIST_GETSTATUSMODEDESCRIPTION, Status, GSMDF_TCHAR) : MSGTREE_RECENT_OTHERGROUP, g_Messages_RecentRootID, RecentGroupID = TreeCtrl->GenerateID(), TIF_GROUP));
			TreeCtrl->SetModified(true);
		}
		int I;
		TCString Title(_T(""));
		for (I = 0; I < TreeCtrl->Value.GetSize(); I++) // try to find an identical message in the same group (to prevent saving multiple identical messages), or at least if we'll find an identical message somewhere else, then we'll use its title for our new message
		{
			if (!(TreeCtrl->Value[I].Flags & TIF_GROUP) && TreeCtrl->Value[I].User_Str1 == (const TCHAR*)Message)
			{
				if (TreeCtrl->Value[I].ParentID == RecentGroupID)
				{ // found it in the same group
					int GroupOrder = TreeCtrl->IDToOrder(RecentGroupID);
					TreeCtrl->Value.MoveElem(I, (GroupOrder >= 0) ? (GroupOrder + 1) : 0); // now move it to the top of recent messages list
					TreeCtrl->SetModified(true);
					break; // no reason to search for anything else
				} else if (Title.IsEmpty())
				{ // it's not in the same group, but at least we'll use its title
					Title = TreeCtrl->Value[I].Title;
				}
			}
		}
		if (I == TreeCtrl->Value.GetSize())
		{ // we didn't find an identical message in the same group, so we'll add our new message here
			if (Title.IsEmpty())
			{ // didn't find a title for our message either
				if (Message.GetLen() > MRM_MAX_GENERATED_TITLE_LEN)
				{
					Title = Message.Left(MRM_MAX_GENERATED_TITLE_LEN - 3) + _T("...");
				} else
				{
					Title = Message;
				}
				TCHAR *p = Title.GetBuffer();
				while (*p) // remove "garbage"
				{
					if (!(p = _tcspbrk(p, _T("\r\n\t"))))
					{
						break;
					}
					*p++ = ' ';
				}
				Title.ReleaseBuffer();
			}
			int GroupOrder = TreeCtrl->IDToOrder(RecentGroupID);
			TreeCtrl->Value.InsertElem(CTreeItem(Title, RecentGroupID, TreeCtrl->GenerateID(), 0, Message), (GroupOrder >= 0) ? (GroupOrder + 1) : 0);
			TreeCtrl->SetModified(true);
		}
	// now clean up here
		int MRMNum = 0;
		int MaxMRMNum = g_MoreOptPage.GetDBValueCopy(IDC_MOREOPTDLG_RECENTMSGSCOUNT);
		for (I = 0; I < TreeCtrl->Value.GetSize(); I++)
		{
			if (TreeCtrl->Value[I].ParentID == RecentGroupID)
			{ // found a child of our group
				if (TreeCtrl->Value[I].Flags & TIF_GROUP || ++MRMNum > MaxMRMNum) // what groups are doing here?! :))
				{
					TreeCtrl->Value.RemoveElem(I);
					TreeCtrl->SetModified(true);
					I--;
				}
			}
		}
		if (g_MoreOptPage.GetDBValueCopy(IDC_MOREOPTDLG_PERSTATUSMRM))
		{ // if we're saving recent messages per status, then remove any messages that were left at the recent messages' root
			for (I = 0; I < TreeCtrl->Value.GetSize(); I++)
			{
				if (TreeCtrl->Value[I].ParentID == g_Messages_RecentRootID)
				{
					if (!(TreeCtrl->Value[I].Flags & TIF_GROUP))
					{
						TreeCtrl->Value.RemoveElem(I);
						TreeCtrl->SetModified(true);
						I--;
					}
				}
			}
		}
		TreeCtrl->MemToDB(CString(MOD_NAME));
	}
}


TCString CProtoSettings::GetMsgFormat(int Flags, int *pOrder)
// returns the requested message; sets Order to the order of the message in the message tree, if available; or to -1 otherwise.
{
	TCString Message = NULL;
	if (pOrder)
	{
		*pOrder = -1;
	}
	if (Flags & GMF_TEMPORARY)
	{
		_ASSERT(!Status || Status == g_ProtoStates[szProto].Status);
		if (g_ProtoStates[szProto].TempMsg.IsSet())
		{
			Message = g_ProtoStates[szProto].TempMsg;
			Flags &= ~GMF_PERSONAL; // don't allow personal message to overwrite our NULL temporary message
		}
	}
	if (Flags & GMF_PERSONAL && Message == NULL)
	{ // try getting personal message (it overrides global)
		Message = DBGetContactSettingString(NULL, MOD_NAME, ProtoStatusToDBSetting(DB_STATUSMSG, IDC_MOREOPTDLG_PERSTATUSPROTOMSGS), (TCHAR*)NULL);
	}
	if (Flags & GMF_PROTOORGLOBAL && Message == NULL)
	{
		Message = CProtoSettings().GetMsgFormat(GMF_PERSONAL | (Flags & GMF_TEMPORARY), pOrder);
		return (Message == NULL) ? _T("") : Message; // global message can't be NULL
	}
	if (Flags & GMF_LASTORDEFAULT && Message == NULL)
	{ // try to get the last or default message, depending on current settings
		COptPage MsgTreeData(g_MsgTreePage);
		COptItem_TreeCtrl *TreeCtrl = (COptItem_TreeCtrl*)MsgTreeData.Find(IDV_MSGTREE);
		TreeCtrl->DBToMem(CString(MOD_NAME));
		Message = NULL;
		if (g_MoreOptPage.GetDBValueCopy(IDC_MOREOPTDLG_USELASTMSG)) // if using last message by default...
		{
			Message = DBGetContactSettingString(NULL, MOD_NAME, ProtoStatusToDBSetting(DB_STATUSMSG, IDC_MOREOPTDLG_PERSTATUSPROTOMSGS), (TCHAR*)NULL); // try per-protocol message first
			if (Message.IsEmpty())
			{
				Message = NULL; // to be sure it's NULL, not "" - as we're checking 'Message == NULL' later
				int RecentGroupID = GetRecentGroupID(Status);
				if (RecentGroupID != -1)
				{
					int I;
					for (I = 0; I < TreeCtrl->Value.GetSize(); I++) // find first message in the group
					{
						if (TreeCtrl->Value[I].ParentID == RecentGroupID && !(TreeCtrl->Value[I].Flags & TIF_GROUP))
						{
							Message = TreeCtrl->Value[I].User_Str1;
							if (pOrder)
							{
								*pOrder = I;
							}
							break;
						}
					}
				}
			}
		} // else, if using default message by default...
		if (Message == NULL) // ...or we didn't succeed retrieving the last message
		{ // get default message for this status
			int DefMsgID = -1;
			static struct {
				int DBSetting, Status;
			} DefMsgDlgItems[] = {
				IDS_MESSAGEDLG_DEF_ONL, ID_STATUS_ONLINE,
				IDS_MESSAGEDLG_DEF_AWAY, ID_STATUS_AWAY,
				IDS_MESSAGEDLG_DEF_NA, ID_STATUS_NA,
				IDS_MESSAGEDLG_DEF_OCC, ID_STATUS_OCCUPIED,
				IDS_MESSAGEDLG_DEF_DND, ID_STATUS_DND,
				IDS_MESSAGEDLG_DEF_FFC, ID_STATUS_FREECHAT,
				IDS_MESSAGEDLG_DEF_INV, ID_STATUS_INVISIBLE,
				IDS_MESSAGEDLG_DEF_OTP, ID_STATUS_ONTHEPHONE,
				IDS_MESSAGEDLG_DEF_OTL, ID_STATUS_OUTTOLUNCH
			};
			int I;
			for (I = 0; I < lengthof(DefMsgDlgItems); I++)
			{
				if (DefMsgDlgItems[I].Status == Status)
				{
					DefMsgID = MsgTreeData.GetDBValue(DefMsgDlgItems[I].DBSetting);
					break;
				}
			}
			if (DefMsgID == -1)
			{
				DefMsgID = MsgTreeData.GetDBValue(IDS_MESSAGEDLG_DEF_AWAY); // use away message for unknown statuses
			}
			int Order = TreeCtrl->IDToOrder(DefMsgID); // this will return -1 in any case if something goes wrong
			if (Order >= 0)
			{
				Message = TreeCtrl->Value[Order].User_Str1;
			}
			if (pOrder)
			{
				*pOrder = Order;
			}
		}
		if (Message == NULL)
		{
			Message = _T(""); // last or default message can't be NULL.. otherwise ICQ won't reply to status message requests and won't notify us of status message requests at all
		}
	}
	return Message;
}