# Gtk::FileFilter.add\_custom

**URL:** <https://www.ruby-forum.com/t/gtk-filefilter-add-custom/211031>\
**Category:** Ruby-Gnome 2\
**Created:** [August 24, 2011, 12:45am UTC](https://www.ruby-forum.com/t/gtk-filefilter-add-custom/211031 "2011-08-24T00:45:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [August 24, 2011, 12:45am UTC](https://www.ruby-forum.com/t/gtk-filefilter-add-custom/211031/1 "2011-08-24T00:45:39Z")

</div>

If I read the doc write this should work…

```auto
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?

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [August 24, 2011, 2:19pm UTC](https://www.ruby-forum.com/t/gtk-filefilter-add-custom/211031/2 "2011-08-24T14:19:27Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [August 24, 2011, 2:28pm UTC](https://www.ruby-forum.com/t/gtk-filefilter-add-custom/211031/3 "2011-08-24T14:28:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [August 24, 2011, 3:14pm UTC](https://www.ruby-forum.com/t/gtk-filefilter-add-custom/211031/4 "2011-08-24T15:14:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [August 24, 2011, 2:30pm UTC](https://www.ruby-forum.com/t/gtk-filefilter-add-custom/211031/5 "2011-08-24T14:30:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [August 24, 2011, 3:42pm UTC](https://www.ruby-forum.com/t/gtk-filefilter-add-custom/211031/6 "2011-08-24T15:42:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [August 24, 2011, 3:56pm UTC](https://www.ruby-forum.com/t/gtk-filefilter-add-custom/211031/7 "2011-08-24T15:56:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [August 24, 2011, 7:23pm UTC](https://www.ruby-forum.com/t/gtk-filefilter-add-custom/211031/8 "2011-08-24T19:23:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [August 24, 2011, 5:17pm UTC](https://www.ruby-forum.com/t/gtk-filefilter-add-custom/211031/9 "2011-08-24T17:17:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [August 26, 2011, 4:41pm UTC](https://www.ruby-forum.com/t/gtk-filefilter-add-custom/211031/10 "2011-08-26T16:41:59Z")

</div>

Thanks Kouhei S.!

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [August 25, 2011, 2:56pm UTC](https://www.ruby-forum.com/t/gtk-filefilter-add-custom/211031/11 "2011-08-25T14:56:45Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“[ruby-gnome2-devel-en] Gtk::FileFilter.add\_custom” on Wed, 24 Aug  
2011 00:45:39 +0200,  
Jeff M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
