Merge branch 'master' of /home/git/wammu
[wammu.git] / Wammu / SMSXML.py
blobfa59de24fd4b8c057d44332c3283e86514d972d8
1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
3 '''
4 Wammu - Phone manager
5 Module for writing SMS to XML.
6 '''
7 __author__ = 'Florent Kaisser'
8 __email__ = 'florent.kaisser@free.fr'
9 __license__ = '''
10 Copyright © 2008 Florent Kaisser
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License version 2 as published by
14 the Free Software Foundation.
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 more details.
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 '''
26 from Wammu.Utils import SearchNumber
27 from Wammu.MessageDisplay import SmsTextFormat
28 import tempfile
29 import Wammu.Data
30 import wx
31 import os
32 if Wammu.gammu_error == None:
33 import gammu
35 XMLheader = '<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-stylesheet type=\"text/xsl\" href=\"sms.xsl\"?>\n'
40 def SMSToXML(cfg, sms, lookuplist = None):
41 '''
42 Convert a sms to XML
43 '''
46 text = SmsTextFormat(cfg, sms['Text'], doxml = True)
48 smsxml = " <message>\n"
50 if sms['DateTime'] is not None:
52 smsxml += " <date>"
53 smsxml += sms['DateTime'].strftime("%d.%m.%Y %H:%M:%S")
54 smsxml += "</date>\n"
56 smsxml += " <dateenc>"
57 smsxml += sms['DateTime'].strftime("%Y%m%d%H%M%S")
58 smsxml += "</dateenc>\n"
60 smsxml += " <text>"
61 smsxml += text.encode('utf-8')
62 smsxml += "</text>\n"
64 smsxml += " <telephone>"
65 smsxml += sms['Number'].encode('utf-8')
66 smsxml += "</telephone>\n"
68 smsxml += " <folder>"
69 smsxml += str(sms['Folder'])
70 smsxml += "</folder>\n"
72 smsxml += " <stat>"
73 smsxml += sms['State']
74 smsxml += "</stat>\n"
76 smsxml += " </message>\n"
78 return smsxml;
80 def SMSExportXML(parent, messages, contacts):
81 count = len(messages)
82 wildcard = _('XML File') + ' (*.xml)|*.xml|' + _('All files') + ' (*.*)|*.*;*'
83 exts = ['xml']
84 exts.append(None)
85 dlg = wx.FileDialog(parent, _('Select XML file...'), os.getcwd(), "", wildcard, wx.SAVE | wx.OVERWRITE_PROMPT | wx.CHANGE_DIR)
87 if dlg.ShowModal() != wx.ID_OK:
88 return
90 path = dlg.GetPath()
91 ext = exts[dlg.GetFilterIndex()]
92 # Add automatic extension if we know one and file does not
93 # have any
94 if (os.path.splitext(path)[1] == '' and
95 ext is not None):
96 path += '.' + ext
98 parent.ShowProgress(_('Saving messages to XML'))
99 try:
100 f = file(path, 'w')
101 f.write(XMLheader)
102 f.write("<messages>\n")
103 for i in range(count):
104 if not parent.progress.Update(i * 100 / count):
105 del parent.progress
106 parent.SetStatusText(_('Export terminated'))
107 return
109 sms = messages[i]
110 data = Wammu.SMSXML.SMSToXML(parent.cfg, sms, contacts)
112 f.write(data)
114 f.write("</messages>\n")
115 f.close()
116 except IOError:
117 del parent.progress
118 wx.MessageDialog(parent,
119 _('Creating of file %s failed, bailing out.') % path,
120 _('Can not create file!'),
121 wx.OK | wx.ICON_ERROR).ShowModal()
122 del parent.progress
123 parent.SetStatusText(_('Export terminated'))
124 return
126 parent.progress.Update(100)
127 del parent.progress
128 parent.SetStatusText(_('%(count)d messages exported to "%(path)s" (%(type)s)') % {'count':count, 'path':path, 'type': _('mailbox')})