2005-03-17 18:21:14 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2004 Martin Hawlisch
|
2006-02-04 03:33:53 +05:30
|
|
|
# Copyright (C) 2005-2006 Donald N. Allingham
|
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$
|
|
|
|
|
|
|
|
"Export Persons to vCard"
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Standard Python Modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import os
|
2006-05-10 01:24:23 +05:30
|
|
|
from gettext 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
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GNOME/GTK modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import gtk
|
|
|
|
import gtk.glade
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GRAMPS modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2006-05-03 06:32:54 +05:30
|
|
|
from Filters import GenericFilter, Rules, build_filter_menu
|
2005-03-17 18:21:14 +05:30
|
|
|
import const
|
2006-02-04 03:33:53 +05:30
|
|
|
from RelLib import Date
|
2005-03-17 18:21:14 +05:30
|
|
|
import Errors
|
|
|
|
from QuestionDialog import ErrorDialog
|
2006-03-11 06:42:06 +05:30
|
|
|
from PluginUtils import register_export
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
class CardWriterOptionBox:
|
|
|
|
"""
|
|
|
|
Create a VBox with the option widgets and define methods to retrieve
|
|
|
|
the options.
|
|
|
|
"""
|
|
|
|
def __init__(self,person):
|
|
|
|
self.person = person
|
|
|
|
|
|
|
|
def get_option_box(self):
|
|
|
|
|
|
|
|
glade_file = "%s/vcardexport.glade" % os.path.dirname(__file__)
|
|
|
|
if not os.path.isfile(glade_file):
|
|
|
|
glade_file = "plugins/vcardexport.glade"
|
|
|
|
|
|
|
|
self.topDialog = gtk.glade.XML(glade_file,"vcardExport","gramps")
|
|
|
|
|
|
|
|
filter_obj = self.topDialog.get_widget("filter")
|
|
|
|
self.copy = 0
|
|
|
|
|
2006-05-03 06:32:54 +05:30
|
|
|
all = GenericFilter()
|
2005-03-17 18:21:14 +05:30
|
|
|
all.set_name(_("Entire Database"))
|
2006-05-03 11:59:07 +05:30
|
|
|
all.add_rule(Rules.Person.Everyone([]))
|
2005-03-17 18:21:14 +05:30
|
|
|
|
2006-10-11 10:29:26 +05:30
|
|
|
the_filters = [all]
|
|
|
|
|
2005-07-09 01:54:54 +05:30
|
|
|
if self.person:
|
2006-05-03 06:32:54 +05:30
|
|
|
des = GenericFilter()
|
2005-07-09 01:54:54 +05:30
|
|
|
des.set_name(_("Descendants of %s") %
|
|
|
|
self.person.get_primary_name().get_name())
|
2006-05-03 11:59:07 +05:30
|
|
|
des.add_rule(Rules.Person.IsDescendantOf(
|
2005-07-09 01:54:54 +05:30
|
|
|
[self.person.get_gramps_id(),1]))
|
2005-03-17 18:21:14 +05:30
|
|
|
|
2006-05-03 06:32:54 +05:30
|
|
|
ans = GenericFilter()
|
2005-07-09 01:54:54 +05:30
|
|
|
ans.set_name(_("Ancestors of %s") %
|
|
|
|
self.person.get_primary_name().get_name())
|
2006-05-03 11:59:07 +05:30
|
|
|
ans.add_rule(Rules.Person.IsAncestorOf(
|
2005-07-09 01:54:54 +05:30
|
|
|
[self.person.get_gramps_id(),1]))
|
2005-03-17 18:21:14 +05:30
|
|
|
|
2006-05-03 06:32:54 +05:30
|
|
|
com = GenericFilter()
|
2005-07-09 01:54:54 +05:30
|
|
|
com.set_name(_("People with common ancestor with %s") %
|
2005-03-17 18:21:14 +05:30
|
|
|
self.person.get_primary_name().get_name())
|
2006-05-03 11:59:07 +05:30
|
|
|
com.add_rule(Rules.Person.HasCommonAncestorWith(
|
2005-07-09 01:54:54 +05:30
|
|
|
[self.person.get_gramps_id()]))
|
2005-03-17 18:21:14 +05:30
|
|
|
|
2006-10-11 10:29:26 +05:30
|
|
|
the_filers += [all,des,ans,com]
|
|
|
|
|
|
|
|
from Filters import CustomFilters
|
|
|
|
the_filters.extend(CustomFilters.get_filters('Person'))
|
|
|
|
self.filter_menu = build_filter_menu(the_filters)
|
2005-03-17 18:21:14 +05:30
|
|
|
filter_obj.set_menu(self.filter_menu)
|
|
|
|
|
|
|
|
the_box = self.topDialog.get_widget('vbox1')
|
|
|
|
the_parent = self.topDialog.get_widget('dialog-vbox1')
|
|
|
|
the_parent.remove(the_box)
|
|
|
|
self.topDialog.get_widget("vcardExport").destroy()
|
|
|
|
return the_box
|
|
|
|
|
|
|
|
def parse_options(self):
|
|
|
|
self.cfilter = self.filter_menu.get_active().get_data("filter")
|
|
|
|
|
|
|
|
class CardWriter:
|
2006-05-10 01:24:23 +05:30
|
|
|
def __init__(self,database,person,cl=0,filename="",
|
|
|
|
option_box=None,callback=None):
|
2005-03-17 18:21:14 +05:30
|
|
|
self.db = database
|
|
|
|
self.person = person
|
|
|
|
self.option_box = option_box
|
|
|
|
self.cl = cl
|
|
|
|
self.filename = filename
|
2006-05-10 01:24:23 +05:30
|
|
|
self.callback = callback
|
|
|
|
if '__call__' in dir(self.callback): # callback is really callable
|
|
|
|
self.update = self.update_real
|
|
|
|
else:
|
|
|
|
self.update = self.update_empty
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
self.plist = {}
|
|
|
|
|
|
|
|
if not option_box:
|
|
|
|
self.cl_setup()
|
|
|
|
else:
|
|
|
|
self.option_box.parse_options()
|
|
|
|
|
|
|
|
if self.option_box.cfilter == None:
|
|
|
|
for p in self.db.get_person_handles(sort_handles=False):
|
|
|
|
self.plist[p] = 1
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
for p in self.option_box.cfilter.apply(self.db, self.db.get_person_handles(sort_handles=False)):
|
|
|
|
self.plist[p] = 1
|
|
|
|
except Errors.FilterError, msg:
|
|
|
|
(m1,m2) = msg.messages()
|
|
|
|
ErrorDialog(m1,m2)
|
|
|
|
return
|
|
|
|
|
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
|
|
|
|
|
2005-03-17 18:21:14 +05:30
|
|
|
def cl_setup(self):
|
|
|
|
for p in self.db.get_person_handles(sort_handles=False):
|
|
|
|
self.plist[p] = 1
|
|
|
|
|
|
|
|
def writeln(self,text):
|
|
|
|
self.g.write('%s\n' % (text.encode('iso-8859-1')))
|
|
|
|
|
|
|
|
def export_data(self,filename):
|
|
|
|
|
|
|
|
self.dirname = os.path.dirname (filename)
|
|
|
|
try:
|
|
|
|
self.g = open(filename,"w")
|
|
|
|
except IOError,msg:
|
|
|
|
msg2 = _("Could not create %s") % filename
|
|
|
|
ErrorDialog(msg2,str(msg))
|
|
|
|
return 0
|
|
|
|
except:
|
|
|
|
ErrorDialog(_("Could not create %s") % filename)
|
|
|
|
return 0
|
|
|
|
|
2006-05-10 01:24:23 +05:30
|
|
|
self.count = 0
|
|
|
|
self.oldval = 0
|
|
|
|
self.total = len(self.plist)
|
2005-03-17 18:21:14 +05:30
|
|
|
for key in self.plist:
|
|
|
|
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()
|
2006-05-10 01:24:23 +05:30
|
|
|
return 1
|
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())
|
|
|
|
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()))
|
|
|
|
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()
|
|
|
|
if not b_date.get_text() and not b_date.is_empty() and not mod == Date.MOD_SPAN and not mod == Date.MOD_RANGE:
|
|
|
|
(day,month,year,sl) = b_date.get_start_date()
|
|
|
|
if day > 0 and month > 0 and year > 0:
|
|
|
|
self.writeln("BDAY:%s-%02d-%02d" % (year,month,day))
|
|
|
|
|
|
|
|
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:
|
2005-03-17 18:21:14 +05:30
|
|
|
self.writeln("ADR:%s;%s;%s;%s;%s;%s;%s" % (postbox,ext,street,city,state,zip,country))
|
|
|
|
|
|
|
|
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("");
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2006-05-10 01:24:23 +05:30
|
|
|
def exportData(database,filename,person,option_box,callback=None):
|
2005-03-17 18:21:14 +05:30
|
|
|
ret = 0
|
2006-05-10 01:24:23 +05:30
|
|
|
cw = CardWriter(database,person,0,filename,option_box,callback)
|
2006-03-05 10:01:24 +05:30
|
|
|
ret = cw.export_data(filename)
|
2005-03-17 18:21:14 +05:30
|
|
|
return ret
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
_title = _('vCard')
|
|
|
|
_description = _('vCard is used in many addressbook and pim applications.')
|
|
|
|
_config = (_('vCard export options'),CardWriterOptionBox)
|
|
|
|
_filename = 'vcf'
|
|
|
|
|
|
|
|
register_export(exportData,_title,_description,_config,_filename)
|