예제 #1
0
    def _choose_image(self):
        from sugar3.graphics.objectchooser import ObjectChooser
        try:
            from sugar3.graphics.objectchooser import FILTER_TYPE_GENERIC_MIME
        except:
            FILTER_TYPE_GENERIC_MIME = 'generic_mime'
        from sugar3 import mime

        chooser = None
        name = None

        if hasattr(mime, 'GENERIC_TYPE_IMAGE'):
            # See #2398
            if 'image/svg+xml' not in \
                    mime.get_generic_type(mime.GENERIC_TYPE_IMAGE).mime_types:
                mime.get_generic_type(
                    mime.GENERIC_TYPE_IMAGE).mime_types.append('image/svg+xml')
            try:
                chooser = ObjectChooser(parent=self._reflection.activity,
                                        what_filter=mime.GENERIC_TYPE_IMAGE,
                                        filter_type=FILTER_TYPE_GENERIC_MIME,
                                        show_preview=True)
            except:
                chooser = ObjectChooser(parent=self._reflection.activity,
                                        what_filter=mime.GENERIC_TYPE_IMAGE)
        else:
            try:
                chooser = ObjectChooser(parent=self, what_filter=None)
            except TypeError:
                chooser = ObjectChooser(
                    None, self._reflection.activity,
                    Gtk.DialogFlags.MODAL |
                    Gtk.DialogFlags.DESTROY_WITH_PARENT)

        if chooser is not None:
            try:
                result = chooser.run()
                if result == Gtk.ResponseType.ACCEPT:
                    jobject = chooser.get_selected_object()
                    if jobject and jobject.file_path:
                        name = jobject.metadata['title']
                        mime_type = jobject.metadata['mime_type']
                        _logger.debug('result of choose: %s (%s)' %
                                      (name, str(mime_type)))
            finally:
                chooser.destroy()
                del chooser

            if name is not None:
                pixbuf = self.add_new_picture(jobject.file_path)
                self._reflection.set_modification_time()
                if self._reflection.activity.sharing and pixbuf is not None:
                    self._reflection.activity.send_event(PICTURE_CMD,
                        {"basename": os.path.basename(jobject.file_path),
                         "data": utils.pixbuf_to_base64(pixbuf)})
                    self._reflection.activity.send_event(IMAGE_REFLECTION_CMD,
                        {"obj_id": self._reflection.data["obj_id"],
                         "basename": os.path.basename(jobject.file_path)})

        self._reflection.activity.reset_cursor()
예제 #2
0
    def event_received_cb(self, collab, buddy, msg):
        ''' Data is passed as tuples: cmd:text '''
        command = msg.get("command")
        payload = msg.get("payload")
        logging.debug(command)

        if command == JOIN_CMD:
            # Sharer needs to send reflections database to joiners.
            if self.initiating:
                # Send pictures first.
                for item in self.reflection_data:
                    if 'content' in item:
                        for content in item['content']:
                            if 'image' in content:
                                pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
                                    content['image'], 120, 90)
                                if pixbuf is not None:
                                    data = utils.pixbuf_to_base64(pixbuf)
                                self.send_event(
                                    PICTURE_CMD, {
                                        "image": os.path.basename(
                                            content['image']),
                                        "data": data
                                    })
                data = json.dumps(self.reflection_data)
                self.send_event(SHARE_CMD, {"data": data})
        elif command == NEW_REFLECTION_CMD:
            self._reflect_window.add_new_reflection(payload)
        elif command == TITLE_CMD:
            obj_id = payload.get("obj_id")
            title = payload.get("title")
            for item in self.reflection_data:
                if item['obj_id'] == obj_id:
                    found_the_object = True
                    self._reflect_window.update_title(obj_id, title)
                    break
            if not found_the_object:
                logging.error('Could not find obj_id %s' % obj_id)
        elif command == TAG_CMD:
            obj_id = payload.get("obj_id")
            data = payload.get("data")
            for item in self.reflection_data:
                if item['obj_id'] == obj_id:
                    found_the_object = True
                    self._reflect_window.update_tags(obj_id, data)
                    break
            if not found_the_object:
                logging.error('Could not find obj_id %s' % obj_id)
        elif command == ACTIVITY_CMD:
            obj_id = payload.get("obj_id")
            bundle_id = payload.get("bundle_id")
            for item in self.reflection_data:
                if item['obj_id'] == obj_id:
                    found_the_object = True
                    self._reflect_window.insert_activity(obj_id, bundle_id)
                    break
            if not found_the_object:
                logging.error('Could not find obj_id %s' % obj_id)
        elif command == STAR_CMD:
            obj_id = payload.get("obj_id")
            stars = payload.get("stars")
            for item in self.reflection_data:
                if item['obj_id'] == obj_id:
                    found_the_object = True
                    self._reflect_window.update_stars(obj_id, int(stars))
                    break
            if not found_the_object:
                logging.error('Could not find obj_id %s' % obj_id)
        elif command == COMMENT_CMD:
            found_the_object = False
            # Receive a comment and associated reflection ID
            obj_id = payload.get("obj_id")
            nick = payload.get("nick")
            color = payload.get("color")
            comment = payload.get("comment")
            for item in self.reflection_data:
                if item['obj_id'] == obj_id:
                    found_the_object = True
                    if 'comments' not in item:
                        item['comments'] = []
                    data = {'nick': nick, 'comment': comment, 'color': color}
                    item['comments'].append(data)
                    self._reflect_window.insert_comment(obj_id, data)
                    break
            if not found_the_object:
                logging.error('Could not find obj_id %s' % obj_id)
        elif command == REFLECTION_CMD:
            found_the_object = False
            # Receive a reflection and associated reflection ID
            obj_id = payload.get("obj_id")
            reflection = payload.get("reflection")
            for item in self.reflection_data:
                if item['obj_id'] == obj_id:
                    found_the_object = True
                    if '' not in item:
                        item['content'] = []
                    item['content'].append({'text': reflection})
                    self._reflect_window.insert_reflection(obj_id, reflection)
                    break
            if not found_the_object:
                logging.error('Could not find obj_id %s' % obj_id)
        elif command == IMAGE_REFLECTION_CMD:
            found_the_object = False
            # Receive a picture reflection and associated reflection ID
            obj_id = payload.get("obj_id")
            basename = payload.get("basename")
            for item in self.reflection_data:
                if item['obj_id'] == obj_id:
                    found_the_object = True
                    if '' not in item:
                        item['content'] = []
                    item['content'].append(
                        {'image': os.path.join(self.tmp_path, basename)})
                    self._reflect_window.insert_picture(
                        obj_id, os.path.join(self.tmp_path, basename))
                    break
            if not found_the_object:
                logging.error('Could not find obj_id %s' % obj_id)
        elif command == PICTURE_CMD:
            # Receive a picture (MAYBE DISPLAY IT AS IT ARRIVES?)
            basename = payload.get("basename")
            data = payload.get("data")
            utils.base64_to_file(data, os.path.join(self.tmp_path, basename))
        elif command == SHARE_CMD:
            # Joiner needs to load reflection database.
            if not self.initiating:
                # Note that pictures should be received.
                self.reflection_data = payload
                self._reflect_window.load(self.reflection_data)
                self._waiting_for_reflections = False
                self.reset_cursor()
                if self._joined_alert is not None:
                    self.remove_alert(self._joined_alert)
                    self._joined_alert = None
예제 #3
0
    def _choose_image(self):
        from sugar3.graphics.objectchooser import ObjectChooser
        try:
            from sugar3.graphics.objectchooser import FILTER_TYPE_GENERIC_MIME
        except BaseException:
            FILTER_TYPE_GENERIC_MIME = 'generic_mime'
        from sugar3 import mime

        chooser = None
        name = None

        if hasattr(mime, 'GENERIC_TYPE_IMAGE'):
            # See #2398
            if 'image/svg+xml' not in \
                    mime.get_generic_type(mime.GENERIC_TYPE_IMAGE).mime_types:
                mime.get_generic_type(
                    mime.GENERIC_TYPE_IMAGE).mime_types.append('image/svg+xml')
            try:
                chooser = ObjectChooser(parent=self._reflection.activity,
                                        what_filter=mime.GENERIC_TYPE_IMAGE,
                                        filter_type=FILTER_TYPE_GENERIC_MIME,
                                        show_preview=True)
            except BaseException:
                chooser = ObjectChooser(parent=self._reflection.activity,
                                        what_filter=mime.GENERIC_TYPE_IMAGE)
        else:
            try:
                chooser = ObjectChooser(parent=self, what_filter=None)
            except TypeError:
                chooser = ObjectChooser(
                    None, self._reflection.activity, Gtk.DialogFlags.MODAL
                    | Gtk.DialogFlags.DESTROY_WITH_PARENT)

        if chooser is not None:
            try:
                result = chooser.run()
                if result == Gtk.ResponseType.ACCEPT:
                    jobject = chooser.get_selected_object()
                    if jobject and jobject.file_path:
                        name = jobject.metadata['title']
                        mime_type = jobject.metadata['mime_type']
                        _logger.debug('result of choose: %s (%s)' %
                                      (name, str(mime_type)))
            finally:
                chooser.destroy()
                del chooser

            if name is not None:
                pixbuf = self.add_new_picture(jobject.file_path)
                self._reflection.set_modification_time()
                if self._reflection.activity.sharing and pixbuf is not None:
                    self._reflection.activity.send_event(
                        PICTURE_CMD, {
                            "basename": os.path.basename(jobject.file_path),
                            "data": utils.pixbuf_to_base64(pixbuf)
                        })
                    self._reflection.activity.send_event(
                        IMAGE_REFLECTION_CMD, {
                            "obj_id": self._reflection.data["obj_id"],
                            "basename": os.path.basename(jobject.file_path)
                        })

        self._reflection.activity.reset_cursor()
예제 #4
0
    def event_received_cb(self, collab, buddy, msg):
        ''' Data is passed as tuples: cmd:text '''
        command = msg.get("command")
        payload = msg.get("payload")
        logging.debug(command)

        if command == JOIN_CMD:
            # Sharer needs to send reflections database to joiners.
            if self.initiating:
                # Send pictures first.
                for item in self.reflection_data:
                    if 'content' in item:
                        for content in item['content']:
                            if 'image' in content:
                                pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
                                    content['image'], 120, 90)
                                if pixbuf is not None:
                                    data = utils.pixbuf_to_base64(pixbuf)
                                self.send_event(PICTURE_CMD,
                                    {"image": os.path.basename(content['image']),
                                     "data": data})
                data = json.dumps(self.reflection_data)
                self.send_event(SHARE_CMD, {"data": data})
        elif command == NEW_REFLECTION_CMD:
            self._reflect_window.add_new_reflection(payload)
        elif command == TITLE_CMD:
            obj_id = payload.get("obj_id")
            title = payload.get("title")
            for item in self.reflection_data:
                if item['obj_id'] == obj_id:
                    found_the_object = True
                    self._reflect_window.update_title(obj_id, title)
                    break
            if not found_the_object:
                logging.error('Could not find obj_id %s' % obj_id)
        elif command == TAG_CMD:
            obj_id = payload.get("obj_id")
            data = payload.get("data")
            for item in self.reflection_data:
                if item['obj_id'] == obj_id:
                    found_the_object = True
                    self._reflect_window.update_tags(obj_id, data)
                    break
            if not found_the_object:
                logging.error('Could not find obj_id %s' % obj_id)
        elif command == ACTIVITY_CMD:
            obj_id = payload.get("obj_id")
            bundle_id = payload.get("bundle_id")
            for item in self.reflection_data:
                if item['obj_id'] == obj_id:
                    found_the_object = True
                    self._reflect_window.insert_activity(obj_id, bundle_id)
                    break
            if not found_the_object:
                logging.error('Could not find obj_id %s' % obj_id)
        elif command == STAR_CMD:
            obj_id = payload.get("obj_id")
            stars = payload.get("stars")
            for item in self.reflection_data:
                if item['obj_id'] == obj_id:
                    found_the_object = True
                    self._reflect_window.update_stars(obj_id, int(stars))
                    break
            if not found_the_object:
                logging.error('Could not find obj_id %s' % obj_id)
        elif command == COMMENT_CMD:
            found_the_object = False
            # Receive a comment and associated reflection ID
            obj_id = payload.get("obj_id")
            nick = payload.get("nick")
            color = payload.get("color")
            comment = payload.get("comment")
            for item in self.reflection_data:
                if item['obj_id'] == obj_id:
                    found_the_object = True
                    if not 'comments' in item:
                        item['comments'] = []
                    data = {'nick': nick, 'comment': comment, 'color': color}
                    item['comments'].append(data)
                    self._reflect_window.insert_comment(obj_id, data)
                    break
            if not found_the_object:
                logging.error('Could not find obj_id %s' % obj_id)
        elif command == REFLECTION_CMD:
            found_the_object = False
            # Receive a reflection and associated reflection ID
            obj_id = payload.get("obj_id")
            reflection = payload.get("reflection")
            for item in self.reflection_data:
                if item['obj_id'] == obj_id:
                    found_the_object = True
                    if not '' in item:
                        item['content'] = []
                    item['content'].append({'text': reflection})
                    self._reflect_window.insert_reflection(obj_id, reflection)
                    break
            if not found_the_object:
                logging.error('Could not find obj_id %s' % obj_id)
        elif command == IMAGE_REFLECTION_CMD:
            found_the_object = False
            # Receive a picture reflection and associated reflection ID
            obj_id = payload.get("obj_id")
            basename = payload.get("basename")
            for item in self.reflection_data:
                if item['obj_id'] == obj_id:
                    found_the_object = True
                    if not '' in item:
                        item['content'] = []
                    item['content'].append(
                        {'image': os.path.join(self.tmp_path, basename)})
                    self._reflect_window.insert_picture(
                        obj_id, os.path.join(self.tmp_path, basename))
                    break
            if not found_the_object:
                logging.error('Could not find obj_id %s' % obj_id)
        elif command == PICTURE_CMD:
            # Receive a picture (MAYBE DISPLAY IT AS IT ARRIVES?)
            basename = payload.get("basename")
            data = payload.get("data")
            utils.base64_to_file(data, os.path.join(self.tmp_path, basename))
        elif command == SHARE_CMD:
            # Joiner needs to load reflection database.
            if not self.initiating:
                # Note that pictures should be received.
                self.reflection_data = payload
                self._reflect_window.load(self.reflection_data)
                self._waiting_for_reflections = False
                self.reset_cursor()
                if self._joined_alert is not None:
                    self.remove_alert(self._joined_alert)
                    self._joined_alert = None
예제 #5
0
 def _dump(self, slide):
     ''' Dump data for sharing.'''
     _logger.debug('dumping %s' % (slide.uid))
     data = [slide.uid, slide.colors, slide.title,
             pixbuf_to_base64(activity, slide.pixbuf), slide.desc]
     return self._data_dumper(data)
 def _dump(self, piece, pixbuf):
     ''' Dump data for sharing.'''
     _logger.debug('dumping %s' % (piece))
     data = [piece, pixbuf_to_base64(activity, pixbuf)]
     return json_dump(data)
예제 #7
0
 def event_received_cb(self, text):
     ''' Data is passed as tuples: cmd:text '''
     logging.debug(text[0])
     if text[0] == JOIN_CMD:
         # Sharer needs to send reflections database to joiners.
         if self.initiating:
             # Send pictures first.
             for item in self.reflection_data:
                 if 'content' in item:
                     for content in item['content']:
                         if 'image' in content:
                             pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
                                 content['image'], 120, 90)
                             if pixbuf is not None:
                                 data = utils.pixbuf_to_base64(pixbuf)
                             self.send_event(
                                 '%s|%s|%s' %
                                 (PICTURE_CMD,
                                  os.path.basename(content['image']),
                                  data))
             data = json.dumps(self.reflection_data)
             self.send_event(SHARE_CMD + data)
     elif text[0] == NEW_REFLECTION_CMD:
         cmd, data = text.split('|', 2)
         self._reflect_window.add_new_reflection(data)
     elif text[0] == TITLE_CMD:
         cmd, obj_id, title = text.split('|', 3)
         for item in self.reflection_data:
             if item['obj_id'] == obj_id:
                 found_the_object = True
                 self._reflect_window.update_title(obj_id, title)
                 break
         if not found_the_object:
             logging.error('Could not find obj_id %s' % obj_id)
     elif text[0] == TAG_CMD:
         cmd, obj_id, data = text.split('|', 3)
         for item in self.reflection_data:
             if item['obj_id'] == obj_id:
                 found_the_object = True
                 self._reflect_window.update_tags(obj_id, data)
                 break
         if not found_the_object:
             logging.error('Could not find obj_id %s' % obj_id)
     elif text[0] == ACTIVITY_CMD:
         cmd, obj_id, bundle_id = text.split('|', 3)
         for item in self.reflection_data:
             if item['obj_id'] == obj_id:
                 found_the_object = True
                 self._reflect_window.insert_activity(obj_id, bundle_id)
                 break
         if not found_the_object:
             logging.error('Could not find obj_id %s' % obj_id)
     elif text[0] == STAR_CMD:
         cmd, obj_id, stars = text.split('|', 3)
         for item in self.reflection_data:
             if item['obj_id'] == obj_id:
                 found_the_object = True
                 self._reflect_window.update_stars(obj_id, int(stars))
                 break
         if not found_the_object:
             logging.error('Could not find obj_id %s' % obj_id)
     elif text[0] == COMMENT_CMD:
         found_the_object = False
         # Receive a comment and associated reflection ID
         cmd, obj_id, nick, color, comment = text.split('|', 5)
         for item in self.reflection_data:
             if item['obj_id'] == obj_id:
                 found_the_object = True
                 if not 'comments' in item:
                     item['comments'] = []
                 data = {'nick': nick, 'comment': comment, 'color': color}
                 item['comments'].append(data)
                 self._reflect_window.insert_comment(obj_id, data)
                 break
         if not found_the_object:
             logging.error('Could not find obj_id %s' % obj_id)
     elif text[0] == REFLECTION_CMD:
         found_the_object = False
         # Receive a reflection and associated reflection ID
         cmd, obj_id, reflection = text.split('|', 3)
         for item in self.reflection_data:
             if item['obj_id'] == obj_id:
                 found_the_object = True
                 if not '' in item:
                     item['content'] = []
                 item['content'].append({'text': reflection})
                 self._reflect_window.insert_reflection(obj_id, reflection)
                 break
         if not found_the_object:
             logging.error('Could not find obj_id %s' % obj_id)
     elif text[0] == IMAGE_REFLECTION_CMD:
         found_the_object = False
         # Receive a picture reflection and associated reflection ID
         cmd, obj_id, basename = text.split('|', 3)
         for item in self.reflection_data:
             if item['obj_id'] == obj_id:
                 found_the_object = True
                 if not '' in item:
                     item['content'] = []
                 item['content'].append(
                     {'image': os.path.join(self.tmp_path, basename)})
                 self._reflect_window.insert_picture(
                     obj_id, os.path.join(self.tmp_path, basename))
                 break
         if not found_the_object:
             logging.error('Could not find obj_id %s' % obj_id)
     elif text[0] == PICTURE_CMD:
         # Receive a picture (MAYBE DISPLAY IT AS IT ARRIVES?)
         cmd, basename, data = text.split('|', 3)
         utils.base64_to_file(data, os.path.join(self.tmp_path, basename))
     elif text[0] == SHARE_CMD:
         # Joiner needs to load reflection database.
         if not self.initiating:
             # Note that pictures should be received.
             self.reflection_data = json.loads(text[1:])
             self._reflect_window.load(self.reflection_data)
             self._waiting_for_reflections = False
             self.reset_cursor()
             if self._joined_alert is not None:
                 self.remove_alert(self._joined_alert)
                 self._joined_alert = None