Merge pull request #5039 from CEikermann/patch-1
[vuplus_xbmc] / xbmc / DatabaseManager.cpp
1 /*
2  *      Copyright (C) 2012-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 "DatabaseManager.h"
22 #include "utils/log.h"
23 #include "addons/AddonDatabase.h"
24 #include "view/ViewDatabase.h"
25 #include "TextureDatabase.h"
26 #include "music/MusicDatabase.h"
27 #include "video/VideoDatabase.h"
28 #include "pvr/PVRDatabase.h"
29 #include "epg/EpgDatabase.h"
30 #include "settings/AdvancedSettings.h"
31
32 using namespace std;
33 using namespace EPG;
34 using namespace PVR;
35
36 CDatabaseManager &CDatabaseManager::Get()
37 {
38   static CDatabaseManager s_manager;
39   return s_manager;
40 }
41
42 CDatabaseManager::CDatabaseManager()
43 {
44 }
45
46 CDatabaseManager::~CDatabaseManager()
47 {
48 }
49
50 void CDatabaseManager::Initialize(bool addonsOnly)
51 {
52   Deinitialize();
53   { CAddonDatabase db; UpdateDatabase(db); }
54   if (addonsOnly)
55     return;
56   CLog::Log(LOGDEBUG, "%s, updating databases...", __FUNCTION__);
57
58   // NOTE: Order here is important. In particular, CTextureDatabase has to be updated
59   //       before CVideoDatabase.
60   { CViewDatabase db; UpdateDatabase(db); }
61   { CTextureDatabase db; UpdateDatabase(db); }
62   { CMusicDatabase db; UpdateDatabase(db, &g_advancedSettings.m_databaseMusic); }
63   { CVideoDatabase db; UpdateDatabase(db, &g_advancedSettings.m_databaseVideo); }
64   { CPVRDatabase db; UpdateDatabase(db, &g_advancedSettings.m_databaseTV); }
65   { CEpgDatabase db; UpdateDatabase(db, &g_advancedSettings.m_databaseEpg); }
66   CLog::Log(LOGDEBUG, "%s, updating databases... DONE", __FUNCTION__);
67 }
68
69 void CDatabaseManager::Deinitialize()
70 {
71   CSingleLock lock(m_section);
72   m_dbStatus.clear();
73 }
74
75 bool CDatabaseManager::CanOpen(const std::string &name)
76 {
77   CSingleLock lock(m_section);
78   map<string, DB_STATUS>::const_iterator i = m_dbStatus.find(name);
79   if (i != m_dbStatus.end())
80     return i->second == DB_READY;
81   return false; // db isn't even attempted to update yet
82 }
83
84 void CDatabaseManager::UpdateDatabase(CDatabase &db, DatabaseSettings *settings)
85 {
86   std::string name = db.GetBaseDBName();
87   UpdateStatus(name, DB_UPDATING);
88   if (db.Update(settings ? *settings : DatabaseSettings()))
89     UpdateStatus(name, DB_READY);
90   else
91     UpdateStatus(name, DB_FAILED);
92 }
93
94 void CDatabaseManager::UpdateStatus(const std::string &name, DB_STATUS status)
95 {
96   CSingleLock lock(m_section);
97   m_dbStatus[name] = status;
98 }