flags for templates
authorKonrad Rosenbaum <konrad@silmor.de>
Tue, 27 Dec 2011 22:07:34 +0000 (23:07 +0100)
committerKonrad Rosenbaum <konrad@silmor.de>
Tue, 27 Dec 2011 22:07:34 +0000 (23:07 +0100)
src/iface/msinterface.cpp
src/templates/templatedlg.cpp
src/templates/templatedlg.h
src/templates/templates.cpp
src/templates/templates.h
wob/transact/template.wolf
www/inc/wext/template.php

index ba9e43d..54bbd80 100644 (file)
@@ -213,6 +213,10 @@ void MSInterface::updateTemplates()
 
 bool MSInterface::checkFlags(const QStringList& fl) const
 {
+       //shortcut for admins
+       if(userflags.contains("_admin") || userroles.contains("_admin"))
+               return true;
+       //check actual flags
        foreach(QString f,fl){
                f=f.trimmed();
                if(f.isEmpty())continue;
index ea0f786..2997212 100644 (file)
@@ -12,6 +12,7 @@
 
 #include "templatedlg.h"
 #include "ticketedit.h"
+#include "flagedit.h"
 
 #include <QBoxLayout>
 #include <QComboBox>
@@ -60,7 +61,7 @@ MTemplate MTemplateChoice::choice()const
        QString fn=box->itemData(box->currentIndex()).toString();
        QSettings set;
        set.beginGroup(sgroup+"/"+fn);
-       return MTemplate(cachedir+"/"+fn, set.value("checksum").toString(), set.value("description").toString());
+       return MTemplate(cachedir+"/"+fn, set.value("checksum").toString(), set.value("description").toString(), set.value("flags").toString());
 }
 
 
@@ -87,12 +88,16 @@ MTemplateEditor::MTemplateEditor(MTemplateStore*s)
        hl->addLayout(vl2=new QVBoxLayout);
        vl2->addWidget(p=new QPushButton(tr("Update Now")));
        connect(p,SIGNAL(clicked()),this,SLOT(forceUpdate()));
+       vl2->addSpacing(20);
        vl2->addWidget(p=new QPushButton(tr("Add Variant")));
        connect(p,SIGNAL(clicked()),this,SLOT(addItem()));
        vl2->addWidget(p=new QPushButton(tr("Delete Variant")));
        connect(p,SIGNAL(clicked()),this,SLOT(deleteItem()));
        vl2->addSpacing(20);
-       vl2->addWidget(p=new QPushButton(tr("Save Template")));
+       vl2->addWidget(p=new QPushButton(tr("Change Flags")));
+       connect(p,SIGNAL(clicked()),this,SLOT(changeFlags()));
+       vl2->addSpacing(20);
+       vl2->addWidget(p=new QPushButton(tr("Save Template...")));
        connect(p,SIGNAL(clicked()),this,SLOT(saveItem()));
        vl2->addWidget(p=new QPushButton(tr("Edit Template")));
        connect(p,SIGNAL(clicked()),this,SLOT(editItem()));
@@ -115,8 +120,8 @@ void MTemplateEditor::updateView()
        //basic cleanup
        nochange=true;
        model->clear();
-       model->insertColumns(0,3);
-       model->setHorizontalHeaderLabels(QStringList()<<tr("Template/Variant")<<tr("Description")<<tr("Checksum"));
+       model->insertColumns(0,4);
+       model->setHorizontalHeaderLabels(QStringList() <<tr("Template/Variant") <<tr("Description") <<tr("Checksum") <<tr("Flags"));
        //get base names
        QStringList base=MTemplate::legalBaseNames();
        //get all templates, sort them into buckets
@@ -145,7 +150,7 @@ void MTemplateEditor::updateView()
                if(hier[base[i]].size()<1)continue;
                QList<MTemplate>sub=hier[base[i]];
                model->insertRows(0,sub.size(),idx);
-               model->insertColumns(0,3,idx);
+               model->insertColumns(0,4,idx);
                for(int j=0;j<sub.size();j++){
                        QModelIndex idx2=model->index(j,0,idx);
                        model->setData(idx2,sub[j].fileName()+" ("+sub[j].variantID()+")");
@@ -155,6 +160,8 @@ void MTemplateEditor::updateView()
                        model->setData(model->index(j,1,idx),sub[j].description());
                        model->setData(model->index(j,2,idx),sub[j].checksum());
                        model->itemFromIndex(model->index(j,2,idx))->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
+                       model->setData(model->index(j,3,idx),sub[j].flags());
+                       model->itemFromIndex(model->index(j,3,idx))->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
                }
        }
        //expand all
@@ -162,6 +169,7 @@ void MTemplateEditor::updateView()
        tree->resizeColumnToContents(0);
        tree->resizeColumnToContents(1);
        tree->resizeColumnToContents(2);
+       tree->resizeColumnToContents(3);
        nochange=false;
 }
 void MTemplateEditor::deleteItem()
@@ -312,3 +320,35 @@ void MTemplateEditor::saveItem()
                QMessageBox::warning(this,tr("Error"),tr("Unable to save the template file."));
        
 }
+
+void MTemplateEditor::changeFlags()
+{
+       //get selection
+       QModelIndex idx=tree->currentIndex();
+       if(!idx.isValid())return;
+       QModelIndex pidx=idx.parent();
+       QModelIndex bidx=model->index(idx.row(),0,pidx);
+       QString base=model->data(bidx,BASEROLE).toString();
+       if(base=="")return;
+       //query file name
+       QString fn=model->data(bidx,NAMEROLE).toString();
+       if(fn=="")return;
+       //get it
+       MTemplate tmp=store->getTemplateByFile(fn);
+       if(tmp.cacheFileName()==""){
+               QMessageBox::warning(this,tr("Warning"),tr("Ooops. Lost the template file, cannot alter it."));
+               return;
+       }
+       //edit flags
+       MFlagEditor fe(this,tmp.flags(),tr("Edit flags of template '%1'").arg(fn));
+       if(fe.exec()!=QDialog::Accepted)return;
+       //send to server
+       QString nflags=fe.currentFlags();
+       if(!store->setTemplateFlags(fn,nflags)){
+               QMessageBox::warning(this,tr("Warning"),tr("Unable to send updated flags to server."));
+               return;
+       }
+       //update locally
+       bidx=model->index(idx.row(),3,pidx);
+       model->setData(bidx,nflags);
+}
index 22aac15..640689d 100644 (file)
@@ -52,6 +52,7 @@ class MTemplateEditor:public QDialog
                void deleteItem();
                void addItem();
                void changeDescr(QStandardItem*);
+               void changeFlags();
                void forceUpdate();
                void editItem();
                void saveItem();
index c7655a6..5f1f1bb 100644 (file)
@@ -62,13 +62,17 @@ MTemplate MTemplateStore::getTemplate(QString f)
        //get directory
        QStringList dir=set.childGroups();
        QStringList tdir;
+       MSInterface*ifc=MSInterface::instance();
        for(int i=0;i<dir.size();i++){
+               //filter by base name
                if(dir[i].split(".")[0].startsWith(f,Qt::CaseInsensitive))
-                       tdir<<dir[i];
+                       //filter by flags
+                       if(ifc->checkFlags(set.value(dir[i]+"/flags").toString()))
+                               tdir<<dir[i];
        }
        //check dir size
        if(tdir.size()==0)return MTemplate();
-       if(tdir.size()==1)return MTemplate(dname+"/"+tdir[0], set.value(tdir[0]+"/checksum").toString(), set.value(tdir[0]+"/description").toString());
+       if(tdir.size()==1)return MTemplate(dname+"/"+tdir[0], set.value(tdir[0]+"/checksum").toString(), set.value(tdir[0]+"/description").toString(), set.value(tdir[0]+"/flags").toString());
        //hmm, more than one choice
        MTemplateChoice c(dname,f,tdir,"templates/"+profileid);
        if(c.exec()==QDialog::Accepted)return c.choice();
@@ -91,7 +95,7 @@ MTemplate MTemplateStore::getTemplateByFile(QString f)
        //check directory
        if(!set.childGroups().contains(f))return MTemplate();
        //return if found
-       return MTemplate(dname+"/"+f, set.value(f+"/checksum").toString(), set.value(f+"/description").toString());
+       return MTemplate(dname+"/"+f, set.value(f+"/checksum").toString(), set.value(f+"/description").toString(), set.value(f+"/flags").toString());
 }
 
 void MTemplateStore::updateTemplates(bool force)
@@ -113,6 +117,7 @@ void MTemplateStore::updateTemplates(bool force)
                MOTemplateInfo t;
                t.setfilename(files[i]);
                t.sethash(set.value(files[i]+"/checksum").toString()); t.setdescription(set.value(files[i]+"/description").toString());
+               t.setflags(set.value(files[i]+"/flags").toString());
                fmap.insert(files[i],t);
        }
        
@@ -154,8 +159,9 @@ void MTemplateStore::updateTemplates(bool force)
                        if(retrieveFile(dname,nfiles[i]))
                                fmap.insert(nfiles[i],nfmap[nfiles[i]]);
                }else{
-                       //file is already up to date, still copy the current description
+                       //file is already up to date, still copy the current description and flags
                        fmap[nfiles[i]].setdescription(nfmap[nfiles[i]].description());
+                       fmap[nfiles[i]].setflags(nfmap[nfiles[i]].flags());
                }
        }
        files=fmap.keys();
@@ -174,6 +180,7 @@ void MTemplateStore::updateTemplates(bool force)
                MOTemplateInfo s=fmap[files[i]];
                set.setValue(s.filename()+"/checksum",s.hash().value());
                set.setValue(s.filename()+"/description",s.description().value());
+               set.setValue(s.filename()+"/flags",s.flags().value());
        }
 }
 
@@ -251,6 +258,24 @@ bool MTemplateStore::setTemplateDescription(QString n,QString d)
        return true;
 }
 
+bool MTemplateStore::setTemplateFlags(QString n, QString flags)
+{
+       //check that we have a real change
+       QSettings set;
+       set.beginGroup("templates/"+profileid+"/"+n);
+       QString o=set.value("flags").toString();
+       qDebug("setting %s '%s' -> '%s'",n.toAscii().data(),o.toAscii().data(),flags.toAscii().data());
+       if(o==flags)return true;
+       //send to server
+       MTSetTemplateFlags std=req->querySetTemplateFlags(n,flags);
+       if(std.hasError())
+               return false;
+       //update internal description
+       set.setValue("flags",flags);
+       //return success
+       return true;
+}
+
 QList<MTemplate> MTemplateStore::allTemplates()
 {
        updateTemplates(false);
@@ -260,7 +285,7 @@ QList<MTemplate> MTemplateStore::allTemplates()
        QStringList names=set.childGroups();
        QList<MTemplate> ret;
        for(int i=0;i<names.size();i++)
-               ret.append(MTemplate(dname+"/"+names[i],set.value(names[i]+"/checksum").toString(),set.value(names[i]+"/description").toString()));
+               ret.append(MTemplate(dname+"/"+names[i], set.value(names[i]+"/checksum").toString(), set.value(names[i]+"/description").toString(), set.value(names[i]+"/flags").toString()));
        return ret;
 }
 
@@ -268,7 +293,8 @@ QList<MTemplate> MTemplateStore::allTemplates()
 
 MTemplate::MTemplate(){}
 MTemplate::MTemplate(QString fn){m_fname=fn;}
-MTemplate::MTemplate(QString fn,QString chk,QString dsc){m_fname=fn;m_checksum=chk;m_descr=dsc;}
+MTemplate::MTemplate(QString fn,QString chk,QString dsc,QString flg)
+{m_fname=fn;m_checksum=chk;m_descr=dsc;m_flags=flg;}
 QString MTemplate::cacheFileName()const{return m_fname;}
 
 QString MTemplate::fileName()const
index 7ee685b..02bcece 100644 (file)
@@ -61,6 +61,9 @@ class MTemplate
                /**returns whether this is a label template*/
                bool isLabel()const;
                
+               ///returns the flags of this template
+               QString flags()const{return m_flags;}
+               
                /**template type; currently only ODF and Labels are known*/
                enum Type{
                        /**uninitialized or unknown label type*/
@@ -103,10 +106,11 @@ class MTemplate
                \param fn physical file name of the template
                \param chk server side check sum of the template
                \param dsc description of the template
+               \param flg flags of the template
                */
-               MTemplate(QString fn,QString chk,QString dsc);
+               MTemplate(QString fn,QString chk,QString dsc,QString flg);
        private:
-               QString m_fname,m_checksum,m_descr;
+               QString m_fname,m_checksum,m_descr,m_flags;
 };
 Q_DECLARE_OPERATORS_FOR_FLAGS(MTemplate::Types)
 
@@ -125,6 +129,9 @@ class MTemplateStore
                /**stores a new description for a template*/
                bool setTemplateDescription(QString templatename,QString description);
                
+               ///stores a change of flags for a template
+               bool setTemplateFlags(QString templatename,QString flags);
+               
                /**deletes a template (requires full name), used by MTemplateEditor*/
                bool deleteTemplate(QString);
                
index 5bdb780..31cdb87 100644 (file)
                </Input>
                <Call lang="php" method="WOTemplate::deleteTemplate($this);"/>
        </Transaction>
+       <Transaction name="SetTemplateFlags">
+               <Doc>Changes the flags of the template.</Doc>
+               <Input>
+                       <Var name="filename" type="astring"/>
+                       <Var name="flags" type="astring"/>
+               </Input>
+               <Call lang="php" method="WOTemplate::changeFlags($this);"/>
+       </Transaction>
 </Wolf>
index 05e5850..46b3dc0 100644 (file)
@@ -63,6 +63,22 @@ class WOTemplate extends WOTemplateAbstract
                //delete
                $tm->deleteFromDb();
        }
+       
+       ///called by the SetTemplateFlags transaction
+       static public function changeFlags($trans)
+       {
+               //find it
+               $tm=WTtemplate::getFromDB($trans->getfilename());
+               if($tm===false){
+                       $trans->abortWithError(tr("No such template."));
+                       return;
+               }
+               //set
+               $tm->flags=$trans->getflags();
+               //delete
+               if(!$tm->update())
+                       $trans->abortWithError(tr("Unable to set flags - DB error."));
+       }
 };
 
 ?>
\ No newline at end of file