예제 #1
0
    def __init__(self, syncdaemon_instance=None, **kwargs):
        """Init."""
        Buildable.__init__(self)
        Gtk.Alignment.__init__(self, **kwargs)

        if syncdaemon_instance is not None:
            self.sd = syncdaemon_instance
        else:
            self.sd = syncdaemon.SyncDaemon()
        self.sd.on_metadata_ready_callback = self.on_metadata_ready

        self._sd_actions = {
            CONNECT: (self.sd.connect, DISCONNECT),
            DISCONNECT: (self.sd.disconnect, CONNECT),
            START: (self.sd.start, STOP),
            STOP: (self.sd.quit, START),
        }
        self._metadata_dialogs = {}
        self._status_images = build_icon_dict(48)

        folders = FoldersButton(syncdaemon_instance=self.sd)
        shares_to_me = SharesToMeButton(syncdaemon_instance=self.sd)
        shares_to_others = SharesToOthersButton(syncdaemon_instance=self.sd)
        self.public_files = PublicFilesButton(syncdaemon_instance=self.sd)

        buttons = (folders, shares_to_me, shares_to_others, self.public_files)
        for button in buttons:
            self.toolbar.insert(button, -1)
        self.toolbar.set_sensitive(False)

        self.action_button.set_use_stock(True)

        self.update()

        self.add(self.info)
        self.show_all()
예제 #2
0
class Status(Buildable, Gtk.Alignment):
    """The toolbar with info."""

    filename = 'status.ui'
    logger = logger
    _u1_root = UBUNTU_ONE_ROOT

    def __init__(self, syncdaemon_instance=None, **kwargs):
        """Init."""
        Buildable.__init__(self)
        Gtk.Alignment.__init__(self, **kwargs)

        if syncdaemon_instance is not None:
            self.sd = syncdaemon_instance
        else:
            self.sd = syncdaemon.SyncDaemon()
        self.sd.on_metadata_ready_callback = self.on_metadata_ready

        self._sd_actions = {
            CONNECT: (self.sd.connect, DISCONNECT),
            DISCONNECT: (self.sd.disconnect, CONNECT),
            START: (self.sd.start, STOP),
            STOP: (self.sd.quit, START),
        }
        self._metadata_dialogs = {}
        self._status_images = build_icon_dict(48)

        folders = FoldersButton(syncdaemon_instance=self.sd)
        shares_to_me = SharesToMeButton(syncdaemon_instance=self.sd)
        shares_to_others = SharesToOthersButton(syncdaemon_instance=self.sd)
        self.public_files = PublicFilesButton(syncdaemon_instance=self.sd)

        buttons = (folders, shares_to_me, shares_to_others, self.public_files)
        for button in buttons:
            self.toolbar.insert(button, -1)
        self.toolbar.set_sensitive(False)

        self.action_button.set_use_stock(True)

        self.update()

        self.add(self.info)
        self.show_all()

    # custom

    def _update_action_button(self, action):
        """Update the action button according to the SD state."""
        self.action_button.set_label(action)
        self.action_button.set_sensitive(True)

    # GTK callbacks

    @defer.inlineCallbacks
    def on_action_button_clicked(self, button):
        """An action was clicked by the user."""
        self.action_button.set_sensitive(False)

        sd_action, next_action = self._sd_actions[button.get_label()]
        yield sd_action()

        self._update_action_button(next_action)

    def on_metadata_clicked(self, widget, data=None):
        """Show metadata for a path choosen by the user."""
        res = self.file_chooser.run()
        self.file_chooser.hide()
        if res != Gtk.FileChooserAction.OPEN:
            return

        path = self.file_chooser.get_filename()
        assert path is not None

        dialog = MetadataDialog()
        self._metadata_dialogs[path] = dialog
        self.sd.get_metadata(path)
        dialog.run()

    def on_file_chooser_open_clicked(self, widget, data=None):
        """Close the file_chooser dialog."""
        self.file_chooser.response(Gtk.FileChooserAction.OPEN)

    def on_file_chooser_show(self, widget, data=None):
        """Close the file_chooser dialog."""
        self.file_chooser.set_current_folder(self._u1_root)

    # SyncDaemon callbacks

    @log(logger, level=logging.INFO)
    def on_initial_data_ready(self):
        """Initial data is now available in syncdaemon."""
        self.toolbar.set_sensitive(True)
        self.public_files.set_sensitive(False)

    @log(logger, level=logging.INFO)
    def on_initial_online_data_ready(self):
        """Online initial data is now available in syncdaemon."""
        self.public_files.set_sensitive(True)

    @log(logger)
    def on_metadata_ready(self, path, metadata):
        """Lower layer has the requested metadata for 'path'."""
        if path not in self._metadata_dialogs:
            logger.info("on_metadata_ready: path %r not in stored paths!",
                        path)
            return

        dialog = self._metadata_dialogs[path]
        dialog.got_metadata(path, metadata)

    def update(self, *args, **kwargs):
        """Update UI based on SD current state."""
        current_state = self.sd.current_state
        logger.debug('updating UI with state %r', current_state)

        state = current_state.state
        status, next_action = ACTION_MAP[state]
        if state == syncdaemon.STATE_IDLE:
            self.status_image.set_from_pixbuf(self._status_images['idle'])
        elif state in (syncdaemon.STATE_CONNECTING, syncdaemon.STATE_STARTING,
                       syncdaemon.STATE_WORKING):
            self.status_image.set_from_pixbuf(self._status_images['working'])
        else:
            self.status_image.set_from_pixbuf(self._status_images['alert'])

        self._update_action_button(next_action)
        self.status_label.set_text(status)