Gtk::FileFilter.add_custom

If I read the doc write this should work…

filefilter1 = Gtk::FileFilter.new
filefilter1.add_custom(Gtk::FileFilter::FILENAME) do |c,filename,u,d,m|
  File.directory? f
end

filechooserwidget1.add_filter filefilter1

and it’s good until it gets into the main loop where it segfaults.

filefilter.add_pattern(’*.rb’) for example, works just fine.

Am I missing something obvious or worse yet, something not obvious?

Can you paste a sample script here which segfaults?

For others who read this here, here is the link to the API docu:

http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3AFileFilter#add_custom

I copy paste quickly:

add_custom(needed){|contains, filename, uri, display_name, mime_type|
…}
Adds rule to a filter that allows files based on a custom callback
block. The bitfield needed which is passed in provides information about
what sorts of information that the filter block needs; this allows GTK+
to avoid retrieving expensive information when it isn’t needed by the
filter. Since 2.4

    needed: bitfield of flags indicating the information that the 

custom filter block needs. (GtkFileFilterFlags).
{|contains, filename, uri, display_name, mime_type| …}:
callback block; if the block returns true, then the file will be
displayed.
contains: Flags indicating which of the following fields
need are filled
filename: the filename of the file being tested
uri: the URI for the file being tested
display_name: the string that will be used to display the
file in the file chooser
mime_type: the mime type of the file
Returns: self

needed

Btw:

filefilter1.add_custom(Gtk::FileFilter::FILENAME) do |c,filename,u,d,m|
File.directory? f
end

Where does f come from?

I think you must check that all the variables you pass in the block are
valid. You probably meant filename, not f. When you rename block
variables, make sure the variables inside the block also refer to the
same name.

hey folks,

interesting, i also get a seg fault… do you need to use
#add_custom ? i’ve used #add_pattern with success, but it doesn’t take
a block, so it might not fit your needs…

audioFilter = Gtk::FileFilter.new
audioFilter.add_pattern("*.mp3")

dialog.add_filter(audioFilter)

# dialog is a Gtk::FileChooserDialog
  • j

Oh, odd. I also get a segfault …

per.rb:68: [BUG] Segmentation fault
ruby 1.8.7 (2011-02-18 patchlevel 334) [x86_64-linux]

Aborted

Perhaps add_custom is buggy? Hmmm.

Marc H. wrote in post #1018229:

Btw:

filefilter1.add_custom(Gtk::FileFilter::FILENAME) do |c,filename,u,d,m|
File.directory? f
end

Where does f come from?

I think you must check that all the variables you pass in the block are
valid. You probably meant filename, not f. When you rename block
variables, make sure the variables inside the block also refer to the
same name.

You caught the transcription failure. Sorry. The code I’m using is
correct.

jake kaiden wrote in post #1018242:

hey folks,

interesting, i also get a seg fault… do you need to use
#add_custom ? i’ve used #add_pattern with success, but it doesn’t take
a block, so it might not fit your needs…

audioFilter = Gtk::FileFilter.new
audioFilter.add_pattern("*.mp3")

dialog.add_filter(audioFilter)

# dialog is a Gtk::FileChooserDialog
  • j

Unfortunately, I’m really interested in selecting directories to be
added to a list for subsequent scanning. Since the directories involved
are arbitrarily named, patterns just don’t work as a selection filter.

I’m certain there are other ways to do this so I can create a
work-around but I appreciate the time you all have taken to confirm that
there is indeed a bug.

For the record…

Linux 2.6.32-33-generic #72-Ubuntu SMP Fri Jul 29 21:07:13 UTC 2011
x86_64 GNU/Linux

ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]

Ruby-GNOME-1.0.0

Again, thanks for confirming.

djief

jake kaiden wrote in post #1018268:

if so, i do something like this in my audio player…

# (`dialog` is a FileChooserDialog...)

selection = dialog.filename

if File.directory?(selection)
    Gtk::FileChooser::ACTION_SELECT_FOLDER
    ## do directory stuff ##
else
    Gtk::FileChooser::ACTION_OPEN
    ## do file stuff ##
end

…if not, don’t know what to tell ya! good luck…

  • j

Thanks for the suggestion…
Looks like it might work for me…
I’ll have to knock off a prototype.

djief

Jeff M. wrote in post #1018255:

Unfortunately, I’m really interested in selecting directories to be
added to a list for subsequent scanning. Since the directories involved
are arbitrarily named, patterns just don’t work as a selection filter.

judging from your first post…

filefilter1 = Gtk::FileFilter.new
filefilter1.add_custom(Gtk::FileFilter::FILENAME) do |c,filename,u,d,m|
File.directory? f

…it looks like you’re trying to determine whether a selection made
from a file chooser widget is a file or a directory… is this the
case?

if so, i do something like this in my audio player…

# (`dialog` is a FileChooserDialog...)

selection = dialog.filename

if File.directory?(selection)
    Gtk::FileChooser::ACTION_SELECT_FOLDER
    ## do directory stuff ##
else
    Gtk::FileChooser::ACTION_OPEN
    ## do file stuff ##
end

…if not, don’t know what to tell ya! good luck…

  • j

Thanks Kouhei S.!

Hi,

In [email protected]
“[ruby-gnome2-devel-en] Gtk::FileFilter.add_custom” on Wed, 24 Aug
2011 00:45:39 +0200,
Jeff M. [email protected] wrote:

and it’s good until it gets into the main loop where it segfaults.

filefilter.add_pattern(‘*.rb’) for example, works just fine.

I’ve fixed it in trunk.
Thanks for your report.

Thanks,

kou