6044f2507145b294ee21f9f0e679ccfebc37824a
[joel/kofoto.git] / src / gkofoto / gkofoto / generatehtmldialog.py
1 import gtk
2 import os
3 import re
4 from sets import Set
5 from environment import env
6 import kofoto.generate
7
8 class GenerateHTMLDialog:
9     def __init__(self, album):
10         self.album = album
11         self.widgets = gtk.glade.XML(
12             env.gladeFile, "generateHtmlDialog")
13         self.dialog = self.widgets.get_widget("generateHtmlDialog")
14         self.browseButton = self.widgets.get_widget("browseButton")
15         self.cancelButton = self.widgets.get_widget("cancelButton")
16         self.directoryTextEntry = self.widgets.get_widget("directoryTextEntry")
17         self.generateButton = self.widgets.get_widget("generateButton")
18
19         self.browseButton.connect("clicked", self._onBrowse)
20         self.cancelButton.connect("clicked", self._onCancel)
21         self.generateButton.connect("clicked", self._onGenerate)
22
23         self.directoryTextEntry.connect(
24             "changed", self._onDirectoryTextEntryModified)
25
26         self.generateButton.set_sensitive(False)
27
28     def run(self):
29         self.dialog.show()
30
31     def _onDirectoryTextEntryModified(self, *unused):
32         self.generateButton.set_sensitive(
33             os.path.isdir(self.directoryTextEntry.get_text()))
34
35     def _onBrowse(self, *unused):
36         directorySelectedInDirList = False
37         dirDialog = gtk.FileChooserDialog(
38             title="Choose directory",
39             action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
40             buttons=(
41                 gtk.STOCK_OK, gtk.RESPONSE_OK,
42                 gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
43         if dirDialog.run() == gtk.RESPONSE_OK:
44             self.directoryTextEntry.set_text(dirDialog.get_filename())
45         dirDialog.destroy()
46
47     def _onCancel(self, *unused):
48         self.dialog.destroy()
49
50     def _onGenerate(self, *unused):
51         for widget in [self.directoryTextEntry, self.browseButton,
52                        self.cancelButton, self.generateButton]:
53             widget.set_sensitive(False)
54         self._generate(self.directoryTextEntry.get_text())
55         self.dialog.destroy()
56
57     def _generate(self, directoryName):
58         # TODO: Rewrite this gross hack.
59
60         def outputParser(string):
61             m = re.match(
62                 r"Creating album (\S+) \((\d+) of (\d+)\)",
63                 string,
64                 re.UNICODE)
65             if m:
66                 progressBar.set_text(m.group(1).decode("latin1"))
67                 progressBar.set_fraction(
68                     (int(m.group(2)) - 1) / float(m.group(3)))
69                 while gtk.events_pending():
70                     gtk.main_iteration()
71
72         progressBar = self.widgets.get_widget("progressBar")
73
74         env.out = outputParser
75         env.verbose = True
76         env.thumbnailsizelimit = env.config.getcoordlist(
77             "album generation", "thumbnail_size_limit")[0]
78         env.defaultsizelimit = env.config.getcoordlist(
79             "album generation", "default_image_size_limit")[0]
80
81         imgsizesval = env.config.getcoordlist(
82             "album generation", "other_image_size_limits")
83         imgsizesset = Set(imgsizesval) # Get rid of duplicates.
84         defaultlimit = env.config.getcoordlist(
85             "album generation", "default_image_size_limit")[0]
86         imgsizesset.add(defaultlimit)
87         imgsizes = list(imgsizesset)
88         imgsizes.sort(lambda x, y: cmp(x[0] * x[1], y[0] * y[1]))
89         env.imagesizelimits = imgsizes
90
91         generator = kofoto.generate.Generator(u"woolly", env)
92         generator.generate(self.album, None, directoryName, "latin1")
93         progressBar.set_fraction(1)
94         while gtk.events_pending():
95             gtk.main_iteration()