/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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# manage-raws.py
#
# Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
#
# This file is part of Manage Raws (hereafter referred to as "this
# program").  See http://ed.am/dev/gtk/eog-manage-raws for more
# information.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


# version 0.1

from gi.repository import GObject, Gtk, Eog, Gio
import os, re

class ManageRawsPlugin( GObject.Object, Eog.WindowActivatable ):

    # Override EogWindowActivatable's window property
    window = GObject.property( type = Eog.Window )

    # list of file extension to recognise as RAW files
    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 ):
        GObject.Object.__init__( self )

    def do_activate( self ):

        # insert menu item
        ui_manager = self.window.get_ui_manager()
        self.action_group = Gtk.ActionGroup( 'EogManageRawsPluginActions' )
        self.action_group.add_actions( [ ( 'EogPluginRunKeepRaw', None,
            _( 'Keep Raw File' ), 'K',
            _( "Move the accompanying raw file to a 'raw' subdirectory" ),
            self.do_keep_raw_file ) ], self.window )
        self.action_group.add_actions( [ ( 'EogPluginRunUnkeepRaw', None,
            _( 'Unkeep Raw File' ), 'U',
            _( "Move the accompanying raw file back to the image's directory" ),
            self.do_unkeep_raw_file ) ], self.window )
        self.action_group.add_actions( [ ( 'EogPluginRunDeleteRaw', None,
            _( 'Delete Unkept Raw Files' ), None,
            _( "Delete all raw files in the current image's directory (the unkept ones)" ),
            self.do_delete_raw_files ) ], self.window )
        ui_manager.insert_action_group( self.action_group, -1 )
        self.ui_id = ui_manager.add_ui_from_string( """
            <ui>
                <menubar name="MainMenu">
                    <menu name="ToolsMenu" action="Tools">
                        <separator />
                        <menuitem name="EogPluginRunKeepRaw"
                            action="EogPluginRunKeepRaw" />
                        <menuitem name="EogPluginRunUnkeepRaw"
                            action="EogPluginRunUnkeepRaw" />
                        <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_size_request( 100, 10 )
        statusbar = self.window.get_statusbar()
        statusbar.pack_end( self.statusbar, False, False, 0 );
        self.statusbar.show()

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

        # init ui state
        self.update_status( self.window )

    def do_deactivate( self ):

        # remove menu items
        ui_manager = self.window.get_ui_manager()
        ui_manager.remove_ui( self.ui_id )
        self.ui_id = 0
        ui_manager.remove_action_group( self.action_group )
        self.action_group = None
        ui_manager.ensure_update()

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

        # remove status bar entry
        statusbar =  self.window.get_statusbar()
        Gtk.Container.remove( statusbar, 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 files 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: unkept' ) )
            else:
                self.statusbar.push( 0, "  " + _( 'raw: keep' ) )

    def do_keep_raw_file( self, action, window ):

        # do we have just the one selected image? we can't handle multiple
        # images because EogThumbView.get_selected_images() doesn't work.
        thumb_view = 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_file( image )

            # update ui
            self.update_status( window )

    def do_unkeep_raw_file( self, action, window ):

        # do we have just the one selected image? we can't handle multiple
        # images because EogThumbView.get_selected_images() doesn't work.
        thumb_view = 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.unkeep_raw_file( image )

            # update ui
            self.update_status( window )

    def keep_raw_file( self, image ):

        raw_fname = self.get_raw_filename_from_image( image )
        if( raw_fname is False ):
            dialog = Gtk.MessageDialog( None, Gtk.DialogFlags.MODAL,
                Gtk.MessageType.INFO, Gtk.ButtonsType.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 unkeep_raw_file( self, image ):

        raw_fname = self.get_raw_filename_from_image( image )
        if( raw_fname is False ):
            dialog = Gtk.MessageDialog( None, Gtk.DialogFlags.MODAL,
                Gtk.MessageType.INFO, Gtk.ButtonsType.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/' + raw_fname ) ):

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

            # if the raw directory is empty, remove it
            if( len( os.listdir( path + '/raw' ) ) == 0 ):
                os.rmdir( path + '/raw' )

    def do_delete_raw_files( self, action, window ):

        # get path to first selected image, if there is one
        thumb_view = 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.DialogFlags.MODAL,
                    Gtk.MessageType.QUESTION, Gtk.ButtonsType.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.ResponseType.YES ):
                    return

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

                self.update_status( window )
        
            # else, tell the user there aren't any raw files!
            else:
                dialog = Gtk.MessageDialog( None, Gtk.DialogFlags.MODAL,
                    Gtk.MessageType.INFO, Gtk.ButtonsType.CLOSE,
                    _( 'No raw files to delete!' ) )
                dialog.format_secondary_text(
                    _( 'There are no raw files to delete!' ) )
                dialog.run()
                dialog.destroy()