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