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

« back to all changes in this revision

Viewing changes to src/ldtp-request.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
 
 *    Veerapuram Varadhan <v.varadhan@gmail.com>
7
 
 *
8
 
 * Copyright 2004 - 2006 Novell, Inc.
9
 
 *
10
 
 * This program is free software; you can redistribute it and/or
11
 
 * modify it under the terms of the GNU Lesser General Public
12
 
 * License as published by the Free Software Foundation; either
13
 
 * version 2 of the License, or (at your option) any later version.
14
 
 *
15
 
 * This program is distributed in the hope that it will be useful,
16
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 
 * Lesser General Public License for more details.
19
 
 *
20
 
 * You should have received a copy of the GNU Lesser General Public
21
 
 * License along with this program; if not, write to the
22
 
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23
 
 * Boston, MA 02110, USA.
24
 
 */
25
 
 
26
 
#include <stdio.h>
27
 
#include <sys/types.h>
28
 
#include <glib.h>
29
 
#include <libxml/parser.h>
30
 
 
31
 
#include "ldtp-request.h"
32
 
#include "ldtp-error.h"
33
 
#include "ldtp-logger.h"
34
 
 
35
 
/* Object initialization function for the ldtp request object */
36
 
void
37
 
ldtp_request_init (LDTPRequest* req)
38
 
{
39
 
        req->request_type = 0;
40
 
        req->application  = NULL;       
41
 
        req->request_id   = NULL;
42
 
        req->context      = NULL;
43
 
        req->action_name  = NULL;
44
 
        req->component    = NULL;
45
 
        req->arg_list     = NULL;
46
 
}
47
 
 
48
 
/* Finalize handler for the ldtp request component object */
49
 
void
50
 
ldtp_request_free (LDTPRequest* req, int finalize)
51
 
{
52
 
        if (!req)
53
 
                return;
54
 
        if (req->arg_list) {
55
 
                g_slist_foreach (req->arg_list, (GFunc)g_free, NULL);
56
 
                g_slist_free (req->arg_list);
57
 
                req->arg_list = NULL;
58
 
        }
59
 
 
60
 
        req->request_type = 0;
61
 
 
62
 
        if (req->context) {
63
 
                g_free (req->context);
64
 
                req->context = NULL;
65
 
        }
66
 
  
67
 
        if (req->component) {
68
 
                g_free (req->component);
69
 
                req->component = NULL;
70
 
        }
71
 
 
72
 
        if (req->action_name) {
73
 
                g_free (req->action_name);
74
 
                req->action_name = NULL;
75
 
        }
76
 
 
77
 
        if (req->request_id) {
78
 
                g_free (req->request_id);
79
 
                req->request_id = NULL;
80
 
        }
81
 
        req = NULL;
82
 
}
83
 
 
84
 
/**
85
 
 * ldtp_request_component_new_from_string:
86
 
 *
87
 
 * Creates a new ldtp request component object from request-packet.  
88
 
 *
89
 
 * Return value: A newly-created ldtp request component object.
90
 
 **/
91
 
void
92
 
ldtp_request_fill_request (LDTPRequest* req, gchar* packet, size_t len, LDTPErrorCode* err)
93
 
{
94
 
        guchar *tmp = NULL;
95
 
        xmlDocPtr doc = NULL;
96
 
        xmlNodePtr node = NULL;
97
 
 
98
 
        if (!req || !packet || len <= 0) {
99
 
                *err = LDTP_ERROR_ARGUMENT_NULL;
100
 
                return;
101
 
        }
102
 
 
103
 
        doc = xmlParseMemory (packet, len);
104
 
        if (!doc) {
105
 
                ldtp_log ("Unable to parse XML in memory\n");
106
 
                goto error;
107
 
        }
108
 
        node = xmlDocGetRootElement (doc);
109
 
        if (node == NULL) {
110
 
                ldtp_log ("Empty request\n");
111
 
                goto error;
112
 
        }
113
 
 
114
 
        /* First level we expect GUI object attributes*/
115
 
        node = node->xmlChildrenNode;
116
 
        while (node && xmlIsBlankNode (node)) {
117
 
                node = node->next;
118
 
        }
119
 
 
120
 
        if (node == NULL)
121
 
                goto error;
122
 
 
123
 
        /* Free the previous contents */
124
 
        ldtp_request_free (req, 0);
125
 
 
126
 
        /* read the attributes */
127
 
        for (;node != NULL; node = node->next) {
128
 
                if (!node || !node->name)
129
 
                        continue;
130
 
                /* Read action name*/
131
 
                g_print ("Node: %s\n", node->name);             
132
 
                if(!xmlStrcmp (node->name, (const xmlChar *) "SCRIPT") ){
133
 
                        if (doc && node->xmlChildrenNode) {
134
 
                                req->request_type = LDTP_SCRIPT;
135
 
                        }
136
 
                        node = node -> xmlChildrenNode;
137
 
                }
138
 
                if(!xmlStrcmp (node->name, (const xmlChar *) "APPLICATION") ){
139
 
                        if (doc && node->xmlChildrenNode) {
140
 
                                tmp = xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
141
 
                                if (tmp) {
142
 
                                        req->application = g_strdup ((char *)tmp);
143
 
                                        xmlFree (tmp);
144
 
                                }
145
 
                                else {
146
 
                                        req->application = NULL;
147
 
                                }
148
 
                        }
149
 
                        node = node -> next;
150
 
                }
151
 
                if (!xmlStrcmp (node->name, (const xmlChar *) "ID")) {
152
 
                        if (doc && node->xmlChildrenNode) {
153
 
                                tmp = xmlNodeListGetString (doc, 
154
 
                                                            node->xmlChildrenNode, 1);
155
 
                                if (tmp) {
156
 
                                        req->request_id = g_strdup ((char *)tmp);
157
 
                                        xmlFree (tmp);
158
 
                                }
159
 
                                else
160
 
                                        req->request_id = NULL;
161
 
                                if (req->request_id)
162
 
                                        g_print ("request_id: %s\n", req->request_id);
163
 
                        }
164
 
                }
165
 
                if (!xmlStrcmp (node->name, (const xmlChar *) "ACTION")) {
166
 
                        if (doc && node->xmlChildrenNode) {
167
 
                                tmp = xmlNodeListGetString (doc, 
168
 
                                                            node->xmlChildrenNode, 1);
169
 
                                if (tmp) {
170
 
                                        req->action_name = g_strdup ((char *)tmp);
171
 
                                        xmlFree (tmp);
172
 
                                }
173
 
                                else
174
 
                                        req->action_name = NULL;
175
 
                                if (req->action_name)
176
 
                                        g_print ("action_name: %s\n", req->action_name);
177
 
                        }
178
 
                }
179
 
                if (!xmlStrcmp (node->name, (const xmlChar *) "CONTEXT")) {
180
 
                        if (doc && node->xmlChildrenNode) {
181
 
                                tmp = xmlNodeListGetString (doc, 
182
 
                                                            node->xmlChildrenNode, 1);
183
 
                                if (tmp) {
184
 
                                        req->context = g_strdup ((char *)tmp);
185
 
                                        xmlFree (tmp);
186
 
                                }
187
 
                                else
188
 
                                        req->context = NULL;
189
 
                                if (req->context)
190
 
                                        g_print ("Window name: %s\n", req->context);
191
 
                        }
192
 
                }
193
 
                if (!xmlStrcmp (node->name, (const xmlChar *) "COMPONENT")) {
194
 
                        if (doc && node->xmlChildrenNode) {
195
 
                                tmp = xmlNodeListGetString (doc, 
196
 
                                                            node->xmlChildrenNode, 1);
197
 
                                if (tmp) {
198
 
                                        req->component = g_strdup ((char *)tmp);
199
 
                                        xmlFree (tmp);
200
 
                                }
201
 
                                else
202
 
                                        req->component = NULL;
203
 
                                if (req->component)
204
 
                                        g_print ("Component name: %s\n", req->component);
205
 
                        }
206
 
                }
207
 
                if (!xmlStrcmp (node->name, (const xmlChar *) "ARGUMENTS")) {
208
 
                        /* next level is a list of arguments */
209
 
                        node = node->xmlChildrenNode;
210
 
                        g_print ("Has children\n");
211
 
                }
212
 
                /*
213
 
                  If ARGUMENTS does not have any children, then the node will be empty
214
 
                  and node will be NULL. So its better to break the loop
215
 
                  NOTE: A better way to do this is, to have a seperate for loop to iterate
216
 
                  children of ARGUMENTS
217
 
                */
218
 
                if (!node) {
219
 
                        break;
220
 
                }
221
 
                if (!xmlStrcmp (node->name, (const xmlChar *) "ARGUMENT")) {
222
 
                        if (doc && node->xmlChildrenNode) {
223
 
                                tmp = (guchar *)xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
224
 
                                if (tmp) {
225
 
                                        req->arg_list = g_slist_prepend (req->arg_list,
226
 
                                                                         (guchar *)g_strdup ((gchar *)tmp));
227
 
                                        xmlFree (tmp);
228
 
                                        if (req->arg_list && req->arg_list->data)
229
 
                                                g_print ("Argument value: %s\n", (char *)req->arg_list->data);
230
 
                                }
231
 
                        }
232
 
                }
233
 
        }
234
 
 
235
 
        if (!req->request_id)
236
 
                goto error;
237
 
 
238
 
        /* Reverse the argument list to get the
239
 
           desired order of arguments.
240
 
        */
241
 
        req->arg_list = g_slist_reverse (req->arg_list);
242
 
        *err = LDTP_ERROR_SUCCESS;
243
 
        goto cleanup;
244
 
 
245
 
 error:
246
 
        *err = LDTP_ERROR_PACKET_INVALID;
247
 
        ldtp_request_free (req, 0);
248
 
 
249
 
 cleanup:
250
 
        xmlCleanupParser ();
251
 
        if (doc)
252
 
                xmlFreeDoc (doc);
253
 
}