Ejemplo n.º 1
0
    def _update_attachment(self):
        filters = get_filters_for_attachment()
        with selectfile(_("Select attachment"), filters=filters) as sf:
            rv = sf.run()
            filename = sf.get_filename()
            if rv != Gtk.ResponseType.OK or not filename:
                return

        data = open(filename, 'rb').read()
        mimetype = Gio.content_type_guess(filename, data)[0]
        if self.attachment is None:
            self.attachment = Attachment(store=self.store)
        self.attachment.name = str(os.path.basename(filename))
        self.attachment.mimetype = str(mimetype)
        self.attachment.blob = data
        self._update_widget()
Ejemplo n.º 2
0
    def _update_attachment(self):
        filters = get_filters_for_attachment()
        with selectfile(_("Select attachment"), filters=filters) as sf:
            rv = sf.run()
            filename = sf.get_filename()
            if rv != gtk.RESPONSE_OK or not filename:
                return

        data = open(filename, 'rb').read()
        mimetype = gio.content_type_guess(filename, data, False)
        if self.attachment is None:
            self.attachment = Attachment(store=self.store)
        self.attachment.name = unicode(os.path.basename(filename))
        self.attachment.mimetype = unicode(mimetype)
        self.attachment.blob = data
        self._update_widget()
Ejemplo n.º 3
0
    def _on_attachment_chooser__file_set(self, button):
        filename = self.attachment_chooser.get_filename()
        data = open(filename, 'rb').read()
        mimetype = str(Gio.content_type_guess(filename, data))

        if self._attachment is None:
            self._attachment = Attachment(store=self.store)
        self._attachment.name = str(os.path.basename(filename))
        self._attachment.mimetype = mimetype
        self._attachment.blob = data
Ejemplo n.º 4
0
    def on_attachment_chooser__file_set(self, button):
        print("FILESET ================")
        filename = self.attachment_chooser.get_filename()
        data = open(filename, 'rb').read()
        mimetype = str(Gio.content_type_guess(filename, data))

        if self.model.attachment is None:
            self.model.attachment = Attachment()#store=self.store)
        self.model.attachment.name = str(os.path.basename(filename))
        self.model.attachment.mimetype = mimetype
        self.model.attachment.blob = data
        print("FILESET2============================")
Ejemplo n.º 5
0
    def _update_attachment(self):
        filters = get_filters_for_attachment()
        with selectfile(_("Select attachment"), filters=filters) as sf:
            rv = sf.run()
            filename = sf.get_filename()
            if rv != gtk.RESPONSE_OK or not filename:
                return

        data = open(filename, 'rb').read()
        mimetype = gio.content_type_guess(filename, data, False)
        if self.attachment is None:
            self.attachment = Attachment(store=self.store)
        self.attachment.name = unicode(os.path.basename(filename))
        self.attachment.mimetype = unicode(mimetype)
        self.attachment.blob = data
        self._update_widget()
Ejemplo n.º 6
0
    def _update_attachment(self):
        filters = get_filters_for_attachment()
        with selectfile(_("Select attachment"), filters=filters) as sf:
            rv = sf.run()
            filename = sf.get_filename()
            if rv != Gtk.ResponseType.OK or not filename:
                return

        data = open(filename, 'rb').read()
        mimetype = Gio.content_type_guess(filename, data)[0]
        if self.attachment is None:
            self.attachment = Attachment(store=self.store)
        self.attachment.name = str(os.path.basename(filename))
        self.attachment.mimetype = str(mimetype)
        self.attachment.blob = data
        self._update_widget()
Ejemplo n.º 7
0
 def test_get_description(self):
     attachment = Attachment(name=u'TesteAttachment')
     self.assertEquals(attachment.get_description(), u'TesteAttachment')
Ejemplo n.º 8
0
class AttachmentField(Field):
    """This field allows attaching files to models.

      The graphical interface for it contains:

        * a button, which you can click if there's an attachment. The attachment
          will open in the default system application based on its mimetype
        * the add_button, which you can click to add an attachment in case
          there's none
        * the edit_button, which you can click to change an attachment
        * the delete_button, which you can click to delete an attachment

      It is the editor's responsibility to get the attachment and associate it
      to the model. For example::

         self.model.attachment = self.fields['attachment'].attachment
    """
    widget_data_type = object

    has_add_button = True
    has_edit_button = True
    has_delete_button = True

    no_attachment_lbl = _('No attachment.')

    attachment = GObject.Property(type=object, default=None)

    def build_widget(self):
        button = Gtk.Button()
        button.connect('clicked', self._on_open_button__clicked)

        box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=4)
        button.add(box)

        self._label = Gtk.Label(label=self.no_attachment_lbl)
        self._label.set_ellipsize(Pango.EllipsizeMode.END)
        self._label.set_alignment(0.5, 0.5)
        box.pack_start(self._label, True, True, 0)

        self.image = Gtk.Image()
        box.pack_end(self.image, False, False, 0)

        self.sep = Gtk.VSeparator()
        box.pack_start(self.sep, False, False, 0)

        button.show_all()

        return button

    def populate(self, attachment):
        self.store = get_store_for_field(self)
        self.attachment = attachment
        self._update_widget()

    def _update_widget(self):
        has_attachment = bool(self.attachment and self.attachment.blob)

        if has_attachment:
            self._label.set_label(self.attachment.get_description())
            app_info = Gio.app_info_get_default_for_type(
                self.attachment.mimetype, must_support_uris=False)
            if app_info:
                gicon = app_info.get_icon()
                self.image.set_from_gicon(gicon, Gtk.IconSize.SMALL_TOOLBAR)
        else:
            self._label.set_label(self.no_attachment_lbl)

        can_open = bool(has_attachment and app_info)

        self._open_button__set_sensitive(can_open)
        self.add_button.set_sensitive(not has_attachment)
        self.edit_button.set_sensitive(has_attachment)
        self.delete_button.set_sensitive(has_attachment)

    def _open_button__set_sensitive(self, can_open):
        self.widget.set_sensitive(can_open)
        self.image.set_visible(can_open)
        self.sep.set_visible(can_open)

    def delete_button_clicked(self, button):
        if not yesno(_("Are you sure you want to remove the attachment?"),
                     Gtk.ResponseType.NO, _("Remove"), _("Don't remove")):
            return

        self.attachment.blob = None
        self._update_widget()

    def add_button_clicked(self, button):
        self._update_attachment()

    def edit_button_clicked(self, button):
        self._update_attachment()

    def _update_attachment(self):
        filters = get_filters_for_attachment()
        with selectfile(_("Select attachment"), filters=filters) as sf:
            rv = sf.run()
            filename = sf.get_filename()
            if rv != Gtk.ResponseType.OK or not filename:
                return

        data = open(filename, 'rb').read()
        mimetype = Gio.content_type_guess(filename, data)[0]
        if self.attachment is None:
            self.attachment = Attachment(store=self.store)
        self.attachment.name = str(os.path.basename(filename))
        self.attachment.mimetype = str(mimetype)
        self.attachment.blob = data
        self._update_widget()

    def _on_open_button__clicked(self, widget):
        assert self.attachment
        assert self.attachment.blob

        name = '%s-%s' % ('stoq-attachment', self.attachment.get_description())
        with tempfile.NamedTemporaryFile(suffix=name, delete=False) as f:
            filename = f.name
            f.write(self.attachment.blob)

        gfile = Gio.File.new_for_path(filename)
        app_info = Gio.app_info_get_default_for_type(self.attachment.mimetype,
                                                     False)
        app_info.launch([gfile])
Ejemplo n.º 9
0
class AttachmentField(Field):
    """This field allows attaching files to models.

      The graphical interface for it contains:

        * a button, which you can click if there's an attachment. The attachment
          will open in the default system application based on its mimetype
        * the add_button, which you can click to add an attachment in case
          there's none
        * the edit_button, which you can click to change an attachment
        * the delete_button, which you can click to delete an attachment

      It is the editor's responsibility to get the attachment and associate it
      to the model. For example::

         self.model.attachment = self.fields['attachment'].attachment
    """
    widget_data_type = object

    has_add_button = True
    has_edit_button = True
    has_delete_button = True

    no_attachment_lbl = _('No attachment.')

    def build_widget(self):
        button = gtk.Button()
        button.connect('clicked', self._on_open_button__clicked)

        box = gtk.HBox(False, 4)
        button.add(box)

        self._label = gtk.Label(self.no_attachment_lbl)
        self._label.set_ellipsize(pango.ELLIPSIZE_END)
        self._label.set_alignment(0.5, 0.5)
        box.pack_start(self._label, True, True, 0)

        self.image = gtk.Image()
        box.pack_end(self.image, False, False, 0)

        self.sep = gtk.VSeparator()
        box.pack_start(self.sep, False, False, 0)

        button.show_all()

        return button

    def populate(self, attachment):
        self.store = get_store_for_field(self)
        self.attachment = attachment
        self._update_widget()

    def _update_widget(self):
        has_attachment = bool(self.attachment and self.attachment.blob)

        if has_attachment:
            self._label.set_label(self.attachment.get_description())
            app_info = gio.app_info_get_default_for_type(
                self.attachment.mimetype,
                must_support_uris=False)
            if app_info:
                gicon = app_info.get_icon()
                self.image.set_from_gicon(gicon, gtk.ICON_SIZE_SMALL_TOOLBAR)
        else:
            self._label.set_label(self.no_attachment_lbl)

        can_open = bool(has_attachment and app_info)

        self._open_button__set_sensitive(can_open)
        self.add_button.set_sensitive(not has_attachment)
        self.edit_button.set_sensitive(has_attachment)
        self.delete_button.set_sensitive(has_attachment)

    def _open_button__set_sensitive(self, can_open):
        self.widget.set_sensitive(can_open)
        self.image.set_visible(can_open)
        self.sep.set_visible(can_open)

    def delete_button_clicked(self, button):
        if not yesno(_("Are you sure you want to remove the attachment?"),
                     gtk.RESPONSE_NO, _("Remove"), _("Don't remove")):
            return

        self.attachment.blob = None
        self._update_widget()

    def add_button_clicked(self, button):
        self._update_attachment()

    def edit_button_clicked(self, button):
        self._update_attachment()

    def _update_attachment(self):
        filters = get_filters_for_attachment()
        with selectfile(_("Select attachment"), filters=filters) as sf:
            rv = sf.run()
            filename = sf.get_filename()
            if rv != gtk.RESPONSE_OK or not filename:
                return

        data = open(filename, 'rb').read()
        mimetype = gio.content_type_guess(filename, data, False)
        if self.attachment is None:
            self.attachment = Attachment(store=self.store)
        self.attachment.name = unicode(os.path.basename(filename))
        self.attachment.mimetype = unicode(mimetype)
        self.attachment.blob = data
        self._update_widget()

    def _on_open_button__clicked(self, widget):
        assert self.attachment
        assert self.attachment.blob

        name = '%s-%s' % ('stoq-attachment', self.attachment.get_description())
        with tempfile.NamedTemporaryFile(suffix=name, delete=False) as f:
            filename = f.name
            f.write(self.attachment.blob)

        gfile = gio.File(path=filename)
        app_info = gio.app_info_get_default_for_type(
            self.attachment.mimetype, False)
        app_info.launch([gfile])
Ejemplo n.º 10
0
 def test_get_description(self):
     attachment = Attachment(name=u'TesteAttachment')
     self.assertEquals(attachment.get_description(), u'TesteAttachment')