eexpress
eexpress

Reputation: 155

Create group menuitem and execute. vala

I read each line from a file. Add to menu. Want to execute those commands. But when I click, got null command error.

GLib-CRITICAL **: g_spawn_command_line_async: assertion `command_line != NULL' failed

ImageMenuItem menuApp;

void create_menuSystem() {
    menuSystem = new Menu();
    var menuSep = new SeparatorMenuItem();
    //read ~/.config/traytool/app
    var file = File.new_for_path(GLib.Environment.get_variable("HOME") + "/.config/traytool/app");
    if(file.query_exists()) {
        try {
            var dis = new DataInputStream(file.read());
            string line;
            while((line = dis.read_line(null)) != null) {
                menuApp = new ImageMenuItem.with_label(line);
                menuApp.activate.connect(() => {
                    spawn_command_line_async(line); // <----- here
                    stdout.printf("..%s\n", line);
                });
                menuSystem.append(menuApp);
            }
        } catch(Error e) {
            error("%s", e.message);
        }
        menuSystem.append(menuSep);
    }
}

Upvotes: 0

Views: 120

Answers (1)

apmasell
apmasell

Reputation: 7153

I've created a simpler test case:

extern void foo(F c);
delegate void F();
void main() {
  string? line;
  while ((line = stdin.read_line())!=null) {
    foo(() => { stdout.puts(line);});
  }
}

And line is only lifted by reference:

_data1_->line = _tmp1_;
_tmp2_ = _data1_->line;
if (!(_tmp2_ != NULL)) {
  break;
}
foo (____lambda2__f, _data1_);

To get around this, either the closure needs ownership, which you can't do, or you need to retain the values. You could put them in a Gee.ArrayList that you keep referenced with an index in the closure. Or, you could extend the ImageMenuItem to have an extra field/property.

Upvotes: 1

Related Questions