Merge pull request #5039 from CEikermann/patch-1
[vuplus_xbmc] / xbmc / BackgroundInfoLoader.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 "BackgroundInfoLoader.h"
22 #include "FileItem.h"
23 #include "settings/AdvancedSettings.h"
24 #include "threads/SingleLock.h"
25 #include "utils/log.h"
26
27 using namespace std;
28
29 CBackgroundInfoLoader::CBackgroundInfoLoader() : m_thread (NULL)
30 {
31   m_bStop = true;
32   m_pObserver=NULL;
33   m_pProgressCallback=NULL;
34   m_pVecItems = NULL;
35   m_bIsLoading = false;
36 }
37
38 CBackgroundInfoLoader::~CBackgroundInfoLoader()
39 {
40   StopThread();
41 }
42
43 void CBackgroundInfoLoader::Run()
44 {
45   try
46   {
47     if (m_vecItems.size() > 0)
48     {
49       OnLoaderStart();
50
51       // Stage 1: All "fast" stuff we have already cached
52       for (vector<CFileItemPtr>::const_iterator iter = m_vecItems.begin(); iter != m_vecItems.end(); ++iter)
53       {
54         CFileItemPtr pItem = *iter;
55
56         // Ask the callback if we should abort
57         if ((m_pProgressCallback && m_pProgressCallback->Abort()) || m_bStop)
58           break;
59
60         try
61         {
62           if (LoadItemCached(pItem.get()) && m_pObserver)
63             m_pObserver->OnItemLoaded(pItem.get());
64         }
65         catch (...)
66         {
67           CLog::Log(LOGERROR, "CBackgroundInfoLoader::LoadItemCached - Unhandled exception for item %s", pItem->GetPath().c_str());
68         }
69       }
70
71       // Stage 2: All "slow" stuff that we need to lookup
72       for (vector<CFileItemPtr>::const_iterator iter = m_vecItems.begin(); iter != m_vecItems.end(); ++iter)
73       {
74         CFileItemPtr pItem = *iter;
75
76         // Ask the callback if we should abort
77         if ((m_pProgressCallback && m_pProgressCallback->Abort()) || m_bStop)
78           break;
79
80         try
81         {
82           if (LoadItemLookup(pItem.get()) && m_pObserver)
83             m_pObserver->OnItemLoaded(pItem.get());
84         }
85         catch (...)
86         {
87           CLog::Log(LOGERROR, "CBackgroundInfoLoader::LoadItemLookup - Unhandled exception for item %s", pItem->GetPath().c_str());
88         }
89       }
90     }
91
92     OnLoaderFinish();
93     m_bIsLoading = false;
94   }
95   catch (...)
96   {
97     m_bIsLoading = false;
98     CLog::Log(LOGERROR, "%s - Unhandled exception", __FUNCTION__);
99   }
100 }
101
102 void CBackgroundInfoLoader::Load(CFileItemList& items)
103 {
104   StopThread();
105
106   if (items.Size() == 0)
107     return;
108
109   CSingleLock lock(m_lock);
110
111   for (int nItem=0; nItem < items.Size(); nItem++)
112     m_vecItems.push_back(items[nItem]);
113
114   m_pVecItems = &items;
115   m_bStop = false;
116   m_bIsLoading = true;
117
118   m_thread = new CThread(this, "BackgroundLoader");
119   m_thread->Create();
120 #ifndef TARGET_POSIX
121   m_thread->SetPriority(THREAD_PRIORITY_BELOW_NORMAL);
122 #endif
123 }
124
125 void CBackgroundInfoLoader::StopAsync()
126 {
127   m_bStop = true;
128 }
129
130
131 void CBackgroundInfoLoader::StopThread()
132 {
133   StopAsync();
134
135   if (m_thread)
136   {
137     m_thread->StopThread();
138     delete m_thread;
139     m_thread = NULL;
140   }
141   m_vecItems.clear();
142   m_pVecItems = NULL;
143   m_bIsLoading = false;
144 }
145
146 bool CBackgroundInfoLoader::IsLoading()
147 {
148   return m_bIsLoading;
149 }
150
151 void CBackgroundInfoLoader::SetObserver(IBackgroundLoaderObserver* pObserver)
152 {
153   m_pObserver = pObserver;
154 }
155
156 void CBackgroundInfoLoader::SetProgressCallback(IProgressCallback* pCallback)
157 {
158   m_pProgressCallback = pCallback;
159 }
160