#!/usr/bin/python3

from PySide6.QtWidgets import QApplication
from PySide6.QtCore import QUrl
from PySide6.QtGui import QIcon
from PySide6.QtQml import QQmlApplicationEngine

import sys
import os
import gettext
gettext.textdomain("dpkg-unlocker")
_ = gettext.gettext


if os.path.exists('/var/run/dpkgUnlocker.lock'):
	print ("  [Dpkg-Unlocker-Gui]: Dpkg-Unlocker is now running ")
	msg=_("Dpkg-Unlocker is now running. Wait a moment and try again.")
	cmd="kdialog --title 'Dpkg-Unlocker' --icon 'dpkg-unlocker-gui' --sorry '%s'"%(msg)
	os.system(cmd)
	sys.exit(1)

else:
	try:
		print("  [Dpkg-Unlocker-Gui]: Checking root")
		f=open("/var/run/DpkgUnlocker.token","w")
		f.close()
		os.remove("/var/run/DpkgUnlocker.token")

	except:
		print("  [Dpkg-Unlocker-Gui]: No administration privileges")
		msg=_("You need administration privileges to run this application.")
		cmd='kdialog --title "Dpkg-Unlocker" --icon "dpkg-unlocker-gui" --sorry "%s"'%(msg)
		
		print(cmd)
		os.system(cmd)
		sys.exit(1)


from dpkgunlockergui.Core import Core
c=Core.get_core()

app = QApplication()
app.setDesktopFileName("dpkg-unlocker-gui")
engine = QQmlApplicationEngine()
engine.clearComponentCache()
context=engine.rootContext()
mainStackBridge=c.mainStack
serviceStackBridge=c.serviceStack
restoreStackBridge=c.restoreStack
protectionStackBridge=c.protectionStack

context.setContextProperty("mainStackBridge", mainStackBridge)
context.setContextProperty("serviceStackBridge", serviceStackBridge)
context.setContextProperty("restoreStackBridge", restoreStackBridge)
context.setContextProperty("protectionStackBridge", protectionStackBridge)

url = QUrl("/usr/lib/python3/dist-packages/dpkgunlockergui/rsrc/dpkg-unlocker.qml")

engine.load(url)
if not engine.rootObjects():
	sys.exit(-1)

engine.quit.connect(QApplication.quit)
ret=app.exec()
del engine
del app
sys.exit(ret)

