# -*- coding: utf-8 -*-
#!/usr/bin/env python

# =============================================================================
#
# Dialog implementation generated from a XDL file.
#
# Created: Mon Feb  8 05:06:58 2021
#      by: unodit 0.8.0
#
# WARNING! All changes made in this file will be overwritten
#          if the file is generated again!
#
# =============================================================================

import uno
import unohelper
from com.sun.star.awt import XActionListener
from com.sun.star.task import XJobExecutor

from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK, BUTTONS_OK_CANCEL, BUTTONS_YES_NO, BUTTONS_YES_NO_CANCEL, BUTTONS_RETRY_CANCEL, BUTTONS_ABORT_IGNORE_RETRY
from com.sun.star.awt.MessageBoxButtons import DEFAULT_BUTTON_OK, DEFAULT_BUTTON_CANCEL, DEFAULT_BUTTON_RETRY, DEFAULT_BUTTON_YES, DEFAULT_BUTTON_NO, DEFAULT_BUTTON_IGNORE
from com.sun.star.awt.MessageBoxType import MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX
from com.sun.star.awt import FontDescriptor

langs = ("auto", "acf", "af", "aig", "am", "ar", "az", "bah", "be", "bem", "bg", "bi", "bjs", "bn", "bod", "br", "bs", "ca", "cb", "cha", "cop", "crs", "cs", "cy", "da", "de", "div", "dzo", "el", "en", "eo", "es", "et", "eu", "fa", "fi", "fn", "fo", "fr", "ga", "gcl", "gd", "gl", "grc", "gu", "gv", "gyn", "ha", "haw", "he", "hi", "hr", "ht", "hu", "hy", "id", "is", "it", "ja", "jam", "jv", "ka", "kab", "kal", "kea", "kk", "km", "kn", "ko", "ku", "ku", "ky", "la", "lb", "lo", "lt", "lv", "men", "mfe", "mg", "mh", "mi", "mk", "mn", "ms", "mt", "my", "ne", "niu", "nl", "no", "nso", "ny", "pa", "pap", "pau", "pis", "pl", "pot", "pov", "ppk", "ps", "pt", "qu", "rm", "ro", "ru", "run", "rw", "sg", "si", "sk", "sl", "smo", "sna", "so", "sq", "sr", "srn", "sv", "svc", "sw", "syc", "ta", "te", "tet", "tg", "th", "ti", "tk", "tkl", "tl", "tmh", "tn", "ton", "tpi", "tr", "tvl", "uk", "ur", "uz", "vi", "vic", "wls", "wo", "xh", "yi", "zdj", "zh-CN", "zh-TW", "zu")

class ExcApp(unohelper.Base, XActionListener, XJobExecutor):
    """
    Class documentation...
    """
    def __init__(self, ctx=uno.getComponentContext(), err="error msg", exc="exception/traceback"):
        self.LocalContext = ctx
        self.ServiceManager = self.LocalContext.ServiceManager
        self.Toolkit = self.ServiceManager.createInstanceWithContext("com.sun.star.awt.ExtToolkit", self.LocalContext)

        # -----------------------------------------------------------
        #               Create dialog and insert controls
        # -----------------------------------------------------------

        # --------------create dialog container and set model and properties
        self.DialogContainer = self.ServiceManager.createInstanceWithContext("com.sun.star.awt.UnoControlDialog", self.LocalContext)
        self.DialogModel = self.ServiceManager.createInstance("com.sun.star.awt.UnoControlDialogModel")
        self.DialogContainer.setModel(self.DialogModel)
        self.DialogModel.Name = "exceptions"
        self.DialogModel.PositionX = "135"
        self.DialogModel.PositionY = "91"
        self.DialogModel.Width = 243
        self.DialogModel.Height = 173
        self.DialogModel.Closeable = True
        self.DialogModel.Moveable = True

        
        # --------- create an instance of FixedText control, set properties ---
        self.message = self.DialogModel.createInstance("com.sun.star.awt.UnoControlFixedTextModel")

        self.message.Name = "message"
        self.message.TabIndex = 0
        self.message.PositionX = "5"
        self.message.PositionY = "13"
        self.message.Width = 233
        self.message.Height = 20
        self.message.HelpURL = "vnd.sun.star.help://help/vnd.include-once.pagetranslate/errors.xhp?Language=en&amp;System=UNIX&amp;UseDB=no"
        self.message.Label = err
        self.message.MultiLine = True
        self.message.BackgroundColor = 0xFFDDCC
        self.message.FontDescriptor = FontDescriptor()
        self.message.FontDescriptor.Name = "Liberation Sans"
        self.message.FontDescriptor.StyleName = "Bold"
        self.message.FontDescriptor.Weight = 150.0
        #self.message.FontDescriptor.Family = "swiss"
        #print(vars(self.message.FontDescriptor))

        # inserts the control model into the dialog model
        self.DialogModel.insertByName("message", self.message)

        # --------- create an instance of FixedText control, set properties ---
        self.Label1 = self.DialogModel.createInstance("com.sun.star.awt.UnoControlFixedTextModel")

        self.Label1.Name = "Label1"
        self.Label1.TabIndex = 4
        self.Label1.PositionX = "5"
        self.Label1.PositionY = "2"
        self.Label1.Width = 139
        self.Label1.Height = 8
        self.Label1.Label = "⚠ So, we ran into a problem here:"

        # inserts the control model into the dialog model
        self.DialogModel.insertByName("Label1", self.Label1)

        # --------- create an instance of Button control, set properties ---
        self.Close = self.DialogModel.createInstance("com.sun.star.awt.UnoControlButtonModel")

        self.Close.Name = "Close"
        self.Close.TabIndex = 1
        self.Close.PositionX = "182"
        self.Close.PositionY = "154"
        self.Close.Width = 54
        self.Close.Height = 13
        self.Close.Label = "OK"

        # inserts the control model into the dialog model
        self.DialogModel.insertByName("Close", self.Close)

        # add the action listener
        self.DialogContainer.getControl('Close').addActionListener(self)
        self.DialogContainer.getControl('Close').setActionCommand('Close_OnClick')

        # --------- create an instance of FixedHyperlink control, set properties ---
        self.help = self.DialogModel.createInstance("com.sun.star.awt.UnoControlFixedHyperlinkModel")

        self.help.Name = "help"
        self.help.TabIndex = 2
        self.help.PositionX = "4"
        self.help.PositionY = "150"
        self.help.Width = 65
        self.help.Height = 17
        self.help.HelpURL = "vnd.sun.star.help://help/vnd.include-once.pagetranslate/errors.xhp?Language=en&amp;System=UNIX&amp;UseDB=no"
        self.help.URL = "https://fossil.include-once.org/pagetranslate/file/help/en/vnd.include-once.pagetranslate/errors.duck"
        self.help.Label = "see help section on common errors"
        self.help.MultiLine = True

        # inserts the control model into the dialog model
        self.DialogModel.insertByName("help", self.help)

        # --------- create an instance of FixedHyperlink control, set properties ---
        self.bugreport = self.DialogModel.createInstance("com.sun.star.awt.UnoControlFixedHyperlinkModel")

        self.bugreport.Name = "bugreport"
        self.bugreport.TabIndex = 6
        self.bugreport.PositionX = "85"
        self.bugreport.PositionY = "150"
        self.bugreport.Width = 30
        self.bugreport.Height = 17
        self.bugreport.HelpURL = "https://fossil.include-once.org/pagetranslate/tktnew"
        self.bugreport.Label = "or report as bug"
        self.bugreport.URL = "https://fossil.include-once.org/pagetranslate/tktnew"
        self.bugreport.MultiLine = True

        # inserts the control model into the dialog model
        self.DialogModel.insertByName("bugreport", self.bugreport)

        # --------- create an instance of Edit control, set properties ---
        self.exception = self.DialogModel.createInstance("com.sun.star.awt.UnoControlEditModel")

        self.exception.Name = "exception"
        self.exception.Border = 0
        self.exception.HScroll = True
        self.exception.VScroll = True
        self.exception.TabIndex = 3
        self.exception.PositionX = "5"
        self.exception.PositionY = "36"
        self.exception.Width = 233
        self.exception.Height = 108
        self.exception.MultiLine = True
        self.exception.Text = exc

        # inserts the control model into the dialog model
        self.DialogModel.insertByName("exception", self.exception)


    def showDialog(self):
        self.DialogContainer.setVisible(True)
        self.DialogContainer.createPeer(self.Toolkit, None)
        self.DialogContainer.execute()

    # --------- functions -----------------------

    def myFunction(self):
        # TODO: not implemented
        pass

    # --------- helpers -------------------------

    def messageBox(self, MsgText, MsgTitle, MsgType=MESSAGEBOX, MsgButtons=BUTTONS_OK):
        sm = self.LocalContext.ServiceManager
        si = sm.createInstanceWithContext("com.sun.star.awt.Toolkit", self.LocalContext)
        mBox = si.createMessageBox(self.Toolkit, MsgType, MsgButtons, MsgTitle, MsgText)
        mBox.execute()

    # -----------------------------------------------------------
    #               Action events
    # -----------------------------------------------------------

    def actionPerformed(self, oActionEvent):
    
        if oActionEvent.ActionCommand == 'Close_OnClick':
            self.Close_OnClick()

    
    def Close_OnClick(self):
        self.DialogContainer.endDialog(1);


def exception(err, exc, *args):

    try:
        ctx = remote_ctx                    # IDE
    except:
        ctx = uno.getComponentContext()     # UI

    try:
        # get desktop
        desktop = ctx.getByName("/singletons/com.sun.star.frame.theDesktop")
        # get document
        document = desktop.getCurrentComponent()
    except:
        pass

    app = ExcApp(ctx=ctx, err=err, exc=exc)
    app.showDialog()



class LangSelection(unohelper.Base, XActionListener, XJobExecutor):
    """
    Class documentation...
    """
    def __init__(self, ctx=uno.getComponentContext()):
        self.LocalContext = ctx
        self.ServiceManager = self.LocalContext.ServiceManager
        self.Toolkit = self.ServiceManager.createInstanceWithContext("com.sun.star.awt.ExtToolkit", self.LocalContext)

        # -----------------------------------------------------------
        #               Create dialog and insert controls
        # -----------------------------------------------------------

        # --------------create dialog container and set model and properties
        self.DialogContainer = self.ServiceManager.createInstanceWithContext("com.sun.star.awt.UnoControlDialog", self.LocalContext)
        self.DialogModel = self.ServiceManager.createInstance("com.sun.star.awt.UnoControlDialogModel")
        self.DialogContainer.setModel(self.DialogModel)
        self.DialogModel.Name = "langselect"
        self.DialogModel.PositionX = "135"
        self.DialogModel.PositionY = "91"
        self.DialogModel.Width = 208
        self.DialogModel.Height = 40
        self.DialogModel.Closeable = True
        self.DialogModel.Moveable = True
        self.DialogModel.Title = "Select translation languages"

        
        # --------- create an instance of ListBox control, set properties ---
        self.fr = self.DialogModel.createInstance("com.sun.star.awt.UnoControlListBoxModel")

        self.fr.Name = "from"
        self.fr.TabIndex = 0
        self.fr.PositionX = "15"
        self.fr.PositionY = "10"
        self.fr.Width = 44
        self.fr.Height = 15
        self.fr.Dropdown = True
        self.fr.LineCount= 15
        self.fr.StringItemList = langs
        self.fr.SelectedItems = (29, )

        # inserts the control model into the dialog model
        self.DialogModel.insertByName("from", self.fr)

        # --------- create an instance of ListBox control, set properties ---
        self.lang = self.DialogModel.createInstance("com.sun.star.awt.UnoControlListBoxModel")

        self.lang.Name = "lang"
        self.lang.TabIndex = 2
        self.lang.PositionX = "90"
        self.lang.PositionY = "10"
        self.lang.Width = 44
        self.lang.Height = 15
        self.lang.Dropdown = True
        self.lang.LineCount = 15
        self.lang.StringItemList = langs
        self.lang.SelectedItems = (31, )

        # inserts the control model into the dialog model
        self.DialogModel.insertByName("lang", self.lang)

        # --------- create an instance of FixedText control, set properties ---
        self.Label1 = self.DialogModel.createInstance("com.sun.star.awt.UnoControlFixedTextModel")

        self.Label1.Name = "Label1"
        self.Label1.TabIndex = 1
        self.Label1.PositionX = "70"
        self.Label1.PositionY = "11"
        self.Label1.Width = 13
        self.Label1.Height = 13
        self.Label1.Label = "➜"

        # inserts the control model into the dialog model
        self.DialogModel.insertByName("Label1", self.Label1)

        # --------- create an instance of Button control, set properties ---
        self.ok = self.DialogModel.createInstance("com.sun.star.awt.UnoControlButtonModel")

        self.ok.Name = "ok"
        self.ok.TabIndex = 3
        self.ok.PositionX = "160"
        self.ok.PositionY = "10"
        self.ok.Width = 35
        self.ok.Height = 17
        self.ok.Label = "OK"

        # inserts the control model into the dialog model
        self.DialogModel.insertByName("ok", self.ok)

        # add the action listener
        self.DialogContainer.getControl('ok').addActionListener(self)
        self.DialogContainer.getControl('ok').setActionCommand('ok_OnClick')

    def showDialog(self):
        self.DialogContainer.setVisible(True)
        self.DialogContainer.createPeer(self.Toolkit, None)
        self.DialogContainer.execute()

    # --------- functions -----------------------

    def myFunction(self):
        # TODO: not implemented
        pass

    # --------- helpers -------------------------

    def messageBox(self, MsgText, MsgTitle, MsgType=MESSAGEBOX, MsgButtons=BUTTONS_OK):
        sm = self.LocalContext.ServiceManager
        si = sm.createInstanceWithContext("com.sun.star.awt.Toolkit", self.LocalContext)
        mBox = si.createMessageBox(self.Toolkit, MsgType, MsgButtons, MsgTitle, MsgText)
        mBox.execute()

    # -----------------------------------------------------------
    #               Action events
    # -----------------------------------------------------------

    def actionPerformed(self, oActionEvent):
    
        if oActionEvent.ActionCommand == 'ok_OnClick':
            self.ok_OnClick()

    
    def ok_OnClick(self):
        self.fr = langs[self.fr.SelectedItems[0]]
        self.to = langs[self.lang.SelectedItems[0]]
        self.DialogContainer.endDialog(1)


def langselect(*args):

    try:
        ctx = remote_ctx                    # IDE
    except:
        ctx = uno.getComponentContext()     # UI

    app = LangSelection(ctx=ctx)
    app.showDialog()
    return app.fr, app.to

