Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/serialize.py
blob: 3da2df05b73e882d3d79f2e8464aee0a78088a33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import xml.dom.minidom
from xml.dom.minidom import getDOMImplementation
from xml.dom.minidom import parse
import cStringIO
import os
import gtk

from sugar.datastore import datastore

from constants import Constants
from instance import Instance
import utils
from recorded import Recorded
import recorded
from savedmap  import SavedMap

def fillMediaHash( index, m ):
	doc = None
	if (os.path.exists(index)):
		try:
			doc = parse( os.path.abspath(index) )
		except:
			doc = None
	if (doc == None):
		return
	
	try:
		lastLat = doc.documentElement.getAttributeNode(Constants.mapLat).nodeValue
		lastLng = doc.documentElement.getAttributeNode(Constants.mapLng).nodeValue
		lastZoom = doc.documentElement.getAttributeNode(Constants.mapZoom).nodeValue
		m.ca.preComet()
		m.ca.cometLogic.handleReceivedMap(lastLat,lastLng,lastZoom)
		m.ca.postComet()
	except:
		# likely an old saved map
		lastLat = 0

	recdElements = doc.documentElement.getElementsByTagName(Constants.recdMapItem)
	for el in recdElements:
		_loadMediaIntoHash( el, m )

	saveElements = doc.documentElement.getElementsByTagName(Constants.recdSavedMapItem)
	for el in saveElements:
		_loadSavedMap( el, m )

	saveElements = doc.documentElement.getElementsByTagName(Constants.recdInfoMarker)
	for el in saveElements:
		_loadInfoMarker( el, m )

	saveElements = doc.documentElement.getElementsByTagName(Constants.recdLine)
	for el in saveElements:
		_loadLine( el, m )

def _loadSavedMap( el, m ):
	latNode = el.getAttributeNode(Constants.recdLat)
	lat = latNode.nodeValue
	lngNode = el.getAttributeNode(Constants.recdLng)
	lng = lngNode.nodeValue
	
	zoomNode = el.getAttributeNode(Constants.recdZoom)
	zoom = zoomNode.nodeValue
	notesNode = el.getAttributeNode(Constants.recdNotes)
	notes = notesNode.nodeValue
	tagsNode = el.getAttributeNode(Constants.recdTags)
	tags = tagsNode.nodeValue

	m.ca.addSavedMap(lat,lng,zoom,notes,False)

def _loadInfoMarker( el, m ):
	latNode = el.getAttributeNode(Constants.recdLat)
	lat = latNode.nodeValue
	lngNode = el.getAttributeNode(Constants.recdLng)
	lng = lngNode.nodeValue
	infoNode = el.getAttributeNode(Constants.recdInfo)
	info = infoNode.nodeValue
	iconNode = el.getAttributeNode(Constants.recdIcon)
	icon = iconNode.nodeValue
	m.setInfo(lat,lng,info,icon)

def _loadLine( el, m ):
	idNode = el.getAttributeNode(Constants.lineID)
	id = idNode.nodeValue
	colorNode = el.getAttributeNode(Constants.lineColor)
	color = colorNode.nodeValue
	thickNode = el.getAttributeNode(Constants.lineThick)
	thick = thickNode.nodeValue
	ptsNode = el.getAttributeNode(Constants.linePts)
	pts = ptsNode.nodeValue
	m.setLine(id,color,thick,pts)

def _loadMediaIntoHash( el, m ):
	if (el.getAttributeNode(Constants.recdDatastoreId) == None):
		return

	datastoreNode = el.getAttributeNode(Constants.recdDatastoreId)
	if (datastoreNode != None):
		datastoreId = datastoreNode.nodeValue
		if (datastoreId != None):
			#quickly check: if you have a datastoreId that the file hasn't been deleted,
			#cause if you do, we need to flag your removal
			#2904 trac
			datastoreOb = getMediaFromDatastore(datastoreId)
			if (datastoreOb != None):

				lat = 0
				lng = 0

				if (el.getAttributeNode(Constants.recdLat) != None):
					latNode = el.getAttributeNode(Constants.recdLat)
					lat = latNode.nodeValue
				else:
					return

				if (el.getAttributeNode(Constants.recdLng) != None):
					lngNode = el.getAttributeNode(Constants.recdLng)
					lng = lngNode.nodeValue
				else:
					return

				m.addMedia( lat, lng, datastoreOb )


def getMediaFromDatastore( id ):
	mediaObject = None
	try:
		mediaObject = datastore.get( id )
	finally:
		if (mediaObject == None):
			return None

	return mediaObject


def fillRecdFromNode( recd, el ):
	latNode = el.getAttributeNode(Constants.recdLat)
	if (latNode != None):
		recd.latitude = latNode.nodeValue
	lngNode = el.getAttributeNode(Constants.recdLng)
	if (lngNode != None):
		recd.longitude = lngNode.nodeValue

	return recd


def _addRecdXmlAttrs( el, recd ):
	el.setAttribute(Constants.recdDatastoreId, str(recd.datastoreId))
	el.setAttribute(Constants.recdLat, str(recd.latitude))
	el.setAttribute(Constants.recdLng, str(recd.longitude))

def _addInfoXmlAttrs( el, recd ):
	el.setAttribute(Constants.recdInfo, recd[2])
	el.setAttribute(Constants.recdIcon, recd[3])	
	el.setAttribute(Constants.recdLat, str(recd[0]))
	el.setAttribute(Constants.recdLng, str(recd[1]))

def _addLineXmlAttrs( el, recd ):
	el.setAttribute(Constants.lineID, recd[0])
	el.setAttribute(Constants.lineColor, recd[1])
	el.setAttribute(Constants.lineThick, recd[2])
	el.setAttribute(Constants.linePts, recd[3])

def _addSaveXmlAttrs( el, smap ):
	el.setAttribute(Constants.recdLat, str(smap.lat))
	el.setAttribute(Constants.recdLng, str(smap.lng))
	el.setAttribute(Constants.recdZoom, str(smap.zoom))
	el.setAttribute(Constants.recdNotes, str(smap.notes))
	el.setAttribute(Constants.recdTags, smap.tags)
	#el.setAttribute(Constants.recdDensity, str(smap.density))

	if (smap.recdDatastoreId != None):
		el.setAttribute(Constants.recdRecdId, smap.recdDatastoreId)
		el.setAttribute(Constants.recdRecdLat, smap.recdLat)
		el.setAttribute(Constants.recdRecdLng, smap.recdLng)


def saveMediaHash( m ):
	impl = getDOMImplementation()
	album = impl.createDocument(None, Constants.recdAlbum, None)
	root = album.documentElement
	root.setAttribute(Constants.mapLat, m.ca.NOW_MAP_CENTER_LAT)
	root.setAttribute(Constants.mapLng, m.ca.NOW_MAP_CENTER_LNG)
	root.setAttribute(Constants.mapZoom, m.ca.NOW_MAP_ZOOM)

	for i in range (0, len(m.recs)):
		recd = m.recs[i]
		mediaEl = album.createElement(Constants.recdMapItem)
		root.appendChild( mediaEl )
		_addRecdXmlAttrs( mediaEl, recd )

	for k, iMarker in m.infoMarkers.iteritems():
		mediaEl = album.createElement(Constants.recdInfoMarker)
		root.appendChild( mediaEl )
		_addInfoXmlAttrs( mediaEl, iMarker.split(";~") )

	for k, iMarker in m.lineData.iteritems():
		mediaEl = album.createElement(Constants.recdLine)
		root.appendChild( mediaEl )
		_addLineXmlAttrs( mediaEl, iMarker.split(";~") )

	for i in range (0, len(m.savedMaps)):
		smap = m.savedMaps[i]
		saveEl = album.createElement(Constants.recdSavedMapItem)
		root.appendChild( saveEl )
		_addSaveXmlAttrs( saveEl, smap )

	return album