aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/objects
diff options
context:
space:
mode:
authorGravatar Noldor2011-03-13 18:54:48 +0000
committerGravatar Noldor2011-03-13 18:54:48 +0000
commit1607d1c9fae6a754643d9a4b60a3d0b2d1e321c6 (patch)
tree7a2a9b937f8e44d302cd27cba2793c175e93b351 /src/modules/objects
parentbump spanish translation (diff)
downloadKVIrc-1607d1c9fae6a754643d9a4b60a3d0b2d1e321c6.tar.gz
KVIrc-1607d1c9fae6a754643d9a4b60a3d0b2d1e321c6.tar.bz2
KVIrc-1607d1c9fae6a754643d9a4b60a3d0b2d1e321c6.zip
More work on class webview
git-svn-id: https://svn.kvirc.de/svn/trunk/kvirc@5581 17fca916-40b9-46aa-a4ea-0a15b648b75c
Diffstat (limited to 'src/modules/objects')
-rw-r--r--src/modules/objects/KvsObject_webView.cpp137
-rw-r--r--src/modules/objects/KvsObject_webView.h34
2 files changed, 166 insertions, 5 deletions
diff --git a/src/modules/objects/KvsObject_webView.cpp b/src/modules/objects/KvsObject_webView.cpp
index 2a679455b..02981a0b3 100644
--- a/src/modules/objects/KvsObject_webView.cpp
+++ b/src/modules/objects/KvsObject_webView.cpp
@@ -29,7 +29,11 @@
#include <QtWebKit/QWebView>
#include <QWebSettings>
#include <QWebElement>
+#include <QNetworkRequest>
+#include <QNetworkReply>
+
#include <QPixmap>
+#include <QFile>
#include <QSize>
#include "KvsObject_webView.h"
#include "KvsObject_pixmap.h"
@@ -37,6 +41,57 @@
#include "kvi_debug.h"
#include "KviLocale.h"
+static int g_iDownloadId=1;
+KviKvsDownloadHandler::KviKvsDownloadHandler(KvsObject_webView *parent,QFile *pFile,QNetworkReply *pNetReply,int iId)
+:QObject(parent)
+{
+ m_Id = iId;
+ m_pParentScript=parent;
+ m_pReply=pNetReply;
+ m_pFile=pFile;
+ connect(m_pReply,SIGNAL(finished()),this, SLOT(slotReplyFinished()));
+ connect(m_pReply,SIGNAL(readyRead()),this, SLOT(slotReadyRead()));
+ }
+void KviKvsDownloadHandler::slotReplyFinished()
+{
+ KviKvsVariantList params(new KviKvsVariant((kvs_int_t)m_Id));
+ m_pParentScript->callFunction(m_pParentScript,"downloadCompletedEvent",&params);
+ m_pFile->close();
+ delete m_pFile;
+ m_pFile=0;
+ m_pReply->deleteLater();
+ m_pReply=0;
+ this->deleteLater();
+}
+KviKvsDownloadHandler::~KviKvsDownloadHandler()
+{
+ if (m_pFile)
+ {
+ m_pFile->close();
+ delete m_pFile;
+ m_pFile = 0;
+ }
+ if (m_pReply)
+ {
+ delete m_pReply;
+ m_pReply = 0;
+ }
+ }
+void KviKvsDownloadHandler::slotReadyRead()
+{
+ qDebug("Slot ready read");
+ QVariant vSize=m_pReply->header(QNetworkRequest::ContentLengthHeader);
+ if(!vSize.isNull()){
+ bool ok;
+ int isize;
+ isize=vSize.toInt(&ok);
+ qDebug("contents %d",isize);
+ }
+ QByteArray bytes=m_pReply->readAll();
+ KviKvsVariantList params(new KviKvsVariant((kvs_int_t)bytes.count()),new KviKvsVariant((kvs_int_t)m_Id));
+ m_pParentScript->callFunction(m_pParentScript,"downloadProgressEvent",&params);
+ m_pFile->write(bytes);
+}
const char * const webattributes_tbl[] = {
"JavaScriptEnabled",
@@ -99,9 +154,16 @@ KVSO_BEGIN_REGISTERCLASS(KvsObject_webView,"webview","widget")
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,setAttribute)
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,setWebSetting)
KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,removeFromDocument)
+ KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,removeClass)
+ KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,classes)
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_REGISTER_HANDLER_BY_NAME(KvsObject_webView,downloadCompletedEvent)
+ KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,downloadRequestEvent)
+ KVSO_REGISTER_HANDLER_BY_NAME(KvsObject_webView,downloadProgressEvent)
+
KVSO_END_REGISTERCLASS(KvsObject_webView)
KVSO_BEGIN_CONSTRUCTOR(KvsObject_webView,KviKvsObject)
@@ -113,12 +175,18 @@ KVSO_BEGIN_DESTRUCTOR(KvsObject_webView)
qDeleteAll(m_dictCache);
KVSO_END_CONSTRUCTOR(KvsObject_webView)
-bool KvsObject_webView::init(KviKvsRunTimeContext * ,KviKvsVariantList *)
+bool KvsObject_webView::init(KviKvsRunTimeContext *c ,KviKvsVariantList *)
{
SET_OBJECT(QWebView);
+ m_pContext = c;
+ m_pNetworkManager = new QNetworkAccessManager(this);
+ /* m_pReplyList=new KviPointerList<QNetworkReply>;
+ m_pReplyList->setAutoDelete(false);*/
+ QWebPage *pPage = ((QWebView *)widget())->page();
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)));
+ connect(pPage,SIGNAL(downloadRequested(const QNetworkRequest &)),this,SLOT(slotDownloadRequest(const QNetworkRequest &)));
return true;
}
void KvsObject_webView::getFrames(QWebFrame *pFrame,KviKvsArray *pArray, kvs_uint_t &uIdx)
@@ -241,7 +309,9 @@ KVSO_CLASS_FUNCTION(webView,moveToQueryResultsAt)
KVSO_CLASS_FUNCTION(webView,removeFromDocument)
{
Q_UNUSED(c);
+ QWebElement element=m_currentElement.parent();
m_currentElement.removeFromDocument();
+ m_currentElement=element;
return true;
}
@@ -288,6 +358,16 @@ KVSO_CLASS_FUNCTION(webView,setAttribute)
m_currentElement.setAttribute(szName,szValue);
return true;
}
+KVSO_CLASS_FUNCTION(webView,removeClass)
+{
+ CHECK_INTERNAL_POINTER(widget())
+ QString szClassName;
+ KVSO_PARAMETERS_BEGIN(c)
+ KVSO_PARAMETER("class_name",KVS_PT_NONEMPTYSTRING,0,szClassName)
+ KVSO_PARAMETERS_END(c)
+ m_currentElement.removeClass(szClassName);
+ return true;
+}
KVSO_CLASS_FUNCTION(webView,setWebSetting)
{
@@ -336,7 +416,19 @@ KVSO_CLASS_FUNCTION(webView,attributeNames)
c->returnValue()->setString(szAttributeNames);
return true;
}
-
+KVSO_CLASS_FUNCTION(webView,classes)
+{
+ 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 szClasses;
+ szClasses=m_currentElement.classes().join(",");
+ c->returnValue()->setString(szClasses);
+ return true;
+}
KVSO_CLASS_FUNCTION(webView,toPlainText)
{
CHECK_INTERNAL_POINTER(widget())
@@ -444,8 +536,25 @@ KVSO_CLASS_FUNCTION(webView,loadStartedEvent)
emitSignal("loadStarted",c);
return true;
}
+KVSO_CLASS_FUNCTION(webView,downloadCompletedEvent)
+{
+ emitSignal("downloadCompleted",c,c->params());
+ return true;
+}
+
+KVSO_CLASS_FUNCTION(webView,downloadProgressEvent)
+{
+ emitSignal("downloadProgress",c,c->params());
+ return true;
+}
+KVSO_CLASS_FUNCTION(webView,downloadRequestEvent)
+{
+ emitSignal("downloadRequest",c,c->params());
+ return true;
+}
// slots
+
void KvsObject_webView::slotLoadFinished(bool bOk)
{
if (bOk)
@@ -464,6 +573,30 @@ void KvsObject_webView::slotLoadProgress(int iProgress)
KviKvsVariantList params(new KviKvsVariant((kvs_int_t) iProgress));
callFunction(this,"loadProgressEvent" ,&params);
}
+void KvsObject_webView::slotDownloadRequest(const QNetworkRequest &r)
+{
+ QNetworkReply *pReply=m_pNetworkManager->get(r);
+ QString szFilePath="";
+ KviKvsVariant *filepathret=new KviKvsVariant(szFilePath);
+ KviKvsVariantList params(new KviKvsVariant(r.url().toString()));
+ callFunction(this,"downloadRequestEvent",filepathret,&params);
+ filepathret->asString(szFilePath);
+ if (!szFilePath.isEmpty())
+ {
+ QFile *pFile=new QFile(szFilePath);
+ if(!pFile->open(QIODevice::WriteOnly))
+ {
+ m_pContext->warning(__tr2qs_ctx("Invalid file path '%Q'","objects"),&szFilePath);
+ pReply->abort();
+ pReply->deleteLater();
+ return;
+ }
+ KviKvsDownloadHandler *pHandler = new KviKvsDownloadHandler(this,pFile,pReply,g_iDownloadId);
+ Q_UNUSED(pHandler);
+ g_iDownloadId++;
+ }
+}
+
#endif
diff --git a/src/modules/objects/KvsObject_webView.h b/src/modules/objects/KvsObject_webView.h
index 2f7d4da71..1f29dc03b 100644
--- a/src/modules/objects/KvsObject_webView.h
+++ b/src/modules/objects/KvsObject_webView.h
@@ -28,9 +28,13 @@
#include "kvi_settings.h"
#ifdef COMPILE_WEBKIT_SUPPORT
#include "object_macros.h"
+#include <QFile>
+#include <QNetworkAccessManager>
#include <QtWebKit/QWebView>
#include <QtWebKit/QWebFrame>
#include <QWebElement>
+class KviKvsDownloadHandler;
+
class KvsObject_webView : public KviKvsObject
{
Q_OBJECT
@@ -39,10 +43,12 @@ public:
public:
QWidget * widget() { return (QWidget *)object(); }
protected:
+ KviKvsRunTimeContext *m_pContext;
QWebPage * m_pWebPage;
QHash<QString,QWebFrame *> m_dictFrames;
QHash<QString,QWebElement *> m_dictCache;
-
+ KviPointerList<QNetworkReply> *m_pReplyList;
+ QNetworkAccessManager *m_pNetworkManager;
QList<QWebElement> lStack;
QWebElementCollection m_webElementCollection;
QWebElement m_currentElement;
@@ -52,6 +58,9 @@ protected:
bool removeFromDocument(KviKvsObjectFunctionCall *c);
bool makePreview(KviKvsObjectFunctionCall *c);
+ bool removeClass(KviKvsObjectFunctionCall *c);
+ bool classes(KviKvsObjectFunctionCall *c);
+
bool rememberCurrent(KviKvsObjectFunctionCall *c);
bool moveTo(KviKvsObjectFunctionCall *c);
@@ -79,12 +88,31 @@ protected:
bool loadStartedEvent(KviKvsObjectFunctionCall *c);
bool loadFinishedEvent(KviKvsObjectFunctionCall *c);
bool loadProgressEvent(KviKvsObjectFunctionCall *c);
-
-
+ bool downloadCompletedEvent(KviKvsObjectFunctionCall *c);
+ bool downloadProgressEvent(KviKvsObjectFunctionCall *c);
+ bool downloadRequestEvent(KviKvsObjectFunctionCall *c);
protected slots:
void slotLoadFinished(bool);
void slotLoadProgress(int);
void slotLoadStarted();
+ void slotReplyFinished(QNetworkReply*);
+ void slotDownloadRequest(const QNetworkRequest &);
+};
+class KviKvsDownloadHandler : public QObject
+{
+ Q_OBJECT
+public:
+ KviKvsDownloadHandler(KvsObject_webView *par,QFile *pFile,QNetworkReply *pNetReply,int iId);
+
+ virtual ~KviKvsDownloadHandler();
+protected:
+ KvsObject_webView *m_pParentScript;
+ QFile *m_pFile;
+ QNetworkReply *m_pReply;
+ int m_Id;
+protected slots:
+ void slotReadyRead();
+ void slotReplyFinished();
};
#endif
#endif //!_CLASS_WEBVIEW_H_