Use list.sort's keyword parameters
[joel/kofoto.git] / src / packages / kofoto / gkofoto / registerimagesdialog.py
1 import gtk
2 import time
3 from kofoto.gkofoto.environment import env
4 from kofoto.shelf import \
5     ImageVersionExistsError, ImageVersionType, NotAnImageFileError
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, unused, responseId):
21         self.hide()
22         if responseId != gtk.RESPONSE_OK:
23             return
24         widgets = gtk.glade.XML(env.gladeFile, "registrationProgressDialog")
25         registrationProgressDialog = widgets.get_widget(
26             "registrationProgressDialog")
27         newImagesCount = widgets.get_widget(
28             "newImagesCount")
29         alreadyRegisteredImagesCount = widgets.get_widget(
30             "alreadyRegisteredImagesCount")
31         nonImagesCount = widgets.get_widget(
32             "nonImagesCount")
33         filesInvestigatedCount = widgets.get_widget(
34             "filesInvestigatedCount")
35         okButton = widgets.get_widget("okButton")
36         okButton.set_sensitive(False)
37
38         registrationProgressDialog.show()
39
40         newImages = 0
41         alreadyRegisteredImages = 0
42         nonImages = 0
43         filesInvestigated = 0
44         images = []
45         registrationTimeString = unicode(time.strftime("%Y-%m-%d %H:%M:%S"))
46         for filepath in walk_files([self.get_filename()]):
47             try:
48                 try:
49                     filepath = filepath.decode("utf-8")
50                 except UnicodeDecodeError:
51                     filepath = filepath.decode("latin1")
52                 image = env.shelf.createImage()
53                 env.shelf.createImageVersion(
54                     image, filepath, ImageVersionType.Original)
55                 image.setAttribute(u"registered", registrationTimeString)
56                 images.append(image)
57                 newImages += 1
58                 newImagesCount.set_text(str(newImages))
59             except ImageVersionExistsError:
60                 alreadyRegisteredImages += 1
61                 alreadyRegisteredImagesCount.set_text(str(alreadyRegisteredImages))
62                 env.shelf.deleteImage(image.getId())
63             except NotAnImageFileError:
64                 nonImages += 1
65                 nonImagesCount.set_text(str(nonImages))
66                 env.shelf.deleteImage(image.getId())
67             filesInvestigated += 1
68             filesInvestigatedCount.set_text(str(filesInvestigated))
69             while gtk.events_pending():
70                 gtk.main_iteration()
71         if self.__albumToAddTo:
72             children = list(self.__albumToAddTo.getChildren())
73             self.__albumToAddTo.setChildren(children + images)
74
75         okButton.set_sensitive(True)
76         registrationProgressDialog.run()
77         registrationProgressDialog.destroy()