2005-12-21 02:18:18 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2006-02-04 03:33:53 +05:30
|
|
|
# Copyright (C) 2000-2006 Donald N. Allingham
|
2010-07-22 07:46:32 +05:30
|
|
|
# Copyright (C) 2010 Michiel D. Nauta
|
2005-12-21 02:18:18 +05:30
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
|
|
|
# $Id$
|
|
|
|
|
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
MediaBase class for GRAMPS.
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GRAMPS modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2008-02-24 19:25:55 +05:30
|
|
|
from gen.lib.mediaref import MediaRef
|
2010-07-22 07:46:32 +05:30
|
|
|
from gen.lib.const import IDENTICAL, EQUAL, DIFFERENT
|
2005-12-21 02:18:18 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# MediaBase class
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2009-05-21 22:49:50 +05:30
|
|
|
class MediaBase(object):
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Base class for storing media references.
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
|
|
|
|
2007-01-08 07:19:33 +05:30
|
|
|
def __init__(self, source=None):
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Create a new MediaBase, copying from source if not None.
|
2005-12-21 02:18:18 +05:30
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:param source: Object used to initialize the new object
|
|
|
|
:type source: MediaBase
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
2010-01-22 00:12:53 +05:30
|
|
|
self.media_list = map(MediaRef, source.media_list) if source else []
|
2005-12-21 02:18:18 +05:30
|
|
|
|
2006-02-04 03:33:53 +05:30
|
|
|
def serialize(self):
|
2007-01-08 07:19:33 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Convert the object to a serialized tuple of data.
|
2007-01-08 07:19:33 +05:30
|
|
|
"""
|
2006-02-04 03:33:53 +05:30
|
|
|
return [mref.serialize() for mref in self.media_list]
|
|
|
|
|
2007-01-08 07:19:33 +05:30
|
|
|
def unserialize(self, data):
|
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Convert a serialized tuple of data to an object.
|
2007-01-08 07:19:33 +05:30
|
|
|
"""
|
2010-02-02 19:26:15 +05:30
|
|
|
self.media_list = [MediaRef().unserialize(item) for item in data]
|
2006-02-04 03:33:53 +05:30
|
|
|
|
2007-01-08 07:19:33 +05:30
|
|
|
def add_media_reference(self, media_ref):
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
2009-06-25 03:26:07 +05:30
|
|
|
Add a :class:`~gen.lib.mediaref.MediaRef` instance to the object's media list.
|
2005-12-21 02:18:18 +05:30
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:param media_ref: :class:`~gen.lib.mediaref.MediaRef` instance to be added to the object's
|
2005-12-21 02:18:18 +05:30
|
|
|
media list.
|
2009-06-25 03:26:07 +05:30
|
|
|
:type media_ref: :class:`~gen.lib.mediaref.MediaRef`
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
|
|
|
self.media_list.append(media_ref)
|
|
|
|
|
|
|
|
def get_media_list(self):
|
|
|
|
"""
|
2009-06-25 03:26:07 +05:30
|
|
|
Return the list of :class:`~gen.lib.mediaref.MediaRef` instances associated with the object.
|
2005-12-21 02:18:18 +05:30
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:returns: list of :class:`~gen.lib.mediaref.MediaRef` instances associated with the object
|
|
|
|
:rtype: list
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
|
|
|
return self.media_list
|
|
|
|
|
2007-01-08 07:19:33 +05:30
|
|
|
def set_media_list(self, media_ref_list):
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
2009-06-25 03:26:07 +05:30
|
|
|
Set the list of :class:`~gen.lib.mediaref.MediaRef` instances associated with the object.
|
2005-12-21 02:18:18 +05:30
|
|
|
It replaces the previous list.
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:param media_ref_list: list of :class:`~gen.lib.mediaref.MediaRef` instances to be assigned
|
2005-12-21 02:18:18 +05:30
|
|
|
to the object.
|
2009-06-25 03:26:07 +05:30
|
|
|
:type media_ref_list: list
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
|
|
|
self.media_list = media_ref_list
|
|
|
|
|
2010-07-22 07:46:32 +05:30
|
|
|
def _merge_media_list(self, acquisition):
|
|
|
|
"""
|
|
|
|
Merge the list of media references from acquisition with our own.
|
|
|
|
|
|
|
|
:param acquisition: the media list of this object will be merged with
|
|
|
|
the current media reference list.
|
|
|
|
:rtype acquisition: MediaBase
|
|
|
|
"""
|
|
|
|
media_list = self.media_list[:]
|
|
|
|
for addendum in acquisition.get_media_list():
|
|
|
|
for obj in media_list:
|
|
|
|
equi = obj.is_equivalent(addendum)
|
|
|
|
if equi == IDENTICAL:
|
|
|
|
break
|
|
|
|
elif equi == EQUAL:
|
|
|
|
obj.merge(addendum)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.media_list.append(addendum)
|
|
|
|
|
2007-01-08 07:19:33 +05:30
|
|
|
def has_media_reference(self, obj_handle) :
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Return True if the object or any of it's child objects has reference
|
2005-12-21 02:18:18 +05:30
|
|
|
to this media object handle.
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:param obj_handle: The media handle to be checked.
|
|
|
|
:type obj_handle: str
|
|
|
|
:returns: Returns whether the object or any of it's child objects has
|
2008-02-24 19:25:55 +05:30
|
|
|
reference to this media handle.
|
2009-06-25 03:26:07 +05:30
|
|
|
:rtype: bool
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
|
|
|
return obj_handle in [media_ref.ref for media_ref in self.media_list]
|
|
|
|
|
2007-01-08 07:19:33 +05:30
|
|
|
def remove_media_references(self, obj_handle_list):
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Remove references to all media handles in the list.
|
2005-12-21 02:18:18 +05:30
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:param obj_handle_list: The list of media handles to be removed.
|
|
|
|
:type obj_handle_list: list
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
|
|
|
new_media_list = [ media_ref for media_ref in self.media_list \
|
|
|
|
if media_ref.ref not in obj_handle_list ]
|
|
|
|
self.media_list = new_media_list
|
|
|
|
|
2007-01-08 07:19:33 +05:30
|
|
|
def replace_media_references(self, old_handle, new_handle):
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
2010-07-22 07:46:32 +05:30
|
|
|
Replace all references to old media handle with the new handle and
|
|
|
|
merge equivalent entries.
|
2005-12-21 02:18:18 +05:30
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:param old_handle: The media handle to be replaced.
|
|
|
|
:type old_handle: str
|
|
|
|
:param new_handle: The media handle to replace the old one with.
|
|
|
|
:type new_handle: str
|
2005-12-21 02:18:18 +05:30
|
|
|
"""
|
|
|
|
refs_list = [ media_ref.ref for media_ref in self.media_list ]
|
2010-07-22 07:46:32 +05:30
|
|
|
new_ref = None
|
|
|
|
if new_handle in refs_list:
|
|
|
|
new_ref = self.media_list[refs_list.index(new_handle)]
|
2005-12-21 02:18:18 +05:30
|
|
|
n_replace = refs_list.count(old_handle)
|
|
|
|
for ix_replace in xrange(n_replace):
|
2010-07-22 07:46:32 +05:30
|
|
|
idx = refs_list.index(old_handle)
|
|
|
|
self.media_list[idx].ref = new_handle
|
|
|
|
refs_list[idx] = new_handle
|
|
|
|
if new_ref:
|
|
|
|
media_ref = self.media_list[idx]
|
|
|
|
equi = new_ref.is_equivalent(media_ref)
|
|
|
|
if equi != DIFFERENT:
|
|
|
|
if equi == EQUAL:
|
|
|
|
new_ref.merge(media_ref)
|
|
|
|
self.media_list.pop(idx)
|
|
|
|
refs_list.pop(idx)
|