File: FileSelector.py

package info (click to toggle)
pybliographer 1.2.12-4squeeze1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,560 kB
  • ctags: 1,413
  • sloc: python: 9,817; xml: 2,560; sh: 813; makefile: 621
file content (130 lines) | stat: -rw-r--r-- 3,398 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# This file is part of pybliographer
# 
# Copyright (C) 1998-2004 Frederic GOBRY
# Email : gobry@pybliographer.org
# 	   
# 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.
# 
# 

import string, os, urlparse

from gnome import ui

import gtk

from Pyblio import Open, Types, Base, Fields, Config, Autoload

from Pyblio.GnomeUI import Utils


class URLFileSelection (gtk.FileChooserDialog):
    ''' Extended file selection dialog, with an URL field and a type
    selector. '''

    defaultdir = None
    
    def __init__(self, title = _("File"),
                 modal = True, has_auto = True, is_save = False,
                 directory = None, show_type=True):

        gtk.FileChooserDialog.__init__ (self)

        accelerator = gtk.AccelGroup ()
        self.add_accel_group (accelerator)

        b = self.add_button (gtk.STOCK_OK, gtk.RESPONSE_OK)
        b.add_accelerator ('clicked', accelerator, gtk.keysyms.Return, 0, 0)

        b = self.add_button (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT)
        b.add_accelerator ('clicked', accelerator, gtk.keysyms.Escape, 0, 0)

        if is_save:
            self.set_action(gtk.FILE_CHOOSER_ACTION_SAVE)

        self.set_local_only (False)

        self.set_title (title)
        
        if directory:
            self.set_current_folder (directory)
            
        elif self.defaultdir:
            self.set_current_folder (self.defaultdir)
        
            
        self.ret = None
	self.ftype = None
	
	if show_type:
	    # type selector
	    hbox = gtk.HBox ()
	    hbox.set_spacing (5)
	    hbox.set_border_width (5)
	    hbox.pack_start (gtk.Label (_("Bibliography type:")),
			     expand = False, fill = False)

	    self.menu = gtk.combo_box_new_text ()

	    hbox.pack_start (self.menu)

	    self.set_extra_widget (hbox)

	    # menu content

	    liste = Autoload.available ('format')
	    liste.sort ()

	    self.formats = []

	    if has_auto:
		self.menu.append_text (_(' - According to file suffix - '))
		self.ftype = None
                self.formats.append(None)
                
	    else:
		self.ftype = liste [0]

	    for avail in liste:
		self.menu.append_text (avail)

	    self.formats += liste

	    self.menu.set_active (0)
	    self.menu.connect ("changed", self.menu_select)

	    hbox.show_all ()
        return


    def menu_select (self, widget):
        self.ftype = self.formats [widget.get_active ()]
        return
        

    def run (self):
        ret = gtk.FileSelection.run (self)

        file = self.get_filename ()
        self.destroy ()

        if ret != gtk.RESPONSE_OK: return (None, None)
        
        URLFileSelection.defaultdir = os.path.dirname (file)
            
        return (file, self.ftype)