1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
4 | * VBoxNetworkFramework class implementation
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include <VBoxNetworkFramework.h>
|
---|
24 | #include <qapplication.h>
|
---|
25 |
|
---|
26 | /* These notifications are used to notify the GUI thread about different
|
---|
27 | * downloading events: Downloading Started, Downloading in Progress,
|
---|
28 | * Downloading Finished, Downloading Error. */
|
---|
29 | enum PostingEvents
|
---|
30 | {
|
---|
31 | PostBeginEventType = QEvent::User + 500,
|
---|
32 | PostDataEventType,
|
---|
33 | PostFinishEventType,
|
---|
34 | PostErrorEventType
|
---|
35 | };
|
---|
36 |
|
---|
37 | class PostBeginEvent : public QEvent
|
---|
38 | {
|
---|
39 | public:
|
---|
40 | PostBeginEvent (int aStatus)
|
---|
41 | : QEvent ((QEvent::Type) PostBeginEventType)
|
---|
42 | , mStatus (aStatus) {}
|
---|
43 |
|
---|
44 | int mStatus;
|
---|
45 | };
|
---|
46 |
|
---|
47 | class PostDataEvent : public QEvent
|
---|
48 | {
|
---|
49 | public:
|
---|
50 | PostDataEvent (const char *aData, ulong aSize)
|
---|
51 | : QEvent ((QEvent::Type) PostDataEventType)
|
---|
52 | , mData (QByteArray().duplicate (aData, aSize)) {}
|
---|
53 |
|
---|
54 | QByteArray mData;
|
---|
55 | };
|
---|
56 |
|
---|
57 | class PostFinishEvent : public QEvent
|
---|
58 | {
|
---|
59 | public:
|
---|
60 | PostFinishEvent()
|
---|
61 | : QEvent ((QEvent::Type) PostFinishEventType) {}
|
---|
62 | };
|
---|
63 |
|
---|
64 | class PostErrorEvent : public QEvent
|
---|
65 | {
|
---|
66 | public:
|
---|
67 | PostErrorEvent (const QString &aInfo)
|
---|
68 | : QEvent ((QEvent::Type) PostErrorEventType)
|
---|
69 | , mInfo (aInfo) {}
|
---|
70 |
|
---|
71 | QString mInfo;
|
---|
72 | };
|
---|
73 |
|
---|
74 | /* This callback is used to handle the request procedure beginning. */
|
---|
75 | void onBegin (const happyhttp::Response *aResponse, void *aUserdata)
|
---|
76 | {
|
---|
77 | VBoxNetworkFramework *obj = static_cast<VBoxNetworkFramework*> (aUserdata);
|
---|
78 | QApplication::postEvent (obj, new PostBeginEvent (aResponse->getstatus()));
|
---|
79 | }
|
---|
80 |
|
---|
81 | /* This callback is used to handle the progress of request procedure. */
|
---|
82 | void onData (const happyhttp::Response*, void *aUserdata,
|
---|
83 | const unsigned char *aData, int aSize)
|
---|
84 | {
|
---|
85 | VBoxNetworkFramework *obj = static_cast<VBoxNetworkFramework*> (aUserdata);
|
---|
86 | QApplication::postEvent (obj, new PostDataEvent ((const char*) aData, aSize));
|
---|
87 | }
|
---|
88 |
|
---|
89 | /* This callback is used to handle the finish event of every request. */
|
---|
90 | void onFinish (const happyhttp::Response*, void *aUserdata)
|
---|
91 | {
|
---|
92 | VBoxNetworkFramework *obj = static_cast<VBoxNetworkFramework*> (aUserdata);
|
---|
93 | QApplication::postEvent (obj, new PostFinishEvent());
|
---|
94 | }
|
---|
95 |
|
---|
96 | bool VBoxNetworkFramework::event (QEvent *aEvent)
|
---|
97 | {
|
---|
98 | switch (aEvent->type())
|
---|
99 | {
|
---|
100 | case PostBeginEventType:
|
---|
101 | {
|
---|
102 | PostBeginEvent *e = static_cast<PostBeginEvent*> (aEvent);
|
---|
103 | emit netBegin (e->mStatus);
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 | case PostDataEventType:
|
---|
107 | {
|
---|
108 | PostDataEvent *e = static_cast<PostDataEvent*> (aEvent);
|
---|
109 | mDataStream.writeRawBytes (e->mData.data(), e->mData.size());
|
---|
110 | emit netData (e->mData);
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 | case PostFinishEventType:
|
---|
114 | {
|
---|
115 | emit netEnd (mDataArray);
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 | case PostErrorEventType:
|
---|
119 | {
|
---|
120 | PostErrorEvent *e = static_cast<PostErrorEvent*> (aEvent);
|
---|
121 | emit netError (e->mInfo);
|
---|
122 | return true;
|
---|
123 | }
|
---|
124 | default:
|
---|
125 | break;
|
---|
126 | }
|
---|
127 |
|
---|
128 | return QObject::event (aEvent);
|
---|
129 | }
|
---|
130 |
|
---|
131 | void VBoxNetworkFramework::postRequest (const QString &aHost,
|
---|
132 | const QString &aUrl)
|
---|
133 | {
|
---|
134 | /* Network requests thread class */
|
---|
135 | class Thread : public QThread
|
---|
136 | {
|
---|
137 | public:
|
---|
138 |
|
---|
139 | Thread (QObject *aProc, const QString &aHost, const QString &aUrl)
|
---|
140 | : mProc (aProc), mHost (aHost), mUrl (aUrl) {}
|
---|
141 |
|
---|
142 | virtual void run()
|
---|
143 | {
|
---|
144 | try
|
---|
145 | {
|
---|
146 | HConnect conn (mHost, 80);
|
---|
147 | conn.setcallbacks (onBegin, onData, onFinish, mProc);
|
---|
148 | const char *headers[] =
|
---|
149 | {
|
---|
150 | "Connection", "close",
|
---|
151 | "Content-type", "application/x-www-form-urlencoded",
|
---|
152 | "Accept", "text/plain",
|
---|
153 | 0
|
---|
154 | };
|
---|
155 |
|
---|
156 | conn.request ("POST", mUrl.ascii(), headers, 0, 0);
|
---|
157 | while (conn.outstanding())
|
---|
158 | conn.pump();
|
---|
159 | }
|
---|
160 | catch (happyhttp::Wobbly &ex)
|
---|
161 | {
|
---|
162 | QApplication::postEvent (mProc, new PostErrorEvent (ex.what()));
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | private:
|
---|
167 |
|
---|
168 | QObject *mProc;
|
---|
169 | QString mHost;
|
---|
170 | QString mUrl;
|
---|
171 | };
|
---|
172 |
|
---|
173 | delete mNetworkThread;
|
---|
174 | mNetworkThread = new Thread (this, aHost, aUrl);
|
---|
175 | mNetworkThread->start();
|
---|
176 | }
|
---|
177 |
|
---|