summaryrefslogtreecommitdiff
path: root/plugins/StatusPlugins/AdvancedAutoAway/msgoptions.cpp
blob: 1954ccb49f2d5895c81d57aff8341558f1187259 (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
/*
    AdvancedAutoAway Plugin for Miranda-IM (www.miranda-im.org)
    Copyright 2003-2006 P. Boon

    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 "../commonstatus.h"
#include "advancedautoaway.h"
#include "../resource.h"

extern char *StatusModeToDbSetting(int status,const char *suffix);

void DisableDialog(HWND hwndDlg)
{
	EnableWindow(GetDlgItem(hwndDlg, IDC_STATUS), FALSE);
	EnableWindow(GetDlgItem(hwndDlg, IDC_STATUSMSG), FALSE);
	EnableWindow(GetDlgItem(hwndDlg, IDC_VARIABLESHELP), FALSE);
	EnableWindow(GetDlgItem(hwndDlg, IDC_RADUSECUSTOM), FALSE);
	EnableWindow(GetDlgItem(hwndDlg, IDC_RADUSEMIRANDA), FALSE);
}

INT_PTR CALLBACK DlgProcAutoAwayMsgOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static AAMSGSETTING** settings;
	static int last, count;

	switch( msg ) {
	case WM_INITDIALOG:
		TranslateDialogDefault( hwndDlg );
		{
			ShowWindow(GetDlgItem(hwndDlg, IDC_VARIABLESHELP), ServiceExists(MS_VARS_SHOWHELP)?SW_SHOW:SW_HIDE);
			count = 0;
			last = -1;
		
			PROTOACCOUNT** proto;
			int protoCount = 0;
			ProtoEnumAccounts(&protoCount, &proto);
			if (protoCount <= 0)
			{
				DisableDialog(hwndDlg);
				break;
			}

			DWORD protoModeMsgFlags = 0;
			for (int i=0; i < protoCount; i++)
				if (CallProtoService(proto[i]->szModuleName, PS_GETCAPS, PFLAGNUM_1, 0) & PF1_MODEMSGSEND & ~PF1_INDIVMODEMSG)
					protoModeMsgFlags |= CallProtoService( proto[i]->szModuleName, PS_GETCAPS, PFLAGNUM_3, 0 );

			if (protoModeMsgFlags == 0)
			{
				DisableDialog(hwndDlg);
				break;
			}

			settings = ( AAMSGSETTING** )malloc(sizeof(AAMSGSETTING*));
			count = 0;
			for (int i=0; i < SIZEOF(statusModeList); i++ ) {
				if ( !( protoModeMsgFlags & Proto_Status2Flag( statusModeList[i] )))
					continue;

				int j = SendDlgItemMessage(hwndDlg, IDC_STATUS, CB_ADDSTRING, 0, (LPARAM)pcli->pfnGetStatusModeDescription(statusModeList[i], 0));
				SendDlgItemMessage( hwndDlg, IDC_STATUS, CB_SETITEMDATA, j, statusModeList[i] );
				settings = ( AAMSGSETTING** )realloc(settings, (count+1)*sizeof(AAMSGSETTING*));
				settings[count] = ( AAMSGSETTING* )malloc(sizeof(AAMSGSETTING));
				settings[count]->status = statusModeList[i];

				DBVARIANT dbv;
				if ( !db_get(NULL, MODULENAME, StatusModeToDbSetting(statusModeList[i],SETTING_STATUSMSG), &dbv)) {
					settings[count]->msg = ( char* )malloc(strlen(dbv.pszVal) + 1);
					strcpy(settings[count]->msg, dbv.pszVal);
					db_free(&dbv);
				}
				else settings[count]->msg = NULL;

				settings[count]->useCustom = db_get_b(NULL, MODULENAME, StatusModeToDbSetting(statusModeList[i], SETTING_MSGCUSTOM), FALSE);
				count += 1;
			}
			SendDlgItemMessage(hwndDlg,IDC_STATUS,CB_SETCURSEL,0,0);
			SendMessage(hwndDlg,WM_COMMAND,MAKEWPARAM(IDC_STATUS,CBN_SELCHANGE),0);
		}
		break;

	case WM_COMMAND:
		if ( ((HIWORD(wParam) == EN_CHANGE) || (HIWORD(wParam) == BN_CLICKED)) && ((HWND)lParam == GetFocus()))
			SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
		
		switch(LOWORD(wParam)) {
		case IDC_RADUSEMIRANDA:
			CheckDlgButton(hwndDlg, IDC_RADUSECUSTOM, !IsDlgButtonChecked(hwndDlg, IDC_RADUSEMIRANDA));
			EnableWindow(GetDlgItem(hwndDlg, IDC_STATUSMSG), IsDlgButtonChecked(hwndDlg, IDC_RADUSECUSTOM));
			EnableWindow(GetDlgItem(hwndDlg, IDC_VARIABLESHELP), IsDlgButtonChecked(hwndDlg, IDC_RADUSECUSTOM));
			settings[SendDlgItemMessage(hwndDlg,IDC_STATUS,CB_GETCURSEL,0,0)]->useCustom = IsDlgButtonChecked(hwndDlg, IDC_RADUSECUSTOM);
			break;

		case IDC_RADUSECUSTOM:
			CheckDlgButton(hwndDlg, IDC_RADUSEMIRANDA, !IsDlgButtonChecked(hwndDlg, IDC_RADUSECUSTOM));
			EnableWindow(GetDlgItem(hwndDlg, IDC_STATUSMSG), IsDlgButtonChecked(hwndDlg, IDC_RADUSECUSTOM));
			EnableWindow(GetDlgItem(hwndDlg, IDC_VARIABLESHELP), IsDlgButtonChecked(hwndDlg, IDC_RADUSECUSTOM));
			settings[SendDlgItemMessage(hwndDlg,IDC_STATUS,CB_GETCURSEL,0,0)]->useCustom = IsDlgButtonChecked(hwndDlg, IDC_RADUSECUSTOM);
			break;

		case IDC_STATUS:
			if ( HIWORD( wParam ) == CBN_SELCHANGE ) {
				int i = SendDlgItemMessage(hwndDlg,IDC_STATUS,CB_GETCURSEL,0,0);
				int len = SendDlgItemMessage(hwndDlg, IDC_STATUSMSG, WM_GETTEXTLENGTH, 0, 0);
				if ( last != -1 ) {
					if (settings[last]->msg == NULL)
						settings[last]->msg = ( char* )malloc(len+1);
					else
						settings[last]->msg = ( char* )realloc(settings[last]->msg, len+1);
					SendDlgItemMessageA(hwndDlg, IDC_STATUSMSG, WM_GETTEXT, (LPARAM)(len+1), (WPARAM)settings[last]->msg);
				}
				if (i != -1) {
					if (settings[i]->msg != NULL)
						SetDlgItemTextA(hwndDlg, IDC_STATUSMSG, settings[i]->msg);
					else if (ServiceExists(MS_AWAYMSG_GETSTATUSMSGT)) {
						TCHAR *msg = (TCHAR*)CallService(MS_AWAYMSG_GETSTATUSMSGT, (WPARAM)settings[i]->status, 0);
						if (msg != NULL) {
							SetDlgItemText(hwndDlg, IDC_STATUSMSG, msg);
							mir_free(msg);
						}
					}
					else if (ServiceExists(MS_AWAYMSG_GETSTATUSMSG)) {
						char *msg = (char*)CallService(MS_AWAYMSG_GETSTATUSMSG, (WPARAM)settings[i]->status, 0);
						if (msg != NULL) {
							SetDlgItemTextA(hwndDlg, IDC_STATUSMSG, msg);
							mir_free(msg);
						}
					}
					if (settings[i]->useCustom) {
						EnableWindow(GetDlgItem(hwndDlg, IDC_STATUSMSG), TRUE);
						EnableWindow(GetDlgItem(hwndDlg, IDC_VARIABLESHELP), TRUE);
						CheckDlgButton(hwndDlg, IDC_RADUSECUSTOM, TRUE);
						CheckDlgButton(hwndDlg, IDC_RADUSEMIRANDA, FALSE);
					}
					else {
						EnableWindow(GetDlgItem(hwndDlg, IDC_STATUSMSG), FALSE);
						EnableWindow(GetDlgItem(hwndDlg, IDC_VARIABLESHELP), FALSE);
						CheckDlgButton(hwndDlg, IDC_RADUSEMIRANDA, TRUE);
						CheckDlgButton(hwndDlg, IDC_RADUSECUSTOM, FALSE);
				}	}
				last = i;
			}
			break;

		case IDC_VARIABLESHELP:
			CallService(MS_VARS_SHOWHELP, (WPARAM)GetDlgItem(hwndDlg, IDC_STATUSMSG), 0);
			break;
		}
		break;

	case WM_NOTIFY:
		switch (((LPNMHDR)lParam)->code) {
		case PSN_APPLY:
			SendMessage(hwndDlg,WM_COMMAND,MAKEWPARAM(IDC_STATUS,CBN_SELCHANGE),0);
			for (int i=0; i < count; i++ ) {
				db_set_b(NULL, MODULENAME, StatusModeToDbSetting(settings[i]->status,SETTING_MSGCUSTOM), (BYTE)settings[i]->useCustom);
				if ( (settings[i]->useCustom) && (settings[i]->msg != NULL) && (settings[i]->msg[0] != '\0'))
					db_set_s(NULL, MODULENAME, StatusModeToDbSetting(settings[i]->status,SETTING_STATUSMSG), settings[i]->msg);
			}
			break;
		}
		break;
	
	case WM_DESTROY:
		for (int i=0; i < count; i++ ) {
			if (settings[i]->msg != NULL)
				free(settings[i]->msg);
			free(settings[i]);
		}
		free(settings);
		break;
	}
	
	return FALSE;
}