Commit 7e810d5a authored by Vincent Pelletier's avatar Vincent Pelletier

CopySupport: Factorise clipboard-related methods and code paths.

Two almost identical methods, each having two 75% identical code
paths: this screamed "factorisation".
parent eeec7f6a
...@@ -423,68 +423,87 @@ class CopyContainer: ...@@ -423,68 +423,87 @@ class CopyContainer:
new_id=self.id) new_id=self.id)
def _duplicate(self, cp): def _duplicate(self, cp):
try: cp = _cb_decode(cp) _, result = self.__duplicate(
except: raise CopyError, 'Clipboard Error' cp,
duplicate=True,
is_indexable=True,
)
return result
oblist=[] def __duplicate(self, cp, duplicate, is_indexable):
op=cp[0] try:
cp = _cb_decode(cp)
except:
raise CopyError(eInvalid)
oblist = []
op = cp[0]
app = self.getPhysicalRoot() app = self.getPhysicalRoot()
result = []
for mdata in cp[1]: for mdata in cp[1]:
m = Moniker.loadMoniker(mdata) m = Moniker.loadMoniker(mdata)
try: ob = m.bind(app) try:
except: raise CopyError, 'Not Found' ob = m.bind(app)
self._verifyObjectPaste(ob, validate_src=1) except:
raise CopyError(eNotFound)
self._verifyObjectPaste(ob, validate_src=op + 1)
oblist.append(ob) oblist.append(ob)
result = []
if op==0: def doMove(self, ob):
for ob in oblist: aq_parent(aq_inner(ob))._delObject(ob.getId())
if not ob.cb_isCopyable(): return aq_base(ob)
raise CopyError, 'Not Supported' is_doable_id, notify_error, do_sanity_check, do, set_owner, is_clone = (
try: ob._notifyOfCopyTo(self, op=0) ( # 0: Copy
except: raise CopyError, 'Copy Error' 'cb_isCopyable',
ob = ob._getCopy(self) 'Copy Error',
orig_id = ob.getId() lambda self, ob: True,
id = self._get_id(ob.getId()) lambda self, ob: ob._getCopy(self),
result.append({'id':orig_id, 'new_id':id}) 1, # Take ownership.
ob._setId(id) True,
self._setObject(id, ob) ),
ob = self._getOb(id) ( # 1: Move
ob._postCopy(self, op=0) 'cb_isMoveable',
ob._postDuplicate() 'Move Error',
ob.wl_clearLocks() sanity_check,
doMove,
if op==1: 0, # Retain original ownership.
# Move operation False,
),
)[op]
for ob in oblist: for ob in oblist:
id = ob.getId() if not getattr(ob, is_doable_id)():
if not ob.cb_isMoveable(): raise CopyError(eNotSupported % escape(ob.getId()))
raise CopyError, 'Not Supported' try:
try: ob._notifyOfCopyTo(self, op=1) ob._notifyOfCopyTo(self, op=op)
except: raise CopyError, 'Move Error' except:
if not sanity_check(self, ob): raise CopyError(MessageDialog(
raise CopyError, 'This object cannot be pasted into itself' title=notify_error,
message=sys.exc_info()[1],
action='manage_main',
))
if not do_sanity_check(self, ob):
raise CopyError('This object cannot be pasted into itself')
if not set_owner:
# try to make ownership explicit so that it gets carried # try to make ownership explicit so that it gets carried
# along to the new location if needed. # along to the new location if needed.
ob.manage_changeOwnershipType(explicit=1) ob.manage_changeOwnershipType(explicit=1)
new_ob = do(self, ob)
aq_parent(aq_inner(ob))._delObject(id) orig_id = ob.getId()
ob = aq_base(ob) new_id = self._get_id(orig_id)
orig_id = id result.append({'id': orig_id, 'new_id': new_id})
id = self._get_id(id) new_ob._setId(new_id)
result.append({'id':orig_id, 'new_id':id }) if not is_indexable:
new_ob._setNonIndexable()
ob._setId(id) self._setObject(new_id, new_ob, set_owner=set_owner)
self._setObject(id, ob, set_owner=0) new_ob = self._getOb(new_id)
ob = self._getOb(id) new_ob._postCopy(self, op=op)
ob._postCopy(self, op=1) if is_clone:
new_ob.manage_afterClone(new_ob)
new_ob.wl_clearLocks()
if duplicate:
new_ob._postDuplicate()
if not set_owner:
# try to make ownership implicit if possible # try to make ownership implicit if possible
ob.manage_changeOwnershipType(explicit=0) new_ob.manage_changeOwnershipType(explicit=0)
return op, result
return result
def _postDuplicate(self): def _postDuplicate(self):
self_base = aq_base(self) self_base = aq_base(self)
...@@ -522,100 +541,27 @@ class CopyContainer: ...@@ -522,100 +541,27 @@ class CopyContainer:
If is_indexable is False, we will avoid indexing the pasted objects and If is_indexable is False, we will avoid indexing the pasted objects and
subobjects subobjects
""" """
cp=None cp = None
if cb_copy_data is not None: if cb_copy_data is not None:
cp=cb_copy_data cp = cb_copy_data
else: elif REQUEST is not None and REQUEST.has_key('__cp'):
if REQUEST and REQUEST.has_key('__cp'): cp = REQUEST['__cp']
cp=REQUEST['__cp']
if cp is None: if cp is None:
raise CopyError, eNoData raise CopyError(eNoData)
op, result = self.__duplicate(
try: cp=_cb_decode(cp) cp,
except: raise CopyError, eInvalid duplicate=False,
is_indexable=is_indexable,
oblist=[] )
op=cp[0] if REQUEST is None:
app = self.getPhysicalRoot()
result = []
for mdata in cp[1]:
m = Moniker.loadMoniker(mdata)
try: ob = m.bind(app)
except: raise CopyError, eNotFound
self._verifyObjectPaste(ob, validate_src=op+1)
oblist.append(ob)
if op==0:
# Copy operation
for ob in oblist:
if not ob.cb_isCopyable():
raise CopyError, eNotSupported % escape(ob.getId())
try: ob._notifyOfCopyTo(self, op=0)
except: raise CopyError, MessageDialog(
title='Copy Error',
message=sys.exc_info()[1],
action ='manage_main')
ob=ob._getCopy(self)
orig_id=ob.getId()
id=self._get_id(ob.getId())
result.append({'id':orig_id, 'new_id':id})
ob._setId(id)
if not is_indexable:
ob._setNonIndexable()
self._setObject(id, ob)
ob = self._getOb(id)
ob._postCopy(self, op=0)
ob.manage_afterClone(ob)
ob.wl_clearLocks()
if REQUEST is not None:
return self.manage_main(self, REQUEST, update_menu=1,
cb_dataValid=1)
if op==1:
# Move operation
for ob in oblist:
id=ob.getId()
if not ob.cb_isMoveable():
raise CopyError, eNotSupported % escape(id)
try: ob._notifyOfCopyTo(self, op=1)
except: raise CopyError, MessageDialog(
title='Move Error',
message=sys.exc_info()[1],
action ='manage_main')
if not sanity_check(self, ob):
raise CopyError, 'This object cannot be pasted into itself'
# try to make ownership explicit so that it gets carried
# along to the new location if needed.
ob.manage_changeOwnershipType(explicit=1)
aq_parent(aq_inner(ob))._delObject(id)
ob = aq_base(ob)
orig_id=id
id=self._get_id(id)
result.append({'id':orig_id, 'new_id':id })
ob._setId(id)
if not is_indexable:
ob._setNonIndexable()
self._setObject(id, ob, set_owner=0)
ob=self._getOb(id)
ob._postCopy(self, op=1)
# try to make ownership implicit if possible
ob.manage_changeOwnershipType(explicit=0)
if REQUEST is not None:
REQUEST['RESPONSE'].setCookie('__cp', 'deleted',
path='%s' % cookie_path(REQUEST),
expires='Wed, 31-Dec-97 23:59:59 GMT')
REQUEST['__cp'] = None
return self.manage_main(self, REQUEST, update_menu=1,
cb_dataValid=0)
return result return result
if op == 0:
cb_dataValid = 1
else:
cb_dataValid = 0
REQUEST['RESPONSE'].setCookie('__cp', 'deleted', path='%s' % cookie_path(REQUEST), expires='Wed, 31-Dec-97 23:59:59 GMT')
REQUEST['__cp'] = None
return self.manage_main(self, REQUEST, update_menu=1, cb_dataValid=cb_dataValid)
#### Helper methods #### Helper methods
def tryMethodCallWithTemporaryPermission(context, permission, method, def tryMethodCallWithTemporaryPermission(context, permission, method,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment