~x3lectric/xbmc/svn-trunk

7104 by darkdonno
changed: file cosmetics removed: trailing whitespace fixed: some TAB usage added: GPL Header
1
/*
10178 by vulkanr
merge with trunk. revision 13100.
2
 *      Copyright (C) 2005-2008 Team XBMC
3
 *      http://www.xbmc.org
7104 by darkdonno
changed: file cosmetics removed: trailing whitespace fixed: some TAB usage added: GPL Header
4
 *
5
 *  This Program 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, or (at your option)
8
 *  any later version.
9
 *
10
 *  This Program 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
10178 by vulkanr
merge with trunk. revision 13100.
16
 *  along with XBMC; see the file COPYING.  If not, write to
7104 by darkdonno
changed: file cosmetics removed: trailing whitespace fixed: some TAB usage added: GPL Header
17
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18
 *  http://www.gnu.org/copyleft/gpl.html
19
 *
20
 */
21
17279 by spiff_
changed: svn nuke stdafx.h
22
#include "system.h"
8035 by yuvalt
Initial port to Linux/SDL
23
#include "MusicDatabase.h"
24
#include "FileSystem/cddb.h"
25
#include "FileSystem/DirectoryCache.h"
26
#include "FileSystem/MusicDatabaseDirectory/DirectoryNode.h"
27
#include "FileSystem/MusicDatabaseDirectory/QueryParams.h"
9868 by vulkanr
merge with trunk. rev. 12480.
28
#include "FileSystem/MusicDatabaseDirectory.h"
13642 by jmarshallnz
changed: Got rid of the remaining P/Q/T/U/Z usage, by moving the mapping out of IoSupport and into it's own class.
29
#include "FileSystem/SpecialProtocol.h"
3746 by jmarshallnz
- 26-07-2005 changed: Skin files are loaded on demand rather than on boot.
30
#include "GUIDialogMusicScan.h"
9868 by vulkanr
merge with trunk. rev. 12480.
31
#include "utils/GUIInfoManager.h"
10043 by vulkanr
merge with trunk. rev. 12805.
32
#include "MusicInfoTag.h"
21369 by alcoheca
cosmetic/tidyup move all addon classes to /xbmc/addons/
33
#include "addons/AddonManager.h"
34
#include "addons/Scraper.h"
35
#include "addons/Addon.h"
10043 by vulkanr
merge with trunk. rev. 12805.
36
#include "Util.h"
37
#include "Artist.h"
38
#include "Album.h"
39
#include "Song.h"
40
#include "GUIWindowManager.h"
41
#include "GUIDialogOK.h"
42
#include "GUIDialogProgress.h"
43
#include "GUIDialogYesNo.h"
44
#include "GUIDialogSelect.h"
45
#include "FileSystem/File.h"
16986 by spiff_
changed: don't include guisettings.h in settings.h
46
#include "GUISettings.h"
16985 by spiff_
changed: refactor. advancedsettings to separate file
47
#include "AdvancedSettings.h"
10043 by vulkanr
merge with trunk. rev. 12805.
48
#include "FileItem.h"
11407 by jmarshallnz
fixed: Don't attempt CDDB lookup if no network is connected (Ticket #3151).
49
#include "Application.h"
15779 by mcgeagh
Fixed issue with missing define for Karaoke when building without it... again
50
#ifdef HAS_KARAOKE
13479 by gyunaev
Merging with karaoke branch.
51
#include "karaoke/karaokelyricsfactory.h"
15779 by mcgeagh
Fixed issue with missing define for Karaoke when building without it... again
52
#endif
16402 by wiso
replaced CDetectDVDMedia::GetCdInfo() by g_mediaManager.GetCdInfo()
53
#include "MediaManager.h"
17027 by spiff_
changed: don't include settings.h in headers
54
#include "Settings.h"
17150 by spiff_
changed: move TIME_FORMAT definition to DateTime.h for sanity.
55
#include "StringUtils.h"
17035 by spiff_
changed: don't include localizestrings.h in stdafx.h
56
#include "LocalizeStrings.h"
17150 by spiff_
changed: move TIME_FORMAT definition to DateTime.h for sanity.
57
#include "utils/log.h"
17686 by davilla
[all] replace winapi timeGetTime/GetTickCOunt with CTimeUtils::GetTimeMS. Will need linux/win verify.
58
#include "utils/TimeUtils.h"
21497 by jmarshallnz
fixed: Clear any cached textures when thumbs are changed
59
#include "TextureCache.h"
3618 by bobbin007
fixed: ftp server can not be shut down via settings menu
60
9396 by vulkanr
merge with trunk. revision: 11693.
61
using namespace std;
62
using namespace AUTOPTR;
7175 by spiff_
fixed: severe global namespace pollution
63
using namespace XFILE;
64
using namespace MUSICDATABASEDIRECTORY;
21055 by elupus
merged: addons-fw branch
65
using ADDON::AddonPtr;
4886 by bobbin007
fixed: music nav, after getting album information the thumb of the item is not updated
66
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
67
#define RECENTLY_PLAYED_LIMIT 25
7893 by jmarshallnz
changed: Search tab in music library only does a full text search if the search string is at least 3 characters long. Otherwise, it just searches on the first word.
68
#define MIN_FULL_SEARCH_LENGTH 3
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
69
17019 by yuvalt
honor HAS_DVD_DRIVE
70
#ifdef HAS_DVD_DRIVE
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
71
using namespace CDDB;
17019 by yuvalt
honor HAS_DVD_DRIVE
72
#endif
105 by yamp
new music database
73
74
CMusicDatabase::CMusicDatabase(void)
75
{
76
}
77
78
CMusicDatabase::~CMusicDatabase(void)
79
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
80
  EmptyCache();
118 by yamp
no message
81
}
82
20932 by jmarshallnz
added: Initial support for mysql database backend, thanks to firnsy (part of #8169)
83
bool CMusicDatabase::Open()
84
{
85
  return CDatabase::Open(g_advancedSettings.m_databaseMusic);
86
}
87
105 by yamp
new music database
88
bool CMusicDatabase::CreateTables()
89
{
14931 by vdrfan
changed: use transactions to speed-up initial database creation + cosmetics
90
  BeginTransaction();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
91
  try
92
  {
3740 by bobbin007
added: base class for database classes
93
    CDatabase::CreateTables();
2734 by jmarshallnz
- 03-03-2005 added: Cached thumb location is now stored in the database. Music database SHOULD auto-update when you first access My Music.
94
1769 by tslayer
- fixed: Fallback skin is now Project Mayhem and not MediaCenter
95
    CLog::Log(LOGINFO, "create artist table");
20929 by jmarshallnz
changed: Indexed columns in the database should be varchar rather than text so that the db layout is more portable. Thanks to firnsy (part of #8169)
96
    m_pDS->exec("CREATE TABLE artist ( idArtist integer primary key, strArtist varchar(256))\n");
1769 by tslayer
- fixed: Fallback skin is now Project Mayhem and not MediaCenter
97
    CLog::Log(LOGINFO, "create album table");
20929 by jmarshallnz
changed: Indexed columns in the database should be varchar rather than text so that the db layout is more portable. Thanks to firnsy (part of #8169)
98
    m_pDS->exec("CREATE TABLE album ( idAlbum integer primary key, strAlbum varchar(256), idArtist integer, strExtraArtists text, idGenre integer, strExtraGenres text, iYear integer, idThumb integer)\n");
1769 by tslayer
- fixed: Fallback skin is now Project Mayhem and not MediaCenter
99
    CLog::Log(LOGINFO, "create genre table");
20929 by jmarshallnz
changed: Indexed columns in the database should be varchar rather than text so that the db layout is more portable. Thanks to firnsy (part of #8169)
100
    m_pDS->exec("CREATE TABLE genre ( idGenre integer primary key, strGenre varchar(256))\n");
1769 by tslayer
- fixed: Fallback skin is now Project Mayhem and not MediaCenter
101
    CLog::Log(LOGINFO, "create path table");
20929 by jmarshallnz
changed: Indexed columns in the database should be varchar rather than text so that the db layout is more portable. Thanks to firnsy (part of #8169)
102
    m_pDS->exec("CREATE TABLE path ( idPath integer primary key, strPath varchar(512), strHash text)\n");
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
103
    CLog::Log(LOGINFO, "create song table");
20929 by jmarshallnz
changed: Indexed columns in the database should be varchar rather than text so that the db layout is more portable. Thanks to firnsy (part of #8169)
104
    m_pDS->exec("CREATE TABLE song ( idSong integer primary key, idAlbum integer, idPath integer, idArtist integer, strExtraArtists text, idGenre integer, strExtraGenres text, strTitle varchar(512), iTrack integer, iDuration integer, iYear integer, dwFileNameCRC text, strFileName text, strMusicBrainzTrackID text, strMusicBrainzArtistID text, strMusicBrainzAlbumID text, strMusicBrainzAlbumArtistID text, strMusicBrainzTRMID text, iTimesPlayed integer, iStartOffset integer, iEndOffset integer, idThumb integer, lastplayed varchar(20) default NULL, rating char default '0', comment text)\n");
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
105
    CLog::Log(LOGINFO, "create albuminfo table");
9868 by vulkanr
merge with trunk. rev. 12480.
106
    m_pDS->exec("CREATE TABLE albuminfo ( idAlbumInfo integer primary key, idAlbum integer, iYear integer, idGenre integer, strExtraGenres text, strMoods text, strStyles text, strThemes text, strReview text, strImage text, strLabel text, strType text, iRating integer)\n");
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
107
    CLog::Log(LOGINFO, "create albuminfosong table");
108
    m_pDS->exec("CREATE TABLE albuminfosong ( idAlbumInfoSong integer primary key, idAlbumInfo integer, iTrack integer, strTitle text, iDuration integer)\n");
109
    CLog::Log(LOGINFO, "create thumb table");
20929 by jmarshallnz
changed: Indexed columns in the database should be varchar rather than text so that the db layout is more portable. Thanks to firnsy (part of #8169)
110
    m_pDS->exec("CREATE TABLE thumb (idThumb integer primary key, strThumb varchar(256))\n");
9868 by vulkanr
merge with trunk. rev. 12480.
111
    CLog::Log(LOGINFO, "create artistnfo table");
14378 by spiff_
added: ticket #4834 - scraped music fanart. uses http://www.htbackdrops.com - go add your favorite artist. thanks to nuzecast for making the backend available
112
    m_pDS->exec("CREATE TABLE artistinfo ( idArtistInfo integer primary key, idArtist integer, strBorn text, strFormed text, strGenres text, strMoods text, strStyles text, strInstruments text, strBiography text, strDied text, strDisbanded text, strYearsActive text, strImage text, strFanart text)\n");
9868 by vulkanr
merge with trunk. rev. 12480.
113
    CLog::Log(LOGINFO, "create content table");
114
    m_pDS->exec("CREATE TABLE content (strPath text, strScraperPath text, strContent text, strSettings text)\n");
115
    CLog::Log(LOGINFO, "create discography table");
116
    m_pDS->exec("CREATE TABLE discography (idArtist integer, strAlbum text, strYear text)\n");
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
117
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
118
    CLog::Log(LOGINFO, "create exartistsong table");
119
    m_pDS->exec("CREATE TABLE exartistsong ( idSong integer, iPosition integer, idArtist integer)\n");
120
    CLog::Log(LOGINFO, "create extragenresong table");
121
    m_pDS->exec("CREATE TABLE exgenresong ( idSong integer, iPosition integer, idGenre integer)\n");
122
    CLog::Log(LOGINFO, "create exartistalbum table");
123
    m_pDS->exec("CREATE TABLE exartistalbum ( idAlbum integer, iPosition integer, idArtist integer)\n");
124
    CLog::Log(LOGINFO, "create exgenrealbum table");
125
    m_pDS->exec("CREATE TABLE exgenrealbum ( idAlbum integer, iPosition integer, idGenre integer)\n");
126
13479 by gyunaev
Merging with karaoke branch.
127
    CLog::Log(LOGINFO, "create karaokedata table");
128
    m_pDS->exec("CREATE TABLE karaokedata ( iKaraNumber integer, idSong integer, iKaraDelay integer, strKaraEncoding text, "
129
                "strKaralyrics text, strKaraLyrFileCRC text )\n");
130
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
131
    // Indexes
132
    CLog::Log(LOGINFO, "create exartistsong index");
133
    m_pDS->exec("CREATE INDEX idxExtraArtistSong ON exartistsong(idSong)");
134
    m_pDS->exec("CREATE INDEX idxExtraArtistSong2 ON exartistsong(idArtist)");
135
    CLog::Log(LOGINFO, "create exgenresong index");
136
    m_pDS->exec("CREATE INDEX idxExtraGenreSong ON exgenresong(idSong)");
137
    m_pDS->exec("CREATE INDEX idxExtraGenreSong2 ON exgenresong(idGenre)");
138
    CLog::Log(LOGINFO, "create exartistalbum index");
139
    m_pDS->exec("CREATE INDEX idxExtraArtistAlbum ON exartistalbum(idAlbum)");
140
    m_pDS->exec("CREATE INDEX idxExtraArtistAlbum2 ON exartistalbum(idArtist)");
141
    CLog::Log(LOGINFO, "create exgenrealbum index");
142
    m_pDS->exec("CREATE INDEX idxExtraGenreAlbum ON exgenrealbum(idAlbum)");
143
    m_pDS->exec("CREATE INDEX idxExtraGenreAlbum2 ON exgenrealbum(idGenre)");
144
145
    CLog::Log(LOGINFO, "create album index");
146
    m_pDS->exec("CREATE INDEX idxAlbum ON album(strAlbum)");
1769 by tslayer
- fixed: Fallback skin is now Project Mayhem and not MediaCenter
147
    CLog::Log(LOGINFO, "create genre index");
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
148
    m_pDS->exec("CREATE INDEX idxGenre ON genre(strGenre)");
1769 by tslayer
- fixed: Fallback skin is now Project Mayhem and not MediaCenter
149
    CLog::Log(LOGINFO, "create artist index");
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
150
    m_pDS->exec("CREATE INDEX idxArtist ON artist(strArtist)");
1769 by tslayer
- fixed: Fallback skin is now Project Mayhem and not MediaCenter
151
    CLog::Log(LOGINFO, "create path index");
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
152
    m_pDS->exec("CREATE INDEX idxPath ON path(strPath)");
2569 by charlydoes
fixed: [ 1113362 ] Selecting "All songs" in Artist view takes awfully long time
153
    CLog::Log(LOGINFO, "create song index");
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
154
    m_pDS->exec("CREATE INDEX idxSong ON song(strTitle)");
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
155
    CLog::Log(LOGINFO, "create song index1");
156
    m_pDS->exec("CREATE INDEX idxSong1 ON song(iTimesPlayed)");
157
    CLog::Log(LOGINFO, "create song index2");
158
    m_pDS->exec("CREATE INDEX idxSong2 ON song(lastplayed)");
2752 by bobbin007
fixed: my music, no thumbs for songs in database views when coverart is loaded from tags
159
    CLog::Log(LOGINFO, "create thumb index");
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
160
    m_pDS->exec("CREATE INDEX idxThumb ON thumb(strThumb)");
161
    //m_pDS->exec("CREATE INDEX idxSong ON song(dwFileNameCRC)");
9930 by vulkanr
merge with trunk. revision 12580.
162
    CLog::Log(LOGINFO, "create artistinfo index");
163
    m_pDS->exec("CREATE INDEX idxArtistInfo on artistinfo(idArtist)");
164
    CLog::Log(LOGINFO, "create albuminfo index");
165
    m_pDS->exec("CREATE INDEX idxAlbumInfo on albuminfo(idAlbum)");
2303 by bobbin007
added: albuminfo, songs from allmusic.com are saved to database
166
13479 by gyunaev
Merging with karaoke branch.
167
    CLog::Log(LOGINFO, "create karaokedata index");
168
    m_pDS->exec("CREATE INDEX idxKaraNumber on karaokedata(iKaraNumber)");
169
    m_pDS->exec("CREATE INDEX idxKarSong on karaokedata(idSong)");
170
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
171
    // Trigger
2303 by bobbin007
added: albuminfo, songs from allmusic.com are saved to database
172
    CLog::Log(LOGINFO, "create albuminfo trigger");
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
173
    m_pDS->exec("CREATE TRIGGER tgrAlbumInfo AFTER delete ON albuminfo FOR EACH ROW BEGIN delete from albuminfosong where albuminfosong.idAlbumInfo=old.idAlbumInfo; END");
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
174
175
    // views
176
    CLog::Log(LOGINFO, "create song view");
13480 by charlydoes
fixed: music database songview view.
177
    m_pDS->exec("create view songview as select song.idSong as idSong, song.strExtraArtists as strExtraArtists, song.strExtraGenres as strExtraGenres, strTitle, iTrack, iDuration, song.iYear as iYear, dwFileNameCRC, strFileName, strMusicBrainzTrackID, strMusicBrainzArtistID, strMusicBrainzAlbumID, strMusicBrainzAlbumArtistID, strMusicBrainzTRMID, iTimesPlayed, iStartOffset, iEndOffset, lastplayed, rating, comment, song.idAlbum as idAlbum, strAlbum, strPath, song.idArtist as idArtist, strArtist, song.idGenre as idGenre, strGenre, strThumb, iKaraNumber, iKaraDelay, strKaraEncoding from song join album on song.idAlbum=album.idAlbum join path on song.idPath=path.idPath join  artist on song.idArtist=artist.idArtist join genre on song.idGenre=genre.idGenre join thumb on song.idThumb=thumb.idThumb left outer join karaokedata on song.idSong=karaokedata.idSong");
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
178
    CLog::Log(LOGINFO, "create album view");
10245 by vulkanr
merge with trunk. rev. 13243.
179
    m_pDS->exec("create view albumview as select album.idAlbum as idAlbum, strAlbum, strExtraArtists, "
180
                "album.idArtist as idArtist, album.strExtraGenres as strExtraGenres, album.idGenre as idGenre, "
181
                "strArtist, strGenre, album.iYear as iYear, strThumb, idAlbumInfo, strMoods, strStyles, strThemes, "
182
                "strReview, strLabel, strType, strImage, iRating from album "
183
                "left outer join artist on album.idArtist=artist.idArtist "
184
                "left outer join genre on album.idGenre=genre.idGenre "
185
                "left outer join thumb on album.idThumb=thumb.idThumb "
20929 by jmarshallnz
changed: Indexed columns in the database should be varchar rather than text so that the db layout is more portable. Thanks to firnsy (part of #8169)
186
                "left outer join albuminfo on album.idAlbum=albuminfo.idAlbum");
13479 by gyunaev
Merging with karaoke branch.
187
188
    // Add 'Karaoke' genre
189
    AddGenre( "Karaoke" );
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
190
  }
191
  catch (...)
192
  {
14931 by vdrfan
changed: use transactions to speed-up initial database creation + cosmetics
193
    CLog::Log(LOGERROR, "%s unable to create tables:%i", __FUNCTION__, (int)GetLastError());
194
    RollbackTransaction();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
195
    return false;
196
  }
14931 by vdrfan
changed: use transactions to speed-up initial database creation + cosmetics
197
  CommitTransaction();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
198
  return true;
105 by yamp
new music database
199
}
200
3740 by bobbin007
added: base class for database classes
201
void CMusicDatabase::AddSong(const CSong& song, bool bCheck)
105 by yamp
new music database
202
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
203
  CStdString strSQL;
204
  try
205
  {
4913 by bobbin007
changed: files added to the musicdatabase with no artist or album set will be added to an artist/album "Unknown"
206
    // We need at least the title
207
    if (song.strTitle.IsEmpty())
208
      return;
209
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
210
    CStdString strPath, strFileName;
3740 by bobbin007
added: base class for database classes
211
    CUtil::Split(song.strFileName, strPath, strFileName);
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
212
    ASSERT(CUtil::HasSlashAtEnd(strPath));
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
213
214
    if (NULL == m_pDB.get()) return ;
215
    if (NULL == m_pDS.get()) return ;
216
217
    // split our (possibly) multiple artist string into individual artists
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
218
    CStdStringArray vecArtists; CStdString extraArtists;
219
    SplitString(song.strArtist, vecArtists, extraArtists);
220
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
221
    // do the same with our albumartist
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
222
    CStdStringArray vecAlbumArtists; CStdString extraAlbumArtists;
223
    SplitString(song.strAlbumArtist, vecAlbumArtists, extraAlbumArtists);
224
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
225
    // and the same for our genres
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
226
    CStdStringArray vecGenres; CStdString extraGenres;
227
    SplitString(song.strGenre, vecGenres, extraGenres);
228
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
229
    // add the primary artist/genre
7739 by jmarshallnz
fixed: Opening anims would start too early (before allocation of textures)
230
    // SplitString returns >= 1 so no worries referencing the first item here
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
231
    int idArtist = AddArtist(vecArtists[0]);
232
    int idGenre = AddGenre(vecGenres[0]);
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
233
    // and also the primary album artist (if applicable)
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
234
    int idAlbumArtist = -1;
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
235
    if (!vecAlbumArtists[0].IsEmpty())
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
236
      idAlbumArtist = AddArtist(vecAlbumArtists[0]);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
237
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
238
    int idPath = AddPath(strPath);
239
    int idThumb = AddThumb(song.strThumb);
240
    int idAlbum;
241
    if (idAlbumArtist > -1)  // have an album artist
242
      idAlbum = AddAlbum(song.strAlbum, idAlbumArtist, extraAlbumArtists, song.strAlbumArtist, idThumb, idGenre, extraGenres, song.iYear);
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
243
    else
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
244
      idAlbum = AddAlbum(song.strAlbum, idArtist, extraArtists, song.strArtist, idThumb, idGenre, extraGenres, song.iYear);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
245
4883 by jmarshallnz
- 02-03-2006 fixed: Musicdatabase paths weren't compared case-insensitive (albums + songs could double up if scanned from 2 different cased bookmarks)
246
    DWORD crc = ComputeCRC(song.strFileName);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
247
248
    bool bInsert = true;
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
249
    int idSong = -1;
15779 by mcgeagh
Fixed issue with missing define for Karaoke when building without it... again
250
    bool bHasKaraoke = false;
251
#ifdef HAS_KARAOKE
252
    bHasKaraoke = CKaraokeLyricsFactory::HasLyrics( song.strFileName );
253
#endif
13479 by gyunaev
Merging with karaoke branch.
254
255
    // If this is karaoke song, change the genre to 'Karaoke' (and add it if it's not there)
256
    if ( bHasKaraoke && g_advancedSettings.m_karaokeChangeGenreForKaraokeSongs )
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
257
      idGenre = AddGenre( "Karaoke" );
13479 by gyunaev
Merging with karaoke branch.
258
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
259
    if (bCheck)
260
    {
3740 by bobbin007
added: base class for database classes
261
      strSQL=FormatSQL("select * from song where idAlbum=%i and dwFileNameCRC='%ul' and strTitle='%s'",
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
262
                    idAlbum, crc, song.strTitle.c_str());
19798 by spiff_
cosmetics
263
15005 by arnova
changed: Use db transactions to improve performance
264
      if (!m_pDS->query(strSQL.c_str()))
265
        return;
19798 by spiff_
cosmetics
266
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
267
      if (m_pDS->num_rows() != 0)
268
      {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
269
        idSong = m_pDS->fv("idSong").get_asInt();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
270
        bInsert = false;
271
      }
2307 by charlydoes
fixed: some memory leaks in the database code.
272
      m_pDS->close();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
273
    }
274
    if (bInsert)
275
    {
2897 by bobbin007
added: support for MusicBrainz id's from ogg and mp3 tags
276
      CStdString strSQL1;
277
8066 by yuvalt
Merged with main trunk up to revision 8787
278
      strSQL=FormatSQL("insert into song (idSong,idAlbum,idPath,idArtist,strExtraArtists,idGenre,strExtraGenres,strTitle,iTrack,iDuration,iYear,dwFileNameCRC,strFileName,strMusicBrainzTrackID,strMusicBrainzArtistID,strMusicBrainzAlbumID,strMusicBrainzAlbumArtistID,strMusicBrainzTRMID,iTimesPlayed,iStartOffset,iEndOffset,idThumb,lastplayed,rating,comment) values (NULL,%i,%i,%i,'%s',%i,'%s','%s',%i,%i,%i,'%ul','%s','%s','%s','%s','%s','%s'",
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
279
                    idAlbum, idPath, idArtist, extraArtists.c_str(), idGenre, extraGenres.c_str(),
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
280
                    song.strTitle.c_str(),
281
                    song.iTrack, song.iDuration, song.iYear,
4889 by jmarshallnz
- 03-03-2006 fixed: Better music database lower case comparisons (paths are stored as is, but compared lower case).
282
                    crc, strFileName.c_str(),
2897 by bobbin007
added: support for MusicBrainz id's from ogg and mp3 tags
283
                    song.strMusicBrainzTrackID.c_str(),
284
                    song.strMusicBrainzArtistID.c_str(),
285
                    song.strMusicBrainzAlbumID.c_str(),
286
                    song.strMusicBrainzAlbumArtistID.c_str(),
287
                    song.strMusicBrainzTRMID.c_str());
288
7984 by jmarshallnz
fixed: Update Library or Rescanning would remove last played and top100 information for songs.
289
      if (song.lastPlayed.GetLength())
8066 by yuvalt
Merged with main trunk up to revision 8787
290
        strSQL1=FormatSQL(",%i,%i,%i,%i,'%s','%c','%s')",
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
291
                      song.iTimesPlayed, song.iStartOffset, song.iEndOffset, idThumb, song.lastPlayed.c_str(), song.rating, song.strComment.c_str());
7984 by jmarshallnz
fixed: Update Library or Rescanning would remove last played and top100 information for songs.
292
      else
8066 by yuvalt
Merged with main trunk up to revision 8787
293
        strSQL1=FormatSQL(",%i,%i,%i,%i,NULL,'%c','%s')",
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
294
                      song.iTimesPlayed, song.iStartOffset, song.iEndOffset, idThumb, song.rating, song.strComment.c_str());
2897 by bobbin007
added: support for MusicBrainz id's from ogg and mp3 tags
295
      strSQL+=strSQL1;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
296
297
      m_pDS->exec(strSQL.c_str());
19283 by jmarshallnz
added: Improved abstraction of the database API, thanks to loopingz and firnsy
298
      idSong = (int)m_pDS->lastinsertid();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
299
    }
300
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
301
    // add extra artists and genres
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
302
    AddExtraSongArtists(vecArtists, idSong, bCheck);
303
    if (idAlbumArtist > -1)
304
      AddExtraAlbumArtists(vecAlbumArtists, idAlbum);
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
305
    else
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
306
      AddExtraAlbumArtists(vecArtists, idAlbum);
307
    AddExtraGenres(vecGenres, idSong, idAlbum, bCheck);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
308
13479 by gyunaev
Merging with karaoke branch.
309
    // Add karaoke information (if any)
310
    if ( bHasKaraoke )
311
    {
312
      // song argument is const :(
313
      CSong mysong = song;
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
314
      mysong.idSong = idSong;
13479 by gyunaev
Merging with karaoke branch.
315
      AddKaraokeData( mysong );
316
    }
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
317
  }
318
  catch (...)
319
  {
1769 by tslayer
- fixed: Fallback skin is now Project Mayhem and not MediaCenter
320
    CLog::Log(LOGERROR, "musicdatabase:unable to addsong (%s)", strSQL.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
321
  }
105 by yamp
new music database
322
}
323
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
324
int CMusicDatabase::AddAlbum(const CStdString& strAlbum1, int idArtist, const CStdString &extraArtists, const CStdString &strArtist, int idThumb, int idGenre, const CStdString &extraGenres, int year)
105 by yamp
new music database
325
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
326
  CStdString strSQL;
327
  try
328
  {
4913 by bobbin007
changed: files added to the musicdatabase with no artist or album set will be added to an artist/album "Unknown"
329
    CStdString strAlbum=strAlbum1;
330
    strAlbum.TrimLeft(" ");
331
    strAlbum.TrimRight(" ");
332
333
    if (strAlbum.IsEmpty())
334
    {
17140 by jmarshallnz
added: Support for singles in the music library. Anything with the ALBUM tag empty is regarded as a single. Singles are grouped into their own root node, and the singles for a particular artist are grouped together into a "Singles" virtual album. Ticket #7061.
335
      // album tag is empty, so we treat this as a single, or a collection of singles,
336
      // so we don't specify a thumb
337
      idThumb = AddThumb("");
4913 by bobbin007
changed: files added to the musicdatabase with no artist or album set will be added to an artist/album "Unknown"
338
    }
339
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
340
    if (NULL == m_pDB.get()) return -1;
341
    if (NULL == m_pDS.get()) return -1;
342
343
    map <CStdString, CAlbumCache>::const_iterator it;
344
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
345
    it = m_albumCache.find(strAlbum + strArtist);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
346
    if (it != m_albumCache.end())
347
      return it->second.idAlbum;
348
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
349
    strSQL=FormatSQL("select * from album where idArtist=%i and strAlbum like '%s'", idArtist, strAlbum.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
350
    m_pDS->query(strSQL.c_str());
351
352
    if (m_pDS->num_rows() == 0)
353
    {
354
      m_pDS->close();
355
      // doesnt exists, add it
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
356
      strSQL=FormatSQL("insert into album (idAlbum, strAlbum, idArtist, strExtraArtists, idGenre, strExtraGenres, iYear, idThumb) values( NULL, '%s', %i, '%s', %i, '%s', %i, %i)", strAlbum.c_str(), idArtist, extraArtists.c_str(), idGenre, extraGenres.c_str(), year, idThumb);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
357
      m_pDS->exec(strSQL.c_str());
358
359
      CAlbumCache album;
19283 by jmarshallnz
added: Improved abstraction of the database API, thanks to loopingz and firnsy
360
      album.idAlbum = (int)m_pDS->lastinsertid();
3740 by bobbin007
added: base class for database classes
361
      album.strAlbum = strAlbum;
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
362
      album.idArtist = idArtist;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
363
      album.strArtist = strArtist;
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
364
      m_albumCache.insert(pair<CStdString, CAlbumCache>(album.strAlbum + album.strArtist, album));
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
365
      return album.idAlbum;
366
    }
367
    else
368
    {
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
369
      // exists in our database and not scanned during this scan, so we should update it as the details
370
      // may have changed (there's a reason we're rescanning, afterall!)
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
371
      CAlbumCache album;
17467 by jmarshallnz
changed: long -> int in database functions.
372
      album.idAlbum = m_pDS->fv("idAlbum").get_asInt();
3740 by bobbin007
added: base class for database classes
373
      album.strAlbum = strAlbum;
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
374
      album.idArtist = idArtist;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
375
      album.strArtist = strArtist;
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
376
      m_albumCache.insert(pair<CStdString, CAlbumCache>(album.strAlbum + album.strArtist, album));
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
377
      m_pDS->close();
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
378
      strSQL=FormatSQL("update album set strExtraArtists='%s', idGenre=%i, strExtraGenres='%s', iYear=%i, idThumb=%i where idAlbum=%i", extraArtists.c_str(), idGenre, extraGenres.c_str(), year, idThumb, album.idAlbum);
379
      m_pDS->exec(strSQL.c_str());
380
      // and clear the exartistalbum and exgenrealbum tables - these are updated in AddSong()
381
      strSQL=FormatSQL("delete from exartistalbum where idAlbum=%i", album.idAlbum);
382
      m_pDS->exec(strSQL.c_str());
383
      strSQL=FormatSQL("delete from exgenrealbum where idAlbum=%i", album.idAlbum);
384
      m_pDS->exec(strSQL.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
385
      return album.idAlbum;
386
    }
387
  }
388
  catch (...)
389
  {
8035 by yuvalt
Initial port to Linux/SDL
390
    CLog::Log(LOGERROR, "%s failed with query (%s)", __FUNCTION__, strSQL.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
391
  }
584 by bobbin007
fixed: ogg tag reading was broken
392
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
393
  return -1;
584 by bobbin007
fixed: ogg tag reading was broken
394
}
395
17467 by jmarshallnz
changed: long -> int in database functions.
396
int CMusicDatabase::AddGenre(const CStdString& strGenre1)
105 by yamp
new music database
397
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
398
  CStdString strSQL;
399
  try
400
  {
401
    CStdString strGenre = strGenre1;
402
    strGenre.TrimLeft(" ");
403
    strGenre.TrimRight(" ");
404
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
405
    if (strGenre.IsEmpty())
406
      strGenre=g_localizeStrings.Get(13205); // Unknown
407
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
408
    if (NULL == m_pDB.get()) return -1;
409
    if (NULL == m_pDS.get()) return -1;
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
410
    map <CStdString, int>::const_iterator it;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
411
412
    it = m_genreCache.find(strGenre);
413
    if (it != m_genreCache.end())
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
414
      return it->second;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
415
416
3740 by bobbin007
added: base class for database classes
417
    strSQL=FormatSQL("select * from genre where strGenre like '%s'", strGenre.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
418
    m_pDS->query(strSQL.c_str());
419
    if (m_pDS->num_rows() == 0)
420
    {
421
      m_pDS->close();
422
      // doesnt exists, add it
3740 by bobbin007
added: base class for database classes
423
      strSQL=FormatSQL("insert into genre (idGenre, strGenre) values( NULL, '%s' )", strGenre.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
424
      m_pDS->exec(strSQL.c_str());
425
19283 by jmarshallnz
added: Improved abstraction of the database API, thanks to loopingz and firnsy
426
      int idGenre = (int)m_pDS->lastinsertid();
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
427
      m_genreCache.insert(pair<CStdString, int>(strGenre1, idGenre));
428
      return idGenre;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
429
    }
430
    else
431
    {
17467 by jmarshallnz
changed: long -> int in database functions.
432
      int idGenre = m_pDS->fv("idGenre").get_asInt();
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
433
      m_genreCache.insert(pair<CStdString, int>(strGenre1, idGenre));
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
434
      m_pDS->close();
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
435
      return idGenre;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
436
    }
437
  }
438
  catch (...)
439
  {
1769 by tslayer
- fixed: Fallback skin is now Project Mayhem and not MediaCenter
440
    CLog::Log(LOGERROR, "musicdatabase:unable to addgenre (%s)", strSQL.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
441
  }
118 by yamp
no message
442
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
443
  return -1;
105 by yamp
new music database
444
}
445
17467 by jmarshallnz
changed: long -> int in database functions.
446
int CMusicDatabase::AddArtist(const CStdString& strArtist1)
105 by yamp
new music database
447
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
448
  CStdString strSQL;
449
  try
450
  {
451
    CStdString strArtist = strArtist1;
452
    strArtist.TrimLeft(" ");
453
    strArtist.TrimRight(" ");
454
4913 by bobbin007
changed: files added to the musicdatabase with no artist or album set will be added to an artist/album "Unknown"
455
    if (strArtist.IsEmpty())
456
      strArtist=g_localizeStrings.Get(13205); // Unknown
457
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
458
    if (NULL == m_pDB.get()) return -1;
459
    if (NULL == m_pDS.get()) return -1;
460
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
461
    map <CStdString, int>::const_iterator it;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
462
463
    it = m_artistCache.find(strArtist);
464
    if (it != m_artistCache.end())
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
465
      return it->second;//.idArtist;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
466
3740 by bobbin007
added: base class for database classes
467
    strSQL=FormatSQL("select * from artist where strArtist like '%s'", strArtist.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
468
    m_pDS->query(strSQL.c_str());
469
470
    if (m_pDS->num_rows() == 0)
471
    {
472
      m_pDS->close();
473
      // doesnt exists, add it
3740 by bobbin007
added: base class for database classes
474
      strSQL=FormatSQL("insert into artist (idArtist, strArtist) values( NULL, '%s' )", strArtist.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
475
      m_pDS->exec(strSQL.c_str());
19283 by jmarshallnz
added: Improved abstraction of the database API, thanks to loopingz and firnsy
476
      int idArtist = (int)m_pDS->lastinsertid();
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
477
      m_artistCache.insert(pair<CStdString, int>(strArtist1, idArtist));
478
      return idArtist;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
479
    }
480
    else
481
    {
17467 by jmarshallnz
changed: long -> int in database functions.
482
      int idArtist = (int)m_pDS->fv("idArtist").get_asInt();
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
483
      m_artistCache.insert(pair<CStdString, int>(strArtist1, idArtist));
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
484
      m_pDS->close();
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
485
      return idArtist;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
486
    }
487
  }
488
  catch (...)
489
  {
1769 by tslayer
- fixed: Fallback skin is now Project Mayhem and not MediaCenter
490
    CLog::Log(LOGERROR, "musicdatabase:unable to addartist (%s)", strSQL.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
491
  }
105 by yamp
new music database
492
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
493
  return -1;
118 by yamp
no message
494
}
495
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
496
void CMusicDatabase::AddExtraSongArtists(const CStdStringArray &vecArtists, int idSong, bool bCheck)
1798 by jmarshallnz
- 11-08-2004 fixed: small bug in resizing algorithm of videos meant zoom (using START) sometimes
497
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
498
  try
499
  {
500
    // add each of the artists in the vector of artists
501
    for (int i = 1; i < (int)vecArtists.size(); i++)
502
    {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
503
      int idArtist = AddArtist(vecArtists[i]);
504
      if (idArtist >= 0)
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
505
      { // added successfully, we must now add entries to the exartistsong table
506
        CStdString strSQL;
507
        // first link the artist with the song
508
        bool bInsert = true;
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
509
        if (bCheck)
510
        {
511
          strSQL=FormatSQL("select * from exartistsong where idSong=%i and idArtist=%i",
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
512
                        idSong, idArtist);
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
513
          if (!m_pDS->query(strSQL.c_str())) return ;
514
          if (m_pDS->num_rows() != 0)
515
            bInsert = false; // already exists
516
          m_pDS->close();
517
        }
518
        if (bInsert)
519
        {
520
          strSQL=FormatSQL("insert into exartistsong (idSong,iPosition,idArtist) values(%i,%i,%i)",
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
521
                        idSong, i, idArtist);
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
522
523
          m_pDS->exec(strSQL.c_str());
524
        }
525
      }
526
    }
527
  }
528
  catch (...)
529
  {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
530
    CLog::Log(LOGERROR, "%s(%i) failed", __FUNCTION__, idSong);
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
531
  }
532
}
533
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
534
void CMusicDatabase::AddExtraAlbumArtists(const CStdStringArray &vecArtists, int idAlbum)
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
535
{
536
  try
537
  {
538
    // add each of the artists in the vector of artists
539
    for (int i = 1; i < (int)vecArtists.size(); i++)
540
    {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
541
      int idArtist = AddArtist(vecArtists[i]);
542
      if (idArtist >= 0)
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
543
      { // added successfully, we must now add entries to the exartistalbum table
544
        CStdString strSQL;
545
        bool bInsert = true;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
546
        // always check artists (as this routine is called whenever a song is added)
3740 by bobbin007
added: base class for database classes
547
        strSQL=FormatSQL("select * from exartistalbum where idAlbum=%i and idArtist=%i",
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
548
                      idAlbum, idArtist);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
549
        if (!m_pDS->query(strSQL.c_str())) return ;
550
        if (m_pDS->num_rows() != 0)
551
          bInsert = false; // already exists
2307 by charlydoes
fixed: some memory leaks in the database code.
552
        m_pDS->close();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
553
        if (bInsert)
554
        {
3740 by bobbin007
added: base class for database classes
555
          strSQL=FormatSQL("insert into exartistalbum (idAlbum,iPosition,idArtist) values(%i,%i,%i)",
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
556
                        idAlbum, i, idArtist);
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
557
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
558
          m_pDS->exec(strSQL.c_str());
559
        }
560
      }
561
    }
562
  }
563
  catch (...)
2449 by bobbin007
changed: my music, background scanner can be canceled during its first operations
564
  {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
565
    CLog::Log(LOGERROR, "%s(%i) failed", __FUNCTION__, idAlbum);
2449 by bobbin007
changed: my music, background scanner can be canceled during its first operations
566
  }
1798 by jmarshallnz
- 11-08-2004 fixed: small bug in resizing algorithm of videos meant zoom (using START) sometimes
567
}
568
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
569
void CMusicDatabase::AddExtraGenres(const CStdStringArray &vecGenres, int idSong, int idAlbum, bool bCheck)
1798 by jmarshallnz
- 11-08-2004 fixed: small bug in resizing algorithm of videos meant zoom (using START) sometimes
570
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
571
  try
572
  {
573
    // add each of the genres in the vector
574
    for (int i = 1; i < (int)vecGenres.size(); i++)
575
    {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
576
      int idGenre = AddGenre(vecGenres[i]);
577
      if (idGenre >= 0)
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
578
      { // added successfully!
579
        CStdString strSQL;
580
        // first link the genre with the song
581
        bool bInsert = true;
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
582
        if (idSong)
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
583
        {
584
          if (bCheck)
585
          {
3740 by bobbin007
added: base class for database classes
586
            strSQL=FormatSQL("select * from exgenresong where idSong=%i and idGenre=%i",
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
587
                          idSong, idGenre);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
588
            if (!m_pDS->query(strSQL.c_str())) return ;
589
            if (m_pDS->num_rows() != 0)
590
              bInsert = false; // already exists
591
            m_pDS->close();
592
          }
593
          if (bInsert)
594
          {
3740 by bobbin007
added: base class for database classes
595
            strSQL=FormatSQL("insert into exgenresong (idSong,iPosition,idGenre) values(%i,%i,%i)",
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
596
                          idSong, i, idGenre);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
597
598
            m_pDS->exec(strSQL.c_str());
599
          }
600
        }
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
601
        // now link the genre with the album - we always check these as there's usually
602
        // more than one song per album with the same extra genres
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
603
        if (idAlbum)
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
604
        {
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
605
          strSQL=FormatSQL("select * from exgenrealbum where idAlbum=%i and idGenre=%i",
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
606
                        idAlbum, idGenre);
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
607
          if (!m_pDS->query(strSQL.c_str())) return ;
608
          if (m_pDS->num_rows() == 0)
609
          { // insert
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
610
            m_pDS->close();
3740 by bobbin007
added: base class for database classes
611
            strSQL=FormatSQL("insert into exgenrealbum (idAlbum,iPosition,idGenre) values(%i,%i,%i)",
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
612
                          idAlbum, i, idGenre);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
613
614
            m_pDS->exec(strSQL.c_str());
615
          }
616
        }
617
      }
618
    }
619
  }
620
  catch (...)
621
  {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
622
    CLog::Log(LOGERROR, "%s(%i,%i) failed", __FUNCTION__, idSong, idAlbum);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
623
  }
1798 by jmarshallnz
- 11-08-2004 fixed: small bug in resizing algorithm of videos meant zoom (using START) sometimes
624
}
625
17467 by jmarshallnz
changed: long -> int in database functions.
626
int CMusicDatabase::AddPath(const CStdString& strPath)
118 by yamp
no message
627
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
628
  CStdString strSQL;
629
  try
630
  {
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
631
    ASSERT(CUtil::HasSlashAtEnd(strPath));
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
632
633
    if (NULL == m_pDB.get()) return -1;
634
    if (NULL == m_pDS.get()) return -1;
635
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
636
    map <CStdString, int>::const_iterator it;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
637
4884 by jmarshallnz
- 02-03-2006 fixed: Musicdatabase paths weren't compared case-insensitive (albums + songs could double up if scanned from 2 different cased bookmarks)
638
    it = m_pathCache.find(strPath);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
639
    if (it != m_pathCache.end())
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
640
      return it->second;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
641
4882 by jmarshallnz
- 02-03-2006 fixed: Musicdatabase paths weren't compared case-insensitive (albums + songs could double up if scanned from 2 different cased bookmarks)
642
    strSQL=FormatSQL( "select * from path where strPath like '%s'", strPath.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
643
    m_pDS->query(strSQL.c_str());
644
    if (m_pDS->num_rows() == 0)
645
    {
646
      m_pDS->close();
647
      // doesnt exists, add it
3740 by bobbin007
added: base class for database classes
648
      strSQL=FormatSQL("insert into path (idPath, strPath) values( NULL, '%s' )", strPath.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
649
      m_pDS->exec(strSQL.c_str());
650
19283 by jmarshallnz
added: Improved abstraction of the database API, thanks to loopingz and firnsy
651
      int idPath = (int)m_pDS->lastinsertid();
4884 by jmarshallnz
- 02-03-2006 fixed: Musicdatabase paths weren't compared case-insensitive (albums + songs could double up if scanned from 2 different cased bookmarks)
652
      m_pathCache.insert(pair<CStdString, int>(strPath, idPath));
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
653
      return idPath;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
654
    }
655
    else
656
    {
17467 by jmarshallnz
changed: long -> int in database functions.
657
      int idPath = m_pDS->fv("idPath").get_asInt();
4884 by jmarshallnz
- 02-03-2006 fixed: Musicdatabase paths weren't compared case-insensitive (albums + songs could double up if scanned from 2 different cased bookmarks)
658
      m_pathCache.insert(pair<CStdString, int>(strPath, idPath));
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
659
      m_pDS->close();
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
660
      return idPath;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
661
    }
662
  }
663
  catch (...)
664
  {
665
    CLog::Log(LOGERROR, "musicdatabase:unable to addpath (%s)", strSQL.c_str());
666
  }
667
668
  return -1;
118 by yamp
no message
669
}
670
4458 by bobbin007
*** empty log message ***
671
CSong CMusicDatabase::GetSongFromDataset(bool bWithMusicDbPath/*=false*/)
1798 by jmarshallnz
- 11-08-2004 fixed: small bug in resizing algorithm of videos meant zoom (using START) sometimes
672
{
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
673
  CSong song;
17467 by jmarshallnz
changed: long -> int in database functions.
674
  song.idSong = m_pDS->fv(song_idSong).get_asInt();
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
675
  // get the full artist string
4513 by bobbin007
changed: speed improvement getting songs from the musicdatabase
676
  song.strArtist = m_pDS->fv(song_strArtist).get_asString();
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
677
  song.strArtist += m_pDS->fv(song_strExtraArtists).get_asString();
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
678
  // and the full genre string
4513 by bobbin007
changed: speed improvement getting songs from the musicdatabase
679
  song.strGenre = m_pDS->fv(song_strGenre).get_asString();
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
680
  song.strGenre += m_pDS->fv(song_strExtraGenres).get_asString();
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
681
  // and the rest...
4513 by bobbin007
changed: speed improvement getting songs from the musicdatabase
682
  song.strAlbum = m_pDS->fv(song_strAlbum).get_asString();
17467 by jmarshallnz
changed: long -> int in database functions.
683
  song.iTrack = m_pDS->fv(song_iTrack).get_asInt() ;
684
  song.iDuration = m_pDS->fv(song_iDuration).get_asInt() ;
685
  song.iYear = m_pDS->fv(song_iYear).get_asInt() ;
4513 by bobbin007
changed: speed improvement getting songs from the musicdatabase
686
  song.strTitle = m_pDS->fv(song_strTitle).get_asString();
17467 by jmarshallnz
changed: long -> int in database functions.
687
  song.iTimesPlayed = m_pDS->fv(song_iTimesPlayed).get_asInt();
7984 by jmarshallnz
fixed: Update Library or Rescanning would remove last played and top100 information for songs.
688
  song.lastPlayed = m_pDS->fv(song_lastplayed).get_asString();
17467 by jmarshallnz
changed: long -> int in database functions.
689
  song.iStartOffset = m_pDS->fv(song_iStartOffset).get_asInt();
690
  song.iEndOffset = m_pDS->fv(song_iEndOffset).get_asInt();
4513 by bobbin007
changed: speed improvement getting songs from the musicdatabase
691
  song.strMusicBrainzTrackID = m_pDS->fv(song_strMusicBrainzTrackID).get_asString();
692
  song.strMusicBrainzArtistID = m_pDS->fv(song_strMusicBrainzArtistID).get_asString();
693
  song.strMusicBrainzAlbumID = m_pDS->fv(song_strMusicBrainzAlbumID).get_asString();
694
  song.strMusicBrainzAlbumArtistID = m_pDS->fv(song_strMusicBrainzAlbumArtistID).get_asString();
695
  song.strMusicBrainzTRMID = m_pDS->fv(song_strMusicBrainzTRMID).get_asString();
8066 by yuvalt
Merged with main trunk up to revision 8787
696
  song.rating = m_pDS->fv(song_rating).get_asChar();
697
  song.strComment = m_pDS->fv(song_comment).get_asString();
4513 by bobbin007
changed: speed improvement getting songs from the musicdatabase
698
  song.strThumb = m_pDS->fv(song_strThumb).get_asString();
17467 by jmarshallnz
changed: long -> int in database functions.
699
  song.iKaraokeNumber = m_pDS->fv(song_iKarNumber).get_asInt();
13479 by gyunaev
Merging with karaoke branch.
700
  song.strKaraokeLyrEncoding = m_pDS->fv(song_strKarEncoding).get_asString();
17467 by jmarshallnz
changed: long -> int in database functions.
701
  song.iKaraokeDelay = m_pDS->fv(song_iKarDelay).get_asInt();
13479 by gyunaev
Merging with karaoke branch.
702
2734 by jmarshallnz
- 03-03-2005 added: Cached thumb location is now stored in the database. Music database SHOULD auto-update when you first access My Music.
703
  if (song.strThumb == "NONE")
704
    song.strThumb.Empty();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
705
  // Get filename with full path
4458 by bobbin007
*** empty log message ***
706
  if (!bWithMusicDbPath)
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
707
    CUtil::AddFileToFolder(m_pDS->fv(song_strPath).get_asString(), m_pDS->fv(song_strFileName).get_asString(), song.strFileName);
4458 by bobbin007
*** empty log message ***
708
  else
709
  {
4513 by bobbin007
changed: speed improvement getting songs from the musicdatabase
710
    CStdString strFileName=m_pDS->fv(song_strFileName).get_asString();
5179 by elupus
changed: handling of file extension + code cleanup
711
    CStdString strExt=CUtil::GetExtension(strFileName);
17467 by jmarshallnz
changed: long -> int in database functions.
712
    song.strFileName.Format("musicdb://3/%ld/%ld%s", m_pDS->fv(song_idAlbum).get_asInt(), m_pDS->fv(song_idSong).get_asInt(), strExt.c_str());
4458 by bobbin007
*** empty log message ***
713
  }
1975 by yuvalt
fixed: [ 1026292 ] tags with blank album name would cause the display of wrong thumbnails
714
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
715
  return song;
1798 by jmarshallnz
- 11-08-2004 fixed: small bug in resizing algorithm of videos meant zoom (using START) sometimes
716
}
717
4472 by bobbin007
changed: music nav, minor speed improvement of loading all songs from database
718
void CMusicDatabase::GetFileItemFromDataset(CFileItem* item, const CStdString& strMusicDBbasePath)
719
{
720
  // get the full artist string
4513 by bobbin007
changed: speed improvement getting songs from the musicdatabase
721
  CStdString strArtist=m_pDS->fv(song_strArtist).get_asString();
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
722
  strArtist += m_pDS->fv(song_strExtraArtists).get_asString();
7609 by spiff_
added: video info tag to cfileitem
723
  item->GetMusicInfoTag()->SetArtist(strArtist);
4472 by bobbin007
changed: music nav, minor speed improvement of loading all songs from database
724
  // and the full genre string
4513 by bobbin007
changed: speed improvement getting songs from the musicdatabase
725
  CStdString strGenre = m_pDS->fv(song_strGenre).get_asString();
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
726
  strGenre += m_pDS->fv(song_strExtraGenres).get_asString();
7609 by spiff_
added: video info tag to cfileitem
727
  item->GetMusicInfoTag()->SetGenre(strGenre);
4472 by bobbin007
changed: music nav, minor speed improvement of loading all songs from database
728
  // and the rest...
7609 by spiff_
added: video info tag to cfileitem
729
  item->GetMusicInfoTag()->SetAlbum(m_pDS->fv(song_strAlbum).get_asString());
17467 by jmarshallnz
changed: long -> int in database functions.
730
  item->GetMusicInfoTag()->SetTrackAndDiskNumber(m_pDS->fv(song_iTrack).get_asInt());
731
  item->GetMusicInfoTag()->SetDuration(m_pDS->fv(song_iDuration).get_asInt());
732
  item->GetMusicInfoTag()->SetDatabaseId(m_pDS->fv(song_idSong).get_asInt());
4472 by bobbin007
changed: music nav, minor speed improvement of loading all songs from database
733
  SYSTEMTIME stTime;
17467 by jmarshallnz
changed: long -> int in database functions.
734
  stTime.wYear = (WORD)m_pDS->fv(song_iYear).get_asInt();
7609 by spiff_
added: video info tag to cfileitem
735
  item->GetMusicInfoTag()->SetReleaseDate(stTime);
736
  item->GetMusicInfoTag()->SetTitle(m_pDS->fv(song_strTitle).get_asString());
4513 by bobbin007
changed: speed improvement getting songs from the musicdatabase
737
  item->SetLabel(m_pDS->fv(song_strTitle).get_asString());
17467 by jmarshallnz
changed: long -> int in database functions.
738
  //song.iTimesPlayed = m_pDS->fv(song_iTimesPlayed).get_asInt();
739
  item->m_lStartOffset = m_pDS->fv(song_iStartOffset).get_asInt();
740
  item->m_lEndOffset = m_pDS->fv(song_iEndOffset).get_asInt();
7609 by spiff_
added: video info tag to cfileitem
741
  item->GetMusicInfoTag()->SetMusicBrainzTrackID(m_pDS->fv(song_strMusicBrainzTrackID).get_asString());
742
  item->GetMusicInfoTag()->SetMusicBrainzArtistID(m_pDS->fv(song_strMusicBrainzArtistID).get_asString());
743
  item->GetMusicInfoTag()->SetMusicBrainzAlbumID(m_pDS->fv(song_strMusicBrainzAlbumID).get_asString());
744
  item->GetMusicInfoTag()->SetMusicBrainzAlbumArtistID(m_pDS->fv(song_strMusicBrainzAlbumArtistID).get_asString());
745
  item->GetMusicInfoTag()->SetMusicBrainzTRMID(m_pDS->fv(song_strMusicBrainzTRMID).get_asString());
8066 by yuvalt
Merged with main trunk up to revision 8787
746
  item->GetMusicInfoTag()->SetRating(m_pDS->fv(song_rating).get_asChar());
747
  item->GetMusicInfoTag()->SetComment(m_pDS->fv(song_comment).get_asString());
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
748
  CStdString strRealPath;
749
  CUtil::AddFileToFolder(m_pDS->fv(song_strPath).get_asString(), m_pDS->fv(song_strFileName).get_asString(), strRealPath);
7609 by spiff_
added: video info tag to cfileitem
750
  item->GetMusicInfoTag()->SetURL(strRealPath);
751
  item->GetMusicInfoTag()->SetLoaded(true);
4513 by bobbin007
changed: speed improvement getting songs from the musicdatabase
752
  CStdString strThumb=m_pDS->fv(song_strThumb).get_asString();
4472 by bobbin007
changed: music nav, minor speed improvement of loading all songs from database
753
  if (strThumb != "NONE")
754
    item->SetThumbnailImage(strThumb);
755
  // Get filename with full path
756
  if (strMusicDBbasePath.IsEmpty())
757
  {
4508 by bobbin007
changed: music playlists, playlists with music database items are now saved with the real path of the file instead of the musicdatabase URL
758
    item->m_strPath = strRealPath;
4472 by bobbin007
changed: music nav, minor speed improvement of loading all songs from database
759
  }
760
  else
761
  {
4513 by bobbin007
changed: speed improvement getting songs from the musicdatabase
762
    CStdString strFileName=m_pDS->fv(song_strFileName).get_asString();
5179 by elupus
changed: handling of file extension + code cleanup
763
    CStdString strExt=CUtil::GetExtension(strFileName);
17467 by jmarshallnz
changed: long -> int in database functions.
764
    item->m_strPath.Format("%s%ld%s", strMusicDBbasePath.c_str(), m_pDS->fv(song_idSong).get_asInt(), strExt.c_str());
4472 by bobbin007
changed: music nav, minor speed improvement of loading all songs from database
765
  }
766
}
767
10245 by vulkanr
merge with trunk. rev. 13243.
768
CAlbum CMusicDatabase::GetAlbumFromDataset(dbiplus::Dataset* pDS, bool imageURL /* = false*/)
1798 by jmarshallnz
- 11-08-2004 fixed: small bug in resizing algorithm of videos meant zoom (using START) sometimes
769
{
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
770
  CAlbum album;
17467 by jmarshallnz
changed: long -> int in database functions.
771
  album.idAlbum = pDS->fv(album_idAlbum).get_asInt();
9868 by vulkanr
merge with trunk. rev. 12480.
772
  album.strAlbum = pDS->fv(album_strAlbum).get_asString();
17140 by jmarshallnz
added: Support for singles in the music library. Anything with the ALBUM tag empty is regarded as a single. Singles are grouped into their own root node, and the singles for a particular artist are grouped together into a "Singles" virtual album. Ticket #7061.
773
  if (album.strAlbum.IsEmpty())
17141 by jmarshallnz
fixed: Localize those strings
774
    album.strAlbum = g_localizeStrings.Get(1050);
9868 by vulkanr
merge with trunk. rev. 12480.
775
  album.strArtist = pDS->fv(album_strArtist).get_asString();
776
  album.strArtist += pDS->fv(album_strExtraArtists).get_asString();
777
  album.strGenre = pDS->fv(album_strGenre).get_asString();
778
  album.strGenre += pDS->fv(album_strExtraGenres).get_asString();
17467 by jmarshallnz
changed: long -> int in database functions.
779
  album.iYear = pDS->fv(album_iYear).get_asInt();
10245 by vulkanr
merge with trunk. rev. 13243.
780
  if (imageURL)
781
    album.thumbURL.ParseString(pDS->fv(album_strThumbURL).get_asString());
782
  else
783
  {
784
    CStdString strThumb = pDS->fv(album_strThumb).get_asString();
785
    if (strThumb != "NONE")
786
      album.thumbURL.ParseString(strThumb);
787
  }
17467 by jmarshallnz
changed: long -> int in database functions.
788
  album.iRating = pDS->fv(album_iRating).get_asInt();
789
  album.iYear = pDS->fv(album_iYear).get_asInt();
10245 by vulkanr
merge with trunk. rev. 13243.
790
  album.strReview = pDS->fv(album_strReview).get_asString();
791
  album.strStyles = pDS->fv(album_strStyles).get_asString();
792
  album.strMoods = pDS->fv(album_strMoods).get_asString();
793
  album.strThemes = pDS->fv(album_strThemes).get_asString();
794
  album.strLabel = pDS->fv(album_strLabel).get_asString();
795
  album.strType = pDS->fv(album_strType).get_asString();
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
796
  return album;
1798 by jmarshallnz
- 11-08-2004 fixed: small bug in resizing algorithm of videos meant zoom (using START) sometimes
797
}
798
9930 by vulkanr
merge with trunk. revision 12580.
799
CArtist CMusicDatabase::GetArtistFromDataset(dbiplus::Dataset* pDS, bool needThumb)
9868 by vulkanr
merge with trunk. rev. 12480.
800
{
801
  CArtist artist;
17467 by jmarshallnz
changed: long -> int in database functions.
802
  artist.idArtist = pDS->fv(artist_idArtist).get_asInt();
9868 by vulkanr
merge with trunk. rev. 12480.
803
  artist.strArtist = pDS->fv("artist.strArtist").get_asString();
10104 by vulkanr
merge with trunk. revision: 12940.
804
  artist.strGenre = pDS->fv(artist_strGenres).get_asString();
805
  artist.strBiography = pDS->fv(artist_strBiography).get_asString();
806
  artist.strStyles = pDS->fv(artist_strStyles).get_asString();
807
  artist.strMoods = pDS->fv(artist_strMoods).get_asString();
808
  artist.strBorn = pDS->fv(artist_strBorn).get_asString();
809
  artist.strFormed = pDS->fv(artist_strFormed).get_asString();
810
  artist.strDied = pDS->fv(artist_strDied).get_asString();
811
  artist.strDisbanded = pDS->fv(artist_strDisbanded).get_asString();
812
  artist.strYearsActive = pDS->fv(artist_strYearsActive).get_asString();
813
  artist.strInstruments = pDS->fv(artist_strInstruments).get_asString();
9868 by vulkanr
merge with trunk. rev. 12480.
814
9930 by vulkanr
merge with trunk. revision 12580.
815
  if (needThumb)
14378 by spiff_
added: ticket #4834 - scraped music fanart. uses http://www.htbackdrops.com - go add your favorite artist. thanks to nuzecast for making the backend available
816
  {
817
    artist.fanart.m_xml = pDS->fv(artist_strFanart).get_asString();
818
    artist.fanart.Unpack();
10104 by vulkanr
merge with trunk. revision: 12940.
819
    artist.thumbURL.ParseString(pDS->fv(artist_strImage).get_asString());
14378 by spiff_
added: ticket #4834 - scraped music fanart. uses http://www.htbackdrops.com - go add your favorite artist. thanks to nuzecast for making the backend available
820
  }
9930 by vulkanr
merge with trunk. revision 12580.
821
9868 by vulkanr
merge with trunk. rev. 12480.
822
  return artist;
823
}
824
3740 by bobbin007
added: base class for database classes
825
bool CMusicDatabase::GetSongByFileName(const CStdString& strFileName, CSong& song)
118 by yamp
no message
826
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
827
  try
828
  {
829
    song.Clear();
8839 by vulkanr
merged revisions 10713, 10714 (lastfm unlove/ban) that were left out from previous merge.
830
    CURL url(strFileName);
10519 by spiff_
cosmetics (trailing whitespace)
831
8839 by vulkanr
merged revisions 10713, 10714 (lastfm unlove/ban) that were left out from previous merge.
832
    if (url.GetProtocol()=="musicdb")
833
    {
834
      CStdString strFile = CUtil::GetFileName(strFileName);
835
      CUtil::RemoveExtension(strFile);
836
      return GetSongById(atol(strFile.c_str()), song);
837
    }
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
838
839
    CStdString strPath;
840
    CUtil::GetDirectory(strFileName, strPath);
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
841
    CUtil::AddSlashAtEnd(strPath);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
842
843
    if (NULL == m_pDB.get()) return false;
844
    if (NULL == m_pDS.get()) return false;
845
4883 by jmarshallnz
- 02-03-2006 fixed: Musicdatabase paths weren't compared case-insensitive (albums + songs could double up if scanned from 2 different cased bookmarks)
846
    DWORD crc = ComputeCRC(strFileName);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
847
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
848
    CStdString strSQL=FormatSQL("select * from songview "
4882 by jmarshallnz
- 02-03-2006 fixed: Musicdatabase paths weren't compared case-insensitive (albums + songs could double up if scanned from 2 different cased bookmarks)
849
                                "where dwFileNameCRC='%ul' and strPath like'%s'"
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
850
                                , crc,
851
                                strPath.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
852
853
    if (!m_pDS->query(strSQL.c_str())) return false;
854
    int iRowsFound = m_pDS->num_rows();
855
    if (iRowsFound == 0)
2307 by charlydoes
fixed: some memory leaks in the database code.
856
    {
857
      m_pDS->close();
858
      return false;
859
    }
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
860
    song = GetSongFromDataset();
861
    m_pDS->close(); // cleanup recordset data
862
    return true;
863
  }
864
  catch (...)
865
  {
8035 by yuvalt
Initial port to Linux/SDL
866
    CLog::Log(LOGERROR, "%s(%s) failed", __FUNCTION__, strFileName.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
867
  }
675 by bobbin007
added: folder.jpg is now shown in Music overlay
868
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
869
  return false;
105 by yamp
new music database
870
}
871
17467 by jmarshallnz
changed: long -> int in database functions.
872
int CMusicDatabase::GetAlbumIdByPath(const CStdString& strPath)
9930 by vulkanr
merge with trunk. revision 12580.
873
{
874
  try
875
  {
876
    CStdString strSQL=FormatSQL("select distinct idAlbum from song join path on song.idPath = path.idPath where path.strPath like '%s'", strPath.c_str());
877
    m_pDS->query(strSQL.c_str());
878
    if (m_pDS->eof())
879
      return -1;
880
17467 by jmarshallnz
changed: long -> int in database functions.
881
    int idAlbum = m_pDS->fv(0).get_asInt();
9930 by vulkanr
merge with trunk. revision 12580.
882
    m_pDS->close();
883
884
    return idAlbum;
885
  }
886
  catch (...)
887
  {
888
    CLog::Log(LOGERROR, "%s(%s) failed", __FUNCTION__, strPath.c_str());
889
  }
890
891
  return false;
892
}
893
17467 by jmarshallnz
changed: long -> int in database functions.
894
int CMusicDatabase::GetSongByArtistAndAlbumAndTitle(const CStdString& strArtist, const CStdString& strAlbum, const CStdString& strTitle)
8679 by vulkanr
895
{
896
  try
897
  {
898
    CStdString strSQL=FormatSQL("select idsong from songview "
899
                                "where strArtist like '%s' and strAlbum like '%s' and "
900
                                "strTitle like '%s'",strArtist.c_str(),strAlbum.c_str(),strTitle.c_str());
901
902
    if (!m_pDS->query(strSQL.c_str())) return false;
903
    int iRowsFound = m_pDS->num_rows();
904
    if (iRowsFound == 0)
905
    {
906
      m_pDS->close();
907
      return -1;
908
    }
17467 by jmarshallnz
changed: long -> int in database functions.
909
    int lResult = m_pDS->fv(0).get_asInt();
8679 by vulkanr
910
    m_pDS->close(); // cleanup recordset data
911
    return lResult;
912
  }
913
  catch (...)
914
  {
915
    CLog::Log(LOGERROR, "%s (%s,%s,%s) failed", __FUNCTION__, strArtist.c_str(),strAlbum.c_str(),strTitle.c_str());
916
  }
917
918
  return -1;
919
}
920
17467 by jmarshallnz
changed: long -> int in database functions.
921
bool CMusicDatabase::GetSongById(int idSong, CSong& song)
4458 by bobbin007
*** empty log message ***
922
{
923
  try
924
  {
925
    song.Clear();
926
927
    if (NULL == m_pDB.get()) return false;
928
    if (NULL == m_pDS.get()) return false;
929
930
    CStdString strSQL=FormatSQL("select * from songview "
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
931
                                "where idSong=%i"
4458 by bobbin007
*** empty log message ***
932
                                , idSong);
933
934
    if (!m_pDS->query(strSQL.c_str())) return false;
935
    int iRowsFound = m_pDS->num_rows();
936
    if (iRowsFound == 0)
937
    {
938
      m_pDS->close();
939
      return false;
940
    }
941
    song = GetSongFromDataset();
942
    m_pDS->close(); // cleanup recordset data
943
    return true;
944
  }
945
  catch (...)
946
  {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
947
    CLog::Log(LOGERROR, "%s(%i) failed", __FUNCTION__, idSong);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
948
  }
675 by bobbin007
added: folder.jpg is now shown in Music overlay
949
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
950
  return false;
105 by yamp
new music database
951
}
952
7862 by jmarshallnz
added: Search entry to the music library - allows searching of albums/artists and songs with on the fly updating.
953
bool CMusicDatabase::SearchArtists(const CStdString& search, CFileItemList &artists)
1290 by bobbin007
added: my music, search based on musicdatabase
954
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
955
  try
956
  {
957
    if (NULL == m_pDB.get()) return false;
958
    if (NULL == m_pDS.get()) return false;
959
960
    // Exclude "Various Artists"
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
961
    int idVariousArtist = AddArtist(g_localizeStrings.Get(340));
3740 by bobbin007
added: base class for database classes
962
7893 by jmarshallnz
changed: Search tab in music library only does a full text search if the search string is at least 3 characters long. Otherwise, it just searches on the first word.
963
    CStdString strSQL;
964
    if (search.GetLength() >= MIN_FULL_SEARCH_LENGTH)
965
      strSQL=FormatSQL("select * from artist "
7869 by jmarshallnz
changed: Search in Music Library now matches all words, rather than just the first word. Slower, but more useful.
966
                                "where (strArtist like '%s%%' or strArtist like '%% %s%%') and idArtist <> %i "
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
967
                                , search.c_str(), search.c_str(), idVariousArtist );
7893 by jmarshallnz
changed: Search tab in music library only does a full text search if the search string is at least 3 characters long. Otherwise, it just searches on the first word.
968
    else
969
      strSQL=FormatSQL("select * from artist "
970
                                "where strArtist like '%s%%' and idArtist <> %i "
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
971
                                , search.c_str(), idVariousArtist );
7862 by jmarshallnz
added: Search entry to the music library - allows searching of albums/artists and songs with on the fly updating.
972
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
973
    if (!m_pDS->query(strSQL.c_str())) return false;
7862 by jmarshallnz
added: Search entry to the music library - allows searching of albums/artists and songs with on the fly updating.
974
    if (m_pDS->num_rows() == 0)
2307 by charlydoes
fixed: some memory leaks in the database code.
975
    {
976
      m_pDS->close();
977
      return false;
978
    }
10519 by spiff_
cosmetics (trailing whitespace)
979
10245 by vulkanr
merge with trunk. rev. 13243.
980
    CStdString artistLabel(g_localizeStrings.Get(557)); // Artist
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
981
    while (!m_pDS->eof())
982
    {
7862 by jmarshallnz
added: Search entry to the music library - allows searching of albums/artists and songs with on the fly updating.
983
      CStdString path;
17467 by jmarshallnz
changed: long -> int in database functions.
984
      path.Format("musicdb://2/%ld/", m_pDS->fv(0).get_asInt());
10487 by jmarshallnz
changed: Use boost::shared_ptr in CFileItemLists to reduce the
985
      CFileItemPtr pItem(new CFileItem(path, true));
7862 by jmarshallnz
added: Search entry to the music library - allows searching of albums/artists and songs with on the fly updating.
986
      CStdString label;
987
      label.Format("[%s] %s", artistLabel.c_str(), m_pDS->fv(1).get_asString());
988
      pItem->SetLabel(label);
989
      label.Format("A %s", m_pDS->fv(1).get_asString()); // sort label is stored in the title tag
990
      pItem->GetMusicInfoTag()->SetTitle(label);
7905 by jmarshallnz
changed: Music library search is delayed 1s after letter input to allow you more time to input letters.
991
      pItem->SetCachedArtistThumb();
7862 by jmarshallnz
added: Search entry to the music library - allows searching of albums/artists and songs with on the fly updating.
992
      artists.Add(pItem);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
993
      m_pDS->next();
994
    }
1290 by bobbin007
added: my music, search based on musicdatabase
995
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
996
    m_pDS->close(); // cleanup recordset data
997
    return true;
998
  }
999
  catch (...)
1000
  {
8035 by yuvalt
Initial port to Linux/SDL
1001
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
3583 by kraqh3d
Part of the new music library search function
1002
  }
1003
1004
  return false;
1005
}
1006
7104 by darkdonno
changed: file cosmetics removed: trailing whitespace fixed: some TAB usage added: GPL Header
1007
bool CMusicDatabase::GetArbitraryQuery(const CStdString& strQuery, const CStdString& strOpenRecordSet, const CStdString& strCloseRecordSet,
10519 by spiff_
cosmetics (trailing whitespace)
1008
                                       const CStdString& strOpenRecord, const CStdString& strCloseRecord, const CStdString& strOpenField,
9706 by spiff_
janitoring (tabs -> spaces)
1009
                                       const CStdString& strCloseField, CStdString& strResult)
6703 by xbmcapi
Added: GetArbitraryQuery to CMusicDatabase
1010
{
1011
  try
1012
  {
1013
    strResult = "";
9706 by spiff_
janitoring (tabs -> spaces)
1014
    if (NULL == m_pDB.get()) return false;
1015
    if (NULL == m_pDS.get()) return false;
14574 by d4rkm4ster
fixed: don't perform string formatting on SQL without arguments (same fix as r19130).
1016
    CStdString strSQL=strQuery;
9706 by spiff_
janitoring (tabs -> spaces)
1017
    if (!m_pDS->query(strSQL.c_str()))
1018
    {
1019
      strResult = m_pDB->getErrorMsg();
1020
      return false;
1021
    }
1022
    strResult=strOpenRecordSet;
1023
    while (!m_pDS->eof())
1024
    {
1025
      strResult += strOpenRecord;
1026
      for (int i=0; i<m_pDS->fieldCount(); i++)
1027
      {
1028
        strResult += strOpenField;
1029
        strResult += m_pDS->fv(i).get_asString();
1030
        strResult += strCloseField;
1031
      }
1032
      strResult += strCloseRecord;
1033
      m_pDS->next();
1034
    }
1035
    strResult += strCloseRecordSet;
1036
    m_pDS->close();
1037
    return true;
6703 by xbmcapi
Added: GetArbitraryQuery to CMusicDatabase
1038
  }
1039
  catch (...)
1040
  {
8035 by yuvalt
Initial port to Linux/SDL
1041
    CLog::Log(LOGERROR, "%s(%s) failed", __FUNCTION__, strQuery.c_str());
6703 by xbmcapi
Added: GetArbitraryQuery to CMusicDatabase
1042
  }
1043
  try
1044
  {
9706 by spiff_
janitoring (tabs -> spaces)
1045
    if (NULL == m_pDB.get()) return false;
1046
      strResult = m_pDB->getErrorMsg();
6703 by xbmcapi
Added: GetArbitraryQuery to CMusicDatabase
1047
  }
1048
  catch (...)
1049
  {
7104 by darkdonno
changed: file cosmetics removed: trailing whitespace fixed: some TAB usage added: GPL Header
1050
6703 by xbmcapi
Added: GetArbitraryQuery to CMusicDatabase
1051
  }
1052
1053
  return false;
1054
}
1055
14405 by jmarshallnz
fixed: You couldn't exec() on the music database from the http api (ticket #6085, thanks to m69bv3)
1056
bool CMusicDatabase::ArbitraryExec(const CStdString& strExec)
1057
{
1058
  try
1059
  {
1060
    if (NULL == m_pDB.get()) return false;
1061
    if (NULL == m_pDS.get()) return false;
14574 by d4rkm4ster
fixed: don't perform string formatting on SQL without arguments (same fix as r19130).
1062
    CStdString strSQL = strExec;
14405 by jmarshallnz
fixed: You couldn't exec() on the music database from the http api (ticket #6085, thanks to m69bv3)
1063
    m_pDS->exec(strSQL.c_str());
1064
    m_pDS->close();
1065
    return true;
1066
  }
1067
  catch (...)
1068
  {
1069
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
1070
  }
1071
  return false;
1072
}
1073
17467 by jmarshallnz
changed: long -> int in database functions.
1074
bool CMusicDatabase::GetAlbumInfo(int idAlbum, CAlbum &info, VECSONGS* songs)
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
1075
{
1076
  try
1077
  {
1078
    if (idAlbum == -1)
1079
      return false; // not in the database
3740 by bobbin007
added: base class for database classes
1080
10245 by vulkanr
merge with trunk. rev. 13243.
1081
    CStdString strSQL=FormatSQL("select * from albumview where idAlbum = %ld", idAlbum);
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
1082
9930 by vulkanr
merge with trunk. revision 12580.
1083
    if (!m_pDS2->query(strSQL.c_str())) return false;
10043 by vulkanr
merge with trunk. rev. 12805.
1084
    int iRowsFound = m_pDS2->num_rows();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1085
    if (iRowsFound != 0)
1086
    {
10245 by vulkanr
merge with trunk. rev. 13243.
1087
      info = GetAlbumFromDataset(m_pDS2.get(), true); // true to grab the thumburl rather than the thumb
17467 by jmarshallnz
changed: long -> int in database functions.
1088
      int idAlbumInfo = m_pDS2->fv(album_idAlbumInfo).get_asInt();
9930 by vulkanr
merge with trunk. revision 12580.
1089
      m_pDS2->close(); // cleanup recordset data
1090
1091
      if (songs)
1092
        GetAlbumInfoSongs(idAlbumInfo, *songs);
1093
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1094
      return true;
1095
    }
10043 by vulkanr
merge with trunk. rev. 12805.
1096
    m_pDS2->close();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1097
    return false;
1098
  }
1099
  catch (...)
1100
  {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
1101
    CLog::Log(LOGERROR, "%s(%i) failed", __FUNCTION__, idAlbum);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1102
  }
675 by bobbin007
added: folder.jpg is now shown in Music overlay
1103
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1104
  return false;
108 by yamp
more my music database fixed
1105
}
340 by yamp
- Added: different playlists for playlistplayer (music, music temp, video, video temp)
1106
17467 by jmarshallnz
changed: long -> int in database functions.
1107
bool CMusicDatabase::HasAlbumInfo(int idAlbum)
10274 by vulkanr
merge with trunk. rev. 13320.
1108
{
1109
  try
1110
  {
1111
    if (idAlbum == -1)
1112
      return false; // not in the database
1113
1114
    CStdString strSQL=FormatSQL("select * from albuminfo where idAlbum = %ld", idAlbum);
1115
1116
    if (!m_pDS2->query(strSQL.c_str())) return false;
1117
    int iRowsFound = m_pDS2->num_rows();
1118
    m_pDS2->close();
1119
    return iRowsFound > 0;
1120
  }
1121
  catch (...)
1122
  {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
1123
    CLog::Log(LOGERROR, "%s(%i) failed", __FUNCTION__, idAlbum);
10274 by vulkanr
merge with trunk. rev. 13320.
1124
  }
1125
1126
  return false;
1127
}
1128
17467 by jmarshallnz
changed: long -> int in database functions.
1129
bool CMusicDatabase::DeleteAlbumInfo(int idAlbum)
9912 by spiff_
fixed: refresh album/artist info didnt work
1130
{
1131
  try
1132
  {
1133
    if (idAlbum == -1)
1134
      return false; // not in the database
1135
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
1136
    CStdString strSQL = FormatSQL("delete from albuminfo where idAlbum=%i",idAlbum);
9912 by spiff_
fixed: refresh album/artist info didnt work
1137
10519 by spiff_
cosmetics (trailing whitespace)
1138
    if (!m_pDS2->exec(strSQL.c_str()))
9912 by spiff_
fixed: refresh album/artist info didnt work
1139
      return false;
1140
1141
    return true;
1142
  }
1143
  catch (...)
1144
  {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
1145
    CLog::Log(LOGERROR, "%s - (%i) failed", __FUNCTION__, idAlbum);
9912 by spiff_
fixed: refresh album/artist info didnt work
1146
  }
1147
1148
  return false;
1149
}
1150
17467 by jmarshallnz
changed: long -> int in database functions.
1151
bool CMusicDatabase::GetArtistInfo(int idArtist, CArtist &info, bool needAll)
9868 by vulkanr
merge with trunk. rev. 12480.
1152
{
1153
  try
1154
  {
1155
    if (idArtist == -1)
1156
      return false; // not in the database
1157
1158
    CStdString strSQL=FormatSQL("select * from artistinfo "
1159
                                "join artist on artist.idartist=artistinfo.idArtist "
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
1160
                                "where artistinfo.idArtist = %i"
9868 by vulkanr
merge with trunk. rev. 12480.
1161
                                , idArtist);
1162
1163
    if (!m_pDS2->query(strSQL.c_str())) return false;
1164
    int iRowsFound = m_pDS2->num_rows();
1165
    if (iRowsFound != 0)
1166
    {
9930 by vulkanr
merge with trunk. revision 12580.
1167
      info = GetArtistFromDataset(m_pDS2.get(),needAll);
1168
      if (needAll)
9868 by vulkanr
merge with trunk. rev. 12480.
1169
      {
9930 by vulkanr
merge with trunk. revision 12580.
1170
        strSQL=FormatSQL("select * from discography where idArtist=%i",idArtist);
1171
        m_pDS2->query(strSQL.c_str());
1172
        while (!m_pDS2->eof())
1173
        {
13056 by spiff_
changed: utilize std namespace flattening
1174
          info.discography.push_back(make_pair(m_pDS2->fv("strAlbum").get_asString(),m_pDS2->fv("strYear").get_asString()));
9930 by vulkanr
merge with trunk. revision 12580.
1175
          m_pDS2->next();
1176
        }
9868 by vulkanr
merge with trunk. rev. 12480.
1177
      }
1178
      m_pDS2->close(); // cleanup recordset data
1179
      return true;
1180
    }
1181
    m_pDS2->close();
1182
    return false;
1183
  }
1184
  catch (...)
1185
  {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
1186
    CLog::Log(LOGERROR, "%s - (%i) failed", __FUNCTION__, idArtist);
9868 by vulkanr
merge with trunk. rev. 12480.
1187
  }
1188
1189
  return false;
1190
}
1191
17467 by jmarshallnz
changed: long -> int in database functions.
1192
bool CMusicDatabase::DeleteArtistInfo(int idArtist)
9912 by spiff_
fixed: refresh album/artist info didnt work
1193
{
1194
  try
1195
  {
1196
    if (idArtist == -1)
1197
      return false; // not in the database
1198
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
1199
    CStdString strSQL = FormatSQL("delete from artistinfo where idartist=%i",idArtist);
9912 by spiff_
fixed: refresh album/artist info didnt work
1200
10519 by spiff_
cosmetics (trailing whitespace)
1201
    if (!m_pDS2->exec(strSQL.c_str()))
9912 by spiff_
fixed: refresh album/artist info didnt work
1202
      return false;
1203
1204
    return true;
1205
  }
1206
  catch (...)
1207
  {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
1208
    CLog::Log(LOGERROR, "%s - (%i) failed", __FUNCTION__, idArtist);
9912 by spiff_
fixed: refresh album/artist info didnt work
1209
  }
1210
1211
  return false;
1212
}
1213
17467 by jmarshallnz
changed: long -> int in database functions.
1214
bool CMusicDatabase::GetAlbumInfoSongs(int idAlbumInfo, VECSONGS& songs)
2303 by bobbin007
added: albuminfo, songs from allmusic.com are saved to database
1215
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1216
  try
1217
  {
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
1218
    CStdString strSQL=FormatSQL("select * from albuminfosong "
1219
                                "where idAlbumInfo=%i "
1220
                                "order by iTrack", idAlbumInfo);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1221
9930 by vulkanr
merge with trunk. revision 12580.
1222
    if (!m_pDS2->query(strSQL.c_str())) return false;
1223
    int iRowsFound = m_pDS2->num_rows();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1224
    if (iRowsFound == 0) return false;
9930 by vulkanr
merge with trunk. revision 12580.
1225
    while (!m_pDS2->eof())
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1226
    {
1227
      CSong song;
17467 by jmarshallnz
changed: long -> int in database functions.
1228
      song.iTrack = m_pDS2->fv("iTrack").get_asInt();
9930 by vulkanr
merge with trunk. revision 12580.
1229
      song.strTitle = m_pDS2->fv("strTitle").get_asString();
17467 by jmarshallnz
changed: long -> int in database functions.
1230
      song.iDuration = m_pDS2->fv("iDuration").get_asInt();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1231
1232
      songs.push_back(song);
9930 by vulkanr
merge with trunk. revision 12580.
1233
      m_pDS2->next();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1234
    }
1235
9930 by vulkanr
merge with trunk. revision 12580.
1236
    m_pDS2->close(); // cleanup recordset data
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1237
1238
    return true;
1239
  }
1240
  catch (...)
1241
  {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
1242
    CLog::Log(LOGERROR, "%s(%i) failed", __FUNCTION__, idAlbumInfo);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1243
  }
2303 by bobbin007
added: albuminfo, songs from allmusic.com are saved to database
1244
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1245
  return false;
2303 by bobbin007
added: albuminfo, songs from allmusic.com are saved to database
1246
}
1247
4458 by bobbin007
*** empty log message ***
1248
bool CMusicDatabase::GetTop100(const CStdString& strBaseDir, CFileItemList& items)
340 by yamp
- Added: different playlists for playlistplayer (music, music temp, video, video temp)
1249
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1250
  try
1251
  {
1252
    if (NULL == m_pDB.get()) return false;
1253
    if (NULL == m_pDS.get()) return false;
3740 by bobbin007
added: base class for database classes
1254
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
1255
    CStdString strSQL="select * from songview "
1256
                      "where iTimesPlayed>0 "
1257
                      "order by iTimesPlayed desc "
1258
                      "limit 100";
1259
8035 by yuvalt
Initial port to Linux/SDL
1260
    CLog::Log(LOGDEBUG, "%s query: %s", __FUNCTION__, strSQL.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1261
    if (!m_pDS->query(strSQL.c_str())) return false;
1262
    int iRowsFound = m_pDS->num_rows();
1263
    if (iRowsFound == 0)
2307 by charlydoes
fixed: some memory leaks in the database code.
1264
    {
1265
      m_pDS->close();
1266
      return false;
1267
    }
4401 by kraqh3d
- 10-12-2005 fixed: Top 100 items in Music Library
1268
    items.Reserve(iRowsFound);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1269
    while (!m_pDS->eof())
1270
    {
10487 by jmarshallnz
changed: Use boost::shared_ptr in CFileItemLists to reduce the
1271
      CFileItemPtr item(new CFileItem);
1272
      GetFileItemFromDataset(item.get(), strBaseDir);
4391 by kraqh3d
- 06-12-2005 fixed: My Music Library access to Playlists and Top 100 Songs
1273
      items.Add(item);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1274
      m_pDS->next();
1275
    }
1276
1277
    m_pDS->close(); // cleanup recordset data
1278
    return true;
1279
  }
1280
  catch (...)
1281
  {
8035 by yuvalt
Initial port to Linux/SDL
1282
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1283
  }
1284
1285
  return false;
340 by yamp
- Added: different playlists for playlistplayer (music, music temp, video, video temp)
1286
}
1287
3877 by kraqh3d
- 10-08-2005 added: integrated the Music Top 100 view into the Music Library at the top level (old window has not been removed yet)
1288
bool CMusicDatabase::GetTop100Albums(VECALBUMS& albums)
1289
{
1290
  try
1291
  {
1292
    albums.erase(albums.begin(), albums.end());
1293
    if (NULL == m_pDB.get()) return false;
1294
    if (NULL == m_pDS.get()) return false;
7104 by darkdonno
changed: file cosmetics removed: trailing whitespace fixed: some TAB usage added: GPL Header
1295
8813 by vulkanr
merge with trunk. revision: 10616.
1296
    // NOTE: The song.idAlbum is needed for the group by, as for some reason group by albumview.idAlbum doesn't work
1297
    //       consistently - possibly an SQLite bug, as it works fine in SQLiteSpy (v3.3.17)
1298
    CStdString strSQL = "select albumview.*, sum(song.iTimesPlayed) as total, song.idAlbum from song "
8289 by yuvalt
Merged with trunk up to revision 9184
1299
                    "join albumview on albumview.idAlbum=song.idAlbum "
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
1300
                    "where song.iTimesPlayed>0 "
8813 by vulkanr
merge with trunk. revision: 10616.
1301
                    "group by song.idAlbum "
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
1302
                    "order by total desc "
1303
                    "limit 100 ";
3877 by kraqh3d
- 10-08-2005 added: integrated the Music Top 100 view into the Music Library at the top level (old window has not been removed yet)
1304
8035 by yuvalt
Initial port to Linux/SDL
1305
    CLog::Log(LOGDEBUG, "%s query: %s", __FUNCTION__, strSQL.c_str());
3877 by kraqh3d
- 10-08-2005 added: integrated the Music Top 100 view into the Music Library at the top level (old window has not been removed yet)
1306
    if (!m_pDS->query(strSQL.c_str())) return false;
1307
    int iRowsFound = m_pDS->num_rows();
1308
    if (iRowsFound == 0)
1309
    {
1310
      m_pDS->close();
1311
      return false;
1312
    }
1313
    while (!m_pDS->eof())
1314
    {
9868 by vulkanr
merge with trunk. rev. 12480.
1315
      albums.push_back(GetAlbumFromDataset(m_pDS.get()));
3877 by kraqh3d
- 10-08-2005 added: integrated the Music Top 100 view into the Music Library at the top level (old window has not been removed yet)
1316
      m_pDS->next();
1317
    }
1318
1319
    m_pDS->close(); // cleanup recordset data
1320
    return true;
1321
  }
1322
  catch (...)
1323
  {
8035 by yuvalt
Initial port to Linux/SDL
1324
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
3877 by kraqh3d
- 10-08-2005 added: integrated the Music Top 100 view into the Music Library at the top level (old window has not been removed yet)
1325
  }
1326
1327
  return false;
1328
}
1329
4458 by bobbin007
*** empty log message ***
1330
bool CMusicDatabase::GetTop100AlbumSongs(const CStdString& strBaseDir, CFileItemList& items)
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1331
{
1332
  try
1333
  {
1334
    if (NULL == m_pDB.get()) return false;
1335
    if (NULL == m_pDS.get()) return false;
1336
1337
    CStdString strSQL;
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
1338
    strSQL.Format("select * from songview join albumview on (songview.idAlbum = albumview.idAlbum) where albumview.idalbum in (select song.idAlbum from song where song.iTimesPlayed>0 group by idalbum order by sum(song.iTimesPlayed) desc limit 100) order by albumview.idalbum in (select song.idAlbum from song where song.iTimesPlayed>0 group by idalbum order by sum(song.iTimesPlayed) desc limit 100)");
4414 by kraqh3d
- 11-12-2005 changed: All Albums in Top 100 Albums, Recently Added Albums, Recently Played Albums is now a nested sql query for better performance
1339
    CLog::Log(LOGDEBUG,"GetTop100AlbumSongs() query: %s", strSQL.c_str());
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1340
    if (!m_pDS->query(strSQL.c_str())) return false;
1341
1342
    int iRowsFound = m_pDS->num_rows();
1343
    if (iRowsFound == 0)
1344
    {
1345
      m_pDS->close();
1346
      return false;
1347
    }
1348
1349
    // get data from returned rows
1350
    items.Reserve(iRowsFound);
1351
    while (!m_pDS->eof())
1352
    {
10487 by jmarshallnz
changed: Use boost::shared_ptr in CFileItemLists to reduce the
1353
      CFileItemPtr item(new CFileItem);
1354
      GetFileItemFromDataset(item.get(), strBaseDir);
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1355
      items.Add(item);
1356
      m_pDS->next();
1357
    }
1358
1359
    // cleanup
7104 by darkdonno
changed: file cosmetics removed: trailing whitespace fixed: some TAB usage added: GPL Header
1360
    m_pDS->close();
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1361
    return true;
1362
  }
1363
  catch (...)
1364
  {
8035 by yuvalt
Initial port to Linux/SDL
1365
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1366
  }
1367
  return false;
1368
}
1369
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
1370
bool CMusicDatabase::GetRecentlyPlayedAlbums(VECALBUMS& albums)
3905 by kraqh3d
- 17-08-2005 added: Recent Albums to Music Library, presents a list of the last 25 albums scanned into the database
1371
{
1372
  try
1373
  {
1374
    albums.erase(albums.begin(), albums.end());
1375
    if (NULL == m_pDB.get()) return false;
1376
    if (NULL == m_pDS.get()) return false;
1377
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1378
    CStdString strSQL;
20933 by jmarshallnz
added: Improved portability of SQL statements used in the video and music libraries, thanks to firnsy. Ticket #8169.
1379
    strSQL.Format("select distinct albumview.* from song join albumview on albumview.idAlbum=song.idAlbum where song.lastplayed IS NOT NULL order by song.lastplayed desc limit %i", RECENTLY_PLAYED_LIMIT);
8035 by yuvalt
Initial port to Linux/SDL
1380
    CLog::Log(LOGDEBUG, "%s query: %s", __FUNCTION__, strSQL.c_str());
3905 by kraqh3d
- 17-08-2005 added: Recent Albums to Music Library, presents a list of the last 25 albums scanned into the database
1381
    if (!m_pDS->query(strSQL.c_str())) return false;
1382
    int iRowsFound = m_pDS->num_rows();
1383
    if (iRowsFound == 0)
1384
    {
1385
      m_pDS->close();
1386
      return false;
1387
    }
1388
    while (!m_pDS->eof())
1389
    {
9868 by vulkanr
merge with trunk. rev. 12480.
1390
      albums.push_back(GetAlbumFromDataset(m_pDS.get()));
3905 by kraqh3d
- 17-08-2005 added: Recent Albums to Music Library, presents a list of the last 25 albums scanned into the database
1391
      m_pDS->next();
1392
    }
1393
1394
    m_pDS->close(); // cleanup recordset data
1395
    return true;
1396
  }
1397
  catch (...)
1398
  {
8035 by yuvalt
Initial port to Linux/SDL
1399
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
1400
  }
1401
1402
  return false;
1403
}
1404
4458 by bobbin007
*** empty log message ***
1405
bool CMusicDatabase::GetRecentlyPlayedAlbumSongs(const CStdString& strBaseDir, CFileItemList& items)
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1406
{
1407
  try
1408
  {
1409
    if (NULL == m_pDB.get()) return false;
1410
    if (NULL == m_pDS.get()) return false;
1411
1412
    CStdString strSQL;
20933 by jmarshallnz
added: Improved portability of SQL statements used in the video and music libraries, thanks to firnsy. Ticket #8169.
1413
    strSQL.Format("select * from songview join albumview on (songview.idAlbum = albumview.idAlbum) where albumview.idalbum in (select distinct albumview.idalbum from albumview join song on albumview.idAlbum=song.idAlbum where song.lastplayed IS NOT NULL order by song.lastplayed desc limit %i)", g_advancedSettings.m_iMusicLibraryRecentlyAddedItems);
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1414
    CLog::Log(LOGDEBUG,"GetRecentlyPlayedAlbumSongs() query: %s", strSQL.c_str());
1415
    if (!m_pDS->query(strSQL.c_str())) return false;
1416
1417
    int iRowsFound = m_pDS->num_rows();
1418
    if (iRowsFound == 0)
1419
    {
1420
      m_pDS->close();
1421
      return false;
1422
    }
1423
1424
    // get data from returned rows
1425
    items.Reserve(iRowsFound);
1426
    while (!m_pDS->eof())
1427
    {
10487 by jmarshallnz
changed: Use boost::shared_ptr in CFileItemLists to reduce the
1428
      CFileItemPtr item(new CFileItem);
1429
      GetFileItemFromDataset(item.get(), strBaseDir);
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1430
      items.Add(item);
1431
      m_pDS->next();
1432
    }
1433
1434
    // cleanup
7104 by darkdonno
changed: file cosmetics removed: trailing whitespace fixed: some TAB usage added: GPL Header
1435
    m_pDS->close();
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1436
    return true;
1437
  }
1438
  catch (...)
1439
  {
8035 by yuvalt
Initial port to Linux/SDL
1440
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1441
  }
1442
  return false;
1443
}
1444
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
1445
bool CMusicDatabase::GetRecentlyAddedAlbums(VECALBUMS& albums)
1446
{
1447
  try
1448
  {
1449
    albums.erase(albums.begin(), albums.end());
1450
    if (NULL == m_pDB.get()) return false;
1451
    if (NULL == m_pDS.get()) return false;
1452
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1453
    CStdString strSQL;
15837 by vdrfan
changed: made the number of recently added items in music- and videolibrary user-definable via advancedsettings.xml (defaults to 25) - fixes #6754
1454
    strSQL.Format("select * from albumview order by idAlbum desc limit %i", g_advancedSettings.m_iMusicLibraryRecentlyAddedItems);
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
1455
8035 by yuvalt
Initial port to Linux/SDL
1456
    CLog::Log(LOGDEBUG, "%s query: %s", __FUNCTION__, strSQL.c_str());
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
1457
    if (!m_pDS->query(strSQL.c_str())) return false;
1458
    int iRowsFound = m_pDS->num_rows();
1459
    if (iRowsFound == 0)
1460
    {
1461
      m_pDS->close();
1462
      return false;
1463
    }
1464
1465
    while (!m_pDS->eof())
1466
    {
9868 by vulkanr
merge with trunk. rev. 12480.
1467
      albums.push_back(GetAlbumFromDataset(m_pDS.get()));
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
1468
      m_pDS->next();
1469
    }
1470
1471
    m_pDS->close(); // cleanup recordset data
1472
    return true;
1473
  }
1474
  catch (...)
1475
  {
8035 by yuvalt
Initial port to Linux/SDL
1476
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
3905 by kraqh3d
- 17-08-2005 added: Recent Albums to Music Library, presents a list of the last 25 albums scanned into the database
1477
  }
1478
1479
  return false;
1480
}
1481
4458 by bobbin007
*** empty log message ***
1482
bool CMusicDatabase::GetRecentlyAddedAlbumSongs(const CStdString& strBaseDir, CFileItemList& items)
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1483
{
1484
  try
1485
  {
1486
    if (NULL == m_pDB.get()) return false;
1487
    if (NULL == m_pDS.get()) return false;
1488
1489
    CStdString strSQL;
15837 by vdrfan
changed: made the number of recently added items in music- and videolibrary user-definable via advancedsettings.xml (defaults to 25) - fixes #6754
1490
    strSQL.Format("select songview.* from albumview join songview on (songview.idAlbum = albumview.idAlbum) where albumview.idalbum in ( select idAlbum from albumview order by idAlbum desc limit %i)", g_advancedSettings.m_iMusicLibraryRecentlyAddedItems);
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1491
    CLog::Log(LOGDEBUG,"GetRecentlyAddedAlbumSongs() query: %s", strSQL.c_str());
1492
    if (!m_pDS->query(strSQL.c_str())) return false;
1493
1494
    int iRowsFound = m_pDS->num_rows();
1495
    if (iRowsFound == 0)
1496
    {
1497
      m_pDS->close();
1498
      return false;
1499
    }
1500
1501
    // get data from returned rows
1502
    items.Reserve(iRowsFound);
1503
    while (!m_pDS->eof())
1504
    {
10487 by jmarshallnz
changed: Use boost::shared_ptr in CFileItemLists to reduce the
1505
      CFileItemPtr item(new CFileItem);
1506
      GetFileItemFromDataset(item.get(), strBaseDir);
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1507
      items.Add(item);
1508
      m_pDS->next();
1509
    }
1510
1511
    // cleanup
7104 by darkdonno
changed: file cosmetics removed: trailing whitespace fixed: some TAB usage added: GPL Header
1512
    m_pDS->close();
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1513
    return true;
1514
  }
1515
  catch (...)
1516
  {
8035 by yuvalt
Initial port to Linux/SDL
1517
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4409 by kraqh3d
Added some new SQL queries to fix some navigational bugs in Music Library, and rearranged GetShares to better handle multipath bookmarks
1518
  }
1519
  return false;
1520
}
1521
3740 by bobbin007
added: base class for database classes
1522
bool CMusicDatabase::IncrTop100CounterByFileName(const CStdString& strFileName)
340 by yamp
- Added: different playlists for playlistplayer (music, music temp, video, video temp)
1523
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1524
  try
1525
  {
1526
    if (NULL == m_pDB.get()) return false;
1527
    if (NULL == m_pDS.get()) return false;
1528
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
1529
    int idSong = GetSongIDFromPath(strFileName);
8066 by yuvalt
Merged with main trunk up to revision 8787
1530
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
1531
    CStdString sql=FormatSQL("UPDATE song SET iTimesPlayed=iTimesPlayed+1, lastplayed=CURRENT_TIMESTAMP where idSong=%i", idSong);
8066 by yuvalt
Merged with main trunk up to revision 8787
1532
    m_pDS->exec(sql.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1533
    return true;
1534
  }
1535
  catch (...)
1536
  {
8035 by yuvalt
Initial port to Linux/SDL
1537
    CLog::Log(LOGERROR, "%s(%s) failed", __FUNCTION__, strFileName.c_str());
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
1538
  }
1539
1540
  return false;
1541
}
1542
1543
bool CMusicDatabase::GetSongsByPath(const CStdString& strPath, CSongMap& songs, bool bAppendToMap)
1544
{
1545
  try
1546
  {
1547
    ASSERT(CUtil::HasSlashAtEnd(strPath));
1548
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1549
    if (!bAppendToMap)
4889 by jmarshallnz
- 03-03-2006 fixed: Better music database lower case comparisons (paths are stored as is, but compared lower case).
1550
      songs.Clear();
3740 by bobbin007
added: base class for database classes
1551
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1552
    if (NULL == m_pDB.get()) return false;
1553
    if (NULL == m_pDS.get()) return false;
3740 by bobbin007
added: base class for database classes
1554
4882 by jmarshallnz
- 02-03-2006 fixed: Musicdatabase paths weren't compared case-insensitive (albums + songs could double up if scanned from 2 different cased bookmarks)
1555
    CStdString strSQL=FormatSQL("select * from songview where strPath like '%s'", strPath.c_str() );
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1556
    if (!m_pDS->query(strSQL.c_str())) return false;
1557
    int iRowsFound = m_pDS->num_rows();
1558
    if (iRowsFound == 0)
2307 by charlydoes
fixed: some memory leaks in the database code.
1559
    {
1560
      m_pDS->close();
1561
      return false;
1562
    }
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1563
    while (!m_pDS->eof())
1564
    {
1565
      CSong song = GetSongFromDataset();
4889 by jmarshallnz
- 03-03-2006 fixed: Better music database lower case comparisons (paths are stored as is, but compared lower case).
1566
      songs.Add(song.strFileName, song);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1567
      m_pDS->next();
1568
    }
675 by bobbin007
added: folder.jpg is now shown in Music overlay
1569
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1570
    m_pDS->close(); // cleanup recordset data
1571
    return true;
1572
  }
1573
  catch (...)
1574
  {
8035 by yuvalt
Initial port to Linux/SDL
1575
    CLog::Log(LOGERROR, "%s(%s) failed", __FUNCTION__, strPath.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1576
  }
675 by bobbin007
added: folder.jpg is now shown in Music overlay
1577
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1578
  return false;
595 by bobbin007
changed: my music, more scan speedup
1579
}
1580
584 by bobbin007
fixed: ogg tag reading was broken
1581
void CMusicDatabase::EmptyCache()
1582
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1583
  m_artistCache.erase(m_artistCache.begin(), m_artistCache.end());
1584
  m_genreCache.erase(m_genreCache.begin(), m_genreCache.end());
1585
  m_pathCache.erase(m_pathCache.begin(), m_pathCache.end());
1586
  m_albumCache.erase(m_albumCache.begin(), m_albumCache.end());
1587
  m_thumbCache.erase(m_thumbCache.begin(), m_thumbCache.end());
584 by bobbin007
fixed: ogg tag reading was broken
1588
}
605 by bobbin007
fixed: my music songs, sort for root items was not loaded from settings
1589
7862 by jmarshallnz
added: Search entry to the music library - allows searching of albums/artists and songs with on the fly updating.
1590
bool CMusicDatabase::Search(const CStdString& search, CFileItemList &items)
1591
{
17715 by jmarshallnz
cleanup: DWORD -> unsigned int where used for timing, and variable names. Ticket #7299, thanks to gnif.
1592
  unsigned int time = CTimeUtils::GetTimeMS();
7862 by jmarshallnz
added: Search entry to the music library - allows searching of albums/artists and songs with on the fly updating.
1593
  // first grab all the artists that match
1594
  SearchArtists(search, items);
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
1595
  CLog::Log(LOGDEBUG, "%s Artist search in %i ms",
17686 by davilla
[all] replace winapi timeGetTime/GetTickCOunt with CTimeUtils::GetTimeMS. Will need linux/win verify.
1596
            __FUNCTION__, CTimeUtils::GetTimeMS() - time); time = CTimeUtils::GetTimeMS();
7862 by jmarshallnz
added: Search entry to the music library - allows searching of albums/artists and songs with on the fly updating.
1597
1598
  // then albums that match
1599
  SearchAlbums(search, items);
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
1600
  CLog::Log(LOGDEBUG, "%s Album search in %i ms",
17686 by davilla
[all] replace winapi timeGetTime/GetTickCOunt with CTimeUtils::GetTimeMS. Will need linux/win verify.
1601
            __FUNCTION__, CTimeUtils::GetTimeMS() - time); time = CTimeUtils::GetTimeMS();
7862 by jmarshallnz
added: Search entry to the music library - allows searching of albums/artists and songs with on the fly updating.
1602
1603
  // and finally songs
1604
  SearchSongs(search, items);
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
1605
  CLog::Log(LOGDEBUG, "%s Songs search in %i ms",
17686 by davilla
[all] replace winapi timeGetTime/GetTickCOunt with CTimeUtils::GetTimeMS. Will need linux/win verify.
1606
            __FUNCTION__, CTimeUtils::GetTimeMS() - time); time = CTimeUtils::GetTimeMS();
7862 by jmarshallnz
added: Search entry to the music library - allows searching of albums/artists and songs with on the fly updating.
1607
  return true;
1608
}
1609
1610
bool CMusicDatabase::SearchSongs(const CStdString& search, CFileItemList &items)
1290 by bobbin007
added: my music, search based on musicdatabase
1611
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1612
  try
1613
  {
1614
    if (NULL == m_pDB.get()) return false;
1615
    if (NULL == m_pDS.get()) return false;
3740 by bobbin007
added: base class for database classes
1616
7893 by jmarshallnz
changed: Search tab in music library only does a full text search if the search string is at least 3 characters long. Otherwise, it just searches on the first word.
1617
    CStdString strSQL;
1618
    if (search.GetLength() >= MIN_FULL_SEARCH_LENGTH)
8384 by yuvalt
Merge with trunk up to revision 9336
1619
      strSQL=FormatSQL("select * from songview where strTitle like '%s%%' or strTitle like '%% %s%%' limit 1000", search.c_str(), search.c_str());
7893 by jmarshallnz
changed: Search tab in music library only does a full text search if the search string is at least 3 characters long. Otherwise, it just searches on the first word.
1620
    else
8384 by yuvalt
Merge with trunk up to revision 9336
1621
      strSQL=FormatSQL("select * from songview where strTitle like '%s%%' limit 1000", search.c_str());
7893 by jmarshallnz
changed: Search tab in music library only does a full text search if the search string is at least 3 characters long. Otherwise, it just searches on the first word.
1622
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1623
    if (!m_pDS->query(strSQL.c_str())) return false;
7862 by jmarshallnz
added: Search entry to the music library - allows searching of albums/artists and songs with on the fly updating.
1624
    if (m_pDS->num_rows() == 0) return false;
1625
1626
    CStdString songLabel = g_localizeStrings.Get(179); // Song
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1627
    while (!m_pDS->eof())
1628
    {
10487 by jmarshallnz
changed: Use boost::shared_ptr in CFileItemLists to reduce the
1629
      CFileItemPtr item(new CFileItem);
1630
      GetFileItemFromDataset(item.get(), "musicdb://4/");
7862 by jmarshallnz
added: Search entry to the music library - allows searching of albums/artists and songs with on the fly updating.
1631
      items.Add(item);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1632
      m_pDS->next();
1633
    }
1634
1635
    m_pDS->close();
1636
    return true;
1637
  }
1638
  catch (...)
1639
  {
8035 by yuvalt
Initial port to Linux/SDL
1640
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1641
  }
1290 by bobbin007
added: my music, search based on musicdatabase
1642
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1643
  return false;
1290 by bobbin007
added: my music, search based on musicdatabase
1644
}
1645
7862 by jmarshallnz
added: Search entry to the music library - allows searching of albums/artists and songs with on the fly updating.
1646
bool CMusicDatabase::SearchAlbums(const CStdString& search, CFileItemList &albums)
3583 by kraqh3d
Part of the new music library search function
1647
{
1648
  try
1649
  {
1650
    if (NULL == m_pDB.get()) return false;
1651
    if (NULL == m_pDS.get()) return false;
1652
7893 by jmarshallnz
changed: Search tab in music library only does a full text search if the search string is at least 3 characters long. Otherwise, it just searches on the first word.
1653
    CStdString strSQL;
1654
    if (search.GetLength() >= MIN_FULL_SEARCH_LENGTH)
1655
      strSQL=FormatSQL("select * from albumview where strAlbum like '%s%%' or strAlbum like '%% %s%%'", search.c_str(), search.c_str());
1656
    else
1657
      strSQL=FormatSQL("select * from albumview where strAlbum like '%s%%'", search.c_str());
1658
3583 by kraqh3d
Part of the new music library search function
1659
    if (!m_pDS->query(strSQL.c_str())) return false;
1660
11281 by jmarshallnz
fixed: [Album] wasn't prefixed to albums returned by the music library search.
1661
    CStdString albumLabel(g_localizeStrings.Get(558)); // Album
3583 by kraqh3d
Part of the new music library search function
1662
    while (!m_pDS->eof())
1663
    {
9868 by vulkanr
merge with trunk. rev. 12480.
1664
      CAlbum album = GetAlbumFromDataset(m_pDS.get());
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
1665
      CStdString path;
1666
      path.Format("musicdb://3/%ld/", album.idAlbum);
10487 by jmarshallnz
changed: Use boost::shared_ptr in CFileItemLists to reduce the
1667
      CFileItemPtr pItem(new CFileItem(path, album));
7862 by jmarshallnz
added: Search entry to the music library - allows searching of albums/artists and songs with on the fly updating.
1668
      CStdString label;
1669
      label.Format("[%s] %s", albumLabel.c_str(), album.strAlbum);
1670
      pItem->SetLabel(label);
1671
      label.Format("B %s", album.strAlbum); // sort label is stored in the title tag
1672
      pItem->GetMusicInfoTag()->SetTitle(label);
1673
      albums.Add(pItem);
3583 by kraqh3d
Part of the new music library search function
1674
      m_pDS->next();
1675
    }
1676
    m_pDS->close(); // cleanup recordset data
1677
    return true;
1678
  }
1679
  catch (...)
1680
  {
8035 by yuvalt
Initial port to Linux/SDL
1681
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
3583 by kraqh3d
Part of the new music library search function
1682
  }
1683
  return false;
1684
}
1685
17467 by jmarshallnz
changed: long -> int in database functions.
1686
int CMusicDatabase::SetAlbumInfo(int idAlbum, const CAlbum& album, const VECSONGS& songs, bool bTransaction)
1591 by bobbin007
changed: album info: shows albumname too, when inexact matches are found
1687
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1688
  CStdString strSQL;
1689
  try
1690
  {
1691
    if (NULL == m_pDB.get()) return -1;
1692
    if (NULL == m_pDS.get()) return -1;
1693
9868 by vulkanr
merge with trunk. rev. 12480.
1694
    if (bTransaction)
1695
      BeginTransaction();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1696
1697
    // and also the multiple genre string into single genres.
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
1698
    CStdStringArray vecGenres; CStdString extraGenres;
1699
    SplitString(album.strGenre, vecGenres, extraGenres);
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
1700
    int idGenre = AddGenre(vecGenres[0]);
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
1701
1702
    // delete any album info we may have
1703
    strSQL=FormatSQL("delete from albuminfo where idAlbum=%i", idAlbum);
1704
    m_pDS->exec(strSQL.c_str());
1705
1706
    // insert the albuminfo
9868 by vulkanr
merge with trunk. rev. 12480.
1707
    strSQL=FormatSQL("insert into albuminfo (idAlbumInfo,idAlbum,idGenre,strExtraGenres,strMoods,strStyles,strThemes,strReview,strImage,strLabel,strType,iRating,iYear) values(NULL,%i,%i,'%s','%s','%s','%s','%s','%s','%s','%s',%i,%i)",
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
1708
                  idAlbum, idGenre, extraGenres.c_str(),
9868 by vulkanr
merge with trunk. rev. 12480.
1709
                  album.strMoods.c_str(),
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1710
                  album.strStyles.c_str(),
9868 by vulkanr
merge with trunk. rev. 12480.
1711
                  album.strThemes.c_str(),
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1712
                  album.strReview.c_str(),
9868 by vulkanr
merge with trunk. rev. 12480.
1713
                  album.thumbURL.m_xml.c_str(),
1714
                  album.strLabel.c_str(),
1715
                  album.strType.c_str(),
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1716
                  album.iRating,
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
1717
                  album.iYear);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1718
    m_pDS->exec(strSQL.c_str());
19283 by jmarshallnz
added: Improved abstraction of the database API, thanks to loopingz and firnsy
1719
    int idAlbumInfo = (int)m_pDS->lastinsertid();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1720
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
1721
    if (SetAlbumInfoSongs(idAlbumInfo, songs))
9868 by vulkanr
merge with trunk. rev. 12480.
1722
    {
1723
      if (bTransaction)
1724
        CommitTransaction();
1725
    }
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1726
    else
1727
    {
9868 by vulkanr
merge with trunk. rev. 12480.
1728
      if (bTransaction) // icky
1729
        RollbackTransaction();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1730
      idAlbumInfo = -1;
1731
    }
1732
1733
    return idAlbumInfo;
1734
  }
1735
  catch (...)
1736
  {
8035 by yuvalt
Initial port to Linux/SDL
1737
    CLog::Log(LOGERROR, "%s failed with query (%s)", __FUNCTION__, strSQL.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1738
  }
1739
9868 by vulkanr
merge with trunk. rev. 12480.
1740
  if (bTransaction)
1741
    RollbackTransaction();
1742
1743
  return -1;
1744
}
1745
17467 by jmarshallnz
changed: long -> int in database functions.
1746
int CMusicDatabase::SetArtistInfo(int idArtist, const CArtist& artist)
9868 by vulkanr
merge with trunk. rev. 12480.
1747
{
1748
  CStdString strSQL;
1749
  try
1750
  {
1751
    if (NULL == m_pDB.get()) return -1;
1752
    if (NULL == m_pDS.get()) return -1;
1753
1754
    // delete any artist info we may have
1755
    strSQL=FormatSQL("delete from artistinfo where idArtist=%i", idArtist);
1756
    m_pDS->exec(strSQL.c_str());
1757
    strSQL=FormatSQL("delete from discography where idArtist=%i", idArtist);
1758
    m_pDS->exec(strSQL.c_str());
1759
1760
    // insert the artistinfo
14378 by spiff_
added: ticket #4834 - scraped music fanart. uses http://www.htbackdrops.com - go add your favorite artist. thanks to nuzecast for making the backend available
1761
    strSQL=FormatSQL("insert into artistinfo (idArtistInfo,idArtist,strBorn,strFormed,strGenres,strMoods,strStyles,strInstruments,strBiography,strDied,strDisbanded,strYearsActive,strImage,strFanart) values(NULL,%i,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
9868 by vulkanr
merge with trunk. rev. 12480.
1762
                  idArtist, artist.strBorn.c_str(),
1763
                  artist.strFormed.c_str(),
1764
                  artist.strGenre.c_str(),
1765
                  artist.strMoods.c_str(),
1766
                  artist.strStyles.c_str(),
1767
                  artist.strInstruments.c_str(),
1768
                  artist.strBiography.c_str(),
1769
                  artist.strDied.c_str(),
1770
                  artist.strDisbanded.c_str(),
1771
                  artist.strYearsActive.c_str(),
14378 by spiff_
added: ticket #4834 - scraped music fanart. uses http://www.htbackdrops.com - go add your favorite artist. thanks to nuzecast for making the backend available
1772
                  artist.thumbURL.m_xml.c_str(),
1773
                  artist.fanart.m_xml.c_str());
9868 by vulkanr
merge with trunk. rev. 12480.
1774
    m_pDS->exec(strSQL.c_str());
19283 by jmarshallnz
added: Improved abstraction of the database API, thanks to loopingz and firnsy
1775
    int idArtistInfo = (int)m_pDS->lastinsertid();
9868 by vulkanr
merge with trunk. rev. 12480.
1776
    for (unsigned int i=0;i<artist.discography.size();++i)
1777
    {
1778
      strSQL=FormatSQL("insert into discography (idArtist,strAlbum,strYear) values (%i,'%s','%s')",idArtist,artist.discography[i].first.c_str(),artist.discography[i].second.c_str());
1779
      m_pDS->exec(strSQL.c_str());
1780
    }
1781
1782
    return idArtistInfo;
1783
  }
1784
  catch (...)
1785
  {
1786
    CLog::Log(LOGERROR, "%s -  failed with query (%s)", __FUNCTION__, strSQL.c_str());
1787
  }
1788
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1789
1790
  return -1;
1591 by bobbin007
changed: album info: shows albumname too, when inexact matches are found
1791
}
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
1792
17467 by jmarshallnz
changed: long -> int in database functions.
1793
bool CMusicDatabase::SetAlbumInfoSongs(int idAlbumInfo, const VECSONGS& songs)
2303 by bobbin007
added: albuminfo, songs from allmusic.com are saved to database
1794
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1795
  CStdString strSQL;
1796
  try
1797
  {
1798
    if (NULL == m_pDB.get()) return false;
1799
    if (NULL == m_pDS.get()) return false;
1800
3740 by bobbin007
added: base class for database classes
1801
    strSQL=FormatSQL("delete from albuminfosong where idAlbumInfo=%i", idAlbumInfo);
11430 by jmarshallnz
fixed: Track info from allmusic.com weren't being added to the db.
1802
    m_pDS->exec(strSQL.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1803
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
1804
    for (int i = 0; i < (int)songs.size(); i++)
1805
    {
1806
      CSong song = songs[i];
1807
      strSQL=FormatSQL("insert into albuminfosong (idAlbumInfoSong,idAlbumInfo,iTrack,strTitle,iDuration) values(NULL,%i,%i,'%s',%i)",
1808
                    idAlbumInfo,
1809
                    song.iTrack,
1810
                    song.strTitle.c_str(),
1811
                    song.iDuration);
1812
      m_pDS->exec(strSQL.c_str());
1813
    }
1814
    return true;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1815
  }
1816
  catch (...)
1817
  {
8035 by yuvalt
Initial port to Linux/SDL
1818
    CLog::Log(LOGERROR, "%s failed with query (%s)", __FUNCTION__, strSQL.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1819
  }
2303 by bobbin007
added: albuminfo, songs from allmusic.com are saved to database
1820
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1821
  return false;
2303 by bobbin007
added: albuminfo, songs from allmusic.com are saved to database
1822
}
1823
2953 by kraqh3d
- 09-04-2005 added: directory tokens which reference XBMC's special directories for use as bookmark paths: $HOME, $SUBTITLES, $THUMBNAILS, $SHORTCUTS, $ALBUMS, $RECORDINGS, $SCREENSHOTS, $PLAYLISTS
1824
bool CMusicDatabase::CleanupSongsByIds(const CStdString &strSongIds)
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
1825
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1826
  try
1827
  {
2953 by kraqh3d
- 09-04-2005 added: directory tokens which reference XBMC's special directories for use as bookmark paths: $HOME, $SUBTITLES, $THUMBNAILS, $SHORTCUTS, $ALBUMS, $RECORDINGS, $SCREENSHOTS, $PLAYLISTS
1828
    if (NULL == m_pDB.get()) return false;
1829
    if (NULL == m_pDS.get()) return false;
1830
    // ok, now find all idSong's
3740 by bobbin007
added: base class for database classes
1831
    CStdString strSQL=FormatSQL("select * from song join path on song.idPath = path.idPath where song.idSong in %s", strSongIds.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1832
    if (!m_pDS->query(strSQL.c_str())) return false;
1833
    int iRowsFound = m_pDS->num_rows();
1834
    if (iRowsFound == 0)
2307 by charlydoes
fixed: some memory leaks in the database code.
1835
    {
1836
      m_pDS->close();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1837
      return true;
2307 by charlydoes
fixed: some memory leaks in the database code.
1838
    }
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1839
    CStdString strSongsToDelete = "(";
1840
    while (!m_pDS->eof())
1841
    { // get the full song path
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
1842
      CStdString strFileName;
1843
      CUtil::AddFileToFolder(m_pDS->fv("path.strPath").get_asString(), m_pDS->fv("song.strFileName").get_asString(), strFileName);
3314 by bobbin007
fixed: ogg streams got deleted from music database when reorg'ing even if the ogg file exists
1844
1845
      //  Special case for streams inside an ogg file. (oggstream)
7104 by darkdonno
changed: file cosmetics removed: trailing whitespace fixed: some TAB usage added: GPL Header
1846
      //  The last dir in the path is the ogg file that
3314 by bobbin007
fixed: ogg streams got deleted from music database when reorg'ing even if the ogg file exists
1847
      //  contains the stream, so test if its there
5190 by elupus
changed: made sure GetExtension checks for oddities like ? / \ in extension
1848
      CStdString strExtension=CUtil::GetExtension(strFileName);
4046 by spiff_
08-09-2005 added: NSF (Nintendo Sound File) codec to paplayer. Based on nosefart.
1849
      if (strExtension==".oggstream" || strExtension==".nsfstream")
3314 by bobbin007
fixed: ogg streams got deleted from music database when reorg'ing even if the ogg file exists
1850
      {
1851
        CStdString strFileAndPath=strFileName;
1852
        CUtil::GetDirectory(strFileAndPath, strFileName);
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
1853
        // we are dropping back to a file, so remove the slash at end
1854
        CUtil::RemoveSlashAtEnd(strFileName);
3314 by bobbin007
fixed: ogg streams got deleted from music database when reorg'ing even if the ogg file exists
1855
      }
7104 by darkdonno
changed: file cosmetics removed: trailing whitespace fixed: some TAB usage added: GPL Header
1856
3575 by bobbin007
changed: removed CUtil::FileExists() and replaced by CFile::Exists()
1857
      if (!CFile::Exists(strFileName))
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1858
      { // file no longer exists, so add to deletion list
1859
        strSongsToDelete += m_pDS->fv("song.idSong").get_asString() + ",";
1860
      }
1861
      m_pDS->next();
1862
    }
1863
    m_pDS->close();
1864
    strSongsToDelete.TrimRight(",");
1865
    strSongsToDelete += ")";
1866
    // ok, now delete these songs + all references to them from the exartistsong and exgenresong tables
1867
    strSQL = "delete from song where idSong in " + strSongsToDelete;
1868
    m_pDS->exec(strSQL.c_str());
1869
    strSQL = "delete from exartistsong where idSong in " + strSongsToDelete;
1870
    m_pDS->exec(strSQL.c_str());
1871
    strSQL = "delete from exgenresong where idSong in " + strSongsToDelete;
1872
    m_pDS->exec(strSQL.c_str());
13479 by gyunaev
Merging with karaoke branch.
1873
    strSQL = "delete from karaokedata where idSong in " + strSongsToDelete;
1874
    m_pDS->exec(strSQL.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1875
    m_pDS->close();
1876
    return true;
1877
  }
1878
  catch (...)
1879
  {
2953 by kraqh3d
- 09-04-2005 added: directory tokens which reference XBMC's special directories for use as bookmark paths: $HOME, $SUBTITLES, $THUMBNAILS, $SHORTCUTS, $ALBUMS, $RECORDINGS, $SCREENSHOTS, $PLAYLISTS
1880
    CLog::Log(LOGERROR, "Exception in CMusicDatabase::CleanupSongsFromPaths()");
1881
  }
1882
  return false;
1883
}
1884
1885
1886
bool CMusicDatabase::CleanupSongs()
1887
{
1888
  try
1889
  {
1890
    // run through all songs and get all unique path ids
1891
    int iLIMIT = 1000;
1892
    for (int i=0;;i+=iLIMIT)
1893
    {
3740 by bobbin007
added: base class for database classes
1894
      CStdString strSQL=FormatSQL("select song.idsong from song order by song.idsong limit %i offset %i",iLIMIT,i);
2953 by kraqh3d
- 09-04-2005 added: directory tokens which reference XBMC's special directories for use as bookmark paths: $HOME, $SUBTITLES, $THUMBNAILS, $SHORTCUTS, $ALBUMS, $RECORDINGS, $SCREENSHOTS, $PLAYLISTS
1895
      if (!m_pDS->query(strSQL.c_str())) return false;
1896
      int iRowsFound = m_pDS->num_rows();
1897
      // keep going until no rows are left!
1898
      if (iRowsFound == 0)
1899
      {
1900
        m_pDS->close();
1901
        return true;
1902
      }
7104 by darkdonno
changed: file cosmetics removed: trailing whitespace fixed: some TAB usage added: GPL Header
1903
      CStdString strSongIds = "(";
2953 by kraqh3d
- 09-04-2005 added: directory tokens which reference XBMC's special directories for use as bookmark paths: $HOME, $SUBTITLES, $THUMBNAILS, $SHORTCUTS, $ALBUMS, $RECORDINGS, $SCREENSHOTS, $PLAYLISTS
1904
      while (!m_pDS->eof())
1905
      {
1906
        strSongIds += m_pDS->fv("song.idSong").get_asString() + ",";
1907
        m_pDS->next();
1908
      }
1909
      m_pDS->close();
1910
      strSongIds.TrimRight(",");
1911
      strSongIds += ")";
1912
      CLog::Log(LOGDEBUG,"Checking songs from song ID list: %s",strSongIds.c_str());
1913
      if (!CleanupSongsByIds(strSongIds)) return false;
1914
    }
1915
    return true;
1916
  }
1917
  catch(...)
1918
  {
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1919
    CLog::Log(LOGERROR, "Exception in CMusicDatabase::CleanupSongs()");
1920
  }
1921
  return false;
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
1922
}
1923
1924
bool CMusicDatabase::CleanupAlbums()
1925
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1926
  try
1927
  {
1928
    // This must be run AFTER songs have been cleaned up
1929
    // delete albums with no reference to songs
8601 by vulkanr
merge with trunk. rev: 9636-10031.
1930
    CStdString strSQL = "select * from album where album.idAlbum not in (select idAlbum from song)";
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1931
    if (!m_pDS->query(strSQL.c_str())) return false;
1932
    int iRowsFound = m_pDS->num_rows();
1933
    if (iRowsFound == 0)
2307 by charlydoes
fixed: some memory leaks in the database code.
1934
    {
1935
      m_pDS->close();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1936
      return true;
1937
    }
1938
    CStdString strAlbumIds = "(";
1939
    while (!m_pDS->eof())
1940
    {
1941
      strAlbumIds += m_pDS->fv("album.idAlbum").get_asString() + ",";
1942
      m_pDS->next();
1943
    }
2307 by charlydoes
fixed: some memory leaks in the database code.
1944
    m_pDS->close();
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
1945
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1946
    strAlbumIds.TrimRight(",");
1947
    strAlbumIds += ")";
1948
    // ok, now we can delete them and the references in the exartistalbum, exgenrealbum and albuminfo tables
1949
    strSQL = "delete from album where idAlbum in " + strAlbumIds;
1950
    m_pDS->exec(strSQL.c_str());
1951
    strSQL = "delete from albuminfo where idAlbum in " + strAlbumIds;
1952
    m_pDS->exec(strSQL.c_str());
1953
    strSQL = "delete from exartistalbum where idAlbum in " + strAlbumIds;
1954
    m_pDS->exec(strSQL.c_str());
1955
    strSQL = "delete from exgenrealbum where idAlbum in " + strAlbumIds;
1956
    m_pDS->exec(strSQL.c_str());
1957
    return true;
1958
  }
1959
  catch (...)
1960
  {
1961
    CLog::Log(LOGERROR, "Exception in CMusicDatabase::CleanupAlbums()");
1962
  }
1963
  return false;
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
1964
}
1965
1966
bool CMusicDatabase::CleanupPaths()
1967
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
1968
  try
1969
  {
1970
    // needs to be done AFTER the songs and albums have been cleaned up.
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
1971
    // we can happily delete any path that has no reference to a song
1972
    // but we must keep all paths that have been scanned that may contain songs in subpaths
14836 by jmarshallnz
fixed: Ticket #6348 - MusicDB Error Clean library.
1973
1974
    // first create a temporary table of song paths
20929 by jmarshallnz
changed: Indexed columns in the database should be varchar rather than text so that the db layout is more portable. Thanks to firnsy (part of #8169)
1975
    m_pDS->exec("CREATE TEMPORARY TABLE songpaths (idPath integer, strPath varchar(512))\n");
14836 by jmarshallnz
fixed: Ticket #6348 - MusicDB Error Clean library.
1976
    m_pDS->exec("INSERT INTO songpaths select idPath,strPath from path where idPath in (select idPath from song)\n");
1977
1978
    // grab all paths that aren't immediately connected with a song
1979
    CStdString sql = "select * from path where idPath not in (select idPath from song)";
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
1980
    if (!m_pDS->query(sql.c_str())) return false;
1981
    int iRowsFound = m_pDS->num_rows();
1982
    if (iRowsFound == 0)
1983
    {
1984
      m_pDS->close();
1985
      return true;
1986
    }
14836 by jmarshallnz
fixed: Ticket #6348 - MusicDB Error Clean library.
1987
    // and construct a list to delete
1988
    CStdString deleteSQL = "delete from path where idPath in (";
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
1989
    while (!m_pDS->eof())
1990
    {
14836 by jmarshallnz
fixed: Ticket #6348 - MusicDB Error Clean library.
1991
      // anything that isn't a parent path of a song path is to be deleted
1992
      CStdString sql = FormatSQL("select count(idPath) from songpaths where strPath like '%s%%'", m_pDS->fv("strPath").get_asString().c_str());
17467 by jmarshallnz
changed: long -> int in database functions.
1993
      if (m_pDS2->query(sql.c_str()) && m_pDS2->num_rows() == 1 && m_pDS2->fv(0).get_asInt() == 0)
1994
        deleteSQL += FormatSQL("%i,", m_pDS->fv("idPath").get_asInt()); // nothing found, so delete
14836 by jmarshallnz
fixed: Ticket #6348 - MusicDB Error Clean library.
1995
      m_pDS2->close();
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
1996
      m_pDS->next();
1997
    }
1998
    m_pDS->close();
14836 by jmarshallnz
fixed: Ticket #6348 - MusicDB Error Clean library.
1999
    deleteSQL.TrimRight(',');
2000
    deleteSQL += ")";
2001
    // do the deletion, and drop our temp table
2002
    m_pDS->exec(deleteSQL.c_str());
2003
    m_pDS->exec("drop table songpaths");
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2004
    return true;
2005
  }
2006
  catch (...)
2007
  {
2008
    CLog::Log(LOGERROR, "Exception in CMusicDatabase::CleanupPaths() or was aborted");
2009
  }
2010
  return false;
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
2011
}
2012
2745 by jmarshallnz
- 04-03-2005 added: Thumbs table clean up on cleanup of the music library.
2013
bool CMusicDatabase::CleanupThumbs()
2014
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2015
  try
2016
  {
2017
    // needs to be done AFTER the songs have been cleaned up.
2018
    // we can happily delete any thumb that has no reference to a song
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2019
    CStdString strSQL = "select * from thumb where idThumb not in (select idThumb from song) and idThumb not in (select idThumb from album)";
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2020
    if (!m_pDS->query(strSQL.c_str())) return false;
2021
    int iRowsFound = m_pDS->num_rows();
2022
    if (iRowsFound == 0)
2023
    {
2024
      m_pDS->close();
2745 by jmarshallnz
- 04-03-2005 added: Thumbs table clean up on cleanup of the music library.
2025
      return true;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2026
    }
2745 by jmarshallnz
- 04-03-2005 added: Thumbs table clean up on cleanup of the music library.
2027
    // get albums dir
5460 by jmarshallnz
- 04-05-2006 changed: Removed <logpath> tag. By default it logs in the XBMC folder, unless it's running off DVD, where it logs in TDATA. UserData is also moved to TDATA if running off DVD.
2028
    CStdString strThumbsDir = g_settings.GetMusicThumbFolder();
2745 by jmarshallnz
- 04-03-2005 added: Thumbs table clean up on cleanup of the music library.
2029
    while (!m_pDS->eof())
2030
    {
2031
      CStdString strThumb = m_pDS->fv("strThumb").get_asString();
2032
      if (strThumb.Left(strThumbsDir.size()) == strThumbsDir)
2033
      { // only delete cached thumbs
21879 by jmarshallnz
fixed: Only delete cached thumbs willy-nilly.
2034
        CTextureCache::Get().ClearCachedImage(strThumb, true);
2745 by jmarshallnz
- 04-03-2005 added: Thumbs table clean up on cleanup of the music library.
2035
      }
2036
      m_pDS->next();
2037
    }
2927 by kraqh3d
- 04-04-2005 fixed: re-scanning music dirs and music thumbnail caching
2038
    // clear the thumb cache
2934 by kraqh3d
- 05-04-2005 removed: My Music Albums, Artists, and Genres views. Use My Music Library instead.
2039
    //CUtil::ThumbCacheClear();
2040
    //g_directoryCache.ClearMusicThumbCache();
2745 by jmarshallnz
- 04-03-2005 added: Thumbs table clean up on cleanup of the music library.
2041
    // now we can delete
2042
    m_pDS->close();
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2043
    strSQL = "delete from thumb where idThumb not in (select idThumb from song) and idThumb not in (select idThumb from album)";
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2044
    m_pDS->exec(strSQL.c_str());
2045
    return true;
2046
  }
2047
  catch (...)
2048
  {
2049
    CLog::Log(LOGERROR, "Exception in CMusicDatabase::CleanupThumbs() or was aborted");
2050
  }
2051
  return false;
2745 by jmarshallnz
- 04-03-2005 added: Thumbs table clean up on cleanup of the music library.
2052
}
2053
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
2054
bool CMusicDatabase::CleanupArtists()
2055
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2056
  try
2057
  {
2058
    // (nested queries by Bobbin007)
2059
    // must be executed AFTER the song, exartistsong, album and exartistalbum tables are cleaned.
2060
    // don't delete the "Various Artists" string
2061
    CStdString strVariousArtists = g_localizeStrings.Get(340);
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2062
    int idVariousArtists = AddArtist(strVariousArtists);
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2063
    CStdString strSQL = "delete from artist where idArtist not in (select idArtist from song)";
2064
    strSQL += " and idArtist not in (select idArtist from exartistsong)";
2065
    strSQL += " and idArtist not in (select idArtist from album)";
2066
    strSQL += " and idArtist not in (select idArtist from exartistalbum)";
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2067
    CStdString strSQL2;
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2068
    strSQL2.Format(" and idArtist<>%i", idVariousArtists);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2069
    strSQL += strSQL2;
2070
    m_pDS->exec(strSQL.c_str());
18495 by spiff_
fixed: clean artist info when removing an artist
2071
    m_pDS->exec("delete from artistinfo where idArtist not in (select idArtist from artist)");
2072
    m_pDS->exec("delete from discography where idArtist not in (select idArtist from artist)");
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2073
    return true;
2074
  }
2075
  catch (...)
2076
  {
2077
    CLog::Log(LOGERROR, "Exception in CMusicDatabase::CleanupArtists() or was aborted");
2078
  }
2079
  return false;
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
2080
}
2081
2082
bool CMusicDatabase::CleanupGenres()
2083
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2084
  try
2085
  {
2086
    // Cleanup orphaned genres (ie those that don't belong to a song or an albuminfo entry)
2087
    // (nested queries by Bobbin007)
2088
    // Must be executed AFTER the song, exgenresong, albuminfo and exgenrealbum tables have been cleaned.
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2089
    CStdString strSQL = "delete from genre where idGenre not in (select idGenre from song) and";
2090
    strSQL += " idGenre not in (select idGenre from exgenresong) and";
2091
    strSQL += " idGenre not in (select idGenre from albuminfo) and";
2092
    strSQL += " idGenre not in (select idGenre from exgenrealbum)";
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2093
    m_pDS->exec(strSQL.c_str());
2094
    return true;
2095
  }
2096
  catch (...)
2097
  {
2098
    CLog::Log(LOGERROR, "Exception in CMusicDatabase::CleanupGenres() or was aborted");
2099
  }
2100
  return false;
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
2101
}
2102
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
2103
bool CMusicDatabase::CleanupOrphanedItems()
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
2104
{
8002 by jmarshallnz
fixed: More reliable tracking of scanned paths in music library.
2105
  // paths aren't cleaned up here - they're cleaned up in RemoveSongsFromPath()
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2106
  if (NULL == m_pDB.get()) return false;
2107
  if (NULL == m_pDS.get()) return false;
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
2108
  if (!CleanupAlbums()) return false;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2109
  if (!CleanupArtists()) return false;
2110
  if (!CleanupGenres()) return false;
2745 by jmarshallnz
- 04-03-2005 added: Thumbs table clean up on cleanup of the music library.
2111
  if (!CleanupThumbs()) return false;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2112
  return true;
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
2113
}
2114
2115
int CMusicDatabase::Cleanup(CGUIDialogProgress *pDlgProgress)
2116
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2117
  if (NULL == m_pDB.get()) return ERROR_DATABASE;
2118
  if (NULL == m_pDS.get()) return ERROR_DATABASE;
2119
  // first cleanup any songs with invalid paths
4573 by bobbin007
changed: more media window base class work (moved some common msgs, consolidated OnClick)
2120
  pDlgProgress->SetHeading(700);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2121
  pDlgProgress->SetLine(0, "");
2122
  pDlgProgress->SetLine(1, 318);
2123
  pDlgProgress->SetLine(2, 330);
2124
  pDlgProgress->SetPercentage(0);
5590 by jmarshallnz
- 25-05-2006 fixed: DoModal() and Show() on dialogs had to be called with the parent window, even though the information was never used.
2125
  pDlgProgress->StartModal();
4573 by bobbin007
changed: more media window base class work (moved some common msgs, consolidated OnClick)
2126
  pDlgProgress->ShowProgressBar(true);
2127
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2128
  if (!CleanupSongs())
2129
  {
2130
    RollbackTransaction();
2131
    return ERROR_REORG_SONGS;
2132
  }
2133
  // then the albums that are not linked to a song or to albuminfo, or whose path is removed
2134
  pDlgProgress->SetLine(1, 326);
2135
  pDlgProgress->SetPercentage(20);
2136
  pDlgProgress->Progress();
2137
  if (!CleanupAlbums())
2138
  {
2139
    RollbackTransaction();
2140
    return ERROR_REORG_ALBUM;
2141
  }
2142
  // now the paths
2143
  pDlgProgress->SetLine(1, 324);
2144
  pDlgProgress->SetPercentage(40);
2145
  pDlgProgress->Progress();
2146
  if (!CleanupPaths() || !CleanupThumbs())
2147
  {
2148
    RollbackTransaction();
2149
    return ERROR_REORG_PATH;
2150
  }
2151
  // and finally artists + genres
2152
  pDlgProgress->SetLine(1, 320);
2153
  pDlgProgress->SetPercentage(60);
2154
  pDlgProgress->Progress();
2155
  if (!CleanupArtists())
2156
  {
2157
    RollbackTransaction();
2158
    return ERROR_REORG_ARTIST;
2159
  }
2160
  pDlgProgress->SetLine(1, 322);
2161
  pDlgProgress->SetPercentage(80);
2162
  pDlgProgress->Progress();
2163
  if (!CleanupGenres())
2164
  {
2165
    RollbackTransaction();
2166
    return ERROR_REORG_GENRE;
2167
  }
2168
  // commit transaction
2169
  pDlgProgress->SetLine(1, 328);
2170
  pDlgProgress->SetPercentage(90);
2171
  pDlgProgress->Progress();
2172
  if (!CommitTransaction())
2173
  {
2174
    RollbackTransaction();
2175
    return ERROR_WRITING_CHANGES;
2176
  }
2177
  // and compress the database
2178
  pDlgProgress->SetLine(1, 331);
2179
  pDlgProgress->SetPercentage(100);
2180
  pDlgProgress->Progress();
10386 by vulkanr
merge with trunk (last one??). revision 13574.
2181
  if (!Compress(false))
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2182
  {
2183
    return ERROR_COMPRESSING;
2184
  }
2185
  return ERROR_OK;
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
2186
}
2187
2188
void CMusicDatabase::DeleteAlbumInfo()
2189
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2190
  // open our database
2191
  Open();
2192
  if (NULL == m_pDB.get()) return ;
2193
  if (NULL == m_pDS.get()) return ;
2194
2195
  // If we are scanning for music info in the background,
2196
  // other writing access to the database is prohibited.
17685 by jmarshallnz
cosmetic: m_gWindowManager -> g_windowManager for consistency.
2197
  CGUIDialogMusicScan* dlgMusicScan = (CGUIDialogMusicScan*)g_windowManager.GetWindow(WINDOW_DIALOG_MUSIC_SCAN);
7878 by jmarshallnz
Fixed: Python dialogs using windowxml would randomly hide.
2198
  if (dlgMusicScan->IsDialogRunning())
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2199
  {
3746 by jmarshallnz
- 26-07-2005 changed: Skin files are loaded on demand rather than on boot.
2200
    CGUIDialogOK::ShowAndGetInput(189, 14057, 0, 0);
2201
    return;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2202
  }
2203
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
2204
  CStdString strSQL="select * from albuminfo,album,artist where and albuminfo.idAlbum=album.idAlbum and album.idArtist=artist.idArtist order by album.strAlbum";
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2205
  if (!m_pDS->query(strSQL.c_str())) return ;
2206
  int iRowsFound = m_pDS->num_rows();
2207
  if (iRowsFound == 0)
2208
  {
2307 by charlydoes
fixed: some memory leaks in the database code.
2209
    m_pDS->close();
3746 by jmarshallnz
- 26-07-2005 changed: Skin files are loaded on demand rather than on boot.
2210
    CGUIDialogOK::ShowAndGetInput(313, 425, 0, 0);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2211
  }
2212
  vector<CAlbumCache> vecAlbums;
2213
  while (!m_pDS->eof())
2214
  {
2215
    CAlbumCache album;
17467 by jmarshallnz
changed: long -> int in database functions.
2216
    album.idAlbum = m_pDS->fv("album.idAlbum").get_asInt() ;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2217
    album.strAlbum = m_pDS->fv("album.strAlbum").get_asString();
2218
    album.strArtist = m_pDS->fv("artist.strArtist").get_asString();
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
2219
    album.strArtist += m_pDS->fv("album.strExtraArtists").get_asString();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2220
    vecAlbums.push_back(album);
2221
    m_pDS->next();
2222
  }
2307 by charlydoes
fixed: some memory leaks in the database code.
2223
  m_pDS->close();
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
2224
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2225
  // Show a selectdialog that the user can select the albuminfo to delete
17685 by jmarshallnz
cosmetic: m_gWindowManager -> g_windowManager for consistency.
2226
  CGUIDialogSelect *pDlg = (CGUIDialogSelect*)g_windowManager.GetWindow(WINDOW_DIALOG_SELECT);
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
2227
  if (pDlg)
2228
  {
4985 by jmarshallnz
- 14-03-2006 changed: All strings/labels are internally UTF-8 now. Will almost certainly require rescanning the databases if you use a non-Ascii charset.
2229
    pDlg->SetHeading(g_localizeStrings.Get(181).c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2230
    pDlg->Reset();
2231
    for (int i = 0; i < (int)vecAlbums.size(); ++i)
2232
    {
2233
      CMusicDatabase::CAlbumCache& album = vecAlbums[i];
2234
      pDlg->Add(album.strAlbum + " - " + album.strArtist);
2235
    }
5590 by jmarshallnz
- 25-05-2006 fixed: DoModal() and Show() on dialogs had to be called with the parent window, even though the information was never used.
2236
    pDlg->DoModal();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2237
2238
    // and wait till user selects one
2239
    int iSelectedAlbum = pDlg->GetSelectedLabel();
2240
    if (iSelectedAlbum < 0)
2241
    {
2242
      vecAlbums.erase(vecAlbums.begin(), vecAlbums.end());
2243
      return ;
2244
    }
2245
2246
    CAlbumCache& album = vecAlbums[iSelectedAlbum];
3740 by bobbin007
added: base class for database classes
2247
    strSQL=FormatSQL("delete from albuminfo where albuminfo.idAlbum=%i", album.idAlbum);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2248
    if (!m_pDS->exec(strSQL.c_str())) return ;
2249
2250
    vecAlbums.erase(vecAlbums.begin(), vecAlbums.end());
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
2251
  }
2252
}
2253
3639 by bobbin007
changed: reorg of music settings
2254
bool CMusicDatabase::LookupCDDBInfo(bool bRequery/*=false*/)
2255
{
19798 by spiff_
cosmetics
2256
#ifdef HAS_DVD_DRIVE
18634 by jmarshallnz
changed: Sanitized the music settings.
2257
  if (!g_guiSettings.GetBool("audiocds.usecddb"))
11407 by jmarshallnz
fixed: Don't attempt CDDB lookup if no network is connected (Ticket #3151).
2258
    return false;
2259
2260
  // check network connectivity
18225 by spiff_
changed: remove enable internet lookup setting - always enabled
2261
  if (!g_application.getNetwork().IsAvailable())
3639 by bobbin007
changed: reorg of music settings
2262
    return false;
2263
2264
  // Get information for the inserted disc
16402 by wiso
replaced CDetectDVDMedia::GetCdInfo() by g_mediaManager.GetCdInfo()
2265
  CCdInfo* pCdInfo = g_mediaManager.GetCdInfo();
3639 by bobbin007
changed: reorg of music settings
2266
  if (pCdInfo == NULL)
2267
    return false;
2268
2269
  // If the disc has no tracks, we are finished here.
2270
  int nTracks = pCdInfo->GetTrackCount();
2271
  if (nTracks <= 0)
2272
    return false;
2273
2274
  //  Delete old info if any
2275
  if (bRequery)
2276
  {
2277
    CStdString strFile;
13629 by jmarshallnz
merged: Changes from the pathcleanup branch. Special Note: Some stored paths (such as in favourites.xml, or in profiles.xml) may no longer be valid, so may need recreating.
2278
    strFile.Format("%x.cddb", pCdInfo->GetCddbDiscId());
2279
    CFile::Delete(CUtil::AddFileToFolder(g_settings.GetCDDBFolder(), strFile));
3639 by bobbin007
changed: reorg of music settings
2280
  }
2281
2282
  // Prepare cddb
2283
  Xcddb cddb;
5460 by jmarshallnz
- 04-05-2006 changed: Removed <logpath> tag. By default it logs in the XBMC folder, unless it's running off DVD, where it logs in TDATA. UserData is also moved to TDATA if running off DVD.
2284
  cddb.setCacheDir(g_settings.GetCDDBFolder());
3639 by bobbin007
changed: reorg of music settings
2285
2286
  // Do we have to look for cddb information
2287
  if (pCdInfo->HasCDDBInfo() && !cddb.isCDCached(pCdInfo))
2288
  {
17685 by jmarshallnz
cosmetic: m_gWindowManager -> g_windowManager for consistency.
2289
    CGUIDialogProgress* pDialogProgress = (CGUIDialogProgress*)g_windowManager.GetWindow(WINDOW_DIALOG_PROGRESS);
2290
    CGUIDialogSelect *pDlgSelect = (CGUIDialogSelect*)g_windowManager.GetWindow(WINDOW_DIALOG_SELECT);
3639 by bobbin007
changed: reorg of music settings
2291
2292
    if (!pDialogProgress) return false;
2293
    if (!pDlgSelect) return false;
2294
2295
    // Show progress dialog if we have to connect to freedb.org
2296
    pDialogProgress->SetHeading(255); //CDDB
2297
    pDialogProgress->SetLine(0, ""); // Querying freedb for CDDB info
2298
    pDialogProgress->SetLine(1, 256);
2299
    pDialogProgress->SetLine(2, "");
4020 by spiff_
03-09-2005 added: sf.net patch [ 1257055 ] - instable behaivour in cddb.cpp - thanks to pade2!
2300
    pDialogProgress->ShowProgressBar(false);
5590 by jmarshallnz
- 25-05-2006 fixed: DoModal() and Show() on dialogs had to be called with the parent window, even though the information was never used.
2301
    pDialogProgress->StartModal();
3639 by bobbin007
changed: reorg of music settings
2302
2303
    // get cddb information
2304
    if (!cddb.queryCDinfo(pCdInfo))
2305
    {
2306
      pDialogProgress->Close();
2307
      int lasterror = cddb.getLastError();
2308
2309
      // Have we found more then on match in cddb for this disc,...
2310
      if (lasterror == E_WAIT_FOR_INPUT)
2311
      {
2312
        // ...yes, show the matches found in a select dialog
2313
        // and let the user choose an entry.
2314
        pDlgSelect->Reset();
2315
        pDlgSelect->SetHeading(255);
2316
        int i = 1;
2317
        while (1)
2318
        {
2319
          CStdString strTitle = cddb.getInexactTitle(i);
2320
          if (strTitle == "") break;
2321
2322
          CStdString strArtist = cddb.getInexactArtist(i);
2323
          if (!strArtist.IsEmpty())
2324
            strTitle += " - " + strArtist;
2325
2326
          pDlgSelect->Add(strTitle);
2327
          i++;
2328
        }
5590 by jmarshallnz
- 25-05-2006 fixed: DoModal() and Show() on dialogs had to be called with the parent window, even though the information was never used.
2329
        pDlgSelect->DoModal();
3639 by bobbin007
changed: reorg of music settings
2330
2331
        // Has the user selected a match...
2332
        int iSelectedCD = pDlgSelect->GetSelectedLabel();
2333
        if (iSelectedCD >= 0)
2334
        {
2335
          // ...query cddb for the inexact match
2336
          if (!cddb.queryCDinfo(pCdInfo, 1 + iSelectedCD))
2337
            pCdInfo->SetNoCDDBInfo();
2338
        }
2339
        else
2340
          pCdInfo->SetNoCDDBInfo();
2341
2342
        pDialogProgress->Close();
2343
      }
4020 by spiff_
03-09-2005 added: sf.net patch [ 1257055 ] - instable behaivour in cddb.cpp - thanks to pade2!
2344
      else if (lasterror == E_NO_MATCH_FOUND)
2345
      {
2346
        pCdInfo->SetNoCDDBInfo();
2347
        pDialogProgress->Close();
2348
      }
3639 by bobbin007
changed: reorg of music settings
2349
      else
2350
      {
2351
        pCdInfo->SetNoCDDBInfo();
2352
        pDialogProgress->Close();
2353
        // ..no, an error occured, display it to the user
17685 by jmarshallnz
cosmetic: m_gWindowManager -> g_windowManager for consistency.
2354
        CGUIDialogOK *pDialogOK = (CGUIDialogOK *)g_windowManager.GetWindow(WINDOW_DIALOG_OK);
3746 by jmarshallnz
- 26-07-2005 changed: Skin files are loaded on demand rather than on boot.
2355
        if (pDialogOK)
2356
        {
4020 by spiff_
03-09-2005 added: sf.net patch [ 1257055 ] - instable behaivour in cddb.cpp - thanks to pade2!
2357
          CStdString strErrorText;
2358
          strErrorText.Format("[%d] %s", cddb.getLastError(), cddb.getLastErrorText());
2359
3746 by jmarshallnz
- 26-07-2005 changed: Skin files are loaded on demand rather than on boot.
2360
          pDialogOK->SetHeading(255);
2361
          pDialogOK->SetLine(0, 257); //ERROR
4020 by spiff_
03-09-2005 added: sf.net patch [ 1257055 ] - instable behaivour in cddb.cpp - thanks to pade2!
2362
          pDialogOK->SetLine(1, strErrorText.c_str() );
3746 by jmarshallnz
- 26-07-2005 changed: Skin files are loaded on demand rather than on boot.
2363
          pDialogOK->SetLine(2, "");
5590 by jmarshallnz
- 25-05-2006 fixed: DoModal() and Show() on dialogs had to be called with the parent window, even though the information was never used.
2364
          pDialogOK->DoModal();
3746 by jmarshallnz
- 26-07-2005 changed: Skin files are loaded on demand rather than on boot.
2365
        }
3639 by bobbin007
changed: reorg of music settings
2366
      }
2367
    } // if ( !cddb.queryCDinfo( pCdInfo ) )
2368
    pDialogProgress->Close();
19482 by spiff_
changed: get rid of the useless stSettings struct
2369
  } // if (pCdInfo->HasCDDBInfo() && g_settings.m_bUseCDDB)
3639 by bobbin007
changed: reorg of music settings
2370
2371
  // Filling the file items with cddb info happens in CMusicInfoTagLoaderCDDA
2372
2373
  return pCdInfo->HasCDDBInfo();
17019 by yuvalt
honor HAS_DVD_DRIVE
2374
#else
2375
  return false;
19798 by spiff_
cosmetics
2376
#endif
3639 by bobbin007
changed: reorg of music settings
2377
}
2378
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
2379
void CMusicDatabase::DeleteCDDBInfo()
2380
{
19798 by spiff_
cosmetics
2381
#ifdef HAS_DVD_DRIVE
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2382
  WIN32_FIND_DATA wfd;
2383
  memset(&wfd, 0, sizeof(wfd));
2384
13629 by jmarshallnz
merged: Changes from the pathcleanup branch. Special Note: Some stored paths (such as in favourites.xml, or in profiles.xml) may no longer be valid, so may need recreating.
2385
  CStdString strCDDBFileMask = CUtil::AddFileToFolder(g_settings.GetCDDBFolder(), "*.cddb");
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2386
2387
  map<ULONG, CStdString> mapCDDBIds;
2388
13629 by jmarshallnz
merged: Changes from the pathcleanup branch. Special Note: Some stored paths (such as in favourites.xml, or in profiles.xml) may no longer be valid, so may need recreating.
2389
  CAutoPtrFind hFind( FindFirstFile(_P(strCDDBFileMask), &wfd));
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2390
  if (!hFind.isValid())
2391
  {
3746 by jmarshallnz
- 26-07-2005 changed: Skin files are loaded on demand rather than on boot.
2392
    CGUIDialogOK::ShowAndGetInput(313, 426, 0, 0);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2393
    return ;
2394
  }
2395
2396
  // Show a selectdialog that the user can select the albuminfo to delete
17685 by jmarshallnz
cosmetic: m_gWindowManager -> g_windowManager for consistency.
2397
  CGUIDialogSelect *pDlg = (CGUIDialogSelect*)g_windowManager.GetWindow(WINDOW_DIALOG_SELECT);
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
2398
  if (pDlg)
2399
  {
4985 by jmarshallnz
- 14-03-2006 changed: All strings/labels are internally UTF-8 now. Will almost certainly require rescanning the databases if you use a non-Ascii charset.
2400
    pDlg->SetHeading(g_localizeStrings.Get(181).c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2401
    pDlg->Reset();
2402
    do
2403
    {
2404
      if ( !(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
2405
      {
2406
        CStdString strFile = wfd.cFileName;
2407
        strFile.Delete(strFile.size() - 5, 5);
2408
        ULONG lDiscId = strtoul(strFile.c_str(), NULL, 16);
2409
        Xcddb cddb;
13629 by jmarshallnz
merged: Changes from the pathcleanup branch. Special Note: Some stored paths (such as in favourites.xml, or in profiles.xml) may no longer be valid, so may need recreating.
2410
        cddb.setCacheDir(g_settings.GetCDDBFolder());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2411
2412
        if (!cddb.queryCache(lDiscId))
2413
          continue;
2414
2415
        CStdString strDiskTitle, strDiskArtist;
2416
        cddb.getDiskTitle(strDiskTitle);
2417
        cddb.getDiskArtist(strDiskArtist);
2418
2419
        CStdString str;
2420
        if (strDiskArtist.IsEmpty())
2421
          str = strDiskTitle;
2422
        else
2423
          str = strDiskTitle + " - " + strDiskArtist;
2424
2425
        pDlg->Add(str);
2426
        mapCDDBIds.insert(pair<ULONG, CStdString>(lDiscId, str));
2427
      }
2428
    }
2429
    while (FindNextFile(hFind, &wfd));
2430
2431
    pDlg->Sort();
5590 by jmarshallnz
- 25-05-2006 fixed: DoModal() and Show() on dialogs had to be called with the parent window, even though the information was never used.
2432
    pDlg->DoModal();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2433
2434
    // and wait till user selects one
2435
    int iSelectedAlbum = pDlg->GetSelectedLabel();
2436
    if (iSelectedAlbum < 0)
2437
    {
2438
      mapCDDBIds.erase(mapCDDBIds.begin(), mapCDDBIds.end());
2439
      return ;
2440
    }
2441
2442
    CStdString strSelectedAlbum = pDlg->GetSelectedLabelText();
2443
    map<ULONG, CStdString>::iterator it;
2444
    for (it = mapCDDBIds.begin();it != mapCDDBIds.end();it++)
2445
    {
2446
      if (it->second == strSelectedAlbum)
2447
      {
2448
        CStdString strFile;
13629 by jmarshallnz
merged: Changes from the pathcleanup branch. Special Note: Some stored paths (such as in favourites.xml, or in profiles.xml) may no longer be valid, so may need recreating.
2449
        strFile.Format("%x.cddb", it->first);
2450
        CFile::Delete(CUtil::AddFileToFolder(g_settings.GetCDDBFolder(), strFile));
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2451
        break;
2452
      }
2453
    }
2454
    mapCDDBIds.erase(mapCDDBIds.begin(), mapCDDBIds.end());
2455
  }
19798 by spiff_
cosmetics
2456
#endif
1837 by jmarshallnz
- 20-08-2004 changed: mplayers SeekTime() now is in ms for better seek accuracy for .cue files etc.
2457
}
2458
2331 by jmarshallnz
02-12-2004: New settings system. !!! BREAKS EVERY SKIN KNOWN TO MAN !!!
2459
void CMusicDatabase::Clean()
2460
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2461
  // If we are scanning for music info in the background,
2462
  // other writing access to the database is prohibited.
17685 by jmarshallnz
cosmetic: m_gWindowManager -> g_windowManager for consistency.
2463
  CGUIDialogMusicScan* dlgMusicScan = (CGUIDialogMusicScan*)g_windowManager.GetWindow(WINDOW_DIALOG_MUSIC_SCAN);
7878 by jmarshallnz
Fixed: Python dialogs using windowxml would randomly hide.
2464
  if (dlgMusicScan->IsDialogRunning())
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2465
  {
3746 by jmarshallnz
- 26-07-2005 changed: Skin files are loaded on demand rather than on boot.
2466
    CGUIDialogOK::ShowAndGetInput(189, 14057, 0, 0);
2467
    return;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2468
  }
2469
3746 by jmarshallnz
- 26-07-2005 changed: Skin files are loaded on demand rather than on boot.
2470
  if (CGUIDialogYesNo::ShowAndGetInput(313, 333, 0, 0))
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2471
  {
17685 by jmarshallnz
cosmetic: m_gWindowManager -> g_windowManager for consistency.
2472
    CGUIDialogProgress* dlgProgress = (CGUIDialogProgress*)g_windowManager.GetWindow(WINDOW_DIALOG_PROGRESS);
3746 by jmarshallnz
- 26-07-2005 changed: Skin files are loaded on demand rather than on boot.
2473
    if (dlgProgress)
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2474
    {
4789 by bobbin007
changed: removed the global instance of the musicdatabase, reason: sqlite is thread save but it's not save to share the same connection across thread
2475
      CMusicDatabase musicdatabase;
2476
      if (musicdatabase.Open())
2477
      {
2478
        int iReturnString = musicdatabase.Cleanup(dlgProgress);
2479
        musicdatabase.Close();
3746 by jmarshallnz
- 26-07-2005 changed: Skin files are loaded on demand rather than on boot.
2480
4789 by bobbin007
changed: removed the global instance of the musicdatabase, reason: sqlite is thread save but it's not save to share the same connection across thread
2481
        if (iReturnString != ERROR_OK)
2482
        {
2483
          CGUIDialogOK::ShowAndGetInput(313, iReturnString, 0, 0);
2484
        }
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2485
      }
3746 by jmarshallnz
- 26-07-2005 changed: Skin files are loaded on demand rather than on boot.
2486
      dlgProgress->Close();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2487
    }
2488
  }
2331 by jmarshallnz
02-12-2004: New settings system. !!! BREAKS EVERY SKIN KNOWN TO MAN !!!
2489
}
2690 by kraqh3d
Initial commit for new music Nav window (does not support multi artist/genres yet!)
2490
4458 by bobbin007
*** empty log message ***
2491
bool CMusicDatabase::GetGenresNav(const CStdString& strBaseDir, CFileItemList& items)
2835 by kraqh3d
fixed: my music nav window showing false items due to allmusic.com lookups
2492
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2493
  try
2494
  {
2495
    if (NULL == m_pDB.get()) return false;
2496
    if (NULL == m_pDS.get()) return false;
2835 by kraqh3d
fixed: my music nav window showing false items due to allmusic.com lookups
2497
2498
    // get primary genres for songs
4458 by bobbin007
*** empty log message ***
2499
    CStdString strSQL="select * from genre "
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
2500
                      "where (idGenre IN ("
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2501
                        "select song.idGenre from song) "
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
2502
                      "or idGenre IN ("
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2503
                        "select exgenresong.idGenre from exgenresong)) ";
2835 by kraqh3d
fixed: my music nav window showing false items due to allmusic.com lookups
2504
4457 by kraqh3d
- 22-12-2005 changed: My Music Library would incorrectly handle artists and albums that were blank as if they were the "all" item. These items are now filtered out of the display.
2505
    // block null strings
2506
    strSQL += " and genre.strGenre != \"\"";
2507
2835 by kraqh3d
fixed: my music nav window showing false items due to allmusic.com lookups
2508
    // run query
8035 by yuvalt
Initial port to Linux/SDL
2509
    CLog::Log(LOGDEBUG, "%s query: %s", __FUNCTION__, strSQL.c_str());
2835 by kraqh3d
fixed: my music nav window showing false items due to allmusic.com lookups
2510
    if (!m_pDS->query(strSQL.c_str())) return false;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2511
    int iRowsFound = m_pDS->num_rows();
2512
    if (iRowsFound == 0)
2835 by kraqh3d
fixed: my music nav window showing false items due to allmusic.com lookups
2513
    {
2514
      m_pDS->close();
2515
      return false;
2516
    }
2517
2518
    // get data from returned rows
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2519
    while (!m_pDS->eof())
2520
    {
10487 by jmarshallnz
changed: Use boost::shared_ptr in CFileItemLists to reduce the
2521
      CFileItemPtr pItem(new CFileItem(m_pDS->fv("strGenre").get_asString()));
7609 by spiff_
added: video info tag to cfileitem
2522
      pItem->GetMusicInfoTag()->SetGenre(m_pDS->fv("strGenre").get_asString());
4458 by bobbin007
*** empty log message ***
2523
      CStdString strDir;
17467 by jmarshallnz
changed: long -> int in database functions.
2524
      strDir.Format("%ld/", m_pDS->fv("idGenre").get_asInt());
4458 by bobbin007
*** empty log message ***
2525
      pItem->m_strPath=strBaseDir + strDir;
2526
      pItem->m_bIsFolder=true;
2527
      items.Add(pItem);
2528
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2529
      m_pDS->next();
2530
    }
2835 by kraqh3d
fixed: my music nav window showing false items due to allmusic.com lookups
2531
2532
    // cleanup
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2533
    m_pDS->close();
2835 by kraqh3d
fixed: my music nav window showing false items due to allmusic.com lookups
2534
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2535
    return true;
2536
  }
2537
  catch (...)
2538
  {
8035 by yuvalt
Initial port to Linux/SDL
2539
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2540
  }
2541
  return false;
2835 by kraqh3d
fixed: my music nav window showing false items due to allmusic.com lookups
2542
}
2543
7949 by jmarshallnz
added: Year node to the music library.
2544
bool CMusicDatabase::GetYearsNav(const CStdString& strBaseDir, CFileItemList& items)
2545
{
2546
  try
2547
  {
2548
    if (NULL == m_pDB.get()) return false;
2549
    if (NULL == m_pDS.get()) return false;
2550
2551
    // get years from album list
2552
    CStdString strSQL="select distinct iYear from album where iYear <> 0";
2553
2554
    // run query
8035 by yuvalt
Initial port to Linux/SDL
2555
    CLog::Log(LOGDEBUG, "%s query: %s", __FUNCTION__, strSQL.c_str());
7949 by jmarshallnz
added: Year node to the music library.
2556
    if (!m_pDS->query(strSQL.c_str())) return false;
2557
    int iRowsFound = m_pDS->num_rows();
2558
    if (iRowsFound == 0)
2559
    {
2560
      m_pDS->close();
2561
      return false;
2562
    }
2563
2564
    // get data from returned rows
2565
    while (!m_pDS->eof())
2566
    {
10487 by jmarshallnz
changed: Use boost::shared_ptr in CFileItemLists to reduce the
2567
      CFileItemPtr pItem(new CFileItem(m_pDS->fv("iYear").get_asString()));
7949 by jmarshallnz
added: Year node to the music library.
2568
      SYSTEMTIME stTime;
17467 by jmarshallnz
changed: long -> int in database functions.
2569
      stTime.wYear = (WORD)m_pDS->fv("iYear").get_asInt();
7949 by jmarshallnz
added: Year node to the music library.
2570
      pItem->GetMusicInfoTag()->SetReleaseDate(stTime);
2571
      CStdString strDir;
17467 by jmarshallnz
changed: long -> int in database functions.
2572
      strDir.Format("%ld/", m_pDS->fv("iYear").get_asInt());
7949 by jmarshallnz
added: Year node to the music library.
2573
      pItem->m_strPath=strBaseDir + strDir;
2574
      pItem->m_bIsFolder=true;
2575
      items.Add(pItem);
2576
2577
      m_pDS->next();
2578
    }
2579
2580
    // cleanup
2581
    m_pDS->close();
2582
2583
    return true;
2584
  }
2585
  catch (...)
2586
  {
8035 by yuvalt
Initial port to Linux/SDL
2587
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
7949 by jmarshallnz
added: Year node to the music library.
2588
  }
2589
  return false;
2590
}
2591
17467 by jmarshallnz
changed: long -> int in database functions.
2592
bool CMusicDatabase::GetAlbumsByYear(const CStdString& strBaseDir, CFileItemList& items, int year)
7949 by jmarshallnz
added: Year node to the music library.
2593
{
2594
  CStdString where = FormatSQL("where iYear=%ld", year);
2595
12538 by jmarshallnz
fixed: Ticket #4993 - album smartplaylists wouldn't work if an order clause was given.
2596
  return GetAlbumsByWhere(strBaseDir, where, "", items);
7949 by jmarshallnz
added: Year node to the music library.
2597
}
2598
17467 by jmarshallnz
changed: long -> int in database functions.
2599
bool CMusicDatabase::GetArtistsNav(const CStdString& strBaseDir, CFileItemList& items, int idGenre, bool albumArtistsOnly)
2690 by kraqh3d
Initial commit for new music Nav window (does not support multi artist/genres yet!)
2600
{
11825 by jmarshallnz
cleanup: Removed some duplicate code, ifdef'd out some xbox-only stuff, and implemented tighter memory control around album + song queries. Fixes #4703.
2601
  if (NULL == m_pDB.get()) return false;
2602
  if (NULL == m_pDS.get()) return false;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2603
  try
2604
  {
2605
    if (NULL == m_pDB.get()) return false;
2606
    if (NULL == m_pDS.get()) return false;
10519 by spiff_
cosmetics (trailing whitespace)
2607
17715 by jmarshallnz
cleanup: DWORD -> unsigned int where used for timing, and variable names. Ticket #7299, thanks to gnif.
2608
    unsigned int time = CTimeUtils::GetTimeMS();
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2609
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2610
    CStdString strSQL = "select * from artist where (idArtist IN ";
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
2611
4458 by bobbin007
*** empty log message ***
2612
    if (idGenre==-1)
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2613
    {
7957 by jmarshallnz
cleanup: Cleaned up some music library filesystem stuff + reordered the library root
2614
      if (!albumArtistsOnly)  // show all artists in this case (ie those linked to a song)
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2615
        strSQL +=         "("
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2616
                          "select song.idArtist from song" // All primary artists linked to a song
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
2617
                          ") "
2618
                        "or idArtist IN "
2619
                          "("
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2620
                          "select exartistsong.idArtist from exartistsong" // All extra artists linked to a song
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2621
                          ") "
2622
                        "or idArtist IN ";
10519 by spiff_
cosmetics (trailing whitespace)
2623
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2624
      // and always show any artists linked to an album (may be different from above due to album artist tag)
2625
      strSQL +=          "("
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2626
                          "select album.idArtist from album" // All primary artists linked to an album
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2627
                          ") "
2628
                        "or idArtist IN "
2629
                          "("
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2630
                          "select exartistalbum.idArtist from exartistalbum "; // All extra artists linked to an album
10519 by spiff_
cosmetics (trailing whitespace)
2631
      if (albumArtistsOnly)
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2632
        strSQL +=         "join album on album.idAlbum = exartistalbum.idAlbum " // if we're hiding compilation artists,
14448 by jmarshallnz
fixed: Ticket #5789 - Non-album artists were shown under a particular genre even if the "Hide compilation artists" option was enabled.
2633
                          "where album.strExtraArtists != ''";                   // then exclude those that have no extra artists
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2634
      strSQL +=           ")"
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
2635
                        ") ";
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2636
    }
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
2637
    else
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2638
    { // same statements as above, but limit to the specified genre
2639
      // in this case we show the whole lot always - there is no limitation to just album artists
14448 by jmarshallnz
fixed: Ticket #5789 - Non-album artists were shown under a particular genre even if the "Hide compilation artists" option was enabled.
2640
      if (!albumArtistsOnly)  // show all artists in this case (ie those linked to a song)
2641
        strSQL+=FormatSQL("("
2642
                          "select song.idArtist from song " // All primary artists linked to primary genres
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2643
                          "where song.idGenre=%i"
14448 by jmarshallnz
fixed: Ticket #5789 - Non-album artists were shown under a particular genre even if the "Hide compilation artists" option was enabled.
2644
                          ") "
2645
                        "or idArtist IN "
2646
                          "("
2647
                          "select song.idArtist from song " // All primary artists linked to extra genres
2648
                            "join exgenresong on song.idSong=exgenresong.idSong "
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2649
                          "where exgenresong.idGenre=%i"
14448 by jmarshallnz
fixed: Ticket #5789 - Non-album artists were shown under a particular genre even if the "Hide compilation artists" option was enabled.
2650
                          ")"
2651
                        "or idArtist IN "
2652
                          "("
2653
                          "select exartistsong.idArtist from exartistsong " // All extra artists linked to extra genres
2654
                            "join song on exartistsong.idSong=song.idSong "
2655
                            "join exgenresong on song.idSong=exgenresong.idSong "
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2656
                          "where exgenresong.idGenre=%i"
14448 by jmarshallnz
fixed: Ticket #5789 - Non-album artists were shown under a particular genre even if the "Hide compilation artists" option was enabled.
2657
                          ") "
2658
                        "or idArtist IN "
2659
                          "("
2660
                          "select exartistsong.idArtist from exartistsong " // All extra artists linked to primary genres
2661
                            "join song on exartistsong.idSong=song.idSong "
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2662
                          "where song.idGenre=%i"
14448 by jmarshallnz
fixed: Ticket #5789 - Non-album artists were shown under a particular genre even if the "Hide compilation artists" option was enabled.
2663
                          ") "
2664
                        "or idArtist IN "
2665
                        , idGenre, idGenre, idGenre, idGenre);
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2666
      // and add any artists linked to an album (may be different from above due to album artist tag)
2667
      strSQL += FormatSQL("("
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2668
                          "select album.idArtist from album " // All primary album artists linked to primary genres
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2669
                          "where album.idGenre=%i"
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
2670
                          ") "
2671
                        "or idArtist IN "
2672
                          "("
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2673
                          "select album.idArtist from album " // All primary album artists linked to extra genres
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2674
                            "join exgenrealbum on album.idAlbum=exgenrealbum.idAlbum "
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2675
                          "where exgenrealbum.idGenre=%i"
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
2676
                          ")"
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2677
                        "or idArtist IN "
2678
                          "("
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2679
                          "select exartistalbum.idArtist from exartistalbum " // All extra album artists linked to extra genres
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2680
                            "join album on exartistalbum.idAlbum=album.idAlbum "
2681
                            "join exgenrealbum on album.idAlbum=exgenrealbum.idAlbum "
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2682
                          "where exgenrealbum.idGenre=%i"
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2683
                          ") "
2684
                        "or idArtist IN "
2685
                          "("
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2686
                          "select exartistalbum.idArtist from exartistalbum " // All extra album artists linked to primary genres
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2687
                            "join album on exartistalbum.idAlbum=album.idAlbum "
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2688
                          "where album.idGenre=%i"
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2689
                          ") "
2690
                        ")", idGenre, idGenre, idGenre, idGenre);
2691
    }
2835 by kraqh3d
fixed: my music nav window showing false items due to allmusic.com lookups
2692
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2693
    // remove the null string
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2694
    strSQL += " and artist.strArtist != \"\"";
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2695
    // and the various artist entry if applicable
14524 by jmarshallnz
fixed: Ticket #6191 - The "Various Artists" item was missing when browsing by Genre while "hide compilation only artists" was enabled.
2696
    if (!albumArtistsOnly)
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2697
    {
2698
      CStdString strVariousArtists = g_localizeStrings.Get(340);
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2699
      int idVariousArtists = AddArtist(strVariousArtists);
2700
      strSQL+=FormatSQL(" and artist.idArtist<>%i", idVariousArtists);
7736 by jmarshallnz
added: Reading of AlbumArtist tag from mp3/mp4/vorbis/ape tags. If present, the artist refered to in library->Albums view is the album artist, while songs retain their particular artist.
2701
    }
2690 by kraqh3d
Initial commit for new music Nav window (does not support multi artist/genres yet!)
2702
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2703
    // run query
8035 by yuvalt
Initial port to Linux/SDL
2704
    CLog::Log(LOGDEBUG, "%s query: %s", __FUNCTION__, strSQL.c_str());
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
2705
    if (!m_pDS->query(strSQL.c_str())) return false;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2706
    int iRowsFound = m_pDS->num_rows();
2707
    if (iRowsFound == 0)
2708
    {
2709
      m_pDS->close();
2710
      return false;
2711
    }
2712
4886 by bobbin007
fixed: music nav, after getting album information the thumb of the item is not updated
2713
    items.Reserve(iRowsFound);
2714
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2715
    // get data from returned rows
2716
    while (!m_pDS->eof())
2717
    {
9930 by vulkanr
merge with trunk. revision 12580.
2718
      CStdString strArtist = m_pDS->fv("strArtist").get_asString();
10487 by jmarshallnz
changed: Use boost::shared_ptr in CFileItemLists to reduce the
2719
      CFileItemPtr pItem(new CFileItem(strArtist));
9930 by vulkanr
merge with trunk. revision 12580.
2720
      pItem->GetMusicInfoTag()->SetArtist(strArtist);
4458 by bobbin007
*** empty log message ***
2721
      CStdString strDir;
17467 by jmarshallnz
changed: long -> int in database functions.
2722
      int idArtist = m_pDS->fv("idArtist").get_asInt();
9930 by vulkanr
merge with trunk. revision 12580.
2723
      strDir.Format("%ld/", idArtist);
4458 by bobbin007
*** empty log message ***
2724
      pItem->m_strPath=strBaseDir + strDir;
2725
      pItem->m_bIsFolder=true;
20662 by topfs2
Merge commit 'origin/jsonrpc' into trunk
2726
      pItem->GetMusicInfoTag()->SetDatabaseId(idArtist);
8765 by vulkanr
merge with trunk. revision: 10510.
2727
      if (CFile::Exists(pItem->GetCachedArtistThumb()))
2728
        pItem->SetThumbnailImage(pItem->GetCachedArtistThumb());
15183 by jmarshallnz
changed: Eliminate the use of "Big" icons as thumbnails, and cleaned up file casing of the icons to MixedCase.
2729
      pItem->SetIconImage("DefaultArtist.png");
9868 by vulkanr
merge with trunk. rev. 12480.
2730
      CArtist artist;
9930 by vulkanr
merge with trunk. revision 12580.
2731
      GetArtistInfo(idArtist,artist,false);
18356 by spiff_
fixed: album/artist info not available in music visualization
2732
2733
      SetPropertiesFromArtist(*pItem,artist);
4458 by bobbin007
*** empty log message ***
2734
      items.Add(pItem);
2735
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2736
      m_pDS->next();
2737
    }
17686 by davilla
[all] replace winapi timeGetTime/GetTickCOunt with CTimeUtils::GetTimeMS. Will need linux/win verify.
2738
    CLog::Log(LOGDEBUG,"Time to retrieve artists from dataset = %i", CTimeUtils::GetTimeMS() - time);
9930 by vulkanr
merge with trunk. revision 12580.
2739
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2740
    // cleanup
2741
    m_pDS->close();
2742
2743
    return true;
2744
  }
2745
  catch (...)
2746
  {
11825 by jmarshallnz
cleanup: Removed some duplicate code, ifdef'd out some xbox-only stuff, and implemented tighter memory control around album + song queries. Fixes #4703.
2747
    m_pDS->close();
8035 by yuvalt
Initial port to Linux/SDL
2748
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
2749
  }
2750
  return false;
2751
}
2752
17467 by jmarshallnz
changed: long -> int in database functions.
2753
bool CMusicDatabase::GetAlbumFromSong(int idSong, CAlbum &album)
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
2754
{
2755
  try
2756
  {
2757
    if (NULL == m_pDB.get()) return false;
2758
    if (NULL == m_pDS.get()) return false;
2759
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2760
    CStdString strSQL = FormatSQL("select albumview.* from song join albumview on song.idAlbum = albumview.idAlbum where song.idSong='%i'", idSong);
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
2761
    if (!m_pDS->query(strSQL.c_str())) return false;
2762
    int iRowsFound = m_pDS->num_rows();
2763
    if (iRowsFound != 1)
2764
    {
2765
      m_pDS->close();
2766
      return false;
2767
    }
2768
9868 by vulkanr
merge with trunk. rev. 12480.
2769
    album = GetAlbumFromDataset(m_pDS.get());
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
2770
2771
    m_pDS->close();
2772
    return true;
2773
2774
  }
2775
  catch (...)
2776
  {
8035 by yuvalt
Initial port to Linux/SDL
2777
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
2778
  }
2779
  return false;
2780
}
2781
2782
// This function won't be required if/when the fileitem tag has idSong information
2783
bool CMusicDatabase::GetAlbumFromSong(const CSong &song, CAlbum &album)
2784
{
2785
  try
2786
  {
2787
    if (NULL == m_pDB.get()) return false;
2788
    if (NULL == m_pDS.get()) return false;
2789
2790
    if (song.idSong != -1) return GetAlbumFromSong(song.idSong, album);
2791
2792
    CStdString path, file;
2793
    CUtil::Split(song.strFileName, path, file);
2794
8289 by yuvalt
Merged with trunk up to revision 9184
2795
    CStdString strSQL = FormatSQL("select albumview.* from song join albumview on song.idAlbum = albumview.idAlbum join path on song.idPath = path.idPath where song.strFileName like '%s' and path.strPath like '%s'", file.c_str(), path.c_str());
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
2796
    if (!m_pDS->query(strSQL.c_str())) return false;
2797
    int iRowsFound = m_pDS->num_rows();
2798
    if (iRowsFound != 1)
2799
    {
2800
      m_pDS->close();
2801
      return false;
2802
    }
2803
9868 by vulkanr
merge with trunk. rev. 12480.
2804
    album = GetAlbumFromDataset(m_pDS.get());
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
2805
2806
    m_pDS->close();
2807
    return true;
2808
  }
2809
  catch (...)
2810
  {
8035 by yuvalt
Initial port to Linux/SDL
2811
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2812
  }
2813
  return false;
2690 by kraqh3d
Initial commit for new music Nav window (does not support multi artist/genres yet!)
2814
}
2815
17467 by jmarshallnz
changed: long -> int in database functions.
2816
bool CMusicDatabase::GetAlbumsNav(const CStdString& strBaseDir, CFileItemList& items, int idGenre, int idArtist)
2690 by kraqh3d
Initial commit for new music Nav window (does not support multi artist/genres yet!)
2817
{
7949 by jmarshallnz
added: Year node to the music library.
2818
  // where clause
2819
  CStdString strWhere;
2820
  if (idGenre!=-1)
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2821
  {
7949 by jmarshallnz
added: Year node to the music library.
2822
    strWhere+=FormatSQL("where (idAlbum IN "
2823
                          "("
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2824
                          "select song.idAlbum from song " // All albums where the primary genre fits
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2825
                          "where song.idGenre=%i"
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
2826
                          ") "
7949 by jmarshallnz
added: Year node to the music library.
2827
                        "or idAlbum IN "
2828
                          "("
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2829
                          "select song.idAlbum from song " // All albums where extra genres fits
7949 by jmarshallnz
added: Year node to the music library.
2830
                            "join exgenresong on song.idSong=exgenresong.idSong "
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2831
                          "where exgenresong.idGenre=%i"
7949 by jmarshallnz
added: Year node to the music library.
2832
                          ")"
2833
                        ") "
2834
                        , idGenre, idGenre);
2835
  }
2836
2837
  if (idArtist!=-1)
2838
  {
4457 by kraqh3d
- 22-12-2005 changed: My Music Library would incorrectly handle artists and albums that were blank as if they were the "all" item. These items are now filtered out of the display.
2839
    if (strWhere.IsEmpty())
2840
      strWhere += "where ";
2841
    else
2842
      strWhere += "and ";
7949 by jmarshallnz
added: Year node to the music library.
2843
2844
    strWhere +=FormatSQL("(idAlbum IN "
2845
                            "("
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2846
                              "select song.idAlbum from song "  // All albums where the primary artist fits
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2847
                              "where song.idArtist=%i"
7949 by jmarshallnz
added: Year node to the music library.
2848
                            ")"
2849
                          " or idAlbum IN "
2850
                            "("
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2851
                              "select song.idAlbum from song "  // All albums where extra artists fit
7949 by jmarshallnz
added: Year node to the music library.
2852
                                "join exartistsong on song.idSong=exartistsong.idSong "
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2853
                              "where exartistsong.idArtist=%i"
7949 by jmarshallnz
added: Year node to the music library.
2854
                            ")"
2855
                          " or idAlbum IN "
2856
                            "("
8601 by vulkanr
merge with trunk. rev: 9636-10031.
2857
                              "select album.idAlbum from album " // All albums where primary album artist fits
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2858
                              "where album.idArtist=%i"
7949 by jmarshallnz
added: Year node to the music library.
2859
                            ")"
2860
                          " or idAlbum IN "
2861
                            "("
2862
                              "select exartistalbum.idAlbum from exartistalbum " // All albums where extra album artists fit
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
2863
                              "where exartistalbum.idArtist=%i"
7949 by jmarshallnz
added: Year node to the music library.
2864
                            ")"
2865
                          ") "
2866
                          , idArtist, idArtist, idArtist, idArtist);
2867
  }
17140 by jmarshallnz
added: Support for singles in the music library. Anything with the ALBUM tag empty is regarded as a single. Singles are grouped into their own root node, and the singles for a particular artist are grouped together into a "Singles" virtual album. Ticket #7061.
2868
  else
2869
  { // no artist given, so exclude any single albums (aka empty tagged albums)
2870
    if (strWhere.IsEmpty())
2871
      strWhere += "where albumview.strAlbum <> ''";
2872
    else
2873
      strWhere += "and albumview.strAlbum <> ''";
2874
  }
7949 by jmarshallnz
added: Year node to the music library.
2875
13242 by spiff_
added: ticket #4834 - Local Music Fanart. stick fanart.jpg in the artist folder. we assume some/path/artist/album/
2876
  bool bResult = GetAlbumsByWhere(strBaseDir, strWhere, "", items);
2877
  if (bResult)
2878
  {
13349 by spiff_
changed: cache music fanart in a separate folder
2879
    CStdString strArtist;
2880
    GetArtistById(idArtist,strArtist);
2881
    CStdString strFanart = items.GetCachedThumb(strArtist,g_settings.GetMusicFanartFolder());
2882
    if (CFile::Exists(strFanart))
2883
      items.SetProperty("fanart_image",strFanart);
13242 by spiff_
added: ticket #4834 - Local Music Fanart. stick fanart.jpg in the artist folder. we assume some/path/artist/album/
2884
  }
2885
2886
  return bResult;
7949 by jmarshallnz
added: Year node to the music library.
2887
}
2888
12538 by jmarshallnz
fixed: Ticket #4993 - album smartplaylists wouldn't work if an order clause was given.
2889
bool CMusicDatabase::GetAlbumsByWhere(const CStdString &baseDir, const CStdString &where, const CStdString &order, CFileItemList &items)
7949 by jmarshallnz
added: Year node to the music library.
2890
{
11825 by jmarshallnz
cleanup: Removed some duplicate code, ifdef'd out some xbox-only stuff, and implemented tighter memory control around album + song queries. Fixes #4703.
2891
  if (NULL == m_pDB.get()) return false;
2892
  if (NULL == m_pDS.get()) return false;
2893
7949 by jmarshallnz
added: Year node to the music library.
2894
  try
2895
  {
17140 by jmarshallnz
added: Support for singles in the music library. Anything with the ALBUM tag empty is regarded as a single. Singles are grouped into their own root node, and the singles for a particular artist are grouped together into a "Singles" virtual album. Ticket #7061.
2896
    CStdString sql = "select * from albumview " + where + order;
2835 by kraqh3d
fixed: my music nav window showing false items due to allmusic.com lookups
2897
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2898
    // run query
8035 by yuvalt
Initial port to Linux/SDL
2899
    CLog::Log(LOGDEBUG, "%s query: %s", __FUNCTION__, sql.c_str());
7949 by jmarshallnz
added: Year node to the music library.
2900
    if (!m_pDS->query(sql.c_str())) return false;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2901
    int iRowsFound = m_pDS->num_rows();
2902
    if (iRowsFound == 0)
2903
    {
2904
      m_pDS->close();
2905
      return false;
2906
    }
2907
4886 by bobbin007
fixed: music nav, after getting album information the thumb of the item is not updated
2908
    items.Reserve(iRowsFound);
2909
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2910
    // get data from returned rows
2911
    while (!m_pDS->eof())
2912
    {
11825 by jmarshallnz
cleanup: Removed some duplicate code, ifdef'd out some xbox-only stuff, and implemented tighter memory control around album + song queries. Fixes #4703.
2913
      try
2914
      {
2915
        CStdString strDir;
17467 by jmarshallnz
changed: long -> int in database functions.
2916
        int idAlbum = m_pDS->fv("idAlbum").get_asInt();
11825 by jmarshallnz
cleanup: Removed some duplicate code, ifdef'd out some xbox-only stuff, and implemented tighter memory control around album + song queries. Fixes #4703.
2917
        strDir.Format("%s%ld/", baseDir.c_str(), idAlbum);
2918
        CFileItemPtr pItem(new CFileItem(strDir, GetAlbumFromDataset(m_pDS.get())));
2919
        items.Add(pItem);
2920
        m_pDS->next();
2921
      }
2922
      catch (...)
2923
      {
2924
        m_pDS->close();
2925
        CLog::Log(LOGERROR, "%s - out of memory getting listing (got %i)", __FUNCTION__, items.Size());
2926
      }
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2927
    }
2928
2929
    // cleanup
2930
    m_pDS->close();
2931
    return true;
2932
  }
2933
  catch (...)
2934
  {
11825 by jmarshallnz
cleanup: Removed some duplicate code, ifdef'd out some xbox-only stuff, and implemented tighter memory control around album + song queries. Fixes #4703.
2935
    m_pDS->close();
8035 by yuvalt
Initial port to Linux/SDL
2936
    CLog::Log(LOGERROR, "%s (%s) failed", __FUNCTION__, where.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
2937
  }
2938
  return false;
2744 by kraqh3d
changed: my->music->nav to support multi artist & multi genre, also added more intelligent sorting to ignore "The" prefix
2939
}
2940
7949 by jmarshallnz
added: Year node to the music library.
2941
bool CMusicDatabase::GetSongsByWhere(const CStdString &baseDir, const CStdString &whereClause, CFileItemList &items)
4940 by jmarshallnz
- 08-03-2006 added: Smart Playlists in My Music. Only loadable from a file (no GUI setup of smart playlists)
2942
{
11649 by jmarshallnz
changed: Tighter exception handling around GetSongsByWhere(), to cope with tight memory conditions on xbox. Fixes ticket #4590.
2943
  if (NULL == m_pDB.get()) return false;
2944
  if (NULL == m_pDS.get()) return false;
2945
4940 by jmarshallnz
- 08-03-2006 added: Smart Playlists in My Music. Only loadable from a file (no GUI setup of smart playlists)
2946
  try
2947
  {
17715 by jmarshallnz
cleanup: DWORD -> unsigned int where used for timing, and variable names. Ticket #7299, thanks to gnif.
2948
    unsigned int time = CTimeUtils::GetTimeMS();
4940 by jmarshallnz
- 08-03-2006 added: Smart Playlists in My Music. Only loadable from a file (no GUI setup of smart playlists)
2949
    // We don't use FormatSQL here, as the WHERE clause is already formatted.
2950
    CStdString strSQL = "select * from songview " + whereClause;
8035 by yuvalt
Initial port to Linux/SDL
2951
    CLog::Log(LOGDEBUG, "%s query = %s", __FUNCTION__, strSQL.c_str());
4940 by jmarshallnz
- 08-03-2006 added: Smart Playlists in My Music. Only loadable from a file (no GUI setup of smart playlists)
2952
    // run query
2953
    if (!m_pDS->query(strSQL.c_str()))
2954
      return false;
2955
    int iRowsFound = m_pDS->num_rows();
2956
    if (iRowsFound == 0)
2957
    {
2958
      m_pDS->close();
2959
      return false;
2960
    }
2961
2962
    // get data from returned rows
11825 by jmarshallnz
cleanup: Removed some duplicate code, ifdef'd out some xbox-only stuff, and implemented tighter memory control around album + song queries. Fixes #4703.
2963
    items.Reserve(items.Size() + iRowsFound);
4940 by jmarshallnz
- 08-03-2006 added: Smart Playlists in My Music. Only loadable from a file (no GUI setup of smart playlists)
2964
    // get songs from returned subtable
5044 by jmarshallnz
- 22-03-2006 fixed: Smart playlists didn't support ordering + limiting.
2965
    int count = 0;
4940 by jmarshallnz
- 08-03-2006 added: Smart Playlists in My Music. Only loadable from a file (no GUI setup of smart playlists)
2966
    while (!m_pDS->eof())
2967
    {
11649 by jmarshallnz
changed: Tighter exception handling around GetSongsByWhere(), to cope with tight memory conditions on xbox. Fixes ticket #4590.
2968
      try
2969
      {
2970
        CFileItemPtr item(new CFileItem);
2971
        GetFileItemFromDataset(item.get(), baseDir);
2972
        // HACK for sorting by database returned order
2973
        item->m_iprogramCount = ++count;
2974
        items.Add(item);
2975
        m_pDS->next();
2976
      }
2977
      catch (...)
2978
      {
2979
        m_pDS->close();
2980
        CLog::Log(LOGERROR, "%s: out of memory loading query: %s", __FUNCTION__, whereClause.c_str());
2981
        return (items.Size() > 0);
2982
      }
4940 by jmarshallnz
- 08-03-2006 added: Smart Playlists in My Music. Only loadable from a file (no GUI setup of smart playlists)
2983
    }
2984
    // cleanup
2985
    m_pDS->close();
17686 by davilla
[all] replace winapi timeGetTime/GetTickCOunt with CTimeUtils::GetTimeMS. Will need linux/win verify.
2986
    CLog::Log(LOGDEBUG, "%s(%s) - took %d ms", __FUNCTION__, whereClause.c_str(), CTimeUtils::GetTimeMS() - time);
4940 by jmarshallnz
- 08-03-2006 added: Smart Playlists in My Music. Only loadable from a file (no GUI setup of smart playlists)
2987
    return true;
2988
  }
2989
  catch (...)
2990
  {
11649 by jmarshallnz
changed: Tighter exception handling around GetSongsByWhere(), to cope with tight memory conditions on xbox. Fixes ticket #4590.
2991
    // cleanup
2992
    m_pDS->close();
8035 by yuvalt
Initial port to Linux/SDL
2993
    CLog::Log(LOGERROR, "%s(%s) failed", __FUNCTION__, whereClause.c_str());
4940 by jmarshallnz
- 08-03-2006 added: Smart Playlists in My Music. Only loadable from a file (no GUI setup of smart playlists)
2994
  }
2995
  return false;
2996
}
2997
17467 by jmarshallnz
changed: long -> int in database functions.
2998
bool CMusicDatabase::GetSongsByYear(const CStdString& baseDir, CFileItemList& items, int year)
7949 by jmarshallnz
added: Year node to the music library.
2999
{
3000
  CStdString where=FormatSQL("where (iYear=%ld)", year);
3001
  return GetSongsByWhere(baseDir, where, items);
3002
}
3003
17467 by jmarshallnz
changed: long -> int in database functions.
3004
bool CMusicDatabase::GetSongsNav(const CStdString& strBaseDir, CFileItemList& items, int idGenre, int idArtist,int idAlbum)
2744 by kraqh3d
changed: my->music->nav to support multi artist & multi genre, also added more intelligent sorting to ignore "The" prefix
3005
{
11825 by jmarshallnz
cleanup: Removed some duplicate code, ifdef'd out some xbox-only stuff, and implemented tighter memory control around album + song queries. Fixes #4703.
3006
  CStdString strWhere;
3007
3008
  if (idAlbum!=-1)
3009
    strWhere=FormatSQL("where (idAlbum=%ld) ", idAlbum);
3010
3011
  if (idGenre!=-1)
3012
  {
3013
    if (strWhere.IsEmpty())
3014
      strWhere += "where ";
3015
    else
3016
      strWhere += "and ";
3017
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3018
    strWhere += FormatSQL("(idGenre=%i " // All songs where primary genre fits
11825 by jmarshallnz
cleanup: Removed some duplicate code, ifdef'd out some xbox-only stuff, and implemented tighter memory control around album + song queries. Fixes #4703.
3019
                          "or idSong IN "
3020
                            "("
3021
                            "select exgenresong.idSong from exgenresong " // All songs by where extra genres fit
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3022
                            "where exgenresong.idGenre=%i"
11825 by jmarshallnz
cleanup: Removed some duplicate code, ifdef'd out some xbox-only stuff, and implemented tighter memory control around album + song queries. Fixes #4703.
3023
                            ")"
3024
                          ") "
3025
                          , idGenre, idGenre);
3026
  }
3027
3028
  if (idArtist!=-1)
3029
  {
3030
    if (strWhere.IsEmpty())
3031
      strWhere += "where ";
3032
    else
3033
      strWhere += "and ";
3034
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3035
    strWhere += FormatSQL("(idArtist=%i " // All songs where primary artist fits
11825 by jmarshallnz
cleanup: Removed some duplicate code, ifdef'd out some xbox-only stuff, and implemented tighter memory control around album + song queries. Fixes #4703.
3036
                          "or idSong IN "
3037
                            "("
3038
                            "select exartistsong.idSong from exartistsong " // All songs where extra artists fit
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3039
                            "where exartistsong.idArtist=%i"
11825 by jmarshallnz
cleanup: Removed some duplicate code, ifdef'd out some xbox-only stuff, and implemented tighter memory control around album + song queries. Fixes #4703.
3040
                            ")"
3041
                          "or idSong IN "
3042
                            "("
3043
                            "select song.idSong from song " // All songs where the primary album artist fits
3044
                            "join album on song.idAlbum=album.idAlbum "
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3045
                            "where album.idArtist=%i"
11825 by jmarshallnz
cleanup: Removed some duplicate code, ifdef'd out some xbox-only stuff, and implemented tighter memory control around album + song queries. Fixes #4703.
3046
                            ")"
3047
                          "or idSong IN "
3048
                            "("
3049
                            "select song.idSong from song " // All songs where the extra album artist fit, excluding
3050
                            "join exartistalbum on song.idAlbum=exartistalbum.idAlbum " // various artist albums
3051
                            "join album on song.idAlbum=album.idAlbum "
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3052
                            "where exartistalbum.idArtist=%i and album.strExtraArtists != ''"
11825 by jmarshallnz
cleanup: Removed some duplicate code, ifdef'd out some xbox-only stuff, and implemented tighter memory control around album + song queries. Fixes #4703.
3053
                            ")"
3054
                          ") "
3055
                          , idArtist, idArtist, idArtist, idArtist);
3056
  }
3057
3058
  // run query
13242 by spiff_
added: ticket #4834 - Local Music Fanart. stick fanart.jpg in the artist folder. we assume some/path/artist/album/
3059
  bool bResult = GetSongsByWhere(strBaseDir, strWhere, items);
3060
  if (bResult)
3061
  {
13309 by jezz_x
Fixed : Music Fanart was not reading the right cached files since changes in r17012 (thanks Spiff)
3062
    CStdString strArtist;
13349 by spiff_
changed: cache music fanart in a separate folder
3063
    GetArtistById(idArtist,strArtist);
3064
    CStdString strFanart = items.GetCachedThumb(strArtist,g_settings.GetMusicFanartFolder());
13309 by jezz_x
Fixed : Music Fanart was not reading the right cached files since changes in r17012 (thanks Spiff)
3065
    if (CFile::Exists(strFanart))
3066
      items.SetProperty("fanart_image",strFanart);
13242 by spiff_
added: ticket #4834 - Local Music Fanart. stick fanart.jpg in the artist folder. we assume some/path/artist/album/
3067
  }
3068
3069
  return bResult;
2734 by jmarshallnz
- 03-03-2005 added: Cached thumb location is now stored in the database. Music database SHOULD auto-update when you first access My Music.
3070
}
3071
6884 by jmarshallnz
fixed: Databases with floating point version numbers could have issues (float -> string -> float conversion). Converted all versions to integers. Also dropped backward compatibility for databases from pre-2.0.0.
3072
bool CMusicDatabase::UpdateOldVersion(int version)
2734 by jmarshallnz
- 03-03-2005 added: Cached thumb location is now stored in the database. Music database SHOULD auto-update when you first access My Music.
3073
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
3074
  if (NULL == m_pDB.get()) return false;
3075
  if (NULL == m_pDS.get()) return false;
3076
  if (NULL == m_pDS2.get()) return false;
2734 by jmarshallnz
- 03-03-2005 added: Cached thumb location is now stored in the database. Music database SHOULD auto-update when you first access My Music.
3077
3078
  try
3079
  {
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
3080
    if (version < 6)
3081
    {
3082
      // add the strHash column to path table
3083
      m_pDS->exec("alter table path add strHash text");
3084
    }
8066 by yuvalt
Merged with main trunk up to revision 8787
3085
    if (version < 7)
3086
    {
3087
      // add the rating and comment columns to the song table (and song view)
3088
      m_pDS->exec("alter table song add rating char default '0'");
3089
      m_pDS->exec("alter table song add comment text");
3090
3091
      // drop the song view
3092
      m_pDS->exec("drop view songview");
3093
3094
      // and re-add it with the updated columns.
3095
      m_pDS->exec("create view songview as select idSong, song.strExtraArtists as strExtraArtists, song.strExtraGenres as strExtraGenres, strTitle, iTrack, iDuration, song.iYear as iYear, dwFileNameCRC, strFileName, strMusicBrainzTrackID, strMusicBrainzArtistID, strMusicBrainzAlbumID, strMusicBrainzAlbumArtistID, strMusicBrainzTRMID, iTimesPlayed, iStartOffset, iEndOffset, lastplayed, rating, comment, song.idAlbum as idAlbum, strAlbum, strPath, song.idArtist as idArtist, strArtist, song.idGenre as idGenre, strGenre, strThumb from song join album on song.idAlbum=album.idAlbum join path on song.idPath=path.idPath join artist on song.idArtist=artist.idArtist join genre on song.idGenre=genre.idGenre join thumb on song.idThumb=thumb.idThumb");
3096
    }
9868 by vulkanr
merge with trunk. rev. 12480.
3097
    if (version < 8)
3098
    {
10519 by spiff_
cosmetics (trailing whitespace)
3099
      // create the artistinfo table
9868 by vulkanr
merge with trunk. rev. 12480.
3100
      m_pDS->exec("CREATE TABLE artistinfo ( idArtistInfo integer primary key, idArtist integer, strBorn text, strFormed text, strGenres text, strMoods text, strStyles text, strInstruments text, strBiography text, strDied text, strDisbanded text, strYearsActive text, strImage text)\n");
3101
      CLog::Log(LOGINFO, "create content table");
3102
      m_pDS->exec("CREATE TABLE content (strPath text, strScraperPath text, strContent text, strSettings text)\n");
3103
      CLog::Log(LOGINFO, "create discography table");
3104
      m_pDS->exec("CREATE TABLE discography (idArtist integer, strAlbum text, strYear text)\n");
3105
      CLog::Log(LOGINFO, "create new albuminfo table");
3106
      m_pDS->exec("DROP TABLE albuminfo\n");
3107
      m_pDS->exec("CREATE TABLE albuminfo ( idAlbumInfo integer primary key, idAlbum integer, iYear integer, idGenre integer, strExtraGenres text, strMoods text, strStyles text, strThemes text, strReview text, strLabel text, strType text, strImage text, iRating integer)\n");
3108
    }
9930 by vulkanr
merge with trunk. revision 12580.
3109
    if (version < 9)
3110
    {
3111
      // add missing indices
3112
      m_pDS->exec("CREATE INDEX idxArtistInfo on artistinfo(idArtist)");
3113
      m_pDS->exec("CREATE INDEX idxAlbumInfo on albuminfo(idAlbum)");
3114
    }
10245 by vulkanr
merge with trunk. rev. 13243.
3115
    if (version < 10)
3116
    { // extend albumview to include all info
3117
      m_pDS->exec("drop view albumview");
3118
      m_pDS->exec("create view albumview as select album.idAlbum as idAlbum, strAlbum, strExtraArtists, "
3119
                  "album.idArtist as idArtist, album.strExtraGenres as strExtraGenres, album.idGenre as idGenre, "
3120
                  "strArtist, strGenre, album.iYear as iYear, strThumb, idAlbumInfo, strMoods, strStyles, strThemes, "
3121
                  "strReview, strLabel, strType, strImage, iRating from album "
3122
                  "left outer join artist on album.idArtist=artist.idArtist "
3123
                  "left outer join genre on album.idGenre=genre.idGenre "
3124
                  "left outer join thumb on album.idThumb=thumb.idThumb "
3125
                  "left outer join albuminfo on album.idAlbum=albumInfo.idAlbum");
3126
    }
13479 by gyunaev
Merging with karaoke branch.
3127
    if (version < 11)
3128
    {
3129
      // add karaoke database
3130
      m_pDS->exec("CREATE TABLE karaokedata ( iKaraNumber integer, idSong integer, iKaraDelay integer, strKaraEncoding text, "
3131
                "strKaralyrics text, strKaraLyrFileCRC text )\n");
3132
3133
      m_pDS->exec("CREATE INDEX idxKaraNumber on karaokedata(iKaraNumber)");
3134
      m_pDS->exec("CREATE INDEX idxKarSong on karaokedata(idSong)");
3135
3136
      // drop the song view
3137
      m_pDS->exec("drop view songview");
13480 by charlydoes
fixed: music database songview view.
3138
      m_pDS->exec("create view songview as select song.idSong as idSong, song.strExtraArtists as strExtraArtists, song.strExtraGenres as strExtraGenres, strTitle, iTrack, iDuration, song.iYear as iYear, dwFileNameCRC, strFileName, strMusicBrainzTrackID, strMusicBrainzArtistID, strMusicBrainzAlbumID, strMusicBrainzAlbumArtistID, strMusicBrainzTRMID, iTimesPlayed, iStartOffset, iEndOffset, lastplayed, rating, comment, song.idAlbum as idAlbum, strAlbum, strPath, song.idArtist as idArtist, strArtist, song.idGenre as idGenre, strGenre, strThumb, iKaraNumber, iKaraDelay, strKaraEncoding from song join album on song.idAlbum=album.idAlbum join path on song.idPath=path.idPath join  artist on song.idArtist=artist.idArtist join genre on song.idGenre=genre.idGenre join thumb on song.idThumb=thumb.idThumb left outer join karaokedata on song.idSong=karaokedata.idSong");
13479 by gyunaev
Merging with karaoke branch.
3139
3140
      AddGenre( "Karaoke" );
3141
    }
13629 by jmarshallnz
merged: Changes from the pathcleanup branch. Special Note: Some stored paths (such as in favourites.xml, or in profiles.xml) may no longer be valid, so may need recreating.
3142
    if (version < 12)
3143
    {
3144
      // update our thumb table as we've changed from storing absolute to relative paths
3145
      CStdString newPath = g_settings.GetMusicThumbFolder();
13642 by jmarshallnz
changed: Got rid of the remaining P/Q/T/U/Z usage, by moving the mapping out of IoSupport and into it's own class.
3146
      CStdString oldPath = CSpecialProtocol::TranslatePath(newPath);
13629 by jmarshallnz
merged: Changes from the pathcleanup branch. Special Note: Some stored paths (such as in favourites.xml, or in profiles.xml) may no longer be valid, so may need recreating.
3147
      if (m_pDS->query("select * from thumb where strThumb != 'NONE'") && m_pDS->num_rows())
3148
      {
3149
        // run through our thumbs and update them to the correct path
3150
        while (!m_pDS->eof())
3151
        {
17467 by jmarshallnz
changed: long -> int in database functions.
3152
          int id = m_pDS->fv(0).get_asInt();
13629 by jmarshallnz
merged: Changes from the pathcleanup branch. Special Note: Some stored paths (such as in favourites.xml, or in profiles.xml) may no longer be valid, so may need recreating.
3153
          CStdString thumb = m_pDS->fv(1).get_asString();
3154
          if (thumb.Left(oldPath.size()).CompareNoCase(oldPath) == 0)
3155
          {
3156
            thumb = CUtil::AddFileToFolder(newPath, thumb.Mid(oldPath.size()));
3157
            CStdString sql = FormatSQL("update thumb set strThumb='%s' where idThumb=%i\n", thumb.c_str(), id);
3158
            m_pDS2->exec(sql.c_str());
3159
          }
3160
          m_pDS->next();
3161
        }
3162
      }
3163
    }
14378 by spiff_
added: ticket #4834 - scraped music fanart. uses http://www.htbackdrops.com - go add your favorite artist. thanks to nuzecast for making the backend available
3164
    if (version < 13)
3165
    {
3166
      // add fanart info
3167
      m_pDS->exec("alter table artistinfo add strFanart text");
3168
    }
16491 by spiff_
changed: sanify the thumb handling in scrapers and nfo files. just add several <thumb> tags instead of the <thumbs> sillyness we used before. i have only updated tmdb, imdb and tvdb video scrapers + the music scrapers.
3169
    if (version < 14)
3170
    {
3171
      const char* tag1[] = {"idAlbumInfo","idArtistInfo"};
3172
      const char* tag2[] = {"albuminfo","artistinfo"};
3173
      for (int i=0;i<2;++i)
3174
      {
3175
        CStdString strSQL=FormatSQL("select %s,strImage from %s",
3176
                                    tag1[i],tag2[i]);
3177
        m_pDS->query(strSQL.c_str());
3178
        while (!m_pDS->eof())
3179
        {
3180
          TiXmlDocument doc;
3181
          doc.Parse(m_pDS->fv(1).get_asString().c_str());
3182
          if (!doc.RootElement() || strcmp(doc.RootElement()->Value(),"thumb") == 0)
3183
          {
3184
            m_pDS->next();
3185
            continue;
3186
          }
3187
          const TiXmlElement* thumb=NULL;
3188
          while (strstr(doc.RootElement()->FirstChild()->Value(),"<"))
3189
          {
3190
            CStdString strThumbs = doc.RootElement()->FirstChild()->Value();
3191
            TiXmlDocument doc2;
3192
            doc2.Parse(strThumbs);
3193
            doc = doc2;
3194
            thumb = doc.FirstChildElement("thumb");
3195
          }
3196
          if (!thumb)
3197
            thumb = doc.RootElement()->FirstChildElement("thumb");
3198
3199
          stringstream str;
3200
          while (thumb)
3201
          {
3202
            str << *thumb;
3203
            thumb = thumb->NextSiblingElement("thumb");
3204
          }
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3205
          CStdString strSQL = FormatSQL("update %s set strImage='%s' where %s=%i",
16491 by spiff_
changed: sanify the thumb handling in scrapers and nfo files. just add several <thumb> tags instead of the <thumbs> sillyness we used before. i have only updated tmdb, imdb and tvdb video scrapers + the music scrapers.
3206
                                        tag2[i],
3207
                                        str.str().c_str(),tag1[i],
17467 by jmarshallnz
changed: long -> int in database functions.
3208
                                        m_pDS->fv(0).get_asInt());
16491 by spiff_
changed: sanify the thumb handling in scrapers and nfo files. just add several <thumb> tags instead of the <thumbs> sillyness we used before. i have only updated tmdb, imdb and tvdb video scrapers + the music scrapers.
3209
          m_pDS2->exec(strSQL.c_str());
3210
          m_pDS->next();
3211
        }
3212
      }
3213
    }
2734 by jmarshallnz
- 03-03-2005 added: Cached thumb location is now stored in the database. Music database SHOULD auto-update when you first access My Music.
3214
  }
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
3215
  catch (...)
2734 by jmarshallnz
- 03-03-2005 added: Cached thumb location is now stored in the database. Music database SHOULD auto-update when you first access My Music.
3216
  {
3217
    CLog::Log(LOGERROR, "Error attempting to update the database version!");
3218
    return false;
3219
  }
2744 by kraqh3d
changed: my->music->nav to support multi artist & multi genre, also added more intelligent sorting to ignore "The" prefix
3220
  return true;
2734 by jmarshallnz
- 03-03-2005 added: Cached thumb location is now stored in the database. Music database SHOULD auto-update when you first access My Music.
3221
}
3222
17467 by jmarshallnz
changed: long -> int in database functions.
3223
int CMusicDatabase::AddThumb(const CStdString& strThumb1)
2734 by jmarshallnz
- 03-03-2005 added: Cached thumb location is now stored in the database. Music database SHOULD auto-update when you first access My Music.
3224
{
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
3225
  CStdString strSQL;
3226
  try
3227
  {
3228
    CStdString strThumb = strThumb1;
3229
    if (strThumb.IsEmpty())
3230
      strThumb = "NONE";
3231
3232
    if (NULL == m_pDB.get()) return -1;
3233
    if (NULL == m_pDS.get()) return -1;
3234
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
3235
    map <CStdString, int>::const_iterator it;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
3236
3237
    it = m_thumbCache.find(strThumb1);
3238
    if (it != m_thumbCache.end())
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
3239
      return it->second;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
3240
3740 by bobbin007
added: base class for database classes
3241
    strSQL=FormatSQL( "select * from thumb where strThumb='%s'", strThumb.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
3242
    m_pDS->query(strSQL.c_str());
3243
    if (m_pDS->num_rows() == 0)
3244
    {
3245
      m_pDS->close();
3246
      // doesnt exists, add it
3740 by bobbin007
added: base class for database classes
3247
      strSQL=FormatSQL("insert into thumb (idThumb, strThumb) values( NULL, '%s' )", strThumb.c_str());
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
3248
      m_pDS->exec(strSQL.c_str());
3249
19283 by jmarshallnz
added: Improved abstraction of the database API, thanks to loopingz and firnsy
3250
      int idPath = (int)m_pDS->lastinsertid();
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
3251
      m_thumbCache.insert(pair<CStdString, int>(strThumb1, idPath));
3252
      return idPath;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
3253
    }
3254
    else
3255
    {
17467 by jmarshallnz
changed: long -> int in database functions.
3256
      int idPath = m_pDS->fv("idThumb").get_asInt();
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
3257
      m_thumbCache.insert(pair<CStdString, int>(strThumb1, idPath));
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
3258
      m_pDS->close();
4856 by jmarshallnz
- 01-03-2006 changed: Music and Video overlays are closed/open before we render the screen (allows vis on music overlay among other things)
3259
      return idPath;
2860 by jmarshallnz
Formatting for tabs -> 2 spaces
3260
    }
3261
  }
3262
  catch (...)
3263
  {
3264
    CLog::Log(LOGERROR, "musicdatabase:unable to addthumb (%s)", strSQL.c_str());
3265
  }
3266
3267
  return -1;
3268
}
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
3269
17467 by jmarshallnz
changed: long -> int in database functions.
3270
unsigned int CMusicDatabase::GetSongIDs(const CStdString& strWhere, vector<pair<int,int> > &songIDs)
5854 by jmarshallnz
- 21-06-2006 fixed: Speed up for party mode starting.
3271
{
3272
  try
3273
  {
3274
    if (NULL == m_pDB.get()) return 0;
3275
    if (NULL == m_pDS.get()) return 0;
3276
3277
    CStdString strSQL = "select idsong from songview " + strWhere;
3278
    if (!m_pDS->query(strSQL.c_str())) return 0;
3279
    songIDs.clear();
3280
    if (m_pDS->num_rows() == 0)
3281
    {
3282
      m_pDS->close();
3283
      return 0;
3284
    }
3285
    songIDs.reserve(m_pDS->num_rows());
3286
    while (!m_pDS->eof())
3287
    {
17467 by jmarshallnz
changed: long -> int in database functions.
3288
      songIDs.push_back(make_pair<int,int>(1,m_pDS->fv(song_idSong).get_asInt()));
5854 by jmarshallnz
- 21-06-2006 fixed: Speed up for party mode starting.
3289
      m_pDS->next();
3290
    }    // cleanup
3291
    m_pDS->close();
3292
    return songIDs.size();
3293
  }
3294
  catch (...)
3295
  {
8035 by yuvalt
Initial port to Linux/SDL
3296
    CLog::Log(LOGERROR, "%s(%s) failed", __FUNCTION__, strWhere.c_str());
5854 by jmarshallnz
- 21-06-2006 fixed: Speed up for party mode starting.
3297
  }
3298
  return 0;
3299
}
3300
4916 by kraqh3d
- 07-03-2006 changed: Music Party Mode now uses a database table for its history, means another upgrade to version 1.6
3301
int CMusicDatabase::GetSongsCount(const CStdString& strWhere)
4797 by kraqh3d
- 23-02-2006 changed: stop disables party mode if its enabled.
3302
{
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
3303
  try
3304
  {
3305
    if (NULL == m_pDB.get()) return 0;
3306
    if (NULL == m_pDS.get()) return 0;
3307
4797 by kraqh3d
- 23-02-2006 changed: stop disables party mode if its enabled.
3308
    CStdString strSQL = "select count(idSong) as NumSongs from songview " + strWhere;
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
3309
    if (!m_pDS->query(strSQL.c_str())) return false;
3310
    if (m_pDS->num_rows() == 0)
3311
    {
3312
      m_pDS->close();
3313
      return 0;
3314
    }
3315
17467 by jmarshallnz
changed: long -> int in database functions.
3316
    int iNumSongs = m_pDS->fv("NumSongs").get_asInt();
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
3317
    // cleanup
3318
    m_pDS->close();
3319
    return iNumSongs;
3320
  }
3321
  catch (...)
3322
  {
8035 by yuvalt
Initial port to Linux/SDL
3323
    CLog::Log(LOGERROR, "%s(%s) failed", __FUNCTION__, strWhere.c_str());
4178 by bobbin007
added: Nav view, recently played albums (musicdatabase will be updated to version 1.3)
3324
  }
3325
  return 0;
3326
}
4458 by bobbin007
*** empty log message ***
3327
17467 by jmarshallnz
changed: long -> int in database functions.
3328
bool CMusicDatabase::GetAlbumPath(int idAlbum, CStdString& path)
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
3329
{
3330
  try
3331
  {
3332
    if (NULL == m_pDB.get()) return false;
12067 by jmarshallnz
fixed; Ticket #4846 - exporting music library would only export a single album.
3333
    if (NULL == m_pDS2.get()) return false;
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
3334
3335
    path.Empty();
3336
14837 by jmarshallnz
fixed: Ticket #6536 - MusicDB view open album info need more than 50 sec, due to slow GetAlbumPath().
3337
    CStdString strSQL=FormatSQL("select strPath from song join path on song.idPath = path.idPath where song.idAlbum=%ld", idAlbum);
12067 by jmarshallnz
fixed; Ticket #4846 - exporting music library would only export a single album.
3338
    if (!m_pDS2->query(strSQL.c_str())) return false;
3339
    int iRowsFound = m_pDS2->num_rows();
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
3340
    if (iRowsFound == 0)
3341
    {
12067 by jmarshallnz
fixed; Ticket #4846 - exporting music library would only export a single album.
3342
      m_pDS2->close();
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
3343
      return false;
3344
    }
3345
3346
    // if this returns more than one path, we just grab the first one.  It's just for determining where to obtain + place
3347
    // a local thumbnail
12067 by jmarshallnz
fixed; Ticket #4846 - exporting music library would only export a single album.
3348
    path = m_pDS2->fv("strPath").get_asString();
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
3349
12067 by jmarshallnz
fixed; Ticket #4846 - exporting music library would only export a single album.
3350
    m_pDS2->close(); // cleanup recordset data
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
3351
    return true;
3352
  }
3353
  catch (...)
3354
  {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3355
    CLog::Log(LOGERROR, "%s(%i) failed", __FUNCTION__, idAlbum);
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
3356
  }
3357
3358
  return false;
3359
}
3360
3361
17467 by jmarshallnz
changed: long -> int in database functions.
3362
bool CMusicDatabase::SaveAlbumThumb(int idAlbum, const CStdString& strThumb)
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
3363
{
3364
  try
3365
  {
3366
    if (NULL == m_pDB.get()) return false;
3367
    if (NULL == m_pDS.get()) return false;
4458 by bobbin007
*** empty log message ***
3368
17467 by jmarshallnz
changed: long -> int in database functions.
3369
    int idThumb=AddThumb(strThumb);
4458 by bobbin007
*** empty log message ***
3370
3371
    if (idThumb>-1)
3372
    {
3373
      CStdString strSQL=FormatSQL("UPDATE album SET idThumb=%ld where idAlbum=%ld", idThumb, idAlbum);
8035 by yuvalt
Initial port to Linux/SDL
3374
      CLog::Log(LOGDEBUG, "%s exec: %s", __FUNCTION__, strSQL.c_str());
4458 by bobbin007
*** empty log message ***
3375
      m_pDS->exec(strSQL.c_str());
3376
      strSQL=FormatSQL("UPDATE song SET idThumb=%ld where idAlbum=%ld", idThumb, idAlbum);
8035 by yuvalt
Initial port to Linux/SDL
3377
      CLog::Log(LOGDEBUG, "%s exec: %s", __FUNCTION__, strSQL.c_str());
4458 by bobbin007
*** empty log message ***
3378
      m_pDS->exec(strSQL.c_str());
3379
      return true;
3380
    }
3381
    return false;
3382
  }
3383
  catch (...)
3384
  {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3385
    CLog::Log(LOGERROR, "%s(%i) failed", __FUNCTION__, idAlbum);
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
3386
  }
3387
3388
  return false;
3389
}
3390
17467 by jmarshallnz
changed: long -> int in database functions.
3391
bool CMusicDatabase::GetAlbumThumb(int idAlbum, CStdString& strThumb)
9868 by vulkanr
merge with trunk. rev. 12480.
3392
{
3393
  try
3394
  {
3395
    if (NULL == m_pDB.get()) return false;
3396
    if (NULL == m_pDS.get()) return false;
3397
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3398
    CStdString strSQL=FormatSQL("select strThumb from thumb join album on album.idThumb = thumb.idThumb where album.idAlbum=%i", idAlbum);
14278 by spiff_
fixed: export music database was broken
3399
    m_pDS2->query(strSQL.c_str());
3400
    if (m_pDS2->eof())
9868 by vulkanr
merge with trunk. rev. 12480.
3401
      return false;
3402
14278 by spiff_
fixed: export music database was broken
3403
    strThumb = m_pDS2->fv("strThumb").get_asString();
3404
    m_pDS2->close();
9868 by vulkanr
merge with trunk. rev. 12480.
3405
    return true;
3406
  }
3407
  catch (...)
3408
  {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3409
    CLog::Log(LOGERROR, "%s - (%i) failed", __FUNCTION__, idAlbum);
9868 by vulkanr
merge with trunk. rev. 12480.
3410
  }
3411
10519 by spiff_
cosmetics (trailing whitespace)
3412
  return false;
9868 by vulkanr
merge with trunk. rev. 12480.
3413
}
3414
17467 by jmarshallnz
changed: long -> int in database functions.
3415
bool CMusicDatabase::GetArtistPath(int idArtist, CStdString &basePath)
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
3416
{
3417
  try
3418
  {
3419
    if (NULL == m_pDB.get()) return false;
13242 by spiff_
added: ticket #4834 - Local Music Fanart. stick fanart.jpg in the artist folder. we assume some/path/artist/album/
3420
    if (NULL == m_pDS2.get()) return false;
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
3421
3422
    // find all albums from this artist, and all the paths to the songs from those albums
13647 by jmarshallnz
fixed: Slow GetArtistPath() query.
3423
    CStdString strSQL=FormatSQL("select strPath from album join song on album.idAlbum = song.idAlbum join path on song.idPath = path.idPath "
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3424
                                "where album.idAlbum in (select idAlbum from album where album.idArtist=%i) "
3425
                                "or album.idAlbum in (select idAlbum from exartistalbum where exartistalbum.idArtist = %i) "
10503 by charlydoes
fixed: extremely slow GetArtistPath query.
3426
                                "group by song.idPath", idArtist, idArtist);
4458 by bobbin007
*** empty log message ***
3427
3428
    // run query
13242 by spiff_
added: ticket #4834 - Local Music Fanart. stick fanart.jpg in the artist folder. we assume some/path/artist/album/
3429
    if (!m_pDS2->query(strSQL.c_str())) return false;
3430
    int iRowsFound = m_pDS2->num_rows();
4458 by bobbin007
*** empty log message ***
3431
    if (iRowsFound == 0)
3432
    {
13242 by spiff_
added: ticket #4834 - Local Music Fanart. stick fanart.jpg in the artist folder. we assume some/path/artist/album/
3433
      m_pDS2->close();
4458 by bobbin007
*** empty log message ***
3434
      return false;
3435
    }
10519 by spiff_
cosmetics (trailing whitespace)
3436
8066 by yuvalt
Merged with main trunk up to revision 8787
3437
    // special case for single path - assume that we're in an artist/album/songs filesystem
3438
    if (iRowsFound == 1)
3439
    {
13242 by spiff_
added: ticket #4834 - Local Music Fanart. stick fanart.jpg in the artist folder. we assume some/path/artist/album/
3440
      CUtil::GetParentPath(m_pDS2->fv("strPath").get_asString(), basePath);
3441
      m_pDS2->close();
8066 by yuvalt
Merged with main trunk up to revision 8787
3442
      return true;
3443
    }
4458 by bobbin007
*** empty log message ***
3444
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
3445
    // find the common path (if any) to these albums
3446
    basePath.Empty();
13242 by spiff_
added: ticket #4834 - Local Music Fanart. stick fanart.jpg in the artist folder. we assume some/path/artist/album/
3447
    while (!m_pDS2->eof())
4458 by bobbin007
*** empty log message ***
3448
    {
13242 by spiff_
added: ticket #4834 - Local Music Fanart. stick fanart.jpg in the artist folder. we assume some/path/artist/album/
3449
      CStdString path = m_pDS2->fv("strPath").get_asString();
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
3450
      if (basePath.IsEmpty())
3451
        basePath = path;
3452
      else
9831 by vulkanr
merge/sync with trunk. rev. 12385.
3453
        CUtil::GetCommonPath(basePath,path);
3454
13242 by spiff_
added: ticket #4834 - Local Music Fanart. stick fanart.jpg in the artist folder. we assume some/path/artist/album/
3455
      m_pDS2->next();
4458 by bobbin007
*** empty log message ***
3456
    }
3457
3458
    // cleanup
13242 by spiff_
added: ticket #4834 - Local Music Fanart. stick fanart.jpg in the artist folder. we assume some/path/artist/album/
3459
    m_pDS2->close();
4458 by bobbin007
*** empty log message ***
3460
    return true;
3461
3462
  }
3463
  catch (...)
3464
  {
8035 by yuvalt
Initial port to Linux/SDL
3465
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4458 by bobbin007
*** empty log message ***
3466
  }
3467
  return false;
3468
}
4518 by kraqh3d
- 08-01-2006 fixed: full restoration of the filter label in my music library
3469
17467 by jmarshallnz
changed: long -> int in database functions.
3470
int CMusicDatabase::GetArtistByName(const CStdString& strArtist)
8679 by vulkanr
3471
{
3472
  try
3473
  {
3474
    if (NULL == m_pDB.get()) return false;
3475
    if (NULL == m_pDS.get()) return false;
3476
3477
    CStdString strSQL=FormatSQL("select idArtist from artist where artist.strArtist like '%s'", strArtist.c_str());
3478
3479
    // run query
3480
    if (!m_pDS->query(strSQL.c_str())) return false;
3481
    int iRowsFound = m_pDS->num_rows();
3482
    if (iRowsFound != 1)
3483
    {
3484
      m_pDS->close();
3485
      return -1;
3486
    }
17467 by jmarshallnz
changed: long -> int in database functions.
3487
    int lResult = m_pDS->fv("artist.idArtist").get_asInt();
8679 by vulkanr
3488
    m_pDS->close();
3489
    return lResult;
3490
  }
3491
  catch (...)
3492
  {
3493
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
3494
  }
3495
  return -1;
3496
}
3497
17467 by jmarshallnz
changed: long -> int in database functions.
3498
int CMusicDatabase::GetAlbumByName(const CStdString& strAlbum, const CStdString& strArtist)
8679 by vulkanr
3499
{
3500
  try
3501
  {
3502
    if (NULL == m_pDB.get()) return false;
3503
    if (NULL == m_pDS.get()) return false;
3504
9868 by vulkanr
merge with trunk. rev. 12480.
3505
    CStdString strSQL;
3506
    if (strArtist.IsEmpty())
3507
      strSQL=FormatSQL("select idAlbum from album where album.strAlbum like '%s'", strAlbum.c_str());
3508
    else
3509
      strSQL=FormatSQL("select album.idAlbum from album join artist on artist.idartist = album.idartist where album.strAlbum like '%s' and artist.strArtist like '%s'", strAlbum.c_str(),strArtist.c_str());
8679 by vulkanr
3510
    // run query
3511
    if (!m_pDS->query(strSQL.c_str())) return false;
3512
    int iRowsFound = m_pDS->num_rows();
3513
    if (iRowsFound != 1)
3514
    {
3515
      m_pDS->close();
3516
      return -1;
3517
    }
17467 by jmarshallnz
changed: long -> int in database functions.
3518
    return m_pDS->fv("album.idAlbum").get_asInt();
8679 by vulkanr
3519
  }
3520
  catch (...)
3521
  {
3522
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
3523
  }
3524
  return -1;
3525
}
3526
17467 by jmarshallnz
changed: long -> int in database functions.
3527
bool CMusicDatabase::GetGenreById(int idGenre, CStdString& strGenre)
4518 by kraqh3d
- 08-01-2006 fixed: full restoration of the filter label in my music library
3528
{
3529
  strGenre = "";
3530
  try
3531
  {
3532
    if (NULL == m_pDB.get()) return false;
3533
    if (NULL == m_pDS.get()) return false;
3534
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3535
    CStdString strSQL=FormatSQL("select strGenre from genre where genre.idGenre = %i", idGenre);
4518 by kraqh3d
- 08-01-2006 fixed: full restoration of the filter label in my music library
3536
3537
    // run query
3538
    if (!m_pDS->query(strSQL.c_str())) return false;
3539
    int iRowsFound = m_pDS->num_rows();
3540
    if (iRowsFound != 1)
3541
    {
3542
      m_pDS->close();
3543
      return false;
3544
    }
3545
    strGenre = m_pDS->fv("genre.strGenre").get_asString();
3546
    return true;
3547
  }
3548
  catch (...)
3549
  {
8035 by yuvalt
Initial port to Linux/SDL
3550
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4518 by kraqh3d
- 08-01-2006 fixed: full restoration of the filter label in my music library
3551
  }
3552
  return false;
3553
}
3554
17467 by jmarshallnz
changed: long -> int in database functions.
3555
int CMusicDatabase::GetGenreByName(const CStdString& strGenre)
12864 by c0diq
added: GetGenreByName (used by UPnP)
3556
{
3557
  try
3558
  {
3559
    if (NULL == m_pDB.get()) return false;
3560
    if (NULL == m_pDS.get()) return false;
3561
3562
    CStdString strSQL;
3563
    strSQL=FormatSQL("select idGenre from genre where genre.strGenre like '%s'", strGenre.c_str());
3564
    // run query
3565
    if (!m_pDS->query(strSQL.c_str())) return false;
3566
    int iRowsFound = m_pDS->num_rows();
3567
    if (iRowsFound != 1)
3568
    {
3569
      m_pDS->close();
3570
      return -1;
3571
    }
17467 by jmarshallnz
changed: long -> int in database functions.
3572
    return m_pDS->fv("genre.idGenre").get_asInt();
12864 by c0diq
added: GetGenreByName (used by UPnP)
3573
  }
3574
  catch (...)
3575
  {
3576
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
3577
  }
3578
  return -1;
3579
}
3580
17467 by jmarshallnz
changed: long -> int in database functions.
3581
bool CMusicDatabase::GetArtistById(int idArtist, CStdString& strArtist)
4518 by kraqh3d
- 08-01-2006 fixed: full restoration of the filter label in my music library
3582
{
3583
  strArtist = "";
3584
  try
3585
  {
3586
    if (NULL == m_pDB.get()) return false;
3587
    if (NULL == m_pDS.get()) return false;
3588
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3589
    CStdString strSQL=FormatSQL("select strArtist from artist where artist.idArtist = %i", idArtist);
4518 by kraqh3d
- 08-01-2006 fixed: full restoration of the filter label in my music library
3590
3591
    // run query
3592
    if (!m_pDS->query(strSQL.c_str())) return false;
3593
    int iRowsFound = m_pDS->num_rows();
3594
    if (iRowsFound != 1)
3595
    {
3596
      m_pDS->close();
3597
      return false;
3598
    }
3599
    strArtist = m_pDS->fv("artist.strArtist").get_asString();
3600
    return true;
3601
  }
3602
  catch (...)
3603
  {
8035 by yuvalt
Initial port to Linux/SDL
3604
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4518 by kraqh3d
- 08-01-2006 fixed: full restoration of the filter label in my music library
3605
  }
3606
  return false;
3607
}
3608
17467 by jmarshallnz
changed: long -> int in database functions.
3609
bool CMusicDatabase::GetAlbumById(int idAlbum, CStdString& strAlbum)
4518 by kraqh3d
- 08-01-2006 fixed: full restoration of the filter label in my music library
3610
{
3611
  strAlbum = "";
3612
  try
3613
  {
3614
    if (NULL == m_pDB.get()) return false;
3615
    if (NULL == m_pDS.get()) return false;
3616
3617
    CStdString strSQL=FormatSQL("select strAlbum from album where album.idAlbum = %ld", idAlbum);
3618
3619
    // run query
3620
    if (!m_pDS->query(strSQL.c_str())) return false;
3621
    int iRowsFound = m_pDS->num_rows();
3622
    if (iRowsFound != 1)
3623
    {
3624
      m_pDS->close();
3625
      return false;
3626
    }
3627
    strAlbum = m_pDS->fv("album.strAlbum").get_asString();
3628
    return true;
3629
  }
3630
  catch (...)
3631
  {
8035 by yuvalt
Initial port to Linux/SDL
3632
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4518 by kraqh3d
- 08-01-2006 fixed: full restoration of the filter label in my music library
3633
  }
3634
  return false;
3635
}
3636
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3637
bool CMusicDatabase::GetRandomSong(CFileItem* item, int& idSong, const CStdString& strWhere)
4867 by kraqh3d
- 02-03-2006 added: seperate track formatting options for the music now playing window. (found in settings->music->playlist)
3638
{
4763 by kraqh3d
*** empty log message ***
3639
  try
3640
  {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3641
    idSong = -1;
4763 by kraqh3d
*** empty log message ***
3642
4916 by kraqh3d
- 07-03-2006 changed: Music Party Mode now uses a database table for its history, means another upgrade to version 1.6
3643
    int iCount = GetSongsCount(strWhere);
3644
    if (iCount <= 0)
3645
      return false;
3646
    int iRandom = rand() % iCount;
3647
3648
    if (NULL == m_pDB.get()) return false;
3649
    if (NULL == m_pDS.get()) return false;
3650
13766 by jmarshallnz
fixed: Reverted r16277 as it broke partymode with filtering. A different fix for #5317 is in progress.
3651
    // We don't use FormatSQL here, as the WHERE clause is already formatted
14108 by spiff_
cosmetics
3652
    CStdString strSQL;
13766 by jmarshallnz
fixed: Reverted r16277 as it broke partymode with filtering. A different fix for #5317 is in progress.
3653
    strSQL.Format("select * from songview %s order by idSong limit 1 offset %i", strWhere.c_str(), iRandom);
12704 by vdrfan
fixed: Ticket #5317 - Party mode causes SQL error
3654
8035 by yuvalt
Initial port to Linux/SDL
3655
    CLog::Log(LOGDEBUG, "%s query = %s", __FUNCTION__, strSQL.c_str());
4916 by kraqh3d
- 07-03-2006 changed: Music Party Mode now uses a database table for its history, means another upgrade to version 1.6
3656
    // run query
3657
    if (!m_pDS->query(strSQL.c_str()))
3658
      return false;
3659
    int iRowsFound = m_pDS->num_rows();
3660
    if (iRowsFound != 1)
4763 by kraqh3d
*** empty log message ***
3661
    {
3662
      m_pDS->close();
4916 by kraqh3d
- 07-03-2006 changed: Music Party Mode now uses a database table for its history, means another upgrade to version 1.6
3663
      return false;
4763 by kraqh3d
*** empty log message ***
3664
    }
4916 by kraqh3d
- 07-03-2006 changed: Music Party Mode now uses a database table for its history, means another upgrade to version 1.6
3665
    GetFileItemFromDataset(item, "");
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3666
    idSong = m_pDS->fv("songview.idSong").get_asInt();
4916 by kraqh3d
- 07-03-2006 changed: Music Party Mode now uses a database table for its history, means another upgrade to version 1.6
3667
    m_pDS->close();
4763 by kraqh3d
*** empty log message ***
3668
    return true;
3669
  }
3670
  catch(...)
3671
  {
8035 by yuvalt
Initial port to Linux/SDL
3672
    CLog::Log(LOGERROR, "%s(%s) failed", __FUNCTION__, strWhere.c_str());
4763 by kraqh3d
*** empty log message ***
3673
  }
3674
  return false;
4784 by bobbin007
added: music nav, "Compilation" folder, it shows all Various Artists albums
3675
}
3676
3677
bool CMusicDatabase::GetVariousArtistsAlbums(const CStdString& strBaseDir, CFileItemList& items)
3678
{
3679
  try
3680
  {
3681
    if (NULL == m_pDB.get()) return false;
3682
    if (NULL == m_pDS.get()) return false;
3683
3684
    CStdString strVariousArtists = g_localizeStrings.Get(340);
17467 by jmarshallnz
changed: long -> int in database functions.
3685
    int idVariousArtists=AddArtist(strVariousArtists);
4784 by bobbin007
added: music nav, "Compilation" folder, it shows all Various Artists albums
3686
    if (idVariousArtists<0)
3687
      return false;
3688
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3689
    CStdString strSQL = FormatSQL("select * from albumview where idArtist=%i", idVariousArtists);
4784 by bobbin007
added: music nav, "Compilation" folder, it shows all Various Artists albums
3690
3691
    // run query
8035 by yuvalt
Initial port to Linux/SDL
3692
    CLog::Log(LOGDEBUG, "%s query: %s", __FUNCTION__, strSQL.c_str());
4784 by bobbin007
added: music nav, "Compilation" folder, it shows all Various Artists albums
3693
    if (!m_pDS->query(strSQL.c_str())) return false;
3694
    int iRowsFound = m_pDS->num_rows();
3695
    if (iRowsFound == 0)
3696
    {
3697
      m_pDS->close();
3698
      return false;
3699
    }
3700
4886 by bobbin007
fixed: music nav, after getting album information the thumb of the item is not updated
3701
    items.Reserve(iRowsFound);
3702
4784 by bobbin007
added: music nav, "Compilation" folder, it shows all Various Artists albums
3703
    // get data from returned rows
3704
    while (!m_pDS->eof())
3705
    {
3706
      CStdString strDir;
17467 by jmarshallnz
changed: long -> int in database functions.
3707
      strDir.Format("%s%ld/", strBaseDir.c_str(), m_pDS->fv("idAlbum").get_asInt());
10487 by jmarshallnz
changed: Use boost::shared_ptr in CFileItemLists to reduce the
3708
      CFileItemPtr pItem(new CFileItem(strDir, GetAlbumFromDataset(m_pDS.get())));
4784 by bobbin007
added: music nav, "Compilation" folder, it shows all Various Artists albums
3709
      items.Add(pItem);
3710
3711
      m_pDS->next();
3712
    }
3713
3714
    // cleanup
3715
    m_pDS->close();
3716
    return true;
3717
3718
  }
3719
  catch (...)
3720
  {
8035 by yuvalt
Initial port to Linux/SDL
3721
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4784 by bobbin007
added: music nav, "Compilation" folder, it shows all Various Artists albums
3722
  }
3723
  return false;
3724
}
3725
3726
bool CMusicDatabase::GetVariousArtistsAlbumsSongs(const CStdString& strBaseDir, CFileItemList& items)
3727
{
3728
  try
3729
  {
3730
    if (NULL == m_pDB.get()) return false;
3731
    if (NULL == m_pDS.get()) return false;
3732
3733
    CStdString strVariousArtists = g_localizeStrings.Get(340);
17467 by jmarshallnz
changed: long -> int in database functions.
3734
    int idVariousArtists=AddArtist(strVariousArtists);
4784 by bobbin007
added: music nav, "Compilation" folder, it shows all Various Artists albums
3735
    if (idVariousArtists<0)
3736
      return false;
3737
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3738
    CStdString strSQL = FormatSQL("select * from songview where idAlbum IN (select idAlbum from album where idArtist=%i)", idVariousArtists);
4784 by bobbin007
added: music nav, "Compilation" folder, it shows all Various Artists albums
3739
3740
    // run query
8035 by yuvalt
Initial port to Linux/SDL
3741
    CLog::Log(LOGDEBUG, "%s query: %s", __FUNCTION__, strSQL.c_str());
4784 by bobbin007
added: music nav, "Compilation" folder, it shows all Various Artists albums
3742
    if (!m_pDS->query(strSQL.c_str())) return false;
3743
    int iRowsFound = m_pDS->num_rows();
3744
    if (iRowsFound == 0)
3745
    {
3746
      m_pDS->close();
3747
      return false;
3748
    }
3749
4886 by bobbin007
fixed: music nav, after getting album information the thumb of the item is not updated
3750
    items.Reserve(iRowsFound);
3751
4784 by bobbin007
added: music nav, "Compilation" folder, it shows all Various Artists albums
3752
    // get data from returned rows
3753
    while (!m_pDS->eof())
3754
    {
10487 by jmarshallnz
changed: Use boost::shared_ptr in CFileItemLists to reduce the
3755
      CFileItemPtr item(new CFileItem);
3756
      GetFileItemFromDataset(item.get(), strBaseDir);
4784 by bobbin007
added: music nav, "Compilation" folder, it shows all Various Artists albums
3757
      items.Add(item);
3758
3759
      m_pDS->next();
3760
    }
3761
3762
    // cleanup
3763
    m_pDS->close();
3764
    return true;
3765
3766
  }
3767
  catch (...)
3768
  {
8035 by yuvalt
Initial port to Linux/SDL
3769
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4784 by bobbin007
added: music nav, "Compilation" folder, it shows all Various Artists albums
3770
  }
3771
  return false;
3772
}
4916 by kraqh3d
- 07-03-2006 changed: Music Party Mode now uses a database table for its history, means another upgrade to version 1.6
3773
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
3774
void CMusicDatabase::SplitString(const CStdString &multiString, vector<CStdString> &vecStrings, CStdString &extraStrings)
3775
{
8523 by jmarshallnz
merged: Revisions 9635 -> 9702 changes from trunk.
3776
  int numStrings = StringUtils::SplitString(multiString, g_advancedSettings.m_musicItemSeparator, vecStrings);
7930 by jmarshallnz
changed: Merged the musicdb branch changes. Improved music library system. Albumartist is now supported, and albums are stored based on album and artist name, as opposed to album and path. ie no path dependence for albums. The library version has been bumped, and THERE IS NO UPDATE SUPPORT - you will have to rescan. Also, album thumb caching is now split over 16 different paths to limit the likelihood of running out of thumbs. They're also cached based on album and artist now, rather than album and path.
3777
  for (int i = 1; i < numStrings; i++)
8523 by jmarshallnz
merged: Revisions 9635 -> 9702 changes from trunk.
3778
    extraStrings += g_advancedSettings.m_musicItemSeparator + vecStrings[i];
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
3779
}
3780
3781
bool CMusicDatabase::SetPathHash(const CStdString &path, const CStdString &hash)
3782
{
3783
  try
3784
  {
3785
    if (NULL == m_pDB.get()) return false;
3786
    if (NULL == m_pDS.get()) return false;
3787
8002 by jmarshallnz
fixed: More reliable tracking of scanned paths in music library.
3788
    if (hash.IsEmpty())
3789
    { // this is an empty folder - we need only add it to the path table
3790
      // if the path actually exists
3791
      if (!CDirectory::Exists(path))
3792
        return false;
3793
    }
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3794
    int idPath = AddPath(path);
3795
    if (idPath < 0) return false;
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
3796
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
3797
    CStdString strSQL=FormatSQL("update path set strHash='%s' where idPath=%ld", hash.c_str(), idPath);
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
3798
    m_pDS->exec(strSQL.c_str());
3799
3800
    return true;
3801
  }
3802
  catch (...)
3803
  {
8035 by yuvalt
Initial port to Linux/SDL
3804
    CLog::Log(LOGERROR, "%s (%s, %s) failed", __FUNCTION__, path.c_str(), hash.c_str());
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
3805
  }
3806
3807
  return false;
3808
}
3809
3810
bool CMusicDatabase::GetPathHash(const CStdString &path, CStdString &hash)
3811
{
3812
  try
3813
  {
3814
    if (NULL == m_pDB.get()) return false;
3815
    if (NULL == m_pDS.get()) return false;
3816
3817
    CStdString strSQL=FormatSQL("select strHash from path where strPath like '%s'", path.c_str());
3818
    m_pDS->query(strSQL.c_str());
3819
    if (m_pDS->num_rows() == 0)
3820
      return false;
3821
    hash = m_pDS->fv("strHash").get_asString();
3822
    return true;
3823
  }
3824
  catch (...)
3825
  {
8035 by yuvalt
Initial port to Linux/SDL
3826
    CLog::Log(LOGERROR, "%s (%s) failed", __FUNCTION__, path.c_str());
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
3827
  }
3828
3829
  return false;
3830
}
3831
18494 by spiff_
added: ticket #7600 - removing a source doesn't remove tracks from the Music library
3832
bool CMusicDatabase::RemoveSongsFromPath(const CStdString &path, CSongMap &songs, bool exact)
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
3833
{
3834
  // We need to remove all songs from this path, as their tags are going
3835
  // to be re-read.  We need to remove all songs from the song table + all links to them
3836
  // from the exartistsong and exgenresong tables (as otherwise if a song is added back
3837
  // to the table with the same idSong, these tables can't be cleaned up properly later)
3838
3839
  // TODO: SQLite probably doesn't allow this, but can we rely on that??
3840
3841
  // We don't need to remove orphaned albums at this point as in AddAlbum() we check
3842
  // first whether the album has already been read during this scan, and if it hasn't
3843
  // we check whether it's in the table and update accordingly at that point, removing the entries from
3844
  // the exartistalbum and exgenrealbum tables.  The only failure point for this is albums
3845
  // that span multiple folders, where just the files in one folder have been changed.  In this case
3846
  // any exalbumartist(s) that are only in the files that haven't changed will be removed.  Clearly
3847
  // the primary albumartist still matches (as that's what we looked up based on) so is this really
3848
  // an issue?  I don't think it is, as those artists will still have links to the album via the songs
3849
  // which is generally what we rely on, so the only failure point is albumartist lookup.  In this
3850
  // case, it will return only things in the exartistalbum table from the newly updated songs (and
3851
  // only if they have additional artists).  I think the effect of this is minimal at best, as ALL
3852
  // songs in the album should have the same albumartist!
3853
8002 by jmarshallnz
fixed: More reliable tracking of scanned paths in music library.
3854
  // we also remove the path at this point as it will be added later on if the
3855
  // path still exists.
3856
  // After scanning we then remove the orphaned artists, genres and thumbs.
18494 by spiff_
added: ticket #7600 - removing a source doesn't remove tracks from the Music library
3857
3858
  // Note: when used to remove all songs from a path and its subpath (exact=false), this
3859
  // does miss archived songs.
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
3860
  try
3861
  {
11619 by spiff_
changed: replace an assert by a warning level log. fixes ticket #4471
3862
    if (!CUtil::HasSlashAtEnd(path))
3863
      CLog::Log(LOGWARNING,"%s: called on path without a trailing slash [%s]",__FUNCTION__,path.c_str());
7984 by jmarshallnz
fixed: Update Library or Rescanning would remove last played and top100 information for songs.
3864
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
3865
    if (NULL == m_pDB.get()) return false;
3866
    if (NULL == m_pDS.get()) return false;
3867
18494 by spiff_
added: ticket #7600 - removing a source doesn't remove tracks from the Music library
3868
    CStdString sql=FormatSQL("select * from songview where strPath like '%s%s'", path.c_str(), (exact?"":"%"));
7984 by jmarshallnz
fixed: Update Library or Rescanning would remove last played and top100 information for songs.
3869
    if (!m_pDS->query(sql.c_str())) return false;
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
3870
    int iRowsFound = m_pDS->num_rows();
8002 by jmarshallnz
fixed: More reliable tracking of scanned paths in music library.
3871
    if (iRowsFound > 0)
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
3872
    {
8002 by jmarshallnz
fixed: More reliable tracking of scanned paths in music library.
3873
      CStdString songIds = "(";
3874
      while (!m_pDS->eof())
3875
      {
3876
        CSong song = GetSongFromDataset();
3877
        songs.Add(song.strFileName, song);
3878
        songIds += FormatSQL("%i,", song.idSong);
3879
        m_pDS->next();
3880
      }
3881
      songIds.TrimRight(",");
3882
      songIds += ")";
3883
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
3884
      m_pDS->close();
8002 by jmarshallnz
fixed: More reliable tracking of scanned paths in music library.
3885
13479 by gyunaev
Merging with karaoke branch.
3886
      // and delete all songs, exartistsongs and exgenresongs and karaoke
8002 by jmarshallnz
fixed: More reliable tracking of scanned paths in music library.
3887
      sql = "delete from song where idSong in " + songIds;
3888
      m_pDS->exec(sql.c_str());
3889
      sql = "delete from exartistsong where idSong in " + songIds;
3890
      m_pDS->exec(sql.c_str());
3891
      sql = "delete from exgenresong where idSong in " + songIds;
3892
      m_pDS->exec(sql.c_str());
13479 by gyunaev
Merging with karaoke branch.
3893
      sql = "delete from karaokedata where idSong in " + songIds;
3894
      m_pDS->exec(sql.c_str());
8002 by jmarshallnz
fixed: More reliable tracking of scanned paths in music library.
3895
    }
3896
    // and remove the path as well (it'll be re-added later on with the new hash if it's non-empty)
18494 by spiff_
added: ticket #7600 - removing a source doesn't remove tracks from the Music library
3897
    sql = FormatSQL("delete from path where strPath like '%s%s'", path.c_str(), (exact?"":"%"));
8002 by jmarshallnz
fixed: More reliable tracking of scanned paths in music library.
3898
    m_pDS->exec(sql.c_str());
3899
    return iRowsFound > 0;
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
3900
  }
3901
  catch (...)
3902
  {
8035 by yuvalt
Initial port to Linux/SDL
3903
    CLog::Log(LOGERROR, "%s (%s) failed", __FUNCTION__, path.c_str());
7982 by jmarshallnz
added: Directory hashing to the music library scan process so that when we rescan we know whether we need to update the library or not. No need to ask user if they want to scan all or just scan newly added items anymore. Current hash is based on item paths and filesizes.
3904
  }
3905
  return false;
3906
}
3907
7983 by jmarshallnz
added: Update Library context menu item to the Music Library - rescans for added and removed items.
3908
bool CMusicDatabase::GetPaths(set<CStdString> &paths)
3909
{
3910
  try
3911
  {
3912
    if (NULL == m_pDB.get()) return false;
3913
    if (NULL == m_pDS.get()) return false;
3914
3915
    paths.clear();
3916
3917
    // find all paths
3918
    if (!m_pDS->query("select strPath from path")) return false;
3919
    int iRowsFound = m_pDS->num_rows();
3920
    if (iRowsFound == 0)
3921
    {
3922
      m_pDS->close();
3923
      return true;
3924
    }
3925
    while (!m_pDS->eof())
3926
    {
3927
      paths.insert(m_pDS->fv("strPath").get_asString());
3928
      m_pDS->next();
3929
    }
3930
    m_pDS->close();
3931
    return true;
3932
  }
3933
  catch (...)
3934
  {
8035 by yuvalt
Initial port to Linux/SDL
3935
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
7983 by jmarshallnz
added: Update Library context menu item to the Music Library - rescans for added and removed items.
3936
  }
3937
  return false;
3938
}
8066 by yuvalt
Merged with main trunk up to revision 8787
3939
3940
bool CMusicDatabase::SetSongRating(const CStdString &filePath, char rating)
3941
{
3942
  try
3943
  {
8839 by vulkanr
merged revisions 10713, 10714 (lastfm unlove/ban) that were left out from previous merge.
3944
    if (filePath.IsEmpty()) return false;
8066 by yuvalt
Merged with main trunk up to revision 8787
3945
    if (NULL == m_pDB.get()) return false;
3946
    if (NULL == m_pDS.get()) return false;
3947
17467 by jmarshallnz
changed: long -> int in database functions.
3948
    int songID = GetSongIDFromPath(filePath);
8066 by yuvalt
Merged with main trunk up to revision 8787
3949
    if (-1 == songID) return false;
3950
3951
    CStdString sql = FormatSQL("update song set rating='%c' where idSong = %i", rating, songID);
3952
    m_pDS->exec(sql.c_str());
3953
    return true;
3954
  }
3955
  catch (...)
3956
  {
3957
    CLog::Log(LOGERROR, "%s (%s,%c) failed", __FUNCTION__, filePath.c_str(), rating);
3958
  }
3959
  return false;
3960
}
3961
17467 by jmarshallnz
changed: long -> int in database functions.
3962
int CMusicDatabase::GetSongIDFromPath(const CStdString &filePath)
8066 by yuvalt
Merged with main trunk up to revision 8787
3963
{
3964
  // grab the where string to identify the song id
3965
  CURL url(filePath);
3966
  if (url.GetProtocol()=="musicdb")
3967
  {
3968
    CStdString strFile=CUtil::GetFileName(filePath);
3969
    CUtil::RemoveExtension(strFile);
3970
    return atol(strFile.c_str());
3971
  }
3972
  // hit the db
3973
  try
3974
  {
3975
    if (NULL == m_pDB.get()) return -1;
3976
    if (NULL == m_pDS.get()) return -1;
3977
    CStdString strPath;
3978
    CUtil::GetDirectory(filePath, strPath);
3979
    CUtil::AddSlashAtEnd(strPath);
3980
3981
    DWORD crc = ComputeCRC(filePath);
3982
3983
    CStdString sql = FormatSQL("select idSong from song join path on song.idPath = path.idPath where song.dwFileNameCRC='%ul'and path.strPath='%s'", crc, strPath.c_str());
3984
    if (!m_pDS->query(sql.c_str())) return -1;
3985
3986
    if (m_pDS->num_rows() == 0)
3987
    {
3988
      m_pDS->close();
3989
      return -1;
3990
    }
3991
17467 by jmarshallnz
changed: long -> int in database functions.
3992
    int songID = m_pDS->fv("idSong").get_asInt();
8066 by yuvalt
Merged with main trunk up to revision 8787
3993
    m_pDS->close();
3994
    return songID;
3995
  }
3996
  catch (...)
3997
  {
3998
    CLog::Log(LOGERROR, "%s (%s) failed", __FUNCTION__, filePath.c_str());
3999
  }
4000
  return -1;
4001
}
9868 by vulkanr
merge with trunk. rev. 12480.
4002
4003
bool CMusicDatabase::CommitTransaction()
4004
{
4005
  if (CDatabase::CommitTransaction())
4006
  { // number of items in the db has likely changed, so reset the infomanager cache
15648 by jmarshallnz
fixed: Save the number of songs/movies/shows etc. in database in local vars to save requerying it whenever we commit a transaction. Caused weird UI glitches on PM3.HD homepage, as well as a bunch of unnecessary sqlite collisions. Also wasn't preserved correctly across profile changes.
4007
    g_infoManager.SetLibraryBool(LIBRARY_HAS_MUSIC, GetSongsCount("") > 0);
9868 by vulkanr
merge with trunk. rev. 12480.
4008
    return true;
4009
  }
4010
  return false;
4011
}
4012
21055 by elupus
merged: addons-fw branch
4013
bool CMusicDatabase::SetScraperForPath(const CStdString& strPath, const ADDON::ScraperPtr& scraper)
9868 by vulkanr
merge with trunk. rev. 12480.
4014
{
4015
  try
4016
  {
4017
    if (NULL == m_pDB.get()) return false;
4018
    if (NULL == m_pDS.get()) return false;
10519 by spiff_
cosmetics (trailing whitespace)
4019
9868 by vulkanr
merge with trunk. rev. 12480.
4020
    // wipe old settings
4021
    CStdString strSQL = FormatSQL("delete from content where strPath like '%s'",strPath.c_str());
4022
    m_pDS->exec(strSQL.c_str());
4023
4024
    // insert new settings
21055 by elupus
merged: addons-fw branch
4025
    strSQL = FormatSQL("insert into content (strPath, strScraperPath, strContent, strSettings) values ('%s','%s','%s','%s')",
21113 by alcoheca
changed: renamed UUID to ID. we now accept any string as an identifier,
4026
      strPath.c_str(), scraper->Parent()->ID().c_str(), ADDON::TranslateContent(scraper->Content()).c_str(), scraper->GetSettings().c_str());
9868 by vulkanr
merge with trunk. rev. 12480.
4027
    m_pDS->exec(strSQL.c_str());
4028
4029
    return true;
4030
  }
4031
  catch (...)
4032
  {
4033
    CLog::Log(LOGERROR, "%s - (%s) failed", __FUNCTION__, strPath.c_str());
4034
  }
4035
  return false;
4036
}
4037
21055 by elupus
merged: addons-fw branch
4038
bool CMusicDatabase::GetScraperForPath(const CStdString& strPath, ADDON::ScraperPtr& info)
9868 by vulkanr
merge with trunk. rev. 12480.
4039
{
4040
  try
4041
  {
4042
    if (NULL == m_pDB.get()) return false;
4043
    if (NULL == m_pDS.get()) return false;
10519 by spiff_
cosmetics (trailing whitespace)
4044
9868 by vulkanr
merge with trunk. rev. 12480.
4045
    CStdString strSQL = FormatSQL("select * from content where strPath like '%s'",strPath.c_str());
4046
    m_pDS->query(strSQL.c_str());
4047
    if (m_pDS->eof()) // no info set for path - fallback logic commencing
4048
    {
4049
      CQueryParams params;
4050
      CDirectoryNode::GetDatabaseInfo(strPath, params);
4051
      if (params.GetGenreId() != -1) // check genre
4052
      {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
4053
        strSQL = FormatSQL("select * from content where strPath like 'musicdb://1/%i/'",params.GetGenreId());
9868 by vulkanr
merge with trunk. rev. 12480.
4054
        m_pDS->query(strSQL.c_str());
4055
      }
4056
      if (m_pDS->eof() && params.GetAlbumId() != -1) // check album
4057
      {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
4058
        strSQL = FormatSQL("select * from content where strPath like 'musicdb://3/%i/'",params.GetGenreId());
9868 by vulkanr
merge with trunk. rev. 12480.
4059
        m_pDS->query(strSQL.c_str());
4060
      }
4061
      if (m_pDS->eof() && params.GetArtistId() != -1) // check artist
4062
      {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
4063
        strSQL = FormatSQL("select * from content where strPath like 'musicdb://2/%i/'",params.GetArtistId());
9868 by vulkanr
merge with trunk. rev. 12480.
4064
        m_pDS->query(strSQL.c_str());
4065
      }
4066
      if (m_pDS->eof()) // general albums setting
4067
      {
4068
        strSQL = FormatSQL("select * from content where strPath like 'musicdb://3/'");
4069
        m_pDS->query(strSQL.c_str());
4070
      }
4071
      if (m_pDS->eof()) // general artist setting
4072
      {
4073
        strSQL = FormatSQL("select * from content where strPath like 'musicdb://2/'");
4074
        m_pDS->query(strSQL.c_str());
4075
      }
4076
    }
4077
4078
    if (!m_pDS->eof())
21055 by elupus
merged: addons-fw branch
4079
    { // try and ascertain scraper for this path
4080
      CONTENT_TYPE content = ADDON::TranslateContent(m_pDS->fv("content.strContent").get_asString());
4081
      CStdString scraperUUID = m_pDS->fv("content.strScraperPath").get_asString();
4082
4083
      if (content != CONTENT_NONE)
4084
      { // content set, use pre configured or default scraper
4085
        ADDON::AddonPtr addon;
21135 by alcoheca
changed: all addons should be store below either special://home/addons or
4086
        if (!scraperUUID.empty() && ADDON::CAddonMgr::Get()->GetAddon(scraperUUID, addon, ADDON::ADDON_SCRAPER) && addon)
21055 by elupus
merged: addons-fw branch
4087
        {
4088
          info = boost::dynamic_pointer_cast<ADDON::CScraper>(addon->Clone(addon));
4089
          if (!info)
4090
            return false;
4091
        }
4092
4093
        // store this path's settings
4094
        info->m_pathContent = content;
4095
        info->LoadUserXML(m_pDS->fv("content.strSettings").get_asString());
4096
      }
18781 by jmarshallnz
fixed: Music scrapers weren't sticking per db path. Thanks cptspiff.
4097
      else
21055 by elupus
merged: addons-fw branch
4098
      { // use default scraper for this content type
4099
        ADDON::AddonPtr defaultScraper;
4100
        if (ADDON::CAddonMgr::Get()->GetDefault(ADDON::ADDON_SCRAPER, defaultScraper, content))
18781 by jmarshallnz
fixed: Music scrapers weren't sticking per db path. Thanks cptspiff.
4101
        {
21055 by elupus
merged: addons-fw branch
4102
          info = boost::dynamic_pointer_cast<ADDON::CScraper>(defaultScraper->Clone(defaultScraper));
4103
          if (info)
4104
          {
4105
            info->m_pathContent = content;
4106
          }
18781 by jmarshallnz
fixed: Music scrapers weren't sticking per db path. Thanks cptspiff.
4107
        }
18521 by jmarshallnz
fixed: The default scraper wasn't choosen for music if you hadn't altered the setting in the UI.
4108
      }
4109
    }
9868 by vulkanr
merge with trunk. rev. 12480.
4110
    m_pDS->close();
21055 by elupus
merged: addons-fw branch
4111
4112
    if (!info)
4113
    { // use default music scraper instead
4114
      ADDON::AddonPtr addon;
4115
      if(ADDON::CAddonMgr::Get()->GetDefault(ADDON::ADDON_SCRAPER, addon, CONTENT_ALBUMS))
4116
      {
4117
        info = boost::dynamic_pointer_cast<ADDON::CScraper>(addon);
4118
        return (info);
4119
      }
4120
      else
4121
        return false;
4122
    }
4123
9868 by vulkanr
merge with trunk. rev. 12480.
4124
    return true;
4125
  }
4126
  catch (...)
4127
  {
4128
    CLog::Log(LOGERROR, "%s -(%s) failed", __FUNCTION__, strPath.c_str());
4129
  }
4130
  return false;
4131
}
4132
13880 by jmarshallnz
added: Ticket #5848 - Export individual .nfo files for music database, thanks to spiff.
4133
void CMusicDatabase::ExportToXML(const CStdString &xmlFile, bool singleFiles, bool images, bool overwrite)
9868 by vulkanr
merge with trunk. rev. 12480.
4134
{
4135
  try
4136
  {
4137
    if (NULL == m_pDB.get()) return;
4138
    if (NULL == m_pDS.get()) return;
4139
    if (NULL == m_pDS2.get()) return;
4140
4141
    // find all albums
4142
    CStdString sql = "select albumview.*,albuminfo.strImage,albuminfo.idalbuminfo from albuminfo "
4143
                     "join albumview on albuminfo.idAlbum=albumview.idAlbum "
4144
                     "join genre on albuminfo.idGenre=genre.idGenre";
10519 by spiff_
cosmetics (trailing whitespace)
4145
9930 by vulkanr
merge with trunk. revision 12580.
4146
    m_pDS->query(sql.c_str());
9868 by vulkanr
merge with trunk. rev. 12480.
4147
17685 by jmarshallnz
cosmetic: m_gWindowManager -> g_windowManager for consistency.
4148
    CGUIDialogProgress *progress = (CGUIDialogProgress *)g_windowManager.GetWindow(WINDOW_DIALOG_PROGRESS);
9868 by vulkanr
merge with trunk. rev. 12480.
4149
    if (progress)
4150
    {
4151
      progress->SetHeading(20196);
4152
      progress->SetLine(0, 650);
4153
      progress->SetLine(1, "");
4154
      progress->SetLine(2, "");
4155
      progress->SetPercentage(0);
4156
      progress->StartModal();
4157
      progress->ShowProgressBar(true);
4158
    }
4159
9930 by vulkanr
merge with trunk. revision 12580.
4160
    int total = m_pDS->num_rows();
9868 by vulkanr
merge with trunk. rev. 12480.
4161
    int current = 0;
4162
4163
    // create our xml document
4164
    TiXmlDocument xmlDoc;
4165
    TiXmlDeclaration decl("1.0", "UTF-8", "yes");
4166
    xmlDoc.InsertEndChild(decl);
4167
    TiXmlNode *pMain = NULL;
4168
    if (singleFiles)
4169
      pMain = &xmlDoc;
4170
    else
4171
    {
4172
      TiXmlElement xmlMainElement("musicdb");
4173
      pMain = xmlDoc.InsertEndChild(xmlMainElement);
4174
    }
9930 by vulkanr
merge with trunk. revision 12580.
4175
    while (!m_pDS->eof())
9868 by vulkanr
merge with trunk. rev. 12480.
4176
    {
9930 by vulkanr
merge with trunk. revision 12580.
4177
      CAlbum album = GetAlbumFromDataset(m_pDS.get());
9868 by vulkanr
merge with trunk. rev. 12480.
4178
      album.thumbURL.Clear();
9930 by vulkanr
merge with trunk. revision 12580.
4179
      album.thumbURL.ParseString(m_pDS->fv("albuminfo.strImage").get_asString());
17467 by jmarshallnz
changed: long -> int in database functions.
4180
      int idAlbumInfo = m_pDS->fv("albuminfo.idAlbumInfo").get_asInt();
9868 by vulkanr
merge with trunk. rev. 12480.
4181
      GetAlbumInfoSongs(idAlbumInfo,album.songs);
10060 by vulkanr
merge with trunk. revision - 12853.
4182
      CStdString strPath;
4183
      GetAlbumPath(album.idAlbum,strPath);
4184
      album.Save(pMain, "album", strPath);
19298 by arnova
added: More debug logging for music/video database when exporting items
4185
      if (singleFiles)
9868 by vulkanr
merge with trunk. rev. 12480.
4186
      {
19298 by arnova
added: More debug logging for music/video database when exporting items
4187
        if (!CDirectory::Exists(strPath))
4188
          CLog::Log(LOGDEBUG, "%s - Not exporting item %s as it does not exist", __FUNCTION__, strPath.c_str());
4189
        else
4190
        {
4191
          CStdString nfoFile;
4192
          CUtil::AddFileToFolder(strPath, "album.nfo", nfoFile);
4193
          if (overwrite || !CFile::Exists(nfoFile))
4194
          {
4195
            if (!xmlDoc.SaveFile(nfoFile))
4196
              CLog::Log(LOGERROR, "%s: Album nfo export failed! ('%s')", __FUNCTION__, nfoFile.c_str());
4197
          }
4198
4199
          if (images)
4200
          {
4201
            CStdString strThumb;
4202
            if (GetAlbumThumb(album.idAlbum,strThumb) && (overwrite || !CFile::Exists(CUtil::AddFileToFolder(strPath,"folder.jpg"))))
4203
              CFile::Cache(strThumb,CUtil::AddFileToFolder(strPath,"folder.jpg"));
4204
          }
4205
          xmlDoc.Clear();
4206
          TiXmlDeclaration decl("1.0", "UTF-8", "yes");
4207
          xmlDoc.InsertEndChild(decl);
4208
        }
9868 by vulkanr
merge with trunk. rev. 12480.
4209
      }
19298 by arnova
added: More debug logging for music/video database when exporting items
4210
9868 by vulkanr
merge with trunk. rev. 12480.
4211
      if ((current % 50) == 0 && progress)
4212
      {
4213
        progress->SetLine(1, album.strAlbum);
4214
        progress->SetPercentage(current * 100 / total);
4215
        progress->Progress();
4216
        if (progress->IsCanceled())
4217
        {
4218
          progress->Close();
9930 by vulkanr
merge with trunk. revision 12580.
4219
          m_pDS->close();
9868 by vulkanr
merge with trunk. rev. 12480.
4220
          return;
4221
        }
4222
      }
9930 by vulkanr
merge with trunk. revision 12580.
4223
      m_pDS->next();
9868 by vulkanr
merge with trunk. rev. 12480.
4224
      current++;
4225
    }
9930 by vulkanr
merge with trunk. revision 12580.
4226
    m_pDS->close();
9868 by vulkanr
merge with trunk. rev. 12480.
4227
4228
    // find all artists
4229
    sql = "select * from artistinfo "
4230
          "join artist on artist.idartist=artistinfo.idArtist";
10519 by spiff_
cosmetics (trailing whitespace)
4231
14280 by spiff_
fixed: music database export was broken (2)
4232
    // needed due to getartistpath
4233
    auto_ptr<dbiplus::Dataset> pDS;
4234
    pDS.reset(m_pDB->CreateDataset());
4235
    pDS->query(sql.c_str());
9868 by vulkanr
merge with trunk. rev. 12480.
4236
14280 by spiff_
fixed: music database export was broken (2)
4237
    total = pDS->num_rows();
9868 by vulkanr
merge with trunk. rev. 12480.
4238
    current = 0;
4239
14280 by spiff_
fixed: music database export was broken (2)
4240
    while (!pDS->eof())
9868 by vulkanr
merge with trunk. rev. 12480.
4241
    {
14280 by spiff_
fixed: music database export was broken (2)
4242
      CArtist artist = GetArtistFromDataset(pDS.get());
9868 by vulkanr
merge with trunk. rev. 12480.
4243
      CStdString strSQL=FormatSQL("select * from discography where idArtist=%i",artist.idArtist);
12698 by spiff_
fixed: ticket #5318 - Exporting music library would only export a single artist
4244
      m_pDS->query(strSQL.c_str());
4245
      while (!m_pDS->eof())
9868 by vulkanr
merge with trunk. rev. 12480.
4246
      {
12698 by spiff_
fixed: ticket #5318 - Exporting music library would only export a single artist
4247
        artist.discography.push_back(make_pair(m_pDS->fv("strAlbum").get_asString(),m_pDS->fv("strYear").get_asString()));
4248
        m_pDS->next();
9868 by vulkanr
merge with trunk. rev. 12480.
4249
      }
12698 by spiff_
fixed: ticket #5318 - Exporting music library would only export a single artist
4250
      m_pDS->close();
10060 by vulkanr
merge with trunk. revision - 12853.
4251
      CStdString strPath;
4252
      GetArtistPath(artist.idArtist,strPath);
4253
      artist.Save(pMain, "artist", strPath);
19298 by arnova
added: More debug logging for music/video database when exporting items
4254
      if (singleFiles)
9868 by vulkanr
merge with trunk. rev. 12480.
4255
      {
19298 by arnova
added: More debug logging for music/video database when exporting items
4256
        if (!CDirectory::Exists(strPath))
4257
          CLog::Log(LOGDEBUG, "%s - Not exporting item %s as it does not exist", __FUNCTION__, strPath.c_str());
4258
        else
4259
        {
4260
          CStdString nfoFile;
4261
          CUtil::AddFileToFolder(strPath, "artist.nfo", nfoFile);
4262
          if (overwrite || !CFile::Exists(nfoFile))
4263
          {
4264
            if (!xmlDoc.SaveFile(nfoFile))
4265
              CLog::Log(LOGERROR, "%s: Artist nfo export failed! ('%s')", __FUNCTION__, nfoFile.c_str());
4266
          }
4267
4268
          if (images)
4269
          {
4270
            CFileItem item(artist);
4271
            if (CFile::Exists(item.GetCachedArtistThumb()) && (overwrite || !CFile::Exists(CUtil::AddFileToFolder(strPath,"folder.jpg"))))
4272
              CFile::Cache(item.GetCachedArtistThumb(),CUtil::AddFileToFolder(strPath,"folder.jpg"));
4273
            if (CFile::Exists(item.GetCachedFanart()) && (overwrite || !CFile::Exists(CUtil::AddFileToFolder(strPath,"fanart.jpg"))))
4274
              CFile::Cache(item.GetCachedFanart(),CUtil::AddFileToFolder(strPath,"fanart.jpg"));
4275
          }
4276
          xmlDoc.Clear();
4277
          TiXmlDeclaration decl("1.0", "UTF-8", "yes");
4278
          xmlDoc.InsertEndChild(decl);
4279
        }
9868 by vulkanr
merge with trunk. rev. 12480.
4280
      }
19798 by spiff_
cosmetics
4281
9868 by vulkanr
merge with trunk. rev. 12480.
4282
      if ((current % 50) == 0 && progress)
4283
      {
4284
        progress->SetLine(1, artist.strArtist);
4285
        progress->SetPercentage(current * 100 / total);
4286
        progress->Progress();
4287
        if (progress->IsCanceled())
4288
        {
4289
          progress->Close();
4290
          m_pDS->close();
4291
          return;
4292
        }
4293
      }
14280 by spiff_
fixed: music database export was broken (2)
4294
      pDS->next();
9868 by vulkanr
merge with trunk. rev. 12480.
4295
      current++;
4296
    }
14280 by spiff_
fixed: music database export was broken (2)
4297
    pDS->close();
9868 by vulkanr
merge with trunk. rev. 12480.
4298
4299
    if (progress)
4300
      progress->Close();
4301
13629 by jmarshallnz
merged: Changes from the pathcleanup branch. Special Note: Some stored paths (such as in favourites.xml, or in profiles.xml) may no longer be valid, so may need recreating.
4302
    xmlDoc.SaveFile(xmlFile);
9868 by vulkanr
merge with trunk. rev. 12480.
4303
  }
4304
  catch (...)
4305
  {
4306
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4307
  }
4308
}
4309
4310
void CMusicDatabase::ImportFromXML(const CStdString &xmlFile)
4311
{
17685 by jmarshallnz
cosmetic: m_gWindowManager -> g_windowManager for consistency.
4312
  CGUIDialogProgress *progress = (CGUIDialogProgress *)g_windowManager.GetWindow(WINDOW_DIALOG_PROGRESS);
9868 by vulkanr
merge with trunk. rev. 12480.
4313
  try
4314
  {
4315
    if (NULL == m_pDB.get()) return;
4316
    if (NULL == m_pDS.get()) return;
4317
4318
    TiXmlDocument xmlDoc;
4319
    if (!xmlDoc.LoadFile(xmlFile))
4320
      return;
4321
4322
    TiXmlElement *root = xmlDoc.RootElement();
4323
    if (!root) return;
4324
4325
    if (progress)
4326
    {
4327
      progress->SetHeading(648);
4328
      progress->SetLine(0, 649);
4329
      progress->SetLine(1, 330);
4330
      progress->SetLine(2, "");
4331
      progress->SetPercentage(0);
4332
      progress->StartModal();
4333
      progress->ShowProgressBar(true);
4334
    }
4335
4336
    TiXmlElement *entry = root->FirstChildElement();
4337
    int current = 0;
4338
    int total = 0;
4339
    // first count the number of items...
4340
    while (entry)
4341
    {
4342
      if (strnicmp(entry->Value(), "artist", 6)==0 ||
4343
          strnicmp(entry->Value(), "album", 5)==0)
4344
        total++;
4345
      entry = entry->NextSiblingElement();
4346
    }
4347
4348
    BeginTransaction();
4349
    entry = root->FirstChildElement();
4350
    while (entry)
4351
    {
4352
      CStdString strTitle;
4353
      if (strnicmp(entry->Value(), "artist", 6) == 0)
10519 by spiff_
cosmetics (trailing whitespace)
4354
      {
20321 by monkeyman_67156
changed: code cleanups, removed unused variables, decreased scope
4355
        CArtist artist;
9868 by vulkanr
merge with trunk. rev. 12480.
4356
        artist.Load(entry);
4357
        strTitle = artist.strArtist;
17467 by jmarshallnz
changed: long -> int in database functions.
4358
        int idArtist = GetArtistByName(artist.strArtist);
9868 by vulkanr
merge with trunk. rev. 12480.
4359
        if (idArtist > -1)
4360
          SetArtistInfo(idArtist,artist);
4361
4362
        current++;
4363
      }
4364
      else if (strnicmp(entry->Value(), "album", 5) == 0)
10519 by spiff_
cosmetics (trailing whitespace)
4365
      {
20321 by monkeyman_67156
changed: code cleanups, removed unused variables, decreased scope
4366
        CAlbum album;
9868 by vulkanr
merge with trunk. rev. 12480.
4367
        album.Load(entry);
4368
        strTitle = album.strAlbum;
17467 by jmarshallnz
changed: long -> int in database functions.
4369
        int idAlbum = GetAlbumByName(album.strAlbum,album.strArtist);
9868 by vulkanr
merge with trunk. rev. 12480.
4370
        if (idAlbum > -1)
4371
          SetAlbumInfo(idAlbum,album,album.songs,false);
4372
4373
        current++;
4374
      }
4375
      entry = entry ->NextSiblingElement();
4376
      if (progress && total)
4377
      {
4378
        progress->SetPercentage(current * 100 / total);
4379
        progress->SetLine(2, strTitle);
4380
        progress->Progress();
4381
        if (progress->IsCanceled())
4382
        {
4383
          progress->Close();
4384
          RollbackTransaction();
4385
          return;
4386
        }
4387
      }
4388
    }
4389
    CommitTransaction();
4390
4391
    g_infoManager.ResetPersistentCache();
4392
  }
4393
  catch (...)
4394
  {
4395
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4396
  }
4397
  if (progress)
4398
    progress->Close();
4399
}
9912 by spiff_
fixed: refresh album/artist info didnt work
4400
13479 by gyunaev
Merging with karaoke branch.
4401
void CMusicDatabase::AddKaraokeData(const CSong& song)
4402
{
4403
  try
4404
  {
4405
    CStdString strSQL;
4406
4407
    // If song.iKaraokeNumber is non-zero, we already have it in the database. Just replace the song ID.
4408
    if ( song.iKaraokeNumber > 0 )
4409
    {
4410
      CStdString strSQL = FormatSQL("UPDATE karaokedata SET idSong=%i WHERE iKaraNumber=%i", song.idSong, song.iKaraokeNumber);
4411
      m_pDS->exec(strSQL.c_str());
4412
      return;
4413
    }
4414
4415
    // Add new karaoke data
4416
    DWORD crc = ComputeCRC( song.strFileName );
4417
4418
    // Get the maximum number allocated
4419
    strSQL=FormatSQL( "SELECT MAX(iKaraNumber) FROM karaokedata" );
4420
    if (!m_pDS->query(strSQL.c_str())) return;
4421
17467 by jmarshallnz
changed: long -> int in database functions.
4422
    int iKaraokeNumber = g_advancedSettings.m_karaokeStartIndex;
13479 by gyunaev
Merging with karaoke branch.
4423
4424
    if ( m_pDS->num_rows() == 1 )
17467 by jmarshallnz
changed: long -> int in database functions.
4425
      iKaraokeNumber = m_pDS->fv("MAX(iKaraNumber)").get_asInt() + 1;
13479 by gyunaev
Merging with karaoke branch.
4426
4427
    // Add the data
4428
    strSQL=FormatSQL( "INSERT INTO karaokedata (iKaraNumber, idSong, iKaraDelay, strKaraEncoding, strKaralyrics, strKaraLyrFileCRC) "
4429
        "VALUES( %i, %i, 0, NULL, NULL, '%ul' )", iKaraokeNumber, song.idSong, crc );
4430
4431
    m_pDS->exec(strSQL.c_str());
4432
  }
4433
  catch (...)
4434
  {
4435
    CLog::Log(LOGERROR, "%s -(%s) failed", __FUNCTION__, song.strFileName.c_str());
4436
  }
4437
}
4438
4439
17467 by jmarshallnz
changed: long -> int in database functions.
4440
bool CMusicDatabase::GetSongByKaraokeNumber(int number, CSong & song)
13479 by gyunaev
Merging with karaoke branch.
4441
{
4442
  try
4443
  {
4444
    // Get info from karaoke db
4445
    if (NULL == m_pDB.get()) return false;
4446
    if (NULL == m_pDS.get()) return false;
4447
4448
    CStdString strSQL=FormatSQL("SELECT * FROM karaokedata where iKaraNumber=%ld", number);
4449
4450
    if (!m_pDS->query(strSQL.c_str())) return false;
4451
    if (m_pDS->num_rows() == 0)
4452
    {
4453
      m_pDS->close();
4454
      return false;
4455
    }
4456
17467 by jmarshallnz
changed: long -> int in database functions.
4457
    int idSong = m_pDS->fv("karaokedata.idSong").get_asInt();
13479 by gyunaev
Merging with karaoke branch.
4458
    m_pDS->close();
4459
4460
    return GetSongById( idSong, song );
4461
  }
4462
  catch (...)
4463
  {
17502 by vdrfan
cosmetics: cleanup for the sake of consistency
4464
    CLog::Log(LOGERROR, "%s(%i) failed", __FUNCTION__, number);
13479 by gyunaev
Merging with karaoke branch.
4465
  }
4466
4467
  return false;
4468
}
4469
4470
4471
void CMusicDatabase::ExportKaraokeInfo(const CStdString & outFile, bool asHTML)
4472
{
4473
  try
4474
  {
4475
    if (NULL == m_pDB.get()) return;
4476
    if (NULL == m_pDS.get()) return;
4477
4478
    // find all karaoke songs
4479
    CStdString sql = "SELECT * FROM songview WHERE iKaraNumber > 0 ORDER BY strFileName";
4480
4481
    m_pDS->query(sql.c_str());
4482
4483
    int total = m_pDS->num_rows();
4484
    int current = 0;
4485
4486
    if ( total == 0 )
4487
    {
4488
      m_pDS->close();
4489
      return;
4490
    }
4491
4492
    // Write the document
4493
    XFILE::CFile file;
4494
14465 by jmarshallnz
fixed: Ticket #5525 - Removed the bBinary flag from the CFile::Open calls, which was inconsistent across platforms, and caused FileCurl (among other things) to break on linux. Thanks to mbuchoff for the patch.
4495
    if ( !file.OpenForWrite( outFile, true ) )
13479 by gyunaev
Merging with karaoke branch.
4496
      return;
4497
17685 by jmarshallnz
cosmetic: m_gWindowManager -> g_windowManager for consistency.
4498
    CGUIDialogProgress *progress = (CGUIDialogProgress *)g_windowManager.GetWindow(WINDOW_DIALOG_PROGRESS);
13479 by gyunaev
Merging with karaoke branch.
4499
    if (progress)
4500
    {
4501
      progress->SetHeading(asHTML ? 22034 : 22035);
4502
      progress->SetLine(0, 650);
4503
      progress->SetLine(1, "");
4504
      progress->SetLine(2, "");
4505
      progress->SetPercentage(0);
4506
      progress->StartModal();
4507
      progress->ShowProgressBar(true);
4508
    }
4509
4510
    CStdString outdoc;
4511
    if ( asHTML )
4512
    {
4513
      outdoc = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></meta></head>\n"
4514
          "<body>\n<table>\n";
4515
4516
      file.Write( outdoc, outdoc.size() );
4517
    }
4518
4519
    while (!m_pDS->eof())
4520
    {
4521
      CSong song = GetSongFromDataset( false );
4522
      CStdString songnum;
4523
      songnum.Format( "%06d", song.iKaraokeNumber );
4524
4525
      if ( asHTML )
4526
        outdoc = "<tr><td>" + songnum + "</td><td>" + song.strArtist + "</td><td>" + song.strTitle + "</td></tr>\r\n";
4527
      else
4528
        outdoc = songnum + "\t" + song.strArtist + "\t" + song.strTitle + "\t" + song.strFileName + "\r\n";
4529
4530
      file.Write( outdoc, outdoc.size() );
4531
4532
      if ((current % 50) == 0 && progress)
4533
      {
4534
        progress->SetPercentage(current * 100 / total);
4535
        progress->Progress();
4536
        if (progress->IsCanceled())
4537
        {
4538
          progress->Close();
4539
          m_pDS->close();
4540
          return;
4541
        }
4542
      }
4543
      m_pDS->next();
4544
      current++;
4545
    }
4546
4547
    m_pDS->close();
4548
4549
    if ( asHTML )
4550
    {
4551
      outdoc = "</table>\n</body>\n</html>\n";
4552
      file.Write( outdoc, outdoc.size() );
4553
    }
4554
4555
    file.Close();
4556
4557
    if (progress)
4558
      progress->Close();
4559
  }
4560
  catch (...)
4561
  {
4562
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4563
  }
4564
}
4565
4566
4567
void CMusicDatabase::ImportKaraokeInfo(const CStdString & inputFile)
4568
{
17685 by jmarshallnz
cosmetic: m_gWindowManager -> g_windowManager for consistency.
4569
  CGUIDialogProgress *progress = (CGUIDialogProgress *)g_windowManager.GetWindow(WINDOW_DIALOG_PROGRESS);
13479 by gyunaev
Merging with karaoke branch.
4570
4571
  try
4572
  {
4573
    if (NULL == m_pDB.get()) return;
4574
4575
    XFILE::CFile file;
4576
16547 by jmarshallnz
fixed: The interface change in r18945 was not propogated through the entire source. Fixes truncation of large nfo xml files (eg mymovies.xml) among other things.
4577
    if ( !file.Open( inputFile ) )
13479 by gyunaev
Merging with karaoke branch.
4578
    {
4579
      CLog::Log( LOGERROR, "Cannot open karaoke import file %s", inputFile.c_str() );
4580
      return;
4581
    }
4582
4583
    unsigned int size = (unsigned int) file.GetLength();
4584
4585
    if ( !size )
4586
      return;
4587
4588
    // Read the file into memory array
4589
    std::vector<char> data( size + 1 );
4590
4591
    file.Seek( 0, SEEK_SET );
4592
4593
    // Read the whole file
4594
    if ( file.Read( &data[0], size) != size )
4595
    {
4596
      CLog::Log( LOGERROR, "Cannot read karaoke import file %s", inputFile.c_str() );
4597
      return;
4598
    }
4599
4600
    file.Close();
4601
    data[ size ] = '\0';
4602
4603
    if (progress)
4604
    {
4605
      progress->SetHeading( 22036 );
4606
      progress->SetLine(0, 649);
4607
      progress->SetLine(1, "");
4608
      progress->SetLine(2, "");
4609
      progress->SetPercentage(0);
4610
      progress->StartModal();
4611
      progress->ShowProgressBar(true);
4612
    }
4613
4614
    if (NULL == m_pDS.get()) return;
4615
    BeginTransaction();
4616
4617
    //
4618
    // A simple state machine to parse the file
4619
    //
4620
    char * linestart = &data[0];
4621
    unsigned int offset = 0, lastpercentage = 0;
4622
4623
    for ( char * p = &data[0]; *p; p++, offset++ )
4624
    {
4625
      // Skip \r
4626
      if ( *p == 0x0D )
4627
      {
4628
        *p = '\0';
4629
        continue;
4630
      }
4631
4632
      // Line number
4633
      if ( *p == 0x0A )
4634
      {
4635
        *p = '\0';
4636
4637
        unsigned int tabs = 0;
4638
        char * songpath;
4639
        for ( songpath = linestart; *songpath; songpath++ )
4640
        {
4641
          if ( *songpath == '\t' )
4642
          {
4643
            tabs++;
4644
4645
            if ( tabs == 1 )
4646
              *songpath = '\0'; // terminate number
4647
4648
            if ( tabs == 3 )
4649
            {
4650
              songpath++;
4651
              break; // songpath points to file name
4652
            }
4653
          }
4654
        }
4655
4656
        int num = atoi( linestart );
4657
        if ( num <= 0 || *songpath == '\0' )
4658
        {
4659
          CLog::Log( LOGERROR, "Karaoke import: error in line %s", linestart );
4660
          m_pDS->close();
4661
          return;
4662
        }
4663
4664
        // Update the database
4665
        CSong song;
4666
        if ( GetSongByFileName( songpath, song) )
4667
        {
4668
          CStdString strSQL = FormatSQL("UPDATE karaokedata SET iKaraNumber=%i WHERE idSong=%i", num, song.idSong);
4669
          m_pDS->exec(strSQL.c_str());
4670
        }
4671
        else
4672
        {
4673
          CLog::Log( LOGDEBUG, "Karaoke import: file '%s' was not found in database, skipped", songpath );
4674
        }
4675
4676
        linestart = p + 1;
4677
4678
        if ( progress && (offset * 100 / size) != lastpercentage )
4679
        {
4680
          lastpercentage = offset * 100 / size;
4681
          progress->SetPercentage( lastpercentage);
4682
          progress->Progress();
4683
          if ( progress->IsCanceled() )
4684
          {
4685
            RollbackTransaction();
4686
            progress->Close();
4687
            m_pDS->close();
4688
            return;
4689
          }
4690
        }
4691
      }
4692
    }
4693
    CommitTransaction();
4694
4695
    CLog::Log( LOGNOTICE, "Karaoke import: file '%s' was imported successfully", inputFile.c_str() );
4696
  }
4697
  catch (...)
4698
  {
4699
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4700
  }
4701
4702
  if (progress)
4703
    progress->Close();
4704
}
4705
4706
17467 by jmarshallnz
changed: long -> int in database functions.
4707
bool CMusicDatabase::SetKaraokeSongDelay(int idSong, int delay)
13479 by gyunaev
Merging with karaoke branch.
4708
{
4709
  try
4710
  {
4711
    if (NULL == m_pDB.get()) return false;
4712
    if (NULL == m_pDS.get()) return false;
4713
4714
    CStdString strSQL = FormatSQL("UPDATE karaokedata SET iKaraDelay=%i WHERE idSong=%i", delay, idSong);
4715
    m_pDS->exec(strSQL.c_str());
4716
4717
    return true;
4718
  }
4719
  catch (...)
4720
  {
4721
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4722
  }
4723
4724
  return false;
4725
}
13507 by gyunaev
As requested, the numbering dialogs will not pop up when there are no karaoke songs in the library.
4726
4727
int CMusicDatabase::GetKaraokeSongsCount()
4728
{
4729
  try
4730
  {
4731
    if (NULL == m_pDB.get()) return 0;
4732
    if (NULL == m_pDS.get()) return 0;
4733
4734
    if (!m_pDS->query( "select count(idSong) as NumSongs from karaokedata")) return 0;
4735
    if (m_pDS->num_rows() == 0)
4736
    {
4737
      m_pDS->close();
4738
      return 0;
4739
    }
4740
17467 by jmarshallnz
changed: long -> int in database functions.
4741
    int iNumSongs = m_pDS->fv("NumSongs").get_asInt();
13507 by gyunaev
As requested, the numbering dialogs will not pop up when there are no karaoke songs in the library.
4742
    // cleanup
4743
    m_pDS->close();
4744
    return iNumSongs;
4745
  }
4746
  catch (...)
4747
  {
4748
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4749
  }
4750
  return 0;
4751
}
18356 by spiff_
fixed: album/artist info not available in music visualization
4752
4753
void CMusicDatabase::SetPropertiesFromArtist(CFileItem& item, const CArtist& artist)
4754
{
4755
  item.SetProperty("artist_instrument",artist.strInstruments);
4756
  item.SetProperty("artist_style",artist.strStyles);
4757
  item.SetProperty("artist_mood",artist.strMoods);
4758
  item.SetProperty("artist_born",artist.strBorn);
4759
  item.SetProperty("artist_formed",artist.strFormed);
4760
  item.SetProperty("artist_description",artist.strBiography);
4761
  item.SetProperty("artist_genre",artist.strGenre);
4762
  item.SetProperty("artist_died",artist.strDied);
4763
  item.SetProperty("artist_disbanded",artist.strDisbanded);
4764
  item.SetProperty("artist_yearsactive",artist.strYearsActive);
4765
}
4766
4767
void CMusicDatabase::SetPropertiesFromAlbum(CFileItem& item, const CAlbum& album)
4768
{
4769
  item.SetProperty("album_description", album.strReview);
4770
  item.SetProperty("album_theme", album.strThemes);
4771
  item.SetProperty("album_mood", album.strMoods);
4772
  item.SetProperty("album_style", album.strStyles);
4773
  item.SetProperty("album_type", album.strType);
4774
  item.SetProperty("album_label", album.strLabel);
4775
  if (album.iRating > 0)
4776
    item.SetProperty("album_rating", album.iRating);
4777
}
4778
20031 by spiff_
added: ticket #7902 - set artist/album info properties for songs queued by the party mode manager and the last.fm manager
4779
void CMusicDatabase::SetPropertiesForFileItem(CFileItem& item)
4780
{
4781
  if (!item.HasMusicInfoTag())
4782
    return;
4783
  int idArtist = GetArtistByName(item.GetMusicInfoTag()->GetArtist());
4784
  if (idArtist > -1)
4785
  {
4786
    CArtist artist;
4787
    if (GetArtistInfo(idArtist,artist))
4788
      SetPropertiesFromArtist(item,artist);
4789
  }
4790
  int idAlbum = GetAlbumByName(item.GetMusicInfoTag()->GetAlbum(),
4791
                               item.GetMusicInfoTag()->GetArtist());
4792
  if (idAlbum > -1)
4793
  {
4794
    CAlbum album;
4795
    if (GetAlbumInfo(idAlbum,album,NULL))
4796
      SetPropertiesFromAlbum(item,album);
4797
  }
4798
4799
  CStdString strFanart = item.GetCachedFanart();
4800
  if (XFILE::CFile::Exists(strFanart))
4801
    item.SetProperty("fanart_image",strFanart);
4802
}
4803
18504 by spiff_
changed: hide compilations node if we have no compilations
4804
int CMusicDatabase::GetVariousArtistsAlbumsCount()
4805
{
4806
  CStdString strVariousArtists = g_localizeStrings.Get(340);
4807
  int idVariousArtists=AddArtist(strVariousArtists);
4808
  CStdString strSQL = FormatSQL("select count(idAlbum) from album where idArtist=%i", idVariousArtists);
4809
  int result=0;
4810
  try
4811
  {
4812
    if (NULL == m_pDB.get()) return 0;
4813
    if (NULL == m_pDS.get()) return 0;
4814
    m_pDS->query(strSQL.c_str());
4815
    if (!m_pDS->eof())
4816
      result = m_pDS->fv(0).get_asInt();
19798 by spiff_
cosmetics
4817
    m_pDS->close();
18504 by spiff_
changed: hide compilations node if we have no compilations
4818
  }
4819
  catch(...)
4820
  {
4821
    CLog::Log(LOGERROR, "%s failed", __FUNCTION__);
4822
  }
4823
4824
  return result;
4825
}