def validate_new_site(form, action, data):
    """ Validate the new bookmarking site data. """
    errors = form.validate(action, data)
    if errors:
        return errors
    if data['new_site_name']:
        if not data['new_site_url']:
            ew = form.widgets.get('new_site_url')
            ew._error = WidgetInputError(form.context.__name__, ew.label, _('Missing site url'))
            return ew._error
        if not data['new_site_icon']:
            ew = form.widgets.get('new_site_icon')
            ew._error = WidgetInputError(form.context.__name__, ew.label, _('Missing site icon'))
            return ew._error
    def handle_edit_action(self, action, data):
        #apply form changes
        if form.applyChanges(self.context, self.form_fields, data, self.adapters):
            #if there is a new site in the form, add it to the properties page
            if data.has_key('new_site_name') and data['new_site_name']:
                ad = self.adapters[IbookmarkletsNewSiteForm]
                ad.setNewBookmarkingSite(data['new_site_name'],
                                         data['new_site_url'],
                                         data['new_site_icon'])

            self.status = _("Changes saved.")
            self._on_save(data)
            self.request['fieldset.current'] = u'fieldsetlegend-bookmarkletssettings'
        else:
            self.status = _("No changes made.")
    def setNewBookmarkingSite(self, name, url, icon):
        """ Set a new bookmarking site  """
        new_site = [name, '', '']
        if url:
            new_site[1] = url
        if icon:
            new_site[2] = icon

        site_id = ''.join(name.lower().split())
        self.bmprops.manage_addProperty(site_id, new_site, 'lines')
        self.bmprops.manage_changeProperties(displayed_sites=list(self.bmprops.displayed_sites) + [site_id])
        self.bmprops.manage_changeProperties(available_sites=list(self.bmprops.available_sites) + [site_id])

settingsset = FormFieldsets(IbookmarkletsSchema)
settingsset.id = 'bookmarkletssettings'
settingsset.label = _(u'Settings')

newsiteset = FormFieldsets(IbookmarkletsNewSiteForm)
newsiteset.id = 'bookmarkletsnewsite'
newsiteset.label = _('New Site')

class BookmarkletsControlPanel(ControlPanelForm):

    implements(IbookmarkletsControlPanel)
    form_fields = FormFieldsets(settingsset, newsiteset)
    form_fields['displayed_sites'].custom_widget = MultiCheckBoxVocabularyWidget
    

    label = _(u'PloneBookmarklets Settings')
    description = _(u'Settings which controls the bookmarking sites to be displayed.')
    form_name = _(u'PloneBookmarklets Settings')