File: elementcollectionhandler.cpp

package info (click to toggle)
qelectrotech 1%3A0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 91,680 kB
  • sloc: cpp: 62,335; xml: 750; sh: 264; perl: 258; makefile: 6
file content (489 lines) | stat: -rw-r--r-- 16,104 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
                Copyright 2006-2019 The QElectroTech Team
                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"
#include "xmlelementcollection.h"
#include "qetproject.h"
#include "qetxml.h"
#include <QFile>
#include <QDir>

/******************************************************/

ECHStrategy::ECHStrategy(ElementsLocation &source, ElementsLocation &destination) :
    m_source(source),
    m_destination (destination)
{}

ECHStrategy::~ECHStrategy() {}

/******************************************************/

ECHSFileToFile::ECHSFileToFile(ElementsLocation &source, ElementsLocation &destination) :
    ECHStrategy(source, destination)
{}

ElementsLocation ECHSFileToFile::copy()
{
		//Check if the destination already have an item with the same name of the item to copy
	ElementsLocation location(m_destination.fileSystemPath() + "/" + m_source.fileName());
	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
			return ElementsLocation();
	}

	if (m_source.isElement())
		return copyElement(m_source, m_destination, rename);
    else
		return copyDirectory(m_source, m_destination, rename);
}

ElementsLocation ECHSFileToFile::copyDirectory(ElementsLocation &source, ElementsLocation &destination, const QString& rename)
{
    QDir source_dir(source.fileSystemPath());
    QDir destination_dir(destination.fileSystemPath());

	if (!source_dir.exists() || !destination_dir.exists()) return ElementsLocation();

    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
		ElementsLocation created_location(created_dir.canonicalPath());
			//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;

		foreach(QString str, source_dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name))
		{
			if (copy_itself)
			{
				if (source_dir.dirName() == str)
				{
					copy_itself = false;
					continue;
				}
			}

			ElementsLocation sub_source(source.fileSystemPath() + "/" + str);
			copyDirectory(sub_source, created_location);
		}

            //Copy all elements found in source_dir to destination_dir
        source_dir.setNameFilters(QStringList() << "*.elmt");
        foreach(QString str, source_dir.entryList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name))
        {
			ElementsLocation sub_source(source.fileSystemPath() + "/" + str);
            copyElement(sub_source, created_location);
        }

        return created_location;
    }

	return ElementsLocation();
}

ElementsLocation ECHSFileToFile::copyElement(ElementsLocation &source, ElementsLocation &destination, const QString& rename)
{
    QString new_elmt_name = rename.isEmpty() ? source.fileName() : rename;
    bool rb = QFile::copy(source.fileSystemPath(), destination.fileSystemPath() + "/" + new_elmt_name);
    if (rb)
	{
#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
		return ElementsLocation (destination.fileSystemPath() + "/" + new_elmt_name);
	}
    else
		return ElementsLocation();
}

/******************************************************/

ECHSXmlToFile::ECHSXmlToFile(ElementsLocation &source, ElementsLocation &destination) :
	ECHStrategy(source, destination)
{}

ElementsLocation ECHSXmlToFile::copy()
{
		//Check if the destination already have an item with the same name of the item to copy
	ElementsLocation location(m_destination.fileSystemPath() + "/" + m_source.fileName());
	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
			return ElementsLocation();
	}

	if (m_source.isElement())
		return copyElement(m_source, m_destination, rename);
	else
		return copyDirectory(m_source, m_destination, rename);
}

ElementsLocation ECHSXmlToFile::copyDirectory(ElementsLocation &source, ElementsLocation &destination, const QString& rename)
{
	QDir destination_dir(destination.fileSystemPath());

	if (!(destination_dir.exists() && source.exist())) return ElementsLocation();

	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);
		ElementsLocation created_location(created_dir.canonicalPath());

			//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)) );
		foreach(QString name, directories_names)
		{
			ElementsLocation sub_source_dir(source.projectCollectionPath() + "/" + name);
			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))) ;
		foreach (QString name, elements_names)
		{
			ElementsLocation source_element(source.projectCollectionPath() + "/" + name);
			copyElement(source_element, created_location);
		}

		return created_location;
	}

	return ElementsLocation();
}

ElementsLocation ECHSXmlToFile::copyElement(ElementsLocation &source, ElementsLocation &destination, const QString& rename)
{	
	if (!(destination.exist() && source.exist())) return ElementsLocation();

	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))
		return ElementsLocation(filepath);
	else
		return ElementsLocation();
}

/******************************************************/

ECHSToXml::ECHSToXml(ElementsLocation &source, ElementsLocation &destination) :
	ECHStrategy(source, destination)
{}

ElementsLocation ECHSToXml::copy()
{
	if (!(m_source.exist() && m_destination.isDirectory() && m_destination.isProject())) return ElementsLocation();

		//Check if the destination already have an item with the same name of the item to copy
	ElementsLocation location(m_destination.projectCollectionPath() + "/" + m_source.fileName());

	QString rename;
	if (location.exist())
	{
		RenameDialog rd(location.collectionPath());
		if(rd.exec() == QDialog::Accepted)
		{
			if (rd.selectedAction() == QET::Rename)
				rename = rd.newName();
		}
		else
			return ElementsLocation();
	}

	return m_destination.projectCollection()->copy(m_source, m_destination, rename);
}

/******************************************************/

/**
 * @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
 */
ElementsLocation ElementCollectionHandler::copy(ElementsLocation &source, ElementsLocation &destination)
{
	if (!source.exist() || !destination.exist() || destination.isElement()) return ElementsLocation();

    if (source.isFileSystem() && destination.isFileSystem()) m_strategy = new ECHSFileToFile(source, destination);
	if (source.isProject() && destination.isFileSystem()) m_strategy = new ECHSXmlToFile(source, destination);
	else if (destination.isProject()) m_strategy = new ECHSToXml(source, destination);

	if (m_strategy)
		return m_strategy->copy();
	else
		return ElementsLocation();
}

/**
 * @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
 * @return : ElementsLocation that represent the new directorie, location can be null if an error was occurred
 */
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 {
			qDebug() << "ElementCollectionHandler::createDir : error was occurred at creation of new directories in file system. ";
			return ElementsLocation();
		}
	}
	else if (parent.isProject()) {
		XmlElementCollection *xmlec = parent.projectCollection();
		if (xmlec->createDir(parent.collectionPath(false), name, name_list)) {
			return created_dir;
		}
		else {
			qDebug() << "ElementCollectionHandler::createDir : error was occurred at creation of new directories in embbeded collection.";
			return ElementsLocation();
		}
	}

	return ElementsLocation();
}

/**
 * @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;
}

/**
 * @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;
}