From dcfbb1d1cadcabb9607289d8180370af622f1daf Mon Sep 17 00:00:00 2001 From: Muriel de Souza Godoi Date: Thu, 08 Nov 2007 20:02:27 +0000 Subject: Rainbow compliance, Use new tubes interface #4739, fix in collaboration mode #4752 --- diff --git a/activity.py b/activity.py index 45c2394..be689a7 100755 --- a/activity.py +++ b/activity.py @@ -18,8 +18,11 @@ # import logging +_logger = logging.getLogger('memorize-activity') + from gettext import gettext as _ from os.path import join, dirname +from os import environ import dbus import gtk @@ -30,7 +33,7 @@ import telepathy.client from sugar.activity.activity import Activity, ActivityToolbox from sugar.presence import presenceservice -import sugar.logger +from sugar.presence.tubeconn import TubeConnection from sugar.graphics.xocolor import XoColor from sugar import profile @@ -43,7 +46,7 @@ import createtoolbar import cardlist import createcardpanel -from sugar.presence.tubeconn import TubeConnection + SERVICE = 'org.laptop.Memorize' IFACE = SERVICE @@ -52,9 +55,6 @@ PATH = '/org/laptop/Memorize' _TOOLBAR_PLAY = 1 _TOOLBAR_CREATE = 2 - -_logger = logging.getLogger('memorize-activity') - class MemorizeActivity(Activity): def __init__(self, handle): @@ -112,8 +112,6 @@ class MemorizeActivity(Activity): self.game.connect('change_game', self._memorizeToolbar.update_toolbar) self._memorizeToolbar.connect('game_changed', self.game.change_game) - self.connect('shared', self._shared_cb) - self.hbox = gtk.HBox(False) self.hbox.pack_start(self.scoreboard, False, False) @@ -147,13 +145,13 @@ class MemorizeActivity(Activity): # Get the Presence Service self.pservice = presenceservice.get_instance() - try: - name, path = self.pservice.get_preferred_connection() - self.tp_conn_name = name - self.tp_conn_path = path - self.conn = telepathy.client.Connection(name, path) - except TypeError: - _logger.debug('Offline') + #try: + # name, path = self.pservice.get_preferred_connection() + # self.tp_conn_name = name + # self.tp_conn_path = path + # self.conn = telepathy.client.Connection(name, path) + #except TypeError: + # _logger.debug('Offline') self.initiating = None # Buddy object for you @@ -162,6 +160,7 @@ class MemorizeActivity(Activity): self.current = 0 self.game.set_myself(self.owner) + self.connect('shared', self._shared_cb) # Owner.props.key if self._shared_activity: @@ -173,7 +172,7 @@ class MemorizeActivity(Activity): else: _logger.debug('buddy joined - __init__: %s', self.owner.props.nick) game_file = join(dirname(__file__),'demos','addition.zip') - self.game.load_game(game_file, 4) + self.game.load_game(game_file, 4, 'demo') _logger.debug('loading conventional') self.game.add_buddy(self.owner) self.show_all() @@ -225,26 +224,26 @@ class MemorizeActivity(Activity): def _shared_cb(self, activity): _logger.debug('My activity was shared') self.initiating = True - self._setup() - - for buddy in self._shared_activity.get_joined_buddies(): - pass # Can do stuff with newly acquired buddies here - - self._shared_activity.connect('buddy-joined', self._buddy_joined_cb) - self._shared_activity.connect('buddy-left', self._buddy_left_cb) + self._sharing_setup() _logger.debug('This is my activity: making a tube...') id = self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].OfferDBusTube( SERVICE, {}) - def _setup(self): + def _sharing_setup(self): if self._shared_activity is None: _logger.error('Failed to share or join activity') return + self.conn = self._shared_activity.telepathy_conn + self.tubes_chan = self._shared_activity.telepathy_tubes_chan + self.text_chan = self._shared_activity.telepathy_text_chan + + self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].connect_to_signal('NewTube', self._new_tube_cb) + + self._shared_activity.connect('buddy-joined', self._buddy_joined_cb) + self._shared_activity.connect('buddy-left', self._buddy_left_cb) - bus_name, conn_path, channel_paths =\ - self._shared_activity.get_channels() - + ''' # Work out what our room is called and whether we have Tubes already room = None tubes_chan = None @@ -282,7 +281,7 @@ class MemorizeActivity(Activity): tubes_chan[telepathy.CHANNEL_TYPE_TUBES].connect_to_signal('NewTube', self._new_tube_cb) - + ''' def _list_tubes_reply_cb(self, tubes): for tube_info in tubes: self._new_tube_cb(*tube_info) @@ -308,10 +307,10 @@ class MemorizeActivity(Activity): self.game.add_buddy(self.owner) self.initiating = False - self._setup() + self._sharing_setup() - self._shared_activity.connect('buddy-joined', self._buddy_joined_cb) - self._shared_activity.connect('buddy-left', self._buddy_left_cb) + #self._shared_activity.connect('buddy-joined', self._buddy_joined_cb) + #self._shared_activity.connect('buddy-left', self._buddy_left_cb) _logger.debug('This is not my activity: waiting for a tube...') self.tubes_chan[telepathy.CHANNEL_TYPE_TUBES].ListTubes( diff --git a/activity/activity.info b/activity/activity.info index 1619c5a..6125854 100644 --- a/activity/activity.info +++ b/activity/activity.info @@ -1,7 +1,7 @@ [Activity] name = Memorize service_name = org.laptop.Memorize -class = activity.MemorizeActivity +exec = sugar-activity activity.MemorizeActivity icon = activity-memorize activity_version = 19 show_launcher = yes diff --git a/cardlist.py b/cardlist.py index faebc60..05518ec 100644 --- a/cardlist.py +++ b/cardlist.py @@ -20,8 +20,11 @@ import gtk import svgcard import logging -from os.path import join + import os +from os import environ +from os.path import join, dirname + import model import zipfile import tempfile @@ -43,7 +46,7 @@ class CardList(gtk.EventBox): def __init__(self): gtk.EventBox.__init__(self) - self.model = model.Model(os.path.dirname(__file__)) + self.model = model.Model(environ['SUGAR_ACTIVITY_ROOT']) self.pairs = [] self.current_pair = None @@ -72,22 +75,22 @@ class CardList(gtk.EventBox): self.clean_list() for key in game_pairs: if game_pairs[key].props.aimg != None: - aimg = gtk.gdk.pixbuf_new_from_file(os.path.join(self.model.data['pathimg'], game_pairs[key].props.aimg)) + aimg = gtk.gdk.pixbuf_new_from_file(join(self.model.data['pathimg'], game_pairs[key].props.aimg)) else: aimg = None if game_pairs[key].props.bimg != None: - bimg = gtk.gdk.pixbuf_new_from_file(os.path.join(self.model.data['pathimg'], game_pairs[key].props.bimg)) + bimg = gtk.gdk.pixbuf_new_from_file(join(self.model.data['pathimg'], game_pairs[key].props.bimg)) else: bimg = None if game_pairs[key].props.asnd != None: - asnd = os.path.join(self.model.data['pathsnd'], game_pairs[key].props.asnd) + asnd = join(self.model.data['pathsnd'], game_pairs[key].props.asnd) else: asnd = None if game_pairs[key].props.bsnd != None: - bsnd = os.path.join(self.model.data['pathsnd'], game_pairs[key].props.bsnd) + bsnd = join(self.model.data['pathsnd'], game_pairs[key].props.bsnd) else: bsnd = None @@ -95,7 +98,8 @@ class CardList(gtk.EventBox): def save_game(self, widget, game_name, equal_pairs, grouped): - temp_folder = tempfile.mkdtemp() + tmp_root = join(environ['SUGAR_ACTIVITY_ROOT'], 'tmp') + temp_folder = tempfile.mkdtemp(dir=tmp_root) temp_img_folder = join(temp_folder, 'images') temp_snd_folder = join(temp_folder, 'sounds') diff --git a/createcardpanel.py b/createcardpanel.py index 5f869e6..a89d1de 100644 --- a/createcardpanel.py +++ b/createcardpanel.py @@ -18,7 +18,9 @@ # import gtk -import os +from os import environ +from os.path import join, dirname + import shutil import tempfile from gettext import gettext as _ @@ -44,7 +46,7 @@ class CreateCardPanel(gtk.EventBox): self.equal_pairs = False # Set the add new pair buttom - add_icon = os.path.join(os.path.dirname(__file__), "images/pair-add.svg") + add_icon = join(dirname(__file__), 'images', 'pair-add.svg') add_image = gtk.Image() add_image.set_from_file(add_icon) self._addbutton = gtk.Button(' ' + _('Add as new pair')) @@ -52,7 +54,7 @@ class CreateCardPanel(gtk.EventBox): self._addbutton.connect('button-press-event', self.emit_add_pair) # Set update selected pair buttom - update_icon = os.path.join(os.path.dirname(__file__), "images/pair-update.svg") + update_icon = join(dirname(__file__), 'images', 'pair-update.svg') update_image = gtk.Image() update_image.set_from_file(update_icon) self._updatebutton = gtk.Button(' ' + _('Update selected pair')) @@ -169,7 +171,8 @@ class CardEditor(gtk.EventBox): gtk.EventBox.__init__(self) self.set_size_request(310, 320) - self.temp_folder = tempfile.mkdtemp() + tmp_root = join(dirname(__file__), 'instance') + self.temp_folder = tempfile.mkdtemp(dir=tmp_root) table = gtk.Table() self.previewlabel = gtk.Label(_('Preview:')) @@ -177,14 +180,14 @@ class CardEditor(gtk.EventBox): self.textlabel = gtk.Label(_('Text:')) self.textlabel.set_alignment(1, 0.5) - picture_icon = os.path.join(os.path.dirname(__file__), 'images/import_picture.svg') + picture_icon = join(dirname(__file__), 'images', 'import_picture.svg') picture_image = gtk.Image() picture_image.set_from_file(picture_icon) self.browsepicture = gtk.Button() self.browsepicture.set_image(picture_image) self.browsepicture.connect('button-press-event', self._import_image) - sound_icon = os.path.join(os.path.dirname(__file__), 'images/import_sound.svg') + sound_icon = join(dirname(__file__), 'images', 'import_sound.svg') sound_image = gtk.Image() sound_image.set_from_file(sound_icon) self.browsesound = gtk.Button() @@ -272,7 +275,7 @@ class CardEditor(gtk.EventBox): self._load_audio(jobject.file_path) def _load_audio(self, index): - dst = os.path.join(self.temp_folder, os.path.basename(index)) + dst = join(self.temp_folder, basename(index)) shutil.copy(index, dst) self.set_snd(dst) _logger.error('Audio Loaded: '+dst) diff --git a/createtoolbar.py b/createtoolbar.py index ce22b9a..0ac7c9d 100755 --- a/createtoolbar.py +++ b/createtoolbar.py @@ -21,7 +21,7 @@ import logging from gettext import gettext as _ import gtk -import os +from os.path import join, dirname from gobject import SIGNAL_RUN_FIRST, TYPE_PYOBJECT from sugar.graphics.toolbutton import ToolButton @@ -45,7 +45,7 @@ class CreateToolbar(gtk.Toolbar): self._lock = True # New Button - new_icon = os.path.join(os.path.dirname(__file__), "images/game-new.svg") + new_icon = join(dirname(__file__), 'images', 'game-new.svg') new_image = gtk.Image() new_image.set_from_file(new_icon) self._new_button = ToolButton() @@ -55,7 +55,7 @@ class CreateToolbar(gtk.Toolbar): self._add_widget(self._new_button) # Load Button - load_icon = os.path.join(os.path.dirname(__file__), "images/game-load.svg") + load_icon = join(dirname(__file__), 'images', 'game-load.svg') load_image = gtk.Image() load_image.set_from_file(load_icon) self._load_button = ToolButton() @@ -65,7 +65,7 @@ class CreateToolbar(gtk.Toolbar): self._add_widget(self._load_button) # Save Button - save_icon = os.path.join(os.path.dirname(__file__), "images/game-save.svg") + save_icon = join(dirname(__file__), 'images', 'game-save.svg') save_image = gtk.Image() save_image.set_from_file(save_icon) self._save_button = ToolButton() @@ -88,8 +88,8 @@ class CreateToolbar(gtk.Toolbar): self._add_widget(self._equal_pairs) self._equal_pairs.connect('toggled', self._emit_equal_pairs) - self._grouped_icon1 = os.path.join(os.path.dirname(__file__), "images/equal_pairs1.svg") - self._grouped_icon2 = os.path.join(os.path.dirname(__file__), "images/equal_pairs2.svg") + self._grouped_icon1 = join(dirname(__file__), 'images', 'equal_pairs1.svg') + self._grouped_icon2 = join(dirname(__file__), 'images', 'equal_pairs2.svg') self._grouped_image1 = gtk.Image() self._grouped_image2 = gtk.Image() self._grouped_image1.set_from_file(self._grouped_icon1) diff --git a/game.py b/game.py index 61c5118..551956e 100755 --- a/game.py +++ b/game.py @@ -90,11 +90,12 @@ class MemorizeGame(GObject): _logger.error(' Error starting csound performance.') self.sound = 0 - def load_game(self, game_name, size): + def load_game(self, game_name, size, mode): self.set_load_mode('Loading game') if self.model.read(game_name) == 0: self.model.def_grid(size) self.model.data['running'] = 'False' + self.model.data['mode'] = mode logging.debug(' Read setup file %s: %s '%(game_name, self.model.grid)) self.emit('load_game', self.model.data, self.model.grid) else: diff --git a/messenger.py b/messenger.py index 8a403e5..375b0ed 100644 --- a/messenger.py +++ b/messenger.py @@ -21,6 +21,7 @@ import logging _logger = logging.getLogger('memorize-activity') import tempfile +from os import environ from os.path import join, getsize, isfile, dirname, basename from dbus.service import method, signal from dbus.gobject_service import ExportedGObject @@ -165,11 +166,11 @@ class Messenger(ExportedGObject): if not (target == 'all' or target == self._tube.get_unique_name()): return - # first chunk if part == 1: - temp_dir = tempfile.mkdtemp() + tmp_root = join(environ['SUGAR_ACTIVITY_ROOT'], 'instance') + temp_dir = tempfile.mkdtemp(dir=tmp_root) self.temp_file = join(temp_dir, 'game.zip') self.files[filename] = self.temp_file self.f = open(self.temp_file, 'a+b') diff --git a/model.py b/model.py index 4861e65..2b25949 100644 --- a/model.py +++ b/model.py @@ -18,8 +18,8 @@ # import libxml2 -import os -from os.path import join, basename, dirname +from os import environ, makedirs +from os.path import join, basename, dirname, isdir, split, normpath import logging import random import gobject @@ -90,11 +90,11 @@ class Model(object): def __init__(self, game_path, dtd_path = None): self.data = {} if dtd_path == None: - self.dtd_path = os.path.dirname(__file__) + self.dtd_path = dirname(__file__) else: self.dtd_path = dtd_path - if os.path.isdir(game_path): + if isdir(game_path): self.game_path = game_path else: _logger.error('Game_path not found ' +str(e)) @@ -122,29 +122,32 @@ class Model(object): self.count = 0 def read(self, game_file): - temp_folder = tempfile.mkdtemp() - self.data['key'] = os.path.basename(game_file) + tmp_root = join(dirname(__file__), 'instance') + temp_folder = tempfile.mkdtemp(dir=tmp_root) + + self.data['key'] = basename(game_file) self.data['game_file'] = game_file self.data['path'] = temp_folder - self.data['pathimg'] = os.path.join(self.data['path'], 'images') - self.data['pathsnd'] = os.path.join(self.data['path'], 'sounds') + self.data['pathimg'] = join(self.data['path'], 'images') + self.data['pathsnd'] = join(self.data['path'], 'sounds') ''' extracts files in the zip file ''' - game_name = os.path.basename(game_file)[:-4] + game_name = basename(game_file)[:-4] zipFile = zipfile.ZipFile(game_file, "r") for each in zipFile.namelist(): if not each.endswith('/'): - root, name = os.path.split(each) - directory = os.path.normpath(os.path.join(self.data['path'], root)) - if not os.path.isdir(directory): - os.makedirs(directory) - file(os.path.join(directory, name), 'wb').write(zipFile.read(each)) + root, name = split(each) + directory = normpath(join(self.data['path'], root)) + if not isdir(directory): + makedirs(directory) + file(join(directory, name), 'wb').write(zipFile.read(each)) self.pairs = {} ''' reads the configuration from an xml file ''' try: - doc = libxml2.parseFile(join(dirname(__file__), join(self.data['path'], 'game.xml'))) + xml_file = join(environ['SUGAR_ACTIVITY_ROOT'], self.data['path'], 'game.xml') + doc = libxml2.parseFile(xml_file) if doc.validateDtd(self.ctxt, self.dtd): # get the requested nodes @@ -247,7 +250,7 @@ class Model(object): # elem.setProp("color", str(self.pairs[key].props.color)) if doc.validateDtd(self.ctxt, self.dtd): - doc.saveFormatFile(os.path.join(self.game_path, 'game.xml'), 1) + doc.saveFormatFile(join(self.game_path, 'game.xml'), 1) else: _logger.error('Write: Error in validation of the file') doc.freeDoc() -- cgit v0.9.1