Basic openoffice.org control, and listening for new presentation documents, still...
[kworship.git] / kworship / archive / KwArchive.cpp
blob874abbf86b69c64f821d17ddef68f6c46499f1f8
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file KwArchive.cpp
22 * @brief A KWorship data archive file.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwArchive.h"
27 #include "KwDataFile.h"
29 #include <KTar>
30 #include <KFilterDev>
32 #include <QFile>
33 #include <QTextStream>
36 * Constructors + destructor
39 /// Primary constructor.
40 KwArchive::KwArchive(QIODevice* dev, bool writing)
41 : KwResourceManager()
42 , m_writing(writing)
43 , m_compressor(0)
44 , m_archive(0)
45 , m_index(0)
46 , m_numPlaylists(-1)
48 // Compression
49 m_compressor = KFilterDev::device(dev, "application/x-bzip2", true);
50 m_compressor->open(writing ? QFile::WriteOnly : QFile::ReadOnly);
52 // Archiving
53 m_archive = new KTar(m_compressor);
54 m_archive->open(writing ? QFile::WriteOnly : QFile::ReadOnly);
56 if (!writing)
58 m_index = loadDataFile("index.kw");
60 else
62 m_index = new KwDataFile();
66 /// Primary constructor.
67 KwArchive::KwArchive(const QString& fileName, bool writing)
68 : KwResourceManager()
69 , m_writing(writing)
70 , m_compressor(0)
71 , m_archive(0)
72 , m_index(0)
73 , m_numPlaylists(-1)
75 // Archiving
76 m_archive = new KTar(fileName, "application/x-bzip2");
77 m_archive->open(writing ? QFile::WriteOnly : QFile::ReadOnly);
79 if (!writing)
81 m_index = loadDataFile("index.kw");
83 else
85 m_index = new KwDataFile();
89 /// Destructor.
90 KwArchive::~KwArchive()
92 if (m_writing)
94 // Finally we need to write out the index file
95 writeDataFile("index.kw", m_index);
97 delete m_index;
99 // Now its safe to close the archive
100 m_archive->close();
101 if (0 != m_compressor)
103 m_compressor->close();
105 delete m_archive;
109 * Accessors
112 /// Get whether the archive is being written.
113 bool KwArchive::isWriting() const
115 return m_writing;
118 /// Get whether the archive is being read.
119 bool KwArchive::isReading() const
121 return !m_writing;
125 * Main resource interface
128 void KwArchive::addResource(const KwResourceLink* link)
133 * Playlist data
136 /// Find how many playlists are in this archive.
137 int KwArchive::numPlaylists()
139 return 0;
142 /// Get a list of playlist names in this archive.
143 QStringList KwArchive::playlists()
145 return QStringList();
148 /// Create a new playlist from the archive.
149 KwPlaylistList* KwArchive::extractPlaylist(QString name)
151 Q_ASSERT(isReading());
153 KwDataFile* playlistFile = loadDataFile("playlist/"+name+".kw");
154 if (0 != playlistFile)
156 return playlistFile->extractPlaylist(this);
158 return 0;
161 /// Add a playlist to the archive.
162 void KwArchive::addPlaylist(KwPlaylistList* playlist)
164 Q_ASSERT(isWriting());
166 KwDataFile* playlistFile = new KwDataFile();
167 playlistFile->insertPlaylist(playlist, this);
168 writeDataFile(QString("playlist/%1.kw").arg(++m_numPlaylists),
169 playlistFile);
170 delete playlistFile;
174 * Access to song data
177 /// Find how many songs are in this archive.
178 int KwArchive::numSongs()
180 return 0;
183 /// Get a list of song names in this archive.
184 QStringList KwArchive::songs()
186 return QStringList();
190 * Access to generic resources
193 /// Find how many resources are in this archive.
194 int KwArchive::numResources()
196 return 0;
199 /// Get a list of resource names in this archive.
200 QStringList KwArchive::resources()
202 return QStringList();
206 * Data files
209 /// Load a data file from the archive.
210 KwDataFile* KwArchive::loadDataFile(QString path)
212 Q_ASSERT(isReading());
214 // Look for the entry
215 const KArchiveEntry* entry = m_archive->directory()->entry(path);
216 if (0 == entry || !entry->isFile())
218 return 0;
221 // Turn entry into a file
222 const KArchiveFile* fileEntry = dynamic_cast<const KArchiveFile*>(entry);
223 Q_ASSERT(0 != fileEntry); // isFile() should mean entry is a KArchiveFile
225 QIODevice* fileDevice = fileEntry->createDevice();
226 KwDataFile* dataFile = new KwDataFile();
227 dataFile->readFrom(fileDevice);
228 delete fileDevice;
230 return dataFile;
233 /// Write a data file to the archive.
234 void KwArchive::writeDataFile(QString path, const KwDataFile* data)
236 Q_ASSERT(isWriting());
238 // Get the raw xml data
239 QByteArray xmlData;
241 QTextStream stream(&xmlData);
242 data->writeTo(stream);
245 // Write this to the archive
246 time_t now = time(0);
247 m_archive->writeFile(path, "user", "group", xmlData, xmlData.size(),
248 0100644, now, now, now);