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-2006, 2008 Donald N. Allingham
|
2008-05-19 00:54:28 +05:30
|
|
|
# Copyright (C) 2008 Brian G. Matherly
|
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 Events to vCalendar."
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Standard Python Modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import os
|
2008-01-29 23:48:45 +05:30
|
|
|
import sys
|
2005-12-06 12:08:09 +05:30
|
|
|
from time import localtime
|
2006-05-10 01:24:23 +05:30
|
|
|
from gettext import gettext as _
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GNOME/GTK modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import gtk
|
2008-02-19 01:37:09 +05:30
|
|
|
from gtk import glade
|
2005-03-17 18:21:14 +05:30
|
|
|
|
2006-03-05 10:01:24 +05:30
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Set up logging
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(".ExportVCal")
|
|
|
|
|
2005-03-17 18:21:14 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# 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 Utils
|
2007-10-08 22:11:39 +05:30
|
|
|
from gen.lib import Date, EventType
|
2005-03-17 18:21:14 +05:30
|
|
|
import Errors
|
|
|
|
from QuestionDialog import ErrorDialog
|
2008-05-19 00:54:28 +05:30
|
|
|
from PluginUtils import PluginManager
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
class CalendarWriterOptionBox:
|
|
|
|
"""
|
|
|
|
Create a VBox with the option widgets and define methods to retrieve
|
2008-01-17 03:12:05 +05:30
|
|
|
the options.
|
|
|
|
|
2005-03-17 18:21:14 +05:30
|
|
|
"""
|
2008-01-15 14:46:45 +05:30
|
|
|
def __init__(self, person):
|
2005-03-17 18:21:14 +05:30
|
|
|
self.person = person
|
|
|
|
|
|
|
|
glade_file = "%s/vcalendarexport.glade" % os.path.dirname(__file__)
|
|
|
|
if not os.path.isfile(glade_file):
|
|
|
|
glade_file = "plugins/vcalendarexport.glade"
|
2008-01-30 14:33:20 +05:30
|
|
|
self.topDialog = glade.XML(glade_file, "calendarExport", "gramps")
|
2005-03-17 18:21:14 +05:30
|
|
|
self.copy = 0
|
2008-01-30 14:33:20 +05:30
|
|
|
self.filter_menu = gtk.Menu()
|
|
|
|
|
|
|
|
def get_option_box(self):
|
|
|
|
filter_obj = self.topDialog.get_widget("filter")
|
2005-03-17 18:21:14 +05:30
|
|
|
|
2008-01-30 14:33:20 +05:30
|
|
|
everyone_filter = GenericFilter()
|
|
|
|
everyone_filter.set_name(_("Entire Database"))
|
|
|
|
everyone_filter.add_rule(Rules.Person.Everyone([]))
|
2005-03-17 18:21:14 +05:30
|
|
|
|
2008-01-30 14:33:20 +05:30
|
|
|
the_filters = [everyone_filter]
|
2006-10-11 10:29:26 +05:30
|
|
|
|
2005-08-18 11:28:28 +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") %
|
2005-03-17 18:21:14 +05:30
|
|
|
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]))
|
|
|
|
|
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") %
|
|
|
|
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()]))
|
|
|
|
|
2008-02-03 08:20:09 +05:30
|
|
|
the_filters += [des, ans, com]
|
2006-10-11 10:29:26 +05:30
|
|
|
|
|
|
|
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("calendarExport").destroy()
|
|
|
|
return the_box
|
|
|
|
|
|
|
|
def parse_options(self):
|
|
|
|
self.cfilter = self.filter_menu.get_active().get_data("filter")
|
|
|
|
|
|
|
|
|
|
|
|
class CalendarWriter:
|
2008-01-15 14:46:45 +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 = {}
|
|
|
|
self.flist = {}
|
|
|
|
|
2008-01-30 14:33:20 +05:30
|
|
|
self.count = 0
|
|
|
|
self.oldval = 0
|
|
|
|
|
2005-03-17 18:21:14 +05:30
|
|
|
self.persons_details_done = []
|
|
|
|
self.persons_notes_done = []
|
|
|
|
self.person_ids = {}
|
|
|
|
|
|
|
|
if not option_box:
|
|
|
|
self.cl_setup()
|
|
|
|
else:
|
|
|
|
self.option_box.parse_options()
|
|
|
|
|
2008-06-16 20:31:46 +05:30
|
|
|
if self.option_box.cfilter is None:
|
2005-03-17 18:21:14 +05:30
|
|
|
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:
|
2008-01-15 14:46:45 +05:30
|
|
|
(m1, m2) = msg.messages()
|
|
|
|
ErrorDialog(m1, m2)
|
2005-03-17 18:21:14 +05:30
|
|
|
return
|
|
|
|
|
|
|
|
self.flist = {}
|
|
|
|
for key in self.plist:
|
|
|
|
p = self.db.get_person_from_handle(key)
|
|
|
|
for family_handle in p.get_family_handle_list():
|
|
|
|
self.flist[family_handle] = 1
|
|
|
|
|
2006-05-10 01:24:23 +05:30
|
|
|
def update_empty(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def update_real(self):
|
|
|
|
self.count += 1
|
2008-01-30 14:33:20 +05:30
|
|
|
newval = int(100 * self.count / self.total)
|
2006-05-10 01:24:23 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
self.flist = {}
|
|
|
|
|
|
|
|
for key in self.plist:
|
|
|
|
p = self.db.get_person_from_handle(key)
|
|
|
|
for family_handle in p.get_family_handle_list():
|
|
|
|
self.flist[family_handle] = 1
|
|
|
|
|
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
|
2008-01-15 14:46:45 +05:30
|
|
|
ErrorDialog(msg2, str(msg))
|
|
|
|
return False
|
2005-03-17 18:21:14 +05:30
|
|
|
except:
|
|
|
|
ErrorDialog(_("Could not create %s") % filename)
|
2008-01-15 14:46:45 +05:30
|
|
|
return False
|
2005-03-17 18:21:14 +05:30
|
|
|
|
2008-01-30 14:33:20 +05:30
|
|
|
self.writeln("BEGIN:VCALENDAR")
|
|
|
|
self.writeln("PRODID:-//GNU//Gramps//EN")
|
|
|
|
self.writeln("VERSION:1.0")
|
2006-05-10 01:24:23 +05:30
|
|
|
|
|
|
|
self.total = len(self.plist) + len(self.flist)
|
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
|
|
|
|
|
|
|
for key in self.flist:
|
|
|
|
self.write_family(key)
|
2006-05-10 01:24:23 +05:30
|
|
|
self.update()
|
2005-03-17 18:21:14 +05:30
|
|
|
|
2008-01-30 14:33:20 +05:30
|
|
|
self.writeln("")
|
|
|
|
self.writeln("END:VCALENDAR")
|
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
|
|
|
|
2008-01-15 14:46:45 +05:30
|
|
|
def write_family(self, family_handle):
|
2005-03-17 18:21:14 +05:30
|
|
|
family = self.db.get_family_from_handle(family_handle)
|
|
|
|
if family:
|
2006-03-31 09:49:06 +05:30
|
|
|
for event_ref in family.get_event_ref_list():
|
|
|
|
event = self.db.get_event_from_handle(event_ref.ref)
|
2006-05-10 01:24:23 +05:30
|
|
|
if event.get_type() == EventType.MARRIAGE:
|
2005-03-17 18:21:14 +05:30
|
|
|
m_date = event.get_date_object()
|
|
|
|
place_handle = event.get_place_handle()
|
2008-01-15 14:46:45 +05:30
|
|
|
text = _("Marriage of %s") % Utils.family_name(family,
|
|
|
|
self.db)
|
2005-03-17 18:21:14 +05:30
|
|
|
if place_handle:
|
|
|
|
place = self.db.get_place_from_handle(place_handle)
|
2005-04-26 21:34:21 +05:30
|
|
|
self.write_vevent( text, m_date, place.get_title())
|
2005-03-17 18:21:14 +05:30
|
|
|
else:
|
2005-04-26 21:34:21 +05:30
|
|
|
self.write_vevent( text, m_date)
|
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:
|
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()
|
|
|
|
place_handle = birth.get_place_handle()
|
|
|
|
if place_handle:
|
|
|
|
place = self.db.get_place_from_handle(place_handle)
|
2005-04-26 21:34:21 +05:30
|
|
|
self.write_vevent(_("Birth of %s") % person.get_primary_name().get_name(), b_date, place.get_title())
|
2005-03-17 18:21:14 +05:30
|
|
|
else:
|
2008-01-30 14:33:20 +05:30
|
|
|
self.write_vevent(_("Birth of %s")
|
|
|
|
% person.get_primary_name().get_name(), b_date)
|
2006-03-31 09:49:06 +05:30
|
|
|
|
|
|
|
death_ref = person.get_death_ref()
|
|
|
|
if death_ref:
|
|
|
|
death = self.db.get_event_from_handle(death_ref.ref)
|
2005-03-17 18:21:14 +05:30
|
|
|
if death:
|
|
|
|
d_date = death.get_date_object()
|
|
|
|
place_handle = death.get_place_handle()
|
|
|
|
if place_handle:
|
|
|
|
place = self.db.get_place_from_handle(place_handle)
|
2005-04-26 21:34:21 +05:30
|
|
|
self.write_vevent(_("Death of %s") % person.get_primary_name().get_name(), d_date, place.get_title())
|
2005-03-17 18:21:14 +05:30
|
|
|
else:
|
2005-04-26 21:34:21 +05:30
|
|
|
self.write_vevent(_("Death of %s") % person.get_primary_name().get_name(), d_date)
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
|
2008-01-15 14:46:45 +05:30
|
|
|
def format_single_date(self, subdate, thisyear, cal):
|
2005-03-17 18:21:14 +05:30
|
|
|
retval = ""
|
2008-01-15 14:46:45 +05:30
|
|
|
(day, month, year, sl) = subdate
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
if thisyear:
|
2005-04-26 21:34:21 +05:30
|
|
|
year = localtime().tm_year
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
if not cal == Date.CAL_GREGORIAN:
|
2005-04-26 21:34:21 +05:30
|
|
|
return ""
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
if year > 0:
|
|
|
|
if month > 0:
|
|
|
|
if day > 0:
|
|
|
|
retval = "%s%02d%02d" % (year, month, day)
|
|
|
|
return retval
|
|
|
|
|
|
|
|
|
2008-01-15 14:46:45 +05:30
|
|
|
def format_date(self, date, thisyear=0):
|
2005-03-17 18:21:14 +05:30
|
|
|
retval = ""
|
2007-12-20 10:40:24 +05:30
|
|
|
if date.get_modifier() == Date.MOD_TEXTONLY:
|
2005-03-17 18:21:14 +05:30
|
|
|
return ""
|
|
|
|
elif not date.is_empty():
|
|
|
|
mod = date.get_modifier()
|
|
|
|
cal = cal = date.get_calendar()
|
|
|
|
if mod == Date.MOD_SPAN or mod == Date.MOD_RANGE:
|
2008-01-15 14:46:45 +05:30
|
|
|
start = self.format_single_date(date.get_start_date(),
|
|
|
|
thisyear, cal)
|
|
|
|
end = self.format_single_date(date.get_stop_date(),
|
|
|
|
thisyear, cal)
|
2005-03-17 18:21:14 +05:30
|
|
|
if start and end:
|
2008-01-15 14:46:45 +05:30
|
|
|
retval = "DTSTART:%sT000001\nDTEND:%sT235959" % (start,
|
|
|
|
end)
|
2005-03-17 18:21:14 +05:30
|
|
|
elif mod == Date.MOD_NONE:
|
2008-01-15 14:46:45 +05:30
|
|
|
start = self.format_single_date(date.get_start_date(),
|
|
|
|
thisyear, cal)
|
2005-03-17 18:21:14 +05:30
|
|
|
if start:
|
2008-01-15 14:46:45 +05:30
|
|
|
retval = "DTSTART:%sT000001\nDTEND:%sT235959" % (start,
|
|
|
|
start)
|
2005-03-17 18:21:14 +05:30
|
|
|
return retval
|
|
|
|
|
|
|
|
def write_vevent(self, event_text, date, location=""):
|
|
|
|
date_string = self.format_date(date)
|
|
|
|
if date_string is not "":
|
2008-01-30 14:33:20 +05:30
|
|
|
self.writeln("")
|
|
|
|
self.writeln("BEGIN:VEVENT")
|
|
|
|
self.writeln("SUMMARY:%s" % event_text)
|
2005-03-17 18:21:14 +05:30
|
|
|
if location:
|
2008-01-30 14:33:20 +05:30
|
|
|
self.writeln("LOCATION:%s" % location)
|
2005-03-17 18:21:14 +05:30
|
|
|
self.writeln(date_string)
|
2008-01-30 14:33:20 +05:30
|
|
|
self.writeln("END:VEVENT")
|
2005-03-17 18:21:14 +05:30
|
|
|
|
2008-01-15 14:46:45 +05:30
|
|
|
date_string = self.format_date(date, 1)
|
2008-01-30 14:33:20 +05:30
|
|
|
self.writeln("")
|
|
|
|
self.writeln("BEGIN:VEVENT")
|
|
|
|
self.writeln("SUMMARY:"+_("Anniversary: %s") % event_text)
|
2005-03-17 18:21:14 +05:30
|
|
|
if location:
|
2008-01-30 14:33:20 +05:30
|
|
|
self.writeln("LOCATION:%s" % location)
|
2005-03-17 18:21:14 +05:30
|
|
|
self.writeln("RRULE:YD1 #0")
|
|
|
|
self.writeln(date_string)
|
2008-01-30 14:33:20 +05:30
|
|
|
self.writeln("END:VEVENT")
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2008-01-15 14:46:45 +05:30
|
|
|
def exportData(database, filename, person, option_box, callback=None):
|
|
|
|
cw = CalendarWriter(database, person, 0, filename, option_box, callback)
|
|
|
|
return cw.export_data(filename)
|
2005-03-17 18:21:14 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2008-01-17 03:12:05 +05:30
|
|
|
_title = _('vC_alendar')
|
2005-03-17 18:21:14 +05:30
|
|
|
_description = _('vCalendar is used in many calendaring and pim applications.')
|
2008-01-15 14:46:45 +05:30
|
|
|
_config = (_('vCalendar export options'), CalendarWriterOptionBox)
|
2005-03-17 18:21:14 +05:30
|
|
|
_filename = 'vcs'
|
|
|
|
|
2008-05-19 00:54:28 +05:30
|
|
|
pmgr = PluginManager.get_instance()
|
|
|
|
pmgr.register_export(exportData, _title, _description, _config, _filename)
|