/*! @file
@ id $ Id $
*/
// 1 2 3 4 5 6 7 8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
# ifndef WEBPAGE_HXX
# define WEBPAGE_HXX
# include <exceptions.hxx>
# include <networkaccessmanager.hxx>
# include <QWebPage>
# include <QWebFrame>
# include <QWebSettings>
# include <QWidget>
# include <QFileDialog>
# include <QFile>
# include <cassert>
# ifndef LOG
# include <iostream>
# define LOG(X) std::clog<<X<<std::endl;
inline std : : ostream & operator < < ( std : : ostream & o , const QString & s ) {
return o < < s . toStdString ( ) ;
}
# endif
class TestWebPage : public QWebPage {
Q_OBJECT ;
public :
TestWebPage ( QObject * parent = 0 , bool unattended = false ) :
QWebPage ( parent ) ,
_unattended ( unattended ) {
setNetworkAccessManager ( new NetworkAccessManager ( this ) ) ;
settings ( ) - > setOfflineStorageDefaultQuota ( 1024 * 1024 * 1024 ) ;
settings ( ) - > setOfflineStoragePath ( " /tmp/ " ) ;
settings ( ) - > setAttribute ( QWebSettings : : OfflineStorageDatabaseEnabled , true ) ;
//settings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, true);
settings ( ) - > setAttribute ( QWebSettings : : LocalStorageEnabled , true ) ;
}
virtual ~ TestWebPage ( ) {
if ( ! _nextFile . isEmpty ( ) & & ! std : : uncaught_exception ( ) )
throw LastFileNotUploaded ( _nextFile ) ;
}
void setNextUploadFile ( QString nextFile ) {
if ( ! _unattended ) throw NotUnattended ( ) ;
if ( ! _nextFile . isEmpty ( ) ) throw LastFileNotUploaded ( _nextFile ) ;
if ( nextFile . isEmpty ( ) ) throw EmptyUploadFile ( ) ;
_nextFile = nextFile ;
}
bool uploadPrepared ( ) {
return ! _nextFile . isEmpty ( ) ;
}
Q_SIGNALS :
void uploadFile ( QString filename ) ;
protected :
virtual QString chooseFile ( QWebFrame * frame , const QString & ) {
if ( _unattended ) {
if ( _nextFile . isEmpty ( ) ) throw NoUploadFile ( ) ;
if ( ! QFile ( _nextFile ) . exists ( ) ) throw FileNotFound ( _nextFile ) ;
QString filename ( _nextFile ) ;
_nextFile . clear ( ) ;
return filename ;
} else {
QString filename ( QFileDialog : : getOpenFileName
( frame - > page ( ) - > view ( ) , tr ( " File to Upload " ) ) ) ;
if ( filename . size ( ) ) uploadFile ( filename ) ;
return filename ;
}
}
virtual void javaScriptAlert ( QWebFrame * frame , const QString & msg ) {
LOG ( " javaScriptAlert: " + msg ) ;
if ( _unattended ) {
return ;
} else {
return ;
//return QWebPage::javaScriptAlert(frame, msg);
}
}
virtual bool javaScriptConfirm ( QWebFrame * frame , const QString & msg ) {
LOG ( " javaScriptConfirm " + msg ) ;
if ( _unattended ) {
return true ;
} else {
return true ;
//return QWebPage::javaScriptConfirm(frame, msg);
}
}
virtual void javaScriptConsoleMessage ( const QString & msg ,
int line , const QString & src ) {
LOG ( " javaScriptConsoleMessage: " + msg ) ;
if ( _unattended ) {
return ;
} else {
return ;
//return QWebPage::javaScriptConsoleMessage(msg, line, src);
}
}
virtual bool javaScriptPrompt ( QWebFrame * frame , const QString & msg ,
const QString & defaultValue , QString * result ) {
LOG ( " javaScriptPrompt: " + msg ) ;
if ( _unattended ) {
return true ;
} else {
return true ;
//return QWebPage::javaScriptPrompt(frame, msg, defaultValue, result);
}
}
private :
bool _unattended ;
QString _nextFile ;
} ;
# endif