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

import ConfigParser, os, sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import uic
from PyKDE4.kdecore import *
from PyKDE4.kdeui import *

smolt_user_config_dir = '~/.smolt/'
smolt_user_config_file = 'ksmolt.cfg'

class App(QWidget):

	def __init__(self):
		QWidget.__init__(self)
		self.init()


	# check whether '/var/run/smolt_do_opensuse_run' which is created by a smolt update exists 
	def init(self):
		if os.path.exists('/var/run/smolt_do_opensuse_run') and self.readConfigShowPopup() == 'true':
			self.popup()
		else:
			QTimer.singleShot(0, self.slotQuit)


	# check whether the user has clicked "Don't ask again" in the past
	def readConfigShowPopup(self):
		conf = ConfigParser.ConfigParser()
		conf.read(os.path.expanduser(os.path.join(smolt_user_config_dir, smolt_user_config_file)))
		try:
			value = conf.get("Popup","showPopup")
		except:
			value = 'true'
		return value


	# remember not to show the popup again
	def writeConfigDontShow(self):
		if not os.path.exists(os.path.expanduser(smolt_user_config_dir)):
			os.mkdir(os.path.expanduser(smolt_user_config_dir), 0700)
		conf = ConfigParser.RawConfigParser()
		conf.add_section("Popup")
		conf.set("Popup", "showPopup", "false")
		with open(os.path.expanduser(os.path.join(smolt_user_config_dir, smolt_user_config_file)), "wb") as configfile:
			conf.write(configfile)


	# show popup
	def popup(self):
		self.notification = KNotification ( "ksmolt", self, KNotification.Persistent );
		self.notification.setText( i18n("Driver development is prioritized based on hardware popularity.<br>"
																		"Please send your system profile to influence this work.") )
		self.actions = QStringList()
		self.actions << i18n( "Send now" )
		self.actions << i18n( "Don't ask again" )
		self.notification.setActions( self.actions )
		self.connect( self.notification, SIGNAL( "activated( unsigned int )" ), self.slotActivated )
		self.connect( self.notification, SIGNAL( "closed()"), self.slotQuit )
		self.notification.sendEvent()


	# user clicked the popup
	def slotActivated( self, action ):
		if action == 1:
			KProcess.startDetached( "smoltGui" )
		self.writeConfigDontShow()
		self.slotQuit()


	# quit application
	def slotQuit( self ):
		self.close()


if __name__ == "__main__":
	appName     = "ksmolt"
	catalogue   = "ksmolt"
	programName = ki18n( "KSmolt" )
	version     = "0.2.2"
	description = ki18n( "System Survey Tool" )
	license     = KAboutData.License_GPL
	copyright   = ki18n("2009 SUSE Linux Products GmbH")
	text        = KLocalizedString()
	homePage    = "http://www.opensuse.org"
	bugEmail    = ""

	aboutData   = KAboutData( appName, catalogue, programName, version, description,
														license, copyright, text, homePage, bugEmail )

	KCmdLineArgs.init(sys.argv, aboutData)

	app = KApplication()
	applet = App() 
	sys.exit( app.exec_() )
