# -*- coding: utf-8 -*-

# Copyright (c) 2004 - 2007 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Compatibility module to use the KDE File Dialog instead of the Qt File Dialog.
"""

import os

import Preferences

try:
    raise ImportError
except (ImportError, RuntimeError):
    os.environ["e4nokde"] = "1"
    
import PyQt4.QtGui

def __qtSetIconProvider(provider):
    """
    Function to set the icon provider.
    
    @param provider QFileIconProvider
    """
    global __iconProvider
    __iconProvider = provider
    
def __qtIconProvider():
    """
    Function to return the previously stored icon provider.
    
    @return QFileIconProvider
    """
    global __iconProvider
    return __iconProvider

__qtGetOpenFileName = PyQt4.QtGui.QFileDialog.getOpenFileName
__qtGetOpenFileNames = PyQt4.QtGui.QFileDialog.getOpenFileNames
__qtGetSaveFileName = PyQt4.QtGui.QFileDialog.getSaveFileName
__qtGetExistingDirectory = PyQt4.QtGui.QFileDialog.getExistingDirectory

################################################################################

from PyQt4.QtCore import QString, QStringList, QDir
from PyQt4.QtGui import QFileDialog

def getOpenFileName(parent = None, caption = QString(), dir_ = QString(),
                    filter = QString(), selectedFilter = None, 
                    options = QFileDialog.Options()):
    """
    Module function to get the name of a file for opening it.
    
    @param parent parent widget of the dialog (QWidget)
    @param caption window title of the dialog (QString)
    @param dir_ working directory of the dialog (QString)
    @param filter filter string for the dialog (QString)
    @param selectedFilter selected filter for the dialog (QString)
    @param options various options for the dialog (QFileDialog.Options)
    @return name of file to be opened (QString)
    """
    if Preferences.getUI("UseKDEDialogs") and not os.getenv("e4nokde"):
        f = __kdeGetOpenFileName(parent, caption, dir_, 
                                    filter, selectedFilter, options)
    else:
        f = __qtGetOpenFileName(parent, caption, dir_, 
                                   filter, selectedFilter, options)
    return QDir.toNativeSeparators(f)
    
def getOpenFileNames(parent = None, caption = QString(), dir_ = QString(),
                    filter = QString(), selectedFilter = None, 
                    options = QFileDialog.Options()):
    """
    Module function to get a list of names of files for opening.
    
    @param parent parent widget of the dialog (QWidget)
    @param caption window title of the dialog (QString)
    @param dir_ working directory of the dialog (QString)
    @param filter filter string for the dialog (QString)
    @param selectedFilter selected filter for the dialog (QString)
    @param options various options for the dialog (QFileDialog.Options)
    @return list of filenames to be opened (QStringList)
    """
    if Preferences.getUI("UseKDEDialogs") and not os.getenv("e4nokde"):
        l = __kdeGetOpenFileNames(parent, caption, dir_, 
                                     filter, selectedFilter, options)
    else:
        l =  __qtGetOpenFileNames(parent, caption, dir_, 
                                    filter, selectedFilter, options)
    fl = QStringList()
    for f in l:
        fl.append(QDir.toNativeSeparators(f))
    return fl
    
def getSaveFileName(parent = None, caption = QString(), dir_ = QString(),
                    filter = QString(), selectedFilter = None, 
                    options = QFileDialog.Options()):
    """
    Module function to get the name of a file for saving it.
    
    @param parent parent widget of the dialog (QWidget)
    @param caption window title of the dialog (QString)
    @param dir_ working directory of the dialog (QString)
    @param filter filter string for the dialog (QString)
    @param selectedFilter selected filter for the dialog (QString)
    @param options various options for the dialog (QFileDialog.Options)
    @return name of file to be saved (QString)
    """
    if Preferences.getUI("UseKDEDialogs") and not os.getenv("e4nokde"):
        f = __kdeGetSaveFileName(parent, caption, dir_, 
                                    filter, selectedFilter, options)
    else:
        f = __qtGetSaveFileName(parent, caption, dir_, 
                                   filter, selectedFilter, options)
    return QDir.toNativeSeparators(f)
    
def getExistingDirectory(parent = None, caption = QString(), dir_ = QString(),
                         options = QFileDialog.Options(QFileDialog.ShowDirsOnly)):
    """
    Module function to get the name of a directory.
    
    @param parent parent widget of the dialog (QWidget)
    @param caption window title of the dialog (QString)
    @param dir_ working directory of the dialog (QString)
    @param options various options for the dialog (QFileDialog.Options)
    @return name of selected directory (QString)
    """
    if Preferences.getUI("UseKDEDialogs") and not os.getenv("e4nokde"):
        d = __kdeGetExistingDirectory(parent, caption, dir_, options)
    else:
        d = __qtGetExistingDirectory(parent, caption, dir_, options)
    return QDir.toNativeSeparators(d)
    
def setIconProvider(provider):
    """
    Function to set the icon provider.
    
    @param provider QFileIconProvider
    """
    if Preferences.getUI("UseKDEDialogs") and not os.getenv("e4nokde"):
        __kdeSetIconProvider(provider)
    else:
        __qtSetIconProvider(provider)
    
def iconProvider():
    """
    Function to return the previously stored icon provider.
    
    @return QFileIconProvider
    """
    if Preferences.getUI("UseKDEDialogs") and not os.getenv("e4nokde"):
        return __kdeIconProvider()
    else:
        return __qtIconProvider()
