/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/manage-raws.py

  • Committer: edam
  • Date: 2012-01-22 01:26:39 UTC
  • Revision ID: edam@waxworlds.org-20120122012639-hluj34f6th75s040
Tags: 0.1
refer to raw files as files, not images

Show diffs side-by-side

added added

removed removed

1
 
# raw-tools.py
 
1
# manage-raws.py
2
2
#
3
3
# Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
4
4
#
5
 
# This file is part of Raw Tools (hereafter referred to as "this program").
6
 
# See http://www.waxworlds.org/edam/software/gtk/eog-raw-tools for more
 
5
# This file is part of Manage Raws (hereafter referred to as "this
 
6
# program").  See http://ed.am/dev/gtk/eog-manage-raws for more
7
7
# information.
8
8
#
9
9
# This program is free software: you can redistribute it and/or modify
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 ManageRawsPlugin( 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
 
            _( 'Keep Raw Image' ), 'K',
 
45
        ui_manager = self.window.get_ui_manager()
 
46
        self.action_group = Gtk.ActionGroup( 'EogManageRawsPluginActions' )
 
47
        self.action_group.add_actions( [ ( 'EogPluginRunKeepRaw', None,
 
48
            _( 'Keep Raw File' ), '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,
49
 
            _( 'Unkeep Raw Image' ), 'U',
 
50
            self.do_keep_raw_file ) ], self.window )
 
51
        self.action_group.add_actions( [ ( 'EogPluginRunUnkeepRaw', None,
 
52
            _( 'Unkeep Raw File' ), '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,
53
 
            _( 'Delete Unkept Raw Images' ), None,
 
54
            self.do_unkeep_raw_file ) ], self.window )
 
55
        self.action_group.add_actions( [ ( 'EogPluginRunDeleteRaw', None,
 
56
            _( 'Delete Unkept Raw Files' ), 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_files ) ], 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 )
140
144
            if( os.path.isfile( base_fname + '.' + ext ) ):
141
145
                return os.path.basename( base_fname + '.' + ext );
142
146
 
143
 
        # path the raw images will be moved to
 
147
        # path the raw files will be moved to
144
148
        raw_path = os.path.dirname( fname ) + '/raw';
145
149
 
146
150
        # loop through valid raw file extensions, uppercase and lowercase
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' ) )
178
 
 
179
 
    def on_keep_raw_image( self, action ):
180
 
 
181
 
        # do we have just the one selected image? we can't handle multiple
182
 
        # images because EogThumbView.get_selected_images() doesn't work.
183
 
        thumb_view = self._window.get_thumb_view()
184
 
        if( thumb_view.get_n_selected() == 1 ):
185
 
 
186
 
            # get the image
187
 
            image = thumb_view.get_first_selected_image()
188
 
            if( image != None ):
189
 
                self.keep_raw_image( image )
190
 
 
191
 
            # update ui
192
 
            self.update_status( self._window )
193
 
 
194
 
    def on_unkeep_raw_image( self, action ):
195
 
 
196
 
        # do we have just the one selected image? we can't handle multiple
197
 
        # images because EogThumbView.get_selected_images() doesn't work.
198
 
        thumb_view = self._window.get_thumb_view()
199
 
        if( thumb_view.get_n_selected() == 1 ):
200
 
 
201
 
            # get the image
202
 
            image = thumb_view.get_first_selected_image()
203
 
            if( image != None ):
204
 
                self.unkeep_raw_image( image )
205
 
 
206
 
            # update ui
207
 
            self.update_status( self._window )
208
 
 
209
 
    def keep_raw_image( self, image ):
 
181
                self.statusbar.push( 0, "  " + _( 'raw: keep' ) )
 
182
 
 
183
    def do_keep_raw_file( self, action, window ):
 
184
 
 
185
        # do we have just the one selected image? we can't handle multiple
 
186
        # images because EogThumbView.get_selected_images() doesn't work.
 
187
        thumb_view = window.get_thumb_view()
 
188
        if( thumb_view.get_n_selected() == 1 ):
 
189
 
 
190
            # get the image
 
191
            image = thumb_view.get_first_selected_image()
 
192
            if( image != None ):
 
193
                self.keep_raw_file( image )
 
194
 
 
195
            # update ui
 
196
            self.update_status( window )
 
197
 
 
198
    def do_unkeep_raw_file( self, action, window ):
 
199
 
 
200
        # do we have just the one selected image? we can't handle multiple
 
201
        # images because EogThumbView.get_selected_images() doesn't work.
 
202
        thumb_view = window.get_thumb_view()
 
203
        if( thumb_view.get_n_selected() == 1 ):
 
204
 
 
205
            # get the image
 
206
            image = thumb_view.get_first_selected_image()
 
207
            if( image != None ):
 
208
                self.unkeep_raw_file( image )
 
209
 
 
210
            # update ui
 
211
            self.update_status( window )
 
212
 
 
213
    def keep_raw_file( 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." ) )
230
234
            # move the raw file in to the raw directory
231
235
            os.rename( path + '/' + raw_fname, path + '/raw/' + raw_fname )
232
236
 
233
 
    def unkeep_raw_image( self, image ):
 
237
    def unkeep_raw_file( self, image ):
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_files( 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!' ) )