jellyfin-kodi/resources/lib/entrypoint/context.py

177 lines
5.2 KiB
Python
Raw Normal View History

2018-09-06 08:36:32 +00:00
# -*- coding: utf-8 -*-
#################################################################################################
import json
import logging
import sys
import xbmc
import xbmcaddon
import database
from dialogs import context
from helper import _, settings, dialog
from downloader import TheVoid
from objects import Actions
#################################################################################################
LOG = logging.getLogger("EMBY."+__name__)
XML_PATH = (xbmcaddon.Addon('plugin.video.emby').getAddonInfo('path'), "default", "1080i")
OPTIONS = {
'Refresh': _(30410),
'Delete': _(30409),
'Addon': _(30408),
'AddFav': _(30405),
'RemoveFav': _(30406),
'Transcode': _(30412)
}
#################################################################################################
class Context(object):
_selected_option = None
2018-09-13 07:07:08 +00:00
def __init__(self, transcode=False, delete=False):
2019-01-14 17:21:08 +00:00
2018-09-13 07:07:08 +00:00
try:
self.kodi_id = sys.listitem.getVideoInfoTag().getDbId() or None
self.media = self.get_media_type()
self.server = sys.listitem.getProperty('embyserver') or None
item_id = sys.listitem.getProperty('embyid')
except AttributeError:
self.server = None
if xbmc.getInfoLabel('ListItem.Property(embyid)'):
item_id = xbmc.getInfoLabel('ListItem.Property(embyid)')
else:
self.kodi_id = xbmc.getInfoLabel('ListItem.DBID')
self.media = xbmc.getInfoLabel('ListItem.DBTYPE')
item_id = None
2019-01-14 17:21:08 +00:00
2018-09-06 08:36:32 +00:00
if self.server or item_id:
self.item = TheVoid('GetItem', {'ServerId': self.server, 'Id': item_id}).get()
else:
self.item = self.get_item_id()
if self.item:
if transcode:
self.transcode()
2018-09-13 07:07:08 +00:00
elif delete:
self.delete_item()
2018-09-06 08:36:32 +00:00
elif self.select_menu():
self.action_menu()
if self._selected_option.decode('utf-8') in (OPTIONS['Delete'], OPTIONS['AddFav'], OPTIONS['RemoveFav']):
xbmc.sleep(500)
xbmc.executebuiltin('Container.Refresh')
def get_media_type(self):
''' Get media type based on sys.listitem. If unfilled, base on visible window.
'''
media = sys.listitem.getVideoInfoTag().getMediaType()
if not media:
if xbmc.getCondVisibility('Container.Content(albums)'):
media = "album"
elif xbmc.getCondVisibility('Container.Content(artists)'):
media = "artist"
elif xbmc.getCondVisibility('Container.Content(songs)'):
media = "song"
elif xbmc.getCondVisibility('Container.Content(pictures)'):
media = "picture"
else:
LOG.info("media is unknown")
return media.decode('utf-8')
def get_item_id(self):
''' Get synced item from embydb.
'''
item = database.get_item(self.kodi_id, self.media)
if not item:
return
return {
'Id': item[0],
'UserData': json.loads(item[4]) if item[4] else {},
'Type': item[3]
}
def select_menu(self):
''' Display the select dialog.
Favorites, Refresh, Delete (opt), Settings.
'''
options = []
if self.item['Type'] not in ('Season'):
if self.item['UserData'].get('IsFavorite'):
options.append(OPTIONS['RemoveFav'])
else:
options.append(OPTIONS['AddFav'])
options.append(OPTIONS['Refresh'])
if settings('enableContextDelete.bool'):
options.append(OPTIONS['Delete'])
options.append(OPTIONS['Addon'])
context_menu = context.ContextMenu("script-emby-context.xml", *XML_PATH)
context_menu.set_options(options)
context_menu.doModal()
if context_menu.is_selected():
self._selected_option = context_menu.get_selected()
return self._selected_option
def action_menu(self):
selected = self._selected_option.decode('utf-8')
if selected == OPTIONS['Refresh']:
TheVoid('RefreshItem', {'ServerId': self.server, 'Id': self.item['Id']})
elif selected == OPTIONS['AddFav']:
TheVoid('FavoriteItem', {'ServerId': self.server, 'Id': self.item['Id'], 'Favorite': True})
elif selected == OPTIONS['RemoveFav']:
TheVoid('FavoriteItem', {'ServerId': self.server, 'Id': self.item['Id'], 'Favorite': False})
elif selected == OPTIONS['Addon']:
xbmc.executebuiltin('Addon.OpenSettings(plugin.video.emby)')
elif selected == OPTIONS['Delete']:
2018-09-13 07:07:08 +00:00
self.delete_item()
def delete_item(self):
delete = True
2018-09-06 08:36:32 +00:00
2018-09-13 07:07:08 +00:00
if not settings('skipContextMenu.bool'):
2018-09-06 08:36:32 +00:00
2018-09-13 07:07:08 +00:00
if not dialog("yesno", heading="{emby}", line1=_(33015)):
delete = False
2018-09-06 08:36:32 +00:00
2018-09-13 07:07:08 +00:00
if delete:
2018-09-14 06:17:37 +00:00
TheVoid('DeleteItem', {'ServerId': self.server, 'Id': self.item['Id']})
2018-09-06 08:36:32 +00:00
def transcode(self):
2019-01-14 17:21:08 +00:00
filename = xbmc.getInfoLabel("ListItem.Filenameandpath")
2019-01-14 23:49:43 +00:00
filename += "&transcode=true"
2019-01-14 17:21:08 +00:00
xbmc.executebuiltin("PlayMedia(%s)" % filename)