File indexing completed on 2024-05-12 04:06:26
0001 /* 0002 SPDX-FileCopyrightText: 2011 Stefan Majewsky <majewsky@gmx.net> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "components.h" 0008 0009 #include <QFile> 0010 0011 Palapeli::CopyComponent::CopyComponent(Palapeli::Puzzle* puzzle) 0012 : m_puzzle(puzzle) 0013 { 0014 } 0015 0016 Palapeli::PuzzleComponent* Palapeli::CopyComponent::cast(Type type) const 0017 { 0018 //get component from other puzzle 0019 if (type == Metadata) 0020 { 0021 const Palapeli::PuzzleComponent* c = m_puzzle->get(Metadata); 0022 const Palapeli::MetadataComponent* cmp = dynamic_cast<const Palapeli::MetadataComponent*>(c); 0023 return cmp ? new Palapeli::MetadataComponent(cmp->metadata) : nullptr; 0024 } 0025 else if (type == Contents) 0026 { 0027 const Palapeli::PuzzleComponent* c = m_puzzle->get(Contents); 0028 const Palapeli::ContentsComponent* cmp = dynamic_cast<const Palapeli::ContentsComponent*>(c); 0029 return cmp ? new Palapeli::ContentsComponent(cmp->contents) : nullptr; 0030 } 0031 else if (type == CreationContext) 0032 { 0033 const Palapeli::PuzzleComponent* c = m_puzzle->get(CreationContext); 0034 const Palapeli::CreationContextComponent* cmp = dynamic_cast<const Palapeli::CreationContextComponent*>(c); 0035 return cmp ? new Palapeli::CreationContextComponent(cmp->creationContext) : nullptr; 0036 } 0037 //casts for writing an archive 0038 else if (type == DirectoryStorage) 0039 return Palapeli::DirectoryStorageComponent::fromData(puzzle()); 0040 else if (type == ArchiveStorage) 0041 { 0042 if (!m_puzzle->component(ArchiveStorage) && !m_puzzle->component(CollectionStorage)) 0043 return Palapeli::ArchiveStorageComponent::fromData(puzzle()); 0044 //optimization: if the other puzzle has an archive or collection 0045 //storage available, copy that instead of recreating everything 0046 m_puzzle->get(ArchiveStorage); 0047 QFile otherFile(m_puzzle->location()); 0048 if (otherFile.copy(puzzle()->location())) 0049 return new Palapeli::ArchiveStorageComponent; 0050 else 0051 return nullptr; 0052 } 0053 //unknown type requested 0054 else 0055 return nullptr; 0056 }