#!/usr/bin/python2.7
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# Copyright (C) 2011 Owais Lone hello@owaislone.org
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, 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 this program.  If not, see <http://www.gnu.org/licenses/>.
### END LICENSE

import os
import sys
from gi.repository import Gio, Gtk, GObject

Gtk.init(sys.argv)
GObject.threads_init()

# First check for running instances, only then move forward.
def activate_application(application):
    try:
        mainapp.present()
    except NameError:
        pass


g_app = Gio.Application.new('org.owaislone.gmailwatcher', Gio.ApplicationFlags.FLAGS_NONE)
g_app.connect('activate', activate_application)
g_app.run(sys.argv)
if g_app.get_is_remote():
    #Quit if another instace is running.
    sys.stdout.write('Already Running!')
    sys.exit()


# Start the application now
# Setup project paths
sys.path.insert(0, '/opt/owaislone')
CURRENT_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if not CURRENT_PATH.startswith('/usr'):
    sys.path.insert(0, CURRENT_PATH)

from gmailwatcher.app.main_window import MainApp
from gmailwatcher.lib import imap4_utf7

loop = GObject.MainLoop()
mainapp = MainApp(main_loop=loop, args=sys.argv)
mainapp.run()

