1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
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. *
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. *
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 ***************************************************************************/
22 * @brief A KWorship data archive file.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwArchive.h"
27 #include "KwDataFile.h"
33 #include <QTextStream>
36 * Constructors + destructor
39 /// Primary constructor.
40 KwArchive::KwArchive(QIODevice
* dev
, bool writing
)
49 m_compressor
= KFilterDev::device(dev
, "application/x-bzip2", true);
50 m_compressor
->open(writing
? QFile::WriteOnly
: QFile::ReadOnly
);
53 m_archive
= new KTar(m_compressor
);
54 m_archive
->open(writing
? QFile::WriteOnly
: QFile::ReadOnly
);
58 m_index
= loadDataFile("index.kw");
62 m_index
= new KwDataFile();
66 /// Primary constructor.
67 KwArchive::KwArchive(const QString
& fileName
, bool writing
)
76 m_archive
= new KTar(fileName
, "application/x-bzip2");
77 m_archive
->open(writing
? QFile::WriteOnly
: QFile::ReadOnly
);
81 m_index
= loadDataFile("index.kw");
85 m_index
= new KwDataFile();
90 KwArchive::~KwArchive()
94 // Finally we need to write out the index file
95 writeDataFile("index.kw", m_index
);
99 // Now its safe to close the archive
101 if (0 != m_compressor
)
103 m_compressor
->close();
112 /// Get whether the archive is being written.
113 bool KwArchive::isWriting() const
118 /// Get whether the archive is being read.
119 bool KwArchive::isReading() const
125 * Main resource interface
128 void KwArchive::addResource(const KwResourceLink
* link
)
136 /// Find how many playlists are in this archive.
137 int KwArchive::numPlaylists()
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);
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
),
174 * Access to song data
177 /// Find how many songs are in this archive.
178 int KwArchive::numSongs()
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()
199 /// Get a list of resource names in this archive.
200 QStringList
KwArchive::resources()
202 return QStringList();
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())
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
);
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
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
);