2006-11-06 08:21:09 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2000-2006 Donald N. Allingham
|
|
|
|
#
|
|
|
|
# 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: Check.py 7321 2006-09-13 02:57:45Z dallingham $
|
|
|
|
|
|
|
|
"Database Processing/Check and repair database"
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import os
|
|
|
|
import cStringIO
|
|
|
|
import sets
|
|
|
|
from gettext import gettext as _
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Set up logging
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(".RemoveUnused")
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# gtk modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import gtk
|
|
|
|
import gtk.glade
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GRAMPS modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import RelLib
|
|
|
|
import Utils
|
|
|
|
import const
|
|
|
|
import ManagedWindow
|
|
|
|
|
|
|
|
from PluginUtils import Tool, register_tool
|
|
|
|
from QuestionDialog import OkDialog, MissingMediaDialog
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# runTool
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2006-11-06 09:53:10 +05:30
|
|
|
class RemoveUnused:
|
2006-11-06 08:21:09 +05:30
|
|
|
def __init__(self, dbstate, uistate, options_class, name, callback=None):
|
|
|
|
|
2006-11-06 09:53:10 +05:30
|
|
|
self.db = dbstate.db
|
|
|
|
self.dbstate = dbstate
|
|
|
|
self.uistate = uistate
|
2006-11-06 08:21:09 +05:30
|
|
|
|
|
|
|
if self.db.readonly:
|
|
|
|
return
|
2006-11-06 09:53:10 +05:30
|
|
|
|
|
|
|
if uistate:
|
|
|
|
self.init_gui()
|
|
|
|
else:
|
|
|
|
self.run_tool(cli=True)
|
|
|
|
|
|
|
|
def init_gui(self):
|
|
|
|
a = gtk.Dialog("%s - GRAMPS" % _('Remove unused objects'),
|
|
|
|
flags=gtk.DIALOG_MODAL,
|
|
|
|
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
|
|
|
|
gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
|
|
|
|
|
|
|
|
a.set_size_request(400, 200)
|
|
|
|
a.set_border_width(12)
|
|
|
|
a.set_has_separator(False)
|
|
|
|
|
|
|
|
self.event = gtk.CheckButton(_('Remove unused events'))
|
|
|
|
self.source = gtk.CheckButton(_('Remove unused sources'))
|
|
|
|
self.place = gtk.CheckButton(_('Remove unused places'))
|
|
|
|
|
|
|
|
self.event.set_active(True)
|
|
|
|
self.source.set_active(True)
|
|
|
|
self.place.set_active(True)
|
|
|
|
|
|
|
|
label = gtk.Label('<span size="larger" weight="bold">%s</span>' % _('Remove unused objects'))
|
|
|
|
label.set_use_markup(True)
|
|
|
|
|
|
|
|
a.vbox.add(label)
|
|
|
|
a.vbox.add(self.event)
|
|
|
|
a.vbox.add(self.source)
|
|
|
|
a.vbox.add(self.place)
|
|
|
|
a.vbox.show_all()
|
|
|
|
result = a.run()
|
|
|
|
a.destroy()
|
|
|
|
|
|
|
|
if result == gtk.RESPONSE_ACCEPT:
|
|
|
|
self.run_tool(self.event.get_active(),
|
|
|
|
self.source.get_active(),
|
|
|
|
self.place.get_active())
|
|
|
|
|
|
|
|
def run_tool(self, clean_events, clean_sources, clean_places):
|
2006-11-07 02:57:49 +05:30
|
|
|
trans = self.db.transaction_begin("",batch=False)
|
2006-11-06 08:21:09 +05:30
|
|
|
self.db.disable_signals()
|
2006-11-06 09:53:10 +05:30
|
|
|
checker = CheckIntegrity(self.dbstate, self.uistate, trans)
|
|
|
|
if clean_events:
|
|
|
|
checker.cleanup_events()
|
|
|
|
if clean_sources:
|
|
|
|
checker.cleanup_sources()
|
|
|
|
if clean_places:
|
|
|
|
checker.cleanup_places()
|
2006-11-06 08:21:09 +05:30
|
|
|
|
|
|
|
self.db.transaction_commit(trans, _("Remove unused objects"))
|
|
|
|
self.db.enable_signals()
|
|
|
|
self.db.request_rebuild()
|
|
|
|
|
2006-11-06 09:53:10 +05:30
|
|
|
errs = checker.build_report()
|
2006-11-06 08:21:09 +05:30
|
|
|
if errs:
|
2006-11-07 02:57:49 +05:30
|
|
|
Report(self.uistate, checker.text.getvalue())
|
2006-11-06 08:21:09 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
class CheckIntegrity:
|
|
|
|
|
|
|
|
def __init__(self, dbstate, uistate, trans):
|
|
|
|
self.db = dbstate.db
|
2006-11-07 02:57:49 +05:30
|
|
|
self.uistate = uistate
|
2006-11-06 08:21:09 +05:30
|
|
|
self.trans = trans
|
2006-11-07 02:57:49 +05:30
|
|
|
self.place_cnt = 0
|
|
|
|
self.source_cnt = 0
|
|
|
|
self.event_cnt = 0
|
2006-11-06 08:21:09 +05:30
|
|
|
self.progress = Utils.ProgressMeter(_('Checking database'),'')
|
|
|
|
|
|
|
|
def cleanup_events(self):
|
|
|
|
self.progress.set_pass(_('Removing unused events'),
|
|
|
|
self.db.get_number_of_events())
|
|
|
|
|
|
|
|
self.event_cnt = 0
|
|
|
|
for handle in self.db.event_map.keys():
|
2006-11-07 02:57:49 +05:30
|
|
|
hlist = [ x for x in self.db.find_backlink_handles(handle)]
|
|
|
|
if len(hlist) == 0:
|
|
|
|
self.db.remove_event(handle, self.trans)
|
2006-11-06 08:21:09 +05:30
|
|
|
self.event_cnt += 1
|
|
|
|
|
|
|
|
def cleanup_sources(self):
|
|
|
|
self.progress.set_pass(_('Removing unused sources'),
|
|
|
|
self.db.get_number_of_events())
|
|
|
|
|
|
|
|
self.source_cnt = 0
|
|
|
|
for handle in self.db.event_map.keys():
|
2006-11-07 02:57:49 +05:30
|
|
|
hlist = [ x for x in self.db.find_backlink_handles(handle)]
|
|
|
|
if len(hlist) == 0:
|
|
|
|
self.db.remove_source(handle, self.trans)
|
2006-11-06 08:21:09 +05:30
|
|
|
self.source_cnt += 1
|
|
|
|
|
2006-11-06 09:53:10 +05:30
|
|
|
def cleanup_places(self):
|
|
|
|
self.progress.set_pass(_('Removing unused places'),
|
|
|
|
self.db.get_number_of_places())
|
|
|
|
|
2006-11-07 02:57:49 +05:30
|
|
|
self.place_cnt = 0
|
2006-11-06 09:53:10 +05:30
|
|
|
for handle in self.db.place_map.keys():
|
2006-11-07 02:57:49 +05:30
|
|
|
hlist = [ x for x in self.db.find_backlink_handles(handle)]
|
|
|
|
if len(hlist) == 0:
|
|
|
|
self.db.remove_place(handle, self.trans)
|
|
|
|
self.place_cnt += 1
|
2006-11-06 09:53:10 +05:30
|
|
|
|
2006-11-06 08:21:09 +05:30
|
|
|
def build_report(self,cl=0):
|
|
|
|
self.progress.close()
|
|
|
|
|
2006-11-07 02:57:49 +05:30
|
|
|
errors = self.event_cnt + self.source_cnt + self.place_cnt
|
2006-11-06 08:21:09 +05:30
|
|
|
|
|
|
|
if errors == 0:
|
|
|
|
if cl:
|
|
|
|
print "No unreferenced objects were found."
|
|
|
|
else:
|
|
|
|
OkDialog(_("No unreferenced objects were found."),
|
|
|
|
_('The database has passed internal checks'))
|
|
|
|
return 0
|
|
|
|
|
|
|
|
self.text = cStringIO.StringIO()
|
|
|
|
if self.event_cnt > 0:
|
|
|
|
if self.event_cnt == 1:
|
|
|
|
self.text.write(_("1 non-referenced event removed\n"))
|
|
|
|
else:
|
|
|
|
self.text.write(_("%d non-referenced event removed\n") % self.event_cnt)
|
|
|
|
|
|
|
|
if self.source_cnt > 0:
|
|
|
|
if self.source_cnt == 1:
|
|
|
|
self.text.write(_("1 non-referenced source removed\n"))
|
|
|
|
else:
|
|
|
|
self.text.write(_("%d non-referenced source removed\n") % self.source_cnt)
|
|
|
|
|
2006-11-07 02:57:49 +05:30
|
|
|
if self.place_cnt > 0:
|
|
|
|
if self.place_cnt == 1:
|
|
|
|
self.text.write(_("1 non-referenced place removed\n"))
|
|
|
|
else:
|
|
|
|
self.text.write(_("%d non-referenced place removed\n") % self.place_cnt)
|
|
|
|
|
2006-11-06 08:21:09 +05:30
|
|
|
return errors
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Display the results
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
class Report(ManagedWindow.ManagedWindow):
|
|
|
|
|
|
|
|
def __init__(self, uistate, text, cl=0):
|
|
|
|
if cl:
|
|
|
|
print text
|
|
|
|
return
|
|
|
|
|
|
|
|
ManagedWindow.ManagedWindow.__init__(self, uistate, [], self)
|
|
|
|
|
|
|
|
base = os.path.dirname(__file__)
|
|
|
|
glade_file = base + os.sep + "summary.glade"
|
|
|
|
topDialog = gtk.glade.XML(glade_file,"summary","gramps")
|
|
|
|
topDialog.get_widget("close").connect('clicked',self.close)
|
|
|
|
|
|
|
|
window = topDialog.get_widget("summary")
|
|
|
|
textwindow = topDialog.get_widget("textwindow")
|
|
|
|
textwindow.get_buffer().set_text(text)
|
|
|
|
|
|
|
|
self.set_window(window,
|
|
|
|
topDialog.get_widget("title"),
|
|
|
|
_("Integrity Check Results"))
|
|
|
|
|
|
|
|
self.show()
|
|
|
|
|
|
|
|
def build_menu_names(self, obj):
|
|
|
|
return (_('Remove unused objects'), None)
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
class CheckOptions(Tool.ToolOptions):
|
|
|
|
"""
|
|
|
|
Defines options and provides handling interface.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self,name,person_id=None):
|
|
|
|
Tool.ToolOptions.__init__(self,name,person_id)
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
register_tool(
|
|
|
|
name = 'remove_unused',
|
|
|
|
category = Tool.TOOL_DBFIX,
|
|
|
|
tool_class = RemoveUnused,
|
|
|
|
options_class = CheckOptions,
|
2006-11-06 09:53:10 +05:30
|
|
|
modes = Tool.MODE_GUI,
|
2006-11-06 08:21:09 +05:30
|
|
|
translated_name = _("Remove unused objects"),
|
|
|
|
status = _("Stable"),
|
|
|
|
author_name = "Donald N. Allingham",
|
|
|
|
author_email = "don@gramps-project.org",
|
|
|
|
description=_("Removes unused objects from the database")
|
|
|
|
)
|