from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.properties import ObjectProperty
from sleekxmpp import ClientXMPP
from kivy.uix.textinput import TextInput
from kivy.uix.modalview import ModalView
from kivy.uix.label import Label
from sleekxmpp.exceptions import XMPPError
from sleekxmpp.jid import InvalidJID
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.uix.listview import ListItemButton
import datetime
from kivy.utils import escape_markup
from kivy.core.audio import SoundLoader


__version__ = 1.0

class Orkiv(App):
    icon = 'customicon.png'

    def __init__(self, root_dir):
        super(Orkiv, self).__init__()
        self.root_dir = root_dir
        self.xmpp = None

    def connect_to_jabber(self, jabber_id, password):
        self.xmpp = ClientXMPP(jabber_id, password)
        self.xmpp.reconnect_max_attempts = 1
        connected = self.xmpp.connect()
        if not connected:
            raise XMPPError("Vyanse kwikonecta")
        self.xmpp.process()
        self.xmpp.send_presence()
        self.xmpp.get_roster()
        self.xmpp.add_event_handler('message', self.root.handle_xmpp_message)

    def disconnect_xmpp(self):
        if self.xmpp and self.xmpp.state.ensure("connected"):
            self.xmpp.abort()
        self.xmpp = None