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 ***************************************************************************/
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"
32 #include <KMessageBox>
35 * Constructors + destructor
38 /// Primary constructor.
39 KwDocument::KwDocument(KUrl url
, QObject
* parent
)
43 , m_playlist(new KwPlaylistList())
48 KwDocument::~KwDocument()
57 /// Find whether the document has been modified.
58 bool KwDocument::isModified() const
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
75 /// Get the main playlist.
76 KwPlaylistList
* KwDocument::playlist()
82 * Saving and loading actions
86 void KwDocument::reload()
88 // Start off by opening the archive file
89 if (!m_url
.isLocalFile())
92 i18n("Non-local loads not yet supported"),
97 // Create a new archive object and fill it
98 KwArchive
* archive
= new KwArchive(m_url
.toLocalFile(), false);
99 loadFromArchive(archive
);
106 void KwDocument::save()
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"),
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()),
130 // Create a new archive object and fill it
131 KwArchive
* archive
= new KwArchive(&file
, true);
132 saveToArchive(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()),
148 /// Save the file to a different URL.
149 void KwDocument::saveAs(const KUrl
& url
)
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
);
173 /// Load from an archive.
174 void KwDocument::loadFromArchive(KwArchive
* archive
)
176 Q_ASSERT(archive
->isReading());
178 KwPlaylistList
* playlist
= archive
->extractPlaylist("0");
182 m_playlist
= playlist
;
187 /// Save to an archive.
188 void KwDocument::saveToArchive(KwArchive
* archive
) const
190 Q_ASSERT(archive
->isWriting());
192 archive
->addPlaylist(m_playlist
);