2 * Copyright (C) 2013 Team XBMC
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)
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.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
23 #include "MediaSettings.h"
24 #include "Application.h"
26 #include "dialogs/GUIDialogContextMenu.h"
27 #include "dialogs/GUIDialogFileBrowser.h"
28 #include "dialogs/GUIDialogYesNo.h"
29 #include "guilib/WindowIDs.h"
30 #include "interfaces/Builtins.h"
31 #include "music/MusicDatabase.h"
32 #include "profiles/ProfilesManager.h"
33 #include "settings/Setting.h"
34 #include "storage/MediaManager.h"
35 #include "threads/SingleLock.h"
36 #include "utils/log.h"
37 #include "utils/URIUtils.h"
38 #include "utils/XBMCTinyXML.h"
39 #include "utils/XMLUtils.h"
40 #include "video/VideoDatabase.h"
44 CMediaSettings::CMediaSettings()
46 m_watchedModes["movies"] = WatchedModeAll;
47 m_watchedModes["tvshows"] = WatchedModeAll;
48 m_watchedModes["musicvideos"] = WatchedModeAll;
50 m_musicPlaylistRepeat = false;
51 m_musicPlaylistShuffle = false;
52 m_videoPlaylistRepeat = false;
53 m_videoPlaylistShuffle = false;
55 m_videoStartWindowed = false;
56 m_additionalSubtitleDirectoryChecked = 0;
58 m_musicNeedsUpdate = 0;
59 m_videoNeedsUpdate = 0;
62 CMediaSettings::~CMediaSettings()
65 CMediaSettings& CMediaSettings::Get()
67 static CMediaSettings sMediaSettings;
68 return sMediaSettings;
71 bool CMediaSettings::Load(const TiXmlNode *settings)
76 CSingleLock lock(m_critical);
77 const TiXmlElement *pElement = settings->FirstChildElement("defaultvideosettings");
81 bool deinterlaceModePresent = XMLUtils::GetInt(pElement, "deinterlacemode", deinterlaceMode, VS_DEINTERLACEMODE_OFF, VS_DEINTERLACEMODE_FORCE);
83 bool interlaceMethodPresent = XMLUtils::GetInt(pElement, "interlacemethod", interlaceMethod, VS_INTERLACEMETHOD_AUTO, VS_INTERLACEMETHOD_MAX);
84 // For smooth conversion of settings stored before the deinterlaceMode existed
85 if (!deinterlaceModePresent && interlaceMethodPresent)
87 if (interlaceMethod == VS_INTERLACEMETHOD_NONE)
89 deinterlaceMode = VS_DEINTERLACEMODE_OFF;
90 interlaceMethod = VS_INTERLACEMETHOD_AUTO;
92 else if (interlaceMethod == VS_INTERLACEMETHOD_AUTO)
93 deinterlaceMode = VS_DEINTERLACEMODE_AUTO;
95 deinterlaceMode = VS_DEINTERLACEMODE_FORCE;
97 m_defaultVideoSettings.m_DeinterlaceMode = (EDEINTERLACEMODE)deinterlaceMode;
98 m_defaultVideoSettings.m_InterlaceMethod = (EINTERLACEMETHOD)interlaceMethod;
100 if (!XMLUtils::GetInt(pElement, "scalingmethod", scalingMethod, VS_SCALINGMETHOD_NEAREST, VS_SCALINGMETHOD_MAX))
101 scalingMethod = (int)VS_SCALINGMETHOD_LINEAR;
102 m_defaultVideoSettings.m_ScalingMethod = (ESCALINGMETHOD)scalingMethod;
104 XMLUtils::GetInt(pElement, "viewmode", m_defaultVideoSettings.m_ViewMode, ViewModeNormal, ViewModeCustom);
105 if (!XMLUtils::GetFloat(pElement, "zoomamount", m_defaultVideoSettings.m_CustomZoomAmount, 0.5f, 2.0f))
106 m_defaultVideoSettings.m_CustomZoomAmount = 1.0f;
107 if (!XMLUtils::GetFloat(pElement, "pixelratio", m_defaultVideoSettings.m_CustomPixelRatio, 0.5f, 2.0f))
108 m_defaultVideoSettings.m_CustomPixelRatio = 1.0f;
109 if (!XMLUtils::GetFloat(pElement, "verticalshift", m_defaultVideoSettings.m_CustomVerticalShift, -2.0f, 2.0f))
110 m_defaultVideoSettings.m_CustomVerticalShift = 0.0f;
111 if (!XMLUtils::GetFloat(pElement, "volumeamplification", m_defaultVideoSettings.m_VolumeAmplification, VOLUME_DRC_MINIMUM * 0.01f, VOLUME_DRC_MAXIMUM * 0.01f))
112 m_defaultVideoSettings.m_VolumeAmplification = VOLUME_DRC_MINIMUM * 0.01f;
113 if (!XMLUtils::GetFloat(pElement, "noisereduction", m_defaultVideoSettings.m_NoiseReduction, 0.0f, 1.0f))
114 m_defaultVideoSettings.m_NoiseReduction = 0.0f;
115 XMLUtils::GetBoolean(pElement, "postprocess", m_defaultVideoSettings.m_PostProcess);
116 if (!XMLUtils::GetFloat(pElement, "sharpness", m_defaultVideoSettings.m_Sharpness, -1.0f, 1.0f))
117 m_defaultVideoSettings.m_Sharpness = 0.0f;
118 XMLUtils::GetBoolean(pElement, "outputtoallspeakers", m_defaultVideoSettings.m_OutputToAllSpeakers);
119 XMLUtils::GetBoolean(pElement, "showsubtitles", m_defaultVideoSettings.m_SubtitleOn);
120 if (!XMLUtils::GetFloat(pElement, "brightness", m_defaultVideoSettings.m_Brightness, 0, 100))
121 m_defaultVideoSettings.m_Brightness = 50;
122 if (!XMLUtils::GetFloat(pElement, "contrast", m_defaultVideoSettings.m_Contrast, 0, 100))
123 m_defaultVideoSettings.m_Contrast = 50;
124 if (!XMLUtils::GetFloat(pElement, "gamma", m_defaultVideoSettings.m_Gamma, 0, 100))
125 m_defaultVideoSettings.m_Gamma = 20;
126 if (!XMLUtils::GetFloat(pElement, "audiodelay", m_defaultVideoSettings.m_AudioDelay, -10.0f, 10.0f))
127 m_defaultVideoSettings.m_AudioDelay = 0.0f;
128 if (!XMLUtils::GetFloat(pElement, "subtitledelay", m_defaultVideoSettings.m_SubtitleDelay, -10.0f, 10.0f))
129 m_defaultVideoSettings.m_SubtitleDelay = 0.0f;
130 XMLUtils::GetBoolean(pElement, "autocrop", m_defaultVideoSettings.m_Crop);
131 XMLUtils::GetBoolean(pElement, "nonlinstretch", m_defaultVideoSettings.m_CustomNonLinStretch);
133 m_defaultVideoSettings.m_SubtitleCached = false;
137 pElement = settings->FirstChildElement("mymusic");
138 if (pElement != NULL)
140 const TiXmlElement *pChild = pElement->FirstChildElement("playlist");
143 XMLUtils::GetBoolean(pChild, "repeat", m_musicPlaylistRepeat);
144 XMLUtils::GetBoolean(pChild, "shuffle", m_musicPlaylistShuffle);
146 if (!XMLUtils::GetInt(pElement, "needsupdate", m_musicNeedsUpdate, 0, INT_MAX))
147 m_musicNeedsUpdate = 0;
150 // Read the watchmode settings for the various media views
151 pElement = settings->FirstChildElement("myvideos");
152 if (pElement != NULL)
155 if (XMLUtils::GetInt(pElement, "watchmodemovies", tmp, (int)WatchedModeAll, (int)WatchedModeWatched))
156 m_watchedModes["movies"] = (WatchedMode)tmp;
157 if (XMLUtils::GetInt(pElement, "watchmodetvshows", tmp, (int)WatchedModeAll, (int)WatchedModeWatched))
158 m_watchedModes["tvshows"] = (WatchedMode)tmp;
159 if (XMLUtils::GetInt(pElement, "watchmodemusicvideos", tmp, (int)WatchedModeAll, (int)WatchedModeWatched))
160 m_watchedModes["musicvideos"] = (WatchedMode)tmp;
162 const TiXmlElement *pChild = pElement->FirstChildElement("playlist");
165 XMLUtils::GetBoolean(pChild, "repeat", m_videoPlaylistRepeat);
166 XMLUtils::GetBoolean(pChild, "shuffle", m_videoPlaylistShuffle);
168 if (!XMLUtils::GetInt(pElement, "needsupdate", m_videoNeedsUpdate, 0, INT_MAX))
169 m_videoNeedsUpdate = 0;
175 bool CMediaSettings::Save(TiXmlNode *settings) const
177 if (settings == NULL)
180 CSingleLock lock(m_critical);
181 // default video settings
182 TiXmlElement videoSettingsNode("defaultvideosettings");
183 TiXmlNode *pNode = settings->InsertEndChild(videoSettingsNode);
187 XMLUtils::SetInt(pNode, "deinterlacemode", m_defaultVideoSettings.m_DeinterlaceMode);
188 XMLUtils::SetInt(pNode, "interlacemethod", m_defaultVideoSettings.m_InterlaceMethod);
189 XMLUtils::SetInt(pNode, "scalingmethod", m_defaultVideoSettings.m_ScalingMethod);
190 XMLUtils::SetFloat(pNode, "noisereduction", m_defaultVideoSettings.m_NoiseReduction);
191 XMLUtils::SetBoolean(pNode, "postprocess", m_defaultVideoSettings.m_PostProcess);
192 XMLUtils::SetFloat(pNode, "sharpness", m_defaultVideoSettings.m_Sharpness);
193 XMLUtils::SetInt(pNode, "viewmode", m_defaultVideoSettings.m_ViewMode);
194 XMLUtils::SetFloat(pNode, "zoomamount", m_defaultVideoSettings.m_CustomZoomAmount);
195 XMLUtils::SetFloat(pNode, "pixelratio", m_defaultVideoSettings.m_CustomPixelRatio);
196 XMLUtils::SetFloat(pNode, "verticalshift", m_defaultVideoSettings.m_CustomVerticalShift);
197 XMLUtils::SetFloat(pNode, "volumeamplification", m_defaultVideoSettings.m_VolumeAmplification);
198 XMLUtils::SetBoolean(pNode, "outputtoallspeakers", m_defaultVideoSettings.m_OutputToAllSpeakers);
199 XMLUtils::SetBoolean(pNode, "showsubtitles", m_defaultVideoSettings.m_SubtitleOn);
200 XMLUtils::SetFloat(pNode, "brightness", m_defaultVideoSettings.m_Brightness);
201 XMLUtils::SetFloat(pNode, "contrast", m_defaultVideoSettings.m_Contrast);
202 XMLUtils::SetFloat(pNode, "gamma", m_defaultVideoSettings.m_Gamma);
203 XMLUtils::SetFloat(pNode, "audiodelay", m_defaultVideoSettings.m_AudioDelay);
204 XMLUtils::SetFloat(pNode, "subtitledelay", m_defaultVideoSettings.m_SubtitleDelay);
205 XMLUtils::SetBoolean(pNode, "autocrop", m_defaultVideoSettings.m_Crop);
206 XMLUtils::SetBoolean(pNode, "nonlinstretch", m_defaultVideoSettings.m_CustomNonLinStretch);
209 pNode = settings->FirstChild("mymusic");
212 TiXmlElement videosNode("mymusic");
213 pNode = settings->InsertEndChild(videosNode);
218 TiXmlElement musicPlaylistNode("playlist");
219 TiXmlNode *playlistNode = pNode->InsertEndChild(musicPlaylistNode);
220 if (playlistNode == NULL)
222 XMLUtils::SetBoolean(playlistNode, "repeat", m_musicPlaylistRepeat);
223 XMLUtils::SetBoolean(playlistNode, "shuffle", m_musicPlaylistShuffle);
225 XMLUtils::SetInt(pNode, "needsupdate", m_musicNeedsUpdate);
228 pNode = settings->FirstChild("myvideos");
231 TiXmlElement videosNode("myvideos");
232 pNode = settings->InsertEndChild(videosNode);
237 XMLUtils::SetInt(pNode, "watchmodemovies", m_watchedModes.find("movies")->second);
238 XMLUtils::SetInt(pNode, "watchmodetvshows", m_watchedModes.find("tvshows")->second);
239 XMLUtils::SetInt(pNode, "watchmodemusicvideos", m_watchedModes.find("musicvideos")->second);
241 TiXmlElement videoPlaylistNode("playlist");
242 playlistNode = pNode->InsertEndChild(videoPlaylistNode);
243 if (playlistNode == NULL)
245 XMLUtils::SetBoolean(playlistNode, "repeat", m_videoPlaylistRepeat);
246 XMLUtils::SetBoolean(playlistNode, "shuffle", m_videoPlaylistShuffle);
248 XMLUtils::SetInt(pNode, "needsupdate", m_videoNeedsUpdate);
253 void CMediaSettings::OnSettingAction(const CSetting *setting)
258 const std::string &settingId = setting->GetId();
259 if (settingId == "karaoke.export")
261 CContextButtons choices;
262 choices.Add(1, g_localizeStrings.Get(22034));
263 choices.Add(2, g_localizeStrings.Get(22035));
265 int retVal = CGUIDialogContextMenu::ShowAndGetChoice(choices);
268 CStdString path(CProfilesManager::Get().GetDatabaseFolder());
270 g_mediaManager.GetLocalDrives(shares);
271 if (CGUIDialogFileBrowser::ShowAndGetDirectory(shares, g_localizeStrings.Get(661), path, true))
273 CMusicDatabase musicdatabase;
274 musicdatabase.Open();
278 path = URIUtils::AddFileToFolder(path, "karaoke.html");
279 musicdatabase.ExportKaraokeInfo( path, true );
283 path = URIUtils::AddFileToFolder(path, "karaoke.csv");
284 musicdatabase.ExportKaraokeInfo( path, false );
286 musicdatabase.Close();
290 else if (settingId == "karaoke.importcsv")
292 CStdString path(CProfilesManager::Get().GetDatabaseFolder());
294 g_mediaManager.GetLocalDrives(shares);
295 if (CGUIDialogFileBrowser::ShowAndGetFile(shares, "karaoke.csv", g_localizeStrings.Get(651) , path))
297 CMusicDatabase musicdatabase;
298 musicdatabase.Open();
299 musicdatabase.ImportKaraokeInfo(path);
300 musicdatabase.Close();
303 else if (settingId == "musiclibrary.cleanup")
305 CMusicDatabase musicdatabase;
306 musicdatabase.Clean();
307 CUtil::DeleteMusicDatabaseDirectoryCache();
309 else if (settingId == "musiclibrary.export")
310 CBuiltins::Execute("exportlibrary(music)");
311 else if (settingId == "musiclibrary.import")
315 g_mediaManager.GetLocalDrives(shares);
316 if (CGUIDialogFileBrowser::ShowAndGetFile(shares, "musicdb.xml", g_localizeStrings.Get(651) , path))
318 CMusicDatabase musicdatabase;
319 musicdatabase.Open();
320 musicdatabase.ImportFromXML(path);
321 musicdatabase.Close();
324 else if (settingId == "videolibrary.cleanup")
326 if (CGUIDialogYesNo::ShowAndGetInput(313, 333, 0, 0))
327 g_application.StartVideoCleanup();
329 else if (settingId == "videolibrary.export")
330 CBuiltins::Execute("exportlibrary(video)");
331 else if (settingId == "videolibrary.import")
335 g_mediaManager.GetLocalDrives(shares);
336 if (CGUIDialogFileBrowser::ShowAndGetDirectory(shares, g_localizeStrings.Get(651) , path))
338 CVideoDatabase videodatabase;
339 videodatabase.Open();
340 videodatabase.ImportFromXML(path);
341 videodatabase.Close();
346 int CMediaSettings::GetWatchedMode(const std::string &content) const
348 CSingleLock lock(m_critical);
349 WatchedModes::const_iterator it = m_watchedModes.find(GetWatchedContent(content));
350 if (it != m_watchedModes.end())
353 return WatchedModeAll;
356 void CMediaSettings::SetWatchedMode(const std::string &content, WatchedMode mode)
358 CSingleLock lock(m_critical);
359 WatchedModes::iterator it = m_watchedModes.find(GetWatchedContent(content));
360 if (it != m_watchedModes.end())
364 void CMediaSettings::CycleWatchedMode(const std::string &content)
366 CSingleLock lock(m_critical);
367 WatchedModes::iterator it = m_watchedModes.find(GetWatchedContent(content));
368 if (it != m_watchedModes.end())
370 it->second = (WatchedMode)((int)it->second + 1);
371 if (it->second > WatchedModeWatched)
372 it->second = WatchedModeAll;
376 std::string CMediaSettings::GetWatchedContent(const std::string &content)
378 if (content == "seasons" || content == "episodes")