Merge pull request #2634 from FlyingRat/patch-0028-cwd-updated
[vuplus_xbmc] / xbmc / settings / MediaSettings.cpp
1 /*
2  *      Copyright (C) 2013 Team XBMC
3  *      http://www.xbmc.org
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
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include <limits.h>
22
23 #include "MediaSettings.h"
24 #include "Application.h"
25 #include "Util.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"
41
42 using namespace std;
43
44 CMediaSettings::CMediaSettings()
45 {
46   m_watchedModes["movies"] = WatchedModeAll;
47   m_watchedModes["tvshows"] = WatchedModeAll;
48   m_watchedModes["musicvideos"] = WatchedModeAll;
49
50   m_musicPlaylistRepeat = false;
51   m_musicPlaylistShuffle = false;
52   m_videoPlaylistRepeat = false;
53   m_videoPlaylistShuffle = false;
54
55   m_videoStartWindowed = false;
56   m_additionalSubtitleDirectoryChecked = 0;
57
58   m_musicNeedsUpdate = 0;
59   m_musicNeedsUpdate = 0;
60 }
61
62 CMediaSettings::~CMediaSettings()
63 { }
64
65 CMediaSettings& CMediaSettings::Get()
66 {
67   static CMediaSettings sMediaSettings;
68   return sMediaSettings;
69 }
70
71 bool CMediaSettings::Load(const TiXmlNode *settings)
72 {
73   if (settings == NULL)
74     return false;
75
76   CSingleLock lock(m_critical);
77   const TiXmlElement *pElement = settings->FirstChildElement("defaultvideosettings");
78   if (pElement != NULL)
79   {
80     int deinterlaceMode;
81     bool deinterlaceModePresent = XMLUtils::GetInt(pElement, "deinterlacemode", deinterlaceMode, VS_DEINTERLACEMODE_OFF, VS_DEINTERLACEMODE_FORCE);
82     int interlaceMethod;
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)
86     {
87       if (interlaceMethod == VS_INTERLACEMETHOD_NONE)
88       {
89         deinterlaceMode = VS_DEINTERLACEMODE_OFF;
90         interlaceMethod = VS_INTERLACEMETHOD_AUTO;
91       }
92       else if (interlaceMethod == VS_INTERLACEMETHOD_AUTO)
93         deinterlaceMode = VS_DEINTERLACEMODE_AUTO;
94       else
95         deinterlaceMode = VS_DEINTERLACEMODE_FORCE;
96     }
97     m_defaultVideoSettings.m_DeinterlaceMode = (EDEINTERLACEMODE)deinterlaceMode;
98     m_defaultVideoSettings.m_InterlaceMethod = (EINTERLACEMETHOD)interlaceMethod;
99     int scalingMethod;
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;
103
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);
132
133     m_defaultVideoSettings.m_SubtitleCached = false;
134   }
135
136   // mymusic settings
137   pElement = settings->FirstChildElement("mymusic");
138   if (pElement != NULL)
139   {
140     const TiXmlElement *pChild = pElement->FirstChildElement("playlist");
141     if (pChild != NULL)
142     {
143       XMLUtils::GetBoolean(pChild, "repeat", m_musicPlaylistRepeat);
144       XMLUtils::GetBoolean(pChild, "shuffle", m_musicPlaylistShuffle);
145     }
146     if (!XMLUtils::GetInt(pElement, "needsupdate", m_musicNeedsUpdate, 0, INT_MAX))
147       m_musicNeedsUpdate = 0;
148   }
149   
150   // Read the watchmode settings for the various media views
151   pElement = settings->FirstChildElement("myvideos");
152   if (pElement != NULL)
153   {
154     int tmp;
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;
161
162     const TiXmlElement *pChild = pElement->FirstChildElement("playlist");
163     if (pChild != NULL)
164     {
165       XMLUtils::GetBoolean(pChild, "repeat", m_videoPlaylistRepeat);
166       XMLUtils::GetBoolean(pChild, "shuffle", m_videoPlaylistShuffle);
167     }
168     if (!XMLUtils::GetInt(pElement, "needsupdate", m_videoNeedsUpdate, 0, INT_MAX))
169       m_videoNeedsUpdate = 0;
170   }
171
172   return true;
173 }
174
175 bool CMediaSettings::Save(TiXmlNode *settings) const
176 {
177   if (settings == NULL)
178     return false;
179
180   CSingleLock lock(m_critical);
181   // default video settings
182   TiXmlElement videoSettingsNode("defaultvideosettings");
183   TiXmlNode *pNode = settings->InsertEndChild(videoSettingsNode);
184   if (pNode == NULL)
185     return false;
186
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);
207
208   // mymusic
209   pNode = settings->FirstChild("mymusic");
210   if (pNode == NULL)
211   {
212     TiXmlElement videosNode("mymusic");
213     pNode = settings->InsertEndChild(videosNode);
214     if (pNode == NULL)
215       return false;
216   }
217
218   TiXmlElement musicPlaylistNode("playlist");
219   TiXmlNode *playlistNode = pNode->InsertEndChild(musicPlaylistNode);
220   if (playlistNode == NULL)
221     return false;
222   XMLUtils::SetBoolean(playlistNode, "repeat", m_musicPlaylistRepeat);
223   XMLUtils::SetBoolean(playlistNode, "shuffle", m_musicPlaylistShuffle);
224
225   XMLUtils::SetInt(pNode, "needsupdate", m_musicNeedsUpdate);
226
227   // myvideos
228   pNode = settings->FirstChild("myvideos");
229   if (pNode == NULL)
230   {
231     TiXmlElement videosNode("myvideos");
232     pNode = settings->InsertEndChild(videosNode);
233     if (pNode == NULL)
234       return false;
235   }
236
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);
240
241   TiXmlElement videoPlaylistNode("playlist");
242   playlistNode = pNode->InsertEndChild(videoPlaylistNode);
243   if (playlistNode == NULL)
244     return false;
245   XMLUtils::SetBoolean(playlistNode, "repeat", m_videoPlaylistRepeat);
246   XMLUtils::SetBoolean(playlistNode, "shuffle", m_videoPlaylistShuffle);
247
248   XMLUtils::SetInt(pNode, "needsupdate", m_videoNeedsUpdate);
249
250   return true;
251 }
252
253 void CMediaSettings::OnSettingAction(const CSetting *setting)
254 {
255   if (setting == NULL)
256     return;
257
258   const std::string &settingId = setting->GetId();
259   if (settingId == "karaoke.export")
260   {
261     CContextButtons choices;
262     choices.Add(1, g_localizeStrings.Get(22034));
263     choices.Add(2, g_localizeStrings.Get(22035));
264
265     int retVal = CGUIDialogContextMenu::ShowAndGetChoice(choices);
266     if ( retVal > 0 )
267     {
268       CStdString path(CProfilesManager::Get().GetDatabaseFolder());
269       VECSOURCES shares;
270       g_mediaManager.GetLocalDrives(shares);
271       if (CGUIDialogFileBrowser::ShowAndGetDirectory(shares, g_localizeStrings.Get(661), path, true))
272       {
273         CMusicDatabase musicdatabase;
274         musicdatabase.Open();
275
276         if ( retVal == 1 )
277         {
278           URIUtils::AddFileToFolder(path, "karaoke.html", path);
279           musicdatabase.ExportKaraokeInfo( path, true );
280         }
281         else
282         {
283           URIUtils::AddFileToFolder(path, "karaoke.csv", path);
284           musicdatabase.ExportKaraokeInfo( path, false );
285         }
286         musicdatabase.Close();
287       }
288     }
289   }
290   else if (settingId == "karaoke.importcsv")
291   {
292     CStdString path(CProfilesManager::Get().GetDatabaseFolder());
293     VECSOURCES shares;
294     g_mediaManager.GetLocalDrives(shares);
295     if (CGUIDialogFileBrowser::ShowAndGetFile(shares, "karaoke.csv", g_localizeStrings.Get(651) , path))
296     {
297       CMusicDatabase musicdatabase;
298       musicdatabase.Open();
299       musicdatabase.ImportKaraokeInfo(path);
300       musicdatabase.Close();
301     }
302   }
303   else if (settingId == "musiclibrary.cleanup")
304   {
305     CMusicDatabase musicdatabase;
306     musicdatabase.Clean();
307     CUtil::DeleteMusicDatabaseDirectoryCache();
308   }
309   else if (settingId == "musiclibrary.export")
310     CBuiltins::Execute("exportlibrary(music)");
311   else if (settingId == "musiclibrary.import")
312   {
313     CStdString path;
314     VECSOURCES shares;
315     g_mediaManager.GetLocalDrives(shares);
316     if (CGUIDialogFileBrowser::ShowAndGetFile(shares, "musicdb.xml", g_localizeStrings.Get(651) , path))
317     {
318       CMusicDatabase musicdatabase;
319       musicdatabase.Open();
320       musicdatabase.ImportFromXML(path);
321       musicdatabase.Close();
322     }
323   }
324   else if (settingId == "videolibrary.cleanup")
325   {
326     if (CGUIDialogYesNo::ShowAndGetInput(313, 333, 0, 0))
327       g_application.StartVideoCleanup();
328   }
329   else if (settingId == "videolibrary.export")
330     CBuiltins::Execute("exportlibrary(video)");
331   else if (settingId == "videolibrary.import")
332   {
333     CStdString path;
334     VECSOURCES shares;
335     g_mediaManager.GetLocalDrives(shares);
336     if (CGUIDialogFileBrowser::ShowAndGetDirectory(shares, g_localizeStrings.Get(651) , path))
337     {
338       CVideoDatabase videodatabase;
339       videodatabase.Open();
340       videodatabase.ImportFromXML(path);
341       videodatabase.Close();
342     }
343   }
344 }
345
346 int CMediaSettings::GetWatchedMode(const std::string &content) const
347 {
348   CSingleLock lock(m_critical);
349   WatchedModes::const_iterator it = m_watchedModes.find(GetWatchedContent(content));
350   if (it != m_watchedModes.end())
351     return it->second;
352
353   return WatchedModeAll;
354 }
355
356 void CMediaSettings::SetWatchedMode(const std::string &content, WatchedMode mode)
357 {
358   CSingleLock lock(m_critical);
359   WatchedModes::iterator it = m_watchedModes.find(GetWatchedContent(content));
360   if (it != m_watchedModes.end())
361     it->second = mode;
362 }
363
364 void CMediaSettings::CycleWatchedMode(const std::string &content)
365 {
366   CSingleLock lock(m_critical);
367   WatchedModes::iterator it = m_watchedModes.find(GetWatchedContent(content));
368   if (it != m_watchedModes.end())
369   {
370     it->second = (WatchedMode)((int)it->second + 1);
371     if (it->second > WatchedModeWatched)
372       it->second = WatchedModeAll;
373   }
374 }
375
376 std::string CMediaSettings::GetWatchedContent(const std::string &content)
377 {
378   if (content == "seasons" || content == "episodes")
379     return "tvshows";
380
381   return content;
382 }