2 * Copyright (C) 2005-2008 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, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 #include "ProgramDatabase.h"
24 #include "windows/GUIWindowFileManager.h"
26 #include "settings/GUISettings.h"
27 #include "settings/Settings.h"
28 #include "utils/log.h"
29 #include "utils/URIUtils.h"
30 #include "dbwrappers/dataset.h"
32 using namespace XFILE;
34 //********************************************************************************************************************************
35 CProgramDatabase::CProgramDatabase(void)
39 //********************************************************************************************************************************
40 CProgramDatabase::~CProgramDatabase(void)
45 //********************************************************************************************************************************
46 bool CProgramDatabase::Open()
48 return CDatabase::Open();
51 bool CProgramDatabase::CreateTables()
56 CDatabase::CreateTables();
58 CLog::Log(LOGINFO, "create files table");
59 m_pDS->exec("CREATE TABLE files ( idFile integer primary key, strFilename text, titleId integer, xbedescription text, iTimesPlayed integer, lastAccessed integer, iRegion integer, iSize integer)\n");
60 CLog::Log(LOGINFO, "create files index");
61 m_pDS->exec("CREATE INDEX idxFiles ON files(strFilename)");
62 CLog::Log(LOGINFO, "create files - titleid index");
63 m_pDS->exec("CREATE INDEX idxTitleIdFiles ON files(titleId)");
67 CLog::Log(LOGERROR, "programdatabase::unable to create tables:%u",
75 bool CProgramDatabase::UpdateOldVersion(int version)
80 uint32_t CProgramDatabase::GetTitleId(const CStdString& strFilenameAndPath)
82 if (NULL == m_pDB.get()) return 0;
83 if (NULL == m_pDS.get()) return 0;
87 CStdString strSQL = PrepareSQL("select * from files where files.strFileName like '%s'", strFilenameAndPath.c_str());
88 if (!m_pDS->query(strSQL.c_str()))
91 int iRowsFound = m_pDS->num_rows();
97 uint32_t dwTitleId = m_pDS->fv("files.TitleId").get_asInt();
103 CLog::Log(LOGERROR, "CProgramDatabase:GetTitleId(%s) failed", strFilenameAndPath.c_str());
108 bool CProgramDatabase::SetTitleId(const CStdString& strFileName, uint32_t dwTitleId)
112 if (NULL == m_pDB.get()) return false;
113 if (NULL == m_pDS.get()) return false;
115 CStdString strSQL = PrepareSQL("select * from files where files.strFileName like '%s'", strFileName.c_str());
116 if (!m_pDS->query(strSQL.c_str())) return false;
117 int iRowsFound = m_pDS->num_rows();
123 int idFile = m_pDS->fv("files.idFile").get_asInt();
126 CLog::Log(LOGDEBUG, "CProgramDatabase::SetTitle(%s), idFile=%i, region=%u",
127 strFileName.c_str(), idFile,dwTitleId);
129 strSQL=PrepareSQL("update files set titleId=%u where idFile=%i",
131 m_pDS->exec(strSQL.c_str());
136 CLog::Log(LOGERROR, "CProgramDatabase:SetDescription(%s) failed", strFileName.c_str());
142 bool CProgramDatabase::GetXBEPathByTitleId(const uint32_t titleId, CStdString& strPathAndFilename)
146 if (NULL == m_pDB.get()) return false;
147 if (NULL == m_pDS.get()) return false;
149 CStdString strSQL=PrepareSQL("select files.strFilename from files where files.titleId=%u", titleId);
150 m_pDS->query(strSQL.c_str());
151 if (m_pDS->num_rows() > 0)
153 strPathAndFilename = m_pDS->fv("files.strFilename").get_asString();
154 strPathAndFilename.Replace('/', '\\');
163 CLog::Log(LOGERROR, "CProgramDatabase::GetXBEPathByTitleId(%u) failed",
169 uint32_t CProgramDatabase::GetProgramInfo(CFileItem *item)
171 uint32_t titleID = 0;
174 if (NULL == m_pDB.get()) return false;
175 if (NULL == m_pDS.get()) return false;
177 CStdString strSQL = PrepareSQL("select xbedescription,iTimesPlayed,lastAccessed,titleId,iSize from files where strFileName like '%s'", item->GetPath().c_str());
178 m_pDS->query(strSQL.c_str());
180 { // get info - only set the label if not preformatted
181 if (!item->IsLabelPreformated())
182 item->SetLabel(m_pDS->fv("xbedescription").get_asString());
183 item->m_iprogramCount = m_pDS->fv("iTimesPlayed").get_asInt();
184 item->m_strTitle = item->GetLabel(); // is this needed?
185 item->m_dateTime = TimeStampToLocalTime(_atoi64(m_pDS->fv("lastAccessed").get_asString().c_str()));
186 item->m_dwSize = _atoi64(m_pDS->fv("iSize").get_asString().c_str());
187 titleID = m_pDS->fv("titleId").get_asInt();
188 if (item->m_dwSize == -1)
191 URIUtils::GetDirectory(item->GetPath(),strPath);
192 int64_t iSize = CGUIWindowFileManager::CalculateFolderSize(strPath);
193 CStdString strSQL=PrepareSQL("update files set iSize=%I64u where strFileName like '%s'",iSize,item->GetPath().c_str());
194 m_pDS->exec(strSQL.c_str());
201 CLog::Log(LOGERROR, "CProgramDatabase::GetProgramInfo(%s) failed", item->GetPath().c_str());
206 bool CProgramDatabase::AddProgramInfo(CFileItem *item, unsigned int titleID)
210 if (NULL == m_pDB.get()) return false;
211 if (NULL == m_pDS.get()) return false;
214 if (g_guiSettings.GetBool("myprograms.gameautoregion"))
219 item->m_dateTime=CDateTime::GetCurrentDateTime();
220 item->m_dateTime.GetAsTimeStamp(time);
222 ULARGE_INTEGER lastAccessed;
223 lastAccessed.u.LowPart = time.dwLowDateTime;
224 lastAccessed.u.HighPart = time.dwHighDateTime;
227 URIUtils::GetDirectory(item->GetPath(),strPath);
228 // special case - programs in root of sources
230 CUtil::GetMatchingSource(strPath,g_settings.m_programSources,bIsShare);
234 struct __stat64 stat;
235 if (CFile::Stat(item->GetPath(),&stat) == 0)
236 iSize = stat.st_size;
239 iSize = CGUIWindowFileManager::CalculateFolderSize(strPath);
241 titleID = (unsigned int) -1;
242 CStdString strSQL=PrepareSQL("insert into files (idFile, strFileName, titleId, xbedescription, iTimesPlayed, lastAccessed, iRegion, iSize) values(NULL, '%s', %u, '%s', %i, %I64u, %i, %I64u)", item->GetPath().c_str(), titleID, item->GetLabel().c_str(), 0, lastAccessed.QuadPart, iRegion, iSize);
243 m_pDS->exec(strSQL.c_str());
244 item->m_dwSize = iSize;
248 CLog::Log(LOGERROR, "CProgramDatabase::AddProgramInfo(%s) failed", item->GetPath().c_str());
253 FILETIME CProgramDatabase::TimeStampToLocalTime(uint64_t timeStamp )
256 ::FileTimeToLocalFileTime( (const FILETIME *)&timeStamp, &fileTime);
260 bool CProgramDatabase::IncTimesPlayed(const CStdString& strFileName)
264 if (NULL == m_pDB.get()) return false;
265 if (NULL == m_pDS.get()) return false;
267 CStdString strSQL = PrepareSQL("select * from files where files.strFileName like '%s'", strFileName.c_str());
268 if (!m_pDS->query(strSQL.c_str())) return false;
269 int iRowsFound = m_pDS->num_rows();
275 int idFile = m_pDS->fv("files.idFile").get_asInt();
276 int iTimesPlayed = m_pDS->fv("files.iTimesPlayed").get_asInt();
279 CLog::Log(LOGDEBUG, "CProgramDatabase::IncTimesPlayed(%s), idFile=%i, iTimesPlayed=%i",
280 strFileName.c_str(), idFile, iTimesPlayed);
282 strSQL=PrepareSQL("update files set iTimesPlayed=%i where idFile=%i",
283 ++iTimesPlayed, idFile);
284 m_pDS->exec(strSQL.c_str());
289 CLog::Log(LOGERROR, "CProgramDatabase:IncTimesPlayed(%s) failed", strFileName.c_str());
295 bool CProgramDatabase::SetDescription(const CStdString& strFileName, const CStdString& strDescription)
299 if (NULL == m_pDB.get()) return false;
300 if (NULL == m_pDS.get()) return false;
302 CStdString strSQL = PrepareSQL("select * from files where files.strFileName like '%s'", strFileName.c_str());
303 if (!m_pDS->query(strSQL.c_str())) return false;
304 int iRowsFound = m_pDS->num_rows();
310 int idFile = m_pDS->fv("files.idFile").get_asInt();
313 CLog::Log(LOGDEBUG, "CProgramDatabase::SetDescription(%s), idFile=%i, description=%s",
314 strFileName.c_str(), idFile,strDescription.c_str());
316 strSQL=PrepareSQL("update files set xbedescription='%s' where idFile=%i",
317 strDescription.c_str(), idFile);
318 m_pDS->exec(strSQL.c_str());
323 CLog::Log(LOGERROR, "CProgramDatabase:SetDescription(%s) failed", strFileName.c_str());