m=mb->addMenu(tr("C&onfigure"));
m->addAction(tr("&Auto-Refresh settings..."),this,SLOT(setRefresh()));
+ m->addAction(tr("&Server Access settings..."),this,SLOT(webSettings()));
m=mb->addMenu(tr("&Admin"));
m->setEnabled(req->hasRole("_admin"));
//status bar
statusBar()->setSizeGripEnabled(true);
+ //make sure webrequest knows its settings
+ webSettings(false);
//fill tables
if(req->hasRole("geteventlist")){
updateEvents();
rtimer.start();
}
+void MOverview::webSettings(bool showdlg)
+{
+ QSettings set;
+ set.beginGroup("profiles/"+profilekey);
+ //get settings
+ MWebRequest::LogLevel lvl=MWebRequest::LogLevel(set.value("webloglevel", MWebRequest::LogOnError).toInt());
+ int timeout=set.value("webtimeout",30).toInt();
+ //dialog
+ if(showdlg){
+ QDialog d;
+ d.setWindowTitle(tr("Server Access Settings"));
+ QGridLayout*gl;
+ QHBoxLayout*hl;
+ d.setLayout(gl=new QGridLayout);
+ gl->addWidget(new QLabel(tr("Request Timeout (seconds):")),0,0);
+ QSpinBox*tmout;
+ gl->addWidget(tmout=new QSpinBox,0,1);
+ tmout->setRange(10,999);
+ tmout->setValue(timeout);
+ QComboBox *log;
+ gl->addWidget(new QLabel(tr("Log Level:")),1,0);
+ gl->addWidget(log=new QComboBox,1,1);
+ log->addItem(tr("Minimal Logging"),MWebRequest::LogMinimal);
+ log->addItem(tr("Log Details on Error"),MWebRequest::LogOnError);
+ log->addItem(tr("Always Log Details"),MWebRequest::LogDetailed);
+ switch(lvl){
+ case MWebRequest::LogMinimal:log->setCurrentIndex(0);break;
+ case MWebRequest::LogOnError:log->setCurrentIndex(1);break;
+ case MWebRequest::LogDetailed:log->setCurrentIndex(2);break;
+ }
+ gl->setRowMinimumHeight(2,15);
+ gl->addLayout(hl=new QHBoxLayout,3,0,1,2);
+ hl->addStretch(10);
+ QPushButton*p;
+ hl->addWidget(p=new QPushButton(tr("&OK")));
+ connect(p,SIGNAL(clicked()),&d,SLOT(accept()));
+ hl->addWidget(p=new QPushButton(tr("&Cancel")));
+ connect(p,SIGNAL(clicked()),&d,SLOT(reject()));
+ if(d.exec()!=QDialog::Accepted)return;
+ //write settings
+ lvl=MWebRequest::LogLevel(log->itemData(log->currentIndex()).toInt());
+ timeout=tmout->value();
+ set.setValue("webloglevel",lvl);
+ set.setValue("webtimeout",timeout);
+ }
+ //reset webrequest
+ req->setLogLevel(lvl);
+ req->setTimeout(timeout*1000);
+}
+
/**********************************************/
MPasswordChange::MPasswordChange(QWidget*par,QString user)
/**set refresh timeout*/
void setRefresh();
+ /**web request settings dialog; shows the dialog per default, just copies settings from registry to webrequest object if false*/
+ void webSettings(bool dlg=true);
+
private:
//my session object
QPointer<MWebRequest>req;
#include <QSettings>
#include <QTimer>
+#include <QtDebug>
+
MWebRequest::MWebRequest(QString pid)
:temp(this,pid)
{
profileid=pid;
- webtimeout=30000;
+ webtimeout=QSettings().value("profiles/"+pid+"/webtimeout",LogOnError).toInt()*1000;
+ loglvl=LogLevel(QSettings().value("profiles/"+pid+"/webloglevel",LogOnError).toInt());
finid=0;finerr=fin=false;
proxyport=-1;
sessiontimeout=0;
return "profsettings/"+profileid;
}
+MWebRequest::LogLevel MWebRequest::logLevel()const
+{
+ return loglvl;
+}
+
+void MWebRequest::setLogLevel(LogLevel l)
+{
+ loglvl=l;
+}
+
+static inline QString esc(const QByteArray ar)
+{
+ QString r;
+ for(int i=0;i<ar.size();i++){
+ unsigned char a=ar[i];
+ if(a=='\\')r+="\\\\";else
+ if(a>=32 && a<=127)r+=(char)a;
+ else r+="\\x"+QByteArray((char*)&a,1).toHex();
+ }
+ return r;
+}
+
bool MWebRequest::request(QString hreq,QByteArray data)
{
//show the user we are waiting
WaitCursor wc;
//set up request
+ QString log;
QEventLoop loop(this);
connect(this,SIGNAL(requestFinInt()),&loop,SLOT(quit()));
int port=url.port();
if(url.scheme().toLower()=="http")port=80;
else port=443;
}
- qDebug("connecting to host=%s port=%i scheme=%s request=%s",url.host().toAscii().data(),(int)port,url.scheme().toAscii().data(),hreq.toAscii().data());
+ qDebug()<<QString("New HTTP Request %1 URL %2\n\tto host=%3 port=%4 scheme=%5")
+ .arg(hreq)
+ .arg(url.toString())
+ .arg(url.host())
+ .arg(port)
+ .arg(url.scheme());
QHttp::ConnectionMode conm;
if(url.scheme().toLower()=="http")conm=QHttp::ConnectionModeHttp;
else conm=QHttp::ConnectionModeHttps;
hrh.setValue("X-MagicSmoke-Session",sessionid);
hrh.setContentLength(data.size());
hrh.setContentType("application/x-magicsmoke; charset=UTF-8");
- qDebug("----------------------->\nRequesting data with:\n%s\n\nRequest Body:\n%s\n<---->",hrh.toString().toAscii().data(),data.data());
waitid=req.request(hrh,data);
+ if(loglvl>LogMinimal)
+ log=QString("Request %3 with header:\n%1\n\nRequest Body:\n%2\n<---->\n").arg(hrh.toString()).arg(esc(data)).arg(waitid);
qDebug("started req %i",waitid);
fin=false;errstr="";
rspstatus="";rspdata.clear();
req.abort();
errstr="Web Request timed out.";
rspstatus="HTTP";
+ qDebug("Request %i timed out.",waitid);
+ if(loglvl&LogOnError)
+ qDebug()<<log;
return false;
}
- qDebug("Received Request finish for ID=%i Status=%s",finid,finerr?"Error":"Ok");
//finished with low-level error?
if(finerr){
errstr="HTTP Error: "+req.errorString();
rspstatus="HTTP";
- qDebug("%s",errstr.toAscii().data());
+ log+=QString("Request %2 finished with HTTP level error: %1").arg(errstr).arg(waitid);
+ if(loglvl&LogOnError)
+ qDebug()<<log;
return false;
}
QHttpResponseHeader rsph=req.lastResponse();
if(rsph.statusCode()!=200){
errstr="HTTP Error, return code "+QString::number(rsph.statusCode())+" "+rsph.reasonPhrase();
rspstatus="HTTP";
+ log+=QString("Request %2 finished with HTTP level error: %1").arg(errstr).arg(waitid);
+ if(loglvl&LogOnError)
+ qDebug()<<log;
return false;
}
//get data
rspdata=req.readAll();
rspstatus=rsph.value("x-magicsmoke-status").remove('"').trimmed().toLower();
//
- qDebug("----->\nHTTP Response Headers:\n%s",rsph.toString().toAscii().data());
- qDebug("HTTP Response Body:\n%s\n<------------------",rspdata.data());
+ if(loglvl==LogDetailed){
+ log+=QString ("----->\nHTTP Response %3 Headers:\n%1\nHTTP Response %3 Body:\n%2\n<------------------").arg(rsph.toString()).arg(esc(rspdata)).arg(waitid);
+ qDebug()<<log;
+ }
return true;
}
/**returns the requested template as cached locally (see MTemplateStore for details)*/
MTemplate getTemplate(QString);
+ /**log settings*/
+ enum LogLevel {
+ /**minimal logging*/
+ LogMinimal=0,
+ /**log details on error*/
+ LogOnError=1,
+ /**always log details*/
+ LogDetailed=0xf
+ };
+
+ /**returns the current log level*/
+ LogLevel logLevel()const;
+
/**returns currently logged in user*/
QString currentUser()const{return user;}
/**set proxy to be used*/
void setProxy(QString,quint16,QString u=QString(),QString p=QString());
+ /**set log level*/
+ void setLogLevel(MWebRequest::LogLevel);
+
/**log in to web server; requires username/password and the local host name as arguments*/
bool login(QString user,QString passwd,QString hostname);
/**re-log in to web server with previously set values*/
QString proxyname,proxyuser,proxypass;
QUrl url;
int webtimeout,proxyport;
+ LogLevel loglvl;
//login data
QString user,passwd,sessionid,hostname;
//accounting data