# Copyright (c) 2000-2001 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 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, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

"""DirectorySelector module."""

__revision__ = "$Id: DirectorySelector.py,v 1.7 2001/09/17 14:11:52 alf Exp $"

from narval.gui import unicode_to_gtk
from gtk import *
import os

class DirectorySelector(GtkDialog):
    """DirectorySelector"""
    
    def __init__(self, initialdir = None):
        GtkDialog.__init__(self)
        self.set_position(WIN_POS_CENTER)
        self.dirList = GtkCList(1,[unicode_to_gtk(_('Directories'))])
        self.dirList.set_usize(200,210)
        self.dirList.connect('button-press-event',self.list_clicked)
        scroll = GtkScrolledWindow()
        scroll.set_policy(POLICY_AUTOMATIC,POLICY_AUTOMATIC)
        scroll.add(self.dirList)
        self.vbox.pack_start(scroll)
        self.vbox.pack_start(GtkLabel(unicode_to_gtk(_('Selection'))),expand=0)
        self.selectionEntry = GtkEntry()
        self.selectionEntry.connect('activate',self.path_edited)
        self.vbox.pack_start(self.selectionEntry,expand=0)

        self.ok_button = GtkButton(unicode_to_gtk(_('OK')))
        self.ok_button.set_flags(CAN_DEFAULT|HAS_DEFAULT)
        self.cancel_button = GtkButton(unicode_to_gtk(_('Cancel')))
        self.cancel_button.set_flags(CAN_DEFAULT)                   
        self.action_area.pack_start(self.ok_button)
        self.action_area.pack_start(self.cancel_button)

        self.cancel_button.connect_object_after('clicked',lambda x:x.destroy(),self)

        self.dir = initialdir or os.getcwd()
        self.update_list()

    def __getattr__(self, attr):
        attrs = ['dirList','selectionEntry','ok_button','cancel_button']
        if attr in attrs:
            return self.__dict__[attr]
        return GtkDialog.__getattr__(self, attr)
    

    def update_list(self):
        try:
            content = filter(lambda x,d=self.dir:os.path.isdir(os.path.join(d,x)),
                             os.listdir(self.dir))
        except Exception,e:
            print e
            content = []
        if self.dir != '/':
            content = ['..']+ content

        content.sort()

        self.dirList.freeze()
        self.dirList.clear()
        for d in content:
            self.dirList.append([d+'/'])
        self.dirList.thaw()
        self.selectionEntry.set_text(self.dir)

    def list_clicked(self,w,event):
        if event.button == 1 and \
           event.type == GDK._2BUTTON_PRESS and \
           self.dirList.selection:
            sel = self.dirList.get_text(self.dirList.selection[0],0)
            self.dir = os.path.normpath(os.path.join(self.dir,sel))
            self.update_list()
            self.dirList.unselect_all()

    def path_edited(self,w):
        try:
            os.listdir(self.selectionEntry.get_text())
        except Exception,e:
            print e
        else:
            self.dir = self.selectionEntry.get_text()
            self.update_list()


# main (test) ##################################################################

if __name__ == "__main__" :
    dlg = DirectorySelector()
    dlg.show_all()
    dlg.connect('delete_event',lambda w,e:mainquit())
    mainloop()
