not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
7.0 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Video Viewer activity: A video viewer a set of videos on a server
  5. # Lionel Laské
  6. from gi.repository import Gtk, Gdk
  7. import logging
  8. import os
  9. from gettext import gettext as _
  10. from sugar3.activity import activity
  11. from sugar3.graphics.toolbarbox import ToolbarBox
  12. from sugar3.graphics.toolbutton import ToolButton
  13. from sugar3.activity.widgets import ActivityButton
  14. from sugar3.activity.widgets import TitleEntry
  15. from sugar3.activity.widgets import StopButton
  16. from sugar3.activity.widgets import ShareButton
  17. from sugar3.activity.widgets import DescriptionItem
  18. from sugar3.presence import presenceservice
  19. from sugar3.activity.widgets import ActivityToolbarButton
  20. from sugar3.graphics.toolbarbox import ToolbarButton
  21. from gi.repository import WebKit
  22. import logging
  23. import gconf
  24. from datetime import date
  25. from enyo import Enyo
  26. class EnyoActivity(activity.Activity):
  27. """EnyoActivity class as specified in activity.info"""
  28. def __init__(self, handle):
  29. """Set up the activity."""
  30. activity.Activity.__init__(self, handle)
  31. self.max_participants = 1
  32. self.context = {}
  33. self.favorite_status = 'notfavorite'
  34. self.filter_status = ''
  35. self.make_toolbar()
  36. self.make_mainview()
  37. def filter_changed(self, button):
  38. if self.filter_status == button.get_label():
  39. self.filter_status = ''
  40. else:
  41. self.filter_status = button.get_label()
  42. self.enyo.send_message("filter_clicked", self.filter_status)
  43. def favorite(self, button):
  44. if self.favorite_status == 'favorite':
  45. self.favorite_button.icon_name = self.favorite_status = 'notfavorite'
  46. self.enyo.send_message("favorite_clicked", 0)
  47. else:
  48. self.favorite_button.icon_name = self.favorite_status = 'favorite'
  49. self.enyo.send_message("favorite_clicked", 1)
  50. def text_filter(self, entry):
  51. self.enyo.send_message("text_typed", entry.props.text)
  52. def library_clicked(self, button):
  53. self.enyo.send_message("library_clicked")
  54. def refresh(self, context):
  55. self.context = context
  56. web_app_page = os.path.join(activity.get_bundle_path(), "index.html")
  57. self.webview.load_uri('file://' + web_app_page+"?onsugar=1")
  58. def init_context(self, args):
  59. """Init Javascript context sending buddy information"""
  60. # Get XO colors
  61. buddy = {}
  62. client = gconf.client_get_default()
  63. colors = client.get_string("/desktop/sugar/user/color")
  64. buddy["colors"] = colors.split(",")
  65. # Get XO name
  66. presenceService = presenceservice.get_instance()
  67. buddy["name"] = presenceService.get_owner().props.nick
  68. self.enyo.send_message("buddy", buddy)
  69. if self.context != {}:
  70. self.enyo.send_message("load-context", self.context)
  71. else:
  72. self.enyo.send_message("library_clicked")
  73. def make_mainview(self):
  74. """Create the activity view"""
  75. # Create global box
  76. vbox = Gtk.VBox(True)
  77. # Create webview
  78. self.webview = webview = WebKit.WebView()
  79. webview.show()
  80. vbox.pack_start(webview, True, True, 0)
  81. vbox.show()
  82. # Activate Enyo interface
  83. self.enyo = Enyo(webview)
  84. self.enyo.connect("ready", self.init_context)
  85. self.enyo.connect("save-context", self.save_context)
  86. self.enyo.connect("refresh-screen", self.refresh)
  87. self.enyo.connect("set_categories", self.set_categories)
  88. # Go to first page
  89. web_app_page = os.path.join(activity.get_bundle_path(), "index.html")
  90. self.webview.load_uri('file://' + web_app_page+"?onsugar=1")
  91. # Display all
  92. self.set_canvas(vbox)
  93. vbox.show()
  94. def make_toolbar(self):
  95. # toolbar with the new toolbar redesign
  96. toolbar_box = ToolbarBox()
  97. activity_button = ActivityToolbarButton(self)
  98. toolbar_box.toolbar.insert(activity_button, 0)
  99. activity_button.show()
  100. self.toolbarview = Gtk.Toolbar()
  101. langtoolbar_button = ToolbarButton(
  102. label=_('Filter'),
  103. page=self.toolbarview,
  104. icon_name='filter')
  105. langtoolbar_button.show()
  106. toolbar_box.toolbar.insert(langtoolbar_button, -1)
  107. self.toolbarview.show()
  108. box_search_item = Gtk.ToolItem()
  109. self.search_entry = Gtk.Entry()
  110. self.search_entry.connect('changed', self.text_filter)
  111. self.search_entry.set_size_request(300, -1)
  112. box_search_item.add(self.search_entry)
  113. self.search_entry.show()
  114. box_search_item.show()
  115. toolbar_box.toolbar.insert(box_search_item, -1)
  116. favorite_button = ToolButton(self.favorite_status)
  117. favorite_button.set_tooltip('Filter on favorite')
  118. favorite_button.connect('clicked', self.favorite)
  119. toolbar_box.toolbar.insert(favorite_button, -1)
  120. favorite_button.show()
  121. self.favorite_button = favorite_button
  122. library_button = ToolButton('library')
  123. library_button.set_tooltip('Show libraries')
  124. library_button.connect('clicked', self.library_clicked)
  125. toolbar_box.toolbar.insert(library_button, -1)
  126. library_button.show()
  127. separator = Gtk.SeparatorToolItem()
  128. separator.props.draw = False
  129. separator.set_expand(True)
  130. toolbar_box.toolbar.insert(separator, -1)
  131. separator.show()
  132. stop_button = StopButton(self)
  133. toolbar_box.toolbar.insert(stop_button, -1)
  134. stop_button.show()
  135. self.set_toolbar_box(toolbar_box)
  136. toolbar_box.show()
  137. def set_categories(self, categories):
  138. """Called when Enyo load a new database with new categories, udate the filter"""
  139. nitems = self.toolbarview.get_n_items()
  140. for i in range(0, nitems):
  141. self.toolbarview.remove(self.toolbarview.get_nth_item(0))
  142. for category in categories:
  143. btn = Gtk.Button.new_with_label(category['id'])
  144. btn.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(0.1568, 0.1568, 0.1568, 1.0))
  145. btn.connect('clicked', self.filter_changed)
  146. btn.show()
  147. tool = Gtk.ToolItem()
  148. tool.add(btn)
  149. tool.show()
  150. self.toolbarview.insert(tool, -1)
  151. self.toolbarview.show()
  152. def write_file(self, file_path):
  153. """Called when activity is saved, get the current context in Enyo"""
  154. self.file_path = file_path
  155. self.enyo.send_message("save-context")
  156. def save_context(self, context):
  157. """Called by Enyo to save the current context"""
  158. file = open(self.file_path, 'w')
  159. try:
  160. file.write(self.enyo.json_encode(context)+'\n')
  161. finally:
  162. file.close()
  163. def read_file(self, file_path):
  164. """Called when activity is loaded, load the current context in the file"""
  165. file = open(file_path, 'r')
  166. self.context = {}
  167. try:
  168. self.context = self.enyo.json_decode(file.readline().strip('\n'))
  169. finally:
  170. file.close()