aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/objects/KvsObject_webView.cpp
blob: f68013ed20b7b07fef05c3de84df78c07b89967a (about) (plain) (blame)
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//=============================================================================
//
//   File : KvsObject_webView.cpp
//   Creation date : Wed Mar 9 23:34:48 CEST 2011
//   by Alessandro Carbone(Noldor)
//
//   This file is part of the KVIrc irc client distribution
//   Copyright (C) 2011 Alessandro Carbone (elfonol at gmail dot com)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================
//

#include "kvi_settings.h"
#ifdef COMPILE_WEBKIT_SUPPORT
#include <QtWebKit/QWebView>
#include <QWebElement>
#include <QPixmap>
#include <QSize>
#include "KvsObject_webView.h"
#include "KvsObject_pixmap.h"
#include "KviError.h"
#include "kvi_debug.h"
#include "KviLocale.h"

/*
	@doc:	webview
	@keyterms:
		An embedded webview widget,
	@title:

	@type:
		class
	@short:
		Provides web support in a widget using webkit.
	@inherits:
		[class]object[/class]
		[class]widget[/class]
	@description:

	@functions:
		!fn: $load()

*/


KVSO_BEGIN_REGISTERCLASS(KvsObject_webView,"webview","widget")
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,load)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,frames)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,firstChild)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,findAll)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,findFirst)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,pushCurrentToStack)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,popCurrentFromStack)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,clearElementStack)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,parentElement)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,nextSibling)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,currentElementTagName)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,moveToQueryResultsAt)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,elementStackCount)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,queryResultsCount)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,getDocumentElement)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,attributeNames)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,attribute)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,makePreview)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,toPlainText)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,setAttribute)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,loadFinishedEvent)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,loadProgressEvent)
	KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,loadStartedEvent)
KVSO_END_REGISTERCLASS(KvsObject_webView)

KVSO_BEGIN_CONSTRUCTOR(KvsObject_webView,KviKvsObject)

KVSO_END_CONSTRUCTOR(KvsObject_webView)


KVSO_BEGIN_DESTRUCTOR(KvsObject_webView)
lStack.clear();
KVSO_END_CONSTRUCTOR(KvsObject_webView)

bool KvsObject_webView::init(KviKvsRunTimeContext * ,KviKvsVariantList *)
{
    SET_OBJECT(QWebView);
    // fixme: this is for testing purpose only
    ((QWebView *)widget())->settings()->setAttribute(QWebSettings::PluginsEnabled,true);
    //
    connect(((QWebView *)widget()),SIGNAL(loadStarted()),this,SLOT(slotLoadStarted()));
    connect(((QWebView *)widget()),SIGNAL(loadFinished(bool)),this,SLOT(slotLoadFinished(bool)));
    connect(((QWebView *)widget()),SIGNAL(loadProgress(int)),this,SLOT(slotLoadProgress(int)));
    return true;
}
void KvsObject_webView::getFrames(QWebFrame *pFrame,KviKvsArray *pArray, kvs_uint_t &uIdx)
{
    m_dictFrames.insert(pFrame->title(),pFrame);
    pArray->set(uIdx,new KviKvsVariant(pFrame->title()));
    uIdx++;
    QList<QWebFrame *> lFrames = pFrame->childFrames();
    for(int i=0;i<lFrames.count();i++)
    {
	QWebFrame *pChildFrame = lFrames.at(i);
	if (pChildFrame->childFrames().count()) getFrames(pChildFrame,pArray,uIdx);
	else
	{
	    pArray->set(uIdx,new KviKvsVariant(pChildFrame->title()));
	    uIdx++;
	}
    }
}

KVSO_CLASS_FUNCTION(webView,load)
{
    CHECK_INTERNAL_POINTER(widget())
    QString szUrl;
    KVSO_PARAMETERS_BEGIN(c)
    	KVSO_PARAMETER("text",KVS_PT_STRING,0,szUrl)
    KVSO_PARAMETERS_END(c)
    ((QWebView *)widget())->load(QUrl(szUrl));
    return true;
}
KVSO_CLASS_FUNCTION(webView,makePreview)
{
    CHECK_INTERNAL_POINTER(widget())
    QSize size=((QWebView *)widget())->page()->mainFrame()->contentsSize();
    qDebug("contents size %d - %d",size.width(),size.height());
    QImage image(212,142, QImage::Format_RGB32);
    QWebFrame  *pFrame=((QWebView *)widget())->page()->mainFrame();
    QPainter painter(&image);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setRenderHint(QPainter::TextAntialiasing, true);
    painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
    painter.setClipRect(0,0,size.width(),768);
    painter.scale(0.20,0.34);
    pFrame->documentElement().render(&painter);
    painter.end();
    QPixmap *pPixmap=new QPixmap();
    *pPixmap=pPixmap->fromImage(image);
    KviKvsObjectClass * pClass = KviKvsKernel::instance()->objectController()->lookupClass("pixmap");
    KviKvsVariantList params;
    KviKvsObject * pObject = pClass->allocateInstance(0,"internalpixmap",c->context(),&params);
    ((KvsObject_pixmap *)pObject)->setInternalPixmap(pPixmap);
    c->returnValue()->setHObject(pObject->handle());
    return true;
}
KVSO_CLASS_FUNCTION(webView,findAll)
{
    CHECK_INTERNAL_POINTER(widget())
    QString szQuery;
    KVSO_PARAMETERS_BEGIN(c)
    	KVSO_PARAMETER("query",KVS_PT_STRING,0,szQuery)
    KVSO_PARAMETERS_END(c)
    m_webElementCollection=m_currentElement.findAll(szQuery);
    return true;
}
KVSO_CLASS_FUNCTION(webView,findFirst)
{
    QString szQuery;
    KVSO_PARAMETERS_BEGIN(c)
    	KVSO_PARAMETER("query",KVS_PT_STRING,0,szQuery)
    KVSO_PARAMETERS_END(c)
    QWebElement tempElement=m_currentElement.findFirst(szQuery);
    if(!tempElement.isNull())
    {
	m_currentElement=tempElement;
	c->returnValue()->setBoolean(true);
    }
    else c->returnValue()->setBoolean(false);

    return true;
}
KVSO_CLASS_FUNCTION(webView,moveToQueryResultsAt)
{
    CHECK_INTERNAL_POINTER(widget())
    kvs_int_t iIdx;
    KVSO_PARAMETERS_BEGIN(c)
    	KVSO_PARAMETER("index",KVS_PT_INTEGER,0,iIdx)
    KVSO_PARAMETERS_END(c)
    m_currentElement=m_webElementCollection.at(iIdx);
    return true;
}
KVSO_CLASS_FUNCTION(webView,elementStackCount)
{
    CHECK_INTERNAL_POINTER(widget())
    c->returnValue()->setInteger((kvs_int_t) lStack.count());
    return true;
}
KVSO_CLASS_FUNCTION(webView,clearElementStack)
{
    Q_UNUSED(c);
    lStack.clear();
    return true;
}

KVSO_CLASS_FUNCTION(webView,queryResultsCount)
{
    CHECK_INTERNAL_POINTER(widget())
    c->returnValue()->setInteger((kvs_int_t) m_webElementCollection.count());
    return true;
}
KVSO_CLASS_FUNCTION(webView,pushCurrentToStack)
{
    Q_UNUSED(c);
    lStack.append(m_currentElement);
    return true;
}
KVSO_CLASS_FUNCTION(webView,popCurrentFromStack)
{
    if(!lStack.isEmpty())
	m_currentElement = lStack.takeLast();
    else c->warning(__tr2qs_ctx("Stack is empty","objects"));
    return true;
}
KVSO_CLASS_FUNCTION(webView,frames)
{
    CHECK_INTERNAL_POINTER(widget())
    m_dictFrames.clear();
    QWebFrame *pFrame=((QWebView *)widget())->page()->mainFrame();
    KviKvsArray *pArray=new KviKvsArray();
    kvs_uint_t uIdx=0;
    getFrames(pFrame,pArray,uIdx);
    c->returnValue()->setArray(pArray);
    return true;
}
KVSO_CLASS_FUNCTION(webView,currentElementTagName)
{
    CHECK_INTERNAL_POINTER(widget())
    if(m_currentElement.isNull())
    {
	c->warning(__tr2qs_ctx("Document element is null: you must call getDocumentElement first","objects"));
	return true;
    }
    c->returnValue()->setString(m_currentElement.tagName());
    return true;
}

KVSO_CLASS_FUNCTION(webView,setAttribute)
{
    CHECK_INTERNAL_POINTER(widget())
    QString szName,szValue;
    KVSO_PARAMETERS_BEGIN(c)
    	KVSO_PARAMETER("name",KVS_PT_NONEMPTYSTRING,0,szName)
	KVSO_PARAMETER("value",KVS_PT_STRING,0,szValue)
    KVSO_PARAMETERS_END(c)
    m_currentElement.setAttribute(szName,szValue);
    return true;
}
KVSO_CLASS_FUNCTION(webView,attribute)
{
    CHECK_INTERNAL_POINTER(widget())
    QString szName;
    KVSO_PARAMETERS_BEGIN(c)
    	KVSO_PARAMETER("name",KVS_PT_NONEMPTYSTRING,0,szName)
    KVSO_PARAMETERS_END(c)
    c->returnValue()->setString(m_currentElement.attribute(szName));
    return true;
}
KVSO_CLASS_FUNCTION(webView,attributeNames)
{
    CHECK_INTERNAL_POINTER(widget())
    if(m_currentElement.isNull())
    {
	c->warning(__tr2qs_ctx("Document element is null: you must call getDocumentElement first","objects"));
	return true;
    }
    QString szAttributeNames;
    szAttributeNames=m_currentElement.attributeNames().join(",");
    c->returnValue()->setString(szAttributeNames);
    return true;
}

KVSO_CLASS_FUNCTION(webView,toPlainText)
{
    CHECK_INTERNAL_POINTER(widget())
    if(m_currentElement.isNull())
    {
	c->warning(__tr2qs_ctx("Document element is null: you must call getDocumentElement first","objects"));
	return true;
    }
    c->returnValue()->setString(m_currentElement.toPlainText());
    return true;
}

KVSO_CLASS_FUNCTION(webView,firstChild)
{
    CHECK_INTERNAL_POINTER(widget())
    if(m_currentElement.isNull())
    {
	c->warning(__tr2qs_ctx("Document element is null: you must call getDocumentElement first","objects"));
	return true;
    }
    if (!m_currentElement.firstChild().isNull())
    {
	m_currentElement=m_currentElement.firstChild();
	c->returnValue()->setBoolean(true);
	return true;
    }
    c->returnValue()->setBoolean(false);
    return true;
}
KVSO_CLASS_FUNCTION(webView,parentElement)
{
    CHECK_INTERNAL_POINTER(widget())
    if(m_currentElement.isNull())
    {
	c->warning(__tr2qs_ctx("Document element is null: you must call getDocumentElement first","objects"));
	return true;
    }
    QWebElement element=m_currentElement.parent();
    if (!element.isNull())
    {
	m_currentElement=element;
	c->returnValue()->setBoolean(true);
	return true;
    }
    c->returnValue()->setBoolean(false);
    return true;
}
KVSO_CLASS_FUNCTION(webView,nextSibling)
{
    CHECK_INTERNAL_POINTER(widget())
    if(m_currentElement.isNull())
    {
	c->warning(__tr2qs_ctx("Document element is null: you must call getDocumentElement first","objects"));
	return true;
    }
    QWebElement element=m_currentElement.nextSibling();
    if (!element.isNull())
    {
	m_currentElement=element;
	c->returnValue()->setBoolean(true);
	return true;
    }
    c->returnValue()->setBoolean(false);
    return true;
}


KVSO_CLASS_FUNCTION(webView,getDocumentElement)
{
    CHECK_INTERNAL_POINTER(widget())
    QString szFrameName;
    KVSO_PARAMETERS_BEGIN(c)
	    KVSO_PARAMETER("frame_name",KVS_PT_STRING,KVS_PF_OPTIONAL,szFrameName)
    KVSO_PARAMETERS_END(c)
    QWebFrame *pFrame;
    if(szFrameName.isEmpty())
	pFrame=((QWebView *)widget())->page()->mainFrame();
    else{
	pFrame=m_dictFrames.value(szFrameName);
	if(!pFrame)
	{
	    c->warning(__tr2qs_ctx("Unknown frame '%Q'","objects"),&szFrameName);
	    return true;
	}
    }
    m_currentElement=pFrame->documentElement();
  //  m_documentElement=m_currentElement;
    return true;
}



KVSO_CLASS_FUNCTION(webView,loadFinishedEvent)
{
	emitSignal("loadFinished",c,c->params());
	return true;
}

KVSO_CLASS_FUNCTION(webView,loadProgressEvent)
{
	emitSignal("loadProgress",c,c->params());
	return true;
}
KVSO_CLASS_FUNCTION(webView,loadStartedEvent)
{
	emitSignal("loadStarted",c);
	return true;
}

// slots
void KvsObject_webView::slotLoadFinished(bool bOk)
{
	if (bOk)
	    m_currentElement=((QWebView *)widget())->page()->mainFrame()->documentElement();
	KviKvsVariantList params(new KviKvsVariant(bOk));
	callFunction(this,"loadFinishedEvent",&params);
}

void KvsObject_webView::slotLoadStarted()
{
	KviKvsVariantList *lParams=0;
	callFunction(this,"loadStartedEvent",lParams);
}
void KvsObject_webView::slotLoadProgress(int iProgress)
{
	KviKvsVariantList params(new KviKvsVariant((kvs_int_t) iProgress));
	callFunction(this,"loadProgressEvent" ,&params);
}

#endif