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

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

"""
Module implementing a dialog to add a file to the project.
"""

import os

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from KdeQt import KQFileDialog

from E4Gui.E4Completers import E4DirCompleter

from Ui_AddFileDialog import Ui_AddFileDialog

import Utilities

class AddFileDialog(QDialog, Ui_AddFileDialog):
    """
    Class implementing a dialog to add a file to the project.
    """
    def __init__(self, pro, parent = None, filter = None, name = None,
                 startdir = None):
        """
        Constructor
        
        @param pro reference to the project object
        @param parent parent widget of this dialog (QWidget)
        @param filter filter specification for the file to add (string or QString)
        @param name name of this dialog (string or QString)
        @param startdir start directory for the selection dialog
        """
        QDialog.__init__(self, parent)
        if name:
            self.setObjectName(name)
        self.setupUi(self)
        
        self.targetDirCompleter = E4DirCompleter(self.targetDirEdit)
        
        self.targetDirEdit.setText(pro.ppath)
        self.filter = filter
        self.ppath = pro.ppath
        self.startdir = startdir
        if pro.pdata["MIXEDLANGUAGE"][0]:
            self.sourceFilter = QString(pro.sourceFilters["Mixed"])
        else:
            self.sourceFilter = QString(pro.sourceFilters[pro.pdata["PROGLANGUAGE"][0]])
        
    @pyqtSignature("")
    def on_targetDirButton_clicked(self):
        """
        Private slot to display a directory selection dialog.
        """
        startdir = self.targetDirEdit.text()
        if startdir.isEmpty() and self.startdir is not None:
            startdir = self.startdir
        directory = KQFileDialog.getExistingDirectory(\
            self,
            self.trUtf8("Select target directory"),
            startdir,
            QFileDialog.Options(QFileDialog.Option(0)))
            
        if not directory.isNull():
            self.targetDirEdit.setText(Utilities.toNativeSeparators(directory))
        
    @pyqtSignature("")
    def on_sourceFileButton_clicked(self):
        """
        Private slot to display a file selection dialog.
        """
        dir = self.sourceFileEdit.text().section(os.pathsep, 0, 0, 
                    QString.SectionFlags(QString.SectionSkipEmpty))
        if dir.isEmpty():
            if self.startdir is not None:
                dir = self.startdir
            else:
                dir = self.targetDirEdit.text()
        if self.filter is None:
            dfilter = QString(self.sourceFilter)
            dfilter.append(self.trUtf8(\
                "Qt User-Interface Files (*.ui);;"
                "Qt Resource Files (*.qrc);;"
                "Interface Files (*.idl);;"
                "Qt Translation Files (*.ts *.qm);;"
                "All Files (*)"
            ))
            caption = self.trUtf8("Select Files")
        elif self.filter == 'form':
            dfilter = self.trUtf8("Qt User-Interface Files (*.ui)")
            caption = self.trUtf8("Select user-interface files")
        elif self.filter == "resource":
            dfilter = self.trUtf8("Qt Resource Files (*.qrc)")
            caption = self.trUtf8("Select resource files")
        elif self.filter == 'source':
            dfilter = QString(self.sourceFilter)
            dfilter.append(self.trUtf8(\
                "All Files (*)"
            ))
            caption = self.trUtf8("Select source files")
        elif self.filter == 'interface':
            dfilter = self.trUtf8("Interface Files (*.idl)")
            caption = self.trUtf8("Select interface files")
        elif self.filter == 'translation':
            dfilter = self.trUtf8("Qt Translation Files (*.ts *.qm)")
            caption = self.trUtf8("Select translation files")
        elif self.filter == 'others':
            dfilter = self.trUtf8("All Files (*)")
            caption = self.trUtf8("Select files")
        else:
            return
            
        fnames = KQFileDialog.getOpenFileNames(\
            self, caption, dir, dfilter)
            
        if len(fnames):
            self.sourceFileEdit.setText(Utilities.toNativeSeparators(\
                fnames.join(os.pathsep)))
        
    @pyqtSignature("QString")
    def on_sourceFileEdit_textChanged(self, sfile):
        """
        Private slot to handle the source file text changed.
        
        If the entered source directory is a subdirectory of the current
        projects main directory, the target directory path is synchronized.
        It is assumed, that the user wants to add a bunch of files to
        the project in place.
        
        @param sfile the text of the source file line edit
        """
        sfile = sfile.section(os.pathsep, 0, 0,
                    QString.SectionFlags(QString.SectionSkipEmpty))
        if sfile.startsWith(self.ppath):
            dir = os.path.dirname(unicode(sfile))
            self.targetDirEdit.setText(dir)
        
    def getData(self):
        """
        Public slot to retrieve the dialogs data.
        
        @return tuple of three values (list of string, string, boolean) giving the 
            source files, the target directory and a flag telling, whether
            the files shall be added as source code
        """
        return (unicode(self.sourceFileEdit.text()).split(os.pathsep), 
            unicode(self.targetDirEdit.text()), self.sourcecodeCheckBox.isChecked())
