summaryrefslogtreecommitdiffstats
path: root/qtsoap/doc/html/google-example.html
blob: 688ea68f7309ad398a49586b1407e8a0b05a7bfb (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- google.qdoc -->
<head>
  <title>Using SOAP with the Google API</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="57" height="67" border="0" /></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a></td>
</tr></table><h1 class="title">Using SOAP with the Google API<br /><span class="subtitle"></span>
</h1>
<p>This example shows how to use the SOAP component to submit searches via the Google SOAP API.</p>
<p>Testing this example requires a valid license key, which is available for free from http://api.google.com/.</p>
    <hr>
    <p>examples/google/google.h:</p>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
 ** Contact: http://www.qt-project.org/legal
 **
 ** This file is part of the Qt Solutions component.
 **
 ** You may use this file under the terms of the BSD license as follows:
 **
 ** &quot;Redistribution and use in source and binary forms, with or without
 ** modification, are permitted provided that the following conditions are
 ** met:
 **   * Redistributions of source code must retain the above copyright
 **     notice, this list of conditions and the following disclaimer.
 **   * Redistributions in binary form must reproduce the above copyright
 **     notice, this list of conditions and the following disclaimer in
 **     the documentation and/or other materials provided with the
 **     distribution.
 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
 **     the names of its contributors may be used to endorse or promote
 **     products derived from this software without specific prior written
 **     permission.
 **
 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
 **
 ****************************************************************************/</span>

 #ifndef GOOGLE_H_INCLUDED
 #define GOOGLE_H_INCLUDED
 #include &lt;QtGui/QWidget&gt;
 #include &lt;qtsoap.h&gt;

 class QLineEdit;
 class QTextBrowser;
 class QPushButton;

 class Google : public QWidget
 {
     Q_OBJECT
 public:
     Google(QWidget *parent = 0);

 private slots:
     void submitRequest();
     void getResponse();

 private:
     QtSoapHttpTransport http;

     QLineEdit *searchString;
     QLineEdit *googleKey;
     QTextBrowser *resultView;
     QPushButton *quitButton;
     QPushButton *searchButton;
     QString key;
 };

 #endif</pre>
    <hr>
    <p>examples/google/google.cpp:</p>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
 ** Contact: http://www.qt-project.org/legal
 **
 ** This file is part of the Qt Solutions component.
 **
 ** You may use this file under the terms of the BSD license as follows:
 **
 ** &quot;Redistribution and use in source and binary forms, with or without
 ** modification, are permitted provided that the following conditions are
 ** met:
 **   * Redistributions of source code must retain the above copyright
 **     notice, this list of conditions and the following disclaimer.
 **   * Redistributions in binary form must reproduce the above copyright
 **     notice, this list of conditions and the following disclaimer in
 **     the documentation and/or other materials provided with the
 **     distribution.
 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
 **     the names of its contributors may be used to endorse or promote
 **     products derived from this software without specific prior written
 **     permission.
 **
 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
 **
 ****************************************************************************/</span>

 #include &lt;QtGui/QApplication&gt;
 #include &lt;QtGui/QMessageBox&gt;
 #include &lt;QtGui/QCursor&gt;
 #include &lt;QtCore/QFile&gt;
 #include &lt;QtGui/QLabel&gt;
 #include &lt;QtGui/QLineEdit&gt;
 #include &lt;QtGui/QTextBrowser&gt;
 #include &lt;QtGui/QLayout&gt;
 #include &lt;QtGui/QPushButton&gt;
 #include &quot;google.h&quot;

 Google::Google(QWidget *parent)
     : QWidget(parent), http(this)
 {
     <span class="comment">// Generate the GUI.</span>
     quitButton = new QPushButton(&quot;&amp;Quit&quot;, this);
     searchString = new QLineEdit(this);
     resultView = new QTextBrowser(this);
     QVBoxLayout *layout = new QVBoxLayout(this);

     QLabel *gLabel = new QLabel(&quot;Google Web APIs license key: &quot;, this);
     googleKey = new QLineEdit(this);
     QHBoxLayout *topBar = new QHBoxLayout();
     topBar-&gt;addWidget(gLabel);
     topBar-&gt;addWidget(googleKey);
     layout-&gt;addLayout(topBar);

     QLabel *sLabel = new QLabel(&quot;Search string: &quot;, this);
     QPushButton *searchButton = new QPushButton(&quot;Search!&quot;, this);
     QHBoxLayout *searchBar = new QHBoxLayout();
     searchBar-&gt;addWidget(sLabel);
     searchBar-&gt;addWidget(searchString);
     searchBar-&gt;addWidget(searchButton);
     layout-&gt;addLayout(searchBar);

     layout-&gt;addWidget(resultView);
     layout-&gt;addWidget(quitButton);

     <span class="comment">// Connect signals to slots. Note the submitRequest() slots.</span>
     connect(&amp;http, SIGNAL(responseReady()), SLOT(getResponse()));
     connect(quitButton, SIGNAL(clicked()), SLOT(close()));
     connect(searchString, SIGNAL(returnPressed()), SLOT(submitRequest()));
     connect(searchButton, SIGNAL(clicked()), SLOT(submitRequest()));
     connect(googleKey, SIGNAL(returnPressed()), SLOT(submitRequest()));

     <span class="comment">// Pressing enter should trigger a search, unless focus has been</span>
     <span class="comment">// explicitly moved.</span>
     searchButton-&gt;setDefault(true);

     <span class="comment">// Prepare to submit request.</span>
     http.setAction(&quot;urn:GoogleSearchAction&quot;);
     http.setHost(&quot;api.google.com&quot;);
 }

 void Google::submitRequest()
 {
     <span class="comment">// Check that the license key is set.</span>
     if (googleKey-&gt;text() == &quot;&quot;) {
         QMessageBox::warning(this, tr(&quot;Missing license key&quot;),
                              tr(&quot;Please enter your Google Web APIs license key.&quot;
                                 &quot; If you do not have one, you can visit&lt;br&gt;&quot;
                                 &quot; http:<span class="comment">//api.google.com/ and create a Google&quot;</span>
                                 &quot; account to obtain a license key.&quot;));
         return;
     }

     <span class="comment">// Check that we have a search string.</span>
     if (searchString-&gt;text() == &quot;&quot;) {
         QMessageBox::warning(this, tr(&quot;Missing search string&quot;),
                              tr(&quot;Please enter a search string.&quot;));
         return;

     }

     <span class="comment">// Generate request. Details about how to generate a proper</span>
     <span class="comment">// request are available from http://api.google.com/. The results</span>
     <span class="comment">// are always fetched as latin1 in this example, but this can be</span>
     <span class="comment">// easily changed (see the &quot;ie&quot; and &quot;oe&quot; arguments below).</span>
     QtSoapMessage request;
     request.setMethod(QtSoapQName(&quot;doGoogleSearch&quot;, &quot;urn:GoogleSearch&quot;));
     request.addMethodArgument(&quot;key&quot;, &quot;&quot;, googleKey-&gt;text());
     request.addMethodArgument(&quot;q&quot;, &quot;&quot;, searchString-&gt;text());
     request.addMethodArgument(&quot;start&quot;, &quot;&quot;, 0);
     request.addMethodArgument(&quot;maxResults&quot;, &quot;&quot;, 10);
     request.addMethodArgument(&quot;filter&quot;, &quot;&quot;, false, 0);
     request.addMethodArgument(&quot;restrict&quot;, &quot;&quot;, &quot;&quot;);
     request.addMethodArgument(&quot;safeSearch&quot;, &quot;&quot;, false, 0);
     request.addMethodArgument(&quot;lr&quot;, &quot;&quot;, &quot;&quot;);
     request.addMethodArgument(&quot;ie&quot;, &quot;&quot;, &quot;latin1&quot;);
     request.addMethodArgument(&quot;oe&quot;, &quot;&quot;, &quot;latin1&quot;);

     <span class="comment">// Submit the method request to the web service.</span>
     http.submitRequest(request, &quot;/search/beta2&quot;);

     <span class="comment">// Set the cursor to wait mode.</span>
     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
 }

 void Google::getResponse()
 {
     <span class="comment">// Set cursor back to normal shape.</span>
     QApplication::restoreOverrideCursor();

     <span class="comment">// Reset resultView.</span>
     resultView-&gt;clear();

     <span class="comment">// Get the response, check for error.</span>
     const QtSoapMessage &amp;resp = http.getResponse();
     if (resp.isFault()) {
         resultView-&gt;setHtml(tr(&quot;&lt;b&gt;Query failed&lt;/b&gt;: &quot;)
                             + resp.faultString().value().toString());
         return;
     }

     <span class="comment">// Extract the return value from this method response, check for</span>
     <span class="comment">// errors.</span>
     const QtSoapType &amp;res = resp.returnValue();
     if (!res.isValid()) {
         resultView-&gt;append(tr(&quot;Invalid return value&quot;));
         return;
     }

     <span class="comment">// Generate resultView output. Make it resemble the actual web</span>
     <span class="comment">// output from http://www.google.com/.</span>
     QString header(tr(&quot;Searched the web for &lt;b&gt;%1&lt;/b&gt;, results %2 - %3 &quot;
                       &quot;of about %4. Search took %5 seconds.&lt;br&gt;&lt;hr&gt;&quot;)
                    .arg(res[&quot;searchQuery&quot;].toString())
                    .arg(res[&quot;startIndex&quot;].toInt())
                    .arg(res[&quot;endIndex&quot;].toInt())
                    .arg(res[&quot;estimatedTotalResultsCount&quot;].toInt())
                    .arg(res[&quot;searchTime&quot;].value().toDouble(), 0, 'f', 2));

     const QtSoapType &amp;resultElements = res[&quot;resultElements&quot;];
     QString allElements;

     for (int i = 0; i &lt; resultElements.count(); ++i) {
         const QtSoapType &amp;resultElement = res[&quot;resultElements&quot;][i];

         QString cat = resultElement[&quot;directoryCategory&quot;][&quot;fullViewableName&quot;].toString();
         QString summary = resultElement[&quot;summary&quot;].toString();
         QString title = resultElement[&quot;title&quot;].toString();
         QString snippet = resultElement[&quot;snippet&quot;].toString();
         QString url = resultElement[&quot;URL&quot;].toString();
         QString cachedSize = resultElement[&quot;cachedSize&quot;].toString();

         QString thisElement = &quot;&lt;br&gt;&quot;;

         if (!title.isEmpty()) {
             thisElement += &quot;&lt;font color=\&quot;#0000FF\&quot;&gt;&lt;b&gt;&lt;u&gt;&quot;
                            + title + &quot;&lt;/u&gt;&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&quot;;
         } else {
             thisElement += &quot;&lt;font color=\&quot;#0000FF\&quot;&gt;&lt;b&gt;&lt;u&gt;&quot;
                            + url + &quot;&lt;/u&gt;&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&quot;;
         }

         if (!snippet.isEmpty())
             thisElement += snippet + &quot;&lt;br&gt;&quot;;

         if (!summary.isEmpty()) {
             thisElement += &quot;&lt;font color=\&quot;#808080\&quot;&gt;Description:&lt;/font&gt; &quot;
                            + summary + &quot;&lt;br&gt;&quot;;
         }

         if (!cat.isEmpty()) {
             thisElement += &quot;&lt;font color=\&quot;#808080\&quot;&gt;Category: &lt;u&gt;&quot;
                            + cat + &quot;&lt;/u&gt;&lt;/font&gt;&lt;br&gt;&quot;;
         }

         if (!title.isEmpty()) {
             thisElement += &quot;&lt;font color=\&quot;#008000\&quot;&gt;&lt;u&gt;&quot; + url
                            + &quot;&lt;/u&gt; - &quot; + cachedSize + &quot;&lt;/font&gt;&lt;br&gt;&quot;;
         }

         allElements += thisElement;
     }

     <span class="comment">// Update the resultView.</span>
     resultView-&gt;setHtml(header + allElements);
 }</pre>
    <hr>
    <p>examples/google/main.cpp:</p>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
 ** Contact: http://www.qt-project.org/legal
 **
 ** This file is part of the Qt Solutions component.
 **
 ** You may use this file under the terms of the BSD license as follows:
 **
 ** &quot;Redistribution and use in source and binary forms, with or without
 ** modification, are permitted provided that the following conditions are
 ** met:
 **   * Redistributions of source code must retain the above copyright
 **     notice, this list of conditions and the following disclaimer.
 **   * Redistributions in binary form must reproduce the above copyright
 **     notice, this list of conditions and the following disclaimer in
 **     the documentation and/or other materials provided with the
 **     distribution.
 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
 **     the names of its contributors may be used to endorse or promote
 **     products derived from this software without specific prior written
 **     permission.
 **
 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
 **
 ****************************************************************************/</span>

 #include &lt;QtGui/QApplication&gt;
 #include &quot;google.h&quot;

 int main(int argc, char **argv)
 {
     QApplication app(argc, argv);

     Google google;

     google.show();

     return app.exec();
 }</pre>
    <hr>
    <p>examples/google/google.pro:</p>
<pre> TEMPLATE = app
 INCLUDEPATH += .

 include(../../src/qtsoap.pri)

 # Input
 HEADERS += google.h
 SOURCES += google.cpp main.cpp</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="40%" align="center"><a href="http://qt.nokia.com/doc/trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt Solutions</div></td>
</tr></table></div></address></body>
</html>