2005-03-17 18:21:14 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2004 Martin Hawlisch
|
2008-01-15 14:46:45 +05:30
|
|
|
# Copyright (C) 2005-2008 Donald N. Allingham
|
2008-05-19 00:54:28 +05:30
|
|
|
# Copyright (C) 2008 Brian G. Matherly
|
2010-05-01 09:42:42 +05:30
|
|
|
# Copyright (C) 2010 Jakim Friant
|
2005-03-17 18:21:14 +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-01-17 03:12:05 +05:30
|
|
|
"Export Persons to vCard."
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Standard Python Modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2008-01-29 23:48:45 +05:30
|
|
|
import sys
|
2009-05-15 01:45:59 +05:30
|
|
|
import os
|
2010-01-18 10:12:17 +05:30
|
|
|
from gen.ggettext import gettext as _
|
2005-03-17 18:21:14 +05:30
|
|
|
|
2006-03-05 10:01:24 +05:30
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Set up logging
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(".ExportVCard")
|
|
|
|
|
2005-03-17 18:21:14 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GRAMPS modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2010-05-30 09:37:50 +05:30
|
|
|
from ExportOptions import WriterOptionBox
|
2009-04-17 00:59:40 +05:30
|
|
|
from Filters import GenericFilter, Rules, build_filter_model
|
2007-10-08 22:11:39 +05:30
|
|
|
from gen.lib import Date
|
2005-03-17 18:21:14 +05:30
|
|
|
import Errors
|
2009-05-15 01:45:59 +05:30
|
|
|
from glade import Glade
|
2005-03-17 18:21:14 +05:30
|
|
|
|
2009-05-21 22:49:50 +05:30
|
|
|
class CardWriter(object):
|
2010-05-30 09:37:50 +05:30
|
|
|
def __init__(self, database, filename, msg_callback, option_box=None, callback=None):
|
2005-03-17 18:21:14 +05:30
|
|
|
self.db = database
|
|
|
|
self.filename = filename
|
2010-05-01 09:42:42 +05:30
|
|
|
self.msg_callback = msg_callback
|
2010-05-30 09:37:50 +05:30
|
|
|
self.option_box = option_box
|
|
|
|
self.callback = callback
|
2008-07-14 21:17:15 +05:30
|
|
|
if callable(self.callback): # callback is really callable
|
2006-05-10 01:24:23 +05:30
|
|
|
self.update = self.update_real
|
|
|
|
else:
|
|
|
|
self.update = self.update_empty
|
2005-03-17 18:21:14 +05:30
|
|
|
|
2010-05-30 09:37:50 +05:30
|
|
|
if option_box:
|
2005-03-17 18:21:14 +05:30
|
|
|
self.option_box.parse_options()
|
2010-05-30 09:37:50 +05:30
|
|
|
self.db = option_box.get_filtered_database(self.db)
|
2005-03-17 18:21:14 +05:30
|
|
|
|
2006-05-10 01:24:23 +05:30
|
|
|
def update_empty(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def update_real(self):
|
|
|
|
self.count += 1
|
|
|
|
newval = int(100*self.count/self.total)
|
|
|
|
if newval != self.oldval:
|
|
|
|
self.callback(newval)
|
|
|
|
self.oldval = newval
|
|
|
|
|
2008-01-15 14:46:45 +05:30
|
|
|
def writeln(self, text):
|
2008-01-29 23:48:45 +05:30
|
|
|
#self.g.write('%s\n' % (text.encode('iso-8859-1')))
|
|
|
|
self.g.write('%s\n' % (text.encode(sys.getfilesystemencoding())))
|
2005-03-17 18:21:14 +05:30
|
|
|
|
2008-01-15 14:46:45 +05:30
|
|
|
def export_data(self, filename):
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
self.dirname = os.path.dirname (filename)
|
|
|
|
try:
|
|
|
|
self.g = open(filename,"w")
|
|
|
|
except IOError,msg:
|
|
|
|
msg2 = _("Could not create %s") % filename
|
2010-05-01 09:42:42 +05:30
|
|
|
self.msg_callback(msg2, str(msg))
|
2008-01-15 14:46:45 +05:30
|
|
|
return False
|
2005-03-17 18:21:14 +05:30
|
|
|
except:
|
2010-05-01 09:42:42 +05:30
|
|
|
self.msg_callback(_("Could not create %s") % filename)
|
2008-01-15 14:46:45 +05:30
|
|
|
return False
|
2005-03-17 18:21:14 +05:30
|
|
|
|
2006-05-10 01:24:23 +05:30
|
|
|
self.count = 0
|
|
|
|
self.oldval = 0
|
2010-05-30 09:37:50 +05:30
|
|
|
self.total = len([x for x in self.db.iter_person_handles()])
|
|
|
|
for key in self.db.iter_person_handles():
|
2005-03-17 18:21:14 +05:30
|
|
|
self.write_person(key)
|
2006-05-10 01:24:23 +05:30
|
|
|
self.update()
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
self.g.close()
|
2008-01-15 14:46:45 +05:30
|
|
|
return True
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
def write_person(self, person_handle):
|
|
|
|
person = self.db.get_person_from_handle(person_handle)
|
|
|
|
if person:
|
|
|
|
self.writeln("BEGIN:VCARD");
|
|
|
|
prname = person.get_primary_name()
|
|
|
|
|
|
|
|
self.writeln("FN:%s" % prname.get_regular_name())
|
2009-04-17 00:59:40 +05:30
|
|
|
self.writeln("N:%s;%s;%s;%s;%s" %
|
|
|
|
(prname.get_surname(),
|
|
|
|
prname.get_first_name(),
|
|
|
|
person.get_nick_name(),
|
|
|
|
prname.get_surname_prefix(),
|
|
|
|
prname.get_suffix()
|
|
|
|
)
|
|
|
|
)
|
2005-03-17 18:21:14 +05:30
|
|
|
if prname.get_title():
|
|
|
|
self.writeln("TITLE:%s" % prname.get_title())
|
|
|
|
|
2006-03-31 09:49:06 +05:30
|
|
|
birth_ref = person.get_birth_ref()
|
|
|
|
if birth_ref:
|
|
|
|
birth = self.db.get_event_from_handle(birth_ref.ref)
|
2005-03-17 18:21:14 +05:30
|
|
|
if birth:
|
|
|
|
b_date = birth.get_date_object()
|
|
|
|
mod = b_date.get_modifier()
|
2008-01-09 22:04:19 +05:30
|
|
|
if (mod != Date.MOD_TEXTONLY and
|
2007-12-20 10:40:24 +05:30
|
|
|
not b_date.is_empty() and
|
|
|
|
not mod == Date.MOD_SPAN and
|
|
|
|
not mod == Date.MOD_RANGE):
|
2008-01-15 14:46:45 +05:30
|
|
|
(day, month, year, sl) = b_date.get_start_date()
|
2005-03-17 18:21:14 +05:30
|
|
|
if day > 0 and month > 0 and year > 0:
|
2008-01-15 14:46:45 +05:30
|
|
|
self.writeln("BDAY:%s-%02d-%02d" % (year, month,
|
|
|
|
day))
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
address_list = person.get_address_list()
|
|
|
|
for address in address_list:
|
|
|
|
postbox = ""
|
|
|
|
ext = ""
|
|
|
|
street = address.get_street()
|
|
|
|
city = address.get_city()
|
|
|
|
state = address.get_state()
|
|
|
|
zip = address.get_postal_code()
|
|
|
|
country = address.get_country()
|
2005-12-06 12:08:09 +05:30
|
|
|
if street or city or state or zip or country:
|
2009-04-17 00:59:40 +05:30
|
|
|
self.writeln("ADR:%s;%s;%s;%s;%s;%s;%s" %
|
|
|
|
(postbox, ext, street, city,state, zip, country))
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
phone = address.get_phone()
|
|
|
|
if phone:
|
|
|
|
self.writeln("TEL:%s" % phone)
|
|
|
|
|
|
|
|
url_list = person.get_url_list()
|
|
|
|
for url in url_list:
|
|
|
|
href = url.get_path()
|
|
|
|
if href:
|
|
|
|
self.writeln("URL:%s" % href)
|
|
|
|
|
|
|
|
self.writeln("END:VCARD");
|
|
|
|
self.writeln("");
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2010-05-30 09:37:50 +05:30
|
|
|
def exportData(database, filename, msg_callback, option_box=None, callback=None):
|
|
|
|
cw = CardWriter(database, filename, msg_callback, option_box, callback)
|
2008-01-15 14:46:45 +05:30
|
|
|
return cw.export_data(filename)
|