ticketrender.cpp \
orderwin.cpp \
labeldlg.cpp \
- version.cpp
+ version.cpp \
+ templates.cpp
HEADERS = \
keygen.h \
ticketrender.h \
orderwin.h \
labeldlg.h \
- misc.h
+ misc.h \
+ templates.h
#some PHP files are listed in this file to scan them for translatable items
#use genphpscan.sh to regenerate it.
--- /dev/null
+//
+// C++ Implementation: templates
+//
+// Description:
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2008
+//
+// Copyright: See README/COPYING files that come with this distribution
+//
+//
+
+#include "templates.h"
+#include "webrequest.h"
+#include "main.h"
+
+#include <QStringList>
+#include <QSettings>
+#include <QRegExp>
+#include <QDateTime>
+#include <QFile>
+#include <QDir>
+#include <QDomDocument>
+#include <QDomElement>
+
+
+MTemplates::MTemplates(MWebRequest*r,QString p)
+{
+ req=r;
+ profileid=p;
+}
+
+QString MTemplates::getTemplate(QString f)
+{
+ //syntax check
+ f=f.toLower();
+ QRegExp fregexp("[a-z0-9_\\.]+");
+ if(!fregexp.exactMatch(f))return "";
+ //basics
+ QString dname=req->dataDir()+"/templates/";
+ QSettings set;
+ set.beginGroup("templates/"+profileid);
+ //do we need an update?
+ QDateTime last=QDateTime::fromTime_t(set.value("lastupdate",0).toInt()+300);
+ if(last<QDateTime::currentDateTime()){do{//pseudo-loop, so we can use break instead of goto
+ //get local info
+ set.beginGroup("checksum");
+ QStringList files=set.childKeys();
+ QMap<QString,QString> fmap;
+ for(int i=0;i<files.size();i++)
+ fmap.insert(files[i],set.value(files[i]).toString());
+ set.endGroup();
+ //get remote info, assume it is still valid if unable to retrieve it
+ if(!req->request("gettemplatelist",""))break;
+ if(req->responseStatus()!=MWebRequest::Ok)break;
+ //remember update time
+ set.setValue("lastupdate",QDateTime::currentDateTime().toTime_t());
+ //parse info
+ QDomDocument doc;
+ if(!doc.setContent(req->responseBody()))break;
+ //prune old stuff
+ set.remove("checksum");
+ //scan and create new list
+ QDomNodeList nl=doc.elementsByTagName("Template");
+ QMap<QString,QString>nfmap;
+ for(int i=0;i<nl.size();i++){
+ QDomElement el=nl.at(i).toElement();
+ if(el.isNull())continue;
+ QString f=el.attribute("name","");
+ QString h=el.attribute("hash","");
+ if(f=="" || !fregexp.exactMatch(f))continue;
+ nfmap.insert(f,h);
+ set.setValue("checksum/"+f,h);
+ }
+ //compare old with new list, delete changed files
+ for(int i=0;i<files.size();i++){
+ if(!nfmap.contains(files[i]))
+ QFile(dname+"/"+f).remove();
+ else
+ if(nfmap[files[i]]!=fmap[files[i]])
+ QFile(dname+"/"+f).remove();
+ }
+ }while(false);}
+ //check that it is a valid template file
+ if(!set.contains("checksum/"+f))return "";
+ //get the file if it does not exist
+ if(!QFile(dname+"/"+f).exists()){
+ //make sure directory exists
+ QDir(req->dataDir()).mkpath("templates");
+ //retrieve template file
+ if(!req->request("gettemplate",f.toAscii()))
+ return "";
+ if(req->responseStatus()!=MWebRequest::Ok)
+ return "";
+ QFile fl(dname+"/"+f);
+ if(!fl.open(QIODevice::WriteOnly))
+ return "";
+ fl.write(req->responseBody());
+ fl.close();
+ }
+ //return file name
+ return dname+"/"+f;
+}
+
+bool MTemplates::setTemplate(QString n,QString f)
+{
+ //sanity check
+ QRegExp fregexp("[a-z0-9_\\.]+");
+ if(!fregexp.exactMatch(n))return false;
+ //get content
+ QFile fl(f);
+ if(!fl.open(QIODevice::ReadOnly))
+ return false;
+ QByteArray ba=fl.readAll();
+ fl.close();
+ //send to server
+ if(!req->request("settemplate",n.toAscii()+"\n"+ba))
+ return false;
+ if(req->responseStatus()!=MWebRequest::Ok)
+ return false;
+ //delete it from cache, so it is retrieved again
+ QSettings().remove("templates/"+profileid+"/checksum/"+n);
+ QFile(req->dataDir()+"/templates/"+n).remove();
+ //return success
+ return true;
+}
--- /dev/null
+//
+// C++ Interface: templates
+//
+// Description:
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2008
+//
+// Copyright: See README/COPYING files that come with this distribution
+//
+//
+
+#ifndef MAGICSMOKE_TEMPLATES_H
+#define MAGICSMOKE_TEMPLATES_H
+
+#include <QString>
+
+class MWebRequest;
+
+class MTemplates
+{
+ public:
+ MTemplates(MWebRequest*,QString);
+
+ QString getTemplate(QString);
+
+ bool setTemplate(QString,QString);
+
+ private:
+ MWebRequest*req;
+ QString profileid;
+
+};
+
+#endif
#include <QTimer>
MWebRequest::MWebRequest(QString pid)
+ :temp(this,pid)
{
profileid=pid;
webtimeout=30000;
QString MWebRequest::getTemplate(QString f)
{
- //syntax check
- f=f.toLower();
- QRegExp fregexp("[a-z0-9_\\.]+");
- if(!fregexp.exactMatch(f))return "";
- //basics
- QString dname=dataDir()+"/templates/";
- QSettings set;
- set.beginGroup("templates/"+profileid);
- //do we need an update?
- QDateTime last=QDateTime::fromTime_t(set.value("lastupdate",0).toInt()+300);
- if(last<QDateTime::currentDateTime()){do{//pseudo-loop, so we can use break instead of goto
- //get local info
- set.beginGroup("checksum");
- QStringList files=set.childKeys();
- QMap<QString,QString> fmap;
- for(int i=0;i<files.size();i++)
- fmap.insert(files[i],set.value(files[i]).toString());
- set.endGroup();
- //get remote info, assume it is still valid if unable to retrieve it
- if(!request("gettemplatelist",""))break;
- if(responseStatus()!=Ok)break;
- //remember update time
- set.setValue("lastupdate",QDateTime::currentDateTime().toTime_t());
- //parse info
- QDomDocument doc;
- if(!doc.setContent(responseBody()))break;
- //prune old stuff
- set.remove("checksum");
- //scan and create new list
- QDomNodeList nl=doc.elementsByTagName("Template");
- QMap<QString,QString>nfmap;
- for(int i=0;i<nl.size();i++){
- QDomElement el=nl.at(i).toElement();
- if(el.isNull())continue;
- QString f=el.attribute("name","");
- QString h=el.attribute("hash","");
- if(f=="" || !fregexp.exactMatch(f))continue;
- nfmap.insert(f,h);
- set.setValue("checksum/"+f,h);
- }
- //compare old with new list, delete changed files
- for(int i=0;i<files.size();i++){
- if(!nfmap.contains(files[i]))
- QFile(dname+"/"+f).remove();
- else
- if(nfmap[files[i]]!=fmap[files[i]])
- QFile(dname+"/"+f).remove();
- }
- }while(false);}
- //check that it is a valid template file
- if(!set.contains("checksum/"+f))return "";
- //get the file if it does not exist
- if(!QFile(dname+"/"+f).exists()){
- //make sure directory exists
- QDir(dataDir()).mkpath("templates");
- //retrieve template file
- if(!request("gettemplate",f.toAscii()))
- return "";
- if(responseStatus()!=Ok)
- return "";
- QFile fl(dname+"/"+f);
- if(!fl.open(QIODevice::WriteOnly))
- return "";
- fl.write(responseBody());
- fl.close();
- }
- //return file name
- return dname+"/"+f;
+ return temp.getTemplate(f);
}
bool MWebRequest::setTemplate(QString n,QString f)
{
- //sanity check
- QRegExp fregexp("[a-z0-9_\\.]+");
- if(!fregexp.exactMatch(n))return false;
- //get content
- QFile fl(f);
- if(!fl.open(QIODevice::ReadOnly))
- return false;
- QByteArray ba=fl.readAll();
- fl.close();
- //send to server
- if(!request("settemplate",n.toAscii()+"\n"+ba))
- return false;
- if(responseStatus()!=Ok)
- return false;
- //delete it from cache, so it is retrieved again
- QSettings().remove("templates/"+profileid+"/checksum/"+n);
- QFile(dataDir()+"/templates/"+n).remove();
- //return success
- return true;
+ return temp.setTemplate(n,f);
}
#include "order.h"
#include "room.h"
#include "shipping.h"
+#include "templates.h"
#include "user.h"
/**abstraction of requests to the web server, handles sessions and all data transfer*/
qint64 sessiontimeout;
//profile name for lookups
QString profileid;
+ //template subsystem
+ MTemplates temp;
/**used by login and relogin to do the actual work*/
bool doLogin();
//templates
$this->scheme["template"]=array(
"filename" => array("string","primarykey"),
+ "description" =>array("string"),
"content" => array("blob"),
"hash" => array("string:32","notnull") //md5
);