# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Emesene is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    emesene is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


import Plugin

class MainClass(Plugin.Plugin):
    '''Main Plugin Class'''

    def __init__(self, controller, msn):
        ''' Constructor '''

        Plugin.Plugin.__init__(self, controller, msn)

        self.description = _('Show some notes (differentiated via contacts) in the conversation window.')
        self.authors = {'Francisco de Souza Junior (Oceanos)': \
            'fsjunior@gmail.com'}
        self.website = ''
        self.displayName = _('Notes')
        self.name = 'Notes'

        self.enabled = False

        self.controller = controller
        self.Slash = controller.Slash

        self.config = controller.getConfig()
        self.config.readPluginConfig(self.name)

    def start(self):
        self.Slash.register('note', self.noteCommand, \
            _('Set a note for the contacts presents in the conversation window.'))
        self.convmanagerId = self.controller.conversationManager.connect( \
            'new-conversation-ui', self.openWindow)
        self.enabled = True

    def noteCommand(self, slashAction):
        '''/note command'''
        message = slashAction.getParams()

        template = self.config.getPluginValue(self.name, 'new_note_template', \
            _('New note for %s: %s'))

        conversation = slashAction.conversation
        members = conversation.getMembers()

        for member in members:
            self.config.setPluginValue(self.name, member, message)
            conversation.appendOutputText(None, template % (member, message), \
                'information')

    def openWindow(self, conversationManager, conversation, window):
        '''A new window (or tab) has been created...'''

        members = conversation.getMembers()

        template = self.config.getPluginValue(self.name, 'note_template', \
            _('Note for %s: %s'))

        for member in members:
            message = self.config.getPluginValue(self.name, member, '')
            if message  != '':
                conversation.appendOutputText(None, template % (member, message), \
                    'information')


    def stop(self):
        self.Slash.unregister('note')
        self.controller.conversationManager.disconnect(self.convmanagerId)
        self.enabled = False

    def check(self):
        return (True, 'Ok')

    def configure(self):
        note_template = self.config.getPluginValue(self.name, 'note_template', \
            _('Note for %s: %s'))
        new_note_template = self.config.getPluginValue(self.name, \
            'new_note_template', _('New note for %s: %s'))

        opts = [Plugin.Option('note_template', str, \
            _('Notification message:'), '', note_template), \
        Plugin.Option('new_note_template', str, \
            _('Notification message for new notes:'), '', new_note_template)]

        result = Plugin.ConfigWindow(self.displayName, opts).run()

        if 'note_template' in result and result['note_template']:
            self.config.setPluginValue(self.name, 'note_template', \
                result['note_template'].value)
        if 'new_note_template' in result and result['new_note_template']:
            self.config.setPluginValue(self.name, 'new_note_template', \
                result['new_note_template'].value)
