/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: 2012-01-21 23:25:05 UTC
  • Revision ID: edam@waxworlds.org-20120121232505-7ayo5dvod8o0rd17
switch to gir Gio

Show diffs side-by-side

added added

removed removed

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