diff options
author | Pranav Ganorkar <pranavg189@gmail.com> | 2016-07-23 19:34:00 +0530 |
---|---|---|
committer | Jonathan Kang <jonathan121537@gmail.com> | 2016-10-20 10:58:39 +0800 |
commit | 3f028d5430e06253fd66c4294a9652d02eb31a21 (patch) | |
tree | 6d98c23452a0a12fb3666110d5fd205022645fc0 /src/gl-journal-model.c | |
parent | b58ad1fcc1973da8869b39e28b65c751619d3036 (diff) | |
download | gnome-logs-3f028d5430e06253fd66c4294a9652d02eb31a21.tar.gz |
Add search type option to search popover
A radio button group was addded to select between "Exact"
and "Substring" search types.The default search type is
"Substring" search.
We hide the search type radio button group if "All Available Fields"
is selected as exact search doesn't make sense in that case.
The GlJournalModel class was modified to handle the case when all
search fields are exact and there are no substring matches.It was also
modified to handle the case when the exact search match string has a
null or empty value in which case we do not pass it to
gl_journal_set_matches().
GL_QUERY_* prefix was added to the enum nicks for making
it consistent with the enum name GlQuerySearchType.
https://bugzilla.gnome.org/show_bug.cgi?id=767996
Diffstat (limited to 'src/gl-journal-model.c')
-rw-r--r-- | src/gl-journal-model.c | 71 |
1 files changed, 51 insertions, 20 deletions
diff --git a/src/gl-journal-model.c b/src/gl-journal-model.c index c2f95d7..73d9f3a 100644 --- a/src/gl-journal-model.c +++ b/src/gl-journal-model.c @@ -245,6 +245,7 @@ gl_query_new (void) query = g_slice_new (GlQuery); query->queryitems = g_ptr_array_new_with_free_func ((GDestroyNotify) gl_query_item_free); + query->search_type = GL_QUERY_SEARCH_TYPE_SUBSTRING; return query; } @@ -265,6 +266,12 @@ gl_query_item_new (const gchar *field_name, return queryitem; } +void +gl_query_set_search_type (GlQuery *query, GlQuerySearchType search_type) +{ + query->search_type = search_type; +} + static gchar * gl_query_item_create_match_string (GlQueryItem *queryitem) { @@ -287,7 +294,7 @@ populate_exact_matches (GlQueryItem *queryitem, GPtrArray *matches) { gchar *match; - if (queryitem->search_type == SEARCH_TYPE_EXACT) + if (queryitem->search_type == GL_QUERY_SEARCH_TYPE_EXACT) { match = gl_query_item_create_match_string (queryitem); @@ -311,7 +318,7 @@ gl_query_get_exact_matches (GlQuery *query) static void populate_substring_matches (GlQueryItem *queryitem, GPtrArray *matches) { - if (queryitem->search_type == SEARCH_TYPE_SUBSTRING) + if (queryitem->search_type == GL_QUERY_SEARCH_TYPE_SUBSTRING) { g_ptr_array_add (matches, queryitem); } @@ -339,6 +346,22 @@ gl_journal_model_process_query (GlJournalModel *model) /* Set the exact matches first */ category_matches = gl_query_get_exact_matches (model->query); + /* Get the search string of the exact match field */ + if (model->query->search_type == GL_QUERY_SEARCH_TYPE_EXACT) + { + gchar *search_match; + gchar *field_value_pos; + + /* Get the search match string */ + search_match = g_ptr_array_index (category_matches, category_matches->len - 1); + + field_value_pos = strchr (search_match, '='); + + /* If it has invalid string value remove it from the matches */ + if (!field_value_pos || !*(field_value_pos + 1)) + g_ptr_array_remove (category_matches, search_match); + } + gl_journal_set_matches (model->journal, category_matches); /* Start re-population of the journal */ @@ -742,7 +765,7 @@ calculate_match (GlJournalEntry *entry, /* check for matches */ - token_match = gl_query_item_new (field_name, field_value, SEARCH_TYPE_SUBSTRING); + token_match = gl_query_item_new (field_name, field_value, GL_QUERY_SEARCH_TYPE_SUBSTRING); field_name_case = is_string_case_sensitive (field_name); field_value_case = is_string_case_sensitive (field_value); @@ -843,28 +866,36 @@ search_in_entry (GlJournalEntry *entry, search_matches = gl_query_get_substring_matches (query); - /* Get search text from a search match */ - search_match = g_ptr_array_index (search_matches, 0); - - /* check for null and empty strings */ - if (!search_match->field_value || !*(search_match->field_value)) + /* Check if there is atleast one substring queryitem */ + if (search_matches->len) { - matches = TRUE; - } - else - { - gchar *search_text; + /* Get search text from a search match */ + search_match = g_ptr_array_index (search_matches, 0); + + /* check for null and empty strings */ + if (!search_match->field_value || !*(search_match->field_value)) + { + matches = TRUE; + } + else + { + gchar *search_text; - search_text = search_match->field_value; + search_text = search_match->field_value; - /* Tokenize the entered text */ - token_array = tokenize_search_string (search_text); + /* Tokenize the entered text */ + token_array = tokenize_search_string (search_text); - /* calculate match depending on the number of tokens */ - matches = calculate_match (entry, token_array, search_matches); + /* calculate match depending on the number of tokens */ + matches = calculate_match (entry, token_array, search_matches); - /* Free variables */ - g_ptr_array_free (token_array, TRUE); + /* Free variables */ + g_ptr_array_free (token_array, TRUE); + } + } + else + { + matches = TRUE; } g_ptr_array_free (search_matches, TRUE); |