# 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()
