/gtk/eog-manage-raws

To get this branch, use:
bzr branch http://bzr.ed.am/gtk/eog-manage-raws
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import eog, gtk, os, re

class HelloWorldPlugin(eog.Plugin):

    raw_file_extensions = [ 'cr2', '3fr', 'ari', 'arw', 'srf', 'sr2', 'bay',
        'crw', 'cr2', 'cap', 'iiq', 'eip', 'dcs', 'dcr', 'drf', 'k25', 'kdc',
        'dng', 'erf', 'fff', 'mef', 'mos', 'mrw', 'nef', 'nrw', 'orf', 'ptx',
        'pef', 'pxn', 'r3d', 'raf', 'raw', 'rw2', 'rwl', 'rwz', 'x3f' ]

    def __init__( self ):
        eog.Plugin.__init__( self )

    def activate( self, window ):
        self._window = window

        # insert menu item
        manager = window.get_ui_manager()
        self._action_group = gtk.ActionGroup( 'EogRawToolsPluginActions' )
        self._action_group.add_actions( [ ( 'EogPluginRunKeepRaw', None,
            _( 'Keep Raw Image' ), 'K',
            _( "Move the accompanying raw file to a 'raw' subdirectory" ),
            self.on_keep_raw_image ) ] )
        self._action_group.add_actions( [ ( 'EogPluginRunDeleteRaw', None,
            _( 'Delete Unkept Raw Images' ), None,
            _( "Delete all raw files in the current image's directory (the unkept ones)" ),
            self.on_delete_raw_images ) ] )
        manager.insert_action_group( self._action_group, -1 )
        self._ui_id = manager.add_ui_from_string( """
            <ui>
                <menubar name="MainMenu">
                    <menu name="ToolsMenu" action="Tools">
                        <separator />
                        <menuitem name="EogPluginRunKeepRaw"
                            action="EogPluginRunKeepRaw" />
                        <menuitem name="EogPluginRunDeleteRaw"
                            action="EogPluginRunDeleteRaw" />
                        <separator />
                    </menu>
                </menubar>
                <popup name="ViewPopup">
                    <separator />
                    <menuitem action="EogPluginRunKeepRaw" />
                    <separator />
                </popup>
            </ui>
        """ )

        # insert status bar
        self._statusbar = gtk.Statusbar()
        self._statusbar.set_has_resize_grip( False )
        self._statusbar.set_size_request( 85, 10 )
        statusbar = window.get_statusbar()
        statusbar.pack_end( self._statusbar, False, False, 0 );
        self._statusbar.show()

        # connect to selection change
        thumb_view = window.get_thumb_view()
        self._on_selection_change_id = thumb_view.connect_after( \
            'selection_changed', self.on_selection_change, window )

        # init ui state
        self.update_status( window )

    def deactivate( self, window ):

        # disconnect handlers
        thumb_view = window.get_thumb_view()
        thumb_view.disconnect( self._on_selection_change_id )

        # remove menu items
        manager = window.get_ui_manager()
        manager.remove_ui( self._ui_id )
        manager.remove_action_group( self._action_group )
        manager.ensure_update()

        # remove status bar entry
        statusbar = window.get_statusbar()
        statusbar.remove( self._statusbar )

    def on_selection_change( self, view, data ):
        self.update_status( data )

    def get_raw_filename_from_image( self, image ):
        fname = image.get_file().get_path()

        # get the base fileanme (with no extension), read for searching
        base_fname = image.get_file().get_path()
        pos = base_fname.rfind( '.' )
        if( pos != -1 ):

            # check if the extension of the current image makes it a raw file
            for ext in self.raw_file_extensions + \
                map( lambda x: x.upper(), self.raw_file_extensions ):
                if( base_fname[ pos : ] == ext ):
                    return False;

            # remove extension from base filename
            base_fname = base_fname[ 0 : pos ]
        
        # check for stupidity
        if( base_fname[ -1 : None ] == '/' ):
            return False

        # loop through valid raw file extensions, uppercase and lowercase
        for ext in self.raw_file_extensions + \
            map( lambda x: x.upper(), self.raw_file_extensions ):

            # if the raw file exists, we found it
            if( os.path.isfile( base_fname + '.' + ext ) ):
                return os.path.basename( base_fname + '.' + ext );

        # path the raw images will be moved to
        raw_path = os.path.dirname( fname ) + '/raw';

        # loop through valid raw file extensions, uppercase and lowercase
        for ext in self.raw_file_extensions + \
            map( lambda x: x.upper(), self.raw_file_extensions ):

            # if the raw file exists, we found it
            if( os.path.isfile( os.path.dirname( fname ) + '/raw/' + \
                os.path.basename( base_fname + '.' + ext ) ) ):
                return os.path.basename( base_fname + '.' + ext );

        # not found
        return False;

    def update_status( self, window ):
        thumb_view = window.get_thumb_view()

        # do we have just the one selected image? we can't handle multiple
        # images because EogThumbView.get_selected_images() doesn't work.
        self._action_group.set_sensitive( \
            thumb_view.get_n_selected() == 1 )

        # update the status bar
        if( thumb_view.get_n_selected() > 0 ):
            image = thumb_view.get_first_selected_image()
            raw_fname = self.get_raw_filename_from_image( image )
            path = os.path.dirname( image.get_file().get_path() )
            self._statusbar.pop( 0 )
            if( raw_fname is False ):
                self._statusbar.push( 0, _( 'raw: -' ) )
            elif( os.path.isfile( path + '/' + raw_fname ) ):
                self._statusbar.push( 0, _( 'raw: delete' ) )
            else:
                self._statusbar.push( 0, _( 'raw: keep' ) )

    def on_keep_raw_image( self, action ):

        # do we have just the one selected image? we can't handle multiple
        # images because EogThumbView.get_selected_images() doesn't work.
        thumb_view = self._window.get_thumb_view()
        if( thumb_view.get_n_selected() == 1 ):

            # get the image
            image = thumb_view.get_first_selected_image()
            if( image != None ):
                self.keep_raw_image( image )

            # update ui
            self.update_status( self._window )

    def keep_raw_image( self, image ):

        raw_fname = self.get_raw_filename_from_image( image )
        if( raw_fname is False ):
            dialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL,
                gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE,
                _( 'Raw file not found!' ) )
            dialog.format_secondary_text( _( "This image doesn't appear to " +
                "have an accompanying raw file." ) )
            dialog.run()
            dialog.destroy()
            return

        # does raw file exist?
        path = os.path.dirname( image.get_file().get_path() )
        if( os.path.isfile( path + '/' + raw_fname ) ):

            # create the raw directory, if it doesn't exist
            if( not os.path.isdir( path + '/raw' ) ):
                os.mkdir( path + '/raw' );

            # move the raw file in to the raw directory
            os.rename( path + '/' + raw_fname, path + '/raw/' + raw_fname )

    def on_delete_raw_images( self, action ):

        # get path to first selected image, if there is one
        thumb_view = self._window.get_thumb_view()
        if( thumb_view.get_n_selected() == 1 ):
            image = thumb_view.get_first_selected_image()
            path = os.path.dirname( image.get_file().get_path() )

            # build a regex that will match raw filenames
            regex = re.compile(
                '.*\.(' + '|'.join( self.raw_file_extensions ) + ')', re.I )

            # itterate through files in the dir
            files = set()
            for file in os.listdir( path ):
                if( regex.match( file ) ):
                    files.add( file )

            # do we have any files to delete?
            if( len( files ) > 0 ):

                # ask the user if they are sure
                dialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL,
                    gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO,
                    _( 'Delete Raw Files?' ) )
                if( len( files ) == 1 ):
                    subject = _( ' remaining raw file' )
                else:
                    subject = _( ' remaining raw files' )
                dialog.format_secondary_text(
                    _( 'You are about to delete the ' ) +
                    str( len( files ) ) + subject +
                    _( " that you haven't specifically asked to keep from " +
                        "the directory that the current image is in.\n\n" +
                        "Do you want to continue?" ) )
                result = dialog.run()
                dialog.destroy()
                if( result != gtk.RESPONSE_YES ):
                    return

                # delete the files
                for file in files:
                    os.unlink( path + '/' + file )

                self.update_status( self._window )
        
            # else, tell the user there aren't any raw files!
            else:
                dialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL,
                    gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE,
                    _( 'No raw files to delete!' ) )
                dialog.format_secondary_text(
                    _( 'There are no raw files to delete!' ) )
                dialog.run()
                dialog.destroy()