2004-05-18 12:30:34 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2005-01-26 02:55:15 +05:30
|
|
|
# Copyright (C) 2000-2005 Donald N. Allingham
|
2004-05-18 12:30:34 +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.
|
|
|
|
#
|
2007-09-10 08:33:46 +05:30
|
|
|
# This program is distributed in the hope that it will be useful,
|
2004-05-18 12:30:34 +05:30
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
2004-09-30 01:04:16 +05:30
|
|
|
# $Id$
|
|
|
|
|
2007-09-10 08:33:46 +05:30
|
|
|
"""
|
|
|
|
Handles the Tip of the Day dialog
|
|
|
|
"""
|
|
|
|
|
2004-09-30 01:04:16 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# standard python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2007-11-13 13:08:36 +05:30
|
|
|
from xml.parsers.expat import ParserCreate,ExpatError
|
2004-09-30 01:04:16 +05:30
|
|
|
from random import Random
|
2006-04-07 03:32:46 +05:30
|
|
|
from gettext import gettext as _
|
2006-03-06 10:52:45 +05:30
|
|
|
import os
|
2004-09-30 01:04:16 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GTK/GNOME modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GRAMPS modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2004-05-18 12:30:34 +05:30
|
|
|
import const
|
2006-03-03 05:40:52 +05:30
|
|
|
import Config
|
2006-04-28 02:06:32 +05:30
|
|
|
import ManagedWindow
|
2007-11-13 13:08:36 +05:30
|
|
|
from QuestionDialog import ErrorDialog
|
2009-05-15 01:45:59 +05:30
|
|
|
from glade import Glade
|
2009-05-06 20:14:22 +05:30
|
|
|
|
2004-09-30 01:04:16 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Tip Display class
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2006-04-28 02:06:32 +05:30
|
|
|
class TipOfDay(ManagedWindow.ManagedWindow):
|
2007-09-10 08:33:46 +05:30
|
|
|
def __init__(self, uistate):
|
2006-04-28 02:06:32 +05:30
|
|
|
|
|
|
|
ManagedWindow.ManagedWindow.__init__(self, uistate, [], self)
|
|
|
|
|
2009-05-15 01:45:59 +05:30
|
|
|
xml = Glade()
|
|
|
|
window = xml.toplevel
|
2007-09-10 08:33:46 +05:30
|
|
|
self.set_window(window,
|
2009-05-06 20:14:22 +05:30
|
|
|
xml.get_object("title"),
|
2007-09-10 08:33:46 +05:30
|
|
|
_("Tip of the Day"),
|
2006-04-28 02:06:32 +05:30
|
|
|
_("Tip of the Day"))
|
|
|
|
|
2009-05-06 20:14:22 +05:30
|
|
|
self.tip = xml.get_object("tip")
|
|
|
|
self.use = xml.get_object('usetips')
|
2006-05-17 19:20:49 +05:30
|
|
|
self.use.set_active(Config.get(Config.USE_TIPS))
|
2009-05-06 20:14:22 +05:30
|
|
|
image = xml.get_object('image')
|
2007-09-10 08:33:46 +05:30
|
|
|
image.set_from_file(os.path.join(const.IMAGE_DIR, 'splash.jpg'))
|
2005-12-06 12:08:09 +05:30
|
|
|
|
2009-05-06 20:14:22 +05:30
|
|
|
next = xml.get_object('next')
|
2007-09-10 08:33:46 +05:30
|
|
|
next.connect("clicked", self.next_tip_cb)
|
2009-05-06 20:14:22 +05:30
|
|
|
close = xml.get_object('close')
|
2007-09-10 08:33:46 +05:30
|
|
|
close.connect("clicked", self.close_cb)
|
2006-05-17 19:20:49 +05:30
|
|
|
|
2007-11-13 13:08:36 +05:30
|
|
|
try:
|
|
|
|
tparser = TipParser()
|
|
|
|
except (IOError,ExpatError), e:
|
|
|
|
self.close()
|
|
|
|
ErrorDialog(
|
|
|
|
_("Failed to display tip of the day"),
|
|
|
|
_("Unable to read the tips from external file.\n\n%s")%e)
|
|
|
|
return
|
2007-09-10 08:33:46 +05:30
|
|
|
self.tip_list = tparser.get()
|
2004-05-18 12:30:34 +05:30
|
|
|
|
2006-05-17 19:20:49 +05:30
|
|
|
self.new_index = range(len(self.tip_list))
|
|
|
|
Random().shuffle(self.new_index)
|
2004-09-30 01:04:16 +05:30
|
|
|
|
2006-05-17 19:20:49 +05:30
|
|
|
self.index = 0
|
|
|
|
self.next_tip_cb()
|
2004-05-18 12:30:34 +05:30
|
|
|
|
2006-05-17 19:20:49 +05:30
|
|
|
window.show_all()
|
2006-04-28 02:06:32 +05:30
|
|
|
|
2007-11-24 16:40:57 +05:30
|
|
|
def escape(self,text):
|
|
|
|
text = text.replace('&','&'); # Must be first
|
|
|
|
text = text.replace(' > ',' > '); # Replace standalone > char
|
|
|
|
text = text.replace('"','"') # quotes
|
|
|
|
return text
|
|
|
|
|
2007-09-10 08:33:46 +05:30
|
|
|
def next_tip_cb(self, dummy=None):
|
2007-11-24 16:40:57 +05:30
|
|
|
tip_text = self.escape(self.tip_list[self.new_index[self.index]])
|
2006-10-27 07:29:33 +05:30
|
|
|
self.tip.set_text(_(tip_text))
|
2006-05-17 19:20:49 +05:30
|
|
|
self.tip.set_use_markup(True)
|
2009-05-06 20:14:22 +05:30
|
|
|
self.index = (self.index + 1) % len(self.tip_list)
|
2006-10-27 07:29:33 +05:30
|
|
|
|
2007-09-10 08:33:46 +05:30
|
|
|
def close_cb(self, dummy=None):
|
|
|
|
Config.set(Config.USE_TIPS, self.use.get_active())
|
2006-05-17 19:20:49 +05:30
|
|
|
self.close()
|
|
|
|
|
2007-09-10 08:33:46 +05:30
|
|
|
def build_menu_names(self, obj):
|
2006-04-28 02:06:32 +05:30
|
|
|
return (_("Tip of the Day"), None)
|
2004-05-18 12:30:34 +05:30
|
|
|
|
2004-09-30 01:04:16 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Tip parser class
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2004-05-18 12:30:34 +05:30
|
|
|
class TipParser:
|
|
|
|
"""
|
|
|
|
Interface to the document template file
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Create a template parser. The parser loads map of tempate names
|
2004-05-18 12:30:34 +05:30
|
|
|
to the file containing the tempate.
|
|
|
|
|
|
|
|
data - dictionary that holds the name to path mappings
|
|
|
|
fpath - filename of the XML file
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.mylist = []
|
2007-09-10 08:33:46 +05:30
|
|
|
self.skip = False
|
2007-09-08 11:24:02 +05:30
|
|
|
xml_file = open(const.TIP_DATA)
|
2004-05-18 12:30:34 +05:30
|
|
|
self.tlist = []
|
2007-09-10 08:33:46 +05:30
|
|
|
parser = ParserCreate()
|
|
|
|
parser.StartElementHandler = self.startElement
|
|
|
|
parser.EndElementHandler = self.endElement
|
|
|
|
parser.CharacterDataHandler = self.characters
|
|
|
|
parser.ParseFile(xml_file)
|
2004-08-23 04:46:57 +05:30
|
|
|
xml_file.close()
|
2004-05-18 12:30:34 +05:30
|
|
|
|
|
|
|
def get(self):
|
2007-09-10 08:33:46 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Return the list of tips
|
2007-09-10 08:33:46 +05:30
|
|
|
"""
|
2004-05-18 12:30:34 +05:30
|
|
|
return self.mylist
|
|
|
|
|
2007-09-10 08:33:46 +05:30
|
|
|
def setDocumentLocator(self, locator):
|
2008-02-24 19:25:55 +05:30
|
|
|
"""Set the XML document locator"""
|
2004-05-18 12:30:34 +05:30
|
|
|
self.locator = locator
|
|
|
|
|
2007-09-10 08:33:46 +05:30
|
|
|
def startElement(self, tag, attrs):
|
2004-05-18 12:30:34 +05:30
|
|
|
"""
|
|
|
|
Loads the dictionary when an XML tag of 'template' is found. The format
|
|
|
|
XML tag is <template title=\"name\" file=\"path\">
|
|
|
|
"""
|
2004-09-30 01:04:16 +05:30
|
|
|
if tag == "tip":
|
|
|
|
self.tlist = []
|
2006-03-18 00:45:16 +05:30
|
|
|
# Skip all tips with xml:lang attribute, as they are
|
|
|
|
# already in the translation catalog
|
2008-07-17 23:40:32 +05:30
|
|
|
self.skip = 'xml:lang' in attrs
|
2004-09-30 01:04:16 +05:30
|
|
|
elif tag != "tips":
|
|
|
|
# let all the other tags through, except for the "tips" tag
|
|
|
|
self.tlist.append("<%s>" % tag)
|
2004-05-18 12:30:34 +05:30
|
|
|
|
2007-09-10 08:33:46 +05:30
|
|
|
def endElement(self, tag):
|
2006-03-18 00:45:16 +05:30
|
|
|
if tag == "tip" and not self.skip:
|
2006-10-27 07:29:33 +05:30
|
|
|
text = ''.join(self.tlist)
|
2004-09-30 01:04:16 +05:30
|
|
|
self.mylist.append(' '.join(text.split()))
|
|
|
|
elif tag != "tips":
|
|
|
|
# let all the other tags through, except for the "tips" tag
|
|
|
|
self.tlist.append("</%s>" % tag)
|
2004-05-18 12:30:34 +05:30
|
|
|
|
|
|
|
def characters(self, data):
|
|
|
|
self.tlist.append(data)
|