2001-05-13 07:26:57 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2000 Donald N. Allingham
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
2001-11-02 23:16:33 +05:30
|
|
|
# Standard python modules
|
2001-05-13 07:26:57 +05:30
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import os
|
2001-10-12 04:26:48 +05:30
|
|
|
import string
|
2001-05-13 07:26:57 +05:30
|
|
|
|
2001-11-02 23:16:33 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GTK/Gnome modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2001-11-25 03:26:31 +05:30
|
|
|
import gtk
|
2001-11-02 23:16:33 +05:30
|
|
|
from gnome.ui import GnomeErrorDialog, GnomeWarningDialog
|
2001-05-13 07:26:57 +05:30
|
|
|
|
2001-11-02 23:16:33 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Gramps modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import const
|
2002-02-22 09:25:32 +05:30
|
|
|
import Utils
|
2002-02-02 06:44:15 +05:30
|
|
|
import ImgManip
|
2001-11-02 23:16:33 +05:30
|
|
|
from intl import gettext
|
|
|
|
_ = gettext
|
|
|
|
|
2001-05-24 03:46:19 +05:30
|
|
|
|
2001-05-13 07:26:57 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
2001-10-12 04:26:48 +05:30
|
|
|
# import_media_object
|
2001-05-13 07:26:57 +05:30
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2001-10-12 04:26:48 +05:30
|
|
|
def import_media_object(filename,path,base):
|
2001-05-13 07:26:57 +05:30
|
|
|
import shutil
|
|
|
|
|
2002-01-09 08:47:19 +05:30
|
|
|
if not os.path.exists(filename):
|
|
|
|
GnomeErrorDialog(_("Could not import %s\nThe file has been moved or deleted") % filename)
|
|
|
|
return ""
|
|
|
|
|
2002-02-22 09:25:32 +05:30
|
|
|
type = Utils.get_mime_type(filename)
|
2001-10-12 04:26:48 +05:30
|
|
|
if type[0:5] == "image":
|
|
|
|
name = "%s/%s.jpg" % (path,base)
|
|
|
|
base = "%s.jpg" % (base)
|
2001-05-13 07:26:57 +05:30
|
|
|
|
2001-10-12 04:26:48 +05:30
|
|
|
thumb = "%s/.thumb" % (path)
|
2001-06-18 00:43:49 +05:30
|
|
|
|
2001-10-12 04:26:48 +05:30
|
|
|
try:
|
|
|
|
if not os.path.exists(thumb):
|
|
|
|
os.mkdir(thumb)
|
|
|
|
except IOError,msg:
|
|
|
|
GnomeErrorDialog(_("Could not create %s") % thumb + "\n" + str(msg))
|
2001-12-19 07:32:59 +05:30
|
|
|
return ""
|
2001-10-12 04:26:48 +05:30
|
|
|
except:
|
|
|
|
GnomeErrorDialog(_("Could not create %s") % thumb)
|
2001-12-19 07:32:59 +05:30
|
|
|
return ""
|
2001-05-31 23:27:14 +05:30
|
|
|
|
2001-10-12 04:26:48 +05:30
|
|
|
try:
|
|
|
|
path = "%s/%s" % (thumb,base)
|
|
|
|
mk_thumb(filename,path,const.thumbScale)
|
|
|
|
except:
|
2002-02-02 06:44:15 +05:30
|
|
|
GnomeErrorDialog(_("Error creating the thumbnail : %s"))
|
2001-12-19 07:32:59 +05:30
|
|
|
return ""
|
2002-01-09 08:47:19 +05:30
|
|
|
|
|
|
|
try:
|
|
|
|
shutil.copy(filename,name)
|
|
|
|
except IOError,msg:
|
|
|
|
GnomeErrorDialog(_("Error copying %s") + "\n" + msg)
|
|
|
|
return ""
|
|
|
|
|
2001-10-12 04:26:48 +05:30
|
|
|
else:
|
|
|
|
bname = os.path.basename(filename)
|
|
|
|
l = string.split(bname,'.')
|
|
|
|
name = "%s/%s.%s" % (path,base,l[-1])
|
|
|
|
shutil.copy(filename,name)
|
2001-09-01 09:27:55 +05:30
|
|
|
|
2001-05-13 07:26:57 +05:30
|
|
|
return name
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# scale_image
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
def scale_image(path,size):
|
|
|
|
import GdkImlib
|
2001-05-31 08:10:08 +05:30
|
|
|
|
|
|
|
try:
|
|
|
|
image1 = GdkImlib.Image(path)
|
|
|
|
except:
|
2001-06-14 22:42:32 +05:30
|
|
|
GnomeWarningDialog(_("Could not load image file %s") % path)
|
2002-01-09 08:47:19 +05:30
|
|
|
return GdkImlib.Image(const.icon)
|
2001-05-13 07:26:57 +05:30
|
|
|
|
|
|
|
width = image1.rgb_width
|
|
|
|
height = image1.rgb_height
|
|
|
|
|
|
|
|
scale = size / float(max(width,height))
|
2002-01-09 08:47:19 +05:30
|
|
|
try:
|
|
|
|
image2 = image1.clone_scaled_image(int(scale*width), int(scale*height))
|
|
|
|
except:
|
|
|
|
GnomeWarningDialog(_("Could not load image file %s") % path)
|
|
|
|
return GdkImlib.Image(const.icon)
|
|
|
|
|
2001-05-13 07:26:57 +05:30
|
|
|
return image2
|
2001-05-31 08:10:08 +05:30
|
|
|
|
2001-05-31 23:27:14 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# scale_image
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
def mk_thumb(source,dest,size):
|
|
|
|
dir = os.path.dirname(dest)
|
2001-06-18 00:43:49 +05:30
|
|
|
try:
|
|
|
|
if not os.path.exists(dir):
|
|
|
|
os.mkdir(dir)
|
|
|
|
except IOError,msg:
|
|
|
|
GnomeErrorDialog(_("Could not create %s") % dir + "\n" + str(msg))
|
|
|
|
return
|
|
|
|
except:
|
|
|
|
GnomeErrorDialog(_("Could not create %s") % dir)
|
|
|
|
return
|
2001-05-31 23:27:14 +05:30
|
|
|
|
2002-01-01 10:55:08 +05:30
|
|
|
if os.path.exists(dest):
|
2002-01-09 08:47:19 +05:30
|
|
|
try:
|
|
|
|
os.remove(dest)
|
|
|
|
except IOError,msg:
|
|
|
|
errmsg = _("Could not replace %s") % dir
|
|
|
|
GnomeErrorDialog(errmsg + "\n" + msg)
|
|
|
|
return
|
|
|
|
|
|
|
|
if not os.path.exists(source):
|
2002-01-15 08:11:35 +05:30
|
|
|
GnomeErrorDialog(_("Could not create a thumbnail for %s\nThe file has been moved or deleted") % source)
|
2002-01-09 08:47:19 +05:30
|
|
|
|
2002-02-02 06:44:15 +05:30
|
|
|
try:
|
|
|
|
img = ImgManip.ImgManip(source)
|
|
|
|
img.jpg_thumbnail(dest,size,size)
|
|
|
|
except:
|
|
|
|
GnomeErrorDialog(_("Could not create a thumbnail for %s") % source)
|
|
|
|
return
|
2001-05-31 23:27:14 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# scale_image
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
def check_thumb(source,dest,size):
|
|
|
|
if not os.path.isfile(source):
|
2002-01-09 08:47:19 +05:30
|
|
|
return 0
|
2001-05-31 23:27:14 +05:30
|
|
|
if not os.path.isfile(dest):
|
|
|
|
mk_thumb(source,dest,size)
|
|
|
|
elif os.path.getmtime(source) > os.path.getmtime(dest):
|
|
|
|
mk_thumb(source,dest,size)
|
2002-01-09 08:47:19 +05:30
|
|
|
return 1
|
2001-05-31 23:27:14 +05:30
|
|
|
|
2001-05-31 08:10:08 +05:30
|
|
|
|