class GPGImageViewer(object):
    """
    Image Viewer class
    """

    def __init__(self, glade_file):
        self.gtk_builder = gtk.Builder()
        self.gtk_builder.add_from_file(glade_file)

        self.gpg = GNUPG(self.gtk_builder.get_object('error_decrypt_dialog'))

        self.window = self.gtk_builder.get_object('mainWindow')
        self.image = self.gtk_builder.get_object('imageView')

        signals = {"on_mainWindow_destroy" : gtk.main_quit,
                   "on_menu_quit_activate" : gtk.main_quit,
                   "on_menu_open_activate" : self.cb_show_file_chooser,
                   "on_go_left_button_clicked" : self.cb_go_left_image,
                   "on_go_right_button_clicked": self.cb_go_right_image,
                  }

        self.gtk_builder.connect_signals(signals)

        self.images = Images()

        self.prev_button = self.gtk_builder.get_object('go_left_button')
        self.next_button = self.gtk_builder.get_object('go_right_button')
        self.gtk_builder.get_object('ok_passphrase_button').grab_default()

    def cb_go_left_image(self, cb_data):
        """
        Show previous image
        """
        self.show_image(self.images.prev())
    # cb_go_left_image()

    def cb_go_right_image(self, _data):
        """
        Show to next image
        """
        self.show_image(self.images.next())
    # cb_go_right_image()

    def cb_show_file_chooser(self, _data):
        """
        Show file chooser
        """
        file_chooser = self.gtk_builder.get_object('file_chooser_dialog')
        response = file_chooser.run()
        file_chooser.hide()

        if response != gtk.RESPONSE_OK:
            return

        image_file = file_chooser.get_filename()

        self.images.load_images(image_file)

        self.show_image(image_file)
    # cb_show_file_chooser()

    def is_gpg_file(self, file_type):
        if file_type.startswith('GPG encrypted data'):
            return True

        if file_type.startswith('PGP RSA encrypted'):
            return True

        return False
    # is_gpg_file()

    def show_image(self, image_path):
        """
        Function show_image
        """

        if not image_path:
            return

        self.images.load_images(image_path)

        self.prev_button.set_sensitive(self.images.image != \
                                         self.images.get_first())
        self.next_button.set_sensitive(self.images.image != \
                                       self.images.get_last())

        # detect if it's a supported image type
        #if not check if is encrypted
        # fail otherwise
        if not gtk.gdk.pixbuf_get_file_info(image_path):

            file_type = magic.from_file(image_path)

            if  self.is_gpg_file(file_type):

                pixbuf = self.gpg.decrypt_file(image_path)
            else:
                not_supported_dialog = \
                self.gtk_builder.get_object('not_supported_dialog')

                not_supported_msg = file_type + ' is not a supported file!'
                not_supported_dialog.set_markup(not_supported_msg)
                not_supported_dialog.run()
                not_supported_dialog.hide()
                return

        else:
            pixbuf = gtk.gdk.pixbuf_new_from_file(image_path)


        pixbuf = pixbuf.apply_embedded_orientation()
        width = pixbuf.get_width()
        height = pixbuf.get_height()

        while width > 900 or height > 900:
            width = width * .9
            height = height * .9

        width = int(width)
        height = int(height)

        resize_pixbuf = pixbuf.scale_simple(width, height, gtk.gdk.INTERP_HYPER)

        main_label = self.gtk_builder.get_object('main_label')
        main_label.set_label("Image " + os.path.basename(image_path) +\
                " size: " + str(width) + " x " + str(height))


        self.image.set_from_pixbuf(resize_pixbuf)

    def __get_user_pw(self):
        """
        This function returns the passphrase that will be used for the GPG
        encrypted files.
        """

        dialog_window = self.gtk_builder.get_object('passwordDialog')
        user_entry = self.gtk_builder.get_object('passphrase_entry')

        response = dialog_window.run()
        text = user_entry.get_text()

        dialog_window.hide()

        if (response == gtk.RESPONSE_OK) and (text != ''):
            return text
        else:
            return None
class TestImages(unittest.TestCase):
    def setUp(self):
        self.images = Images()
        self.image = os.path.dirname(__file__)+'/fixtures/image4.jpg'
        self.first_image = os.path.dirname(__file__)+'/fixtures/image1.jpg'
        self.last_image = os.path.dirname(__file__)+'/fixtures/image9.jpg'
    # setUp()

    def test_load_images(self):
        image = os.path.dirname(__file__)+'/fixtures/image4.jpg'
        self.images.load_images(image)
        self.assertEqual(image, self.images.image)
        self.assertEqual(9, self.images.num_images)
    # test_load_images()

    def test_next_image(self):
        self.images.load_images(self.image)
        next_image = os.path.dirname(__file__)+'/fixtures/image5.jpg'

        self.assertEqual(next_image, self.images.next())
    # test_nextImage()

    def test_prev_image(self):
        self.images.load_images(self.image)
        prev_image = os.path.dirname(__file__)+'/fixtures/image3.jpg'

        self.assertEqual(prev_image, self.images.prev())
    # test_nextImage()

    def test_prev_first(self):
        self.images.load_images(self.image) # image4
        self.images.prev() # image3
        self.images.prev() # image2
        self.images.prev() # image1

        self.assertEqual(self.first_image, self.images.image)

        self.images.prev() # image1

        self.assertEqual(self.first_image, self.images.image)

    # test_prevThanFirst()

    def test_after_last(self):
        self.images.load_images(self.image) # image4
        self.images.next() # image5
        self.images.next() # image6
        self.images.next() # image7
        self.images.next() # image8
        self.images.next() # image9

        self.assertEqual(self.last_image, self.images.image)

        self.images.next() # image9

        self.assertEqual(self.last_image, self.images.image)

    # test_prevThanFirst()

    def test_last_image(self):
        """
        Return last image
        """
        self.images.load_images(self.image)
        self.assertEqual(self.last_image, self.images.get_last())
    # test_lastImage()

    def test_first_image(self):
        """
        Return first Image
        """
        self.images.load_images(self.image)
        self.assertEqual(self.first_image, self.images.get_first())
    # test_lastImage()

    def test_do_nothing_when_empty(self):
        """
        Try to move with no images
        """

        self.assertIsNone(self.images.image)

        self.images.next()

        self.assertIsNone(self.images.image)

        self.images.prev()

        self.assertIsNone(self.images.image)