#!/usr/bin/env python
# encoding: utf-8
"""Information pop-up for vimiv.

Shows the current version, the icon, gives a link to the GitHub site and the
possibility to display the licence.
"""

from gi import require_version
require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib


class Information():
    """Class containing information on vimiv."""

    def __init__(self):
        self.version = "vimiv 0.8.dev0"

    def get_version(self):
        """Return current version.

        Return:
            String of the current version.
        """
        return self.version

    def show_version_info(self, running_tests=False):
        """Show information about current version in a Gtk pop-up.

        Args:
            running_tests: If True running from testsuite. Do not show pop-up.
        """
        # Popup with buttons
        popup = Gtk.Dialog(title="vimiv - Info", transient_for=Gtk.Window())
        licence_button = popup.add_button(" Licence", Gtk.ResponseType.HELP)
        popup.add_button("Close", Gtk.ResponseType.CLOSE)
        # Different widgets
        licence_button.connect("clicked", self.show_licence)
        licence_icon = Gtk.Image.new_from_icon_name(
            "text-x-generic-symbolic", 0)
        licence_button.set_image(licence_icon)
        licence_button.set_always_show_image(True)

        version_label = Gtk.Label()
        version_size = '<span size="x-large">'
        version_label.set_markup(version_size + self.version + '</span>')

        info_label = Gtk.Label()
        info_label.set_text("vimiv - an image viewer with vim-like keybindings")
        info_label.set_hexpand(True)

        website_label = Gtk.Label()
        website_label.set_markup('<a href="https://www.github.com/karlch/vimiv"'
                                 'title="GitHub site">vimiv on GitHub</a>')

        icon_theme = Gtk.IconTheme.get_default()
        # Icon not available in theme when icon cache was not updated
        try:
            vimiv_pixbuf = icon_theme.load_icon("vimiv", 128, 0)
            vimiv_image = Gtk.Image.new_from_pixbuf(vimiv_pixbuf)
            load_image = 1
        except GLib.Error:
            load_image = 0

        # Layout
        box = popup.get_child()
        box.set_border_width(12)
        grid = Gtk.Grid()
        grid.set_column_spacing(12)
        grid.set_row_spacing(12)
        box.pack_start(grid, False, False, 12)
        if load_image:
            grid.attach(vimiv_image, 0, 0, 1, 1)
        grid.attach(version_label, 0, 1, 1, 1)
        grid.attach(info_label, 0, 2, 1, 1)
        grid.attach(website_label, 0, 4, 1, 1)
        popup.show_all()
        if not running_tests:
            popup.run()
            popup.destroy()

    def show_licence(self, button, running_tests=False):
        """Show the licence in a new pop-up window.

        Args:
            button: The Gtk.Button() that called this function.
            running_tests: If True running from testsuite. Do not show pop-up.
        """
        popup = Gtk.Dialog(title="vimiv - Licence", transient_for=Gtk.Window())
        popup.add_button("Close", Gtk.ResponseType.CLOSE)

        label = Gtk.Label()
        title = '<span size="large"><b>MIT Licence</b></span>\n\n'

        # Get license text from file
        with open("/usr/share/licenses/vimiv/LICENSE") as license_file:
            license_text = license_file.read()
        label_markup = title + license_text
        label.set_markup(label_markup)

        box = popup.get_child()
        box.pack_start(label, False, False, 0)
        box.set_border_width(12)
        popup.show_all()
        if not running_tests:
            popup.run()
            popup.destroy()

@@ -1407,16 +1407,17 @@
  size=%22x
+x
 -large%22%3E
@@ -1703,24 +1703,300 @@
 e_label.set_
+halign(Gtk.Align.END)%0A        website_label.set_markup('%3Ca href=%22http://karlch.github.io/vimiv%22'%0A                                 'title=%22vimiv website%22%3EWebsite%3C/a%3E')%0A        github_label = Gtk.Label()%0A        github_label.set_halign(Gtk.Align.START)%0A        github_label.set_
 markup('%3Ca h
@@ -2070,25 +2070,24 @@
         
- 
 'title=%22
 GitHub s
@@ -2082,21 +2082,8 @@
 le=%22
-GitHub site%22%3E
 vimi
@@ -2087,16 +2087,24 @@
 imiv on 
+GitHub%22%3E
 GitHub%3C/
@@ -2397,17 +2397,20 @@
 image = 
-1
+True
 %0A       
@@ -2454,17 +2454,21 @@
 image = 
-0
+False
 %0A%0A      
@@ -2681,24 +2681,71 @@
  False, 12)%0A
+        grid.attach(version_label, 0, 0, 2, 1)%0A
         if l
@@ -2795,20 +2795,20 @@
 age, 0, 
-0, 1
+1, 2
 , 1)%0A   
@@ -2816,39 +2816,36 @@
     grid.attach(
-version
+info
 _label, 0, 1, 1,
@@ -2839,20 +2839,20 @@
 bel, 0, 
-1, 1
+2, 2
 , 1)%0A   
@@ -2860,36 +2860,39 @@
     grid.attach(
-info
+website
 _label, 0, 2, 1,
@@ -2886,17 +2886,17 @@
 bel, 0, 
-2
+3
 , 1, 1)%0A
@@ -2915,31 +2915,30 @@
 .attach(
-website
+github
 _label, 
 0, 4, 1,
@@ -2933,12 +2933,12 @@
 el, 
-0, 4
+1, 3
 , 1,

Add website to :version pop-up and change layout