1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2001-2003, Ximian, Inc. |
---|
4 | */ |
---|
5 | |
---|
6 | #include <ctype.h> |
---|
7 | #include <errno.h> |
---|
8 | #include <fcntl.h> |
---|
9 | #include <stdio.h> |
---|
10 | #include <stdlib.h> |
---|
11 | #include <string.h> |
---|
12 | #include <sys/stat.h> |
---|
13 | #include <unistd.h> |
---|
14 | |
---|
15 | #include <libsoup/soup.h> |
---|
16 | #include <libsoup/soup-soap-message.h> |
---|
17 | #include <libsoup/soup-soap-response.h> |
---|
18 | |
---|
19 | SoupSession *session; |
---|
20 | GMainLoop *loop; |
---|
21 | |
---|
22 | static void |
---|
23 | got_response (SoupMessage *msg, gpointer user_data) |
---|
24 | { |
---|
25 | SoupSoapResponse *response; |
---|
26 | SoupSoapParameter *param, *subparam; |
---|
27 | char *word, *dict, *def; |
---|
28 | int count = 0; |
---|
29 | |
---|
30 | if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) { |
---|
31 | fprintf (stderr, "%d %s\n", msg->status_code, msg->reason_phrase); |
---|
32 | exit (1); |
---|
33 | } |
---|
34 | |
---|
35 | response = soup_soap_message_parse_response (SOUP_SOAP_MESSAGE (msg)); |
---|
36 | if (!response) { |
---|
37 | fprintf (stderr, "Could not parse SOAP response\n"); |
---|
38 | exit (1); |
---|
39 | } |
---|
40 | |
---|
41 | param = soup_soap_response_get_first_parameter_by_name (response, "DefineResult"); |
---|
42 | if (!param) { |
---|
43 | fprintf (stderr, "Could not find result in SOAP response\n"); |
---|
44 | exit (1); |
---|
45 | } |
---|
46 | |
---|
47 | param = soup_soap_parameter_get_first_child_by_name (param, "Definitions"); |
---|
48 | if (!param) |
---|
49 | goto done; |
---|
50 | |
---|
51 | for (param = soup_soap_parameter_get_first_child_by_name (param, "Definition"); |
---|
52 | param; |
---|
53 | param = soup_soap_parameter_get_next_child_by_name (param, "Definition")) { |
---|
54 | subparam = soup_soap_parameter_get_first_child_by_name (param, "Word"); |
---|
55 | if (!subparam) |
---|
56 | continue; |
---|
57 | word = soup_soap_parameter_get_string_value (subparam); |
---|
58 | |
---|
59 | subparam = soup_soap_parameter_get_first_child_by_name (param, "Dictionary"); |
---|
60 | if (subparam) |
---|
61 | subparam = soup_soap_parameter_get_first_child_by_name (subparam, "Name"); |
---|
62 | if (subparam) |
---|
63 | dict = soup_soap_parameter_get_string_value (subparam); |
---|
64 | else |
---|
65 | dict = NULL; |
---|
66 | |
---|
67 | printf ("% 2d. %s (%s):\n", ++count, word, dict); |
---|
68 | g_free (word); |
---|
69 | g_free (dict); |
---|
70 | |
---|
71 | subparam = soup_soap_parameter_get_first_child_by_name (param, "WordDefinition"); |
---|
72 | if (subparam) { |
---|
73 | def = soup_soap_parameter_get_string_value (subparam); |
---|
74 | printf ("%s\n", def); |
---|
75 | g_free (def); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | done: |
---|
80 | if (count == 0) |
---|
81 | printf ("No definition\n"); |
---|
82 | |
---|
83 | g_object_unref (response); |
---|
84 | g_main_quit (loop); |
---|
85 | } |
---|
86 | |
---|
87 | static void |
---|
88 | usage (void) |
---|
89 | { |
---|
90 | fprintf (stderr, "Usage: dict [-p proxy_uri] WORD\n"); |
---|
91 | exit (1); |
---|
92 | } |
---|
93 | |
---|
94 | int |
---|
95 | main (int argc, char **argv) |
---|
96 | { |
---|
97 | SoupUri *proxy = NULL; |
---|
98 | SoupSoapMessage *msg; |
---|
99 | int opt; |
---|
100 | |
---|
101 | g_type_init (); |
---|
102 | g_thread_init (NULL); |
---|
103 | |
---|
104 | while ((opt = getopt (argc, argv, "p:")) != -1) { |
---|
105 | switch (opt) { |
---|
106 | case 'p': |
---|
107 | proxy = soup_uri_new (optarg); |
---|
108 | if (!proxy) { |
---|
109 | fprintf (stderr, "Could not parse %s as URI\n", |
---|
110 | optarg); |
---|
111 | exit (1); |
---|
112 | } |
---|
113 | break; |
---|
114 | |
---|
115 | case '?': |
---|
116 | usage (); |
---|
117 | break; |
---|
118 | } |
---|
119 | } |
---|
120 | argc -= optind; |
---|
121 | argv += optind; |
---|
122 | |
---|
123 | if (argc != 1) |
---|
124 | usage (); |
---|
125 | |
---|
126 | session = soup_session_async_new_with_options ( |
---|
127 | SOUP_SESSION_PROXY_URI, proxy, |
---|
128 | NULL); |
---|
129 | |
---|
130 | msg = soup_soap_message_new ("POST", |
---|
131 | "http://services.aonaware.com/DictService/DictService.asmx", |
---|
132 | FALSE, NULL, NULL, NULL); |
---|
133 | if (!msg) { |
---|
134 | fprintf (stderr, "Could not create web service request\n"); |
---|
135 | exit (1); |
---|
136 | } |
---|
137 | |
---|
138 | soup_message_add_header (SOUP_MESSAGE (msg)->request_headers, |
---|
139 | "SOAPAction", "http://services.aonaware.com/webservices/Define"); |
---|
140 | |
---|
141 | soup_soap_message_start_envelope (msg); |
---|
142 | soup_soap_message_start_body (msg); |
---|
143 | |
---|
144 | soup_soap_message_start_element (msg, "Define", NULL, |
---|
145 | "http://services.aonaware.com/webservices/"); |
---|
146 | soup_soap_message_add_namespace (msg, NULL, "http://services.aonaware.com/webservices/"); |
---|
147 | soup_soap_message_start_element (msg, "word", NULL, NULL); |
---|
148 | soup_soap_message_write_string (msg, argv[0]); |
---|
149 | soup_soap_message_end_element (msg); |
---|
150 | soup_soap_message_end_element (msg); |
---|
151 | |
---|
152 | soup_soap_message_end_body (msg); |
---|
153 | soup_soap_message_end_envelope (msg); |
---|
154 | soup_soap_message_persist (msg); |
---|
155 | |
---|
156 | soup_session_queue_message (session, SOUP_MESSAGE (msg), |
---|
157 | got_response, NULL); |
---|
158 | |
---|
159 | loop = g_main_loop_new (NULL, TRUE); |
---|
160 | g_main_run (loop); |
---|
161 | g_main_loop_unref (loop); |
---|
162 | |
---|
163 | return 0; |
---|
164 | } |
---|