summaryrefslogtreecommitdiff
path: root/src/av-cp/entry-completion.c
blob: c0e7330ab5f3fad058648787f7024536f0702506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "entry-completion.h"

#include <libgupnp-av/gupnp-av.h>

static void
entry_completion_constructed (GObject *self);

struct _EntryCompletion
{
        GtkEntryCompletion parent_instance;

        GtkListStore *store;
};


G_DEFINE_TYPE (EntryCompletion, entry_completion, GTK_TYPE_ENTRY_COMPLETION)

GtkEntryCompletion *
entry_completion_new (void)
{
        return g_object_new (ENTRY_TYPE_COMPLETION, NULL);
}

static void
entry_completion_finalize (GObject *object)
{
        G_OBJECT_CLASS (entry_completion_parent_class)->finalize (object);
}

static void
entry_completion_get_property (GObject    *object,
                               guint       prop_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
        switch (prop_id)
        {
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
}

static void
entry_completion_set_property (GObject      *object,
                               guint         prop_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
    switch (prop_id)
      {
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      }
}

static gboolean
entry_completion_on_match_selected (GtkEntryCompletion *self,
                                    GtkTreeModel       *model,
                                    GtkTreeIter        *iter)
{
        char *match = NULL;
        GtkEntry *entry;

        entry = GTK_ENTRY (gtk_entry_completion_get_entry (self));
        gtk_tree_model_get (model, iter, 0, &match, -1);
        
        char *old_text = g_strdup (gtk_entry_get_text (entry));
        char *needle = strrchr (old_text, ' ');
        if (needle != NULL) {
                needle++;
                *needle = '\0';
        }

        char *new_text = g_strconcat (needle == NULL ? ""
                                                     : old_text,
                                      match,
                                      NULL);
        gtk_entry_set_text (entry, new_text);
        gtk_editable_set_position (GTK_EDITABLE (entry), strlen (new_text));
        g_free (new_text);
        g_free (old_text);

        return TRUE;
}

static void
entry_completion_class_init (EntryCompletionClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS (klass);

        object_class->constructed = entry_completion_constructed;
        object_class->finalize = entry_completion_finalize;
        object_class->get_property = entry_completion_get_property;
        object_class->set_property = entry_completion_set_property;

        GtkEntryCompletionClass *entry_class = GTK_ENTRY_COMPLETION_CLASS (klass);
        entry_class->match_selected = entry_completion_on_match_selected;
}

static gboolean
entry_completion_on_match (GtkEntryCompletion *self, const char *key, GtkTreeIter *iter, gpointer user_data)
{
        GtkTreeModel *model = gtk_entry_completion_get_model (self);
        char *candidate;
        GtkWidget *entry;
        gboolean retval = FALSE;

        entry  = gtk_entry_completion_get_entry (self);
        gtk_tree_model_get (model, iter, 0, &candidate, -1);

        const char *needle = strrchr (key, ' ');
        if (needle != NULL) {
                if ((needle - key)> gtk_editable_get_position (GTK_EDITABLE (entry))) {
                        g_print ("Position wrong\n");
                        goto out;
                }
                needle++;
        } else {
                needle = key;
        }

        if (strlen(needle) == 0) {
                goto out;
        }

        retval = g_str_has_prefix (candidate, needle);

out:
        g_free (candidate);

        return retval;
}

static void
entry_completion_init (EntryCompletion *self)
{
}

static void
entry_completion_constructed (GObject *object)
{
        EntryCompletion *self = ENTRY_COMPLETION (object);

        if (G_OBJECT_CLASS (entry_completion_parent_class)->constructed != NULL) {
                G_OBJECT_CLASS (entry_completion_parent_class)->constructed (G_OBJECT (self));
        }

        gtk_entry_completion_set_match_func (GTK_ENTRY_COMPLETION (self),
                                             entry_completion_on_match,
                                             NULL,
                                             NULL);

        self->store = gtk_list_store_new (1, G_TYPE_STRING);

        gtk_entry_completion_set_model (GTK_ENTRY_COMPLETION (self), GTK_TREE_MODEL (self->store));
        gtk_entry_completion_set_text_column (GTK_ENTRY_COMPLETION (self), 0);
}

static const char *keywords[] = {
        "and",
        "or",
        "contains",
        "doesNotContain",
        "derivedFrom",
        "exists",
        "true",
        "false",
        ">", ">=", "<", "<=", "=", "!=", "*",
        NULL
};

void
entry_completion_set_search_criteria (EntryCompletion *self, char const * const * criteria)
{
        GtkTreeIter iter;
        gtk_list_store_clear (self->store);

        // Prefill ListStore with the search expression keywords
        char const * const *it = keywords;
        while (*it != NULL) {
                gtk_list_store_insert_with_values (self->store,
                                                   &iter,
                                                   -1,
                                                   0,
                                                   *it,
                                                   -1);
                it++;
        }

        // Add the properties supported by the search criteria
        it = criteria;
        while (*it != NULL) {
                gtk_list_store_insert_with_values (self->store,
                                                   &iter,
                                                   -1,
                                                   0,
                                                   *it,
                                                   -1);
                it++;
        }
}