| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include <stack> |
| 24 |
|
| 25 |
#include <QCoreApplication> |
| 26 |
#include <zipios++/zipinputstream.h> |
| 27 |
|
| 28 |
#include <App/Document.h> |
| 29 |
#include <Base/Reader.h> |
| 30 |
#include <Base/Writer.h> |
| 31 |
|
| 32 |
#include "MergeDocuments.h" |
| 33 |
|
| 34 |
|
| 35 |
using namespace App; |
| 36 |
namespace sp = std::placeholders; |
| 37 |
|
| 38 |
namespace App |
| 39 |
{ |
| 40 |
|
| 41 |
class XMLMergeReader: public Base::XMLReader |
| 42 |
{ |
| 43 |
public: |
| 44 |
XMLMergeReader(std::map<std::string, std::string>& name, |
| 45 |
const char* FileName, |
| 46 |
std::istream& str) |
| 47 |
: Base::XMLReader(FileName, str) |
| 48 |
, nameMap(name) |
| 49 |
{} |
| 50 |
|
| 51 |
void addName(const char* s1, const char* s2) override |
| 52 |
{ |
| 53 |
nameMap[s1] = s2; |
| 54 |
} |
| 55 |
const char* getName(const char* name) const override |
| 56 |
{ |
| 57 |
std::map<std::string, std::string>::const_iterator it = nameMap.find(name); |
| 58 |
if (it != nameMap.end()) { |
| 59 |
return it->second.c_str(); |
| 60 |
} |
| 61 |
else { |
| 62 |
return name; |
| 63 |
} |
| 64 |
} |
| 65 |
bool doNameMapping() const override |
| 66 |
{ |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
protected: |
| 71 |
private: |
| 72 |
std::map<std::string, std::string>& nameMap; |
| 73 |
using PropertyTag = std::pair<std::string, std::string>; |
| 74 |
std::stack<PropertyTag> propertyStack; |
| 75 |
}; |
| 76 |
} // namespace App |
| 77 |
|
| 78 |
MergeDocuments::MergeDocuments(App::Document* doc) |
| 79 |
: appdoc(doc) |
| 80 |
{ |
| 81 |
// NOLINTBEGIN |
| 82 |
connectExport = doc->signalExportObjects.connect( |
| 83 |
std::bind(&MergeDocuments::exportObject, this, sp::_1, sp::_2)); |
| 84 |
connectImport = doc->signalImportObjects.connect( |
| 85 |
std::bind(&MergeDocuments::importObject, this, sp::_1, sp::_2)); |
| 86 |
// NOLINTEND |
| 87 |
|
| 88 |
QCoreApplication* app = QCoreApplication::instance(); |
| 89 |
if (app && app->inherits("QApplication")) { |
| 90 |
guiup = true; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
MergeDocuments::~MergeDocuments() |
| 95 |
{ |
| 96 |
connectExport.disconnect(); |
| 97 |
connectImport.disconnect(); |
| 98 |
} |
| 99 |
|
| 100 |
unsigned int MergeDocuments::getMemSize() const |
| 101 |
{ |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
std::vector<App::DocumentObject*> MergeDocuments::importObjects(std::istream& input) |
| 106 |
{ |
| 107 |
this->nameMap.clear(); |
| 108 |
this->stream = new zipios::ZipInputStream(input); |
| 109 |
XMLMergeReader reader(this->nameMap, "<memory>", *stream); |
| 110 |
reader.setVerbose(isVerbose()); |
| 111 |
std::vector<App::DocumentObject*> objs = appdoc->importObjects(reader); |
| 112 |
|
| 113 |
delete this->stream; |
| 114 |
this->stream = nullptr; |
| 115 |
|
| 116 |
return objs; |
| 117 |
} |
| 118 |
|
| 119 |
void MergeDocuments::importObject(const std::vector<App::DocumentObject*>& o, Base::XMLReader& r) |
| 120 |
{ |
| 121 |
objects = o; |
| 122 |
Restore(r); |
| 123 |
r.readFiles(*this->stream); |
| 124 |
} |
| 125 |
|
| 126 |
void MergeDocuments::exportObject(const std::vector<App::DocumentObject*>& o, Base::Writer& w) |
| 127 |
{ |
| 128 |
objects = o; |
| 129 |
Save(w); |
| 130 |
} |
| 131 |
|
| 132 |
void MergeDocuments::Save(Base::Writer& w) const |
| 133 |
{ |
| 134 |
// Save view provider stuff |
| 135 |
if (guiup) { |
| 136 |
w.addFile("GuiDocument.xml", this); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
void MergeDocuments::Restore(Base::XMLReader& r) |
| 141 |
{ |
| 142 |
// Restore view provider stuff |
| 143 |
if (guiup) { |
| 144 |
r.addFile("GuiDocument.xml", this); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
void MergeDocuments::SaveDocFile(Base::Writer& w) const |
| 149 |
{ |
| 150 |
// Save view provider stuff |
| 151 |
appdoc->signalExportViewObjects(this->objects, w); |
| 152 |
} |
| 153 |
|
| 154 |
void MergeDocuments::RestoreDocFile(Base::Reader& r) |
| 155 |
{ |
| 156 |
// Restore view provider stuff |
| 157 |
appdoc->signalImportViewObjects(this->objects, r, this->nameMap); |
| 158 |
} |