listitem.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import xbmcgui
  2. class ListItem(object):
  3. '''A wrapper for the xbmcgui.ListItem class. The class keeps track
  4. of any set properties that xbmcgui doesn't expose getters for.
  5. '''
  6. def __init__(self, label=None, label2=None, icon=None, thumbnail=None,
  7. path=None):
  8. '''Defaults are an emtpy string since xbmcgui.ListItem will not
  9. accept None.
  10. '''
  11. kwargs = {
  12. 'label': label,
  13. 'label2': label2,
  14. 'iconImage': icon,
  15. #'thumbnailImage': thumbnail,
  16. 'path': path,
  17. }
  18. # kwargs = dict((key, val) for key, val in locals().items() if val is
  19. # not None and key != 'self')
  20. kwargs = dict((key, val) for key, val in kwargs.items()
  21. if val is not None)
  22. self._listitem = xbmcgui.ListItem(**kwargs)
  23. # xbmc doesn't make getters available for these properties so we'll
  24. # keep track on our own
  25. self._icon = icon
  26. self._path = path
  27. self._thumbnail = thumbnail
  28. self._context_menu_items = []
  29. self.is_folder = True
  30. self._played = False
  31. def __repr__(self):
  32. return ("<ListItem '%s'>" % self.label).encode('utf-8')
  33. def __str__(self):
  34. return ('%s (%s)' % (self.label, self.path)).encode('utf-8')
  35. def get_context_menu_items(self):
  36. '''Returns the list of currently set context_menu items.'''
  37. return self._context_menu_items
  38. def add_context_menu_items(self, items, replace_items=False):
  39. '''Adds context menu items. If replace_items is True all
  40. previous context menu items will be removed.
  41. '''
  42. for label, action in items:
  43. assert isinstance(label, basestring)
  44. assert isinstance(action, basestring)
  45. if replace_items:
  46. self._context_menu_items = []
  47. self._context_menu_items.extend(items)
  48. self._listitem.addContextMenuItems(items, replace_items)
  49. def get_label(self):
  50. '''Sets the listitem's label'''
  51. return self._listitem.getLabel()
  52. def set_label(self, label):
  53. '''Returns the listitem's label'''
  54. return self._listitem.setLabel(label)
  55. label = property(get_label, set_label)
  56. def get_label2(self):
  57. '''Returns the listitem's label2'''
  58. return self._listitem.getLabel2()
  59. def set_label2(self, label):
  60. '''Sets the listitem's label2'''
  61. return self._listitem.setLabel2(label)
  62. label2 = property(get_label2, set_label2)
  63. def is_selected(self):
  64. '''Returns True if the listitem is selected.'''
  65. return self._listitem.isSelected()
  66. def select(self, selected_status=True):
  67. '''Sets the listitems selected status to the provided value.
  68. Defaults to True.
  69. '''
  70. return self._listitem.select(selected_status)
  71. selected = property(is_selected, select)
  72. def set_info(self, type, info_labels):
  73. '''Sets the listitems info'''
  74. return self._listitem.setInfo(type, info_labels)
  75. def set_art(self, values):
  76. '''Sets the listitems info'''
  77. try:
  78. return self._listitem.setArt(values)
  79. except:
  80. return None
  81. def get_property(self, key):
  82. '''Returns the property associated with the given key'''
  83. return self._listitem.getProperty(key)
  84. def set_property(self, key, value):
  85. '''Sets a property for the given key and value'''
  86. return self._listitem.setProperty(key, value)
  87. def add_stream_info(self, stream_type, stream_values):
  88. '''Adds stream details'''
  89. return self._listitem.addStreamInfo(stream_type, stream_values)
  90. def get_icon(self):
  91. '''Returns the listitem's icon image'''
  92. return self._icon
  93. def set_icon(self, icon):
  94. '''Sets the listitem's icon image'''
  95. self._icon = icon
  96. return self._listitem.setIconImage(icon)
  97. icon = property(get_icon, set_icon)
  98. def get_thumbnail(self):
  99. '''Returns the listitem's thumbnail image'''
  100. return self._thumbnail
  101. def set_thumbnail(self, thumbnail):
  102. '''Sets the listitem's thumbnail image'''
  103. self._thumbnail = thumbnail
  104. return self._listitem.setThumbnailImage(thumbnail)
  105. thumbnail = property(get_thumbnail, set_thumbnail)
  106. def get_path(self):
  107. '''Returns the listitem's path'''
  108. return self._path
  109. def set_path(self, path):
  110. '''Sets the listitem's path'''
  111. self._path = path
  112. return self._listitem.setPath(path)
  113. path = property(get_path, set_path)
  114. def get_is_playable(self):
  115. '''Returns True if the listitem is playable, False if it is a
  116. directory
  117. '''
  118. return not self.is_folder
  119. def set_is_playable(self, is_playable):
  120. '''Sets the listitem's playable flag'''
  121. value = 'false'
  122. if is_playable:
  123. value = 'true'
  124. self.set_property('isPlayable', value)
  125. self.is_folder = not is_playable
  126. playable = property(get_is_playable, set_is_playable)
  127. def set_played(self, was_played):
  128. '''Sets the played status of the listitem. Used to
  129. differentiate between a resolved video versus a playable item.
  130. Has no effect on XBMC, it is strictly used for xbmcswift2.
  131. '''
  132. self._played = was_played
  133. def get_played(self):
  134. '''Returns True if the video was played.'''
  135. return self._played
  136. def as_tuple(self):
  137. '''Returns a tuple of list item properties:
  138. (path, the wrapped xbmcgui.ListItem, is_folder)
  139. '''
  140. return self.path, self._listitem, self.is_folder
  141. def as_xbmc_listitem(self):
  142. '''Returns the wrapped xbmcgui.ListItem'''
  143. return self._listitem
  144. @classmethod
  145. def from_dict(cls, label=None, label2=None, icon=None, thumbnail=None,
  146. path=None, selected=None, info=None, properties=None,
  147. context_menu=None, replace_context_menu=False, art=None,
  148. is_playable=None, info_type='video', stream_info=None, mime_type=None):
  149. '''A ListItem constructor for setting a lot of properties not
  150. available in the regular __init__ method. Useful to collect all
  151. the properties in a dict and then use the **dct to call this
  152. method.
  153. '''
  154. listitem = cls(label, label2, icon, thumbnail, path)
  155. if selected is not None:
  156. listitem.select(selected)
  157. if info:
  158. listitem.set_info(info_type, info)
  159. if art:
  160. listitem.set_art(art)
  161. if mime_type is not None:
  162. listitem._listitem.setMimeType(mime_type)
  163. if is_playable is not None:
  164. listitem.set_is_playable(is_playable)
  165. if properties:
  166. # Need to support existing tuples, but prefer to have a dict for
  167. # properties.
  168. if hasattr(properties, 'items'):
  169. properties = properties.items()
  170. for key, val in properties:
  171. listitem.set_property(key, val)
  172. if stream_info:
  173. for stream_type, stream_values in stream_info.items():
  174. listitem.add_stream_info(stream_type, stream_values)
  175. if context_menu:
  176. listitem.add_context_menu_items(context_menu, replace_context_menu)
  177. return listitem