appDisplay: Use the desktop file index for app searching

Rather than scanning all apps for searching, use Ryan's new desktop
file index and the glib support APIs for app searching instead of our
own system.

https://bugzilla.gnome.org/show_bug.cgi?id=711631
This commit is contained in:
Jasper St. Pierre
2013-11-02 20:13:42 -04:00
parent 831bd07b0d
commit ba602c17d4
9 changed files with 26 additions and 422 deletions

View File

@ -81,12 +81,7 @@ struct _ShellApp
ShellAppRunningState *running_state;
char *window_id_string;
char *casefolded_name;
char *casefolded_generic_name;
char *name_collation_key;
char *casefolded_exec;
char **casefolded_keywords;
};
enum {
@ -1360,49 +1355,6 @@ trim_exec_line (const char *str)
return g_strndup (start, end - start);
}
static void
shell_app_init_search_data (ShellApp *app)
{
const char *name;
const char *generic_name;
const char *exec;
const char * const *keywords;
char *normalized_exec;
name = g_app_info_get_name (G_APP_INFO (app->info));
app->casefolded_name = shell_util_normalize_casefold_and_unaccent (name);
generic_name = g_desktop_app_info_get_generic_name (app->info);
if (generic_name)
app->casefolded_generic_name = shell_util_normalize_casefold_and_unaccent (generic_name);
else
app->casefolded_generic_name = NULL;
exec = g_app_info_get_executable (G_APP_INFO (app->info));
normalized_exec = shell_util_normalize_casefold_and_unaccent (exec);
app->casefolded_exec = trim_exec_line (normalized_exec);
g_free (normalized_exec);
keywords = g_desktop_app_info_get_keywords (app->info);
if (keywords)
{
int i;
app->casefolded_keywords = g_new0 (char*, g_strv_length ((char **)keywords) + 1);
i = 0;
while (keywords[i])
{
app->casefolded_keywords[i] = shell_util_normalize_casefold_and_unaccent (keywords[i]);
++i;
}
app->casefolded_keywords[i] = NULL;
}
else
app->casefolded_keywords = NULL;
}
/**
* shell_app_compare_by_name:
* @app: One app
@ -1419,116 +1371,6 @@ shell_app_compare_by_name (ShellApp *app, ShellApp *other)
return strcmp (app->name_collation_key, other->name_collation_key);
}
static ShellAppSearchMatch
_shell_app_match_search_terms (ShellApp *app,
GSList *terms)
{
GSList *iter;
ShellAppSearchMatch match;
if (G_UNLIKELY (!app->casefolded_name))
shell_app_init_search_data (app);
match = MATCH_NONE;
for (iter = terms; iter; iter = iter->next)
{
ShellAppSearchMatch current_match;
const char *term = iter->data;
const char *p;
current_match = MATCH_NONE;
p = strstr (app->casefolded_name, term);
if (p != NULL)
{
if (p == app->casefolded_name || *(p - 1) == ' ')
current_match = MATCH_PREFIX;
else
current_match = MATCH_SUBSTRING;
}
if (app->casefolded_generic_name)
{
p = strstr (app->casefolded_generic_name, term);
if (p != NULL)
{
if (p == app->casefolded_generic_name || *(p - 1) == ' ')
current_match = MATCH_PREFIX;
else if (current_match < MATCH_PREFIX)
current_match = MATCH_SUBSTRING;
}
}
if (app->casefolded_exec)
{
p = strstr (app->casefolded_exec, term);
if (p != NULL)
{
if (p == app->casefolded_exec || *(p - 1) == '-')
current_match = MATCH_PREFIX;
else if (current_match < MATCH_PREFIX)
current_match = MATCH_SUBSTRING;
}
}
if (app->casefolded_keywords)
{
int i = 0;
while (app->casefolded_keywords[i] && current_match < MATCH_PREFIX)
{
p = strstr (app->casefolded_keywords[i], term);
if (p != NULL)
{
if (p == app->casefolded_keywords[i])
current_match = MATCH_PREFIX;
else
current_match = MATCH_SUBSTRING;
}
++i;
}
}
if (current_match == MATCH_NONE)
return current_match;
if (current_match > match)
match = current_match;
}
return match;
}
void
_shell_app_do_match (ShellApp *app,
GSList *terms,
GSList **prefix_results,
GSList **substring_results)
{
ShellAppSearchMatch match;
g_assert (app != NULL);
/* Skip window-backed apps */
if (app->info == NULL)
return;
/* Skip not-visible apps */
if (!g_app_info_should_show (G_APP_INFO (app->info)))
return;
match = _shell_app_match_search_terms (app, terms);
switch (match)
{
case MATCH_NONE:
break;
case MATCH_PREFIX:
*prefix_results = g_slist_prepend (*prefix_results, app);
break;
case MATCH_SUBSTRING:
*substring_results = g_slist_prepend (*substring_results, app);
break;
}
}
static void
shell_app_init (ShellApp *self)
{
@ -1561,11 +1403,7 @@ shell_app_finalize (GObject *object)
g_free (app->window_id_string);
g_free (app->casefolded_name);
g_free (app->casefolded_generic_name);
g_free (app->name_collation_key);
g_free (app->casefolded_exec);
g_strfreev (app->casefolded_keywords);
G_OBJECT_CLASS(shell_app_parent_class)->finalize (object);
}