#! /usr/bin/env python

##############################################################################
#
# Mnemosyne <Peter.Bienstman@UGent.be>
#
##############################################################################

import sys, os
from qt import *
from mnemosyne.pyqt_ui.main_dlg import *
from mnemosyne.core import *

# Create main widget.

try:

    # We need the absolute path for the file name: It will be stored in the
    # user's configuration and shall also point to the right location when
    # Mnemosyne is started from a different directory next time.

    filename = os.path.abspath(sys.argv[1])

except:
    filename = None

a = QApplication(sys.argv)

# Check if there is another instance of Mnemosyne running.

basedir = os.path.join(os.path.expanduser("~"), ".mnemosyne")
if os.path.exists(os.path.join(basedir,"MNEMOSYNE_LOCK")):
    status = QMessageBox.warning(None,
             a.trUtf8("Mnemosyne"),
             a.trUtf8("Another running copy of Mnemosyne was detected.\n" +\
                      "Starting a new copy could lead to data loss!\n"),
             a.trUtf8("&Exit"), a.trUtf8("&Continue"),
             QString(), 0, -1)
    if status == 0 or status == -1:
        sys.exit()

# Start program.

import pygame
pygame.mixer.init()

initialise()
            
w = MainDlg(filename)

QObject.connect(a,SIGNAL("lastWindowClosed()"),w.fileExit)
QObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT("quit()"))

a.setMainWidget(w)
w.show()

if get_config("first_run") == True:
    w.productTour()
    set_config("first_run", False)
    
a.exec_loop()

finalise()

