2 * Copyright (C) 2005-2013 Team XBMC
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)
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.
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/>.
21 #include "ViewDatabase.h"
22 #include "utils/URIUtils.h"
23 #include "settings/Settings.h"
24 #include "view/ViewState.h"
25 #include "utils/log.h"
27 #include "linux/ConvUtils.h" // GetLastError()
29 #include "dbwrappers/dataset.h"
32 //********************************************************************************************************************************
33 CViewDatabase::CViewDatabase(void)
37 //********************************************************************************************************************************
38 CViewDatabase::~CViewDatabase(void)
43 //********************************************************************************************************************************
44 bool CViewDatabase::Open()
46 return CDatabase::Open();
49 bool CViewDatabase::CreateTables()
53 CDatabase::CreateTables();
55 CLog::Log(LOGINFO, "create view table");
56 m_pDS->exec("CREATE TABLE view ( idView integer primary key, window integer, path text, viewMode integer, sortMethod integer, sortOrder integer, skin text)\n");
57 CLog::Log(LOGINFO, "create view index");
58 m_pDS->exec("CREATE INDEX idxViews ON view(path)");
59 CLog::Log(LOGINFO, "create view - window index");
60 m_pDS->exec("CREATE INDEX idxViewsWindow ON view(window)");
64 CLog::Log(LOGERROR, "%s unable to create tables:%u",
65 __FUNCTION__, GetLastError());
72 bool CViewDatabase::UpdateOldVersion(int version)
75 m_pDS->exec("alter table view add skin text");
79 bool CViewDatabase::GetViewState(const CStdString &path, int window, CViewState &state, const CStdString &skin)
83 if (NULL == m_pDB.get()) return false;
84 if (NULL == m_pDS.get()) return false;
86 CStdString path1(path);
87 URIUtils::AddSlashAtEnd(path1);
88 if (path1.IsEmpty()) path1 = "root://";
92 sql = PrepareSQL("select * from view where window = %i and path='%s'", window, path1.c_str());
94 sql = PrepareSQL("select * from view where window = %i and path='%s' and skin='%s'", window, path1.c_str(), skin.c_str());
95 m_pDS->query(sql.c_str());
98 { // have some information
99 state.m_viewMode = m_pDS->fv("viewMode").get_asInt();
100 state.m_sortMethod = (SORT_METHOD)m_pDS->fv("sortMethod").get_asInt();
101 state.m_sortOrder = (SortOrder)m_pDS->fv("sortOrder").get_asInt();
109 CLog::Log(LOGERROR, "%s, failed on path '%s'", __FUNCTION__, path.c_str());
114 bool CViewDatabase::SetViewState(const CStdString &path, int window, const CViewState &state, const CStdString &skin)
118 if (NULL == m_pDB.get()) return false;
119 if (NULL == m_pDS.get()) return false;
121 CStdString path1(path);
122 URIUtils::AddSlashAtEnd(path1);
123 if (path1.IsEmpty()) path1 = "root://";
125 CStdString sql = PrepareSQL("select idView from view where window = %i and path='%s' and skin='%s'", window, path1.c_str(), skin.c_str());
126 m_pDS->query(sql.c_str());
129 int idView = m_pDS->fv("idView").get_asInt();
131 sql = PrepareSQL("update view set viewMode=%i,sortMethod=%i,sortOrder=%i where idView=%i", state.m_viewMode, (int)state.m_sortMethod, (int)state.m_sortOrder, idView);
132 m_pDS->exec(sql.c_str());
137 sql = PrepareSQL("insert into view (idView, path, window, viewMode, sortMethod, sortOrder, skin) values(NULL, '%s', %i, %i, %i, %i, '%s')", path1.c_str(), window, state.m_viewMode, (int)state.m_sortMethod, (int)state.m_sortOrder, skin.c_str());
138 m_pDS->exec(sql.c_str());
143 CLog::Log(LOGERROR, "%s failed on path '%s'", __FUNCTION__, path.c_str());
148 bool CViewDatabase::ClearViewStates(int windowID)
152 if (NULL == m_pDB.get()) return false;
153 if (NULL == m_pDS.get()) return false;
155 CStdString sql = PrepareSQL("delete from view where window = %i", windowID);
156 m_pDS->exec(sql.c_str());
160 CLog::Log(LOGERROR, "%s failed on window '%i'", __FUNCTION__, windowID);