#!/usr/bin/python3
# -*- coding: utf-8 -*-
'''
The purpose of this program is to display the about-box for MX-Fluxbox.
Copyright (C) 2020 MX Authors

Authors: fehlix
         MX DevTeam <http://mxlinux.org>

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/>.
'''

import sys
import subprocess
import gettext
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import QApplication, QMessageBox, QPushButton
from distutils import spawn 
from os import path
from string import Template

gettext.bindtextdomain('mx-fluxbox-about', '/usr/share/locale')
gettext.textdomain('mx-fluxbox-about')
gettext.install('mx-fluxbox-about')
_ = gettext.gettext

# globals
global msgBoxIcon;
global windowIcon;
global changelogButtonText
global closeButtonText
global licenseButtonText
global changelogButtonText
global changelogTitle
global closeButtonText
global closeButtonText
global licenseButtonText
global licenseViewerTitle
global about
global aboutTitle
global aboutText
global msgText

# icons
msgBoxIcon = '/usr/share/icons/hicolor/128x128/apps/mx-fluxbox.png'
windowIcon = '/usr/share/icons/hicolor/32x32/apps/mxflux.png'

# translations
about               = _('MX-Fluxbox')
aboutTitle          = _('About MX-Fluxbox')
aboutText           = _('A version of fluxbox developed specifically as a fully integrated overlay for MX Linux.')
changelogButtonText = _('Changelog')
changelogTitle      = _('MX-Fluxbox Changelog')
closeButtonText     = _('Close')
licenseButtonText   = _('License')
licenseViewerTitle  = _('MX-Fluxbox license')

msgText= '''
    <p align=center><b><h2>$about</h2></b></p>
    <p align=center>Version: $version</p>
    <p align=center>Release: $release</p>
    <p align=center><h3>$aboutText</h3></p>
    <p align=center><a href=http://mxlinux.org>http://mxlinux.org</a>
    <br></p><p align=center>Copyright (c) MX Linux<br /><br/></p>
'''

def showAbout(msgBox):
    version = subprocess.check_output(["cat /etc/mxfb_version 2>/dev/null || echo ''" ], shell=True).decode('utf-8')
    release = subprocess.check_output(["dpkg-query -f '${Version}' -W mx-fluxbox 2>/dev/null || echo ''" ], shell=True).decode('utf-8')
    
    boxText=Template(msgText).substitute(about=about, aboutText=aboutText, release=release, version=version)
    
    iconPixmap = QtGui.QPixmap(msgBoxIcon)
    msgBox.setIconPixmap(iconPixmap)
    msgBox.setWindowTitle(aboutTitle)
    msgBox.setWindowIcon(QtGui.QIcon(windowIcon))
    msgBox.setText(boxText)
    
    changelogButton = msgBox.addButton((changelogButtonText), QMessageBox.ActionRole)
    licenseButton   = msgBox.addButton((licenseButtonText)  , QMessageBox.ActionRole)
    closeButton     = msgBox.addButton((closeButtonText)    , QMessageBox.RejectRole)
    msgBox.setDefaultButton(closeButton)
    msgBox.setEscapeButton(closeButton)
    
    reply = msgBox.exec_()
    
    if msgBox.clickedButton() == closeButton:
        sys.exit(reply)
    if msgBox.clickedButton() == licenseButton:
        showLicence(reply)
    if msgBox.clickedButton() == changelogButton:
        showChangelog(reply)

def showLicence(reply):
    if path.exists("/usr/share/doc/mx-fluxbox/license.html"):
        flux_box_license = "/usr/share/doc/mx-fluxbox/license.html"
    else:
        flux_box_license = "/usr/share/doc/mx-fluxbox-about/mx-fluxbox-license.html"
        
    if spawn.find_executable("mx-viewer"):
        licenseViewer = "mx-viewer"
        p=subprocess.call([licenseViewer, flux_box_license, licenseViewerTitle])
    elif spawn.find_executable("sensible-browser"):
        licenseViewer = "sensible-browser"
        p=subprocess.call([licenseViewer, flux_box_license])
    else:
        licenseViewer = "xdg-open"
        p=subprocess.call([licenseViewer, flux_box_license])
    sys.exit(reply)
    
def showChangelog(reply):
    command_string = ''
    command_string += 'icon="'  + windowIcon     + '";'
    command_string += 'title="' + changelogTitle + '";'
    command_string += 'close="' + closeButtonText + '";'
    command_string += '''
        read width height < <(xdotool getdisplaygeometry); 
        width=$(($width*3/4)); 
        height=$(($height*2/3)); 
        zcat /usr/share/doc/mx-fluxbox/changelog.gz 2>/dev/null | 
        yad --width=$width
          --height=$height
          --center
          --button="$close"
          --window-icon="$icon"
          --title="$title"
          --fontname=mono
          --margins=7
          --borders=5
          --text-info
        '''
    command = " ".join(command_string.split())
    command = "bash -c '%s'" % command
    p=subprocess.call([command], shell=True)
    sys.exit(reply)

def main():
    app = QApplication(sys.argv)
    msgBox = QMessageBox()
    showAbout(msgBox)
    msgBox.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
