gtkquery.c 3.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21
 *
 * Author: Anders Carlsson <andersca@imendio.com>
 *
 * Based on nautilus-query.c
 */

22
#include "config.h"
23 24 25 26
#include <string.h>

#include "gtkquery.h"

27
struct _GtkQueryPrivate
28 29
{
  gchar *text;
30
  GFile *location;
31
  GList *mime_types;
32
  gchar **words;
33 34
};

Matthias Clasen's avatar
Matthias Clasen committed
35
G_DEFINE_TYPE_WITH_PRIVATE (GtkQuery, gtk_query, G_TYPE_OBJECT)
36 37 38 39 40

static void
finalize (GObject *object)
{
  GtkQuery *query;
41

42
  query = GTK_QUERY (object);
43

44
  g_clear_object (&query->priv->location);
45
  g_free (query->priv->text);
46
  g_strfreev (query->priv->words);
47

Matthias Clasen's avatar
Matthias Clasen committed
48
  G_OBJECT_CLASS (gtk_query_parent_class)->finalize (object);
49 50 51
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
52
gtk_query_class_init (GtkQueryClass *class)
53 54
{
  GObjectClass *gobject_class;
Matthias Clasen's avatar
Matthias Clasen committed
55

56 57 58 59 60
  gobject_class = G_OBJECT_CLASS (class);
  gobject_class->finalize = finalize;
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
61
gtk_query_init (GtkQuery *query)
62
{
Matthias Clasen's avatar
Matthias Clasen committed
63
  query->priv = gtk_query_get_instance_private (query);
64 65 66
}

GtkQuery *
Matthias Clasen's avatar
Matthias Clasen committed
67
gtk_query_new (void)
68 69 70 71 72
{
  return g_object_new (GTK_TYPE_QUERY,  NULL);
}


73
const gchar *
Matthias Clasen's avatar
Matthias Clasen committed
74
gtk_query_get_text (GtkQuery *query)
75
{
76
  return query->priv->text;
77 78
}

Matthias Clasen's avatar
Matthias Clasen committed
79 80 81
void
gtk_query_set_text (GtkQuery    *query,
                    const gchar *text)
82 83 84
{
  g_free (query->priv->text);
  query->priv->text = g_strdup (text);
85 86 87

  g_strfreev (query->priv->words);
  query->priv->words = NULL;
88 89
}

90
GFile *
Matthias Clasen's avatar
Matthias Clasen committed
91
gtk_query_get_location (GtkQuery *query)
92
{
93
  return query->priv->location;
94
}
Matthias Clasen's avatar
Matthias Clasen committed
95

96
void
97 98
gtk_query_set_location (GtkQuery *query,
                        GFile    *file)
99
{
100
  g_set_object (&query->priv->location, file);
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
static gchar *
prepare_string_for_compare (const gchar *string)
{
  gchar *normalized, *res;

  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,
                          const gchar *string)
{
  gchar *prepared;
  gboolean found;
  gint i;

  if (!query->priv->text)
    return FALSE;

  if (!query->priv->words)
    {
      prepared = prepare_string_for_compare (query->priv->text);
      query->priv->words = g_strsplit (prepared, " ", -1);
      g_free (prepared);
    }

  prepared = prepare_string_for_compare (string);

  found = TRUE;
  for (i = 0; query->priv->words[i]; i++)
    {
      if (strstr (prepared, query->priv->words[i]) == NULL)
        {
          found = FALSE;
          break;
        }
    }

  g_free (prepared);

  return found;
}