[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [calligra] libs/widgets: ResourceManagerUpdt
From:       Victor Lafon <metabolic.ewilan () hotmail ! fr>
Date:       2014-02-26 23:45:16
Message-ID: E1WIoA8-0008Hj-8r () scm ! kde ! org
[Download RAW message or body]

Git commit e06d17571f4f6ad110be4b8844d99f17b66ccf38 by Victor Lafon.
Committed on 26/02/2014 at 23:44.
Pushed by vlafon into branch 'master'.

ResourceManagerUpdt

M  +2    -0    libs/widgets/CMakeLists.txt
M  +54   -3    libs/widgets/KoResourceBundle.cpp
M  +12   -2    libs/widgets/KoResourceBundle.h
A  +218  -0    libs/widgets/KoResourceBundleManager.cpp     [License: LGPL (v2.1+)]
A  +154  -0    libs/widgets/KoResourceBundleManager.h     [License: LGPL (v2.1+)]
M  +22   -1    libs/widgets/KoXmlGenerator.cpp
M  +18   -3    libs/widgets/KoXmlGenerator.h
M  +15   -3    libs/widgets/KoXmlResourceBundleMeta.cpp
M  +3    -0    libs/widgets/KoXmlResourceBundleMeta.h

http://commits.kde.org/calligra/e06d17571f4f6ad110be4b8844d99f17b66ccf38

diff --git a/libs/widgets/CMakeLists.txt b/libs/widgets/CMakeLists.txt
index 0be0f8b..2e65f1a 100644
--- a/libs/widgets/CMakeLists.txt
+++ b/libs/widgets/CMakeLists.txt
@@ -71,6 +71,7 @@ set(kowidgets_LIB_SRCS
 	KoXmlResourceBundleManifest.cpp
 	KoXmlResourceBundleMeta.cpp
     KoResourceBundle.cpp
+    KoResourceBundleManager.cpp
 )
 
 kde4_add_ui_files( kowidgets_LIB_SRCS
@@ -142,6 +143,7 @@ install( FILES
 	KoXmlResourceBundleManifest.h
 	KoXmlResourceBundleMeta.h
     KoResourceBundle.h
+    KoResourceBundleManager.h
 
     DESTINATION
     ${INCLUDE_INSTALL_DIR}/calligra
diff --git a/libs/widgets/KoResourceBundle.cpp b/libs/widgets/KoResourceBundle.cpp
index e6c18a0..12515d1 100644
--- a/libs/widgets/KoResourceBundle.cpp
+++ b/libs/widgets/KoResourceBundle.cpp
@@ -18,28 +18,79 @@
  */
 
 #include "KoResourceBundle.h"
+#include "KoXmlResourceBundleManifest.h"
+#include "KoXmlResourceBundleMeta.h"
 
 KoResourceBundle::KoResourceBundle(QString const& file):KoResource(file)
 {
 
 }
 
+KoResourceBundle::~KoResourceBundle()
+{
+    delete man;
+    delete meta;
+    delete manifest;
+}
+
+QImage KoResourceBundle::image() const
+{
+    return thumbnail;
+}
+
 bool KoResourceBundle::load()
 {
-    setValid(true);
+    man.setReadPack(fileName());
+    if(man.bad()){
+        //Le fichier n'existe pas
+        manifest=new KoXmlResourceBundleManifest();
+        meta=new KoXmlResourceBundleManifest();
+    }
+    else{
+        //Le fichier existe
+        //TODO Tester si getfile suffit au lieu de getfiledata
+        //TODO Vérifier si on peut éviter de recréer manifest et meta à chaque \
load +        manifest=new \
KoXmlResourceBundleManifest(man->getFileData("manifest.xml")); +        meta=new \
KoXmlResourceBundleManifest(man->getFileData("meta.xml")); +        \
thumbnail.load(man->getFile("thumbnail.jpg"); +        setValid(true);
+    }
     return true;
 }
 
 bool KoResourceBundle::save()
 {
+    if(man.bad()){
+        //Le fichier n'existe pas
+        meta.addTags(manifest.getTags());
+    }
+    man.setWritePack(fileName());
+    man.createPack(manifest,meta);
+    setValid(true);
     return true;
 }
 
+void KoResourceBundle::addFile(QString fileType,QString filePath)
+{
+    manifest->addTag(fileType,filePath);
+    //TODO Voir s'il faut copier ou pas le fichier tout de suite
+    //TODO Cas où le paquet n'est pas installé...
+}
 
-QString KoResourceBundle::defaultFileExtension() const{
+void KoResourceBundle::removeFile(QString fileName)
+{
+    QList<QString> list=manifest->removeFile(fileName);
+    for (int i=0;i<list.size();i++) {
+        meta->removeFirstTag("tag",list.at(i));
+    }
+}
+
+QString KoResourceBundle::defaultFileExtension() const
+{
     return QString(".zip");
 }
 
-QImage KoResourceBundle::image() const{
+QImage KoResourceBundle::image() const
+{
 	return thumbnail;
 }
diff --git a/libs/widgets/KoResourceBundle.h b/libs/widgets/KoResourceBundle.h
index 2ca3185..fd10ff7 100644
--- a/libs/widgets/KoResourceBundle.h
+++ b/libs/widgets/KoResourceBundle.h
@@ -23,13 +23,19 @@
 #include "KoResource.h"
 #include "kowidgets_export.h"
 
+class KoXmlResourceBundleManifest;
+class KoXmlResourceBundleMeta;
+class KoResourceBundleManager;
+
 
 class KOWIDGETS_EXPORT KoResourceBundle : public KoResource
 {
 
 public:
     KoResourceBundle(QString const&);
-    
+        
+    ~KoResourceBundle();
+
     /**
      * Load this resource.
      */
@@ -49,9 +55,13 @@ public:
     /// Returns the default file extension which should be when saving the resource
     QString defaultFileExtension() const;
 
+    void addFile();
+    
 private:
     QImage thumbnail;
-
+    KoXmlResourceBundleManifest* manifest;
+    KoXmlResourceBundleMeta* meta;
+    KoResourceBundleManager* man;
 };
 
 #endif // KORESOURCEBUNDLE_H
diff --git a/libs/widgets/KoResourceBundleManager.cpp \
b/libs/widgets/KoResourceBundleManager.cpp new file mode 100644
index 0000000..4dbe86e
--- /dev/null
+++ b/libs/widgets/KoResourceBundleManager.cpp
@@ -0,0 +1,218 @@
+/* This file is part of the KDE project
+   Copyright (C) 2014, Victor Lafon <metabolic.ewilan@hotmail.fr>
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "KoResourceBundleManager.h"
+#include <QtCore/QFile>
+
+KoResourceBundleManager::KoResourceBundleManager(QString kPath,QString \
pName,KoStore::Mode mode):kritaPath(kPath),packName(pName) +{
+    if (packName!="") {
+        resourcePack=KoStore::createStore(packName,mode,"",KoStore::Zip);
+    }
+}
+
+KoResourceBundleManager::KoResourceBundleManager(KoStore* store,QString \
kPath):kritaPath(kPath) +{
+    resourcePack=store;
+}
+
+void KoResourceBundleManager::setReadPack(QString packName)
+{
+    if (packName!="") {
+        resourcePack=KoStore::createStore(packName,KoStore::Read,"",KoStore::Zip);
+        this->packName=packName;
+    }
+}
+
+void KoResourceBundleManager::setWritePack(QString packName)
+{
+    if (packName!="") {
+        resourcePack=KoStore::createStore(packName,KoStore::Write,"",KoStore::Zip);
+        this->packName=packName;
+    }
+}
+
+void KoResourceBundleManager::setKritaPath(QString kritaPath)
+{
+    this->kritaPath=kritaPath;
+}
+
+bool KoResourceBundleManager::isPathSet()
+{
+    return kritaPath=="";
+}
+
+void KoResourceBundleManager::toRoot()
+{
+    while(resourcePack->leaveDirectory());
+}
+
+bool KoResourceBundleManager::addKFile(QString path)
+{
+    toRoot();
+    int cpt=path.count('/');
+    return resourcePack->addLocalFile(path,path.section('/',cpt-2,cpt-2).append
+            (path.section('/',cpt)));
+}
+
+void KoResourceBundleManager::addKFiles(QString* pathList)
+{
+    for (int cpt=0;cpt<pathList->length();cpt++) {
+        if (!addKFile(pathList[cpt])) {
+            delete [] pathList;
+            exit(1);
+        }
+    }
+    delete [] pathList;
+}
+
+void KoResourceBundleManager::extractKFiles(QString* pathList)
+{
+    QString currentPath;
+    if (isPathSet()) {
+        for (int i=0;i<pathList->length();i++) {
+            toRoot();
+            currentPath=pathList[i];
+            if (!resourcePack->extractFile(currentPath,kritaPath.append(currentPath))) \
{ +                delete [] pathList;
+                exit(1);
+            }
+        }
+    }
+    delete [] pathList;
+}
+
+void KoResourceBundleManager::extractPack(QString packName)
+{
+    QFile *file;
+    this->packName=packName;
+    toRoot();
+    //TODO extractThumbnail();
+    KoXmlResourceBundleManifest* manifest=new \
KoXmlResourceBundleManifest(getFile("manifest.xml")); +    \
extractKFiles(manifest->getFileList()); //TODO getFileList() doit gérer le \
sous-dossier portant le nom du paquet +}
+
+void KoResourceBundleManager::createPack(KoXmlResourceBundleManifest manifest, \
KoXmlResourceBundleMeta meta) +{
+    if (meta.getName()!="") {
+        packName=meta.getName();
+        resourcePack=KoStore::createStore(packName,KoStore::Write,"",KoStore::Zip);
+
+        if (resourcePack!=NULL) {
+            addKFiles(manifest.getFileList());
+            //TODO addThumbnail();
+            resourcePack->finalize();
+        }
+    }
+}
+
+QByteArray KoResourceBundleManager::getFileData(const QString &fileName)
+{
+    QByteArray result;
+
+    if (hasFile(fileName)) {
+        if (isOpen()) {
+            close();
+        }
+        open(fileName);
+        while (!atEnd()) {
+            result+=read(size());
+        }
+        close();
+    }
+    return result;
+}
+
+QIODevice* KoResourceBundleManager::getFile(const QString &fileName)
+{
+    if (hasFile(fileName)) {
+        if (isOpen()) {
+            close();
+        }
+        open(fileName);
+        return resourcePack->device();
+    }
+}
+
+
+//File Method Shortcuts
+
+bool KoResourceBundleManager::hasFile(const QString &name) const
+{
+    return resourcePack->hasFile(name);
+}
+
+bool KoResourceBundleManager::open(const QString &name)
+{
+    return resourcePack->open(name);
+}
+
+bool KoResourceBundleManager::isOpen() const
+{
+    return resourcePack->isOpen();
+}
+
+bool KoResourceBundleManager::close()
+{
+    return resourcePack->close();
+}
+
+QByteArray KoResourceBundleManager::read(qint64 max)
+{
+    return resourcePack->read(max);
+}
+
+qint64 KoResourceBundleManager::read(char *_buffer, qint64 _len)
+{
+    return resourcePack->read(_buffer,_len);
+}
+
+qint64 KoResourceBundleManager::write(const QByteArray &_data)
+{
+    return resourcePack->write(_data);
+}
+
+qint64 KoResourceBundleManager::write(const char *_data, qint64 _len)
+{
+    return resourcePack->write(_data,_len);
+}
+
+qint64 KoResourceBundleManager::size() const
+{
+    return resourcePack->size();
+}
+
+bool KoResourceBundleManager::atEnd() const
+{
+    return resourcePack->atEnd();
+}
+
+bool KoResourceBundleManager::enterDirectory(const QString &directory)
+{
+    return resourcePack->enterDirectory(directory);
+}
+
+bool KoResourceBundleManager::leaveDirectory()
+{
+    return resourcePack->leaveDirectory();
+}
+
+QIODevice* KoResourceBundleManager::device()
+{
+    return resourcePack->device();
+}
+
diff --git a/libs/widgets/KoResourceBundleManager.h \
b/libs/widgets/KoResourceBundleManager.h new file mode 100644
index 0000000..28dce2b
--- /dev/null
+++ b/libs/widgets/KoResourceBundleManager.h
@@ -0,0 +1,154 @@
+/* This file is part of the KDE project
+   Copyright (C) 2014, Victor Lafon <metabolic.ewilan@hotmail.fr>
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef KORESOURCEBUNDLEMANAGER_H
+#define KORESOURCEBUNDLEMANAGER_H
+
+#include <QString>
+#include "KoXmlResourceBundleManifest.h"
+#include "KoXmlResourceBundleMeta.h"
+#include "KoStore.h"
+
+
+class KoResourceBundleManager
+{
+    ///@description the virtual resource package
+    KoStore* resourcePack;
+
+public:
+    ///@description the path containing main Krita resources
+    QString kritaPath;
+    QString packName;
+
+    /**
+     * Constructor
+     * Creates a virtual XML file.
+     * @param packName the name of the package to be opened/created
+     * @param kritaPath the path containing main Krita resources
+     * @param mode the opening mode for the file
+     */
+    KoResourceBundleManager(QString kritaPath="",QString packName="",KoStore::Mode \
mode=KoStore::Write); +
+    /**
+     * Constructor
+     * Creates a virtual XML file.
+     * @param store the existing store to be used as resource pack
+     * @param kritaPath the path containing main Krita resources
+     */
+    KoResourceBundleManager(KoStore *store,QString kritaPath="");
+
+    /**
+     * Opens the store in Read mode.
+     * @param packName the name of the package to be opened
+     */
+    void setReadPack(QString packName);
+
+    /**
+     * Opens the store in Write mode.
+     * @param packName the name of the package to be opened/created
+     */
+    void setWritePack(QString packName);
+
+    /**
+     * Update the path attribute.
+     * @param kritaPath the path containing main Krita resources
+     */
+    void setKritaPath(QString kritaPath);
+
+    ///@return true if the path is set (eq. !=""), false otherwise.
+    bool isPathSet();
+
+    /**
+     * Set current directory to the root of the resource package.
+     */
+    void toRoot();
+
+    /**
+     * Add a Krita resource file to the store.
+     * @param path the path containing the Krita resource File.
+     * @return true if the file has been added, false otherwise.
+     */
+    bool addKFile(QString path);
+
+    /**
+     * Add several Krita resource files to the store.
+     * @param pathList the list containing all the paths of the files to be added.
+     */
+    void addKFiles(QString *pathList);
+
+    /**
+     * Extract several Krita resource files from the store to Krita resource path.
+     * @param packName the name of the package to be extracted
+     */
+    void extractKFiles(QString *pathList);
+
+
+    /**
+     * Extract a full resource package.
+     * @param pathList the list containing all the paths of the files to be \
extracted. +     */
+    void extractPack(QString packName);
+
+    /**
+     * Create a full resource package.
+     * @param manifest the virtual generator of manifest file
+     * @param manifest the virtual generator of meta file
+     */
+    void createPack(KoXmlResourceBundleManifest manifest, KoXmlResourceBundleMeta \
meta); +
+    /**
+     * @return a QByteArray containing data of the file in the store
+     * @param fileName the path of the file in the store
+     */
+    QByteArray getFileData(const QString &fileName);
+
+    /**
+     * @return a QIODevice containing the file in the store
+     * @param fileName the path of the file in the store
+     */
+    QIODevice* getFile(const QString &fileName);
+
+    ///File Method shortcuts
+
+    bool hasFile(const QString &name) const;
+    bool open(const QString &name);
+    bool isOpen() const;
+    bool close();
+    QByteArray read(qint64 max);
+    qint64 write(const QByteArray &_data);
+    qint64 read(char *_buffer, qint64 _len);
+    qint64 write(const char *_data, qint64 _len);
+    qint64 size() const;
+    bool atEnd() const;
+    bool enterDirectory(const QString &directory);
+    bool leaveDirectory();
+    QIODevice* device();
+
+
+    //TODO The following methods are not implemented for the moment.
+    //Their specifications are given as potential but not final.
+
+    //TODO QFile* extractManifest();
+    //TODO void extractMeta();
+    //TODO void extractThumbnail();
+
+    //TODO addManifest(KoXmlManifest);
+    //TODO addMeta(KoXmlMeta);
+    //TODO addThumbnail();
+};
+
+#endif // KORESOURCEBUNDLEMANAGER_H
diff --git a/libs/widgets/KoXmlGenerator.cpp b/libs/widgets/KoXmlGenerator.cpp
index 88d6277..a52faa8 100644
--- a/libs/widgets/KoXmlGenerator.cpp
+++ b/libs/widgets/KoXmlGenerator.cpp
@@ -16,7 +16,6 @@
 */
 
 #include "KoXmlGenerator.h"
-#include <QtXml/QDomElement>
 #include <QFile>
 #include <QTextStream>
 #include <cstdlib>
@@ -128,6 +127,28 @@ bool KoXmlGenerator::removeFirstTag(QString tagName,QString \
textValue)  }
 }
 
+bool KoXmlGenerator::removeFirstTag(QString tagName,QString attName,QString \
attValue) +{
+    QDomNodeList tagList=xmlDocument.elementsByTagName(tagName);
+
+    if (tagList.isEmpty()) {
+        return false;
+    }
+    else {
+        for(int i=0;i<tagList.size();i++){
+            QDomNode prov=tagList.at(i);
+            QDomAttr att=prov.toElement().attributeNode(attName);
+            if (!att.isNull()){
+                if(att.value()==attValue){
+                    prov.parentNode().removeChild(prov);
+                    return true;
+                }
+            }
+        }
+    }
+    return false;
+}
+
 void KoXmlGenerator::removeTag(QString tagName)
 {
     QDomNodeList tagList=xmlDocument.elementsByTagName(tagName);
diff --git a/libs/widgets/KoXmlGenerator.h b/libs/widgets/KoXmlGenerator.h
index f86e9a5..c9cd228 100644
--- a/libs/widgets/KoXmlGenerator.h
+++ b/libs/widgets/KoXmlGenerator.h
@@ -42,11 +42,18 @@ public:
     /**
      * Constructor
      * Create a virtual XML file.
-     * @param xmlName the name of the XML file to be created
+     * @param xmlFileName the name of the XML file to be created
      */
-    KoXmlGenerator(QString);
+    KoXmlGenerator(QString xmlFileName);
 
-    KoXmlGenerator(QByteArray,QString);
+    /**
+     * Constructor
+     * Create a virtual XML file.
+     * @param data the name of the XML file to be created
+     * @param xmlFileName the name of the XML file to be created
+     */
+
+    KoXmlGenerator(QByteArray data,QString xmlFileName);
 
     /**
      * Constructor
@@ -86,6 +93,14 @@ public:
     bool removeFirstTag(QString tagName,QString textValue="");
 
     /**
+     * Remove the first tag having the same name, and same text value if defined.
+     * @param tagName the name of the tag to be removed.
+     * @param textValue the text linked to the tag.
+     * @return true if a tag has been removed, false otherwise.
+     */
+    bool removeFirstTag(QString tagName,QString attName,QString attValue);
+
+    /**
      * Remove all the tags having the same name.
      * @param tagName the name of the tag to be removed.
      */
diff --git a/libs/widgets/KoXmlResourceBundleMeta.cpp \
b/libs/widgets/KoXmlResourceBundleMeta.cpp index 9e5dd70..2569b10 100644
--- a/libs/widgets/KoXmlResourceBundleMeta.cpp
+++ b/libs/widgets/KoXmlResourceBundleMeta.cpp
@@ -16,6 +16,7 @@
 */
 
 #include "KoXmlResourceBundleMeta.h"
+#include <QList>
 
 KoXmlResourceBundleMeta::KoXmlResourceBundleMeta(QString \
xmlName):KoXmlGenerator(xmlName)  {
@@ -135,7 +136,7 @@ QDomElement KoXmlResourceBundleMeta::addTag(QString \
tagName,QString textValue,bo  
     int tagEnumValue=getTagEnumValue(tagName);
 
-    if (tagEnumValue!=Other && textValue=="") {
+    if (tagEnumValue==Other && textValue=="") {
         return QDomElement();
     }
     else {
@@ -143,8 +144,7 @@ QDomElement KoXmlResourceBundleMeta::addTag(QString \
tagName,QString textValue,bo  QDomNode node=tagList.item(0);
 
         if (emptyFile || tagEnumValue==Other || node.isNull() || (tagEnumValue==Tag \
                &&
-             searchValue(tagList,textValue).isNull()))
-              {
+             searchValue(tagList,textValue).isNull())) {
             QDomElement child = xmlDocument.createElement(tagName);
             root.appendChild(child);
 
@@ -163,3 +163,15 @@ QDomElement KoXmlResourceBundleMeta::addTag(QString \
tagName,QString textValue,bo  }
 }
 
+void KoXmlResourceBundleMeta::addTags(QList<QString> list)
+{
+    QDomNodeList tagList=xmlDocument.elementsByTagName("tag");
+    QString prov;
+    for (int i=0;i<list.size();i++) {
+        prov=list.at(i);
+        if (prov != "" && searchValue(tagList,prov).isNull()) {
+            root.appendChild(xmlDocument.createElement("tag").appendChild(xmlDocument.createTextNode(prov)));
 +        }
+    }
+}
+
diff --git a/libs/widgets/KoXmlResourceBundleMeta.h \
b/libs/widgets/KoXmlResourceBundleMeta.h index 3fac2b6..9be9e48 100644
--- a/libs/widgets/KoXmlResourceBundleMeta.h
+++ b/libs/widgets/KoXmlResourceBundleMeta.h
@@ -90,6 +90,9 @@ public:
      * @return the element corresponding to the created tag.
      */
     QDomElement addTag(QString tagName,QString textValue="",bool empty=false);
+
+    ///Add all the resource tags in the list if necessary
+    void addTags(QList<QString> list);
 };
 
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic