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 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"
37 * Constructors + destructor
40 /// Construct a new version for database insertion.
41 KwSongdbVersion::KwSongdbVersion(KwSongdbSong
* song
)
49 , m_songBookNumbersLoaded(false)
54 /// Construct from the database.
55 KwSongdbVersion::KwSongdbVersion(int id
)
63 , m_songBookNumbersLoaded(false)
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` "
71 query
.addBindValue(QVariant(id
));
72 bool worked
= query
.exec();
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);
88 KwSongdbVersion::~KwSongdbVersion()
96 /// Get the version id.
97 int KwSongdbVersion::id()
102 /// Get the song this is a version of.
103 KwSongdbSong
* KwSongdbVersion::song() const
108 /// Get the name of this version.
109 QString
KwSongdbVersion::name() const
114 /// Get a non-empty name of this version.
115 QString
KwSongdbVersion::niceName() const
117 if (m_name
.isEmpty())
119 return i18n("Default version");
127 /// Get the name of the writer.
128 QString
KwSongdbVersion::writer() const
133 /// Get the copyright notice.
134 QString
KwSongdbVersion::copyright() const
139 /// Get the copyright notice.
140 const KwSongdbLyrics
& KwSongdbVersion::lyrics() const
145 /// Get song book numbers.
146 QList
<KwSongdbSongBookSong
*> KwSongdbVersion::songBookNumbers()
148 if (!m_songBookNumbersLoaded
)
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();
161 m_songBookNumbersLoaded
= true;
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
;
182 void KwSongdbVersion::setName(const QString
& name
)
186 m_modifiedFields
|= Name
;
192 void KwSongdbVersion::setWriter(const QString
& writer
)
194 if (writer
!= m_writer
)
196 m_modifiedFields
|= 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
);
232 void KwSongdbVersion::setLyrics(const KwSongdbLyrics
& lyrics
)
234 if (lyrics
!= m_lyrics
)
236 m_modifiedFields
|= Lyrics
;
241 /// Save changes to the version data.
242 void KwSongdbVersion::save()
245 QList
<QVariant
> values
;
248 // Straightforward modifications
250 if (m_modifiedFields
.testFlag(Name
))
253 fields
.push_back("`name`=?");
254 values
.push_back(QVariant(m_name
));
257 if (m_modifiedFields
.testFlag(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
))
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());
286 fields
.push_back("`song_id`=?");
287 values
.push_back(QVariant(m_song
->id()));
288 query
.prepare("INSERT INTO `SongVersion` "
289 "SET " + fields
.join(","));
293 query
.prepare("UPDATE `SongVersion` "
294 "SET " + fields
.join(",") + " "
296 values
.push_back(QVariant(m_id
));
300 foreach (QVariant value
, values
)
302 query
.addBindValue(value
);
306 bool worked
= query
.exec();
309 // Update which fields are modified
310 m_modifiedFields
&= ~handled
;
312 // Update relevent objects
315 m_id
= query
.lastInsertId().toInt();
316 KwSongdb::self()->registerVersion(this);
317 m_song
->registerVersion(this);