summaryrefslogtreecommitdiff
path: root/src/modules/options/filter.cpp
blob: 4d75ba55aa13ce0ddd69808a731c95fcb6a02335 (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
/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2009 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

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 "..\..\core\commonheaders.h"
#include "filter.h"

HANDLE hOptionsInitialize;

CPageList filterStrings(1);

void AddFilterString(const PageHash key, TCHAR *data)
{
	if (ContainsFilterString(key, data)) return;

	CPageKeywords * values = filterStrings[key];
	if (values == NULL) {
		values = new CPageKeywords(key);
		filterStrings.insert(values);
	}
	values->AddKeyWord(data);
}

void ClearFilterStrings()
{
    filterStrings.destroy();
}

BOOL ContainsFilterString(const PageHash key, TCHAR *data)
{
	CPageKeywords* values = filterStrings[key];
	return (values) ? values->ContainsString(data) : FALSE;	
}

void AddTreeViewNodes(HWND hWndDlg, PageHash key, HTREEITEM root)
{
	if (root) {
		TCHAR title[2048] = {0};
		
		TVITEM item = {0};
		item.mask = TVIF_TEXT;
		item.hItem = root;
		item.pszText = title;
		item.cchTextMax = SIZEOF(title);
		
		if (TreeView_GetItem(hWndDlg, &item))
			if (_tcslen(title) > 0)
				AddFilterString(key, title);

		HTREEITEM child = root;
		while (child) {
			child = TreeView_GetNextItem(hWndDlg, child, TVGN_CHILD);
			AddTreeViewNodes(hWndDlg, key, child);
		}
		
		AddTreeViewNodes(hWndDlg, key, TreeView_GetNextSibling(hWndDlg, root));
	}
}

void AddDialogString(HWND hWndDlg, const PageHash key)
{
	TCHAR title[2048];
	GetWindowText(hWndDlg, title, SIZEOF(title));
	if (_tcslen(title) > 0)
		AddFilterString(key, title);
	
	TCHAR szClass[64];
	GetClassName(hWndDlg, szClass, SIZEOF(szClass));
	
	if (lstrcmpi(szClass, _T("SysTreeView32")) == 0) {
		HTREEITEM hItem = TreeView_GetRoot(hWndDlg);
		AddTreeViewNodes(hWndDlg, key, hItem);
	}
	else {
		if (lstrcmpi(szClass, _T("listbox")) == 0) {
			if (GetWindowStyle(hWndDlg) & LBS_HASSTRINGS) {
				int count = ListBox_GetCount(hWndDlg);
				for (int i=0; i < count; i++) {
					title[0] = 0; //safety
					int res = ListBox_GetText(hWndDlg, i, title);
					if (res != LB_ERR) {
						title[SIZEOF(title) - 1] = 0;
						if (_tcslen(title) > 0)
							AddFilterString(key, title);
			}	}	}
		}
		else {
			if (lstrcmpi(szClass, _T("SysListView32")) == 0) {
				int count = ListView_GetItemCount(hWndDlg);
				for (int i=0; i < count; i++) {
					title[0] = 0; //safety
					ListView_GetItemText(hWndDlg, i, 0, title, SIZEOF(title));
					
					if (_tcslen(title) > 0)
						AddFilterString(key, title);
			}	}

			if (lstrcmpi(szClass, _T("combobox")) == 0) {
				if (GetWindowStyle(hWndDlg) & CBS_HASSTRINGS) { 
					int count = ComboBox_GetCount(hWndDlg);
					for (int i=0; i < count; i++) {
						title[0] = 0; //safety
						int res = ComboBox_GetLBText(hWndDlg, i, title);
						if (res != CB_ERR) {
							title[SIZEOF(title) - 1] = 0;
							
							if (_tcslen(title) > 0)
								AddFilterString(key, title);
}	}	}	}	}	}	}

static BOOL CALLBACK GetDialogStringsCallback(HWND hWnd, LPARAM lParam)
{
	AddDialogString(hWnd, lParam);
	
	return TRUE;
}

void GetDialogStrings(int enableKeywordFiltering, const PageHash key, TCHAR *pluginName, HWND hWnd, TCHAR *group, TCHAR *title, TCHAR *tab, TCHAR *name)
{
	AddFilterString(key, pluginName); //add the plugin name as keyword
	if (group) AddFilterString(key, group);
	if (title) AddFilterString(key, title);
	if (tab)   AddFilterString(key, tab);
	if (name)  AddFilterString(key, name);

	if ((enableKeywordFiltering) && (hWnd != 0)) {
		AddDialogString(hWnd, key);
		
		EnumChildWindows(hWnd, GetDialogStringsCallback, (LPARAM) key);
	}
}

static INT_PTR CALLBACK DlgProcOptSearch(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hWnd);
		
		CheckDlgButton(hWnd, IDC_ENABLE_KEYWORDFILTERING, DBGetContactSettingWord(NULL, "Options", "EnableKeywordFiltering", TRUE) ? BST_CHECKED : BST_UNCHECKED);
		return TRUE;
	
	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDC_ENABLE_KEYWORDFILTERING:
			SendMessage(GetParent(hWnd), PSM_CHANGED, 0, 0);
			break;
		}
		break;

	case WM_SETFOCUS:
		SetFocus( GetDlgItem(hWnd, IDC_ENABLE_KEYWORDFILTERING));
		break;

	case WM_NOTIFY:
		switch(((LPNMHDR)lParam)->idFrom) {
		case 0:
			switch (((LPNMHDR)lParam)->code) {
			case PSN_APPLY:
				DBWriteContactSettingWord(NULL, "Options", "EnableKeywordFiltering", IsDlgButtonChecked(hWnd, IDC_ENABLE_KEYWORDFILTERING));
				break;
			}
			break;
		}
		break;
	}
	
	return 0;
}

static int OnOptionsInitialise(WPARAM wParam, LPARAM)
{
	OPTIONSDIALOGPAGE odp = {0};

	odp.cbSize = sizeof(odp);
	odp.position = -190000000;
	odp.hInstance = hInst;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPT_KEYWORDFILTER);
	odp.pszTitle = LPGEN("Options search");
	odp.pszGroup = LPGEN("Customize");
	odp.groupPosition = 810000000;
	odp.flags = ODPF_BOLDGROUPS;
	odp.pfnDlgProc = DlgProcOptSearch;
	Options_AddPage(wParam, &odp);
	return 0;
}

int HookFilterEvents()
{
	hOptionsInitialize = HookEvent(ME_OPT_INITIALISE, OnOptionsInitialise);
	return 0;
}

int UnhookFilterEvents()
{
	UnhookEvent(hOptionsInitialize);
	return 0;
}