2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "GUIDialogVideoBookmarks.h"
12 #include "FileItemList.h"
13 #include "ServiceBroker.h"
15 #include "application/Application.h"
16 #include "application/ApplicationComponents.h"
17 #include "application/ApplicationPlayer.h"
18 #include "dialogs/GUIDialogContextMenu.h"
19 #include "dialogs/GUIDialogKaiToast.h"
20 #include "guilib/GUIComponent.h"
21 #include "guilib/GUIWindowManager.h"
22 #include "guilib/LocalizeStrings.h"
23 #include "imagefiles/ImageFileURL.h"
24 #include "input/actions/Action.h"
25 #include "input/actions/ActionIDs.h"
26 #include "messaging/ApplicationMessenger.h"
27 #include "pictures/Picture.h"
28 #include "profiles/ProfileManager.h"
29 #include "settings/AdvancedSettings.h"
30 #include "settings/Settings.h"
31 #include "settings/SettingsComponent.h"
32 #include "utils/Crc32.h"
33 #include "utils/FileUtils.h"
34 #include "utils/StringUtils.h"
35 #include "utils/URIUtils.h"
36 #include "utils/Variant.h"
37 #include "utils/log.h"
38 #include "video/VideoDatabase.h"
39 #include "video/VideoFileItemClassify.h"
40 #include "view/ViewState.h"
47 #define CONTROL_ADD_BOOKMARK 2
48 #define CONTROL_CLEAR_BOOKMARKS 3
49 #define CONTROL_ADD_EPISODE_BOOKMARK 4
51 #define CONTROL_THUMBS 11
53 using namespace KODI::VIDEO
;
55 CGUIDialogVideoBookmarks::CGUIDialogVideoBookmarks()
56 : CGUIDialog(WINDOW_DIALOG_VIDEO_BOOKMARKS
, "VideoOSDBookmarks.xml")
58 m_vecItems
= new CFileItemList
;
59 m_loadType
= LOAD_EVERY_TIME
;
62 CGUIDialogVideoBookmarks::~CGUIDialogVideoBookmarks()
67 bool CGUIDialogVideoBookmarks::OnMessage(CGUIMessage
& message
)
69 switch ( message
.GetMessage() )
71 case GUI_MSG_WINDOW_DEINIT
:
73 CUtil::DeleteVideoDatabaseDirectoryCache();
78 case GUI_MSG_WINDOW_INIT
:
80 // don't init this dialog if we don't playback a file
81 const auto& components
= CServiceBroker::GetAppComponents();
82 const auto appPlayer
= components
.GetComponent
<CApplicationPlayer
>();
83 if (!appPlayer
->IsPlaying())
86 CGUIWindow::OnMessage(message
);
94 int iControl
= message
.GetSenderId();
95 if (iControl
== CONTROL_ADD_BOOKMARK
)
100 else if (iControl
== CONTROL_CLEAR_BOOKMARKS
)
104 else if (iControl
== CONTROL_ADD_EPISODE_BOOKMARK
)
106 AddEpisodeBookmark();
109 else if (m_viewControl
.HasControl(iControl
)) // list/thumb control
111 int iItem
= m_viewControl
.GetSelectedItem();
112 int iAction
= message
.GetParam1();
113 if (iAction
== ACTION_DELETE_ITEM
)
117 else if (iAction
== ACTION_SELECT_ITEM
|| iAction
== ACTION_MOUSE_LEFT_CLICK
)
124 case GUI_MSG_SETFOCUS
:
126 if (m_viewControl
.HasControl(message
.GetControlId()) && m_viewControl
.GetCurrentControl() != message
.GetControlId())
128 m_viewControl
.SetFocused();
133 case GUI_MSG_REFRESH_LIST
:
135 switch (message
.GetParam1())
147 return CGUIDialog::OnMessage(message
);
150 bool CGUIDialogVideoBookmarks::OnAction(const CAction
&action
)
152 switch(action
.GetID())
154 case ACTION_CONTEXT_MENU
:
155 case ACTION_MOUSE_RIGHT_CLICK
:
157 OnPopupMenu(m_viewControl
.GetSelectedItem());
161 return CGUIDialog::OnAction(action
);
165 void CGUIDialogVideoBookmarks::OnPopupMenu(int item
)
167 if (item
< 0 || item
>= (int) m_bookmarks
.size())
170 // highlight the item
171 (*m_vecItems
)[item
]->Select(true);
173 CContextButtons choices
;
174 choices
.Add(1, (m_bookmarks
[item
].type
== CBookmark::EPISODE
? 20405 : 20404)); // "Remove episode bookmark" or "Remove bookmark"
176 int button
= CGUIDialogContextMenu::ShowAndGetChoice(choices
);
178 // unhighlight the item
179 (*m_vecItems
)[item
]->Select(false);
185 void CGUIDialogVideoBookmarks::Delete(int item
)
187 if ( item
>=0 && (unsigned)item
< m_bookmarks
.size() )
189 CVideoDatabase videoDatabase
;
190 videoDatabase
.Open();
191 const std::string path
{g_application
.CurrentFileItem().GetDynPath()};
192 videoDatabase
.ClearBookMarkOfFile(path
, m_bookmarks
[item
], m_bookmarks
[item
].type
);
193 videoDatabase
.Close();
194 CUtil::DeleteVideoDatabaseDirectoryCache();
199 void CGUIDialogVideoBookmarks::OnRefreshList()
202 std::vector
<CFileItemPtr
> items
;
204 // open the d/b and retrieve the bookmarks for the current movie
205 m_filePath
= g_application
.CurrentFileItem().GetDynPath();
207 CVideoDatabase videoDatabase
;
208 videoDatabase
.Open();
209 videoDatabase
.GetBookMarksForFile(m_filePath
, m_bookmarks
);
210 videoDatabase
.GetBookMarksForFile(m_filePath
, m_bookmarks
, CBookmark::EPISODE
, true);
211 videoDatabase
.Close();
213 std::unique_lock
<CCriticalSection
> lock(m_refreshSection
);
216 // cycle through each stored bookmark and add it to our list control
217 for (unsigned int i
= 0; i
< m_bookmarks
.size(); ++i
)
219 std::string bookmarkTime
;
220 if (m_bookmarks
[i
].type
== CBookmark::EPISODE
)
221 bookmarkTime
= StringUtils::Format("{} {} {} {}", g_localizeStrings
.Get(20373),
222 m_bookmarks
[i
].seasonNumber
, g_localizeStrings
.Get(20359),
223 m_bookmarks
[i
].episodeNumber
);
225 bookmarkTime
= StringUtils::SecondsToTimeString((long)m_bookmarks
[i
].timeInSeconds
, TIME_FORMAT_HH_MM_SS
);
227 CFileItemPtr
item(new CFileItem(StringUtils::Format(g_localizeStrings
.Get(299), i
+ 1)));
228 item
->SetLabel2(bookmarkTime
);
229 item
->SetArt("thumb", m_bookmarks
[i
].thumbNailImage
);
230 item
->SetProperty("resumepoint", m_bookmarks
[i
].timeInSeconds
);
231 item
->SetProperty("playerstate", m_bookmarks
[i
].playerState
);
232 item
->SetProperty("isbookmark", "true");
233 items
.push_back(item
);
236 // add chapters if around
237 const auto& components
= CServiceBroker::GetAppComponents();
238 const auto appPlayer
= components
.GetComponent
<CApplicationPlayer
>();
239 for (int i
= 1; i
<= appPlayer
->GetChapterCount(); ++i
)
241 std::string chapterName
;
242 appPlayer
->GetChapterName(chapterName
, i
);
244 int64_t pos
= appPlayer
->GetChapterPos(i
);
245 std::string time
= StringUtils::SecondsToTimeString((long) pos
, TIME_FORMAT_HH_MM_SS
);
247 if (chapterName
.empty() ||
248 StringUtils::StartsWithNoCase(chapterName
, time
) ||
249 StringUtils::IsNaturalNumber(chapterName
))
250 chapterName
= StringUtils::Format(g_localizeStrings
.Get(25010), i
);
252 CFileItemPtr
item(new CFileItem(chapterName
));
253 item
->SetLabel2(time
);
255 if (CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
256 CSettings::SETTING_MYVIDEOS_EXTRACTCHAPTERTHUMBS
))
258 auto chapterPath
= IMAGE_FILES::CImageFileURL::FromFile(m_filePath
, "video");
259 chapterPath
.AddOption("chapter", std::to_string(i
));
260 item
->SetArt("thumb", chapterPath
.ToCacheKey());
263 item
->SetProperty("chapter", i
);
264 item
->SetProperty("resumepoint", static_cast<double>(pos
));
265 item
->SetProperty("ischapter", "true");
266 items
.push_back(item
);
269 // sort items by resume point
270 std::sort(items
.begin(), items
.end(), [](const CFileItemPtr
&item1
, const CFileItemPtr
&item2
) {
271 return item1
->GetProperty("resumepoint").asDouble() < item2
->GetProperty("resumepoint").asDouble();
274 // add items to file list and mark the proper item as selected if the current playtime is above
275 int selectedItemIndex
= 0;
276 double playTime
= g_application
.GetTime();
277 for (auto& item
: items
)
279 m_vecItems
->Add(item
);
280 if (playTime
>= item
->GetProperty("resumepoint").asDouble())
281 selectedItemIndex
= m_vecItems
->Size() - 1;
284 m_viewControl
.SetItems(*m_vecItems
);
285 m_viewControl
.SetSelectedItem(selectedItemIndex
);
288 void CGUIDialogVideoBookmarks::Update()
290 CVideoDatabase videoDatabase
;
291 videoDatabase
.Open();
293 if (g_application
.CurrentFileItem().HasVideoInfoTag() && g_application
.CurrentFileItem().GetVideoInfoTag()->m_iEpisode
> -1)
295 std::vector
<CVideoInfoTag
> episodes
;
296 videoDatabase
.GetEpisodesByFile(g_application
.CurrentFile(),episodes
);
297 if (episodes
.size() > 1)
299 CONTROL_ENABLE(CONTROL_ADD_EPISODE_BOOKMARK
);
303 CONTROL_DISABLE(CONTROL_ADD_EPISODE_BOOKMARK
);
308 CONTROL_DISABLE(CONTROL_ADD_EPISODE_BOOKMARK
);
312 m_viewControl
.SetCurrentView(DEFAULT_VIEW_ICONS
);
314 // empty the list ready for population
319 videoDatabase
.Close();
322 void CGUIDialogVideoBookmarks::Clear()
324 m_viewControl
.Clear();
328 void CGUIDialogVideoBookmarks::GotoBookmark(int item
)
330 auto& components
= CServiceBroker::GetAppComponents();
331 const auto appPlayer
= components
.GetComponent
<CApplicationPlayer
>();
332 if (item
< 0 || item
>= m_vecItems
->Size() || !appPlayer
->HasPlayer())
335 CFileItemPtr fileItem
= m_vecItems
->Get(item
);
336 int chapter
= static_cast<int>(fileItem
->GetProperty("chapter").asInteger());
339 appPlayer
->SetPlayerState(fileItem
->GetProperty("playerstate").asString());
340 g_application
.SeekTime(fileItem
->GetProperty("resumepoint").asDouble());
343 appPlayer
->SeekChapter(chapter
);
348 void CGUIDialogVideoBookmarks::ClearBookmarks()
350 CVideoDatabase videoDatabase
;
351 videoDatabase
.Open();
352 const std::string path
{g_application
.CurrentFileItem().GetDynPath()};
353 videoDatabase
.ClearBookMarksOfFile(path
, CBookmark::STANDARD
);
354 videoDatabase
.ClearBookMarksOfFile(path
, CBookmark::RESUME
);
355 videoDatabase
.ClearBookMarksOfFile(path
, CBookmark::EPISODE
);
356 videoDatabase
.Close();
360 bool CGUIDialogVideoBookmarks::AddBookmark(CVideoInfoTag
* tag
)
362 CVideoDatabase videoDatabase
;
364 bookmark
.timeInSeconds
= (int)g_application
.GetTime();
365 bookmark
.totalTimeInSeconds
= (int)g_application
.GetTotalTime();
367 auto& components
= CServiceBroker::GetAppComponents();
368 const auto appPlayer
= components
.GetComponent
<CApplicationPlayer
>();
370 if (appPlayer
->HasPlayer())
371 bookmark
.playerState
= appPlayer
->GetPlayerState();
373 bookmark
.playerState
.clear();
375 bookmark
.player
= g_application
.GetCurrentPlayer();
377 // create the thumbnail image
378 const float aspectRatio
{appPlayer
->GetRenderAspectRatio()};
379 CRect srcRect
{}, renderRect
{}, viewRect
{};
380 appPlayer
->GetRects(srcRect
, renderRect
, viewRect
);
381 const unsigned int srcWidth
{static_cast<unsigned int>(srcRect
.Width())};
382 const unsigned int srcHeight
{static_cast<unsigned int>(srcRect
.Height())};
383 const unsigned int renderWidth
{static_cast<unsigned int>(renderRect
.Width())};
384 const unsigned int renderHeight
{static_cast<unsigned int>(renderRect
.Height())};
385 const unsigned int viewWidth
{static_cast<unsigned int>(viewRect
.Width())};
386 const unsigned int viewHeight
{static_cast<unsigned int>(viewRect
.Height())};
388 if (!srcWidth
|| !srcHeight
|| !renderWidth
|| !renderHeight
|| !viewWidth
|| !viewHeight
)
391 const unsigned int orientation
{appPlayer
->GetOrientation()};
392 const bool rotated
{orientation
== 90 || orientation
== 270};
394 // FIXME: the renderer sets the scissors to the size of the screen (provided by graphiccontext),
395 // limiting the max size of thumbs (for example 4k video played on 1024x768 screen)
397 // The advanced setting defines the max size of the largest dimension (depends on orientation)
398 const unsigned int maxThumbDim
{
399 CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_imageRes
};
400 unsigned int width
{}, height
{};
404 if (aspectRatio
>= 1.0f
)
406 width
= std::min({maxThumbDim
, viewWidth
, renderWidth
, srcWidth
});
407 height
= static_cast<unsigned int>(width
/ aspectRatio
);
411 height
= std::min({maxThumbDim
, viewHeight
, renderHeight
, srcHeight
});
412 width
= static_cast<unsigned int>(height
* aspectRatio
);
417 // rotation is applied during rendering, switching source width and height
418 if (aspectRatio
>= 1.0f
)
420 height
= std::min({maxThumbDim
, viewHeight
, renderHeight
, srcWidth
});
421 width
= static_cast<unsigned int>(height
/ aspectRatio
);
425 width
= std::min({maxThumbDim
, viewWidth
, renderWidth
, srcHeight
});
426 height
= static_cast<unsigned int>(width
* aspectRatio
);
430 uint8_t *pixels
= (uint8_t*)malloc(height
* width
* 4);
431 unsigned int captureId
= appPlayer
->RenderCaptureAlloc();
433 appPlayer
->RenderCapture(captureId
, width
, height
, CAPTUREFLAG_IMMEDIATELY
);
434 bool hasImage
= appPlayer
->RenderCaptureGetPixels(captureId
, 1000, pixels
, height
* width
* 4);
438 const std::shared_ptr
<CProfileManager
> profileManager
= CServiceBroker::GetSettingsComponent()->GetProfileManager();
440 auto crc
= Crc32::ComputeFromLowerCase(g_application
.CurrentFile());
441 bookmark
.thumbNailImage
=
442 StringUtils::Format("{:08x}_{}.jpg", crc
, (int)bookmark
.timeInSeconds
);
443 bookmark
.thumbNailImage
= URIUtils::AddFileToFolder(profileManager
->GetBookmarksThumbFolder(), bookmark
.thumbNailImage
);
445 if (!CPicture::CreateThumbnailFromSurface(pixels
, width
, height
, width
* 4,
446 bookmark
.thumbNailImage
))
448 bookmark
.thumbNailImage
.clear();
451 CLog::Log(LOGERROR
,"CGUIDialogVideoBookmarks: failed to create thumbnail");
453 appPlayer
->RenderCaptureRelease(captureId
);
456 CLog::Log(LOGERROR
,"CGUIDialogVideoBookmarks: failed to create thumbnail 2");
460 videoDatabase
.Open();
462 videoDatabase
.AddBookMarkForEpisode(*tag
, bookmark
);
465 const std::string path
{g_application
.CurrentFileItem().GetDynPath()};
466 videoDatabase
.AddBookMarkToFile(path
, bookmark
, CBookmark::STANDARD
);
468 videoDatabase
.Close();
472 void CGUIDialogVideoBookmarks::OnWindowLoaded()
474 CGUIDialog::OnWindowLoaded();
475 m_viewControl
.Reset();
476 m_viewControl
.SetParentWindow(GetID());
477 m_viewControl
.AddView(GetControl(CONTROL_THUMBS
));
481 void CGUIDialogVideoBookmarks::OnWindowUnload()
484 CGUIDialog::OnWindowUnload();
485 m_viewControl
.Reset();
488 CGUIControl
*CGUIDialogVideoBookmarks::GetFirstFocusableControl(int id
)
490 if (m_viewControl
.HasControl(id
))
491 id
= m_viewControl
.GetCurrentControl();
492 return CGUIWindow::GetFirstFocusableControl(id
);
495 bool CGUIDialogVideoBookmarks::AddEpisodeBookmark()
497 std::vector
<CVideoInfoTag
> episodes
;
498 CVideoDatabase videoDatabase
;
499 videoDatabase
.Open();
500 videoDatabase
.GetEpisodesByFile(g_application
.CurrentFile(), episodes
);
501 videoDatabase
.Close();
502 if (!episodes
.empty())
504 CContextButtons choices
;
505 for (unsigned int i
=0; i
< episodes
.size(); ++i
)
507 std::string strButton
=
508 StringUtils::Format("{} {}, {} {}", g_localizeStrings
.Get(20373), episodes
[i
].m_iSeason
,
509 g_localizeStrings
.Get(20359), episodes
[i
].m_iEpisode
);
510 choices
.Add(i
, strButton
);
513 int pressed
= CGUIDialogContextMenu::ShowAndGetChoice(choices
);
516 AddBookmark(&episodes
[pressed
]);
525 bool CGUIDialogVideoBookmarks::OnAddBookmark()
527 if (!IsVideo(g_application
.CurrentFileItem()))
530 if (CGUIDialogVideoBookmarks::AddBookmark())
532 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(GUI_MSG_REFRESH_LIST
, 0, WINDOW_DIALOG_VIDEO_BOOKMARKS
);
533 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info
,
534 g_localizeStrings
.Get(298), // "Bookmarks"
535 g_localizeStrings
.Get(21362));// "Bookmark created"
541 bool CGUIDialogVideoBookmarks::OnAddEpisodeBookmark()
543 bool bReturn
= false;
544 if (g_application
.CurrentFileItem().HasVideoInfoTag() && g_application
.CurrentFileItem().GetVideoInfoTag()->m_iEpisode
> -1)
546 CVideoDatabase videoDatabase
;
547 videoDatabase
.Open();
548 std::vector
<CVideoInfoTag
> episodes
;
549 videoDatabase
.GetEpisodesByFile(g_application
.CurrentFile(),episodes
);
550 if (episodes
.size() > 1)
552 bReturn
= CGUIDialogVideoBookmarks::AddEpisodeBookmark();
555 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(GUI_MSG_REFRESH_LIST
, 0, WINDOW_DIALOG_VIDEO_BOOKMARKS
);
556 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info
,
557 g_localizeStrings
.Get(298), // "Bookmarks"
558 g_localizeStrings
.Get(21363));// "Episode Bookmark created"
562 videoDatabase
.Close();