| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- #include "cachewebview.h"
- #include <QtGui>
- CacheWebView::CacheWebView(QWidget *parent) :
- QWebView(parent)
- {
- cache = 0;
- isCacheFile = false;
- m_isloading = false;
- setAcceptDrops(false);
- QObject::connect(this,SIGNAL(linkClicked(QUrl)),this,SLOT(onLinkClicked(QUrl)));
- }
- void CacheWebView::setPageUrl(QString pageUrl, QString cachePath)
- {
- if(pageUrl.isEmpty())
- return;
- this->pageUrl = pageUrl;
- this->cachePath = cachePath;
- QUrl url(pageUrl);
- connect(this,SIGNAL(loadFinished(bool)),this,SLOT(onWebViewFinished(bool)));
- this->page()->setLinkDelegationPolicy(QWebPage::DelegateExternalLinks);
- this->load(url);
- }
- CacheWebView::~CacheWebView()
- {
- if(cache)
- {
- cache->abort();
- cache->wait();
- delete cache;
- cache = 0;
- }
- qDebug()<<QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")\
- <<"CacheWebView Destroyed.";
- }
- void CacheWebView::onLinkClicked(QUrl url)
- {
- QDesktopServices::openUrl(url);
- }
- void CacheWebView::onWebViewFinished(bool ok)
- {
- m_isloading = false;
- disconnect(this,SIGNAL(loadFinished(bool)),this,SLOT(onWebViewFinished(bool)));
- if(!isCacheFile)
- {
- if(ok)
- {
- if(!cache)
- {
- cache = new CacheWebPage(this->pageUrl,QCoreApplication::applicationDirPath()+"/../cache/");
- }
- cache->start();
- }
- else
- {
- isCacheFile = true;
- displayCacheFile();
- }
- }
- }
- void CacheWebView::displayCacheFile()
- {
- QUrl url(this->pageUrl);
- QString md5;
- QByteArray ba;
- ba = QCryptographicHash::hash ( this->pageUrl.toAscii(), QCryptographicHash::Md5 );
- md5.append(ba.toHex());
- QString cacheFile = QCoreApplication::applicationDirPath()+"/../cache/" + md5 + url.path();
- if(QFile::exists(cacheFile))
- {
- this->load(QUrl::fromUserInput(cacheFile));
- }
- }
|