2007-05-02 22:51:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 Novell, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2007-05-02 22:51:43 +00:00
|
|
|
*
|
|
|
|
* Author: Anders Carlsson <andersca@imendio.com>
|
|
|
|
*
|
|
|
|
* Based on nautilus-query.c
|
|
|
|
*/
|
|
|
|
|
2008-06-22 14:28:52 +00:00
|
|
|
#include "config.h"
|
2007-05-02 22:51:43 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "gtkquery.h"
|
|
|
|
|
2015-05-16 04:37:07 +00:00
|
|
|
struct _GtkQueryPrivate
|
2007-05-02 22:51:43 +00:00
|
|
|
{
|
2020-07-24 18:40:36 +00:00
|
|
|
char *text;
|
2015-07-24 19:29:51 +00:00
|
|
|
GFile *location;
|
2007-05-02 22:51:43 +00:00
|
|
|
GList *mime_types;
|
2020-07-24 18:40:36 +00:00
|
|
|
char **words;
|
2007-05-02 22:51:43 +00:00
|
|
|
};
|
|
|
|
|
2015-05-16 04:45:25 +00:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GtkQuery, gtk_query, G_TYPE_OBJECT)
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
finalize (GObject *object)
|
|
|
|
{
|
2019-05-15 15:53:18 +00:00
|
|
|
GtkQuery *query = GTK_QUERY (object);
|
|
|
|
GtkQueryPrivate *priv = gtk_query_get_instance_private (query);
|
|
|
|
|
|
|
|
g_clear_object (&priv->location);
|
|
|
|
g_free (priv->text);
|
|
|
|
g_strfreev (priv->words);
|
2007-05-02 22:51:43 +00:00
|
|
|
|
2015-05-16 04:45:25 +00:00
|
|
|
G_OBJECT_CLASS (gtk_query_parent_class)->finalize (object);
|
2007-05-02 22:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-05-16 04:45:25 +00:00
|
|
|
gtk_query_class_init (GtkQueryClass *class)
|
2007-05-02 22:51:43 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2015-05-16 04:45:25 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
gobject_class = G_OBJECT_CLASS (class);
|
|
|
|
gobject_class->finalize = finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-05-16 04:45:25 +00:00
|
|
|
gtk_query_init (GtkQuery *query)
|
2007-05-02 22:51:43 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkQuery *
|
2015-05-16 04:45:25 +00:00
|
|
|
gtk_query_new (void)
|
2007-05-02 22:51:43 +00:00
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_QUERY, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *
|
2015-05-16 04:45:25 +00:00
|
|
|
gtk_query_get_text (GtkQuery *query)
|
2007-05-02 22:51:43 +00:00
|
|
|
{
|
2019-05-15 15:53:18 +00:00
|
|
|
GtkQueryPrivate *priv = gtk_query_get_instance_private (query);
|
|
|
|
|
|
|
|
return priv->text;
|
2007-05-02 22:51:43 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 04:45:25 +00:00
|
|
|
void
|
|
|
|
gtk_query_set_text (GtkQuery *query,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *text)
|
2007-05-02 22:51:43 +00:00
|
|
|
{
|
2019-05-15 15:53:18 +00:00
|
|
|
GtkQueryPrivate *priv = gtk_query_get_instance_private (query);
|
|
|
|
|
|
|
|
g_free (priv->text);
|
|
|
|
priv->text = g_strdup (text);
|
2015-05-16 04:37:07 +00:00
|
|
|
|
2019-05-15 15:53:18 +00:00
|
|
|
g_strfreev (priv->words);
|
|
|
|
priv->words = NULL;
|
2007-05-02 22:51:43 +00:00
|
|
|
}
|
|
|
|
|
2015-07-24 19:29:51 +00:00
|
|
|
GFile *
|
2015-05-16 04:45:25 +00:00
|
|
|
gtk_query_get_location (GtkQuery *query)
|
2007-05-02 22:51:43 +00:00
|
|
|
{
|
2019-05-15 15:53:18 +00:00
|
|
|
GtkQueryPrivate *priv = gtk_query_get_instance_private (query);
|
|
|
|
|
|
|
|
return priv->location;
|
2007-05-02 22:51:43 +00:00
|
|
|
}
|
2015-05-16 04:45:25 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
void
|
2015-07-24 19:29:51 +00:00
|
|
|
gtk_query_set_location (GtkQuery *query,
|
|
|
|
GFile *file)
|
2007-05-02 22:51:43 +00:00
|
|
|
{
|
2019-05-15 15:53:18 +00:00
|
|
|
GtkQueryPrivate *priv = gtk_query_get_instance_private (query);
|
|
|
|
|
|
|
|
g_set_object (&priv->location, file);
|
2007-05-02 22:51:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
static char *
|
|
|
|
prepare_string_for_compare (const char *string)
|
2015-05-16 04:37:07 +00:00
|
|
|
{
|
2020-07-24 18:40:36 +00:00
|
|
|
char *normalized, *res;
|
2015-05-16 04:37:07 +00:00
|
|
|
|
|
|
|
normalized = g_utf8_normalize (string, -1, G_NORMALIZE_NFD);
|
|
|
|
res = g_utf8_strdown (normalized, -1);
|
|
|
|
g_free (normalized);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gtk_query_matches_string (GtkQuery *query,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *string)
|
2015-05-16 04:37:07 +00:00
|
|
|
{
|
2019-05-15 15:53:18 +00:00
|
|
|
GtkQueryPrivate *priv = gtk_query_get_instance_private (query);
|
2020-07-24 18:40:36 +00:00
|
|
|
char *prepared;
|
2015-05-16 04:37:07 +00:00
|
|
|
gboolean found;
|
2020-07-24 13:54:49 +00:00
|
|
|
int i;
|
2015-05-16 04:37:07 +00:00
|
|
|
|
2019-05-15 15:53:18 +00:00
|
|
|
if (!priv->text)
|
2015-05-16 04:37:07 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2019-05-15 15:53:18 +00:00
|
|
|
if (!priv->words)
|
2015-05-16 04:37:07 +00:00
|
|
|
{
|
2019-05-15 15:53:18 +00:00
|
|
|
prepared = prepare_string_for_compare (priv->text);
|
|
|
|
priv->words = g_strsplit (prepared, " ", -1);
|
2015-05-16 04:37:07 +00:00
|
|
|
g_free (prepared);
|
|
|
|
}
|
|
|
|
|
|
|
|
prepared = prepare_string_for_compare (string);
|
|
|
|
|
|
|
|
found = TRUE;
|
2019-05-15 15:53:18 +00:00
|
|
|
for (i = 0; priv->words[i]; i++)
|
2015-05-16 04:37:07 +00:00
|
|
|
{
|
2019-05-15 15:53:18 +00:00
|
|
|
if (strstr (prepared, priv->words[i]) == NULL)
|
2015-05-16 04:37:07 +00:00
|
|
|
{
|
|
|
|
found = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (prepared);
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|