2016-02-13 12:51:56 +00:00
|
|
|
/*
|
2020-06-15 17:42:37 +02:00
|
|
|
Copyright 2006-2020 The QElectroTech Team
|
2016-02-13 12:51:56 +00:00
|
|
|
This file is part of QElectroTech.
|
|
|
|
|
|
|
|
QElectroTech is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
QElectroTech 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "elementcollectionhandler.h"
|
|
|
|
#include "renamedialog.h"
|
2016-02-21 18:53:40 +00:00
|
|
|
#include "xmlelementcollection.h"
|
2016-07-18 17:37:02 +00:00
|
|
|
#include "qetproject.h"
|
2016-02-21 18:53:40 +00:00
|
|
|
#include "qetxml.h"
|
2016-02-13 12:51:56 +00:00
|
|
|
#include <QFile>
|
|
|
|
#include <QDir>
|
|
|
|
|
2016-03-09 10:17:28 +00:00
|
|
|
/******************************************************/
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
ECHStrategy::ECHStrategy(ElementsLocation &source, ElementsLocation &destination) :
|
2016-02-13 12:51:56 +00:00
|
|
|
m_source(source),
|
|
|
|
m_destination (destination)
|
|
|
|
{}
|
|
|
|
|
|
|
|
ECHStrategy::~ECHStrategy() {}
|
|
|
|
|
|
|
|
/******************************************************/
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
ECHSFileToFile::ECHSFileToFile(ElementsLocation &source, ElementsLocation &destination) :
|
2016-02-13 12:51:56 +00:00
|
|
|
ECHStrategy(source, destination)
|
|
|
|
{}
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
ElementsLocation ECHSFileToFile::copy()
|
2016-02-13 12:51:56 +00:00
|
|
|
{
|
|
|
|
//Check if the destination already have an item with the same name of the item to copy
|
2016-03-15 15:23:11 +00:00
|
|
|
ElementsLocation location(m_destination.fileSystemPath() + "/" + m_source.fileName());
|
2016-02-13 12:51:56 +00:00
|
|
|
QString rename;
|
|
|
|
if (location.exist())
|
|
|
|
{
|
|
|
|
RenameDialog rd(location.fileSystemPath());
|
|
|
|
if (rd.exec() == QDialog::Accepted)
|
|
|
|
{
|
|
|
|
if (rd.selectedAction() == QET::Erase)
|
|
|
|
{
|
|
|
|
if (location.isDirectory())
|
|
|
|
{
|
|
|
|
QDir dir(location.fileSystemPath());
|
|
|
|
dir.removeRecursively();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QFile file(location.fileSystemPath());
|
|
|
|
file.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (rd.selectedAction() == QET::Rename)
|
|
|
|
{
|
|
|
|
rename = rd.newName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-03-15 15:23:11 +00:00
|
|
|
return ElementsLocation();
|
2016-02-13 12:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_source.isElement())
|
|
|
|
return copyElement(m_source, m_destination, rename);
|
|
|
|
else
|
|
|
|
return copyDirectory(m_source, m_destination, rename);
|
|
|
|
}
|
|
|
|
|
2018-07-19 14:14:31 +00:00
|
|
|
ElementsLocation ECHSFileToFile::copyDirectory(ElementsLocation &source, ElementsLocation &destination, const QString& rename)
|
2016-02-13 12:51:56 +00:00
|
|
|
{
|
|
|
|
QDir source_dir(source.fileSystemPath());
|
|
|
|
QDir destination_dir(destination.fileSystemPath());
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
if (!source_dir.exists() || !destination_dir.exists()) return ElementsLocation();
|
2016-02-13 12:51:56 +00:00
|
|
|
|
|
|
|
QString new_dir_name = rename.isEmpty() ? source_dir.dirName() : rename;
|
|
|
|
|
|
|
|
//Create a new dir
|
|
|
|
if (destination_dir.mkdir(new_dir_name))
|
|
|
|
{
|
|
|
|
//The new created directory
|
|
|
|
QDir created_dir(destination_dir.canonicalPath() + "/" + new_dir_name);
|
|
|
|
|
|
|
|
//Copy the qet_directory file
|
|
|
|
QFile::copy(source_dir.canonicalPath() + "/qet_directory", created_dir.canonicalPath() + "/qet_directory");
|
|
|
|
|
|
|
|
//Copy all dirs found in source_dir to destination_dir
|
2016-03-15 15:23:11 +00:00
|
|
|
ElementsLocation created_location(created_dir.canonicalPath());
|
2017-01-03 16:47:39 +00:00
|
|
|
//Used this bool when user drop a folder into itself to avoid infinite recursive creation of the dropped dir
|
|
|
|
bool copy_itself = false;
|
|
|
|
if (source_dir == destination_dir)
|
|
|
|
copy_itself = true;
|
|
|
|
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach(QString str, source_dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name))
|
2017-01-03 16:47:39 +00:00
|
|
|
{
|
|
|
|
if (copy_itself)
|
|
|
|
{
|
|
|
|
if (source_dir.dirName() == str)
|
|
|
|
{
|
|
|
|
copy_itself = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
ElementsLocation sub_source(source.fileSystemPath() + "/" + str);
|
2017-01-03 16:47:39 +00:00
|
|
|
copyDirectory(sub_source, created_location);
|
|
|
|
}
|
2016-02-13 12:51:56 +00:00
|
|
|
|
|
|
|
//Copy all elements found in source_dir to destination_dir
|
|
|
|
source_dir.setNameFilters(QStringList() << "*.elmt");
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach(QString str, source_dir.entryList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name))
|
2016-02-13 12:51:56 +00:00
|
|
|
{
|
2016-03-15 15:23:11 +00:00
|
|
|
ElementsLocation sub_source(source.fileSystemPath() + "/" + str);
|
2016-02-13 12:51:56 +00:00
|
|
|
copyElement(sub_source, created_location);
|
|
|
|
}
|
|
|
|
|
|
|
|
return created_location;
|
|
|
|
}
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
return ElementsLocation();
|
2016-02-13 12:51:56 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 14:14:31 +00:00
|
|
|
ElementsLocation ECHSFileToFile::copyElement(ElementsLocation &source, ElementsLocation &destination, const QString& rename)
|
2016-02-13 12:51:56 +00:00
|
|
|
{
|
|
|
|
QString new_elmt_name = rename.isEmpty() ? source.fileName() : rename;
|
|
|
|
bool rb = QFile::copy(source.fileSystemPath(), destination.fileSystemPath() + "/" + new_elmt_name);
|
|
|
|
if (rb)
|
2018-12-13 21:39:44 +00:00
|
|
|
{
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
//On windows when user drag and drop an element from the common elements collection
|
|
|
|
//to the custom elements collection, the element file stay in read only mode, and so
|
|
|
|
//user can't save the element
|
|
|
|
extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
|
|
|
|
qt_ntfs_permission_lookup++;
|
|
|
|
QFile file(destination.fileSystemPath() + "/" + new_elmt_name);
|
|
|
|
if (!file.isWritable()) {
|
|
|
|
if (!file.setPermissions(file.permissions() | QFileDevice::WriteUser)) {
|
|
|
|
qDebug() << "Failed to change file permission of : " << QFileInfo(file).canonicalFilePath() \
|
|
|
|
<< " in ECHSFileToFile::copyElement";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qt_ntfs_permission_lookup--;
|
|
|
|
#endif
|
2016-03-15 15:23:11 +00:00
|
|
|
return ElementsLocation (destination.fileSystemPath() + "/" + new_elmt_name);
|
2018-12-13 21:39:44 +00:00
|
|
|
}
|
2016-02-13 12:51:56 +00:00
|
|
|
else
|
2016-03-15 15:23:11 +00:00
|
|
|
return ElementsLocation();
|
2016-02-13 12:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************/
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
ECHSXmlToFile::ECHSXmlToFile(ElementsLocation &source, ElementsLocation &destination) :
|
2016-03-09 10:17:28 +00:00
|
|
|
ECHStrategy(source, destination)
|
|
|
|
{}
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
ElementsLocation ECHSXmlToFile::copy()
|
2016-03-09 10:17:28 +00:00
|
|
|
{
|
|
|
|
//Check if the destination already have an item with the same name of the item to copy
|
2016-03-15 15:23:11 +00:00
|
|
|
ElementsLocation location(m_destination.fileSystemPath() + "/" + m_source.fileName());
|
2016-03-09 10:17:28 +00:00
|
|
|
QString rename;
|
|
|
|
if (location.exist())
|
|
|
|
{
|
|
|
|
RenameDialog rd(location.fileSystemPath());
|
|
|
|
if (rd.exec() == QDialog::Accepted)
|
|
|
|
{
|
|
|
|
if (rd.selectedAction() == QET::Erase)
|
|
|
|
{
|
|
|
|
if (location.isDirectory())
|
|
|
|
{
|
|
|
|
QDir dir(location.fileSystemPath());
|
|
|
|
dir.removeRecursively();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QFile file(location.fileSystemPath());
|
|
|
|
file.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (rd.selectedAction() == QET::Rename)
|
|
|
|
{
|
|
|
|
rename = rd.newName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-03-15 15:23:11 +00:00
|
|
|
return ElementsLocation();
|
2016-03-09 10:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_source.isElement())
|
|
|
|
return copyElement(m_source, m_destination, rename);
|
|
|
|
else
|
|
|
|
return copyDirectory(m_source, m_destination, rename);
|
|
|
|
}
|
|
|
|
|
2018-07-19 14:14:31 +00:00
|
|
|
ElementsLocation ECHSXmlToFile::copyDirectory(ElementsLocation &source, ElementsLocation &destination, const QString& rename)
|
2016-03-09 10:17:28 +00:00
|
|
|
{
|
|
|
|
QDir destination_dir(destination.fileSystemPath());
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
if (!(destination_dir.exists() && source.exist())) return ElementsLocation();
|
2016-03-09 10:17:28 +00:00
|
|
|
|
|
|
|
QString new_dir_name = rename.isEmpty() ? source.fileName() : rename;
|
|
|
|
|
|
|
|
//Create new dir
|
|
|
|
if (destination_dir.mkdir(new_dir_name))
|
|
|
|
{
|
|
|
|
QDir created_dir(destination_dir.canonicalPath() + "/" + new_dir_name);
|
2016-03-15 15:23:11 +00:00
|
|
|
ElementsLocation created_location(created_dir.canonicalPath());
|
2016-03-09 10:17:28 +00:00
|
|
|
|
|
|
|
//Create the qet-directory file
|
|
|
|
QDomDocument document;
|
|
|
|
QDomElement root = document.createElement("qet-directory");
|
|
|
|
document.appendChild(root);
|
|
|
|
root.appendChild(source.nameList().toXml(document));
|
|
|
|
|
|
|
|
QString filepath = created_dir.canonicalPath() + "/qet_directory";
|
|
|
|
QET::writeXmlFile(document, filepath);
|
|
|
|
|
|
|
|
//Create all directory found in source to created_dir
|
|
|
|
XmlElementCollection *project_collection = source.projectCollection();
|
|
|
|
|
|
|
|
QStringList directories_names = project_collection->directoriesNames( project_collection->directory(source.collectionPath(false)) );
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach(QString name, directories_names)
|
2016-03-09 10:17:28 +00:00
|
|
|
{
|
2016-03-15 15:23:11 +00:00
|
|
|
ElementsLocation sub_source_dir(source.projectCollectionPath() + "/" + name);
|
2016-03-09 10:17:28 +00:00
|
|
|
copyDirectory(sub_source_dir, created_location);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create all elements found in source to destination
|
|
|
|
QStringList elements_names = project_collection->elementsNames( project_collection->directory(source.collectionPath(false))) ;
|
2017-02-05 16:18:50 +00:00
|
|
|
foreach (QString name, elements_names)
|
2016-03-09 10:17:28 +00:00
|
|
|
{
|
2016-03-15 15:23:11 +00:00
|
|
|
ElementsLocation source_element(source.projectCollectionPath() + "/" + name);
|
2016-03-09 10:17:28 +00:00
|
|
|
copyElement(source_element, created_location);
|
|
|
|
}
|
|
|
|
|
|
|
|
return created_location;
|
|
|
|
}
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
return ElementsLocation();
|
2016-03-09 10:17:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 14:14:31 +00:00
|
|
|
ElementsLocation ECHSXmlToFile::copyElement(ElementsLocation &source, ElementsLocation &destination, const QString& rename)
|
2016-03-09 10:17:28 +00:00
|
|
|
{
|
2016-03-15 15:23:11 +00:00
|
|
|
if (!(destination.exist() && source.exist())) return ElementsLocation();
|
2016-03-09 10:17:28 +00:00
|
|
|
|
|
|
|
QString new_element_name = rename.isEmpty() ? source.fileName() : rename;
|
|
|
|
|
|
|
|
//Get the xml descrption of the element
|
|
|
|
QDomDocument document;
|
|
|
|
document.appendChild(document.importNode(source.xml(), true));
|
|
|
|
|
|
|
|
//Create the .elmt file
|
|
|
|
QString filepath = destination.fileSystemPath() + "/" + new_element_name;
|
|
|
|
if (QET::writeXmlFile(document, filepath))
|
2016-03-15 15:23:11 +00:00
|
|
|
return ElementsLocation(filepath);
|
2016-03-09 10:17:28 +00:00
|
|
|
else
|
2016-03-15 15:23:11 +00:00
|
|
|
return ElementsLocation();
|
2016-03-09 10:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************/
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
ECHSToXml::ECHSToXml(ElementsLocation &source, ElementsLocation &destination) :
|
2016-02-21 18:53:40 +00:00
|
|
|
ECHStrategy(source, destination)
|
|
|
|
{}
|
|
|
|
|
2016-03-15 15:23:11 +00:00
|
|
|
ElementsLocation ECHSToXml::copy()
|
2016-02-21 18:53:40 +00:00
|
|
|
{
|
2016-03-15 15:23:11 +00:00
|
|
|
if (!(m_source.exist() && m_destination.isDirectory() && m_destination.isProject())) return ElementsLocation();
|
2016-02-21 18:53:40 +00:00
|
|
|
|
|
|
|
//Check if the destination already have an item with the same name of the item to copy
|
2016-03-15 15:23:11 +00:00
|
|
|
ElementsLocation location(m_destination.projectCollectionPath() + "/" + m_source.fileName());
|
2016-02-21 18:53:40 +00:00
|
|
|
|
|
|
|
QString rename;
|
|
|
|
if (location.exist())
|
|
|
|
{
|
|
|
|
RenameDialog rd(location.collectionPath());
|
|
|
|
if(rd.exec() == QDialog::Accepted)
|
|
|
|
{
|
|
|
|
if (rd.selectedAction() == QET::Rename)
|
|
|
|
rename = rd.newName();
|
|
|
|
}
|
|
|
|
else
|
2016-03-15 15:23:11 +00:00
|
|
|
return ElementsLocation();
|
2016-02-21 18:53:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_destination.projectCollection()->copy(m_source, m_destination, rename);
|
|
|
|
}
|
|
|
|
|
2016-03-09 10:17:28 +00:00
|
|
|
/******************************************************/
|
|
|
|
|
2016-02-13 12:51:56 +00:00
|
|
|
/**
|
|
|
|
* @brief ElementCollectionHandler::ElementCollectionHandler
|
|
|
|
* @param widget
|
|
|
|
*/
|
|
|
|
ElementCollectionHandler::ElementCollectionHandler() {}
|
|
|
|
|
|
|
|
ElementCollectionHandler::~ElementCollectionHandler()
|
|
|
|
{
|
|
|
|
if (m_strategy) delete m_strategy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ElementCollectionHandler::copy
|
|
|
|
* Copy the content of collection represented by source to the collection represented by destination.
|
|
|
|
* Destination must be a directory, else the copy do nothing and return a null ElementLocation
|
|
|
|
* if destination have an item with the same name of source, a dialog ask to user what to do.
|
|
|
|
* @param source
|
|
|
|
* @param destination
|
|
|
|
* @return
|
|
|
|
*/
|
2016-03-15 15:23:11 +00:00
|
|
|
ElementsLocation ElementCollectionHandler::copy(ElementsLocation &source, ElementsLocation &destination)
|
2016-02-13 12:51:56 +00:00
|
|
|
{
|
2016-03-15 15:23:11 +00:00
|
|
|
if (!source.exist() || !destination.exist() || destination.isElement()) return ElementsLocation();
|
2016-02-13 12:51:56 +00:00
|
|
|
|
|
|
|
if (source.isFileSystem() && destination.isFileSystem()) m_strategy = new ECHSFileToFile(source, destination);
|
2016-03-09 10:17:28 +00:00
|
|
|
if (source.isProject() && destination.isFileSystem()) m_strategy = new ECHSXmlToFile(source, destination);
|
2016-03-06 14:40:52 +00:00
|
|
|
else if (destination.isProject()) m_strategy = new ECHSToXml(source, destination);
|
2016-02-13 12:51:56 +00:00
|
|
|
|
|
|
|
if (m_strategy)
|
|
|
|
return m_strategy->copy();
|
|
|
|
else
|
2016-03-15 15:23:11 +00:00
|
|
|
return ElementsLocation();
|
2016-02-13 12:51:56 +00:00
|
|
|
}
|
2016-05-12 15:41:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ElementCollectionHandler::createDir
|
|
|
|
* Create a directorie with name @name as child of @parent.
|
|
|
|
* Parent must be a directory
|
|
|
|
* @param parent : parent of the dir to create
|
|
|
|
* @param name : name of directorie to create
|
|
|
|
* @param name_list : translations of the directorie name
|
2018-03-11 13:38:41 +00:00
|
|
|
* @return : ElementsLocation that represent the new directorie, location can be null if an error was occurred
|
2016-05-12 15:41:55 +00:00
|
|
|
*/
|
|
|
|
ElementsLocation ElementCollectionHandler::createDir(ElementsLocation &parent, const QString &name, const NamesList &name_list)
|
|
|
|
{
|
|
|
|
//Parent must be a directorie and writable
|
|
|
|
if (!(parent.isDirectory() && parent.isWritable() && parent.exist())) {
|
|
|
|
qDebug() << "ElementCollectionHandler::createDir : the prerequisites are not valid. " << parent;
|
|
|
|
return ElementsLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Directorie to create must not already exist
|
|
|
|
ElementsLocation created_dir = parent;
|
|
|
|
created_dir.addToPath(name);
|
|
|
|
if (created_dir.exist()) {
|
|
|
|
return ElementsLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Location is a file system
|
|
|
|
if (parent.isFileSystem()) {
|
|
|
|
|
|
|
|
QDir parent_dir(parent.fileSystemPath());
|
|
|
|
|
|
|
|
if (parent_dir.mkdir(name)) {
|
|
|
|
//Create the qet-directory file
|
|
|
|
QDomDocument document;
|
|
|
|
QDomElement root = document.createElement("qet-directory");
|
|
|
|
document.appendChild(root);
|
|
|
|
root.appendChild(name_list.toXml(document));
|
|
|
|
|
|
|
|
QString filepath = created_dir.fileSystemPath() + "/qet_directory";
|
|
|
|
if (!QET::writeXmlFile(document, filepath)) {
|
|
|
|
qDebug() << "ElementCollectionHandler::createDir : write qet-directory file failed";
|
|
|
|
}
|
|
|
|
return created_dir;
|
|
|
|
}
|
|
|
|
else {
|
2018-03-11 13:38:41 +00:00
|
|
|
qDebug() << "ElementCollectionHandler::createDir : error was occurred at creation of new directories in file system. ";
|
2016-05-12 15:41:55 +00:00
|
|
|
return ElementsLocation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (parent.isProject()) {
|
|
|
|
XmlElementCollection *xmlec = parent.projectCollection();
|
|
|
|
if (xmlec->createDir(parent.collectionPath(false), name, name_list)) {
|
|
|
|
return created_dir;
|
|
|
|
}
|
|
|
|
else {
|
2018-03-11 13:38:41 +00:00
|
|
|
qDebug() << "ElementCollectionHandler::createDir : error was occurred at creation of new directories in embbeded collection.";
|
2016-05-12 15:41:55 +00:00
|
|
|
return ElementsLocation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ElementsLocation();
|
|
|
|
}
|
|
|
|
|
2016-07-18 17:37:02 +00:00
|
|
|
/**
|
|
|
|
* @brief ElementCollectionHandler::importFromProject
|
|
|
|
* Import the element represented by @location to the embedded collection of @project at the same path.
|
|
|
|
* @location must represente an element owned by a project embedded collection
|
|
|
|
* @param project : project where copy the element
|
|
|
|
* @param location : location to copy
|
|
|
|
* @return true if import with success
|
|
|
|
*/
|
|
|
|
bool ElementCollectionHandler::importFromProject(QETProject *project, ElementsLocation &location)
|
|
|
|
{
|
|
|
|
if (!(location.isElement() && location.exist() && location.isProject())) return false;
|
|
|
|
|
|
|
|
ElementsLocation destination(location.collectionPath(false), project);
|
|
|
|
if (destination.exist()) return true;
|
|
|
|
|
|
|
|
QList <QString> names;
|
|
|
|
|
|
|
|
//Get the parent of location and find if exist in embedded collection of project
|
|
|
|
ElementsLocation source = location.parent();
|
|
|
|
names.append(location.fileName());
|
|
|
|
|
|
|
|
destination = ElementsLocation(source.collectionPath(), project);
|
|
|
|
|
|
|
|
//Go back until to find an existing directory in destination
|
|
|
|
while (!destination.exist()) {
|
|
|
|
names.append(source.fileName());
|
|
|
|
source = source.parent();
|
|
|
|
destination = ElementsLocation(source.collectionPath(), project);
|
|
|
|
}
|
|
|
|
|
|
|
|
XmlElementCollection *collection = project->embeddedElementCollection();
|
|
|
|
|
|
|
|
while (!names.isEmpty()) {
|
|
|
|
source.addToPath(names.takeLast());
|
|
|
|
destination = collection->copy(source, destination, QString(), false);
|
|
|
|
|
|
|
|
if (!destination.exist())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-12 15:41:55 +00:00
|
|
|
/**
|
|
|
|
* @brief ElementCollectionHandler::setNames
|
|
|
|
* Set the names stored in @name_list as the names of the item represented by location
|
|
|
|
* @param location : location to change the names
|
|
|
|
* @param name_list : NamesList to use
|
|
|
|
* @return return true if success
|
|
|
|
*/
|
|
|
|
bool ElementCollectionHandler::setNames(ElementsLocation &location, const NamesList &name_list)
|
|
|
|
{
|
|
|
|
if ( !(location.exist() && location.isWritable()) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (location.isFileSystem()) {
|
|
|
|
if (location.isDirectory()) {
|
|
|
|
QDomDocument document;
|
|
|
|
QDomElement root = document.createElement("qet-directory");
|
|
|
|
document.appendChild(root);
|
|
|
|
root.appendChild(name_list.toXml(document));
|
|
|
|
|
|
|
|
QString filepath = location.fileSystemPath() + "/qet_directory";
|
|
|
|
if (!QET::writeXmlFile(document, filepath)) {
|
|
|
|
qDebug() << "ElementCollectionHandler::setNames : write qet-directory file failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (location.isElement()) {
|
|
|
|
QDomDocument document;
|
|
|
|
document.appendChild(document.importNode(location.xml(), true));
|
|
|
|
if (document.isNull()) {
|
|
|
|
qDebug() << "ElementCollectionHandler::setNames : failed to load xml document from file";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDomElement document_element = document.documentElement();
|
|
|
|
document_element.replaceChild(name_list.toXml(document), document_element.firstChildElement("names"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (location.isProject()) {
|
|
|
|
QDomElement element = location.xml();
|
|
|
|
QDomDocument document = element.ownerDocument();
|
|
|
|
element.replaceChild(name_list.toXml(document), element.firstChildElement("names"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|