Example #1
0
class FavoriteItem(Item):
    """
    Item class for favorite items
    """
    def __init__(self, parent, fav, fav_action='edit'):
        """ """
        Item.__init__(self, parent, skin_type='video')
        logger.log(9,
                   'FavoriteItem.__init__(parent=%r, fav=%r, fav_action=%r)',
                   parent, fav, fav_action)
        self.recordclient = RecordClient()
        self.fav = fav
        self.name = self.origname = fav.name
        self.title = fav.title
        self.fav_action = fav_action
        if hasattr(fav, 'allowDuplicates'):
            self.allowDuplicates = int(fav.allowDuplicates)

        if hasattr(self.fav, 'onlyNew'):
            self.onlyNew = int(fav.onlyNew)

        self.week_days = (_('Monday'), _('Tuesday'), _('Wednesday'),
                          _('Thursday'), _('Friday'), _('Saturday'),
                          _('Sunday'))

        if fav.channel == 'ANY':
            self.channel = _('ANY CHANNEL')
        else:
            self.channel = fav.channel
        if fav.dow == 'ANY':
            self.dow = _('ANY DAY')
        else:
            self.dow = self.week_days[int(fav.dow)]
        if fav.mod == 'ANY':
            self.mod = _('ANY TIME')
        else:
            try:
                self.mod = time.strftime(config.TV_TIME_FORMAT,
                                         time.gmtime(float(int(fav.mod) * 60)))
            except:
                print 'Cannot add "%s" to favorites' % fav.name

        # needed by the inputbox handler
        self.menuw = None

    def actions(self):
        logger.log(9, 'actions()')
        return [(self.display_submenu, _('Edit favorite')),
                (self.priority_up, _('Increase priority')),
                (self.priority_down, _('Decrease priority')),
                (self.rem_favorite, _('Remove favorite'))]

    def display_submenu(self, arg=None, menuw=None):
        """ Display menu for a favorite

        With this menu the user can made a program a favorite.
        All attributes of a favorite can be edited here and in the end
        the user must select 'save changes' to finally create the favorite.
        """
        logger.log(9, 'display_submenu(arg=%r, menuw=%r)', arg, menuw)
        ### create menu items for editing the favorites attributes
        items = []

        items.append(
            menu.MenuItem(_('Name') + u'\t' + _(self.name),
                          action=self.mod_name))
        items.append(
            menu.MenuItem(_('Channel') + u'\t' + _(self.channel),
                          action=self.mod_channel))
        items.append(
            menu.MenuItem(_('Day of the Week') + u'\t' + _(self.dow),
                          action=self.mod_day))
        items.append(
            menu.MenuItem(_('Time of Day') + u'\t' + _(self.mod),
                          action=self.mod_time))

        if config.TV_RECORD_DUPLICATE_DETECTION:
            if self.allowDuplicates:
                value = _('Allow duplicates')
            else:
                value = _('Prevent duplicates')
            items.append(
                menu.MenuItem(_('Duplicate Recordings') + u'\t' + _(value),
                              action=self.alter_prop,
                              arg=('dup', not self.allowDuplicates)))

        if config.TV_RECORD_ONLY_NEW_DETECTION:
            if self.onlyNew:
                value = _('Only new episodes')
            else:
                value = _('All episodes')
            items.append(
                menu.MenuItem(_('Episode Filter') + u'\t' + _(value),
                              action=self.alter_prop,
                              arg=('new', not self.onlyNew)))

        # XXX: priorities aren't quite supported yet
        if 0:
            (status, favorites) = self.recordclient.getFavoritesNow()
            if status and len(favorites) > 1:
                items.append(
                    menu.MenuItem(_('Modify priority'),
                                  action=self.mod_priority))

        ### save favorite
        items.append(
            menu.MenuItem(_('Save changes') + u'\t', action=self.save_changes))

        ### remove this program from favorites
        if not self.fav_action == 'add':
            items.append(
                menu.MenuItem(_('Remove favorite') + u'\t',
                              action=self.rem_favorite))

        ### put the whole menu together
        favorite_menu = menu.Menu(_('Favorite Menu'),
                                  items,
                                  item_types='tv favorite menu')
        favorite_menu.infoitem = self
        favorite_menu.is_submenu = True
        favorite_menu.table = (50, 50)
        menuw.pushmenu(favorite_menu)
        menuw.refresh()

    ### Actions:

    def mod_name(self, arg=None, menuw=None):
        """ Modify name

        This opens a input box to ask the user for a new name for this favorite.
        The default name of a favorite is the name of the program.
        """
        logger.log(9, 'mod_name(arg=%r, menuw=%r)', arg, menuw)
        self.menuw = menuw
        InputBox(text=_('Alter Name'), handler=self.alter_name, \
            width=osd.get_singleton().width - config.OSD_OVERSCAN_LEFT - 20, input_text=self.name).show()

    def alter_name(self, name):
        """ set the new name"""
        logger.log(9, 'alter_name(name=%r)', name)
        if name:
            self.name = self.fav.name = name.strip()
        menustack = self.menuw.menustack[-1]
        menustack.selected.dirty = True
        self.menuw.refresh()

    def mod_channel(self, arg=None, menuw=None):
        """Modify channel"""
        logger.log(9, 'mod_channel(arg=%r, menuw=%r)', arg, menuw)
        items = []

        items.append(
            menu.MenuItem('ANY CHANNEL',
                          action=self.alter_prop,
                          arg=('channel', 'ANY')))

        for chanline in config.TV_CHANNELS:
            items.append(
                menu.MenuItem(chanline[1],
                              action=self.alter_prop,
                              arg=('channel', chanline[1])))

        favorite_menu = menu.Menu(_('Modify Channel'),
                                  items,
                                  item_types='tv favorite menu')
        favorite_menu.infoitem = self
        menuw.pushmenu(favorite_menu)
        menuw.refresh()

    def mod_day(self, arg=None, menuw=None):
        """ Modify day

        Opens a submenu where the day of the week of a favorite can be configured.
        """
        logger.log(9, 'mod_day(arg=%r, menuw=%r)', arg, menuw)
        items = []

        items.append(
            menu.MenuItem(_('ANY DAY'),
                          action=self.alter_prop,
                          arg=('dow', 'ANY')))

        for i in range(len(self.week_days)):
            items.append(
                menu.MenuItem(self.week_days[i],
                              action=self.alter_prop,
                              arg=('dow', i)))

        favorite_menu = menu.Menu(_('Modify Day'),
                                  items,
                                  item_types='tv favorite menu')
        favorite_menu.infoitem = self
        menuw.pushmenu(favorite_menu)
        menuw.refresh()

    def mod_time(self, arg=None, menuw=None):
        """ Modify time

        Opens a submenu where the time of a favorite can be configured.
        """
        logger.log(9, 'mod_time(arg=%r, menuw=%r)', arg, menuw)
        items = []

        items.append(
            menu.MenuItem(_('ANY TIME'),
                          action=self.alter_prop,
                          arg=('mod', 'ANY')))

        for i in range(48):
            mod = i * 30
            items.append(menu.MenuItem(time.strftime(config.TV_TIME_FORMAT, time.gmtime(float(mod * 60))), \
                action=self.alter_prop, arg=('mod', mod)))

        favorite_menu = menu.Menu(_('Modify Time'),
                                  items,
                                  item_types='tv favorite menu')
        favorite_menu.infoitem = self
        menuw.pushmenu(favorite_menu)
        menuw.refresh()

    def alter_prop(self, arg=(None, None), menuw=None):
        """ Alter a favorites property

        This function is where the properties of a favorite really are changed.
        """
        logger.log(9, 'alter_prop(arg=%r, menuw=%r)', arg, menuw)
        (prop, val) = arg
        new_text = None
        new_arg = None
        back = True

        if prop == 'channel':
            if val == 'ANY':
                self.channel = 'ANY CHANNEL'
                self.fav.channel = 'ANY'
            else:
                self.channel = val
                self.fav.channel = val
            new_text = _('Channel') + u'\t' + _(self.channel)

        elif prop == 'dow':
            if val == 'ANY':
                self.dow = 'ANY DAY'
                self.fav.dow = 'ANY'
            else:
                self.dow = self.week_days[val]
                self.fav.dow = val
            new_text = _('Day of the Week') + u'\t' + _(self.dow)

        elif prop == 'mod':
            if val == 'ANY':
                self.mod = 'ANY TIME'
                self.fav.mod = 'ANY'
            else:
                # self.mod = tv_util.minToTOD(val)
                self.mod = time.strftime(config.TV_TIME_FORMAT,
                                         time.gmtime(float(val * 60)))
                self.fav.mod = val
            new_text = _('Time of Day') + u'\t' + _(self.mod)

        elif prop == 'dup':
            self.allowDuplicates = self.fav.allowDuplicates = val
            if val:
                value = _('Allow Duplicates')
            else:
                value = _('Prevent Duplicates')

            new_text = _('Duplicate Recordings') + u'\t' + value
            new_arg = ('dup', not val)
            back = False

        elif prop == 'new':
            self.onlyNew = self.fav.onlyNew = val
            if val:
                value = _('Only New Episodes')
            else:
                value = _('All New Episodes')
            new_text = _('Episode Filter') + u'\t' + value
            new_arg = ('new', not val)
            back = False

        if menuw and new_text:
            menu = menuw.menustack[back and -2 or -1]
            menu.selected.name = new_text
            if new_arg:
                menu.selected.arg = new_arg
            menu.selected.dirty = True
            if back:
                menuw.back_one_menu(arg='reload')
            else:
                menuw.refresh()

    def save_changes(self, arg=None, menuw=None):
        """
        Save favorite
        """
        logger.log(9, 'save_changes(arg=%r, menuw=%r)', arg, menuw)
        # this can take some time, as it means although to update the schedule
        msgtext = _('Saving the changes to this favorite.') + '\n' + _(
            'This may take some time.')
        pop = dialog.show_working_indicator(msgtext)

        if self.fav_action == 'edit':
            # first we remove the old favorite
            (result, msg) = self.recordclient.removeFavoriteNow(self.origname)
        elif self.fav_action == 'add':
            result = True

        if result:
            # create a new edited favorite
            (result, msg) = self.recordclient.addEditedFavoriteNow(
                self.fav.name, self.fav.title, self.fav.channel, self.fav.dow,
                self.fav.mod, self.fav.priority, self.fav.allowDuplicates,
                self.fav.onlyNew)
            if result:
                if menuw:
                    menuw.delete_submenu()
                    if self.fav_action == 'add':
                        menuw.refresh(reload=1)
                self.fav_action = 'edit'
                pop.hide()
            else:
                pop.hide()
                # it is important to show the user this error,
                # because that means the favorite is removed,
                # and must be created again
                msgtext = _('Save failed, favorite was lost') + (':\n%s' % msg)
                dialog.show_alert(msgtext)

    def rem_favorite(self, arg=None, menuw=None):
        """
        Remove favorite
        """
        logger.log(9, 'rem_favorite(arg=%r, menuw=%r)', arg, menuw)
        name = self.origname
        (result, msg) = self.recordclient.removeFavoriteNow(name)
        if result:
            # if this is successfull
            if menuw:
                menuw.delete_submenu()
                menuw.refresh(reload=1)
            # and show a short message of success
            msgtext = text = _('"%s" has been removed from favorites') % name
            dialog.show_alert(msgtext)
        else:
            # if all fails then we should show an error
            msgtext = _('Remove failed') + (':\n%s' % msg)
            dialog.show_alert(msgtext)

    def priority_mod(self, adjust, menuw=None):
        """
        Move current item in the priority list and refresh menu to
        show new position
        """
        name = self.origname
        result = self.recordclient.adjustPriorityNow(name, adjust)
        if result:
            # if this is successfull
            if menuw:
                menuw.delete_submenu()
                menuw.refresh(reload=1)
            else:
                event.Event('MENU_RELOAD').post()
        else:
            # if all fails then we should show an error
            msgtext = _('Priority adjustment failed') + (':\n%s' % msg)
            dialog.show_message(msgtext)

    def priority_up(self, arg=None, menuw=None):
        self.priority_mod(-1, menuw)

    def priority_down(self, arg=None, menuw=None):
        self.priority_mod(1, menuw)
Example #2
0
class EditFavorite(PopupBox):
    """ """

    def __init__(self, parent=None, subject=None, left=None, top=None, width=500, height=350, context=None):
        """
        Initialise an instance of EditFavorite

        @ivar parent:  parent of the class
        @ivar subject: the title of the program
        @ivar left:    x coordinate, Integer
        @ivar top:     y coordinate, Integer
        @ivar width:   width in pixels, Integer
        @ivar height:  height in pixels, Integer
        @ivar context: context in which the object is instantiated
        """
        logger.debug(
            "EditFavorite.__init__(parent=%r, subject=%r, left=%r, top=%r, width=%r, height=%r, context=%r)",
            parent,
            subject,
            left,
            top,
            width,
            height,
            context,
        )

        self.oldname = None
        if context:
            self.context = context
        else:
            context = "guide"

        self.recordclient = RecordClient()
        if isinstance(subject, TvProgram):
            (result, favs) = self.recordclient.getFavorites()
            if result:
                num_favorites = len(favs)
                self.priority = num_favorites + 1
            else:
                self.priority = 1
            self.fav = Favorite(subject.title, subject, TRUE, TRUE, TRUE, self.priority, TRUE, FALSE)

        else:
            self.fav = subject
            self.oldname = self.fav.name

        PopupBox.__init__(self, text=_("Edit Favorite"), x=left, y=top, width=width, height=height)

        self.v_spacing = 15
        self.h_margin = 20
        self.v_margin = 20

        self.internal_h_align = Align.LEFT

        if not self.left:
            self.left = self.osd.width / 2 - self.width / 2
        if not self.top:
            self.top = self.osd.height / 2 - self.height / 2

        name = Label(_("Name") + ":", self, Align.LEFT)
        self.name_input = LetterBoxGroup(text=self.fav.name)
        self.name_input.h_align = Align.NONE
        self.add_child(self.name_input)

        title = Label(_("Title") + ": %s" % self.fav.title, self, Align.LEFT)

        chan = Label(_("Channel") + ":", self, Align.LEFT)

        self.chan_box = OptionBox("ANY")
        self.chan_box.h_align = Align.NONE
        self.chan_box.add_item(text=_("ANY"), value="ANY")

        i = 1
        chan_index = 0
        for ch in tv.epg.channels:
            # if ch.id == self.fav.channel_id:
            if ch.displayname == self.fav.channel:
                chan_index = i
            i += 1
            self.chan_box.add_item(text=ch.displayname, value=ch.displayname)

        self.chan_box.toggle_selected_index(chan_index)
        # This is a hack for setting the OptionBox's label to the current value.
        # It should be done by OptionBox when drawing, but it doesn't work :(
        self.chan_box.change_item(None)
        self.add_child(self.chan_box)

        dow = Label(_("Day of Week") + ":", self, Align.LEFT)
        self.dow_box = OptionBox("ANY DAY")
        self.dow_box.h_align = Align.NONE

        self.dow_box.add_item(text=_("ANY DAY"), value="ANY")

        i = 1
        dow_index = 0
        for dow in (_("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat"), _("Sun")):
            val = "%d" % (i - 1)
            self.dow_box.add_item(text=_(dow), value=val)
            if val == self.fav.dow:
                dow_index = i
            i += 1
        self.dow_box.toggle_selected_index(dow_index)
        # This is a hack for setting the OptionBox's label to the current value.
        # It should be done by OptionBox when drawing, but it doesn't work :(
        self.dow_box.change_item(None)
        self.add_child(self.dow_box)

        tod = Label(_("Time of Day") + ":", self, Align.LEFT)
        self.tod_box = OptionBox("ANY")
        self.tod_box.h_align = Align.NONE
        self.tod_box.add_item(text=_("ANY TIME"), value="ANY")

        i = 0
        tod_index = 0

        for h in range(0, 24):
            for m in (00, 30):
                val = i * 30
                # Little hack: we calculate the hours from Jan 1st, 1970 GMT,
                # and then use strftime to get the string representation
                text = strftime(config.TV_TIME_FORMAT, gmtime(h * 3600 + 60 * m))
                self.tod_box.add_item(text=text, value=val)
                if val == self.fav.mod:
                    tod_index = i + 1
                i += 1
        self.tod_box.toggle_selected_index(tod_index)
        # This is a hack for setting the OptionBox's label to the current
        # value. It should be done by OptionBox when drawing, but it doesn't
        # work :(
        self.tod_box.change_item(None)
        self.add_child(self.tod_box)

        self.save = Button(_("Save"))
        self.add_child(self.save)
        if self.oldname:
            self.remove = Button(_("Remove"))
            self.add_child(self.remove)
        else:
            self.remove = None
        self.cancel = Button(_("CANCEL"))
        self.add_child(self.cancel)

    def removeFavorite(self):
        logger.log(9, "removeFavorite()")
        (result, reason) = self.recordclient.removeFavoriteNow(self.oldname)
        if result:
            searcher = None
            if self.parent and self.context == "favorites":
                for child in self.parent.children:
                    if isinstance(child, ViewFavorites):
                        searcher = child
                        break
                if searcher:
                    searcher.refreshList()
                self.destroy()
                if searcher:
                    searcher.draw()
                    self.osd.update()
        else:
            AlertBox(parent=self, text=_("Remove favorite failed") + (":\n%s" % reason)).show()

    def eventhandler(self, event, menuw=None):
        logger.log(9, "eventhandler(event=%r, menuw=%r)", event, menuw)

        if self.get_selected_child() == self.name_input:
            if event == em.INPUT_LEFT:
                self.name_input.change_selected_box("left")
                self.draw()
                return True
            elif event == em.INPUT_RIGHT:
                self.name_input.change_selected_box("right")
                self.draw()
                return True
            elif event == em.INPUT_ENTER:
                self.name_input.get_selected_box().toggle_selected()
                self.chan_box.toggle_selected()
                self.draw()
                return True
            elif event == em.INPUT_UP:
                self.name_input.get_selected_box().charUp()
                self.draw()
                return True
            elif event == em.INPUT_DOWN:
                self.name_input.get_selected_box().charDown()
                self.draw()
                return True
            elif event in em.INPUT_ALL_NUMBERS:
                self.name_input.get_selected_box().cycle_phone_char(event)
                self.draw()
                return True
            elif event == em.INPUT_EXIT:
                self.destroy()
                return True

        elif self.get_selected_child() == self.chan_box:
            if event in (em.INPUT_UP, em.INPUT_DOWN):
                self.chan_box.change_item(event)
                self.draw()
            elif event == em.INPUT_ENTER:
                if self.chan_box.selected or self.chan_box.list.is_visible():
                    self.chan_box.toggle_box()
                    self.draw()
            elif event in (em.INPUT_LEFT, em.MENU_PAGEUP):
                self.chan_box.toggle_selected()
                self.name_input.boxes[0].toggle_selected()
                self.draw()
            elif event in (em.INPUT_RIGHT, em.MENU_PAGEDOWN):
                self.chan_box.toggle_selected()
                self.dow_box.toggle_selected()
                self.draw()
            elif event == em.INPUT_EXIT:
                self.destroy()
            return True

        elif self.get_selected_child() == self.dow_box:
            if event in (em.INPUT_UP, em.INPUT_DOWN):
                self.dow_box.change_item(event)
                self.draw()
            elif event == em.INPUT_ENTER:
                if self.dow_box.selected or self.dow_box.list.is_visible():
                    self.dow_box.toggle_box()
                    self.draw()
            elif event in (em.INPUT_LEFT, em.MENU_PAGEUP):
                self.dow_box.toggle_selected()
                self.chan_box.toggle_selected()
                self.draw()
            elif event in (em.INPUT_RIGHT, em.MENU_PAGEDOWN):
                self.dow_box.toggle_selected()
                self.tod_box.toggle_selected()
                self.draw()
            elif event == em.INPUT_EXIT:
                self.destroy()
                return True
            return True

        elif self.get_selected_child() == self.tod_box:
            if event in (em.INPUT_UP, em.INPUT_DOWN):
                self.tod_box.change_item(event)
                self.draw()
            elif event == em.INPUT_ENTER:
                if self.tod_box.selected or self.tod_box.list.is_visible():
                    self.tod_box.toggle_box()
                    self.draw()
            elif event in (em.INPUT_LEFT, em.MENU_PAGEUP):
                self.tod_box.toggle_selected()
                self.dow_box.toggle_selected()
                self.draw()
            elif event in (em.INPUT_RIGHT, em.MENU_PAGEDOWN):
                self.tod_box.toggle_selected()
                self.save.toggle_selected()
                self.draw()
            elif event == em.INPUT_EXIT:
                self.destroy()
                return True
            return True

        elif self.get_selected_child() == self.save:
            if event == em.INPUT_ENTER:
                # remove the old favourite
                if self.oldname:
                    (result, reason) = self.recordclient.removeFavoriteNow(self.oldname)
                    if not result:
                        AlertBox(parent=self, text=_("Add favorite failed") + (":\n%s" % reason)).show()
                # add the new favourite
                (result, reason) = self.recordclient.addEditedFavoriteNow(
                    self.name_input.get_word(),
                    self.fav.title,
                    self.chan_box.list.get_selected_item().value,
                    self.dow_box.list.get_selected_item().value,
                    self.tod_box.list.get_selected_item().value,
                    self.fav.priority,
                    self.fav.allowDuplicates,
                    self.fav.onlyNew,
                )
                if result:
                    # tv.view_favorites.ViewFavorites(parent=self.parent, text='Favorites').show()
                    self.destroy()
                    AlertBox(parent="osd", text=_("Favorite %s has been saved") % self.name_input.get_word()).show()
                else:
                    AlertBox(parent=self, text=_("Add favorite failed") + (":\n%s" % reason)).show()
                return True
            elif event in (em.INPUT_LEFT, em.MENU_PAGEUP):
                self.save.toggle_selected()
                self.tod_box.toggle_selected()
                self.draw()
            elif event in (em.INPUT_RIGHT, em.MENU_PAGEDOWN):
                self.save.toggle_selected()
                if self.remove:
                    self.remove.toggle_selected()
                else:
                    self.cancel.toggle_selected()
                self.draw()
            elif event == em.INPUT_EXIT:
                self.destroy()
                return True
            return True

        elif self.get_selected_child() == self.remove:
            if event == em.INPUT_ENTER:
                ConfirmBox(
                    text=_("Do you want to remove %s?") % self.name_input.get_word(), handler=self.removeFavorite
                ).show()
                return True
            elif event in (em.INPUT_LEFT, em.MENU_PAGEUP):
                self.save.toggle_selected()
                self.remove.toggle_selected()
                self.draw()
            elif event in (em.INPUT_RIGHT, em.MENU_PAGEDOWN):
                self.remove.toggle_selected()
                self.cancel.toggle_selected()
                self.draw()
            elif event in (em.INPUT_ENTER, em.INPUT_EXIT):
                self.destroy()
                return True
            return True

        elif self.get_selected_child() == self.cancel:
            if event in (em.INPUT_LEFT, em.MENU_PAGEUP):
                if self.remove:
                    self.remove.toggle_selected()
                else:
                    self.save.toggle_selected()
                self.cancel.toggle_selected()
                self.draw()
            elif event in (em.INPUT_ENTER, em.INPUT_EXIT):
                self.destroy()
                return True
            return True
        if event == em.INPUT_EXIT:
            self.destroy()
            return True
        elif event in (em.MENU_PAGEDOWN, em.MENU_PAGEUP):
            return True
        else:
            return self.parent.eventhandler(event)
Example #3
0
class FavoriteItem(Item):
    """
    Item class for favorite items
    """
    def __init__(self, parent, fav, fav_action='edit'):
        """ """
        Item.__init__(self, parent, skin_type='video')
        logger.log( 9, 'FavoriteItem.__init__(parent=%r, fav=%r, fav_action=%r)', parent, fav, fav_action)
        self.recordclient = RecordClient()
        self.fav   = fav
        self.name  = self.origname = fav.name
        self.title = fav.title
        self.fav_action = fav_action
        if hasattr(fav, 'allowDuplicates'):
            self.allowDuplicates = int(fav.allowDuplicates)

        if hasattr(self.fav, 'onlyNew'):
            self.onlyNew = int(fav.onlyNew)

        self.week_days = (_('Monday'), _('Tuesday'), _('Wednesday'), _('Thursday'), _('Friday'), _('Saturday'), _('Sunday'))


        if fav.channel == 'ANY':
            self.channel = _('ANY CHANNEL')
        else:
            self.channel = fav.channel
        if fav.dow == 'ANY':
            self.dow = _('ANY DAY')
        else:
            self.dow = self.week_days[int(fav.dow)]
        if fav.mod == 'ANY':
            self.mod = _('ANY TIME')
        else:
            try:
                self.mod = time.strftime(config.TV_TIME_FORMAT, time.gmtime(float(int(fav.mod) * 60)))
            except:
                print 'Cannot add "%s" to favorites' % fav.name

        # needed by the inputbox handler
        self.menuw = None


    def actions(self):
        logger.log( 9, 'actions()')
        return [( self.display_submenu, _('Edit favorite')), 
                ( self.priority_up, _('Increase priority')), 
                ( self.priority_down, _('Decrease priority')),
                ( self.rem_favorite, _('Remove favorite'))]


    def display_submenu(self, arg=None, menuw=None):
        """ Display menu for a favorite

        With this menu the user can made a program a favorite.
        All attributes of a favorite can be edited here and in the end
        the user must select 'save changes' to finally create the favorite.
        """
        logger.log( 9, 'display_submenu(arg=%r, menuw=%r)', arg, menuw)
        ### create menu items for editing the favorites attributes
        items = []

        items.append(menu.MenuItem(_('Name') + u'\t' + _(self.name), action=self.mod_name))
        items.append(menu.MenuItem(_('Channel') + u'\t' + _(self.channel), action=self.mod_channel))
        items.append(menu.MenuItem(_('Day of the Week') + u'\t' + _(self.dow), action=self.mod_day))
        items.append(menu.MenuItem(_('Time of Day') + u'\t' + _(self.mod), action=self.mod_time))

        if config.TV_RECORD_DUPLICATE_DETECTION:
            if self.allowDuplicates:
                value = _('Allow duplicates')
            else:
                value = _('Prevent duplicates')
            items.append(menu.MenuItem(_('Duplicate Recordings') + u'\t' + _(value),
                                        action=self.alter_prop,
                                        arg=('dup', not self.allowDuplicates)))


        if config.TV_RECORD_ONLY_NEW_DETECTION:
            if self.onlyNew:
                value = _('Only new episodes')
            else:
                value = _('All episodes')
            items.append(menu.MenuItem(_('Episode Filter') + u'\t' + _(value),
                                        action=self.alter_prop,
                                        arg=('new', not self.onlyNew)))

        # XXX: priorities aren't quite supported yet
        if 0:
            (status, favorites) = self.recordclient.getFavoritesNow()
            if status and len(favorites) > 1:
                items.append(menu.MenuItem(_('Modify priority'), action=self.mod_priority))


        ### save favorite
        items.append(menu.MenuItem(_('Save changes') + u'\t', action=self.save_changes))

        ### remove this program from favorites
        if not self.fav_action == 'add':
            items.append(menu.MenuItem(_('Remove favorite') + u'\t', action=self.rem_favorite))

        ### put the whole menu together
        favorite_menu = menu.Menu(_('Favorite Menu'), items, item_types='tv favorite menu')
        favorite_menu.infoitem = self
        favorite_menu.is_submenu = True
        favorite_menu.table = (50, 50)
        menuw.pushmenu(favorite_menu)
        menuw.refresh()



    ### Actions:

    def mod_name(self, arg=None, menuw=None):
        """ Modify name

        This opens a input box to ask the user for a new name for this favorite.
        The default name of a favorite is the name of the program.
        """
        logger.log( 9, 'mod_name(arg=%r, menuw=%r)', arg, menuw)
        self.menuw = menuw
        InputBox(text=_('Alter Name'), handler=self.alter_name, \
            width=osd.get_singleton().width - config.OSD_OVERSCAN_LEFT - 20, input_text=self.name).show()


    def alter_name(self, name):
        """ set the new name"""
        logger.log( 9, 'alter_name(name=%r)', name)
        if name:
            self.name = self.fav.name = name.strip()
        menustack = self.menuw.menustack[-1]
        menustack.selected.dirty = True
        self.menuw.refresh()


    def mod_channel(self, arg=None, menuw=None):
        """Modify channel"""
        logger.log( 9, 'mod_channel(arg=%r, menuw=%r)', arg, menuw)
        items = []

        items.append(menu.MenuItem('ANY CHANNEL', action=self.alter_prop, arg=('channel', 'ANY')))

        for chanline in config.TV_CHANNELS:
            items.append(menu.MenuItem(chanline[1], action=self.alter_prop, arg=('channel', chanline[1])))

        favorite_menu = menu.Menu(_('Modify Channel'), items, item_types='tv favorite menu')
        favorite_menu.infoitem = self
        menuw.pushmenu(favorite_menu)
        menuw.refresh()


    def mod_day(self, arg=None, menuw=None):
        """ Modify day

        Opens a submenu where the day of the week of a favorite can be configured.
        """
        logger.log( 9, 'mod_day(arg=%r, menuw=%r)', arg, menuw)
        items = []

        items.append(menu.MenuItem(_('ANY DAY'), action=self.alter_prop, arg=('dow', 'ANY')))

        for i in range(len(self.week_days)):
            items.append(menu.MenuItem(self.week_days[i], action=self.alter_prop, arg=('dow', i)))

        favorite_menu = menu.Menu(_('Modify Day'), items, item_types = 'tv favorite menu')
        favorite_menu.infoitem = self
        menuw.pushmenu(favorite_menu)
        menuw.refresh()


    def mod_time(self, arg=None, menuw=None):
        """ Modify time

        Opens a submenu where the time of a favorite can be configured.
        """
        logger.log( 9, 'mod_time(arg=%r, menuw=%r)', arg, menuw)
        items = []

        items.append(menu.MenuItem(_('ANY TIME'), action=self.alter_prop, arg=('mod', 'ANY')))

        for i in range(48):
            mod = i * 30
            items.append(menu.MenuItem(time.strftime(config.TV_TIME_FORMAT, time.gmtime(float(mod * 60))), \
                action=self.alter_prop, arg=('mod', mod)))

        favorite_menu = menu.Menu(_('Modify Time'), items, item_types = 'tv favorite menu')
        favorite_menu.infoitem = self
        menuw.pushmenu(favorite_menu)
        menuw.refresh()


    def alter_prop(self, arg=(None, None), menuw=None):
        """ Alter a favorites property

        This function is where the properties of a favorite really are changed.
        """
        logger.log( 9, 'alter_prop(arg=%r, menuw=%r)', arg, menuw)
        (prop, val) = arg
        new_text = None
        new_arg = None
        back = True

        if prop == 'channel':
            if val == 'ANY':
                self.channel = 'ANY CHANNEL'
                self.fav.channel = 'ANY'
            else:
                self.channel = val
                self.fav.channel = val
            new_text = _('Channel') + u'\t' + _(self.channel)

        elif prop == 'dow':
            if val == 'ANY':
                self.dow = 'ANY DAY'
                self.fav.dow = 'ANY'
            else:
                self.dow = self.week_days[val]
                self.fav.dow = val
            new_text = _('Day of the Week') + u'\t' + _(self.dow)

        elif prop == 'mod':
            if val == 'ANY':
                self.mod = 'ANY TIME'
                self.fav.mod = 'ANY'
            else:
                # self.mod = tv_util.minToTOD(val)
                self.mod = time.strftime(config.TV_TIME_FORMAT, time.gmtime(float(val * 60)))
                self.fav.mod = val
            new_text = _('Time of Day') + u'\t' + _(self.mod)

        elif prop == 'dup':
            self.allowDuplicates= self.fav.allowDuplicates= val
            if val:
                value = _('Allow Duplicates')
            else:
                value = _('Prevent Duplicates')

            new_text =  _('Duplicate Recordings') + u'\t' + value
            new_arg = ('dup', not val)
            back = False

        elif prop == 'new':
            self.onlyNew= self.fav.onlyNew = val
            if val:
                value = _('Only New Episodes')
            else:
                value = _('All New Episodes')
            new_text = _('Episode Filter') + u'\t' + value
            new_arg = ('new', not val)
            back = False

        if menuw and new_text:
            menu = menuw.menustack[back and -2 or -1]
            menu.selected.name = new_text
            if new_arg:
                menu.selected.arg = new_arg
            menu.selected.dirty = True
            if back:
                menuw.back_one_menu(arg='reload')
            else:
                menuw.refresh()

    def save_changes(self, arg=None, menuw=None):
        """
        Save favorite
        """
        logger.log( 9, 'save_changes(arg=%r, menuw=%r)', arg, menuw)
        # this can take some time, as it means although to update the schedule
        msgtext = _('Saving the changes to this favorite.')+'\n'+_('This may take some time.')
        pop = dialog.show_working_indicator(msgtext)

        if self.fav_action == 'edit':
            # first we remove the old favorite
            (result, msg) = self.recordclient.removeFavoriteNow(self.origname)
        elif self.fav_action =='add':
            result = True

        if result:
            # create a new edited favorite
            (result, msg) = self.recordclient.addEditedFavoriteNow(self.fav.name,
                self.fav.title, self.fav.channel, self.fav.dow, self.fav.mod,
                self.fav.priority, self.fav.allowDuplicates, self.fav.onlyNew)
            if result:
                if menuw:
                    menuw.delete_submenu()
                    if self.fav_action == 'add':
                        menuw.refresh(reload=1)
                self.fav_action = 'edit'
                pop.hide()
            else:
                pop.hide()
                # it is important to show the user this error,
                # because that means the favorite is removed,
                # and must be created again
                msgtext=_('Save failed, favorite was lost')+(':\n%s' % msg)
                dialog.show_alert(msgtext)


    def rem_favorite(self, arg=None, menuw=None):
        """
        Remove favorite
        """
        logger.log( 9, 'rem_favorite(arg=%r, menuw=%r)', arg, menuw)
        name = self.origname
        (result, msg) = self.recordclient.removeFavoriteNow(name)
        if result:
            # if this is successfull
            if menuw:
                menuw.delete_submenu()
                menuw.refresh(reload=1)
            # and show a short message of success
            msgtext = text=_('"%s" has been removed from favorites') % name
            dialog.show_alert(msgtext)
        else:
            # if all fails then we should show an error
            msgtext = _('Remove failed')+(':\n%s' % msg)
            dialog.show_alert(msgtext)


    def priority_mod(self, adjust, menuw=None):
        """
        Move current item in the priority list and refresh menu to
        show new position
        """
        name = self.origname
        result = self.recordclient.adjustPriorityNow(name, adjust)
        if result:
            # if this is successfull
            if menuw:
                menuw.delete_submenu()
                menuw.refresh(reload=1)
            else:
                event.Event('MENU_RELOAD').post()
        else:
            # if all fails then we should show an error
            msgtext = _('Priority adjustment failed')+(':\n%s' % msg)
            dialog.show_message(msgtext)


    def priority_up(self, arg=None, menuw=None):
        self.priority_mod(-1, menuw)


    def priority_down(self, arg=None, menuw=None):
        self.priority_mod(1, menuw)
Example #4
0
class FavoriteItem(Item):
    """
    Item class for favorite items
    """
    def __init__(self, parent, fav, fav_action='edit'):
        """ """
        Item.__init__(self, parent, skin_type='video')
        _debug_('FavoriteItem.__init__(parent=%r, fav=%r, fav_action=%r)' % (parent, fav, fav_action), 2)
        self.recordclient = RecordClient()
        self.fav   = fav
        self.name  = self.origname = fav.name
        self.title = fav.title
        self.fav_action = fav_action
        # twisted needs FALSE and TRUE, it does not know False and True
        if hasattr(fav,'allowDuplicates') and not fav.allowDuplicates:
            self.allowDuplicates = fav.allowDuplicates = FALSE
        else:
            self.allowDuplicates = fav.allowDuplicates = TRUE
        # twisted needs FALSE and TRUE, it does not know False and True
        if hasattr(fav,'onlyNew') and fav.onlyNew:
            self.onlyNew = fav.onlyNew = TRUE
        else:
            self.onlyNew = fav.onlyNew = FALSE

        self.week_days = (_('Mon'), _('Tue'), _('Wed'), _('Thu'), _('Fri'), _('Sat'), _('Sun'))

        if fav.channel == 'ANY':
            self.channel = _('ANY CHANNEL')
        else:
            self.channel = fav.channel
        if fav.dow == 'ANY':
            self.dow = _('ANY DAY')
        else:
            self.dow = self.week_days[int(fav.dow)]
        if fav.mod == 'ANY':
            self.mod = _('ANY TIME')
        else:
            try:
                self.mod = time.strftime(config.TV_TIME_FORMAT, time.gmtime(float(int(fav.mod) * 60)))
            except:
                print 'Cannot add "%s" to favorites' % fav.name

        # needed by the inputbox handler
        self.menuw = None

        self.red_action = (self.display_submenu,_('Edit favorite'))
        self.green_action = (self.rem_favorite, _('Remove favorite'))


    def actions(self):
        _debug_('actions()', 2)
        return [( self.display_submenu , _('Edit favorite'))]


    def display_submenu(self, arg=None, menuw=None):
        """ Display menu for a favorite

        With this menu the user can made a program a favorite.
        All attributes of a favorite can be edited here and in the end
        the user must select 'save changes' to finally create the favorite.
        """
        _debug_('display_submenu(arg=%r, menuw=%r)' % (arg, menuw), 2)
        ### create menu items for editing the favorites attributes
        items = []

        items.append(menu.MenuItem(_('Modify name'), action=self.mod_name))
        items.append(menu.MenuItem(_('Modify channel'), action=self.mod_channel))
        items.append(menu.MenuItem(_('Modify day of week'), action=self.mod_day))
        items.append(menu.MenuItem(_('Modify time of day'), action=self.mod_time))

        if config.TV_RECORD_DUPLICATE_DETECTION:
            if self.allowDuplicates:
                items.append(menu.MenuItem(_('Prevent Recording of Duplicates'), \
                    action=self.alter_prop, arg=('dup', 'False')))
            else:
                items.append(menu.MenuItem(_('Allow Recording of Duplicates'),
                    action=self.alter_prop, arg=('dup', 'True')))


        if config.TV_RECORD_ONLY_NEW_DETECTION:
            if self.onlyNew:
                items.append(menu.MenuItem(_('Record All Episodes'), action=self.alter_prop, arg=('new', 'False')))
            else:
                items.append(menu.MenuItem(_('Record Only New Episodes'), action=self.alter_prop, arg=('new', 'True')))

        # XXX: priorities aren't quite supported yet
        if 0:
            (status, favorites) = self.recordclient.getFavoritesNow()
            if status and len(favorites) > 1:
                items.append(menu.MenuItem(_('Modify priority'), action=self.mod_priority))


        ### save favorite
        items.append(menu.MenuItem(_('Save changes'), action=self.save_changes))

        ### remove this program from favorites
        if not self.fav_action == 'add':
            items.append(menu.MenuItem(_('Remove favorite'), action=self.rem_favorite))

        ### put the whole menu together
        favorite_menu = menu.Menu(_('Favorite Menu'), items, item_types='tv favorite menu')
        favorite_menu.infoitem = self
        favorite_menu.is_submenu = True
        menuw.pushmenu(favorite_menu)
        menuw.refresh()



    ### Actions:

    def mod_name(self, arg=None, menuw=None):
        """ Modify name

        This opens a input box to ask the user for a new name for this favorite.
        The default name of a favorite is the name of the program.
        """
        _debug_('mod_name(arg=%r, menuw=%r)' % (arg, menuw), 2)
        self.menuw = menuw
        InputBox(text=_('Alter Name'), handler=self.alter_name, \
            width=osd.get_singleton().width - config.OSD_OVERSCAN_LEFT - 20, input_text=self.name).show()


    def alter_name(self, name):
        """ set the new name"""
        _debug_('alter_name(name=%r)' % (name,), 2)
        if name:
            self.name = self.fav.name = name.strip()

        self.menuw.refresh()


    def mod_channel(self, arg=None, menuw=None):
        """Modify channel"""
        _debug_('mod_channel(arg=%r, menuw=%r)' % (arg, menuw), 2)
        items = []

        items.append(menu.MenuItem('ANY CHANNEL', action=self.alter_prop, arg=('channel', 'ANY')))

        for chanline in config.TV_CHANNELS:
            items.append(menu.MenuItem(chanline[1], action=self.alter_prop, arg=('channel', chanline[1])))

        favorite_menu = menu.Menu(_('Modify Channel'), items, item_types='tv favorite menu')
        favorite_menu.infoitem = self
        menuw.pushmenu(favorite_menu)
        menuw.refresh()


    def mod_day(self, arg=None, menuw=None):
        """ Modify day

        Opens a submenu where the day of the week of a favorite can be configured.
        """
        _debug_('mod_day(arg=%r, menuw=%r)' % (arg, menuw), 2)
        items = []

        items.append(menu.MenuItem(_('ANY DAY'), action=self.alter_prop, arg=('dow', 'ANY')))

        for i in range(len(self.week_days)):
            items.append(menu.MenuItem(self.week_days[i], action=self.alter_prop, arg=('dow', i)))

        favorite_menu = menu.Menu(_('Modify Day'), items, item_types = 'tv favorite menu')
        favorite_menu.infoitem = self
        menuw.pushmenu(favorite_menu)
        menuw.refresh()


    def mod_time(self, arg=None, menuw=None):
        """ Modify time

        Opens a submenu where the time of a favorite can be configured.
        """
        _debug_('mod_time(arg=%r, menuw=%r)' % (arg, menuw), 2)
        items = []

        items.append(menu.MenuItem(_('ANY TIME'), action=self.alter_prop, arg=('mod', 'ANY')))

        for i in range(48):
            mod = i * 30
            items.append(menu.MenuItem(time.strftime(config.TV_TIME_FORMAT, time.gmtime(float(mod * 60))), \
                action=self.alter_prop, arg=('mod', mod)))

        favorite_menu = menu.Menu(_('Modify Time'), items, item_types = 'tv favorite menu')
        favorite_menu.infoitem = self
        menuw.pushmenu(favorite_menu)
        menuw.refresh()


    def alter_prop(self, arg=(None,None), menuw=None):
        """ Alter a favorites property

        This function is where the properties of a favorite really are changed.
        """
        _debug_('alter_prop(arg=%r, menuw=%r)' % (arg, menuw), 2)
        (prop, val) = arg

        if prop == 'channel':
            if val == 'ANY':
                self.channel = 'ANY CHANNEL'
                self.fav.channel = 'ANY'
            else:
                self.channel = val
                self.fav.channel = val
            if menuw:
                menuw.back_one_menu(arg='reload')

        elif prop == 'dow':
            if val == 'ANY':
                self.dow = 'ANY DAY'
                self.fav.dow = 'ANY'
            else:
                self.dow = self.week_days[val]
                self.fav.dow = val
            if menuw:
                menuw.back_one_menu(arg='reload')

        elif prop == 'mod':
            if val == 'ANY':
                self.mod = 'ANY TIME'
                self.fav.mod = 'ANY'
            else:
                # self.mod = tv_util.minToTOD(val)
                self.mod = time.strftime(config.TV_TIME_FORMAT, time.gmtime(float(val * 60)))
                self.fav.mod = val
            if menuw:
                menuw.back_one_menu(arg='reload')

        elif prop == 'dup':
            if val == 'True':
                # twisted needs FALSE and TRUE, it does not know False and True
                self.allowDuplicates=TRUE
                self.fav.allowDuplicates=TRUE
                newname = _('Prevent Recording of Duplicates')
                val = 'False'
            else:
                # twisted needs FALSE and TRUE, it does not know False and True
                self.allowDuplicates=FALSE
                self.fav.allowDuplicates=FALSE
                newname = _('Allow Recordings of Duplicates')
                val = 'True'
            if menuw:
                menustack = menuw.menustack[-1]
                pos = menustack.choices.index(menustack.selected)
                new = menu.MenuItem(newname, action=self.alter_prop, arg=('dup', val))
                menustack.choices[pos] = new
                menustack.selected = menustack.choices[pos]
                menuw.init_page()
                menuw.refresh()

        elif prop == 'new':
            # twisted needs FALSE and TRUE, it does not know False and True
            if val == 'True':
                self.onlyNew=TRUE
                self.fav.onlyNew=TRUE
                newname = _('Record All Episodes')
                val = 'False'
            else:
                self.onlyNew=FALSE
                self.fav.onlyNew=FALSE
                newname = _('Record Only New Episodes')
                val = 'True'
            if menuw:
                menustack = menuw.menustack[-1]
                pos = menustack.choices.index(menustack.selected)
                new = menu.MenuItem(newname, action=self.alter_prop, arg=('new', val))
                menustack.choices[pos] = new
                menustack.selected = menustack.choices[pos]
                menuw.init_page()
                menuw.refresh()


    def save_changes(self, arg=None, menuw=None):
        """
        Save favorite
        """
        _debug_('save_changes(arg=%r, menuw=%r)' % (arg, menuw), 2)
        # this can take some time, as it means although to update the schedule
        msgtext = _('Saving the changes to this favorite.')+'\n'+_('This may take some time.')
        pop = PopupBox(text=msgtext)
        pop.show()

        if self.fav_action == 'edit':
            # first we remove the old favorite
            (result, msg) = self.recordclient.removeFavoriteNow(self.origname)
        elif self.fav_action =='add':
            result = True

        if result:
            # create a new edited favorite
            (result, msg) = self.recordclient.addEditedFavoriteNow(self.fav.name,
                self.fav.title, self.fav.channel, self.fav.dow, self.fav.mod,
                self.fav.priority, self.fav.allowDuplicates, self.fav.onlyNew)
            if result:
                if menuw:
                    menuw.delete_submenu()
                    if self.fav_action == 'add':
                        menuw.refresh(reload=1)
                self.fav_action = 'edit'
                pop.destroy()
            else:
                pop.destroy()
                # it is important to show the user this error,
                # because that means the favorite is removed,
                # and must be created again
                msgtext=_('Save failed, favorite was lost')+(':\n%s' % msg)
                AlertBox(text=msgtext).show()


    def rem_favorite(self, arg=None, menuw=None):
        """
        Remove favorite
        """
        _debug_('rem_favorite(arg=%r, menuw=%r)' % (arg, menuw), 2)
        name = self.origname
        (result, msg) = self.recordclient.removeFavoriteNow(name)
        if result:
            # if this is successfull
            if menuw:
                menuw.delete_submenu()
                menuw.refresh(reload=1)
            # and show a short message of success
            msgtext = text=_('"%s" has been removed from favorites') % name
            AlertBox(text=msgtext).show()
        else:
            # if all fails then we should show an error
            msgtext = _('Remove failed')+(':\n%s' % msg)
            AlertBox(text=msgtext).show()
Example #5
0
class EditFavorite(PopupBox):
    """
    @param prog:    the program to record
    @param left:    x coordinate, Integer
    @param top:     y coordinate, Integer
    @param width:   width in pixels, Integer
    @param height:  height in pixels, Integer
    @param context: context in which the object is instantiated
    """
    def __init__(self, parent=None, subject=None, left=None, top=None, width=500, height=350, context=None):
        """ """
        _debug_('EditFavorite.__init__(parent=%r, subject=%r, left=%r, top=%r, width=%r, height=%r, context=%r)' % \
            (parent, subject, left, top, width, height, context), 1)

        self.oldname = None
        if context:
            self.context = context
        else:
            context = 'guide'

        self.recordclient = RecordClient()
        if isinstance(subject, TvProgram):
            (result, favs) = self.recordclient.getFavorites()
            if result:
                num_favorites = len(favs)
                self.priority = num_favorites + 1
            else:
                self.priority = 1
            self.fav = Favorite(subject.title, subject, TRUE, TRUE, TRUE, self.priority, TRUE, FALSE)

        else:
            self.fav = subject
            self.oldname = self.fav.name

        PopupBox.__init__(self, text=_('Edit Favorite'), x=left, y=top, width=width, height=height)

        self.v_spacing = 15
        self.h_margin = 20
        self.v_margin = 20

        self.internal_h_align = Align.LEFT

        if not self.left:     self.left   = self.osd.width/2 - self.width/2
        if not self.top:      self.top    = self.osd.height/2 - self.height/2

        guide = epg_xmltv.get_guide()

        name = Label(_('Name')+':', self, Align.LEFT)
        self.name_input = LetterBoxGroup(text=self.fav.name)
        self.name_input.h_align = Align.NONE
        self.add_child(self.name_input)


        title = Label(_('Title')+': %s' % self.fav.title, self, Align.LEFT)

        chan = Label(_('Channel')+':', self, Align.LEFT)

        self.chan_box = OptionBox('ANY')
        self.chan_box.h_align = Align.NONE
        self.chan_box.add_item(text=_('ANY'), value='ANY')

        i = 1
        chan_index = 0
        for ch in guide.chan_list:
            #if ch.id == self.fav.channel_id:
            if ch.displayname == self.fav.channel:
                chan_index = i
            i += 1
            self.chan_box.add_item(text=ch.displayname, value=ch.displayname)


        self.chan_box.toggle_selected_index(chan_index)
        # This is a hack for setting the OptionBox's label to the current value.
        # It should be done by OptionBox when drawing, but it doesn't work :(
        self.chan_box.change_item(None)
        self.add_child(self.chan_box)

        dow = Label(_('Day of Week') +':', self, Align.LEFT)
        self.dow_box = OptionBox('ANY DAY')
        self.dow_box.h_align = Align.NONE

        self.dow_box.add_item(text=_('ANY DAY'), value='ANY')

        i=1
        dow_index = 0
        for dow in (_('Mon'), _('Tue'), _('Wed'), _('Thu'), _('Fri'), _('Sat'), _('Sun')):
            val = "%d" % (i-1)
            self.dow_box.add_item(text=_(dow), value=val )
            if val == self.fav.dow:
                dow_index = i
            i += 1
        self.dow_box.toggle_selected_index(dow_index)
        # This is a hack for setting the OptionBox's label to the current value.
        # It should be done by OptionBox when drawing, but it doesn't work :(
        self.dow_box.change_item(None)
        self.add_child(self.dow_box)

        tod = Label(_('Time of Day')+':', self, Align.LEFT)
        self.tod_box = OptionBox('ANY')
        self.tod_box.h_align = Align.NONE
        self.tod_box.add_item(text=_('ANY TIME'), value='ANY')

        i = 0
        tod_index = 0

        for h in range(0, 24):
            for m in (00, 30):
                val = i*30
                # Little hack: we calculate the hours from Jan 1st, 1970 GMT,
                # and then use strftime to get the string representation
                text = strftime(config.TV_TIME_FORMAT, gmtime(h * 3600 + 60 * m))
                self.tod_box.add_item(text=text, value=val)
                if val == self.fav.mod:
                    tod_index = i+1
                i += 1
        self.tod_box.toggle_selected_index(tod_index)
        # This is a hack for setting the OptionBox's label to the current
        # value. It should be done by OptionBox when drawing, but it doesn't
        # work :(
        self.tod_box.change_item(None)
        self.add_child(self.tod_box)

        self.save = Button(_('Save'))
        self.add_child(self.save)
        if self.oldname:
            self.remove = Button(_('Remove'))
            self.add_child(self.remove)
        else:
            self.remove = None
        self.cancel = Button(_('CANCEL'))
        self.add_child(self.cancel)


    def removeFavorite(self):
        _debug_('removeFavorite()', 2)
        (result, reason) = self.recordclient.removeFavoriteNow(self.oldname)
        if result:
            searcher = None
            if self.parent and self.context == 'favorites':
                for child in self.parent.children:
                    if isinstance(child, ViewFavorites):
                        searcher = child
                        break
                if searcher:
                    searcher.refreshList()
                self.destroy()
                if searcher:
                    searcher.draw()
                    self.osd.update()
        else:
            AlertBox(parent=self, text=_('Remove favorite failed')+(':\n%s' % reason)).show()


    def eventhandler(self, event, menuw=None):
        _debug_('eventhandler(event=%r, menuw=%r)' % (event, menuw), 2)

        if self.get_selected_child() == self.name_input:
            if event == em.INPUT_LEFT:
                self.name_input.change_selected_box('left')
                self.draw()
                return True
            elif event == em.INPUT_RIGHT:
                self.name_input.change_selected_box('right')
                self.draw()
                return True
            elif event == em.INPUT_ENTER:
                self.name_input.get_selected_box().toggle_selected()
                self.chan_box.toggle_selected()
                self.draw()
                return True
            elif event == em.INPUT_UP:
                self.name_input.get_selected_box().charUp()
                self.draw()
                return True
            elif event == em.INPUT_DOWN:
                self.name_input.get_selected_box().charDown()
                self.draw()
                return True
            elif event in em.INPUT_ALL_NUMBERS:
                self.name_input.get_selected_box().cycle_phone_char(event)
                self.draw()
                return True
            elif event == em.INPUT_EXIT:
                self.destroy()
                return True

        elif self.get_selected_child() == self.chan_box:
            if event in (em.INPUT_UP, em.INPUT_DOWN):
                self.chan_box.change_item(event)
                self.draw()
            elif event == em.INPUT_ENTER:
                if self.chan_box.selected or self.chan_box.list.is_visible():
                    self.chan_box.toggle_box()
                    self.draw()
            elif event in (em.INPUT_LEFT, em.MENU_PAGEUP):
                self.chan_box.toggle_selected()
                self.name_input.boxes[0].toggle_selected()
                self.draw()
            elif event in (em.INPUT_RIGHT, em.MENU_PAGEDOWN):
                self.chan_box.toggle_selected()
                self.dow_box.toggle_selected()
                self.draw()
            elif event == em.INPUT_EXIT:
                self.destroy()
            return True

        elif self.get_selected_child() == self.dow_box:
            if event in (em.INPUT_UP, em.INPUT_DOWN):
                self.dow_box.change_item(event)
                self.draw()
            elif event == em.INPUT_ENTER:
                if self.dow_box.selected or self.dow_box.list.is_visible():
                    self.dow_box.toggle_box()
                    self.draw()
            elif event in (em.INPUT_LEFT, em.MENU_PAGEUP):
                self.dow_box.toggle_selected()
                self.chan_box.toggle_selected()
                self.draw()
            elif event in (em.INPUT_RIGHT, em.MENU_PAGEDOWN):
                self.dow_box.toggle_selected()
                self.tod_box.toggle_selected()
                self.draw()
            elif event == em.INPUT_EXIT:
                self.destroy()
                return True
            return True

        elif self.get_selected_child() == self.tod_box:
            if event in (em.INPUT_UP, em.INPUT_DOWN):
                self.tod_box.change_item(event)
                self.draw()
            elif event == em.INPUT_ENTER:
                if self.tod_box.selected or self.tod_box.list.is_visible():
                    self.tod_box.toggle_box()
                    self.draw()
            elif event in (em.INPUT_LEFT, em.MENU_PAGEUP):
                self.tod_box.toggle_selected()
                self.dow_box.toggle_selected()
                self.draw()
            elif event in (em.INPUT_RIGHT, em.MENU_PAGEDOWN):
                self.tod_box.toggle_selected()
                self.save.toggle_selected()
                self.draw()
            elif event == em.INPUT_EXIT:
                self.destroy()
                return True
            return True

        elif self.get_selected_child() == self.save:
            if event == em.INPUT_ENTER:
                # remove the old favourite
                if self.oldname:
                    (result, reason) = self.recordclient.removeFavoriteNow(self.oldname)
                    if not result:
                        AlertBox(parent=self, text=_('Add favorite failed')+(':\n%s' % reason)).show()
                # add the new favourite
                (result, reason) = self.recordclient.addEditedFavoriteNow(self.name_input.get_word(), self.fav.title,
                    self.chan_box.list.get_selected_item().value, self.dow_box.list.get_selected_item().value,
                    self.tod_box.list.get_selected_item().value, self.fav.priority, self.fav.allowDuplicates,
                    self.fav.onlyNew)
                if result:
                    #tv.view_favorites.ViewFavorites(parent=self.parent, text='Favorites').show()
                    self.destroy()
                    AlertBox(parent='osd', text=_('Favorite %s has been saved') % self.name_input.get_word()).show()
                else:
                    AlertBox(parent=self, text=_('Add favorite failed')+(':\n%s' % reason)).show()
                return True
            elif event in (em.INPUT_LEFT, em.MENU_PAGEUP):
                self.save.toggle_selected()
                self.tod_box.toggle_selected()
                self.draw()
            elif event in (em.INPUT_RIGHT, em.MENU_PAGEDOWN):
                self.save.toggle_selected()
                if self.remove:
                    self.remove.toggle_selected()
                else:
                    self.cancel.toggle_selected()
                self.draw()
            elif event == em.INPUT_EXIT:
                self.destroy()
                return True
            return True

        elif self.get_selected_child() == self.remove:
            if event == em.INPUT_ENTER:
                ConfirmBox(text=_('Do you want to remove %s?') % self.name_input.get_word(),
                           handler=self.removeFavorite).show()
                return True
            elif event in (em.INPUT_LEFT, em.MENU_PAGEUP):
                self.save.toggle_selected()
                self.remove.toggle_selected()
                self.draw()
            elif event in (em.INPUT_RIGHT, em.MENU_PAGEDOWN):
                self.remove.toggle_selected()
                self.cancel.toggle_selected()
                self.draw()
            elif event in (em.INPUT_ENTER, em.INPUT_EXIT):
                self.destroy()
                return True
            return True

        elif self.get_selected_child() == self.cancel:
            if event in (em.INPUT_LEFT, em.MENU_PAGEUP):
                if self.remove:
                    self.remove.toggle_selected()
                else:
                    self.save.toggle_selected()
                self.cancel.toggle_selected()
                self.draw()
            elif event in (em.INPUT_ENTER, em.INPUT_EXIT):
                self.destroy()
                return True
            return True
        if event == em.INPUT_EXIT:
            self.destroy()
            return True
        elif event in (em.MENU_PAGEDOWN, em.MENU_PAGEUP):
            return True
        else:
            return self.parent.eventhandler(event)