Use list.sort's keyword parameters
[joel/kofoto.git] / src / packages / kofoto / gkofoto / registerimageversionsdialog.py
1 import os
2 import re
3
4 import gtk
5
6 from kofoto.gkofoto.environment import env
7 from kofoto.shelf import \
8     ImageVersionDoesNotExistError, \
9     ImageVersionExistsError, \
10     ImageVersionType, \
11     NotAnImageFileError
12
13 class RegisterImageVersionsDialog:
14     def __init__(self, model):
15         self._image = None
16         self._model = model
17         self._widgets = gtk.glade.XML(
18             env.gladeFile, "registerImageVersionsDialog")
19         self._dialog = self._widgets.get_widget("registerImageVersionsDialog")
20         self._cancelButton = self._widgets.get_widget("cancelButton")
21         self._okButton = self._widgets.get_widget("okButton")
22         self._browseButton = self._widgets.get_widget("browseButton")
23         self._fileListView = self._widgets.get_widget("fileList")
24
25         self._cancelButton.connect("clicked", self._onCancel)
26         self._okButton.connect("clicked", self._onOk)
27         self._browseButton.connect("clicked", self._onBrowse)
28
29         renderer = gtk.CellRendererText()
30         column = gtk.TreeViewColumn("Image filename", renderer, text=0)
31         self._fileListView.append_column(column)
32         self._fileListView.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
33
34         self._fileListStore = gtk.ListStore(str)
35         self._fileListView.set_model(self._fileListStore)
36
37     def run(self, image):
38         self._image = image
39         files = set()
40         for imageversion in image.getImageVersions():
41             base, filename = os.path.split(imageversion.getLocation())
42             prefix, _ = os.path.splitext(filename)
43             for candidateFilename in os.listdir(base):
44                 if re.match("%s[^a-zA-Z0-9].*" % prefix, candidateFilename):
45                     candidatePath = os.path.join(base, candidateFilename)
46                     if os.path.isfile(candidatePath):
47                         try:
48                             env.shelf.getImageVersionByLocation(candidatePath)
49                         except ImageVersionDoesNotExistError:
50                             files.add(candidatePath)
51         self.__setFiles(files)
52         self._dialog.run()
53
54     def _onCancel(self, *unused):
55         self._dialog.destroy()
56
57     def _onOk(self, *unused):
58         selection = self._fileListView.get_selection()
59         _, selectedRows = selection.get_selected_rows()
60         changed = False
61         for path in selectedRows:
62             treeiter = self._fileListStore.get_iter(path)
63             location = \
64                 self._fileListStore.get_value(treeiter, 0).decode("utf-8")
65             try:
66                 imageVersion = env.shelf.createImageVersion(
67                     self._image, location, ImageVersionType.Other)
68                 changed = True
69             except NotAnImageFileError:
70                 dialog = gtk.MessageDialog(
71                     self._dialog,
72                     gtk.DIALOG_MODAL,
73                     gtk.MESSAGE_ERROR,
74                     gtk.BUTTONS_OK,
75                     "Not an image: %s" % location)
76                 dialog.run()
77                 dialog.destroy()
78             except ImageVersionExistsError:
79                 dialog = gtk.MessageDialog(
80                     self._dialog,
81                     gtk.DIALOG_MODAL,
82                     gtk.MESSAGE_ERROR,
83                     gtk.BUTTONS_OK,
84                     "Already registered: %s" % location)
85                 dialog.run()
86                 dialog.destroy()
87         if changed:
88             imageVersion.makePrimary()
89             self._model.reloadSelectedRows()
90         self._dialog.destroy()
91
92     def _onBrowse(self, *unused):
93         dialog = gtk.FileChooserDialog(
94             "Choose image version files",
95             self._dialog,
96             gtk.FILE_CHOOSER_ACTION_OPEN,
97             (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
98              gtk.STOCK_OK, gtk.RESPONSE_OK))
99         dialog.set_select_multiple(True)
100         if dialog.run() == gtk.RESPONSE_OK:
101             filenames = [x.decode("utf-8") for x in dialog.get_filenames()]
102             self.__setFiles(filenames)
103         dialog.destroy()
104
105     def __setFiles(self, filenames):
106         self._fileListStore.clear()
107         for filename in filenames:
108             self._fileListStore.append([filename])
109         self._fileListView.get_selection().select_all()