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