Example #1
0
    def __init__(self, path, provider, parent):
        self._window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        self.path = path
        self._provider = provider
        self._parent = parent
        self._application = self._parent._parent
        self._page_count = 0

        associations_manager = self._application.associations_manager
        self._mime_type = associations_manager.get_mime_type(path)

        if associations_manager.is_mime_type_unknown(self._mime_type):
            data = associations_manager.get_sample_data(path, provider)
            self._mime_type = associations_manager.get_mime_type(data=data)

        # configure window
        self._window.set_title(
            _('{0} - Viewer').format(os.path.basename(self.path)))
        self._window.set_size_request(800, 600)
        self._window.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
        self._window.set_resizable(True)
        self._window.set_skip_taskbar_hint(False)
        self._window.set_wmclass('Sunflower', 'Sunflower')
        self._window.set_border_width(0)

        # connect signals
        self._window.connect('destroy', self._handle_destroy)
        self._window.connect('key-press-event', self._handle_key_press)

        # create user interface according to mime type
        vbox = gtk.VBox(homogeneous=False, spacing=0)
        self.status_bar = StatusBar()
        self.status_bar.set_border_width(2)
        self.status_bar.add_group_with_icon('mime_type', 'document-properties',
                                            self._mime_type)
        self.status_bar.show()

        self._notebook = gtk.Notebook()
        self._notebook.set_border_width(2)

        # create page for executables
        if self._mime_type in ('application/x-executable', 'application/x-sharedlib') \
        and executable_exists('nm'):
            # get output from command
            data = ''
            try:
                output = subprocess.Popen(
                    ['nm', '-al', path], stdout=subprocess.PIPE).communicate()

                data = output[0]

            except OSError as error:
                # report error to user
                raise error

            # create new page
            self._create_text_page(_('Executable'), data)

        # create text page if needed
        if associations_manager.is_mime_type_subset(self._mime_type,
                                                    'text/plain'):
            # get data from the file
            raw_file = self._provider.get_file_handle(self.path, FileMode.READ)
            data = raw_file.read()
            raw_file.close()

            # create new page
            self._create_text_page(_('Text'), data)

        # create image page if needed
        if self._mime_type.startswith('image/'):
            container = gtk.ScrolledWindow()
            container.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
            container.set_shadow_type(gtk.SHADOW_NONE)
            container.set_border_width(5)
            viewport = gtk.Viewport()

            image = gtk.Image()
            image.set_from_file(self.path)

            viewport.add(image)
            container.add(viewport)
            self._insert_page(_('Image'), container)

        # create extensions
        self._create_extensions()

        # pack user interface
        vbox.pack_start(self._notebook, True, True, 0)
        vbox.pack_start(self.status_bar, False, False, 0)

        self._window.add(vbox)

        # show all widgets if there are pages present
        if self._page_count > 0:
            self._window.show_all()

        else:
            # show information and close window
            dialog = gtk.MessageDialog(
                self._application, gtk.DIALOG_DESTROY_WITH_PARENT,
                gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
                _('Viewer is unable to display this file type.'))
            dialog.run()
            dialog.destroy()

            self._window.destroy()