/gtk/eog-manage-raws

To get this branch, use:
bzr branch http://bzr.ed.am/gtk/eog-manage-raws
1 by edam
initial comit
1
import eog, gtk, os
2
3
class HelloWorldPlugin(eog.Plugin):
4
5
    raw_file_extensions = [ 'cr2', '3fr', 'ari', 'arw', 'srf', 'sr2', 'bay',
6
        'crw', 'cr2', 'cap', 'iiq', 'eip', 'dcs', 'dcr', 'drf', 'k25', 'kdc',
7
        'dng', 'erf', 'fff', 'mef', 'mos', 'mrw', 'nef', 'nrw', 'orf', 'ptx',
8
        'pef', 'pxn', 'r3d', 'raf', 'raw', 'rw2', 'rwl', 'rwz', 'x3f' ]
9
10
    def __init__( self ):
11
        eog.Plugin.__init__( self )
12
13
    def activate( self, window ):
14
        self._window = window
15
16
        # insert menu item
17
        manager = window.get_ui_manager()
18
        self._action_group = gtk.ActionGroup( "EogRawToolsPluginActions" )
19
        self._action_group.add_actions( [ ( "EogPluginRunKeepRaw", None,
20
            _( "Keep Raw Image" ), "K",
21
            _( "Keep accompanying raw image" ),
22
            self.on_keep_raw_image ) ] )
23
        manager.insert_action_group( self._action_group, -1 )
24
        self._ui_id = manager.add_ui_from_string( """
25
            <ui>
26
                <menubar name="MainMenu">
27
                    <menu name="ToolsMenu" action="Tools">
28
                        <separator />
29
                        <menuitem name="EogPluginRawTools"
30
                            action="EogPluginRunKeepRaw" />
31
                        <separator />
32
                    </menu>
33
                </menubar>
34
                <popup name="ViewPopup">
35
                    <separator />
36
                    <menuitem action="EogPluginRunKeepRaw" />
37
                    <separator />
38
                </popup>
39
            </ui>
40
        """ )
41
42
        # connect to selection change
43
        thumb_view = window.get_thumb_view()
44
        self._on_selection_change_id = thumb_view.connect_after( \
45
            "selection_changed", self.on_selection_change, window )
46
47
        # init ui state
48
        self.update_action_group_sensitivity( window )
49
50
    def deactivate( self, window ):
51
52
        # remove menu items
53
        manager = window.get_ui_manager()
54
        manager.remove_ui( self._ui_id )
55
        manager.remove_action_group( self._action_group )
56
        manager.ensure_update()
57
58
        # disconnect handlers
59
        thumb_view = window.get_thumb_view()
60
        thumb_view.disconnect( self._on_selection_change_id )
61
62
    def on_selection_change( self, view, data ):
63
        self.update_action_group_sensitivity( data )
64
65
    def update_action_group_sensitivity( self, window ):
66
67
        # do we have just the one selected image? we can't handle multiple
68
        # images because EogThumbView.get_selected_images() doesn't work.
69
        thumb_view = window.get_thumb_view()
70
        self._action_group.set_sensitive( \
71
            thumb_view.get_n_selected() == 1 )
72
73
    def on_keep_raw_image( self, action ):
74
75
        # do we have just the one selected image? we can't handle multiple
76
        # images because EogThumbView.get_selected_images() doesn't work.
77
        thumb_view = self._window.get_thumb_view()
78
        if( thumb_view.get_n_selected() == 1 ):
79
80
            # get the image
81
            image = thumb_view.get_first_selected_image()
82
            if( image != None ):
83
                self.keep_raw_image( image )
84
85
    def keep_raw_image( self, image ):
86
87
        fname = image.get_file().get_path()
88
89
        # strip the file extension off, ready to search for raw files
90
        base_fname = image.get_file().get_path()
91
        pos = base_fname.rfind( '.' )
92
        if( pos != -1 ):
93
            base_fname = base_fname[ 0 : pos ]
94
        
95
        # check for stupidity
96
        if( base_fname[ -1 : None ] == '/' ):
97
            dialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO,
98
                gtk.BUTTONS_CLOSE, "Image is invalid!" )
99
            dialog.run()
100
            dialog.destroy()
101
            return
102
103
        # path the raw images will be moved to
104
        raw_path = os.path.dirname( fname ) + '/raw';
105
106
        # loop through valid raw file extensions
107
        found = False;
108
        for ext in self.raw_file_extensions + \
109
             map( lambda x: x.upper(), self.raw_file_extensions ):
110
111
            # calculate the name of the raw file and where we would move it to
112
            src_fname = base_fname + '.' + ext
113
            dst_fname = raw_path + '/' + os.path.basename( src_fname )
114
115
            # look for existing files
116
            if( os.path.isfile( src_fname ) ):
117
                found = True;
118
119
                # create the raw directory, if it doesn't exist
120
                if( not os.path.isdir( raw_path ) ):
121
                    os.mkdir( raw_path );
122
123
                # move the raw file in to the raw directory
124
                os.rename( src_fname, dst_fname )
125
126
            # check for already-moved raw files
127
            elif( os.path.isfile( dst_fname ) ):
128
                found = True;
129
130
        # check we found the raw file
131
        if( not found ):
132
            dialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO,
133
                gtk.BUTTONS_CLOSE, "Raw file not found!" )
134
            dialog.format_secondary_text( "This image doesn't appear to have an accompanying raw file." )
135
            dialog.run()
136
            dialog.destroy()
137
            return