# -*- coding: UTF-8 -*-
# -*- Mode: Python; py-indent-offset: 4 -*-
# Authors: Nik Kim <fafhrd@datacom.kz> 

from AccessControl import ModuleSecurityInfo, allow_module
from AccessControl.Permissions import view

import SMTPEvents
from SMTPService import SMTPService

cp_id = 'smtp_service'

def getSMTPservice(context):
    """ returns the SMTP srevice instance """
    return context.Control_Panel.smtp_service

def make_smtp_service(cp):
    """Control_Panel smtp service"""
    smtp_service = SMTPService(cp_id)
    cp._setObject(cp_id, smtp_service)
    return getattr(cp, cp_id)

def initialize(context):
    # hook into the Control Panel
    cp = context._ProductContext__app.Control_Panel
    if cp_id in cp.objectIds():
        #cp._delObject(cp_id)
        smtp = getattr(cp, cp_id)
        smtp_service = smtp
        if not isinstance(smtp_service, SMTPService):
            smtp = make_smtp_service(cp)
    else:
        smtp = make_smtp_service(cp)
