This is part of an exercise. And I couldn't find a similar question at SO. Here is a little GTK program, where after clicking the Rename button the file should be renamed.
Although the uri seems to be pointing to the correct file, for some reason I get the assertion G_IS_FILE failed at runtime.
Here is the code:
[indent=4]
uses
Gtk
class TestWindow:Window
_file_chooser:FileChooserButton
_entry:Gtk.Entry
_button:Gtk.Button
_file:File
construct()
title = "File chooser"
window_position = WindowPosition.CENTER
destroy.connect( Gtk.main_quit )
var folder_chooser = new FileChooserButton("Choose a Folder",FileChooserAction.SELECT_FOLDER)
folder_chooser.set_current_folder( Environment.get_home_dir() )
//I used selection_changed directly as per the question in stack_exchange
//http://stackoverflow.com/questions/34689763/the-signal-connect-syntax
folder_chooser.selection_changed.connect( folder_changed )
_file_chooser = new FileChooserButton("Choose a File",FileChooserAction.OPEN)
_file_chooser.set_current_folder( Environment.get_home_dir() )
_file_chooser.file_set.connect( file_changed )
_entry = new Gtk.Entry()
_entry.set_text("Here the file name")
//_entry.activate.connect(name_altered)
_button = new Button.with_label("Rename")
_button.set_sensitive(false)
_button.clicked.connect(btn_pressed)
var box = new Box( Orientation.VERTICAL, 0 )
box.pack_start( folder_chooser, true, true, 0 )
box.pack_start( _file_chooser, true, true, 0 )
box.pack_start( _entry, true, true, 0 )
box.pack_start( _button, true, true, 0 )
add( box )
def folder_changed( folder_chooser_widget:FileChooser )
folder:string = folder_chooser_widget.get_uri()
_file_chooser.set_current_folder_uri( folder )
def file_changed ( file_chooser_widget: FileChooser )
_file:File = File.new_for_uri(file_chooser_widget.get_uri())
try
info:FileInfo = _file.query_info (FileAttribute.ACCESS_CAN_WRITE, FileQueryInfoFlags.NONE, null)
writable: bool = info.get_attribute_boolean (FileAttribute.ACCESS_CAN_WRITE)
if !writable
_entry.set_sensitive (false)
else
_button.set_sensitive (true)
except e: Error
print e.message
_entry.set_text(_file.get_path())
def btn_pressed ()
_file.set_display_name(_entry.get_text())
init
Gtk.init( ref args )
var test = new TestWindow()
test.show_all()
Gtk.main()
The problem appears at the line: _file.set_display_name(_entry.get_text())
. I am looking for some insight on what I am doing wrong in this small Genie code.
_file
is null when you call_file.set_display_name()
. The problem is here:You're creating a new variable called
_file
which shadows the_file
field. Changing it toshould fix the issue. Note that you could also access the field with
this._field
instead; it's a bit longer to type, but easier to read and it makes errors like this much more obvious.