Translated using Weblate.
[wammu.git] / Wammu / SMSXML.py
blobf3a710580343a55ae87149d64996f33d0da39709
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, contact = 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 += " <contact>"
69 smsxml += contact.encode('utf-8')
70 smsxml += "</contact>\n"
72 smsxml += " <folder>"
73 smsxml += str(sms['Folder'])
74 smsxml += "</folder>\n"
76 smsxml += " <stat>"
77 smsxml += sms['State']
78 smsxml += "</stat>\n"
80 smsxml += " </message>\n"
82 return smsxml;
84 def SMSExportXML(parent, messages, contacts):
85 count = len(messages)
86 wildcard = _('XML File') + ' (*.xml)|*.xml|' + _('All files') + ' (*.*)|*.*;*'
87 exts = ['xml']
88 exts.append(None)
89 dlg = wx.FileDialog(parent, _('Select XML file...'), os.getcwd(), "", wildcard, wx.SAVE | wx.OVERWRITE_PROMPT | wx.CHANGE_DIR)
91 if dlg.ShowModal() != wx.ID_OK:
92 return
94 path = dlg.GetPath()
95 ext = exts[dlg.GetFilterIndex()]
96 # Add automatic extension if we know one and file does not
97 # have any
98 if (os.path.splitext(path)[1] == '' and
99 ext is not None):
100 path += '.' + ext
102 parent.ShowProgress(_('Saving messages to XML'))
103 try:
104 f = file(path, 'w')
105 f.write(XMLheader)
106 f.write("<messages>\n")
107 for i in range(count):
108 if not parent.progress.Update(i * 100 / count):
109 del parent.progress
110 parent.SetStatusText(_('Export terminated'))
111 return
113 sms = messages[i]
114 j = SearchNumber(contacts, sms['Number'])
115 if j == -1:
116 contact = sms['Number']
117 else:
118 contact = contacts[j]['Name']
119 data = Wammu.SMSXML.SMSToXML(parent.cfg, sms, contact)
121 f.write(data)
123 f.write("</messages>\n")
124 f.close()
125 except IOError:
126 del parent.progress
127 wx.MessageDialog(parent,
128 _('Creating of file %s failed, bailing out.') % path,
129 _('Can not create file!'),
130 wx.OK | wx.ICON_ERROR).ShowModal()
131 del parent.progress
132 parent.SetStatusText(_('Export terminated'))
133 return
135 parent.progress.Update(100)
136 del parent.progress
137 parent.SetStatusText(_('%(count)d messages exported to "%(path)s" (%(type)s)') % {'count':count, 'path':path, 'type': _('mailbox')})