2002-10-20 14:25:16 +00:00
#
# Gramps - a GTK+/GNOME based genealogy program
#
2006-03-19 03:50:47 +00:00
# Copyright (C) 2000-2006 Donald N. Allingham
2002-10-20 14:25:16 +00:00
#
# 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
#
2003-12-02 04:27:23 +00:00
# $Id$
2002-10-20 14:25:16 +00:00
"""
2008-02-24 13:55:55 +00:00
Provide the interface to allow a person to add a media object to the database .
2002-10-20 14:25:16 +00:00
"""
#-------------------------------------------------------------------------
#
# Standard python modules
#
#-------------------------------------------------------------------------
import os
#-------------------------------------------------------------------------
#
# internationalization
#
#-------------------------------------------------------------------------
2006-04-06 22:02:46 +00:00
from gettext import gettext as _
2002-10-20 14:25:16 +00:00
#-------------------------------------------------------------------------
#
# GTK/Gnome modules
#
#-------------------------------------------------------------------------
2008-02-18 20:07:09 +00:00
import gtk
2002-10-20 14:25:16 +00:00
#-------------------------------------------------------------------------
#
# gramps modules
#
#-------------------------------------------------------------------------
import const
2009-10-08 01:12:51 +00:00
import config
2002-10-20 14:25:16 +00:00
import Utils
2006-03-03 00:23:04 +00:00
import Mime
2005-12-06 06:38:09 +00:00
import GrampsDisplay
2006-04-28 00:33:44 +00:00
import ManagedWindow
2008-02-18 20:07:09 +00:00
from QuestionDialog import ErrorDialog , WarningDialog
2009-05-14 20:15:59 +00:00
from glade import Glade
2007-11-07 18:49:14 +00:00
2002-10-20 14:25:16 +00:00
#-------------------------------------------------------------------------
#
# AddMediaObject
#
#-------------------------------------------------------------------------
2006-04-28 00:33:44 +00:00
class AddMediaObject ( ManagedWindow . ManagedWindow ) :
2002-10-20 14:25:16 +00:00
"""
Displays the Add Media Dialog window , allowing the user to select
2008-01-06 10:51:20 +00:00
a file from the file system , while providing a description .
2002-10-20 14:25:16 +00:00
"""
2008-01-06 10:51:20 +00:00
def __init__ ( self , dbstate , uistate , track , mediaobj , callback = None ) :
2002-10-20 14:25:16 +00:00
"""
2008-02-24 13:55:55 +00:00
Create and displays the dialog box
2002-10-20 14:25:16 +00:00
db - the database in which the new object is to be stored
2008-01-06 10:51:20 +00:00
The mediaobject is updated with the information , and on save , the
callback function is called
2002-10-20 14:25:16 +00:00
"""
2006-04-28 00:33:44 +00:00
ManagedWindow . ManagedWindow . __init__ ( self , uistate , track , self )
2006-05-10 01:50:45 +00:00
2007-05-15 04:17:12 +00:00
self . dbase = dbstate . db
2008-01-06 10:51:20 +00:00
self . obj = mediaobj
self . callback = callback
2009-10-08 01:12:51 +00:00
self . last_directory = config . get ( ' behavior.addmedia-image-dir ' )
self . relative_path = config . get ( ' behavior.addmedia-relative-path ' )
2006-04-28 00:33:44 +00:00
2009-05-14 20:15:59 +00:00
self . glade = Glade ( )
2006-04-28 00:33:44 +00:00
self . set_window (
2009-05-14 20:15:59 +00:00
self . glade . toplevel ,
2009-05-06 14:44:22 +00:00
self . glade . get_object ( ' title ' ) ,
2006-04-28 00:33:44 +00:00
_ ( ' Select a media object ' ) )
2009-05-06 14:44:22 +00:00
self . description = self . glade . get_object ( " photoDescription " )
self . image = self . glade . get_object ( " image " )
self . file_text = self . glade . get_object ( " fname " )
2008-01-06 10:51:20 +00:00
if not ( self . last_directory and os . path . isdir ( self . last_directory ) ) :
2008-02-11 22:27:24 +00:00
self . last_directory = const . USER_HOME
#if existing path, use dir of path
if not self . obj . get_path ( ) == " " :
fullname = Utils . media_path_full ( self . dbase , self . obj . get_path ( ) )
dir = os . path . dirname ( fullname )
if os . path . isdir ( dir ) :
self . last_directory = dir
self . file_text . select_filename ( fullname )
else :
self . file_text . set_current_folder ( self . last_directory )
else :
self . file_text . set_current_folder ( self . last_directory )
if not self . obj . get_description ( ) == " " :
self . description . set_text ( self . obj . get_description ( ) )
2007-11-27 19:51:45 +00:00
2009-05-06 14:44:22 +00:00
self . relpath = self . glade . get_object ( ' relpath ' )
2008-01-06 10:51:20 +00:00
self . relpath . set_active ( self . relative_path )
2002-10-20 14:25:16 +00:00
self . temp_name = " "
2003-09-10 13:38:02 +00:00
self . object = None
2003-08-13 04:28:07 +00:00
2009-05-06 14:44:22 +00:00
self . glade . get_object ( ' fname ' ) . connect ( ' update_preview ' ,
2006-03-08 05:22:58 +00:00
self . on_name_changed )
2009-05-06 14:44:22 +00:00
self . ok_button = self . glade . get_object ( ' button79 ' )
self . help_button = self . glade . get_object ( ' button103 ' )
self . cancel_button = self . glade . get_object ( ' button81 ' )
2009-08-14 16:10:24 +00:00
self . ok_button . connect ( ' clicked ' , self . save )
2008-01-06 10:51:20 +00:00
self . ok_button . set_sensitive ( not self . dbase . readonly )
2008-04-05 14:17:15 +00:00
self . help_button . connect ( ' clicked ' , lambda x : GrampsDisplay . help ( ) )
2008-01-06 10:51:20 +00:00
self . cancel_button . connect ( ' clicked ' , self . close )
2006-04-28 00:33:44 +00:00
self . show ( )
2008-01-06 10:51:20 +00:00
self . modal_call ( )
2006-04-28 00:33:44 +00:00
def build_menu_names ( self , obj ) :
2007-05-15 04:17:12 +00:00
"""
2009-08-14 16:10:24 +00:00
Build the menu name for the window manager .
2007-05-15 04:17:12 +00:00
"""
return ( _ ( ' Select media object ' ) , None )
2003-12-02 04:27:23 +00:00
2008-01-06 10:51:20 +00:00
def save ( self , * obj ) :
2002-10-20 14:25:16 +00:00
"""
2009-08-14 16:10:24 +00:00
Callback function called when the save button is pressed .
The media object is updated , and callback called .
2002-10-20 14:25:16 +00:00
"""
2003-12-17 05:23:16 +00:00
description = unicode ( self . description . get_text ( ) )
2002-10-20 14:25:16 +00:00
2007-11-27 19:51:45 +00:00
if self . file_text . get_filename ( ) is None :
msgstr = _ ( " Import failed " )
msgstr2 = _ ( " The filename supplied could not be found. " )
ErrorDialog ( msgstr , msgstr2 )
return
filename = Utils . get_unicode_path ( self . file_text . get_filename ( ) )
full_file = filename
if self . relpath . get_active ( ) :
2009-08-14 16:10:24 +00:00
pname = unicode ( Utils . media_path ( self . dbase ) )
if not os . path . exists ( pname ) :
msgstr = _ ( " Cannot import %s " )
msgstr2 = _ ( " Directory specified in preferences: Base path for relative media paths: %s does not exist. Change preferences or do not use relative path when importing " )
ErrorDialog ( msgstr % filename , msgstr2 % pname )
return
2007-11-27 19:51:45 +00:00
filename = Utils . relative_path ( filename , pname )
mtype = Mime . get_type ( full_file )
2009-05-06 14:44:22 +00:00
description = description or os . path . basename ( filename )
2007-11-27 19:51:45 +00:00
2008-01-06 10:51:20 +00:00
self . obj . set_description ( description )
self . obj . set_mime_type ( mtype )
2007-11-27 19:51:45 +00:00
name = filename
2008-01-06 10:51:20 +00:00
self . obj . set_path ( name )
self . last_directory = os . path . dirname ( full_file )
self . relative_path = self . relpath . get_active ( )
self . _cleanup_on_exit ( )
if self . callback :
self . callback ( self . obj )
2006-05-10 01:50:45 +00:00
2007-05-15 04:17:12 +00:00
def on_name_changed ( self , * obj ) :
2002-10-20 14:25:16 +00:00
"""
Called anytime the filename text window changes . Checks to
2009-08-14 16:10:24 +00:00
see if the file exists . If it does , the image is loaded into
2002-10-20 14:25:16 +00:00
the preview window .
"""
2007-05-15 04:17:12 +00:00
fname = self . file_text . get_filename ( )
if not fname :
2007-01-22 04:59:23 +00:00
return
2007-11-16 16:45:45 +00:00
filename = Utils . get_unicode_path ( fname )
2002-10-20 14:25:16 +00:00
basename = os . path . basename ( filename )
2007-05-15 04:17:12 +00:00
( root , ext ) = os . path . splitext ( basename )
2003-12-17 05:23:16 +00:00
old_title = unicode ( self . description . get_text ( ) )
2002-10-20 14:25:16 +00:00
if old_title == ' ' or old_title == self . temp_name :
self . description . set_text ( root )
self . temp_name = root
2005-12-06 06:38:09 +00:00
filename = Utils . find_file ( filename )
if filename :
2006-03-03 00:23:04 +00:00
mtype = Mime . get_type ( filename )
2005-05-24 13:08:06 +00:00
if mtype and mtype . startswith ( " image " ) :
2007-09-08 05:54:02 +00:00
image = scale_image ( filename , const . THUMBSCALE )
2002-10-20 14:25:16 +00:00
else :
2006-03-03 00:23:04 +00:00
image = Mime . find_mime_type_pixbuf ( mtype )
2003-02-08 17:28:41 +00:00
self . image . set_from_pixbuf ( image )
2003-09-10 13:38:02 +00:00
2008-01-06 10:51:20 +00:00
def _cleanup_on_exit ( self ) :
2009-10-08 01:12:51 +00:00
config . set ( ' behavior.addmedia-image-dir ' , self . last_directory )
config . set ( ' behavior.addmedia-relative-path ' , self . relative_path )
config . save ( )
2006-03-19 03:50:47 +00:00
#-------------------------------------------------------------------------
#
# scale_image
#
#-------------------------------------------------------------------------
2007-05-15 04:17:12 +00:00
def scale_image ( path , size ) :
"""
Scales the image to the specified size
"""
2006-03-19 03:50:47 +00:00
2006-03-29 22:51:27 +00:00
title_msg = _ ( " Cannot display %s " ) % path
detail_msg = _ ( ' GRAMPS is not able to display the image file. '
' This may be caused by a corrupt file. ' )
2006-03-19 03:50:47 +00:00
try :
2006-03-29 22:51:27 +00:00
image1 = gtk . gdk . pixbuf_new_from_file ( path )
width = image1 . get_width ( )
height = image1 . get_height ( )
2007-05-15 04:17:12 +00:00
scale = size / float ( max ( width , height ) )
2006-03-19 03:50:47 +00:00
return image1 . scale_simple ( int ( scale * width ) , int ( scale * height ) ,
gtk . gdk . INTERP_BILINEAR )
except :
2006-03-29 22:51:27 +00:00
WarningDialog ( title_msg , detail_msg )
2007-09-08 05:54:02 +00:00
return gtk . gdk . pixbuf_new_from_file ( const . ICON )
2006-03-29 22:51:27 +00:00