# Phatch - Photo Batch Processor
# Copyright (C) 2007-2008 www.stani.be
#
# 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/
#
# Phatch recommends SPE (http://pythonide.stani.be) for editing python files.

# Embedded icon is designed by Igor Kekeljevic (http://www.admiror-ns.co.yu).

# Follows PEP8

from core import ct, models
from lib.reverse_translation import _t

#no need to lazily import these as they are always imported
import os
import shutil


class Action(models.Action):
    """Defined variables: <filename> <type> <folder> <width> <height>"""

    label = _t('Rename')
    author = 'Stani'
    email = 'spe.stani.be@gmail.com'
    version = '0.1'
    tags = [_t('file')]
    __doc__ = _t('Rename the original image')
    valid_last = True

    def interface(self, fields):
        fields[_t('File Name')] = self.FileNameField(choices=self.FILENAMES)
        fields[_t('In')] = self.FolderField(self.DEFAULT_FOLDER,
                                        choices=self.FOLDERS)

    def apply(self, photo, setting, cache):
        #get info
        info = photo.info
        #get file values
        folder, filename, typ = self.is_done_info(info)
        if setting('overwrite_existing_images') \
                or not os.path.exists(filename):
            #ensure folder
            filename = self.ensure_path_or_desktop(folder, photo, filename)
            #do it
            os.rename(info['path'], filename)
        return photo

    def is_done_info(self, info):
        folder = self.get_field('In', info)
        filename = self.get_field('File Name', info)
        typ = info['type']
        return folder, os.path.join(folder, '%s.%s' % (filename, typ)), typ

    def is_overwrite_existing_images_forced(self):
        """Force overwrite when using default values."""
        return (self.get_field_string('In') == self.FOLDER) and\
            (self.get_field_string('File Name') == self.FILENAME)

    icon = \
'x\xda\x01\x9c\x04c\xfb\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x000\x00\
\x00\x000\x08\x06\x00\x00\x00W\x02\xf9\x87\x00\x00\x00\x04sBIT\x08\x08\x08\
\x08|\x08d\x88\x00\x00\x04SIDATh\x81\xed\x98]\x88[E\x14\xc7\xffg\x92\xbd\xa6\
\xba\xban\xebWA\x10\xbb+T\xd6\xc4$c6\xc4me\x1f\xb4\x94\n\x16\x8a[\n\x8dE|\
\xab/U\x10\x14\x8a\x88_\xef\xf5\xebE\xc4\x17\x1fDj\xb5\x8a\x08\xa2\xa0\xed\
\x83KI\xc6,\xa9y\x10\xb2\xac\xae\xa8\x0f]vMP6\x9b\xee\xbd\xc7\x87&\xf5\xe6\
\xee\xdc\xe4N>\xd8\x97\xfc\xdef\xce\x993\xff\xff\xbd3\x97\xb9\x03\x0c\x192d\
\xc8vB\xdb-\x00\x00\xe2\xf1\xf8}\xe1p8\xc6\xcc\x02\x00\x98\x99\x00\xc4\x88h\
\r\xc0\xb2+uY)u\xc9=v\xdb\r$\x93\xc9,\x11}\x08`$H>\x11\x9d\xc9\xe7\xf3\xcf7\
\xdbb`\xca\x02"\x84x\x16\x01\xc5\x03\x003\x9f\x9c\x9a\x9a\xb2\xae\x8f\xefe\
\xf2L&\xb33\x95J\xdd\xd5K\rf\xfe\xd9p\xc8/\xa5R\xa9\xdel\x84\xbb\x99TJy\x00\
\xc0\x99z\xbd~\x7f\xa3}\x85\x88^\xcb\xe7\xf3\xef\x9a\xd6\xb2,\xebT\xbd^_\x01\
0\xd9\xe8\x8a\x02\xd8\xebI;\x0b\x00D\xb4f\xdb\xf6\xab\xee\x80\xf1\x1eh\x88\
\xff\n\xfa\xd7\xfe\xb2R\xea\r\xd3\x9a\x9e\xfaQ\x00EW\xd7e\xa5T\xcc/\xdfh\t\
\xcd\xce\xce\x86\x01\xbc\x0f\xff5\xfb\x8a\x94\xd2\xfb\xf4\x06\x8a\x91\x81j\
\xb5z\x04\xc0=mR\xc2\x00N\xf5\xa4\xc8\x10#\x03Dt2@Z6\x1e\x8f\xdf\xda\xa5\x1e\
c\x02\x1bH\xa5RS\x00f\x03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