# This file is part of the Coral project.
# Please read the copyright notice in the LICENSE file.

import qt
import string
from coral.modeler.unicode import QStringToFileName, QFileDialogToFilename, PyStringToFileName

import coral


def openProject(fileName=""):
    """Open a project stored in fileName."""
    if not fileName:
        dlg=qt.QFileDialog(None, "openProject-filedialog")
        XMIversions=coral.getSupportedXMIVersions()
        filters = ""
        for i in XMIversions:
            if filters: filters += ", "
            filters += "XMI " + i
        filters += ", Poseidon (*.xmi *.xml *.zuml)"
        dlg.setFilters(filters)
        dlg.setCaption("Load Project")
        dlg.show()
        acc = dlg.exec_loop()
        if not acc == qt.QDialog.Accepted:
            return
        _f = dlg.selectedFile()
        fileName = QFileDialogToFilename(_f)
    else:
        _f = fileName
        fileName = PyStringToFileName(fileName)

    if not fileName:
        return

    project=None
    coral.getTransactionManager().beginModifyCmd()
    try:
        project=coral.loadDocument(fileName)
    finally:                    
        coral.getTransactionManager().endModifyCmd()


    return project

def saveProject(project, xmiversion=None):
    """ Save project"""

    sf = None
    assert project
    
    if project:
        sf = project.getFilename()
        x = project.getXMIFormat()
        if x and not xmiversion:
            xmiversion = x

    if not xmiversion:
        xmiversion=coral.getSupportedXMIVersions()[-1]

    coral.getTransactionManager().beginModifyCmd()
    try:
        if not sf:
            saveProjectAs(project=project)
        else:
            assert project
            project.setFilename(sf)
            project.setXMIFormat(xmiversion)
            coral.saveDocument(project)

    finally:
        coral.getTransactionManager().endModifyCmd()
                
def saveProjectAs(newFileName="",xmiversion=None, project=None):
    assert project

    x = project.getXMIFormat()
    if x and not xmiversion:
        xmiversion = x

    if not xmiversion:
        xmiversion=coral.getSupportedXMIVersions()[-1]

    if not newFileName:
        dlg=qt.QFileDialog(None)
        filters=''
        selected = -1
        n = 0
        for s in coral.getSupportedXMIVersions():
            if xmiversion == s:
                selected = n
            n += 1
            if filters:
                filters += ";;XMI "+s+" (*.xmi *.xml)"
            else:
                filters="XMI "+s+" (*.xmi *.xml)"
        if selected == -1:
            selected = n - 1

        dlg.setFilters(filters)
        dlg.setSelectedFilter(selected)
        dlg.setMode(qt.QFileDialog.AnyFile)
        dlg.setCaption("Save Project")
        dlg.show()
        dlg.exec_loop()
        if dlg.result() == qt.QDialog.Rejected:
            return
        fn=QFileDialogToFilename(dlg.selectedFile())
        xmiversion=QStringToFileName(dlg.selectedFilter())[4:][:-14]
    else:
        fn=PyStringToFileName(newFileName)

    if not fn:
        return

    coral.getTransactionManager().beginModifyCmd()
    try:
        #fixing no extension
        _s = string.split(fn, ".")[-1]
        if fn[:6] != "smw://" and _s != "xmi" and _s != "xml":
            fn+=".xml"

        project.setFilename(fn)
        saveProject(project, xmiversion)
    finally:
        coral.getTransactionManager().endModifyCmd()
    return
