#!/usr/bin/python3

import sys
import os
import subprocess
import psutil
import gettext

gettext.textdomain("dpkg-unlocker")
_ = gettext.gettext


def isDpkgUnlockerRunning():

	lockToken="/var/run/dpkgUnlocker.lock"
	if os.path.exists(lockToken):
		try:
			with open(lockToken,'r') as fd:
				unlockerPid=fd.readline().strip()

			if unlockerPid:
				if psutil.pid_exists(int(unlockerPid)):
					print ("  [Dpkg-Unlocker-Gui]: Dpkg-Unlocker is now running ")
					msg=_("Dpkg-Unlocker is now running. Wait a moment and try again.")
					try:
						subprocess.run(["kdialog","--title","Dpkg-Unlocker","--icon","dpkg-unlocker-gui","--sorry",msg],timeout=10)
					except Exception as e:
						pass
					sys.exit(1)

			os.remove(lockToken)	

		except (ValueError,OSError):
			print("  [Dpkg-Unlocker-Gui: Dpkg-Unlocker is now running. Wait a moment and try again")
			sys.exit(1)		

#def isDpkgUnlockedRunning

if os.geteuid() != 0:
	print("  [Dpkg-Unlocker-Gui]: No administration privileges")
	msg=_("You need administration privileges to run this application.")
	try:
		subprocess.run(["kdialog","--title","Dpkg-Unlocker","--icon","dpkg-unlocker-gui","--sorry",msg],timeout=10)
	except Exception as e:
		pass
	sys.exit(1)

isDpkgUnlockerRunning()

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

from dpkgunlockergui.Core import Core

if os.geteuid()==0:
	os.environ["XDG_CURRENT_DESKTOP"]="KDE"

icons_paths=["/usr/share/icons","/usr/local/share/icons"]
QIcon.setThemeName("breeze")

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(app.quit)
app.aboutToQuit.connect(serviceStackBridge.stopServices)
ret=app.exec()
del engine
del app
sys.exit(ret)

