Use list.sort's keyword parameters
[joel/kofoto.git] / src / packages / kofoto / gkofoto / duplicateandopenimagedialog.py
1 import gtk
2 import os
3 import shutil
4
5 from kofoto.gkofoto.environment import env
6
7 class DuplicateAndOpenImageDialog:
8     def __init__(self):
9         self._imageversion = None
10         self._widgets = gtk.glade.XML(
11             env.gladeFile, "duplicateAndOpenImageDialog")
12         self._dialog = self._widgets.get_widget("duplicateAndOpenImageDialog")
13         self._cancelButton = self._widgets.get_widget("cancelButton")
14         self._okButton = self._widgets.get_widget("okButton")
15         self._browseButton = self._widgets.get_widget("browseButton")
16         self._fileEntry = self._widgets.get_widget("fileEntry")
17
18         self._cancelButton.connect("clicked", self._onCancel)
19         self._okButton.connect("clicked", self._onOk)
20         self._browseButton.connect("clicked", self._onBrowse)
21
22     def run(self, imageversion):
23         self._imageversion = imageversion
24         location = imageversion.getLocation()
25         prefix, suffix = os.path.splitext(location)
26         duplicateLocation = "%s+fix%s" % (prefix, suffix)
27         self.__setFile(duplicateLocation)
28         self._fileEntry.select_region(len(prefix) + 1, len(prefix + "fix") + 1)
29         self._dialog.run()
30
31     def _onCancel(self, *unused):
32         self._dialog.destroy()
33
34     def _onOk(self, *unused):
35         duplicateLocation = self._fileEntry.get_text()
36         if os.path.exists(duplicateLocation):
37             dialog = gtk.MessageDialog(
38                 self._dialog,
39                 gtk.DIALOG_MODAL,
40                 gtk.MESSAGE_ERROR,
41                 gtk.BUTTONS_OK,
42                 "File already exists: %s" % duplicateLocation)
43             dialog.run()
44             dialog.destroy()
45         else:
46             shutil.copyfile(
47                 self._imageversion.getLocation(),
48                 duplicateLocation)
49             command = env.openCommand % {"locations": duplicateLocation}
50             result = os.system(command.encode(env.localeEncoding) + " &")
51             if result != 0:
52                 dialog = gtk.MessageDialog(
53                     self._dialog,
54                     gtk.DIALOG_MODAL,
55                     gtk.MESSAGE_ERROR,
56                     gtk.BUTTONS_OK,
57                     "Failed to execute command: \"%s\"" % command)
58                 dialog.run()
59                 dialog.destroy()
60         self._dialog.destroy()
61
62     def _onBrowse(self, *unused):
63         dialog = gtk.FileChooserDialog(
64             "Choose name of duplicate image",
65             self._dialog,
66             gtk.FILE_CHOOSER_ACTION_SAVE,
67             (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
68              gtk.STOCK_OK, gtk.RESPONSE_OK))
69         if dialog.run() == gtk.RESPONSE_OK:
70             self.__setFile(dialog.get_filename())
71         dialog.destroy()
72
73     def __setFile(self, location):
74         self._fileEntry.set_text(location)
75         self._fileEntry.set_position(-1)  # Last.