1#
2# Gramps - a GTK+/GNOME based genealogy program
3#
4# Copyright (C) 2011       Tim G L Lyons
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#
20
21"""Tools/Debug/Populate sources and citations"""
22
23#------------------------------------------------------------------------
24#
25# Python modules
26#
27#------------------------------------------------------------------------
28import logging
29LOG = logging.getLogger(".citation")
30
31#-------------------------------------------------------------------------
32#
33# gnome/gtk
34#
35#-------------------------------------------------------------------------
36from gi.repository import Gtk
37
38#-------------------------------------------------------------------------
39#
40# gramps modules
41#
42#-------------------------------------------------------------------------
43from gramps.gen.const import COLON, GRAMPS_LOCALE as glocale
44_ = glocale.translation.gettext
45from gramps.gui.utils import ProgressMeter
46from gramps.gui.plug import tool
47from gramps.gui.dialog import OkDialog
48from gramps.gui.managedwindow import ManagedWindow
49from gramps.gen.lib import Citation, Source
50from gramps.gen.db import DbTxn
51
52class PopulateSources(tool.Tool, ManagedWindow):
53    """
54    Tool that populates the database with sources and citations.
55    """
56
57    def __init__(self, dbstate, user, options_class, name, callback=None):
58        uistate = user.uistate
59        self.label = 'Populate sources and citations tool'
60        ManagedWindow.__init__(self, uistate, [], self.__class__)
61        self.set_window(Gtk.Window(), Gtk.Label(), '')
62        tool.Tool.__init__(self, dbstate, options_class, name)
63
64        dialog = self.display()
65        response = dialog.run()
66        dialog.destroy()
67
68        if response == Gtk.ResponseType.ACCEPT:
69            self.on_ok_clicked()
70            OkDialog('Data generated',
71                     "The requested sources and citations were generated",
72                     parent=uistate.window)
73
74        self.close()
75
76    def display(self):
77        """
78        Constructs the GUI, consisting of a message, and fields to enter the
79        required number of sources and citations
80        """
81
82        # retrieve options
83        num_sources = self.options.handler.options_dict['sources']
84        num_citations = self.options.handler.options_dict['citations']
85
86        # GUI setup:
87        dialog = Gtk.Dialog("Populate sources and citations tool",
88                                self.uistate.window,
89                                Gtk.DialogFlags.MODAL|Gtk.DialogFlags.DESTROY_WITH_PARENT,
90                                (_('_Cancel'), Gtk.ResponseType.REJECT,
91                                 _('_OK'), Gtk.ResponseType.ACCEPT))
92        label = Gtk.Label("Enter a valid number of sources and citations."
93                          " This will create the requested number of sources,"
94                          " and for each source, will create the requested"
95                          " number of citations.")
96        label.set_line_wrap(True)
97
98        hbox1 = Gtk.Box()
99        label_sources = Gtk.Label(label="Number of sources" + COLON)
100        self.sources_entry = Gtk.Entry()
101        self.sources_entry.set_text("%d" % num_sources)
102        hbox1.pack_start(label_sources, False, True, 0)
103        hbox1.pack_start(self.sources_entry, True, True, 0)
104
105        hbox2 = Gtk.Box()
106        label_citations = Gtk.Label(label="Number of citations" + COLON)
107        self.citations_entry = Gtk.Entry()
108        self.citations_entry.set_text("%d" % num_citations)
109        hbox2.pack_start(label_citations, False, True, 0)
110        hbox2.pack_start(self.citations_entry, True, True, 0)
111
112        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
113        vbox.pack_start(label, True, True, 0)
114        vbox.pack_start(hbox1, False, True, 0)
115        vbox.pack_start(hbox2, False, True, 0)
116
117        dialog.vbox.set_spacing(10)
118        dialog.vbox.pack_start(vbox, True, True, 0)
119        dialog.show_all()
120        return dialog
121
122    def on_ok_clicked(self):
123        """
124        Method that is run when you click the OK button. The numbers of sources
125        and citations are retrieved from the entry box and used to govern the
126        amount of data generated
127        """
128
129        num_sources_text = self.sources_entry.get_text()
130        try:
131            num_sources = int(num_sources_text)
132        except:
133            return
134        num_citations_text = self.citations_entry.get_text()
135        num_citations = int(num_citations_text)
136
137        self.progress = ProgressMeter(
138            'Generating data', '', parent=self.uistate.window)
139        self.progress.set_pass('Generating data',
140                               num_sources*num_citations)
141        LOG.debug("sources %04d citations %04d" % (num_sources,
142                                                     num_citations))
143
144        source = Source()
145        citation = Citation()
146
147        self.db.disable_signals()
148        with DbTxn('Populate sources and citations', self.db) as trans:
149            for i in range(num_sources):
150                source.gramps_id = None
151                source.handle = None
152                source.title = "Source %04d" % (i + 1)
153                source_handle = self.db.add_source(source, trans)
154
155                for j in range(num_citations):
156                    citation.gramps_id = None
157                    citation.handle = None
158                    citation.source_handle = source_handle
159                    citation.page = "Page %04d" % (j + 1)
160                    self.db.add_citation(citation, trans)
161                    self.progress.step()
162            LOG.debug("sources and citations added")
163        self.db.enable_signals()
164        self.db.request_rebuild()
165        self.progress.close()
166
167        self.options.handler.options_dict['sources'] = num_sources
168        self.options.handler.options_dict['citations'] = num_citations
169        # Save options
170        self.options.handler.save_options()
171
172class PopulateSourcesOptions(tool.ToolOptions):
173    """
174    Defines options and provides handling interface.
175    """
176
177    def __init__(self, name, person_id=None):
178        tool.ToolOptions.__init__(self, name, person_id)
179
180        # Options specific for this report
181        self.options_dict = {
182            'sources'   : 2,
183            'citations' : 2,
184        }
185        self.options_help = {
186            'sources'   : ("=num",
187                           "Number of sources to generate",
188                           "Integer number"),
189            'citations' : ("=num",
190                           "Number of citations to generate for each source",
191                           "Integer number")
192            }
193