Skip to content

Commit

Permalink
Merge pull request #8872 from Montellese/videodb_set_dateadded
Browse files Browse the repository at this point in the history
videolibrary: support manually setting dateadded through NFO and JSON-RPC
  • Loading branch information
Montellese committed Feb 2, 2016
2 parents 779d63a + 862f159 commit 5f85ae2
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 39 deletions.
6 changes: 6 additions & 0 deletions xbmc/interfaces/json-rpc/VideoLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1174,4 +1174,10 @@ void CVideoLibrary::UpdateVideoTag(const CVariant &parameterObject, CVideoInfoTa
}
}
}

if (ParameterNotNull(parameterObject, "dateadded"))
{
SetFromDBDateTime(parameterObject["dateadded"], details.m_dateAdded);
updatedDetails.insert("dateadded");
}
}
12 changes: 8 additions & 4 deletions xbmc/interfaces/json-rpc/schema/methods.json
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,8 @@
{ "name": "art", "type": [ "null", { "$ref": "Media.Artwork.Set", "required": true } ], "default": null },
{ "name": "resume", "type": [ "null", { "$ref": "Video.Resume", "required": true } ], "default": null },
{ "name": "userrating", "$ref": "Optional.Integer" },
{ "name": "ratings", "$ref": "Video.Ratings" }
{ "name": "ratings", "$ref": "Video.Ratings" },
{ "name": "dateadded", "$ref": "Optional.String" }
],
"returns": "string"
},
Expand Down Expand Up @@ -1457,7 +1458,8 @@
{ "name": "tag", "type": [ "null", { "$ref": "Array.String", "required": true } ], "default": null },
{ "name": "art", "type": [ "null", { "$ref": "Media.Artwork.Set", "required": true } ], "default": null },
{ "name": "userrating", "$ref": "Optional.Integer" },
{ "name": "ratings", "$ref": "Video.Ratings" }
{ "name": "ratings", "$ref": "Video.Ratings" },
{ "name": "dateadded", "$ref": "Optional.String" }
],
"returns": "string"
},
Expand Down Expand Up @@ -1499,7 +1501,8 @@
{ "name": "art", "type": [ "null", { "$ref": "Media.Artwork.Set", "required": true } ], "default": null },
{ "name": "resume", "type": [ "null", { "$ref": "Video.Resume", "required": true } ], "default": null },
{ "name": "userrating", "$ref": "Optional.Integer" },
{ "name": "ratings", "$ref": "Video.Ratings" }
{ "name": "ratings", "$ref": "Video.Ratings" },
{ "name": "dateadded", "$ref": "Optional.String" }
],
"returns": "string"
},
Expand Down Expand Up @@ -1528,7 +1531,8 @@
{ "name": "art", "type": [ "null", { "$ref": "Media.Artwork.Set", "required": true } ], "default": null },
{ "name": "resume", "type": [ "null", { "$ref": "Video.Resume", "required": true } ], "default": null },
{ "name": "rating", "$ref": "Optional.Number" },
{ "name": "userrating", "$ref": "Optional.Integer" }
{ "name": "userrating", "$ref": "Optional.Integer" },
{ "name": "dateadded", "$ref": "Optional.String" }
],
"returns": "string"
},
Expand Down
2 changes: 1 addition & 1 deletion xbmc/interfaces/json-rpc/schema/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.7.0
7.8.0
96 changes: 66 additions & 30 deletions xbmc/video/VideoDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ bool CVideoDatabase::GetSubPaths(const std::string &basepath, std::vector<std::p
return false;
}

int CVideoDatabase::AddPath(const std::string& strPath, const std::string &parentPath /*= "" */, const std::string &strDateAdded /*= "" */)
int CVideoDatabase::AddPath(const std::string& strPath, const std::string &parentPath /*= "" */, const CDateTime& dateAdded /* = CDateTime() */)
{
std::string strSQL;
try
Expand All @@ -712,15 +712,15 @@ int CVideoDatabase::AddPath(const std::string& strPath, const std::string &paren
// add the path
if (idParentPath < 0)
{
if (!strDateAdded.empty())
strSQL=PrepareSQL("insert into path (idPath, strPath, dateAdded) values (NULL, '%s', '%s')", strPath1.c_str(), strDateAdded.c_str());
if (dateAdded.IsValid())
strSQL=PrepareSQL("insert into path (idPath, strPath, dateAdded) values (NULL, '%s', '%s')", strPath1.c_str(), dateAdded.GetAsDBDateTime().c_str());
else
strSQL=PrepareSQL("insert into path (idPath, strPath) values (NULL, '%s')", strPath1.c_str());
}
else
{
if (!strDateAdded.empty())
strSQL=PrepareSQL("insert into path (idPath, strPath, dateAdded, idParentPath) values (NULL, '%s', '%s', %i)", strPath1.c_str(), strDateAdded.c_str(), idParentPath);
if (dateAdded.IsValid())
strSQL = PrepareSQL("insert into path (idPath, strPath, dateAdded, idParentPath) values (NULL, '%s', '%s', %i)", strPath1.c_str(), dateAdded.GetAsDBDateTime().c_str(), idParentPath);
else
strSQL=PrepareSQL("insert into path (idPath, strPath, idParentPath) values (NULL, '%s', %i)", strPath1.c_str(), idParentPath);
}
Expand Down Expand Up @@ -891,32 +891,35 @@ int CVideoDatabase::AddFile(const CFileItem& item)
return AddFile(item.GetPath());
}

void CVideoDatabase::UpdateFileDateAdded(int idFile, const std::string& strFileNameAndPath)
void CVideoDatabase::UpdateFileDateAdded(int idFile, const std::string& strFileNameAndPath, const CDateTime& dateAdded /* = CDateTime() */)
{
if (idFile < 0 || strFileNameAndPath.empty())
return;

CDateTime dateAdded;
CDateTime finalDateAdded = dateAdded;
try
{
if (NULL == m_pDB.get()) return;
if (NULL == m_pDS.get()) return;

// 1 prefering to use the files mtime(if it's valid) and only using the file's ctime if the mtime isn't valid
if (g_advancedSettings.m_iVideoLibraryDateAdded == 1)
dateAdded = CFileUtils::GetModificationDate(strFileNameAndPath, false);
//2 using the newer datetime of the file's mtime and ctime
else if (g_advancedSettings.m_iVideoLibraryDateAdded == 2)
dateAdded = CFileUtils::GetModificationDate(strFileNameAndPath, true);
//0 using the current datetime if non of the above matches or one returns an invalid datetime
if (!dateAdded.IsValid())
dateAdded = CDateTime::GetCurrentDateTime();

m_pDS->exec(PrepareSQL("UPDATE files SET dateAdded='%s' WHERE idFile=%d", dateAdded.GetAsDBDateTime().c_str(), idFile));

if (!finalDateAdded.IsValid())
{
// 1 prefering to use the files mtime(if it's valid) and only using the file's ctime if the mtime isn't valid
if (g_advancedSettings.m_iVideoLibraryDateAdded == 1)
finalDateAdded = CFileUtils::GetModificationDate(strFileNameAndPath, false);
//2 using the newer datetime of the file's mtime and ctime
else if (g_advancedSettings.m_iVideoLibraryDateAdded == 2)
finalDateAdded = CFileUtils::GetModificationDate(strFileNameAndPath, true);
//0 using the current datetime if non of the above matches or one returns an invalid datetime
if (!finalDateAdded.IsValid())
finalDateAdded = CDateTime::GetCurrentDateTime();
}

m_pDS->exec(PrepareSQL("UPDATE files SET dateAdded='%s' WHERE idFile=%d", finalDateAdded.GetAsDBDateTime().c_str(), idFile));
}
catch (...)
{
CLog::Log(LOGERROR, "%s (%s, %s) failed", __FUNCTION__, CURL::GetRedacted(strFileNameAndPath).c_str(), dateAdded.GetAsDBDateTime().c_str());
CLog::Log(LOGERROR, "%s (%s, %s) failed", __FUNCTION__, CURL::GetRedacted(strFileNameAndPath).c_str(), finalDateAdded.GetAsDBDateTime().c_str());
}
}

Expand Down Expand Up @@ -1277,17 +1280,15 @@ int CVideoDatabase::AddMovie(const std::string& strFilenameAndPath)
return -1;
}

bool CVideoDatabase::AddPathToTvShow(int idShow, const std::string &path, const std::string &parentPath)
bool CVideoDatabase::AddPathToTvShow(int idShow, const std::string &path, const std::string &parentPath, const CDateTime& dateAdded /* = CDateTime() */)
{
// Check if this path is already added
int idPath = GetPathId(path);
if (idPath < 0)
{
// Get the creation datetime of the tvshow directory
CDateTime dateAdded;

CDateTime finalDateAdded = dateAdded;
// Skip looking at the files ctime/mtime if defined by the user through as.xml
if (g_advancedSettings.m_iVideoLibraryDateAdded > 0)
if (!finalDateAdded.IsValid() && g_advancedSettings.m_iVideoLibraryDateAdded > 0)
{
struct __stat64 buffer;
if (XFILE::CFile::Stat(path, &buffer) == 0)
Expand All @@ -1304,15 +1305,15 @@ bool CVideoDatabase::AddPathToTvShow(int idShow, const std::string &path, const
time = localtime((const time_t*)&buffer.st_ctime);
#endif
if (time)
dateAdded = *time;
finalDateAdded = *time;
}
}
}

if (!dateAdded.IsValid())
dateAdded = CDateTime::GetCurrentDateTime();
if (!finalDateAdded.IsValid())
finalDateAdded = CDateTime::GetCurrentDateTime();

idPath = AddPath(path, parentPath, dateAdded.GetAsDBDateTime());
idPath = AddPath(path, parentPath, finalDateAdded);
}

return ExecuteQuery(PrepareSQL("REPLACE INTO tvshowlinkpath(idShow, idPath) VALUES (%i,%i)", idShow, idPath));
Expand Down Expand Up @@ -2168,6 +2169,15 @@ int CVideoDatabase::SetDetailsForMovie(const std::string& strFilenameAndPath, CV
}
}

// update dateadded if it's set
if (details.m_dateAdded.IsValid())
{
if (details.m_iFileId <= 0)
details.m_iFileId = GetFileId(strFilenameAndPath);

UpdateFileDateAdded(details.m_iFileId, strFilenameAndPath, details.m_dateAdded);
}

AddCast(idMovie, "movie", details.m_cast);
AddLinksToItem(idMovie, MediaTypeMovie, "genre", details.m_genre);
AddLinksToItem(idMovie, MediaTypeMovie, "studio", details.m_studio);
Expand Down Expand Up @@ -2269,6 +2279,13 @@ int CVideoDatabase::UpdateDetailsForMovie(int idMovie, CVideoInfoTag& details, c
SetArtForItem(idMovie, MediaTypeMovie, artwork);
if (updatedDetails.find("ratings") != updatedDetails.end())
details.m_iIdRating = UpdateRatings(idMovie, MediaTypeMovie, details.m_ratings, details.m_strDefaultRating);
if (updatedDetails.find("dateadded") != updatedDetails.end() && details.m_dateAdded.IsValid())
{
if (details.m_iFileId <= 0)
details.m_iFileId = GetFileId(details.GetPath());

UpdateFileDateAdded(details.m_iFileId, details.GetPath(), details.m_dateAdded);
}

// track if the set was updated
int idSet = 0;
Expand Down Expand Up @@ -2412,7 +2429,7 @@ int CVideoDatabase::SetDetailsForTvShow(const std::vector<std::pair<std::string,

// add any paths to the tvshow
for (std::vector<std::pair<std::string, std::string>>::const_iterator i = paths.begin(); i != paths.end(); ++i)
AddPathToTvShow(idTvShow, i->first, i->second);
AddPathToTvShow(idTvShow, i->first, i->second, details.m_dateAdded);

UpdateDetailsForTvShow(idTvShow, details, artwork, seasonArt);

Expand Down Expand Up @@ -2544,6 +2561,15 @@ int CVideoDatabase::SetDetailsForEpisode(const std::string& strFilenameAndPath,
}
}

// update dateadded if it's set
if (details.m_dateAdded.IsValid())
{
if (details.m_iFileId <= 0)
details.m_iFileId = GetFileId(strFilenameAndPath);

UpdateFileDateAdded(details.m_iFileId, strFilenameAndPath, details.m_dateAdded);
}

AddCast(idEpisode, "episode", details.m_cast);
AddActorLinksToItem(idEpisode, MediaTypeEpisode, "director", details.m_director);
AddActorLinksToItem(idEpisode, MediaTypeEpisode, "writer", details.m_writingCredits);
Expand Down Expand Up @@ -2656,6 +2682,16 @@ int CVideoDatabase::SetDetailsForMusicVideo(const std::string& strFilenameAndPat
}
}

// update dateadded if it's set
if (details.m_dateAdded.IsValid())
{
int idFile = details.m_iFileId;
if (idFile <= 0)
idFile = GetFileId(strFilenameAndPath);

UpdateFileDateAdded(idFile, strFilenameAndPath, details.m_dateAdded);
}

AddActorLinksToItem(idMVideo, MediaTypeMusicVideo, "actor", details.m_artist);
AddActorLinksToItem(idMVideo, MediaTypeMusicVideo, "director", details.m_director);
AddLinksToItem(idMVideo, MediaTypeMusicVideo, "genre", details.m_genre);
Expand Down
10 changes: 6 additions & 4 deletions xbmc/video/VideoDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -746,17 +746,18 @@ class CVideoDatabase : public CDatabase
If the path is already in the database, we simply return its id.
\param strPath the path to add
\param parentPath the parent path of the path to add. If empty, URIUtils::GetParentPath() will determine the path.
\param strDateAdded datetime when the path was added to the filesystem/database
\param dateAdded datetime when the path was added to the filesystem/database
\return id of the file, -1 if it could not be added.
*/
int AddPath(const std::string& strPath, const std::string &parentPath = "", const std::string &strDateAdded = "");
int AddPath(const std::string& strPath, const std::string &parentPath = "", const CDateTime& dateAdded = CDateTime());

/*! \brief Updates the dateAdded field in the files table for the file
with the given idFile and the given path based on the files modification date
\param idFile id of the file in the files table
\param strFileNameAndPath path to the file
\param dateAdded datetime when the file was added to the filesystem/database
*/
void UpdateFileDateAdded(int idFile, const std::string& strFileNameAndPath);
void UpdateFileDateAdded(int idFile, const std::string& strFileNameAndPathh, const CDateTime& dateAdded = CDateTime());

void ExportToXML(const std::string &path, bool singleFile = true, bool images=false, bool actorThumbs=false, bool overwrite=false);
void ExportActorThumbs(const std::string &path, const CVideoInfoTag& tag, bool singleFiles, bool overwrite=false);
Expand Down Expand Up @@ -863,9 +864,10 @@ class CVideoDatabase : public CDatabase
\param idShow the id of the show.
\param path the path to add.
\param parentPath the parent path of the path to add.
\param dateAdded date/time when the path was added
\return true if successfully added, false otherwise.
*/
bool AddPathToTvShow(int idShow, const std::string &path, const std::string &parentPath);
bool AddPathToTvShow(int idShow, const std::string &path, const std::string &parentPath, const CDateTime& dateAdded = CDateTime());

/*! \brief Check whether a show is already in the library.
Matches on unique identifier or matching title and premiered date.
Expand Down

0 comments on commit 5f85ae2

Please sign in to comment.