Saving of songs to zionworx
[kworship.git] / kworship / songdb / KwSongdbVersion.cpp
blob7cd95a08a62fd54a76136beb0da838db36b50a13
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 KwSongdbVersion.cpp
22 * @brief A song version from the database.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwSongdbVersion.h"
27 #include "KwSongdbSong.h"
28 #include "KwSongdbSongBookSong.h"
29 #include "KwSongdb.h"
31 #include <KLocale>
33 #include <QSqlQuery>
34 #include <QVariant>
37 * Constructors + destructor
40 /// Construct a new version for database insertion.
41 KwSongdbVersion::KwSongdbVersion(KwSongdbSong* song)
42 : m_id(-1)
43 , m_song(song)
44 , m_modifiedFields(0)
45 , m_name()
46 , m_writer()
47 , m_copyright()
48 , m_lyrics()
49 , m_songBookNumbersLoaded(false)
50 , m_songBookNumbers()
54 /// Construct from the database.
55 KwSongdbVersion::KwSongdbVersion(int id)
56 : m_id(id)
57 , m_song(0)
58 , m_modifiedFields(0)
59 , m_name()
60 , m_writer()
61 , m_copyright()
62 , m_lyrics()
63 , m_songBookNumbersLoaded(false)
64 , m_songBookNumbers()
66 // Get the song version data
67 QSqlQuery query(KwSongdb::self()->database());
68 query.prepare("SELECT `song_id`, `name`, `writer`, `copyright`, `lyrics`, `css_style_sheet_id` "
69 "FROM `SongVersion` "
70 "WHERE `id` = ?");
71 query.addBindValue(QVariant(id));
72 bool worked = query.exec();
73 Q_ASSERT(worked);
75 // Copy the data
76 Q_ASSERT(query.first());
77 m_song = KwSongdb::self()->songById(query.value(0).toInt());
78 m_name = query.value(1).toString();
79 m_writer = query.value(2).toString();
80 m_copyright = query.value(3).toString();
81 m_lyrics.setMarkup(query.value(4).toString());
83 // Register with songdb
84 KwSongdb::self()->registerVersion(this);
87 /// Destructor.
88 KwSongdbVersion::~KwSongdbVersion()
93 * Accessors
96 /// Get the version id.
97 int KwSongdbVersion::id()
99 return m_id;
102 /// Get the song this is a version of.
103 KwSongdbSong* KwSongdbVersion::song() const
105 return m_song;
108 /// Get the name of this version.
109 QString KwSongdbVersion::name() const
111 return m_name;
114 /// Get a non-empty name of this version.
115 QString KwSongdbVersion::niceName() const
117 if (m_name.isEmpty())
119 return i18n("Default version");
121 else
123 return m_name;
127 /// Get the name of the writer.
128 QString KwSongdbVersion::writer() const
130 return m_writer;
133 /// Get the copyright notice.
134 QString KwSongdbVersion::copyright() const
136 return m_copyright;
139 /// Get the copyright notice.
140 const KwSongdbLyrics& KwSongdbVersion::lyrics() const
142 return m_lyrics;
145 /// Get song book numbers.
146 QList<KwSongdbSongBookSong*> KwSongdbVersion::songBookNumbers()
148 if (!m_songBookNumbersLoaded)
150 Q_ASSERT(m_id >= 0);
152 // Get the song version data
153 QSqlQuery query(KwSongdb::self()->database());
154 query.prepare("SELECT `book_id`, `book_number` "
155 "FROM `SongBookSong` "
156 "WHERE `version_id` = ?");
157 query.addBindValue(QVariant(m_id));
158 bool worked = query.exec();
159 Q_ASSERT(worked);
161 m_songBookNumbersLoaded = true;
163 // Copy the data
164 if (query.first())
166 do {
167 int bookId = query.value(0).toInt();
168 int bookNumber = query.value(1).toInt();
169 KwSongdbSongBook* songBook = KwSongdb::self()->songBookById(bookId);
170 m_songBookNumbers.push_back(new KwSongdbSongBookSong(songBook, bookNumber, this));
171 } while (query.next());
174 return m_songBookNumbers;
178 * Mutators
181 /// Set the name.
182 void KwSongdbVersion::setName(const QString& name)
184 if (name != m_name)
186 m_modifiedFields |= Name;
187 m_name = name;
191 /// Set the writer.
192 void KwSongdbVersion::setWriter(const QString& writer)
194 if (writer != m_writer)
196 m_modifiedFields |= Writer;
197 m_writer = writer;
201 /// Set the copyright notice.
202 void KwSongdbVersion::setCopyright(const QString& copyright)
204 if (copyright != m_copyright)
206 m_modifiedFields |= Copyright;
207 m_copyright = copyright;
211 /// Set the lyrics markup.
212 void KwSongdbVersion::setLyricsMarkup(const QString& markup)
214 if (markup != m_lyrics.markup())
216 m_modifiedFields |= Lyrics;
217 m_lyrics.setMarkup(markup);
221 /// Set the lyrics plain text verses.
222 void KwSongdbVersion::setLyricsPlainVerses(const QStringList& plainVerses)
224 if (plainVerses != m_lyrics.plainVerses())
226 m_modifiedFields |= Lyrics;
227 m_lyrics.setPlainVerses(plainVerses);
231 /// Set the lyrics.
232 void KwSongdbVersion::setLyrics(const KwSongdbLyrics& lyrics)
234 if (lyrics != m_lyrics)
236 m_modifiedFields |= Lyrics;
237 m_lyrics = lyrics;
241 /// Save changes to the version data.
242 void KwSongdbVersion::save()
244 QStringList fields;
245 QList<QVariant> values;
246 Fields handled;
248 // Straightforward modifications
250 if (m_modifiedFields.testFlag(Name))
252 handled |= Name;
253 fields.push_back("`name`=?");
254 values.push_back(QVariant(m_name));
257 if (m_modifiedFields.testFlag(Writer))
259 handled |= Writer;
260 fields.push_back("`writer`=?");
261 values.push_back(QVariant(m_writer));
264 if (m_modifiedFields.testFlag(Copyright))
266 handled |= Copyright;
267 fields.push_back("`copyright`=?");
268 values.push_back(QVariant(m_copyright));
271 if (m_modifiedFields.testFlag(Lyrics))
273 handled |= Lyrics;
274 fields.push_back("`lyrics`=?");
275 values.push_back(QVariant(m_lyrics.markup()));
278 bool insertion = (m_id < 0);
279 if (insertion || !fields.isEmpty())
281 QSqlQuery query(KwSongdb::self()->database());
283 if (insertion)
285 // Insert a new row
286 fields.push_back("`song_id`=?");
287 values.push_back(QVariant(m_song->id()));
288 query.prepare("INSERT INTO `SongVersion` "
289 "SET " + fields.join(","));
291 else
293 query.prepare("UPDATE `SongVersion` "
294 "SET " + fields.join(",") + " "
295 "WHERE id = ?");
296 values.push_back(QVariant(m_id));
299 // Add bind values
300 foreach (QVariant value, values)
302 query.addBindValue(value);
305 // Execute query
306 bool worked = query.exec();
307 Q_ASSERT(worked);
309 // Update which fields are modified
310 m_modifiedFields &= ~handled;
312 // Update relevent objects
313 if (insertion)
315 m_id = query.lastInsertId().toInt();
316 KwSongdb::self()->registerVersion(this);
317 m_song->registerVersion(this);