Change interface to categorise bibles by language, and adapt SWORD bible manager...
[kworship.git] / kworship / KwDocument.cpp
blob7dc0536891dbdd704d3fad52f9584804bdf72bea
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 KwDocument.cpp
22 * @brief A KWorship document (anything saved in a KWorship data file).
23 * @author James Hogan <james@albanarts.com>
26 #include "KwDocument.h"
27 #include "KwArchive.h"
28 #include "KwPlaylistList.h"
30 #include <KSaveFile>
31 #include <KLocale>
32 #include <KMessageBox>
35 * Constructors + destructor
38 /// Primary constructor.
39 KwDocument::KwDocument(KUrl url, QObject* parent)
40 : QObject(parent)
41 , m_url(url)
42 , m_modified(false)
43 , m_playlist(new KwPlaylistList())
47 /// Destructor.
48 KwDocument::~KwDocument()
50 delete m_playlist;
54 * Accessors
57 /// Find whether the document has been modified.
58 bool KwDocument::isModified() const
60 return m_modified;
63 /// Find whether the document has ever been saved.
64 bool KwDocument::isSaved() const
66 return !m_url.isEmpty();
69 /// Get the URL the document is saved at.
70 KUrl KwDocument::url() const
72 return m_url;
75 /// Get the main playlist.
76 KwPlaylistList* KwDocument::playlist()
78 return m_playlist;
82 * Saving and loading actions
85 /// Load the file.
86 void KwDocument::reload()
88 // Start off by opening the archive file
89 if (!m_url.isLocalFile())
91 KMessageBox::error(0,
92 i18n("Non-local loads not yet supported"),
93 i18n("KWorship"));
94 return;
97 // Create a new archive object and fill it
98 KwArchive* archive = new KwArchive(m_url.toLocalFile(), false);
99 loadFromArchive(archive);
100 delete archive;
102 setModified(false);
105 /// Save the file.
106 void KwDocument::save()
108 Q_ASSERT(isSaved());
110 // Start off by opening the archive file
111 if (!m_url.isLocalFile())
113 KMessageBox::error(0,
114 i18n("Non-local saves not yet supported"),
115 i18n("KWorship"));
116 return;
118 KSaveFile file;
119 file.setFileName(m_url.toLocalFile());
120 if (!file.open(QFile::WriteOnly))
122 KMessageBox::error(0,
123 i18n("Cannot write file %1:\n%2.")
124 .arg(file.fileName())
125 .arg(file.errorString()),
126 i18n("KWorship"));
127 return;
130 // Create a new archive object and fill it
131 KwArchive* archive = new KwArchive(&file, true);
132 saveToArchive(archive);
133 delete archive;
135 if (!file.finalize())
137 KMessageBox::error(0,
138 i18n("Cannot finalize file %1:\n%2.")
139 .arg(file.fileName())
140 .arg(file.errorString()),
141 i18n("KWorship"));
142 return;
145 setModified(false);
148 /// Save the file to a different URL.
149 void KwDocument::saveAs(const KUrl& url)
151 m_url = url;
152 save();
156 * Other slots
159 /// Set whether the file is modified.
160 void KwDocument::setModified(bool modified)
162 if (m_modified != modified)
164 m_modified = modified;
165 modifiedChanged(modified);
170 * Archive interface.
173 /// Load from an archive.
174 void KwDocument::loadFromArchive(KwArchive* archive)
176 Q_ASSERT(archive->isReading());
178 KwPlaylistList* playlist = archive->extractPlaylist("0");
179 if (0 != playlist)
181 delete m_playlist;
182 m_playlist = playlist;
183 playlistReset();
187 /// Save to an archive.
188 void KwDocument::saveToArchive(KwArchive* archive) const
190 Q_ASSERT(archive->isWriting());
192 archive->addPlaylist(m_playlist);