moved templates to own class
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Fri, 21 Nov 2008 08:39:34 +0000 (08:39 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Fri, 21 Nov 2008 08:39:34 +0000 (08:39 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@197 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/smoke.pro
src/templates.cpp [new file with mode: 0644]
src/templates.h [new file with mode: 0644]
src/webrequest.cpp
src/webrequest.h
www/inc/db/db_scheme.php

index a62f6df..35e7e21 100644 (file)
@@ -44,7 +44,8 @@ SOURCES = \
        ticketrender.cpp \
        orderwin.cpp \
        labeldlg.cpp \
-       version.cpp
+       version.cpp \
+       templates.cpp
 
 HEADERS = \
        keygen.h \
@@ -67,7 +68,8 @@ HEADERS = \
        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.
diff --git a/src/templates.cpp b/src/templates.cpp
new file mode 100644 (file)
index 0000000..c53ba42
--- /dev/null
@@ -0,0 +1,126 @@
+//
+// 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;
+}
diff --git a/src/templates.h b/src/templates.h
new file mode 100644 (file)
index 0000000..3ef3c1a
--- /dev/null
@@ -0,0 +1,35 @@
+//
+// 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
index 10d622a..67342a1 100644 (file)
@@ -25,6 +25,7 @@
 #include <QTimer>
 
 MWebRequest::MWebRequest(QString pid)
+       :temp(this,pid)
 {
        profileid=pid;
        webtimeout=30000;
@@ -586,95 +587,10 @@ QString MWebRequest::hostName()
 
 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);
 }
index 97fdf9a..d9dc944 100644 (file)
@@ -28,6 +28,7 @@
 #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*/
@@ -161,6 +162,8 @@ class MWebRequest:public QObject
                qint64 sessiontimeout;
                //profile name for lookups
                QString profileid;
+               //template subsystem
+               MTemplates temp;
                
                /**used by login and relogin to do the actual work*/
                bool doLogin();
index 931baba..5f18636 100644 (file)
@@ -71,6 +71,7 @@ class DbScheme {
                //templates
                $this->scheme["template"]=array(
                        "filename" => array("string","primarykey"),
+                       "description" =>array("string"),
                        "content" => array("blob"),
                        "hash" => array("string:32","notnull") //md5
                );