[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / view / ViewDatabase.cpp
1 /*
2  *      Copyright (C) 2005-2013 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, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "ViewDatabase.h"
22 #include "utils/URIUtils.h"
23 #include "settings/Settings.h"
24 #include "view/ViewState.h"
25 #include "utils/log.h"
26 #ifdef _LINUX
27 #include "linux/ConvUtils.h" // GetLastError()
28 #endif
29 #include "dbwrappers/dataset.h"
30
31
32 //********************************************************************************************************************************
33 CViewDatabase::CViewDatabase(void)
34 {
35 }
36
37 //********************************************************************************************************************************
38 CViewDatabase::~CViewDatabase(void)
39 {
40
41 }
42
43 //********************************************************************************************************************************
44 bool CViewDatabase::Open()
45 {
46   return CDatabase::Open();
47 }
48
49 bool CViewDatabase::CreateTables()
50 {
51   try
52   {
53     CDatabase::CreateTables();
54
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)");
61   }
62   catch (...)
63   {
64     CLog::Log(LOGERROR, "%s unable to create tables:%u",
65               __FUNCTION__, GetLastError());
66     return false;
67   }
68
69   return true;
70 }
71
72 bool CViewDatabase::UpdateOldVersion(int version)
73 {
74   if (version < 4)
75     m_pDS->exec("alter table view add skin text");
76   return true;
77 }
78
79 bool CViewDatabase::GetViewState(const CStdString &path, int window, CViewState &state, const CStdString &skin)
80 {
81   try
82   {
83     if (NULL == m_pDB.get()) return false;
84     if (NULL == m_pDS.get()) return false;
85
86     CStdString path1(path);
87     URIUtils::AddSlashAtEnd(path1);
88     if (path1.IsEmpty()) path1 = "root://";
89
90     CStdString sql;
91     if (skin.IsEmpty())
92       sql = PrepareSQL("select * from view where window = %i and path='%s'", window, path1.c_str());
93     else
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());
96
97     if (!m_pDS->eof())
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();
102       m_pDS->close();
103       return true;
104     }
105     m_pDS->close();
106   }
107   catch (...)
108   {
109     CLog::Log(LOGERROR, "%s, failed on path '%s'", __FUNCTION__, path.c_str());
110   }
111   return false;
112 }
113
114 bool CViewDatabase::SetViewState(const CStdString &path, int window, const CViewState &state, const CStdString &skin)
115 {
116   try
117   {
118     if (NULL == m_pDB.get()) return false;
119     if (NULL == m_pDS.get()) return false;
120
121     CStdString path1(path);
122     URIUtils::AddSlashAtEnd(path1);
123     if (path1.IsEmpty()) path1 = "root://";
124
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());
127     if (!m_pDS->eof())
128     { // update the view
129       int idView = m_pDS->fv("idView").get_asInt();
130       m_pDS->close();
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());
133     }
134     else
135     { // add the view
136       m_pDS->close();
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());
139     }
140   }
141   catch (...)
142   {
143     CLog::Log(LOGERROR, "%s failed on path '%s'", __FUNCTION__, path.c_str());
144   }
145   return true;
146 }
147
148 bool CViewDatabase::ClearViewStates(int windowID)
149 {
150   try
151   {
152     if (NULL == m_pDB.get()) return false;
153     if (NULL == m_pDS.get()) return false;
154
155     CStdString sql = PrepareSQL("delete from view where window = %i", windowID);
156     m_pDS->exec(sql.c_str());
157   }
158   catch (...)
159   {
160     CLog::Log(LOGERROR, "%s failed on window '%i'", __FUNCTION__, windowID);
161   }
162   return true;
163 }