Moved gkofoto library files into a subdirectory of their own again. It
[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.FileSelection(title="Choose directory")
38         dirDialog.file_list.set_sensitive(False)
39         dirDialog.fileop_del_file.set_sensitive(False)
40         dirDialog.fileop_ren_file.set_sensitive(False)
41         if dirDialog.run() == gtk.RESPONSE_OK:
42             model, iterator = dirDialog.dir_list.get_selection().get_selected()
43             directory = dirDialog.get_filename()
44             if iterator:
45                 directory = os.path.join(
46                     directory, model.get_value(iterator, 0))
47             self.directoryTextEntry.set_text(directory)
48         dirDialog.destroy()
49
50     def _onCancel(self, *unused):
51         self.dialog.destroy()
52
53     def _onGenerate(self, *unused):
54         for widget in [self.directoryTextEntry, self.browseButton,
55                        self.cancelButton, self.generateButton]:
56             widget.set_sensitive(False)
57         self._generate(self.directoryTextEntry.get_text())
58         self.dialog.destroy()
59
60     def _generate(self, directoryName):
61         # TODO: Rewrite this gross hack.
62
63         def outputParser(string):
64             m = re.match(
65                 r"Creating album (\S+) \((\d+) of (\d+)\)",
66                 string,
67                 re.UNICODE)
68             if m:
69                 progressBar.set_text(m.group(1).decode("latin1"))
70                 progressBar.set_fraction(
71                     (int(m.group(2)) - 1) / float(m.group(3)))
72                 while gtk.events_pending():
73                     gtk.main_iteration()
74
75         progressBar = self.widgets.get_widget("progressBar")
76
77         env.out = outputParser
78         env.verbose = True
79         env.thumbnailsizelimit = env.config.getcoordlist(
80             "album generation", "thumbnail_size_limit")[0]
81         env.defaultsizelimit = env.config.getcoordlist(
82             "album generation", "default_image_size_limit")[0]
83
84         imgsizesval = env.config.getcoordlist(
85             "album generation", "other_image_size_limits")
86         imgsizesset = Set(imgsizesval) # Get rid of duplicates.
87         defaultlimit = env.config.getcoordlist(
88             "album generation", "default_image_size_limit")[0]
89         imgsizesset.add(defaultlimit)
90         imgsizes = list(imgsizesset)
91         imgsizes.sort(lambda x, y: cmp(x[0] * x[1], y[0] * y[1]))
92         env.imagesizelimits = imgsizes
93
94         generator = kofoto.generate.Generator(u"woolly", env)
95         generator.generate(self.album, None, directoryName, "latin1")
96         progressBar.set_fraction(1)
97         while gtk.events_pending():
98             gtk.main_iteration()