gramps/src/plugins/WriteCD.py

323 lines
12 KiB
Python
Raw Normal View History

2003-05-14 09:53:30 +05:30
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2006 Donald N. Allingham
2003-05-14 09:53:30 +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$
2003-05-15 00:51:18 +05:30
"Export to CD (nautilus)"
2003-05-14 09:53:30 +05:30
#-------------------------------------------------------------------------
#
# standard python modules
#
#-------------------------------------------------------------------------
import os
from cStringIO import StringIO
from TransUtils import sgettext as _
2003-05-14 09:53:30 +05:30
2006-03-05 10:01:24 +05:30
#------------------------------------------------------------------------
#
# Set up logging
#
#------------------------------------------------------------------------
import logging
log = logging.getLogger(".WriteCD")
2003-05-14 09:53:30 +05:30
#-------------------------------------------------------------------------
#
# GNOME/GTK modules
#
#-------------------------------------------------------------------------
import gtk
import gtk.glade
import gnome
try:
from gnomevfs import URI, create, OPEN_WRITE, make_directory, FileExistsError
except:
from gnome.vfs import URI, create, OPEN_WRITE, make_directory, FileExistsError
2003-05-14 09:53:30 +05:30
#-------------------------------------------------------------------------
#
# GRAMPS modules
#
#-------------------------------------------------------------------------
import WriteXML
import Utils
2006-03-03 05:53:04 +05:30
import Mime
2003-05-15 00:51:18 +05:30
import const
2003-05-15 04:52:35 +05:30
import QuestionDialog
import ImgManip
from PluginUtils import register_export
2003-05-14 09:53:30 +05:30
_title_string = _("Export to CD")
2003-05-14 09:53:30 +05:30
#-------------------------------------------------------------------------
#
# writeData
#
#-------------------------------------------------------------------------
def writeData(database,filename,person,option_box=None):
ret = 0
2006-03-05 10:01:24 +05:30
writer = PackageWriter(database,filename)
ret = writer.export()
return ret
2003-05-14 09:53:30 +05:30
#-------------------------------------------------------------------------
#
# PackageWriter
#
#-------------------------------------------------------------------------
class PackageWriter:
def __init__(self,database,filename="",cl=0):
2003-05-14 09:53:30 +05:30
self.db = database
self.cl = cl
self.filename = filename
def export(self):
if self.cl:
self.cl_run()
else:
self.gui_run()
2003-05-15 00:51:18 +05:30
def cl_run(self):
base = os.path.basename(self.filename)
try:
uri = URI('burn:///%s' % base)
make_directory(uri,OPEN_WRITE)
except FileExistsError, msg:
2004-04-09 09:35:48 +05:30
QuestionDialog.ErrorDialog(_("CD export preparation failed"),
"1 %s " % str(msg))
return
except:
2005-12-06 12:08:09 +05:30
uri_name = "burn:///" + base
2004-04-09 09:35:48 +05:30
QuestionDialog.ErrorDialog("CD export preparation failed",
2005-12-06 12:08:09 +05:30
'Could not create %s' % uri_name)
2004-04-09 09:35:48 +05:30
return
try:
uri = URI('burn:///%s/.thumb' % base)
make_directory(uri,OPEN_WRITE)
except FileExistsError, msg:
2004-04-09 09:35:48 +05:30
QuestionDialog.ErrorDialog("CD export preparation failed",
"2 %s " % str(msg))
return
for obj_id in self.db.get_media_object_handles():
obj = self.db.get_object_from_handle(obj_id)
oldfile = obj.get_path()
root = os.path.basename(oldfile)
if os.path.isfile(oldfile):
self.copy_file(oldfile,'burn:///%s/%s' % (base,root))
2005-05-24 18:38:06 +05:30
mime_type = obj.get_mime_type()
if mime_type and mime_type.startswith("image"):
self.make_thumbnail(base,root,obj.get_path())
else:
print "Warning: media file %s was not found," % root,\
"so it was ignored."
# Write XML now
g = create('burn:///%s/data.gramps' % base,OPEN_WRITE )
gfile = WriteXML.XmlWriter(self.db,None,1)
gfile.write_handle(g)
g.close()
def gui_run(self):
missmedia_action = 0
2003-05-14 09:53:30 +05:30
# base = os.path.basename(self.db.get_save_path())
base = os.path.basename(self.filename)
2003-05-15 00:51:18 +05:30
2003-05-15 04:52:35 +05:30
try:
uri = URI('burn:///%s' % base)
make_directory(uri,OPEN_WRITE)
except FileExistsError:
2004-04-09 09:35:48 +05:30
QuestionDialog.ErrorDialog(_("CD export preparation failed"),
"File already exists")
return
except:
2005-12-06 12:08:09 +05:30
uri_name = "burn:///" + base
2004-04-09 09:35:48 +05:30
QuestionDialog.ErrorDialog(_("CD export preparation failed"),
2005-12-06 12:08:09 +05:30
_('Could not create %s') % uri_name)
2004-04-09 09:35:48 +05:30
return
2003-05-15 04:52:35 +05:30
try:
uri = URI('burn:///%s/.thumb' % base)
make_directory(uri,OPEN_WRITE)
except FileExistsError, msg:
2004-04-09 09:35:48 +05:30
QuestionDialog.ErrorDialog("CD export preparation failed",
"4 %s " % str(msg))
return
except:
2005-12-06 12:08:09 +05:30
uri_name = "burn:///" + base + "/.thumb"
2004-04-09 09:35:48 +05:30
QuestionDialog.ErrorDialog(_("CD export preparation failed"),
2005-12-06 12:08:09 +05:30
_('Could not create %s') % uri_name)
2004-04-09 09:35:48 +05:30
return
2003-05-15 00:51:18 +05:30
#--------------------------------------------------------
def remove_clicked():
# File is lost => remove all references and the object itself
for p_id in self.db.get_family_handles():
* PeopleModel.py: simplify model interface * EditPerson.py: get_family_from_handle fixes * EditSource.py: get_family_from_handle fixes * GraphLayout.py: get_family_from_handle fixes * ImageSelect.py: get_family_from_handle fixes * MediaView.py: get_family_from_handle fixes * MergeData.py: get_family_from_handle fixes * PlaceView.py: get_family_from_handle fixes * ReadXML.py: get_family_from_handle fixes * RelLib.py: get_family_from_handle fixes * Relationship.py: get_family_from_handle fixes * SelectChild.py: get_family_from_handle fixes * SourceView.py: get_family_from_handle fixes * SubstKeywords.py: get_family_from_handle fixes * WriteXML.py: get_family_from_handle fixes * gramps_main.py: get_family_from_handle fixes * plugins/AncestorChart.py: get_family_from_handle fixes * plugins/AncestorChart2.py: get_family_from_handle fixes * plugins/AncestorReport.py: get_family_from_handle fixes * plugins/Ancestors.py: get_family_from_handle fixes * plugins/Check.py: get_family_from_handle fixes * plugins/CountAncestors.py: get_family_from_handle fixes * plugins/Desbrowser.py: get_family_from_handle fixes * plugins/DescendReport.py: get_family_from_handle fixes * plugins/DetAncestralReport.py: get_family_from_handle fixes * plugins/DetDescendantReport.py: get_family_from_handle fixes * plugins/FamilyGroup.py: get_family_from_handle fixes * plugins/FanChart.py: get_family_from_handle fixes * plugins/FtmStyleAncestors.py: get_family_from_handle fixes * plugins/FtmStyleDescendants.py: get_family_from_handle fixes * plugins/GraphViz.py: get_family_from_handle fixes * plugins/IndivComplete.py: get_family_from_handle fixes * plugins/IndivSummary.py: get_family_from_handle fixes * plugins/Merge.py: get_family_from_handle fixes * plugins/RelGraph.py: get_family_from_handle fixes * plugins/Verify.py: get_family_from_handle fixes * plugins/WebPage.py: get_family_from_handle fixes * plugins/WriteCD.py: get_family_from_handle fixes * plugins/WritePkg.py: get_family_from_handle fixes * plugins/rel_de.py: get_family_from_handle fixes * plugins/rel_hu.py: get_family_from_handle fixes * plugins/rel_ru.py: get_family_from_handle fixes svn: r3443
2004-08-20 03:05:16 +05:30
p = self.db.get_family_from_handle(p_id)
2004-02-21 11:41:59 +05:30
nl = p.get_media_list()
2003-05-15 00:51:18 +05:30
for o in nl:
if o.get_reference_handle() == self.object_handle:
2003-05-15 00:51:18 +05:30
nl.remove(o)
2004-02-21 11:41:59 +05:30
p.set_media_list(nl)
self.db.commit_family(p,None)
2004-04-09 09:35:48 +05:30
for key in self.db.get_person_handles(sort_handles=False):
p = self.db.get_person_from_handle(key)
2004-02-21 11:41:59 +05:30
nl = p.get_media_list()
2003-05-15 00:51:18 +05:30
for o in nl:
if o.get_reference_handle() == self.object_handle:
2003-05-15 00:51:18 +05:30
nl.remove(o)
2004-02-21 11:41:59 +05:30
p.set_media_list(nl)
self.db.commit_person(p,None)
for key in self.db.get_source_handles():
p = self.db.get_source_from_handle(key)
2004-02-21 11:41:59 +05:30
nl = p.get_media_list()
2003-05-15 00:51:18 +05:30
for o in nl:
if o.get_reference_handle() == self.object_handle:
2003-05-15 00:51:18 +05:30
nl.remove(o)
2004-02-21 11:41:59 +05:30
p.set_media_list(nl)
self.db.commit_source(p,None)
for key in self.db.get_place_handles():
p = self.db.get_place_from_handle(key)
2004-02-21 11:41:59 +05:30
nl = p.get_media_list()
2003-05-15 00:51:18 +05:30
for o in nl:
if o.get_reference_handle() == self.object_handle:
2003-05-15 00:51:18 +05:30
nl.remove(o)
2004-02-21 11:41:59 +05:30
p.set_media_list(nl)
self.db.commit_place(p,None)
for key in self.db.get_event_handles():
p = self.db.get_event_from_handle(key)
nl = p.get_media_list()
for o in nl:
if o.get_reference_handle() == self.object_handle:
nl.remove(o)
p.set_media_list(nl)
self.db.commit_event(p,None)
self.db.remove_object(self.object_handle,None)
2003-05-15 00:51:18 +05:30
def leave_clicked():
# File is lost => do nothing, leave as is
pass
def select_clicked():
# File is lost => select a file to replace the lost one
def fs_close_window(obj):
pass
2003-05-15 00:51:18 +05:30
def fs_ok_clicked(obj):
newfile = fs_top.get_filename()
if os.path.isfile(newfile):
2003-05-15 04:52:35 +05:30
self.copy_file(newfile,'burn:///%s/%s' % (base,obase))
2006-03-03 05:53:04 +05:30
ntype = Mime.get_type(newfile)
2005-05-24 18:38:06 +05:30
if ntype and ntype.startswith("image"):
2003-05-15 04:52:35 +05:30
self.make_thumbnail(base,obase,newfile)
2003-05-15 00:51:18 +05:30
fs_top = gtk.FileSelection("%s - GRAMPS" % _("Select file"))
fs_top.hide_fileop_buttons()
fs_top.ok_button.connect('clicked',fs_ok_clicked)
fs_top.cancel_button.connect('clicked',fs_close_window)
fs_top.run()
fs_top.destroy()
2003-05-15 00:51:18 +05:30
#----------------------------------------------------------
# Write media files first, since the database may be modified
# during the process (i.e. when removing object)
2003-05-15 04:52:35 +05:30
for obj_id in self.db.get_media_object_handles():
obj = self.db.get_object_from_handle(obj_id)
oldfile = obj.get_path()
2003-05-15 04:52:35 +05:30
root = os.path.basename(oldfile)
2003-05-15 00:51:18 +05:30
if os.path.isfile(oldfile):
2003-05-15 04:52:35 +05:30
self.copy_file(oldfile,'burn:///%s/%s' % (base,root))
2005-05-24 18:38:06 +05:30
mime_type = obj.get_mime_type()
if mime_type and mime_type.startswith("image"):
self.make_thumbnail(base,root,obj.get_path())
2003-05-15 00:51:18 +05:30
else:
# File is lost => ask what to do
self.object_handle = obj.get_handle()
if missmedia_action == 0:
mmd = QuestionDialog.MissingMediaDialog(_("Media object could not be found"),
_("%(file_name)s is referenced in the database, but no longer exists. "
"The file may have been deleted or moved to a different location. "
"You may choose to either remove the reference from the database, "
"keep the reference to the missing file, or select a new file."
) % { 'file_name' : oldfile },
remove_clicked, leave_clicked, select_clicked)
missmedia_action = mmd.default_action
elif missmedia_action == 1:
remove_clicked()
elif missmedia_action == 2:
leave_clicked()
elif missmedia_action == 3:
select_clicked()
2003-05-15 00:51:18 +05:30
# Write XML now
g = create('burn:///%s/data.gramps' % base,OPEN_WRITE )
2003-05-14 09:53:30 +05:30
gfile = WriteXML.XmlWriter(self.db,None,1)
gfile.write_handle(g)
g.close()
os.system("nautilus --no-desktop burn:///")
return 1
def copy_file(self,src,dest):
original = open(src,"r")
destobj = URI(dest)
target = create(destobj,OPEN_WRITE)
done = 0
while 1:
buf = original.read(2048)
if buf == '':
break
else:
target.write(buf)
target.close()
original.close()
def make_thumbnail(self,dbname,root,path):
img = ImgManip.ImgManip(path)
data = img.jpg_scale_data(const.thumbScale,const.thumbScale)
uri = URI('burn:///%s/.thumb/%s.jpg' % (dbname,root))
th = create(uri,OPEN_WRITE)
th.write(data)
th.close()
2003-05-14 09:53:30 +05:30
#-------------------------------------------------------------------------
#
# Register the plugin
#
#-------------------------------------------------------------------------
_title = _('Export to CD (p_ortable XML)')
_description = _('Exporting to CD copies all your data and media '
'object files to the CD Creator. You may later burn the CD '
'with this data, and that copy will be completely portable '
'across different machines and binary architectures.')
_config = None
_filename = 'burn'
2003-05-14 09:53:30 +05:30
register_export(writeData,_title,_description,_config,_filename)