source: osm/applications/editors/josm/plugins/wmsplugin/webkit-image.cpp@ 19704

Last change on this file since 19704 was 19704, checked in by stoecker, 15 years ago

improved proxy handling (patches by Robert Schedel) and added caching for QT >= 4.5

File size: 4.4 KB
Line 
1/* compile with
2moc webkit-image.cpp >webkit-image.h
3g++ webkit-image.cpp -o webkit-image -lQtCore -lQtWebKit -lQtGui -s -O2
4or under Windows:
5g++ webkit-image.cpp -o webkit-image -lQtCore4 -lQtWebKit4 -lQtGui4 -s O2
6adding the correct directories with -L or -I:
7-I C:\Progra~1\Qt\include -L C:\Progra~1\Qt\lib
8*/
9#include <QtGui/QApplication>
10#include <QtGui/QPainter>
11#include <QtCore/QFile>
12#include <QtCore/QString>
13#include <QtCore/QUrl>
14#include <QtWebKit/QWebPage>
15#include <QtWebKit/QWebFrame>
16#include <QtNetwork/QNetworkProxy>
17#include <QtCore/QProcess>
18#if QT_VERSION >= 0x040500
19#include <QDesktopServices>
20#include <QNetworkDiskCache>
21#endif
22
23/* using mingw to set binary mode */
24#ifdef WIN32
25#include <io.h>
26#include <fcntl.h>
27#define BINARYSTDOUT setmode(fileno(stdout), O_BINARY);
28#else
29#define BINARYSTDOUT
30#endif
31
32class Save : public QObject
33{
34Q_OBJECT
35public:
36 Save(QWebPage *p) : page(p) {};
37
38public slots:
39 void loaded(bool ok)
40 {
41 if(ok)
42 {
43 page->setViewportSize(page->mainFrame()->contentsSize());
44 QImage im(page->viewportSize(), QImage::Format_ARGB32);
45 QPainter painter(&im);
46 page->mainFrame()->render(&painter);
47
48 QFile f;
49 BINARYSTDOUT
50 if(f.open(stdout, QIODevice::WriteOnly|QIODevice::Unbuffered))
51 {
52 if(!im.save(&f, "JPEG"))
53 {
54 im.save(&f, "PNG");
55 }
56 }
57 }
58 emit finish();
59 }
60signals:
61 void finish(void);
62
63private:
64 QWebPage * page;
65};
66
67#include "webkit-image.h"
68
69int main(int argc, char **argv)
70{
71 if(argc != 2)
72 return 20;
73 QString url = QString(argv[1]);
74
75 QApplication a(argc, argv);
76 QWebPage * page = new QWebPage();
77 Save * s = new Save(page);
78
79 QStringList environment = QProcess::systemEnvironment();
80 int idx = environment.indexOf(QRegExp("http_proxy=.*"));
81 if(idx != -1)
82 {
83 QString scheme = "http"; // default
84 QString proxyHost;
85
86 int proxyPort = 8080;
87 QStringList tmpList = environment.at(idx).split("=");
88
89#if QT_VERSION >= 0x040600 // Qt4.6: Use QUrl::fromUserInput
90 // set URL (and guess if proto scheme missing)
91 QUrl url (QUrl::fromUserInput(tmpList.at(1)));
92#else
93 // set URL
94 QUrl url (tmpList.at(1));
95#endif
96 if (url.isValid() && !url.host().isEmpty())
97 {
98 proxyHost = url.host();
99
100 if (url.port() != -1)
101 proxyPort = url.port();
102
103 if (!url.scheme().isEmpty())
104 scheme = url.scheme();
105
106 if (scheme == "http") // we support only http
107 {
108 QNetworkProxy proxy;
109 proxy.setType(QNetworkProxy::HttpCachingProxy);
110 proxy.setHostName(proxyHost);
111 proxy.setPort(proxyPort);
112 if (!url.userName().isEmpty())
113 proxy.setUser(url.userName());
114 if (!url.password().isEmpty())
115 proxy.setPassword(url.password());
116 page->networkAccessManager()->setProxy(proxy);
117 }
118 }
119 else /* manual mode */
120 {
121 QStringList proto_host_port = tmpList.at(1).split("://");
122 QStringList host_port;
123 if (proto_host_port.size() == 2) // string has proto
124 {
125 scheme = proto_host_port.at(0);
126 host_port = proto_host_port.at(1).split(":");
127 }
128 else // no proto (or invalid format with several delimiters)
129 {
130 host_port = tmpList.at(1).split(":");
131 }
132 if (scheme == "http") // we support only http
133 {
134 proxyHost = host_port.at(0);
135 if(host_port.size() == 2)
136 {
137 bool ok;
138 int port = host_port.at(1).toInt(&ok);
139 if(ok)
140 proxyPort = port;
141 }
142
143 QNetworkProxy proxy;
144 proxy.setType(QNetworkProxy::HttpCachingProxy);
145 proxy.setHostName(proxyHost);
146 proxy.setPort(proxyPort);
147 page->networkAccessManager()->setProxy(proxy);
148 }
149 }
150 }
151
152#if QT_VERSION >= 0x040500
153 QNetworkDiskCache *diskCache = new QNetworkDiskCache(page);
154 QString location = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
155 diskCache->setCacheDirectory(location);
156 page->networkAccessManager()->setCache(diskCache);
157#endif
158
159 QObject::connect(page, SIGNAL(loadFinished(bool)), s, SLOT(loaded(bool)));
160 QObject::connect(s, SIGNAL(finish(void)), &a, SLOT(quit()));
161 /* set some useful defaults for a webpage */
162// page->setViewportSize(QSize(1280,1024));
163// page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
164// page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
165 page->mainFrame()->load (QUrl(url));
166 return a.exec();
167}
Note: See TracBrowser for help on using the repository browser.