Merge pull request #3819 from arnova/subtitles_for_stacks
[vuplus_xbmc] / xbmc / windows / GUIWindowHome.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://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 "GUIWindowHome.h"
22 #include "guilib/Key.h"
23 #include "guilib/WindowIDs.h"
24 #include "utils/JobManager.h"
25 #include "utils/RecentlyAddedJob.h"
26 #include "interfaces/AnnouncementManager.h"
27 #include "utils/log.h"
28 #include "settings/AdvancedSettings.h"
29 #include "utils/Variant.h"
30 #include "guilib/GUIWindowManager.h"
31 #include "Application.h"
32
33 using namespace ANNOUNCEMENT;
34
35 CGUIWindowHome::CGUIWindowHome(void) : CGUIWindow(WINDOW_HOME, "Home.xml"), 
36                                        m_recentlyAddedRunning(false),
37                                        m_cumulativeUpdateFlag(0),
38                                        m_dbUpdating(false)
39 {
40   m_updateRA = (Audio | Video | Totals);
41   m_loadType = KEEP_IN_MEMORY;
42   
43   CAnnouncementManager::AddAnnouncer(this);
44 }
45
46 CGUIWindowHome::~CGUIWindowHome(void)
47 {
48   CAnnouncementManager::RemoveAnnouncer(this);
49 }
50
51 bool CGUIWindowHome::OnAction(const CAction &action)
52 {
53   static unsigned int min_hold_time = 1000;
54   if (action.GetID() == ACTION_NAV_BACK &&
55       action.GetHoldTime() < min_hold_time &&
56       g_application.m_pPlayer->IsPlaying())
57   {
58     g_application.SwitchToFullScreen();
59     return true;
60   }
61   return CGUIWindow::OnAction(action);
62 }
63
64 void CGUIWindowHome::OnInitWindow()
65 {  
66   // for shared databases (ie mysql) always force an update on return to home
67   // this is a temporary solution until remote announcements can be delivered
68   if ( g_advancedSettings.m_databaseVideo.type.Equals("mysql") ||
69        g_advancedSettings.m_databaseMusic.type.Equals("mysql") )
70     m_updateRA = (Audio | Video | Totals);
71   AddRecentlyAddedJobs( m_updateRA );
72
73   CGUIWindow::OnInitWindow();
74 }
75
76 void CGUIWindowHome::Announce(AnnouncementFlag flag, const char *sender, const char *message, const CVariant &data)
77 {
78   int ra_flag = 0;
79
80   CLog::Log(LOGDEBUG, "GOT ANNOUNCEMENT, type: %i, from %s, message %s",(int)flag, sender, message);
81
82   // we are only interested in library changes
83   if ((flag & (VideoLibrary | AudioLibrary)) == 0)
84     return;
85
86   if (strcmp(message, "OnScanStarted") == 0 ||
87       strcmp(message, "OnCleanStarted") == 0)
88   {
89     m_dbUpdating = true;
90     return;
91   }
92
93   if (strcmp(message, "OnScanFinished") == 0 ||
94       strcmp(message, "OnCleanFinished") == 0)
95     m_dbUpdating = false;
96
97   // we are in an update/clean
98   if (m_dbUpdating)
99     return;
100
101   bool onUpdate = strcmp(message, "OnUpdate") == 0;
102   // always update Totals except on an OnUpdate with no playcount update
103   if (!onUpdate || data.isMember("playcount"))
104     ra_flag |= Totals;
105
106   // always update the full list except on an OnUpdate
107   if (!onUpdate)
108   {
109     if (flag & VideoLibrary)
110       ra_flag |= Video;
111     else if (flag & AudioLibrary)
112       ra_flag |= Audio;
113   }
114
115   CGUIMessage reload(GUI_MSG_NOTIFY_ALL, GetID(), 0, GUI_MSG_REFRESH_THUMBS, ra_flag);
116   g_windowManager.SendThreadMessage(reload, GetID());
117 }
118
119 void CGUIWindowHome::AddRecentlyAddedJobs(int flag)
120 {
121   bool getAJob = false;
122
123   // this block checks to see if another one is running
124   // and keeps track of the flag
125   {
126     CSingleLock lockMe(*this);
127     if (!m_recentlyAddedRunning)
128     {
129       getAJob = true;
130
131       flag |= m_cumulativeUpdateFlag; // add the flags from previous calls to AddRecentlyAddedJobs
132
133       m_cumulativeUpdateFlag = 0; // now taken care of in flag.
134                                   // reset this since we're going to execute a job
135
136       // we're about to add one so set the indicator
137       if (flag)
138         m_recentlyAddedRunning = true; // this will happen in the if clause below
139     }
140     else
141       // since we're going to skip a job, mark that one came in and ...
142       m_cumulativeUpdateFlag |= flag; // this will be used later
143   }
144
145   if (flag && getAJob)
146     CJobManager::GetInstance().AddJob(new CRecentlyAddedJob(flag), this);
147
148   m_updateRA = 0;
149 }
150
151 void CGUIWindowHome::OnJobComplete(unsigned int jobID, bool success, CJob *job)
152 {
153   int flag = 0;
154
155   {
156     CSingleLock lockMe(*this);
157
158     // the job is finished.
159     // did one come in in the meantime?
160     flag = m_cumulativeUpdateFlag;
161     m_recentlyAddedRunning = false; /// we're done.
162   }
163
164   if (flag)
165     AddRecentlyAddedJobs(0 /* the flag will be set inside AddRecentlyAddedJobs via m_cumulativeUpdateFlag */ );
166 }
167
168
169 bool CGUIWindowHome::OnMessage(CGUIMessage& message)
170 {
171   switch ( message.GetMessage() )
172   {
173   case GUI_MSG_NOTIFY_ALL:
174     if (message.GetParam1() == GUI_MSG_WINDOW_RESET || message.GetParam1() == GUI_MSG_REFRESH_THUMBS)
175     {
176       int updateRA = (message.GetSenderId() == GetID()) ? message.GetParam2() : (Video | Audio | Totals);
177
178       if (IsActive())
179         AddRecentlyAddedJobs(updateRA);
180       else
181         m_updateRA |= updateRA;
182     }
183     break;
184
185   default:
186     break;
187   }
188
189   return CGUIWindow::OnMessage(message);
190 }