#!/usr/bin/python

"""
This file is part of qnotero.

qnotero is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

qnotero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with qnotero.  If not, see <http://www.gnu.org/licenses/>.
"""

if __name__ == "__main__":

	# The listener will fail if another instance of Qnotero is already running.
	# In that case we send an activate signal, to pop up the Qnotero window, and
	# exit.
	import sys
	from libqnotero.listener import Listener
	try:
		if "--notray" not in sys.argv:
			listener = Listener()
		else:
			listener = None
	except:
		from libqnotero.config import getConfig
		import socket
		print "qnotero: Qnotero already running, sending activate signal"
		sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
		sock.sendto("activate", ("localhost", getConfig("listenerPort")) )
		sys.exit()

	# Normal start of Qnotero
	from libqnotero.qnotero import Qnotero
	from PyQt4.QtGui import QApplication
	reset = "--reset" in sys.argv
	systray = "--notray" not in sys.argv
	app = QApplication(sys.argv)
	app.setQuitOnLastWindowClosed(False)
	qnotero = Qnotero(systray=systray, debug=True, reset=reset)
	qnotero.listener = listener
	if not systray:
		qnotero.popUp()
	if listener != None:
		listener.qnotero = qnotero
		listener.start()
	sys.exit(app.exec_())

