/gtk/eog-manage-raws

To get this branch, use:
bzr branch http://bzr.ed.am/gtk/eog-manage-raws

« back to all changes in this revision

Viewing changes to src/raw-tools.py

  • Committer: edam
  • Date: 2011-09-22 21:51:14 UTC
  • Revision ID: edam@waxworlds.org-20110922215114-mdmf2k6vd16bibh3
- added unkeep command
- delete raw iamges now moves them to trash

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
# version 0.1
24
24
 
25
 
from gi.repository import GObject, Gtk, Eog
26
 
import os, re, gio
27
 
 
28
 
class RawToolsPlugin( GObject.Object, Eog.WindowActivatable ):
29
 
 
30
 
    # Override EogWindowActivatable's window property
31
 
    window = GObject.property( type = Eog.Window )
32
 
 
33
 
    # list of file extension to recognise as RAW files
 
25
 
 
26
import eog, gtk, os, re, gio
 
27
 
 
28
class RawToolsPlugin(eog.Plugin):
 
29
 
34
30
    raw_file_extensions = [ 'cr2', '3fr', 'ari', 'arw', 'srf', 'sr2', 'bay',
35
31
        'crw', 'cr2', 'cap', 'iiq', 'eip', 'dcs', 'dcr', 'drf', 'k25', 'kdc',
36
32
        'dng', 'erf', 'fff', 'mef', 'mos', 'mrw', 'nef', 'nrw', 'orf', 'ptx',
37
33
        'pef', 'pxn', 'r3d', 'raf', 'raw', 'rw2', 'rwl', 'rwz', 'x3f' ]
38
34
 
39
35
    def __init__( self ):
40
 
        GObject.Object.__init__( self )
 
36
        eog.Plugin.__init__( self )
41
37
 
42
 
    def do_activate( self ):
 
38
    def activate( self, window ):
 
39
        self._window = window
43
40
 
44
41
        # insert menu item
45
 
        ui_manager = self.window.get_ui_manager()
46
 
        self.action_group = Gtk.ActionGroup( 'EogRawToolsPluginActions' )
47
 
        self.action_group.add_actions( [ ( 'EogPluginRunKeepRaw', None,
 
42
        manager = window.get_ui_manager()
 
43
        self._action_group = gtk.ActionGroup( 'EogRawToolsPluginActions' )
 
44
        self._action_group.add_actions( [ ( 'EogPluginRunKeepRaw', None,
48
45
            _( 'Keep Raw Image' ), 'K',
49
46
            _( "Move the accompanying raw file to a 'raw' subdirectory" ),
50
 
            self.do_keep_raw_image ) ], self.window )
51
 
        self.action_group.add_actions( [ ( 'EogPluginRunUnkeepRaw', None,
 
47
            self.on_keep_raw_image ) ] )
 
48
        self._action_group.add_actions( [ ( 'EogPluginRunUnkeepRaw', None,
52
49
            _( 'Unkeep Raw Image' ), 'U',
53
50
            _( "Move the accompanying raw file back to the image's directory" ),
54
 
            self.do_unkeep_raw_image ) ], self.window )
55
 
        self.action_group.add_actions( [ ( 'EogPluginRunDeleteRaw', None,
 
51
            self.on_unkeep_raw_image ) ] )
 
52
        self._action_group.add_actions( [ ( 'EogPluginRunDeleteRaw', None,
56
53
            _( 'Delete Unkept Raw Images' ), None,
57
54
            _( "Delete all raw files in the current image's directory (the unkept ones)" ),
58
 
            self.do_delete_raw_images ) ], self.window )
59
 
        ui_manager.insert_action_group( self.action_group, -1 )
60
 
        self.ui_id = ui_manager.add_ui_from_string( """
 
55
            self.on_delete_raw_images ) ] )
 
56
        manager.insert_action_group( self._action_group, -1 )
 
57
        self._ui_id = manager.add_ui_from_string( """
61
58
            <ui>
62
59
                <menubar name="MainMenu">
63
60
                    <menu name="ToolsMenu" action="Tools">
80
77
        """ )
81
78
 
82
79
        # insert status bar
83
 
        self.statusbar = Gtk.Statusbar()
84
 
        #self.statusbar.set_size_request( 100, 10 )
85
 
        statusbar = self.window.get_statusbar()
86
 
        statusbar.pack_end( self.statusbar, False, False, 0 );
87
 
        self.statusbar.show()
 
80
        self._statusbar = gtk.Statusbar()
 
81
        self._statusbar.set_has_resize_grip( False )
 
82
        self._statusbar.set_size_request( 80, 10 )
 
83
        statusbar = window.get_statusbar()
 
84
        statusbar.pack_end( self._statusbar, False, False, 0 );
 
85
        self._statusbar.show()
88
86
 
89
87
        # connect to selection change
90
 
        thumb_view = self.window.get_thumb_view()
91
 
        self.on_selection_change_id = thumb_view.connect_after( \
92
 
            'selection_changed', self.on_selection_change, self.window )
 
88
        thumb_view = window.get_thumb_view()
 
89
        self._on_selection_change_id = thumb_view.connect_after( \
 
90
            'selection_changed', self.on_selection_change, window )
93
91
 
94
92
        # init ui state
95
 
        self.update_status( self.window )
96
 
 
97
 
    def do_deactivate( self ):
 
93
        self.update_status( window )
 
94
 
 
95
    def deactivate( self, window ):
 
96
 
 
97
        # disconnect handlers
 
98
        thumb_view = window.get_thumb_view()
 
99
        thumb_view.disconnect( self._on_selection_change_id )
98
100
 
99
101
        # remove menu items
100
 
        ui_manager = self.window.get_ui_manager()
101
 
        ui_manager.remove_ui( self.ui_id )
102
 
        self.ui_id = 0
103
 
        ui_manager.remove_action_group( self.action_group )
104
 
        self.action_group = None
105
 
        ui_manager.ensure_update()
106
 
 
107
 
        # disconnect handlers
108
 
        thumb_view = self.window.get_thumb_view()
109
 
        thumb_view.disconnect( self.on_selection_change_id )
 
102
        manager = window.get_ui_manager()
 
103
        manager.remove_ui( self._ui_id )
 
104
        manager.remove_action_group( self._action_group )
 
105
        manager.ensure_update()
110
106
 
111
107
        # remove status bar entry
112
 
        statusbar =  self.window.get_statusbar()
113
 
        Gtk.Container.remove( statusbar, self.statusbar )
 
108
        statusbar = window.get_statusbar()
 
109
        statusbar.remove( self._statusbar )
114
110
 
115
111
    def on_selection_change( self, view, data ):
116
112
        self.update_status( data )
164
160
 
165
161
        # do we have just the one selected image? we can't handle multiple
166
162
        # images because EogThumbView.get_selected_images() doesn't work.
167
 
        self.action_group.set_sensitive( \
 
163
        self._action_group.set_sensitive( \
168
164
            thumb_view.get_n_selected() == 1 )
169
165
 
170
166
        # update the status bar
172
168
            image = thumb_view.get_first_selected_image()
173
169
            raw_fname = self.get_raw_filename_from_image( image )
174
170
            path = os.path.dirname( image.get_file().get_path() )
175
 
            self.statusbar.pop( 0 )
 
171
            self._statusbar.pop( 0 )
176
172
            if( raw_fname is False ):
177
 
                self.statusbar.push( 0, "  " + _( 'raw: -' ) )
 
173
                self._statusbar.push( 0, _( 'raw: -' ) )
178
174
            elif( os.path.isfile( path + '/' + raw_fname ) ):
179
 
                self.statusbar.push( 0, "  " + _( 'raw: unkept' ) )
 
175
                self._statusbar.push( 0, _( 'raw: unkept' ) )
180
176
            else:
181
 
                self.statusbar.push( 0, "  " + _( 'raw: keep' ) )
 
177
                self._statusbar.push( 0, _( 'raw: keep' ) )
182
178
 
183
 
    def do_keep_raw_image( self, action, window ):
 
179
    def on_keep_raw_image( self, action ):
184
180
 
185
181
        # do we have just the one selected image? we can't handle multiple
186
182
        # images because EogThumbView.get_selected_images() doesn't work.
187
 
        thumb_view = window.get_thumb_view()
 
183
        thumb_view = self._window.get_thumb_view()
188
184
        if( thumb_view.get_n_selected() == 1 ):
189
185
 
190
186
            # get the image
193
189
                self.keep_raw_image( image )
194
190
 
195
191
            # update ui
196
 
            self.update_status( window )
 
192
            self.update_status( self._window )
197
193
 
198
 
    def do_unkeep_raw_image( self, action, window ):
 
194
    def on_unkeep_raw_image( self, action ):
199
195
 
200
196
        # do we have just the one selected image? we can't handle multiple
201
197
        # images because EogThumbView.get_selected_images() doesn't work.
202
 
        thumb_view = window.get_thumb_view()
 
198
        thumb_view = self._window.get_thumb_view()
203
199
        if( thumb_view.get_n_selected() == 1 ):
204
200
 
205
201
            # get the image
208
204
                self.unkeep_raw_image( image )
209
205
 
210
206
            # update ui
211
 
            self.update_status( window )
 
207
            self.update_status( self._window )
212
208
 
213
209
    def keep_raw_image( self, image ):
214
210
 
215
211
        raw_fname = self.get_raw_filename_from_image( image )
216
212
        if( raw_fname is False ):
217
 
            dialog = Gtk.MessageDialog( None, Gtk.DialogFlags.MODAL,
218
 
                Gtk.MessageType.INFO, Gtk.ButtonsType.CLOSE,
 
213
            dialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL,
 
214
                gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE,
219
215
                _( 'Raw file not found!' ) )
220
216
            dialog.format_secondary_text( _( "This image doesn't appear to " +
221
217
                "have an accompanying raw file." ) )
238
234
 
239
235
        raw_fname = self.get_raw_filename_from_image( image )
240
236
        if( raw_fname is False ):
241
 
            dialog = Gtk.MessageDialog( None, Gtk.DialogFlags.MODAL,
242
 
                Gtk.MessageType.INFO, Gtk.ButtonsType.CLOSE,
 
237
            dialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL,
 
238
                gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE,
243
239
                _( 'Raw file not found!' ) )
244
240
            dialog.format_secondary_text( _( "This image doesn't appear to " +
245
241
                "have an accompanying raw file." ) )
258
254
            if( len( os.listdir( path + '/raw' ) ) == 0 ):
259
255
                os.rmdir( path + '/raw' )
260
256
 
261
 
    def do_delete_raw_images( self, action, window ):
 
257
    def on_delete_raw_images( self, action ):
262
258
 
263
259
        # get path to first selected image, if there is one
264
 
        thumb_view = window.get_thumb_view()
 
260
        thumb_view = self._window.get_thumb_view()
265
261
        if( thumb_view.get_n_selected() == 1 ):
266
262
            image = thumb_view.get_first_selected_image()
267
263
            path = os.path.dirname( image.get_file().get_path() )
280
276
            if( len( files ) > 0 ):
281
277
 
282
278
                # ask the user if they are sure
283
 
                dialog = Gtk.MessageDialog( None, Gtk.DialogFlags.MODAL,
284
 
                    Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO,
 
279
                dialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL,
 
280
                    gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO,
285
281
                    _( 'Delete Raw Files?' ) )
286
282
                if( len( files ) == 1 ):
287
283
                    subject = _( ' remaining raw file' )
295
291
                        "Do you want to continue?" ) )
296
292
                result = dialog.run()
297
293
                dialog.destroy()
298
 
                if( result != Gtk.ResponseType.YES ):
 
294
                if( result != gtk.RESPONSE_YES ):
299
295
                    return
300
296
 
301
297
                # delete the files
303
299
                    gio.file_parse_name( path + '/' + file ).trash()
304
300
                    #os.unlink( path + '/' + file )
305
301
 
306
 
                self.update_status( window )
 
302
                self.update_status( self._window )
307
303
        
308
304
            # else, tell the user there aren't any raw files!
309
305
            else:
310
 
                dialog = Gtk.MessageDialog( None, Gtk.DialogFlags.MODAL,
311
 
                    Gtk.MessageType.INFO, Gtk.ButtonsType.CLOSE,
 
306
                dialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL,
 
307
                    gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE,
312
308
                    _( 'No raw files to delete!' ) )
313
309
                dialog.format_secondary_text(
314
310
                    _( 'There are no raw files to delete!' ) )