Forum: Ruby-Gnome 2 Gtk::FileFilter.add_custom

Posted by Jeff Moore (djief)
on 2011-08-24 00:45
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?
Posted by Marc Heiler (shevegen)
on 2011-08-24 14:19
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%...


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
Posted by Marc Heiler (shevegen)
on 2011-08-24 14:28
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.
Posted by Marc Heiler (shevegen)
on 2011-08-24 14:30
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.
Posted by jake kaiden (lljk)
on 2011-08-24 15:14
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
Posted by Jeff Moore (djief)
on 2011-08-24 15:42
Marc Heiler 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.
Posted by Jeff Moore (djief)
on 2011-08-24 15:56
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
Posted by jake kaiden (lljk)
on 2011-08-24 17:17
Jeff Moore 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
Posted by Jeff Moore (djief)
on 2011-08-24 19:23
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
Posted by Kouhei Sutou (Guest)
on 2011-08-25 14:56
(Received via mailing list)
Hi,

In <395be299878293e5fc27ce41e193f87c@ruby-forum.com>
  "[ruby-gnome2-devel-en] Gtk::FileFilter.add_custom" on Wed, 24 Aug 
2011 00:45:39 +0200,
  Jeff Moore <ruby-forum-incoming@andreas-s.net> 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
Posted by Marc Heiler (shevegen)
on 2011-08-26 16:41
Thanks Kouhei Sutou!
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.