This repository has been archived by the owner on Apr 15, 2023. It is now read-only.
/
AnnouncementManager.cpp
195 lines (168 loc) · 6.07 KB
/
AnnouncementManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright (C) 2005-2010 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "AnnouncementManager.h"
#include "threads/SingleLock.h"
#include <stdio.h>
#include "utils/log.h"
#include "utils/Variant.h"
#include "FileItem.h"
#include "music/tags/MusicInfoTag.h"
#include "music/MusicDatabase.h"
#include "video/VideoDatabase.h"
#define LOOKUP_PROPERTY "database-lookup"
using namespace std;
using namespace ANNOUNCEMENT;
CCriticalSection CAnnouncementManager::m_critSection;
vector<IAnnouncer *> CAnnouncementManager::m_announcers;
void CAnnouncementManager::AddAnnouncer(IAnnouncer *listener)
{
if (!listener)
return;
CSingleLock lock (m_critSection);
m_announcers.push_back(listener);
}
void CAnnouncementManager::RemoveAnnouncer(IAnnouncer *listener)
{
if (!listener)
return;
CSingleLock lock (m_critSection);
for (unsigned int i = 0; i < m_announcers.size(); i++)
{
if (m_announcers[i] == listener)
{
m_announcers.erase(m_announcers.begin() + i);
return;
}
}
}
void CAnnouncementManager::Announce(EAnnouncementFlag flag, const char *sender, const char *message)
{
CVariant data;
Announce(flag, sender, message, data);
}
void CAnnouncementManager::Announce(EAnnouncementFlag flag, const char *sender, const char *message, CVariant &data)
{
CLog::Log(LOGDEBUG, "CAnnouncementManager - Announcement: %s from %s", message, sender);
CSingleLock lock (m_critSection);
for (unsigned int i = 0; i < m_announcers.size(); i++)
m_announcers[i]->Announce(flag, sender, message, data);
}
void CAnnouncementManager::Announce(EAnnouncementFlag flag, const char *sender, const char *message, CFileItemPtr item)
{
CVariant data;
Announce(flag, sender, message, item, data);
}
void CAnnouncementManager::Announce(EAnnouncementFlag flag, const char *sender, const char *message, CFileItemPtr item, CVariant &data)
{
if (!item.get())
{
Announce(flag, sender, message, data);
return;
}
// Extract db id of item
CVariant object = data.isNull() || data.isObject() ? data : CVariant::VariantTypeObject;
CStdString type;
int id = 0;
if (item->HasVideoInfoTag())
{
id = item->GetVideoInfoTag()->m_iDbId;
// TODO: Can be removed once this is properly handled when starting playback of a file
if (id <= 0 && !item->GetPath().empty() &&
(!item->HasProperty(LOOKUP_PROPERTY) || item->GetProperty(LOOKUP_PROPERTY).asBoolean()))
{
CVideoDatabase videodatabase;
if (videodatabase.Open())
{
if (videodatabase.LoadVideoInfo(item->GetPath(), *item->GetVideoInfoTag()))
id = item->GetVideoInfoTag()->m_iDbId;
videodatabase.Close();
}
}
CVideoDatabase::VideoContentTypeToString((VIDEODB_CONTENT_TYPE)item->GetVideoContentType(), type);
if (id <= 0)
{
// TODO: Can be removed once this is properly handled when starting playback of a file
item->SetProperty(LOOKUP_PROPERTY, false);
object["title"] = item->GetVideoInfoTag()->m_strTitle;
switch (item->GetVideoContentType())
{
case VIDEODB_CONTENT_MOVIES:
if (item->GetVideoInfoTag()->m_iYear > 0)
object["year"] = item->GetVideoInfoTag()->m_iYear;
break;
case VIDEODB_CONTENT_EPISODES:
if (item->GetVideoInfoTag()->m_iEpisode >= 0)
object["episode"] = item->GetVideoInfoTag()->m_iEpisode;
if (item->GetVideoInfoTag()->m_iSeason >= 0)
object["season"] = item->GetVideoInfoTag()->m_iSeason;
if (!item->GetVideoInfoTag()->m_strShowTitle.empty())
object["showtitle"] = item->GetVideoInfoTag()->m_strShowTitle;
break;
case VIDEODB_CONTENT_MUSICVIDEOS:
if (!item->GetVideoInfoTag()->m_strAlbum.empty())
object["album"] = item->GetVideoInfoTag()->m_strAlbum;
if (!item->GetVideoInfoTag()->m_strArtist.empty())
object["artist"] = item->GetVideoInfoTag()->m_strArtist;
break;
}
}
}
else if (item->HasMusicInfoTag())
{
id = item->GetMusicInfoTag()->GetDatabaseId();
type = "song";
// TODO: Can be removed once this is properly handled when starting playback of a file
if (id <= 0 && !item->GetPath().empty() &&
(!item->HasProperty(LOOKUP_PROPERTY) || item->GetProperty(LOOKUP_PROPERTY).asBoolean()))
{
CMusicDatabase musicdatabase;
if (musicdatabase.Open())
{
CSong song;
if (musicdatabase.GetSongByFileName(item->GetPath(), song))
{
item->GetMusicInfoTag()->SetSong(song);
item->SetMusicThumb();
id = item->GetMusicInfoTag()->GetDatabaseId();
}
musicdatabase.Close();
}
}
if (id <= 0)
{
// TODO: Can be removed once this is properly handled when starting playback of a file
item->SetProperty(LOOKUP_PROPERTY, false);
object["title"] = item->GetMusicInfoTag()->GetTitle();
if (item->GetMusicInfoTag()->GetTrackNumber() > 0)
object["track"] = item->GetMusicInfoTag()->GetTrackNumber();
if (!item->GetMusicInfoTag()->GetAlbum().empty())
object["album"] = item->GetMusicInfoTag()->GetAlbum();
if (!item->GetMusicInfoTag()->GetArtist().empty())
object["artist"] = item->GetMusicInfoTag()->GetArtist();
}
}
else
type = "unknown";
object["item"]["type"] = type;
if (id > 0)
object["item"]["id"] = id;
Announce(flag, sender, message, object);
}