Changeset 2240
- Timestamp:
- 07/03/07 02:16:47 (1 year ago)
- Files:
-
- plugins/moto-sync/motosync.py (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/moto-sync/motosync.py
r2232 r2240 2110 2110 return ret 2111 2111 2112 def delete_entry(self, uid):2113 """Delete an event with the given UID2112 def delete_entry(self, objtype, uid): 2113 """Delete an event with the given objtype and UID 2114 2114 2115 2115 Returns True on success, False otherwise. 2116 2116 """ 2117 objtype, positions = self.__uid_to_pos(uid)2117 positions = self.__uid_to_pos(uid, objtype) 2118 2118 for pos in positions: 2119 2119 if objtype == 'event': … … 2144 2144 context.report_osyncwarning(err) 2145 2145 # we have an entry that can't be stored on the phone 2146 # if its modified and we've seen it before,delete it2146 # if its modified, we've seen it before, so delete it 2147 2147 # otherwise just ignore it 2148 if (change.changetype == opensync.CHANGE_TYPE_MODIFIED and self.uid_seen(change.uid)):2148 if change.changetype == opensync.CHANGE_TYPE_MODIFIED: 2149 2149 change.changetype = opensync.CHANGE_TYPE_DELETED 2150 2150 change.data = None 2151 return self.delete_entry( change.uid)2151 return self.delete_entry(objtype, change.uid) 2152 2152 else: 2153 2153 return False … … 2157 2157 entry.set_pos(positions) 2158 2158 else: 2159 _, positions = self.__uid_to_pos(change.uid)2159 positions = self.__uid_to_pos(change.uid, objtype) 2160 2160 # check if the number of positions required has changed 2161 2161 pos_diff = entry.num_pos() - len(positions) … … 2179 2179 return True 2180 2180 2181 def uid_seen(self, uid):2182 """Returns true iff the given UID is one of ours."""2183 try:2184 self.__uid_to_pos(uid)2185 return True2186 except (AssertionError, ValueError, TypeError):2187 return False2188 2189 2181 def __init_categories(self): 2190 2182 """Initialise a hash and reverse hash of category IDs.""" … … 2210 2202 return m.hexdigest() 2211 2203 2212 def __generate_uid(self, entry): 2213 """Generate a "hopefully unique" UID for an entry. 2214 2215 Uses the last 8 digit's of the phone's IMEI to do so. 2204 @staticmethod 2205 def __generate_uid(entry): 2206 """Generate a UID for an entry, that encodes the objtype and the position.""" 2207 return ("moto-%s-%s" % (entry.get_objtype(), entry.generate_uid())) 2208 2209 @staticmethod 2210 def __uid_to_pos(uid, objtype): 2211 """Reverse of the generate_uid function above. 2212 2213 Also checks that the objtype matches. 2216 2214 """ 2217 return ("moto-%s-%s@%s" % (entry.get_objtype(), entry.generate_uid(), self.serial[-8:])) 2218 2219 def __uid_to_pos(self, uid): 2220 """Reverse the generate_uid function above. 2221 2222 Also checks that it is one of ours. 2223 """ 2224 moto, objtype, lastpart = uid.split('-', 2) 2225 assert(moto == "moto" and objtype in SUPPORTED_OBJTYPES, 'Invalid UID: %s' % uid) 2226 lastpos = lastpart.rindex('@') 2227 assert(lastpart[lastpos + 1:] == self.serial[-8:], 'Entry not created on this phone') 2215 try: 2216 moto, uid_objtype, lastpart = uid.split('-', 2) 2217 assert(moto == "moto") 2218 except (ValueError, AssertionError): 2219 raise opensync.Error("Invalid UID: " + uid) 2220 2221 if uid_objtype != objtype: 2222 raise opensync.Error("UID %s doesn't match objtype %s", uid, objtype) 2228 2223 2229 2224 if objtype == "event": 2230 positions = PhoneEventSimple.unpack_uid(lastpart[:lastpos])2225 return PhoneEventSimple.unpack_uid(lastpart) 2231 2226 elif objtype == "contact": 2232 positions = PhoneContact.unpack_uid(lastpart[:lastpos]) 2233 return objtype, positions 2227 return PhoneContact.unpack_uid(lastpart) 2228 else: 2229 raise opensync.Error("Unsupported objtype: " + uid) 2234 2230 2235 2231 … … 2248 2244 """Connect to the phone.""" 2249 2245 self.access.connect() 2246 anchorpath = os.path.join(info.configdir, 'anchor.db') 2247 if not opensync.anchor_compare(anchorpath, 'serial', self.access.serial): 2248 self.sink.slowsync = True 2250 2249 2251 2250 def get_changes(self, info, ctx): … … 2271 2270 raise opensync.Error('unsupported objtype %s' % change.objtype, opensync.ERROR_NOT_SUPPORTED) 2272 2271 if change.changetype == opensync.CHANGE_TYPE_DELETED: 2273 success = (self.access.uid_seen(change.uid) and self.access.delete_entry(change.uid))2272 success = self.access.delete_entry(change.objtype, change.uid) 2274 2273 elif change.changetype == opensync.CHANGE_TYPE_MODIFIED: 2275 2274 old_uid = change.uid … … 2283 2282 if success: 2284 2283 self.hashtable.update_hash(change.changetype, change.uid, change.hash) 2284 2285 def sync_done(self, info, ctx): 2286 """Called when a sync completes successfully.""" 2287 anchorpath = os.path.join(info.configdir, 'anchor.db') 2288 opensync.anchor_update(anchorpath, 'serial', self.access.serial) 2285 2289 2286 2290 def disconnect(self, info, ctx):
