2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2012 the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 # include "claws-features.h"
25 #include "advsearch.h"
31 #include "matcher_parser.h"
34 struct _AdvancedSearch {
36 AdvancedSearchType type;
40 MatcherList *predicate;
42 gboolean search_aborted;
45 gboolean (*cb)(gpointer data, guint at, guint matched, guint total);
49 void (*cb)(gpointer data);
54 void advsearch_set_on_progress_cb(AdvancedSearch *search, gboolean (*cb)(gpointer, guint, guint, guint), gpointer data)
56 search->on_progress_cb.cb = cb;
57 search->on_progress_cb.data = data;
60 void advsearch_set_on_error_cb(AdvancedSearch* search, void (*cb)(gpointer data), gpointer data)
62 search->on_error_cb.cb = cb;
63 search->on_error_cb.data = data;
66 static void prepare_matcher(AdvancedSearch *search);
67 static gboolean search_impl(MsgInfoList **messages, AdvancedSearch* search,
68 FolderItem* folderItem, gboolean recursive);
70 // --------------------------
72 AdvancedSearch* advsearch_new()
74 AdvancedSearch *result;
76 result = g_new0(AdvancedSearch, 1);
81 void advsearch_free(AdvancedSearch *search)
83 if (search->predicate != NULL)
84 matcherlist_free(search->predicate);
86 g_free(search->request.matchstring);
90 void advsearch_set(AdvancedSearch *search, AdvancedSearchType type, const gchar *matchstring)
92 cm_return_if_fail(search != NULL);
94 search->request.type = type;
96 g_free(search->request.matchstring);
97 search->request.matchstring = g_strdup(matchstring);
99 prepare_matcher(search);
102 gboolean advsearch_is_fast(AdvancedSearch *search)
104 cm_return_val_if_fail(search != NULL, FALSE);
106 return search->is_fast;
109 gboolean advsearch_has_proper_predicate(AdvancedSearch *search)
111 cm_return_val_if_fail(search != NULL, FALSE);
113 return search->predicate != NULL;
116 gboolean advsearch_search_msgs_in_folders(AdvancedSearch* search, MsgInfoList **messages,
117 FolderItem* folderItem, gboolean recursive)
119 if (search == NULL || search->predicate == NULL)
122 search->search_aborted = FALSE;
123 return search_impl(messages, search, folderItem, recursive);
126 void advsearch_abort(AdvancedSearch *search)
128 search->search_aborted = TRUE;
131 gchar *advsearch_expand_search_string(const gchar *search_string)
134 gchar term_char, save_char;
135 gchar *cmd_start, *cmd_end;
137 gchar *returnstr = NULL;
139 gboolean casesens, dontmatch, regex;
140 /* list of allowed pattern abbreviations */
142 gchar *abbreviated; /* abbreviation */
143 gchar *command; /* actual matcher command */
144 gint numparams; /* number of params for cmd */
145 gboolean qualifier; /* do we append stringmatch operations */
146 gboolean quotes; /* do we need quotes */
149 { "a", "all", 0, FALSE, FALSE },
150 { "ag", "age_greater", 1, FALSE, FALSE },
151 { "al", "age_lower", 1, FALSE, FALSE },
152 { "b", "body_part", 1, TRUE, TRUE },
153 { "B", "message", 1, TRUE, TRUE },
154 { "c", "cc", 1, TRUE, TRUE },
155 { "C", "to_or_cc", 1, TRUE, TRUE },
156 { "D", "deleted", 0, FALSE, FALSE },
157 { "e", "header \"Sender\"", 1, TRUE, TRUE },
158 { "E", "execute", 1, FALSE, TRUE },
159 { "f", "from", 1, TRUE, TRUE },
160 { "F", "forwarded", 0, FALSE, FALSE },
161 { "h", "headers_part", 1, TRUE, TRUE },
162 { "ha", "has_attachments", 0, FALSE, FALSE },
163 { "i", "header \"Message-ID\"", 1, TRUE, TRUE },
164 { "I", "inreplyto", 1, TRUE, TRUE },
165 { "k", "colorlabel", 1, FALSE, FALSE },
166 { "L", "locked", 0, FALSE, FALSE },
167 { "n", "newsgroups", 1, TRUE, TRUE },
168 { "N", "new", 0, FALSE, FALSE },
169 { "O", "~new", 0, FALSE, FALSE },
170 { "r", "replied", 0, FALSE, FALSE },
171 { "R", "~unread", 0, FALSE, FALSE },
172 { "s", "subject", 1, TRUE, TRUE },
173 { "se", "score_equal", 1, FALSE, FALSE },
174 { "sg", "score_greater", 1, FALSE, FALSE },
175 { "sl", "score_lower", 1, FALSE, FALSE },
176 { "Se", "size_equal", 1, FALSE, FALSE },
177 { "Sg", "size_greater", 1, FALSE, FALSE },
178 { "Ss", "size_smaller", 1, FALSE, FALSE },
179 { "t", "to", 1, TRUE, TRUE },
180 { "tg", "tag", 1, TRUE, TRUE },
181 { "T", "marked", 0, FALSE, FALSE },
182 { "U", "unread", 0, FALSE, FALSE },
183 { "x", "header \"References\"", 1, TRUE, TRUE },
184 { "X", "test", 1, FALSE, FALSE },
185 { "y", "header \"X-Label\"", 1, TRUE, TRUE },
186 { "&", "&", 0, FALSE, FALSE },
187 { "|", "|", 0, FALSE, FALSE },
188 { "p", "partial", 0, FALSE, FALSE },
189 { NULL, NULL, 0, FALSE, FALSE }
192 if (search_string == NULL)
195 copy_str = g_strdup(search_string);
197 matcherstr = g_string_sized_new(16);
198 cmd_start = copy_str;
199 while (cmd_start && *cmd_start) {
200 /* skip all white spaces */
201 while (*cmd_start && isspace((guchar)*cmd_start))
205 /* extract a command */
206 while (*cmd_end && !isspace((guchar)*cmd_end))
210 save_char = *cmd_end;
217 /* ~ and ! mean logical NOT */
218 if (*cmd_start == '~' || *cmd_start == '!')
223 /* % means case sensitive match */
224 if (*cmd_start == '%')
229 /* # means regex match */
230 if (*cmd_start == '#') {
235 /* find matching abbreviation */
236 for (i = 0; cmds[i].command; i++) {
237 if (!strcmp(cmd_start, cmds[i].abbreviated)) {
238 /* restore character */
239 *cmd_end = save_char;
242 if (matcherstr->len > 0) {
243 g_string_append(matcherstr, " ");
246 g_string_append(matcherstr, "~");
247 g_string_append(matcherstr, cmds[i].command);
248 g_string_append(matcherstr, " ");
250 /* stop if no params required */
251 if (cmds[i].numparams == 0)
254 /* extract a parameter, allow quotes */
255 while (*cmd_end && isspace((guchar)*cmd_end))
259 if (*cmd_start == '"') {
266 /* extract actual parameter */
267 while ((*cmd_end) && (*cmd_end != term_char))
273 save_char = *cmd_end;
276 if (cmds[i].qualifier) {
278 g_string_append(matcherstr, regex ? "regexp " : "match ");
280 g_string_append(matcherstr, regex ? "regexpcase " : "matchcase ");
283 /* do we need to add quotes ? */
284 if (cmds[i].quotes && term_char != '"')
285 g_string_append(matcherstr, "\"");
287 /* copy actual parameter */
288 g_string_append(matcherstr, cmd_start);
290 /* do we need to add quotes ? */
291 if (cmds[i].quotes && term_char != '"')
292 g_string_append(matcherstr, "\"");
294 /* restore original character */
295 *cmd_end = save_char;
308 /* return search string if no match is found to allow
309 all available filtering expressions in advanced search */
310 if (matcherstr->len > 0) returnstr = matcherstr->str;
311 else returnstr = g_strdup(search_string);
312 g_string_free(matcherstr, FALSE);
316 // --------------------------
318 static gchar *expand_tag_search_string(const gchar *search_string)
320 gchar *newstr = NULL;
321 gchar **words = search_string ? g_strsplit(search_string, " ", -1):NULL;
323 while (words && words[i] && *words[i]) {
324 g_strstrip(words[i]);
326 newstr = g_strdup_printf("tag matchcase \"%s\"", words[i]);
328 gint o_len = strlen(newstr);
329 gint s_len = 17; /* strlen("|tag matchcase \"\"") */
330 gint n_len = s_len + strlen(words[i]);
331 newstr = g_realloc(newstr, o_len + n_len + 1);
332 strcpy(newstr + o_len, "|tag matchcase \"");
333 strcpy(newstr + o_len + (s_len - 1), words[i]);
334 strcpy(newstr + o_len + (n_len - 1), "\"");
342 static void prepare_matcher_extended(AdvancedSearch *search)
344 gchar *newstr = advsearch_expand_search_string(search->request.matchstring);
346 if (newstr && newstr[0] != '\0') {
347 search->predicate = matcher_parser_get_cond(newstr, &search->is_fast);
352 static void prepare_matcher_tag(AdvancedSearch *search)
354 char *newstr = expand_tag_search_string(search->request.matchstring);
355 search->predicate = matcher_parser_get_cond(newstr, &search->is_fast);
359 static void prepare_matcher_header(AdvancedSearch *search, gint match_header)
361 MatcherProp *matcher;
363 if (search->predicate == NULL)
364 search->predicate = g_new0(MatcherList, 1);
366 matcher = matcherprop_new(match_header, NULL, MATCHTYPE_MATCHCASE,
367 search->request.matchstring, 0);
369 search->predicate->matchers = g_slist_prepend(search->predicate->matchers, matcher);
372 static void prepare_matcher_mixed(AdvancedSearch *search)
374 prepare_matcher_tag(search);
376 prepare_matcher_header(search, MATCHCRITERIA_SUBJECT);
377 prepare_matcher_header(search, MATCHCRITERIA_FROM);
378 prepare_matcher_header(search, MATCHCRITERIA_TO);
379 prepare_matcher_header(search, MATCHCRITERIA_TAG);
382 static void prepare_matcher(AdvancedSearch *search)
384 const gchar *search_string;
386 cm_return_if_fail(search != NULL);
388 if (search->predicate) {
389 matcherlist_free(search->predicate);
390 search->predicate = NULL;
393 search_string = search->request.matchstring;
395 if (search_string == NULL || search_string[0] == '\0')
398 switch (search->request.type) {
399 case ADVANCED_SEARCH_SUBJECT:
400 prepare_matcher_header(search, MATCHCRITERIA_SUBJECT);
403 case ADVANCED_SEARCH_FROM:
404 prepare_matcher_header(search, MATCHCRITERIA_FROM);
407 case ADVANCED_SEARCH_TO:
408 prepare_matcher_header(search, MATCHCRITERIA_TO);
411 case ADVANCED_SEARCH_TAG:
412 prepare_matcher_header(search, MATCHCRITERIA_TAG);
415 case ADVANCED_SEARCH_MIXED:
416 prepare_matcher_mixed(search);
419 case ADVANCED_SEARCH_EXTENDED:
420 prepare_matcher_extended(search);
424 debug_print("unknown search type (%d)\n", search->request.type);
429 static gboolean search_progress_notify_cb(gpointer data, gboolean on_server, guint at,
430 guint matched, guint total)
432 AdvancedSearch *search = (AdvancedSearch*) data;
434 if (search->search_aborted)
437 if (on_server || search->on_progress_cb.cb == NULL)
440 return search->on_progress_cb.cb(search->on_progress_cb.data, at, matched, total);
443 static gboolean search_filter_folder(MsgNumberList **msgnums, AdvancedSearch *search,
444 FolderItem *folderItem, gboolean onServer)
447 gboolean tried_server = onServer;
449 matched = folder_item_search_msgs(folderItem->folder,
454 search_progress_notify_cb,
458 if (search->on_error_cb.cb != NULL)
459 search->on_error_cb.cb(search->on_error_cb.data);
463 if (folderItem->folder->klass->supports_server_search && tried_server && !onServer) {
464 return search_filter_folder(msgnums, search, folderItem, onServer);
470 static gboolean search_impl(MsgInfoList **messages, AdvancedSearch* search,
471 FolderItem* folderItem, gboolean recursive)
474 if (!search_impl(messages, search, folderItem, FALSE))
477 if (folderItem->node->children != NULL && !search->search_aborted) {
479 for (node = folderItem->node->children; node != NULL; node = node->next) {
480 FolderItem *cur = FOLDER_ITEM(node->data);
481 debug_print("in: %s\n", cur->path);
482 if (!search_impl(messages, search, cur, TRUE))
486 } else if (!folderItem->no_select) {
487 MsgNumberList *msgnums = NULL;
489 MsgInfoList *msgs = NULL;
491 if (!search_filter_folder(&msgnums, search, folderItem,
492 folderItem->folder->klass->supports_server_search)) {
493 g_slist_free(msgnums);
497 for (cur = msgnums; cur != NULL; cur = cur->next) {
498 MsgInfo *msg = folder_item_get_msginfo(folderItem, GPOINTER_TO_UINT(cur->data));
500 msgs = g_slist_prepend(msgs, msg);
503 while (msgs != NULL) {
504 MsgInfoList *front = msgs;
508 front->next = *messages;
512 g_slist_free(msgnums);