# Path: Wammu/Utils.py
# def SearchNumber(lst, number):
#     for i in range(len(lst)):
#         for x in lst[i]['Entries']:
#             if GetItemType(x['Type']) == 'phone' and NormalizeNumber(number) == NormalizeNumber(x['Value']):
#                 return i
#     return -1
# 
# Path: Wammu/MessageDisplay.py
# def SmsTextFormat(cfg, txt, dohtml=True, doxml=False):
#     if txt is None:
#         return ''
#     if cfg.Read('/Message/Format') == 'yes':
#         parts = []
#         arr = txt.split(' ')
#         for a in arr:
#             if re.match('^([a-z]+[^ ]*)?[A-Z].*[a-z]{2,}[A-Z]{2,}.*$', a) is not None:
#                 prevtype = 'p'
#                 if UnicodeConv(string.lowercase).find(a[0]) != -1:
#                     curtype = 'l'
#                 elif UnicodeConv(string.uppercase).find(a[0]) != -1:
#                     curtype = 'u'
#                 elif UnicodeConv(string.digits).find(a[0]) != -1:
#                     curtype = 'd'
#                 else:
#                     curtype = 'p'
# 
#                 s = a[0]
# 
#                 for x in a[1:]:
#                     if UnicodeConv(string.lowercase).find(x) != -1:
#                         nexttype = 'l'
#                     elif UnicodeConv(string.uppercase).find(x) != -1:
#                         nexttype = 'u'
#                     elif UnicodeConv(string.digits).find(x) != -1:
#                         nexttype = 'd'
#                     else:
#                         nexttype = 'p'
# 
#                     if curtype == nexttype:
#                         s += x
#                     else:
#                         if curtype == 'u' and nexttype == 'l' and prevtype == 'p' and len(s) == 1:
#                             curtype = 'l'
#                             prevtype = 'u'
#                             s += x
#                             continue
#                         if curtype == 'p':
#                             parts[-1] += s
#                         elif curtype == 'u':
#                             parts.append(s.lower())
#                         else:
#                             parts.append(s)
#                         s = x
#                         prevtype = curtype
#                         curtype = nexttype
# 
#                 if curtype == 'p':
#                     parts[-1] += s
#                 elif curtype == 'u':
#                     parts.append(s.lower())
#                 else:
#                     parts.append(s)
#                 s = x
#             else:
#                 parts.append(a)
#         ret = ' '.join(parts)
#     else:
#         ret = txt
#     if dohtml or doxml:
#         xmlsafe = xml.sax.saxutils.escape(ret)
#         if doxml:
#             return xmlsafe.replace('\n', '<br />')
#         else:
#             return xmlsafe.replace('\n', '<br>')
#     else:
#         return ret.replace('\n', ' ')


from Wammu.Utils import SearchNumber
from Wammu.MessageDisplay import SmsTextFormat
from email.MIMEAudio import MIMEAudio
from email.MIMEImage import MIMEImage
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from hashlib import md5
import email.Utils
import time
import tempfile
import os
import wx
import Wammu.Data
    import gammu
    Converts ringtone to MIDI data.

    @todo: I'd like to avoid creating temporary file, but Gammmu doesn't
    provide such functionality.
    '''
    handle, name = tempfile.mkstemp()
    f = os.fdopen(handle, 'w+')
    gammu.SaveRingtone(f, data, 'mid')
    f.seek(0)
    data = f.read()
    f.close()
    os.unlink(name)
    return data

def DateToString(date):
    '''
    Convert date to RFC 2822 format.
    '''
    return email.Utils.formatdate(time.mktime(date.timetuple()), True)

def SMSToMail(cfg, sms, lookuplist=None, mailbox=False):
    '''
    Converts SMS to formated mail. It will contain all images and sounds from
    message and predefined animations. Also text formatting is preserved in
    HTML.
    '''
    msg = MIMEMultipart('related', None, None, type='text/html')
    prepend = ''
    name = ''
    if lookuplist is not None:

# Path: Wammu/Utils.py
# def SearchNumber(lst, number):
#     for i in range(len(lst)):
#         for x in lst[i]['Entries']:
#             if GetItemType(x['Type']) == 'phone' and NormalizeNumber(number) == NormalizeNumber(x['Value']):
#                 return i
#     return -1
# 
# Path: Wammu/MessageDisplay.py
# def SmsTextFormat(cfg, txt, dohtml=True, doxml=False):
#     if txt is None:
#         return ''
#     if cfg.Read('/Message/Format') == 'yes':
#         parts = []
#         arr = txt.split(' ')
#         for a in arr:
#             if re.match('^([a-z]+[^ ]*)?[A-Z].*[a-z]{2,}[A-Z]{2,}.*$', a) is not None:
#                 prevtype = 'p'
#                 if UnicodeConv(string.lowercase).find(a[0]) != -1:
#                     curtype = 'l'
#                 elif UnicodeConv(string.uppercase).find(a[0]) != -1:
#                     curtype = 'u'
#                 elif UnicodeConv(string.digits).find(a[0]) != -1:
#                     curtype = 'd'
#                 else:
#                     curtype = 'p'
# 
#                 s = a[0]
# 
#                 for x in a[1:]:
#                     if UnicodeConv(string.lowercase).find(x) != -1:
#                         nexttype = 'l'
#                     elif UnicodeConv(string.uppercase).find(x) != -1:
#                         nexttype = 'u'
#                     elif UnicodeConv(string.digits).find(x) != -1:
#                         nexttype = 'd'
#                     else:
#                         nexttype = 'p'
# 
#                     if curtype == nexttype:
#                         s += x
#                     else:
#                         if curtype == 'u' and nexttype == 'l' and prevtype == 'p' and len(s) == 1:
#                             curtype = 'l'
#                             prevtype = 'u'
#                             s += x
#                             continue
#                         if curtype == 'p':
#                             parts[-1] += s
#                         elif curtype == 'u':
#                             parts.append(s.lower())
#                         else:
#                             parts.append(s)
#                         s = x
#                         prevtype = curtype
#                         curtype = nexttype
# 
#                 if curtype == 'p':
#                     parts[-1] += s
#                 elif curtype == 'u':
#                     parts.append(s.lower())
#                 else:
#                     parts.append(s)
#                 s = x
#             else:
#                 parts.append(a)
#         ret = ' '.join(parts)
#     else:
#         ret = txt
#     if dohtml or doxml:
#         xmlsafe = xml.sax.saxutils.escape(ret)
#         if doxml:
#             return xmlsafe.replace('\n', '<br />')
#         else:
#             return xmlsafe.replace('\n', '<br>')
#     else:
#         return ret.replace('\n', ' ')


# Path: Wammu/MailWriter.py
from Wammu.Utils import SearchNumber
from Wammu.MessageDisplay import SmsTextFormat
from email.MIMEAudio import MIMEAudio
from email.MIMEImage import MIMEImage
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from hashlib import md5
import email.Utils
import time
import tempfile
import os
import wx
import Wammu.Data
    import gammu

    Converts ringtone to MIDI data.

    @todo: I'd like to avoid creating temporary file, but Gammmu doesn't
    provide such functionality.
    '''
    handle, name = tempfile.mkstemp()
    f = os.fdopen(handle, 'w+')
    gammu.SaveRingtone(f, data, 'mid')
    f.seek(0)
    data = f.read()
    f.close()
    os.unlink(name)
    return data

def DateToString(date):
    '''
    Convert date to RFC 2822 format.
    '''
    return email.Utils.formatdate(time.mktime(date.timetuple()), True)

def SMSToMail(cfg, sms, lookuplist=None, mailbox=False):
    '''
    Converts SMS to formated mail. It will contain all images and sounds from
    message and predefined animations. Also text formatting is preserved in
    HTML.
    '''
    msg = MIMEMultipart('related', None, None, type='text/html')
    prepend = ''
    name = ''
    if lookuplist is not None:

        i = SearchNumber(lookuplist, sms['Number'])