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