Use GNOME-style layout of OK and Cancel buttons.
[joel/kofoto.git] / src / gkofoto / gkofoto / registerimagesdialog.py
1 import gtk
2 import os
3 import time
4 from environment import env
5 from kofoto.shelf import ImageExistsError, NotAnImageError, makeValidTag
6 from kofoto.clientutils import walk_files
7
8 class RegisterImagesDialog(gtk.FileChooserDialog):
9     def __init__(self, albumToAddTo=None):
10         gtk.FileChooserDialog.__init__(
11             self,
12             title="Register images",
13             action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
14             buttons=(
15                 gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
16                 gtk.STOCK_OK, gtk.RESPONSE_OK))
17         self.__albumToAddTo = albumToAddTo
18         self.connect("response", self._response)
19
20     def _response(self, widget, responseId):
21         if responseId == gtk.RESPONSE_CANCEL:
22             return
23         widgets = gtk.glade.XML(env.gladeFile, "registrationProgressDialog")
24         registrationProgressDialog = widgets.get_widget(
25             "registrationProgressDialog")
26         newImagesCount = widgets.get_widget(
27             "newImagesCount")
28         alreadyRegisteredImagesCount = widgets.get_widget(
29             "alreadyRegisteredImagesCount")
30         nonImagesCount = widgets.get_widget(
31             "nonImagesCount")
32         filesInvestigatedCount = widgets.get_widget(
33             "filesInvestigatedCount")
34         okButton = widgets.get_widget("okButton")
35         okButton.set_sensitive(False)
36
37         registrationProgressDialog.show()
38
39         newImages = 0
40         alreadyRegisteredImages = 0
41         nonImages = 0
42         filesInvestigated = 0
43         images = []
44         registrationTimeString = unicode(time.strftime("%Y-%m-%d %H:%M:%S"))
45         for filepath in walk_files([self.get_filename()]):
46             try:
47                 try:
48                     filepath = filepath.decode("utf-8")
49                 except UnicodeDecodeError:
50                     filepath = filepath.decode("latin1")
51                 image = env.shelf.createImage(filepath)
52                 image.setAttribute(u"registered", registrationTimeString)
53                 images.append(image)
54                 newImages += 1
55                 newImagesCount.set_text(str(newImages))
56             except ImageExistsError:
57                 alreadyRegisteredImages += 1
58                 alreadyRegisteredImagesCount.set_text(str(alreadyRegisteredImages))
59             except NotAnImageError:
60                 nonImages += 1
61                 nonImagesCount.set_text(str(nonImages))
62             filesInvestigated += 1
63             filesInvestigatedCount.set_text(str(filesInvestigated))
64             while gtk.events_pending():
65                 gtk.main_iteration()
66         if self.__albumToAddTo:
67             children = list(self.__albumToAddTo.getChildren())
68             self.__albumToAddTo.setChildren(children + images)
69
70         okButton.set_sensitive(True)
71         registrationProgressDialog.run()
72         registrationProgressDialog.destroy()