~ubuntu-branches/ubuntu/lucid/ldtp/lucid

« back to all changes in this revision

Viewing changes to src/ldtp-appmap.c

  • Committer: Bazaar Package Importer
  • Author(s): Ara Pulido
  • Date: 2010-01-27 17:57:45 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20100127175745-4g7my2y5c17zhkop
Tags: 2.0.2-0ubuntu1
* New upstream release (Fixes LP: #516248):
  + LDTPv2 is a complete rewrite of LDTPv1 in Python
  + LTFX is completely removed in LDTP v2 in favor of wnck

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
 
/*
3
 
 * Linux Desktop Testing Project http://ldtp.freedesktop.org
4
 
 *
5
 
 * Author:
6
 
 *    Nagappan Alagappan <nagappan@gmail.com>
7
 
 *    S Thanikachalam <sthanikachalam@novell.com>
8
 
 *    Premkumar J <jpremkumar@novell.com>
9
 
 *
10
 
 * Copyright 2004 - 2006 Novell, Inc.
11
 
 * Copyright 2007 - 2008 Nagappan Alagappan
12
 
 *
13
 
 * This program is free software; you can redistribute it and/or
14
 
 * modify it under the terms of the GNU Lesser General Public
15
 
 * License as published by the Free Software Foundation; either
16
 
 * version 2 of the License, or (at your option) any later version.
17
 
 *
18
 
 * This program is distributed in the hope that it will be useful,
19
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21
 
 * Lesser General Public License for more details.
22
 
 *
23
 
 * You should have received a copy of the GNU Lesser General Public
24
 
 * License along with this program; if not, write to the
25
 
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26
 
 * Boston, MA 02110, USA.
27
 
 */
28
 
 
29
 
#include "ldtp.h"
30
 
#include "ldtp-gui.h"
31
 
#include "ldtp-utils.h"
32
 
#include "ldtp-error.h"
33
 
#include "ldtp-logger.h"
34
 
#include "ldtp-appmap.h"
35
 
#include "ldtp-command.h"
36
 
#include "ldtp-gui-comp.h"
37
 
 
38
 
extern gboolean ldtp_debug;
39
 
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
40
 
 
41
 
static gchar*
42
 
read_delimiter (int fd, gchar delimit)
43
 
{
44
 
        int len = 128;
45
 
        int size = 0;
46
 
        gchar ch;
47
 
        gchar *data = NULL;
48
 
 
49
 
        while (read (fd, &ch, 1) > 0) {
50
 
                if (!data)
51
 
                        data = (gchar *) malloc (sizeof (gchar)*len);
52
 
                if (size%len == 0)
53
 
                        data = (gchar *) realloc (data, sizeof (gchar)*len+size+1);
54
 
                data[size++] = ch;
55
 
                if (data[0] == '\n' || ch == delimit || (size && data[size - 1] == ']')) {
56
 
                        if (data[0] != '\n' && read (fd, &ch, 1) > 0) {
57
 
                                /*
58
 
                                 * Don't do any operation
59
 
                                 */
60
 
                        }
61
 
                        if (size%len == 0)
62
 
                                data = (gchar *) realloc (data, sizeof (gchar)+size+1);
63
 
                        data[size] = '\0';
64
 
                        return data;
65
 
                }
66
 
        }
67
 
        return NULL;
68
 
}
69
 
 
70
 
static void
71
 
add_child_attributes (gchar *cur_entry, GHashTable *cur_context_table)
72
 
{
73
 
        gchar *token = NULL;
74
 
        int offset;
75
 
        gchar *component = NULL;
76
 
            
77
 
        token = strtok (strdup (cur_entry), "=");
78
 
        if (token)
79
 
                component = strdup (token);
80
 
            
81
 
        offset = strlen (component) + 1;
82
 
        if (cur_entry[offset] == '{') {
83
 
                gchar *end = strstr (cur_entry, "}");
84
 
                if (end) {
85
 
                        gchar *key, *value;
86
 
                        gchar *all_attributes;
87
 
                        GHashTable *hash_attributes;
88
 
                        int len = (end - cur_entry) - (offset+1);
89
 
         
90
 
                        all_attributes = (gchar *) malloc (sizeof (gchar) * len + 1);
91
 
                        strncpy (all_attributes, (cur_entry + offset + 1), len);
92
 
                        all_attributes[len] = '\0';
93
 
 
94
 
                        /*
95
 
                          Create new attribute hash table
96
 
                        */
97
 
                        hash_attributes = g_hash_table_new (&g_str_hash, &g_str_equal);
98
 
                        key = strtok (strdup (all_attributes), "=");
99
 
                        while (key) {
100
 
                                gchar *hash_key;
101
 
                                gchar *hash_value;
102
 
                                hash_key = g_strdup (key);
103
 
                                value = strtok (NULL, ",");
104
 
                                hash_value = g_strdup (value);
105
 
                                if (hash_key && hash_value)
106
 
                                        g_print ("%s: %s\n", hash_key, hash_value);
107
 
                                if (hash_attributes && hash_key && hash_value)
108
 
                                        g_hash_table_insert (hash_attributes, hash_key, hash_value);
109
 
                                key = strtok (NULL, "=");
110
 
                        }
111
 
                        if (component && cur_context_table && hash_attributes)
112
 
                                g_hash_table_insert (cur_context_table, component, hash_attributes);
113
 
                }
114
 
        }
115
 
}
116
 
 
117
 
void
118
 
print_attributes (gchar *key, gchar *value, gchar *userdata)
119
 
{
120
 
        if (!key || !value)
121
 
                return;
122
 
        g_print ("\t|-------%s = %s\n", key, value);
123
 
}
124
 
 
125
 
void
126
 
print_component (gchar *key, GHashTable *component, gchar *userdata)
127
 
{
128
 
        if (!key || !component)
129
 
                return;
130
 
        g_print ("|-------%s\n", key);
131
 
        g_hash_table_foreach (component, (GHFunc)&print_attributes, userdata);
132
 
}
133
 
 
134
 
void
135
 
print_context (gchar *key, GHashTable *context, gchar *userdata)
136
 
{
137
 
        if (!key || !context || !ldtp_debug)
138
 
                return;
139
 
        g_print (":%s:\n", key);
140
 
        g_hash_table_foreach (context, (GHFunc)&print_component, userdata);
141
 
}
142
 
 
143
 
/*
144
 
  Initialize application map
145
 
*/
146
 
GHashTable*
147
 
appmap_init (gchar *gui_map_filename, FILE *log_fp)
148
 
{
149
 
        int fd;
150
 
        //int quit_flag = 0;
151
 
        FILE *fp;
152
 
        gchar *cur_entry = NULL;
153
 
        GHashTable *appmap;
154
 
        GHashTable *cur_context_table = NULL;
155
 
 
156
 
        appmap = g_hash_table_new (&g_str_hash, &g_str_equal);
157
 
    
158
 
        fp = fopen (gui_map_filename, "r");
159
 
        if (fp == NULL) {
160
 
                g_print ("Unable to open appmap %s file\n", gui_map_filename);
161
 
                log_msg (LDTP_LOG_CAUSE, "Unable to open appmap file", log_fp);
162
 
                return NULL;
163
 
        }
164
 
        fd = fileno (fp);
165
 
 
166
 
        cur_entry = read_delimiter (fd, '}');
167
 
        while (1) {
168
 
                int cur_entry_len;
169
 
                gchar *context = NULL;
170
 
                if (!cur_entry)
171
 
                        break;
172
 
                cur_entry_len = strlen (cur_entry);
173
 
                if (cur_entry [0] == '[' && cur_entry [cur_entry_len - 1] == ']') {
174
 
                        cur_context_table = g_hash_table_new (&g_str_hash, &g_str_equal);
175
 
                        context = (gchar *) g_new0 (gchar, sizeof (gchar) * (cur_entry_len - 1));
176
 
 
177
 
                        /* Copy everything except the leading '[' and trailing ']' */
178
 
                        memcpy (context, cur_entry+1, cur_entry_len-2);
179
 
                        if (appmap && context && cur_context_table) 
180
 
                                g_hash_table_insert (appmap, context, cur_context_table);
181
 
                }
182
 
                else if (cur_entry[0] == '\n') {
183
 
                        cur_entry = read_delimiter (fd, '}');
184
 
                        continue;
185
 
                }
186
 
                else
187
 
                        add_child_attributes (cur_entry, cur_context_table);
188
 
                cur_entry = read_delimiter (fd, '}');
189
 
        }
190
 
        if (ldtp_debug)
191
 
                g_hash_table_foreach (appmap, (GHFunc)&print_context, NULL);
192
 
        return appmap;
193
 
}
194
 
 
195
 
void
196
 
destroy_hashtable (gpointer data)
197
 
{
198
 
        if (data) {
199
 
                g_hash_table_destroy (data);
200
 
                data = NULL;
201
 
        }
202
 
}
203
 
 
204
 
void
205
 
destroy_string (gpointer data)
206
 
{
207
 
        if (data) {
208
 
                g_free (data);
209
 
                data = NULL;
210
 
        }
211
 
}
212
 
 
213
 
static gboolean
214
 
is_remaped (gpointer key, gpointer value, gpointer user_data)
215
 
{
216
 
        if (key == NULL || value == NULL)
217
 
                return FALSE;
218
 
        g_print ("%s - %s ******* DEBUG\n", (gchar *) key, (gchar *) value);
219
 
        if (g_utf8_collate (key, "remap") == 0)
220
 
                return TRUE;
221
 
        return FALSE;
222
 
}
223
 
 
224
 
gboolean
225
 
remove_remapped_entry (gpointer key, gpointer value, gpointer user_data)
226
 
{
227
 
        if (key == NULL || value == NULL)
228
 
                return TRUE;
229
 
        g_hash_table_foreach_remove (value, is_remaped, key);
230
 
        return TRUE;
231
 
}
232
 
 
233
 
/*
234
 
  Free application map resource
235
 
*/
236
 
void
237
 
ldtp_appmap_free (GHashTable *appmap)
238
 
{
239
 
        if (!appmap)
240
 
                return;
241
 
 
242
 
        pthread_mutex_lock (&mutex);
243
 
        g_hash_table_destroy (appmap);
244
 
        pthread_mutex_unlock (&mutex);
245
 
        appmap = NULL;
246
 
}
247
 
 
248
 
static gboolean
249
 
search_key_based (gpointer key, gpointer value, gpointer user_data)
250
 
{
251
 
        OBJInfo *obj = (OBJInfo *) user_data;
252
 
        if (!key || !obj || !obj->key) {
253
 
                return FALSE;
254
 
        }
255
 
        if (g_pattern_match_simple (obj->key, key)) {
256
 
                if (ldtp_debug && obj->key && key) {
257
 
                        g_print ("Key: %s %s\n",
258
 
                                 (gchar *) key,
259
 
                                 obj->key);
260
 
                }
261
 
                if (obj->parent_name && value) {
262
 
                        gchar *parent = get_property (value, "parent", NULL);
263
 
                        if (parent == NULL) {
264
 
                                // Only the window's parent can be
265
 
                                // empty in current hash table, so let
266
 
                                // us consider that the requested key
267
 
                                // is window name
268
 
                                g_print ("Parent NULL\n");
269
 
                                return TRUE;
270
 
                        }
271
 
                        if (parent && g_pattern_match_simple (obj->parent_name, parent))
272
 
                                return TRUE;
273
 
                } else
274
 
                        return TRUE;
275
 
        }
276
 
        return FALSE;
277
 
}
278
 
 
279
 
static void
280
 
search_window_key_based (gpointer key, gpointer value, gpointer user_data)
281
 
{
282
 
        OBJInfo *obj;
283
 
        obj = (OBJInfo *) user_data;
284
 
        if (obj && obj->ht_data_list && search_key_based (key, value, user_data)) {
285
 
                g_hash_table_insert (obj->ht_data_list, g_strdup ((gchar *) key), NULL);
286
 
        }
287
 
}
288
 
 
289
 
static gboolean
290
 
search_obj_based_on_index (gpointer key, gpointer value, gpointer user_data)
291
 
{
292
 
        gchar *obj_index = NULL;
293
 
        OBJInfo *obj = (OBJInfo *) user_data;
294
 
        if (!value || !obj || !obj->key) {
295
 
                return FALSE;
296
 
        }
297
 
        obj_index = get_property (value, "obj_index", NULL);
298
 
        if (obj_index && g_pattern_match_simple (obj->key, obj_index)) {
299
 
                if (obj->parent_name) {
300
 
                        gchar *parent = get_property (value, "parent", NULL);
301
 
                        if (parent == NULL) {
302
 
                                // Only the window's parent can be
303
 
                                // empty in current hash table, so let
304
 
                                // us consider that the requested key
305
 
                                // is window name
306
 
                                g_print ("Parent NULL\n");
307
 
                                return TRUE;
308
 
                        }
309
 
                        if (parent && g_pattern_match_simple (obj->parent_name, parent))
310
 
                                return TRUE;
311
 
                } else
312
 
                        return TRUE;
313
 
        }
314
 
        return FALSE;
315
 
}
316
 
 
317
 
static gboolean
318
 
search_key_glob_based (gpointer key, gpointer value, gpointer user_data)
319
 
{
320
 
        gchar *tmp;
321
 
        OBJInfo *obj;
322
 
        if (!key || !user_data) {
323
 
                return FALSE;
324
 
        }
325
 
        /*
326
 
          FIXME: Use get_object_info function to get the the type of object
327
 
          if type and key matches then return true
328
 
        */
329
 
        obj = (OBJInfo *) user_data;
330
 
        if (ldtp_debug && obj->key && key) {
331
 
                g_print ("glob - key: %d - %d - %d - %s %s %ld %ld\n",
332
 
                         obj->obj_is_window,
333
 
                         g_pattern_match_simple ("frm*", obj->key),
334
 
                         g_pattern_match_simple ("dlg*", obj->key),
335
 
                         obj->key, (gchar *) key, g_utf8_strlen (key, -1),
336
 
                         g_utf8_strlen (obj->key, -1));
337
 
        }
338
 
        if (obj->obj_is_window) {
339
 
                if (obj->pattern) {
340
 
                        if (g_pattern_match_string (obj->pattern, key)) {
341
 
                                if (obj->parent_name && value) {
342
 
                                        gchar *parent = get_property (value, "parent", NULL);
343
 
                                        if (parent == NULL) {
344
 
                                                // Only the window's parent can be
345
 
                                                // empty in current hash table, so let
346
 
                                                // us consider that the requested key
347
 
                                                // is window name
348
 
                                                g_print ("Parent NULL\n");
349
 
                                                return TRUE;
350
 
                                        }
351
 
                                        if (parent && g_pattern_match_simple (obj->parent_name, parent))
352
 
                                                return TRUE;
353
 
                                } else
354
 
                                        return TRUE;
355
 
                        }
356
 
                }
357
 
                tmp = g_strdup_printf ("frm%s", (gchar *)key);
358
 
                if (tmp && g_pattern_match_simple (tmp, key)) {
359
 
                        g_free (tmp);
360
 
                        tmp = NULL;
361
 
                        if (obj->parent_name && value) {
362
 
                                gchar *parent = get_property (value, "parent", NULL);
363
 
                                if (parent == NULL) {
364
 
                                        // Only the window's parent can be
365
 
                                        // empty in current hash table, so let
366
 
                                        // us consider that the requested key
367
 
                                        // is window name
368
 
                                        g_print ("Parent NULL\n");
369
 
                                        return TRUE;
370
 
                                }
371
 
                                if (parent && g_pattern_match_simple (obj->parent_name, parent))
372
 
                                        return TRUE;
373
 
                        } else
374
 
                                return TRUE;
375
 
                }
376
 
                if (tmp)
377
 
                        g_free (tmp);
378
 
                tmp = g_strdup_printf ("dlg%s", obj->key);
379
 
                if (tmp && g_pattern_match_simple (tmp, key)) {
380
 
                        g_free (tmp);
381
 
                        tmp = NULL;
382
 
                        if (obj->parent_name && value) {
383
 
                                gchar *parent = get_property (value, "parent", NULL);
384
 
                                if (parent == NULL) {
385
 
                                        // Only the window's parent can be
386
 
                                        // empty in current hash table, so let
387
 
                                        // us consider that the requested key
388
 
                                        // is window name
389
 
                                        g_print ("Parent NULL\n");
390
 
                                        return TRUE;
391
 
                                }
392
 
                                if (parent && g_pattern_match_simple (obj->parent_name, parent))
393
 
                                        return TRUE;
394
 
                        } else
395
 
                                return TRUE;
396
 
                }
397
 
                if (tmp)
398
 
                        g_free (tmp);
399
 
                tmp = g_strdup_printf ("frm%s*", obj->key);
400
 
                if (tmp && g_pattern_match_simple (tmp, key)) {
401
 
                        g_free (tmp);
402
 
                        tmp = NULL;
403
 
                        if (obj->parent_name && value) {
404
 
                                gchar *parent = get_property (value, "parent", NULL);
405
 
                                if (parent == NULL) {
406
 
                                        // Only the window's parent can be
407
 
                                        // empty in current hash table, so let
408
 
                                        // us consider that the requested key
409
 
                                        // is window name
410
 
                                        return TRUE;
411
 
                                }
412
 
                                if (parent && g_pattern_match_simple (obj->parent_name, parent))
413
 
                                        return TRUE;
414
 
                        } else
415
 
                                return TRUE;
416
 
                }
417
 
                if (tmp)
418
 
                        g_free (tmp);
419
 
                tmp = g_strdup_printf ("dlg%s*", obj->key);
420
 
                if (tmp && g_pattern_match_simple (tmp, key)) {
421
 
                        g_free (tmp);
422
 
                        tmp = NULL;
423
 
                        if (obj->parent_name && value) {
424
 
                                gchar *parent = get_property (value, "parent", NULL);
425
 
                                if (parent == NULL) {
426
 
                                        // Only the window's parent can be
427
 
                                        // empty in current hash table, so let
428
 
                                        // us consider that the requested key
429
 
                                        // is window name
430
 
                                        g_print ("Parent NULL\n");
431
 
                                        return TRUE;
432
 
                                }
433
 
                                if (parent && g_pattern_match_simple (obj->parent_name, parent))
434
 
                                        return TRUE;
435
 
                        } else
436
 
                                return TRUE;
437
 
                }
438
 
                if (tmp)
439
 
                        g_free (tmp);
440
 
                return FALSE;
441
 
        }
442
 
        if (obj->pattern && !obj->obj_is_window) {
443
 
                gboolean flag = g_pattern_match_string (obj->pattern, key);
444
 
                if (flag && obj->parent_name && value) {
445
 
                        gchar *parent = get_property (value, "parent", NULL);
446
 
                        if (parent == NULL) {
447
 
                                // Only the window's parent can be
448
 
                                // empty in current hash table, so let
449
 
                                // us consider that the requested key
450
 
                                // is window name
451
 
                                g_print ("Parent NULL\n");
452
 
                                return TRUE;
453
 
                        }
454
 
                        if (parent && g_pattern_match_simple (obj->parent_name, parent))
455
 
                                return TRUE;
456
 
                } else
457
 
                        return flag;
458
 
        }
459
 
        return FALSE;
460
 
}
461
 
 
462
 
static void
463
 
search_window_key_glob_based (gpointer key, gpointer value, gpointer user_data)
464
 
{
465
 
        OBJInfo *obj;
466
 
        obj = (OBJInfo *) user_data;
467
 
        if (obj && obj->ht_data_list && search_key_glob_based (key, value, user_data)) {
468
 
                g_hash_table_insert (obj->ht_data_list, g_strdup ((gchar *) key), value);
469
 
        }
470
 
}
471
 
 
472
 
static gboolean
473
 
search_label_based (gpointer key, gpointer value, gpointer user_data)
474
 
{
475
 
        OBJInfo *obj;
476
 
        gchar *tmp   = NULL;
477
 
        gchar *class = NULL;
478
 
        gchar *label = NULL;
479
 
        gchar *label_by = NULL;
480
 
        gchar *tmp_data = NULL;
481
 
        gboolean flag = FALSE;
482
 
        GPatternSpec *pattern = NULL;
483
 
 
484
 
        if (!value || !user_data) {
485
 
                return FALSE;
486
 
        }
487
 
        if (key)
488
 
                g_print ("Key: %s\n", (gchar *) key);
489
 
        obj = (OBJInfo *) user_data;
490
 
        g_hash_table_lookup_extended (value, "class", NULL, (gpointer) &class);
491
 
        g_hash_table_lookup_extended (value, "label", NULL, (gpointer) &label);
492
 
 
493
 
        tmp_data = obj->key;
494
 
        if (class)
495
 
                pattern = g_pattern_spec_new (class);
496
 
        if (pattern && (g_pattern_match_string (pattern, "frame") ||
497
 
            g_pattern_match_string (pattern, "dialog") ||
498
 
            g_pattern_match_string (pattern, "alert") ||
499
 
            g_pattern_match_string (pattern, "file_chooser") ||
500
 
            g_pattern_match_string (pattern, "font_chooser"))) {
501
 
                flag = TRUE;
502
 
        }
503
 
        if (pattern)
504
 
                g_pattern_spec_free (pattern);
505
 
        /*
506
 
          FIXME: Use get_object_info function to get the the type of object
507
 
          if type and key matches then return true
508
 
        */
509
 
        if (label && tmp_data) {
510
 
                if (ldtp_debug && (gchar *)key && label && tmp_data)
511
 
                        g_print ("Label: %s - %s - %s\n", (gchar *)key, (gchar *)tmp_data, label);
512
 
                if (!obj->obj_is_window || flag)
513
 
                        if (g_utf8_collate (label, tmp_data) == 0) {
514
 
                                if (obj->parent_name && value) {
515
 
                                        gchar *parent = get_property (value, "parent", NULL);
516
 
                                        if (parent == NULL) {
517
 
                                                // Only the window's parent can be
518
 
                                                // empty in current hash table, so let
519
 
                                                // us consider that the requested key
520
 
                                                // is window name
521
 
                                                g_print ("Parent NULL\n");
522
 
                                                return TRUE;
523
 
                                        }
524
 
                                        if (parent && g_pattern_match_simple (obj->parent_name, parent))
525
 
                                                return TRUE;
526
 
                                } else
527
 
                                        return TRUE;
528
 
                        }
529
 
                // Search for mnemonics, if available then remove just _
530
 
                if (g_utf8_strchr (label, -1, '_') == NULL)
531
 
                        return FALSE;
532
 
                tmp = escape_character (label, '_');
533
 
                if (tmp && tmp_data)
534
 
                        if (g_utf8_collate (tmp, tmp_data) == 0) {
535
 
                                g_free (tmp);
536
 
                                tmp = NULL;
537
 
                                if (obj->parent_name && value) {
538
 
                                        gchar *parent = get_property (value, "parent", NULL);
539
 
                                        if (parent)
540
 
                                                g_print ("Parent: %s - %s\n", parent, obj->parent_name);
541
 
                                        if (parent == NULL) {
542
 
                                                // Only the window's parent can be
543
 
                                                // empty in current hash table, so let
544
 
                                                // us consider that the requested key
545
 
                                                // is window name
546
 
                                                g_print ("Parent NULL\n");
547
 
                                                return TRUE;
548
 
                                        }
549
 
                                        if (parent && g_pattern_match_simple (obj->parent_name, parent))
550
 
                                                return TRUE;
551
 
                                } else
552
 
                                        return TRUE;
553
 
                        }
554
 
                if (tmp)
555
 
                        g_free (tmp);
556
 
                return FALSE;
557
 
        }
558
 
        g_hash_table_lookup_extended (value, "label_by", NULL, (gpointer) &label_by);
559
 
        if (label_by && tmp_data) {
560
 
                if ((gchar *)key && label_by && tmp_data)
561
 
                        g_print ("LabelBy: %s - %s - %s\n", (gchar *)key, (gchar *)tmp_data, label_by);
562
 
                if (!obj->obj_is_window || flag)
563
 
                        if (g_utf8_collate (label_by, tmp_data) == 0) {
564
 
                                if (obj->parent_name && value) {
565
 
                                        gchar *parent = get_property (value, "parent", NULL);
566
 
                                        if (parent == NULL) {
567
 
                                                // Only the window's parent can be
568
 
                                                // empty in current hash table, so let
569
 
                                                // us consider that the requested key
570
 
                                                // is window name
571
 
                                                g_print ("Parent NULL\n");
572
 
                                                return TRUE;
573
 
                                        }
574
 
                                        if (parent && g_pattern_match_simple (obj->parent_name, parent))
575
 
                                                return TRUE;
576
 
                                } else
577
 
                                        return TRUE;
578
 
                        }
579
 
                // Search for mnemonics, if available then remove just _
580
 
                if (g_utf8_strchr (label_by, -1, '_') == NULL)
581
 
                        return FALSE;
582
 
                tmp = escape_character (label, '_');
583
 
                if (tmp && tmp_data)
584
 
                        if (g_utf8_collate (tmp, tmp_data) == 0) {
585
 
                                g_free (tmp);
586
 
                                tmp = NULL;
587
 
                                if (obj->parent_name && value) {
588
 
                                        gchar *parent = get_property (value, "parent", NULL);
589
 
                                        if (parent == NULL) {
590
 
                                                // Only the window's parent can be
591
 
                                                // empty in current hash table, so let
592
 
                                                // us consider that the requested key
593
 
                                                // is window name
594
 
                                                g_print ("Parent NULL\n");
595
 
                                                return TRUE;
596
 
                                        }
597
 
                                        if (parent && g_pattern_match_simple (obj->parent_name, parent))
598
 
                                                return TRUE;
599
 
                                } else
600
 
                                        return TRUE;
601
 
                        }
602
 
                if (tmp)
603
 
                        g_free (tmp);
604
 
                return FALSE;
605
 
        }
606
 
        return FALSE;
607
 
}
608
 
 
609
 
static void
610
 
search_window_label_based (gpointer key, gpointer value, gpointer user_data)
611
 
{
612
 
        OBJInfo *obj;
613
 
        obj = (OBJInfo *) user_data;
614
 
        if (obj && obj->ht_data_list && search_label_based (key, value, user_data)) {
615
 
                g_hash_table_insert (obj->ht_data_list, g_strdup ((gchar *) key), value);
616
 
        }
617
 
}
618
 
 
619
 
static gboolean
620
 
search_label_glob_based (gpointer key, gpointer value, gpointer user_data)
621
 
{
622
 
        OBJInfo *obj;
623
 
        gchar *class = NULL;
624
 
        gchar *tmp_label = NULL;
625
 
        gchar *tmp_label_by = NULL;
626
 
        GPatternSpec *pattern = NULL;
627
 
        gboolean flag = FALSE;
628
 
 
629
 
        if (!value || !user_data)
630
 
                return FALSE;
631
 
        /*
632
 
          FIXME: Remove under score from label or label_by and do a pattern match too
633
 
        */
634
 
 
635
 
        obj = (OBJInfo *) user_data;
636
 
        if (g_hash_table_lookup_extended (value, "class", NULL, (gpointer) &class))
637
 
                pattern = g_pattern_spec_new (class);
638
 
        if (pattern && (g_pattern_match_string (pattern, "frame") ||
639
 
            g_pattern_match_string (pattern, "dialog") ||
640
 
            g_pattern_match_string (pattern, "alert") ||
641
 
            g_pattern_match_string (pattern, "file_chooser") ||
642
 
            g_pattern_match_string (pattern, "font_chooser"))) {
643
 
                flag = TRUE;
644
 
        }
645
 
        if (pattern)
646
 
                g_pattern_spec_free (pattern);
647
 
        if (g_hash_table_lookup_extended (value, "label", NULL, (gpointer) &tmp_label)) {
648
 
                if (key && value)
649
 
                        g_print ("Label glob: %s - %s\n", (gchar *)key, (gchar *)value);
650
 
                if (obj->pattern && (!obj->obj_is_window || flag == TRUE)) {
651
 
                        gboolean tmp_flag = g_pattern_match_string (obj->pattern, tmp_label);
652
 
                        if (tmp_flag && obj->parent_name && value) {
653
 
                                gchar *parent = get_property (value, "parent", NULL);
654
 
                                if (parent == NULL) {
655
 
                                        // Only the window's parent can be
656
 
                                        // empty in current hash table, so let
657
 
                                        // us consider that the requested key
658
 
                                        // is window name
659
 
                                        g_print ("Parent NULL\n");
660
 
                                        return TRUE;
661
 
                                }
662
 
                                if (parent && g_pattern_match_simple (obj->parent_name, parent))
663
 
                                        return TRUE;
664
 
                        } else
665
 
                                return tmp_flag;
666
 
                }
667
 
        }
668
 
 
669
 
        if (g_hash_table_lookup_extended (value, "label_by", NULL, (gpointer) &tmp_label_by)) {
670
 
                g_print ("Label_By glob: %s\n", tmp_label_by);
671
 
                if (obj->pattern && (!obj->obj_is_window || flag)) {
672
 
                        gboolean tmp_flag = g_pattern_match_string (obj->pattern, tmp_label_by);
673
 
                        if (tmp_flag && obj->parent_name && value) {
674
 
                                gchar *parent = get_property (value, "parent", NULL);
675
 
                                if (parent == NULL) {
676
 
                                        // Only the window's parent can be
677
 
                                        // empty in current hash table, so let
678
 
                                        // us consider that the requested key
679
 
                                        // is window name
680
 
                                        g_print ("Parent NULL\n");
681
 
                                        return TRUE;
682
 
                                }
683
 
                                if (parent && g_pattern_match_simple (obj->parent_name, parent))
684
 
                                        return TRUE;
685
 
                        } else
686
 
                                return tmp_flag;
687
 
                }
688
 
        }
689
 
        return FALSE;
690
 
}
691
 
 
692
 
static void
693
 
search_window_label_glob_based (gpointer key, gpointer value, gpointer user_data)
694
 
{
695
 
        OBJInfo *obj;
696
 
        obj = (OBJInfo *) user_data;
697
 
        if (obj && obj->ht_data_list && search_label_glob_based (key, value, user_data)) {
698
 
                g_hash_table_insert (obj->ht_data_list, g_strdup ((gchar *) key), value);
699
 
        }
700
 
}
701
 
 
702
 
/*
703
 
static gboolean 
704
 
search_label_name (gpointer key, gpointer value, gpointer user_data)
705
 
{
706
 
        gchar *child_index = NULL;
707
 
        UnknLabelProperty *tmp_data = (UnknLabelProperty *) user_data;
708
 
        if (!value && !tmp_data && !key) {
709
 
                return FALSE;
710
 
        }
711
 
        if (g_hash_table_lookup_extended (value, "child_index", NULL, (gpointer) &child_index)) {
712
 
                if ((gchar *)key && tmp_data->str_child_index && tmp_data->parent_name)
713
 
                        g_print ("Key - Child index - Parent: %s - %s - %s\n",
714
 
                                 (gchar *)key, tmp_data->str_child_index,
715
 
                                 tmp_data->parent_name);
716
 
                if (child_index && tmp_data->str_child_index &&
717
 
                    g_utf8_collate (child_index, tmp_data->str_child_index) == 0) {
718
 
                        gchar *parent_name = NULL;
719
 
                        g_hash_table_lookup_extended (value, "parent", NULL, (gpointer) &parent_name);
720
 
                        g_print ("Key - Parent: %s - %s\n",
721
 
                                 (gchar *)key,
722
 
                                 parent_name);
723
 
                        if (parent_name && tmp_data->parent_name &&
724
 
                            g_utf8_collate (parent_name, tmp_data->parent_name) == 0) {
725
 
                                tmp_data->obj_name = (gchar *) key;
726
 
                                return TRUE;
727
 
                        }
728
 
                }
729
 
        }
730
 
        return FALSE;
731
 
}
732
 
*/
733
 
 
734
 
static gboolean
735
 
search_obj_after_stripping_space (gpointer key, gpointer value, gpointer user_data)
736
 
{
737
 
        gchar *tmp;
738
 
        OBJInfo *obj = (OBJInfo *) user_data;
739
 
        if (!key || !user_data || !obj->key)
740
 
                return FALSE;
741
 
        if (g_utf8_strchr (obj->key, -1, ' ') == NULL)
742
 
                return FALSE;
743
 
        tmp = escape_character (obj->key, ' ');
744
 
        if (!tmp)
745
 
                return FALSE;
746
 
        g_print ("DEBUG: %s - %s - %s\n", tmp, (gchar *)obj->key, (gchar *)key);
747
 
        if (g_utf8_collate (key, tmp) == 0 || g_utf8_collate (key, obj->key) == 0) {
748
 
                g_free (tmp);
749
 
                return TRUE;
750
 
        }
751
 
        else {
752
 
                gchar *glob_str = NULL;
753
 
                /*
754
 
                    FIXME: Use get_object_info function to get the the type of object
755
 
                      and based on that use the convention, instead of directly using *
756
 
                */
757
 
                glob_str = g_strdup_printf ("*%s", tmp);
758
 
                g_print ("Glob str: %s - %s - %s\n", glob_str, (gchar *)key, tmp);
759
 
                if (g_pattern_match_simple (glob_str, key) ||
760
 
                    g_pattern_match_simple (key, tmp)) {
761
 
                        g_print ("search_obj_after_stripping_space - matched\n");
762
 
                        g_free (tmp);
763
 
                        g_free (glob_str);
764
 
                        return TRUE;
765
 
                }
766
 
                g_free (glob_str);
767
 
                g_free (tmp);
768
 
        }
769
 
        return FALSE;
770
 
}
771
 
 
772
 
static void
773
 
search_window_after_stripping_space (gpointer key, gpointer value, gpointer user_data)
774
 
{
775
 
        OBJInfo *obj;
776
 
        if (!key || !user_data || !value)
777
 
                return;
778
 
        obj = (OBJInfo *) user_data;
779
 
        if (obj && obj->ht_data_list && search_obj_after_stripping_space (key, value, obj)) {
780
 
                g_hash_table_insert (obj->ht_data_list, g_strdup ((gchar *) key), value);
781
 
        }
782
 
}
783
 
 
784
 
GHashTable*
785
 
get_all_matching_def (GHashTable *ht, gchar *context, gboolean obj_is_window, FILE *log_fp)
786
 
{
787
 
        GHashTable *ht_data_list = NULL;
788
 
        LDTPErrorCode err;
789
 
 
790
 
        OBJInfo obj;
791
 
        GPatternSpec *pattern = NULL;
792
 
        if (ht == NULL) {
793
 
                err = LDTP_ERROR_APPMAP_NOT_INITIALIZED;
794
 
                g_print ("%s - %d - %s\n", __FILE__, __LINE__, ldtp_error_get_message (err));
795
 
                log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (err), log_fp);
796
 
                return NULL;
797
 
        }
798
 
        if (context == NULL) {
799
 
                err = LDTP_ERROR_ARGUMENT_NULL;
800
 
                g_print ("%s\n", ldtp_error_get_message (err));
801
 
                log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (err), log_fp);
802
 
                return NULL;
803
 
        }
804
 
        ht_data_list = g_hash_table_new_full (&g_str_hash, &g_str_equal,
805
 
                                                 destroy_string, NULL);
806
 
        obj.key = context;
807
 
        obj.parent_name = NULL;
808
 
        obj.obj_is_window = obj_is_window;
809
 
        obj.ht_data_list = ht_data_list;
810
 
        g_hash_table_foreach (ht, search_window_key_based, &obj);
811
 
 
812
 
        if (!g_hash_table_size (ht_data_list)) {
813
 
                /*
814
 
                  Search key based on glob expression
815
 
                */
816
 
                g_print ("Search key based on glob expression: %s %s %d\n", context, __FILE__, __LINE__);
817
 
                if (context)
818
 
                        pattern = g_pattern_spec_new ((const gchar *)context);
819
 
 
820
 
                obj.obj_is_window = obj_is_window;
821
 
                obj.pattern = pattern;
822
 
                obj.key = context;
823
 
                obj.ht_data_list = ht_data_list;
824
 
                g_hash_table_foreach (ht, search_window_key_glob_based, &obj);
825
 
                if (pattern)
826
 
                        g_pattern_spec_free (pattern);
827
 
        }
828
 
        if (!g_hash_table_size (ht_data_list)) {
829
 
                /*
830
 
                  Search label based
831
 
                */
832
 
                g_print ("Search label based: %s\n", context);
833
 
 
834
 
                obj.pattern = NULL;
835
 
                obj.key = context;
836
 
                obj.parent_name = NULL;
837
 
                obj.obj_is_window = obj_is_window;
838
 
                obj.ht_data_list = ht_data_list;
839
 
                g_hash_table_foreach (ht, search_window_label_based, &obj);
840
 
        }
841
 
        if (!g_hash_table_size (ht_data_list)) {
842
 
                /*
843
 
                  Search label based on glob expression
844
 
                */
845
 
                OBJInfo obj;
846
 
                GPatternSpec *pattern = NULL;
847
 
 
848
 
                g_print ("Search label based on glob expression: %s\n", context);
849
 
                if (context)
850
 
                        pattern = g_pattern_spec_new (context);
851
 
 
852
 
                obj.pattern = pattern;
853
 
                obj.key = context;
854
 
                obj.parent_name = NULL;
855
 
                obj.obj_is_window = obj_is_window;
856
 
                obj.ht_data_list = ht_data_list;
857
 
                g_hash_table_foreach (ht, search_window_label_glob_based, &obj);
858
 
                if (pattern)
859
 
                        g_pattern_spec_free (pattern);
860
 
        }
861
 
        if (!g_hash_table_size (ht_data_list)) {
862
 
                /*
863
 
                  Search key after stripping space
864
 
                */
865
 
                g_print ("Search key after stripping space: %s\n", context);
866
 
                obj.key = context;
867
 
                obj.parent_name = NULL;
868
 
                obj.obj_is_window = obj_is_window;
869
 
                obj.ht_data_list = ht_data_list;
870
 
                g_hash_table_foreach (ht, search_window_after_stripping_space, &obj);
871
 
        }
872
 
        return ht_data_list;
873
 
}
874
 
 
875
 
gboolean
876
 
match_object_def (gchar *key, gchar *value, gboolean obj_is_window)
877
 
{
878
 
        OBJInfo obj;
879
 
        gchar msg [256];
880
 
        GPatternSpec *pattern = NULL;
881
 
        /*
882
 
          Search key based on glob expression
883
 
        */
884
 
        g_print ("Search key based on glob expression: %s %s %d\n", key, __FILE__, __LINE__);
885
 
        if (key)
886
 
                pattern = g_pattern_spec_new (/*(const ggchar *)*/key);
887
 
 
888
 
        obj.obj_is_window = obj_is_window;
889
 
        obj.pattern = pattern;
890
 
        obj.key = key;
891
 
        if (search_key_glob_based (value, NULL, &obj)) {
892
 
                if (pattern)
893
 
                        g_pattern_spec_free (pattern);
894
 
                return TRUE;
895
 
        }
896
 
        if (pattern)
897
 
                g_pattern_spec_free (pattern);
898
 
 
899
 
        g_print ("Search label based: %s - %d\n", value, obj_is_window);
900
 
 
901
 
        obj.obj_is_window = obj_is_window;
902
 
        obj.pattern = NULL;
903
 
        obj.key = key;
904
 
        if (search_label_based (NULL, value, &obj))
905
 
                return TRUE;
906
 
 
907
 
        g_print ("Search label based on glob expression\n");
908
 
        if (key)
909
 
                pattern = g_pattern_spec_new (key);
910
 
 
911
 
        obj.obj_is_window = obj_is_window;
912
 
        obj.pattern = pattern;
913
 
        obj.key = key;
914
 
        if (search_label_glob_based (NULL, value, &obj)) {
915
 
                if (pattern)
916
 
                        g_pattern_spec_free (pattern);
917
 
                return TRUE;
918
 
        }
919
 
        if (pattern)
920
 
                g_pattern_spec_free (pattern);
921
 
        /*
922
 
          Search key after stripping space
923
 
        */
924
 
        g_print ("Search key after stripping space\n");
925
 
        if (search_obj_after_stripping_space (key, NULL, value))
926
 
                return TRUE;
927
 
 
928
 
        g_sprintf (msg, "Object definition %s not match", key);
929
 
        g_print ("%s\n", msg);
930
 
        return FALSE;
931
 
}
932
 
 
933
 
/*
934
 
  Get window definition
935
 
*/
936
 
GHashTable*
937
 
get_object_def (GHashTable *ht, gchar *context, gchar *parent, FILE *log_fp, gboolean obj_is_window)
938
 
{
939
 
        OBJInfo obj;
940
 
        gchar msg [256];
941
 
        LDTPErrorCode err;
942
 
        GPatternSpec *pattern  = NULL;
943
 
        GHashTable *ht_context = NULL;
944
 
 
945
 
        if (ht == NULL) {
946
 
                err = LDTP_ERROR_APPMAP_NOT_INITIALIZED;
947
 
                g_print ("%s - %d - %s\n", __FILE__, __LINE__, ldtp_error_get_message (err));
948
 
                log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (err), log_fp);
949
 
                return NULL;
950
 
        }
951
 
        if (context == NULL) {
952
 
                err = LDTP_ERROR_ARGUMENT_NULL;
953
 
                g_print ("%s\n", ldtp_error_get_message (err));
954
 
                log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (err), log_fp);
955
 
                return NULL;
956
 
        }
957
 
        /*
958
 
          Search given context in hash table
959
 
        */
960
 
        if (g_utf8_strchr (context, -1, '#')) {
961
 
                /*
962
 
                  Search key based on object index
963
 
                  ex: btn#1, mnu#1
964
 
                */
965
 
                g_print ("Search based on index: %s\n", context);
966
 
                obj.key = context;
967
 
                obj.parent_name = parent;
968
 
                obj.obj_is_window = obj_is_window;
969
 
                ht_context = g_hash_table_find (ht, search_obj_based_on_index, &obj);
970
 
        }
971
 
        if (!ht_context) {
972
 
                g_print ("Search based on key: %s\n", context);
973
 
                obj.key = context;
974
 
                obj.parent_name = parent;
975
 
                obj.obj_is_window = obj_is_window;
976
 
                ht_context = g_hash_table_find (ht, search_key_based, &obj);
977
 
        }
978
 
        if (!ht_context) {
979
 
                /*
980
 
                  Search key based on glob expression
981
 
                */
982
 
                g_print ("Search key based on glob expression: %s - %d %s %d\n", context, obj_is_window, __FILE__, __LINE__);
983
 
                if (context)
984
 
                        pattern = g_pattern_spec_new ((const gchar *)context);
985
 
 
986
 
                obj.pattern = pattern;
987
 
                obj.key = context;
988
 
                obj.parent_name = parent;
989
 
                obj.obj_is_window = obj_is_window;
990
 
                ht_context = g_hash_table_find (ht, search_key_glob_based, &obj);
991
 
                if (pattern)
992
 
                        g_pattern_spec_free (pattern);
993
 
        }
994
 
        if (!ht_context) {
995
 
                /*
996
 
                  Search label based
997
 
                */
998
 
                OBJInfo obj;
999
 
 
1000
 
                g_print ("Search label based: %s - %d\n", context, obj_is_window);
1001
 
 
1002
 
                obj.pattern = NULL;
1003
 
                obj.key = context;
1004
 
                obj.parent_name = parent;
1005
 
                obj.obj_is_window = obj_is_window;
1006
 
                ht_context = g_hash_table_find (ht, search_label_based, &obj);
1007
 
        }
1008
 
        if (!ht_context) {
1009
 
                /*
1010
 
                  Search label based on glob expression
1011
 
                */
1012
 
                OBJInfo obj;
1013
 
                GPatternSpec *pattern = NULL;
1014
 
 
1015
 
                g_print ("Search label based on glob expression: %s\n", context);
1016
 
                if (context)
1017
 
                        pattern = g_pattern_spec_new (context);
1018
 
 
1019
 
                obj.pattern = pattern;
1020
 
                obj.key = context;
1021
 
                obj.parent_name = parent;
1022
 
                obj.obj_is_window = obj_is_window;
1023
 
                ht_context = g_hash_table_find (ht, search_label_glob_based, &obj);
1024
 
                if (pattern)
1025
 
                        g_pattern_spec_free (pattern);
1026
 
        }
1027
 
        if (!ht_context) {
1028
 
                /*
1029
 
                  Search key after stripping space
1030
 
                */
1031
 
                g_print ("Search key after stripping space: %s\n", context);
1032
 
                obj.key = context;
1033
 
                obj.parent_name = parent;
1034
 
                obj.obj_is_window = obj_is_window;
1035
 
                ht_context = g_hash_table_find (ht, search_obj_after_stripping_space, &obj);
1036
 
        }
1037
 
        if (!ht_context) {
1038
 
                g_sprintf (msg, "Object definition %s not found in appmap", context);
1039
 
                g_print ("%s\n", msg);
1040
 
                log_msg (LDTP_LOG_DEBUG, msg, log_fp);
1041
 
        }
1042
 
        return ht_context;
1043
 
}
1044
 
 
1045
 
static gboolean
1046
 
find_property (gpointer key, gpointer value, gpointer user_data)
1047
 
{
1048
 
        if (!key || !value || !user_data)
1049
 
                return FALSE;
1050
 
        if (ldtp_debug) {
1051
 
                g_print ("User data: %s\n", (gchar *) user_data);
1052
 
                // g_print ("Value: %s\n", (gchar *) value);
1053
 
                g_print ("Key: %s\n", (gchar *) key);
1054
 
        }
1055
 
 
1056
 
        if (g_utf8_collate (key, user_data) == 0)
1057
 
                return TRUE;
1058
 
        return FALSE;
1059
 
}
1060
 
 
1061
 
gchar*
1062
 
get_property (GHashTable *ht, gchar *property, FILE *log_fp)
1063
 
{
1064
 
        gchar *value = NULL;
1065
 
        gchar msg [256];
1066
 
        LDTPErrorCode err;
1067
 
 
1068
 
        if (ht == NULL) {
1069
 
                err = LDTP_ERROR_APPMAP_NOT_INITIALIZED;
1070
 
                g_print ("%s - %d - %s\n", __FILE__, __LINE__, ldtp_error_get_message (err));
1071
 
                log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (err), log_fp);
1072
 
                return NULL;
1073
 
        }
1074
 
        if (property == NULL) {
1075
 
                err = LDTP_ERROR_ARGUMENT_NULL;
1076
 
                g_print ("%s\n", ldtp_error_get_message (err));
1077
 
                log_msg (LDTP_LOG_CAUSE, ldtp_error_get_message (err), log_fp);
1078
 
                return NULL;
1079
 
        }
1080
 
        value = (gchar *) g_hash_table_find (ht, find_property, (gpointer) property);
1081
 
        if (!value) {
1082
 
                g_sprintf (msg, "Property %s not found in appmap", property);
1083
 
                g_print ("%s\n", msg);
1084
 
                log_msg (LDTP_LOG_DEBUG, msg, log_fp);
1085
 
        } else
1086
 
                g_print ("Property: %s - Value: %s\n", property, value);
1087
 
        return value;
1088
 
}
1089
 
 
1090
 
gboolean
1091
 
search_title_based (gpointer key, gpointer value, gpointer user_data)
1092
 
{
1093
 
        gchar *tmp_key = NULL;
1094
 
        if (!key && !user_data)
1095
 
                return FALSE;
1096
 
        if (g_utf8_collate (key, user_data) == 0)
1097
 
                return TRUE;
1098
 
        if (g_pattern_match_simple (key, user_data))
1099
 
                return TRUE;
1100
 
        tmp_key = g_strdup_printf ("*%s", (gchar *)key);
1101
 
        if (tmp_key && g_pattern_match_simple (tmp_key, user_data)) {
1102
 
                g_free (tmp_key);
1103
 
                return TRUE;
1104
 
        }
1105
 
        if (tmp_key)
1106
 
                g_free (tmp_key);
1107
 
        return FALSE;
1108
 
}