Re: [Vala] regarding Gtk.Application managing a single instance app

2013-02-04 Thread Erick PĂ©rez Castellanos
/* I guess the issues is the way you handle the app invocation, you're
trying to parse the
command-line arguments yourself, and that could be causing the errors. I
recommend to look into
the vala application inside gnome that use Gtk.Application. Gnome Contacts,
Gnome Boxes does that
and I think Baobab does as well. */
/* Check the code below which is mainly what I use. */

public class App: Gtk.Application {
private ApplicationWindow window;

public App () {
/* Here, you should check the flags, since those flags define how your app
will be invoked */
Object (application_id: "nova.ncopier", flags:
ApplicationFlags.HANDLES_COMMAND_LINE);
}

/* For instance the HANDLES_COMMAND_LINE flags wil give you access to this
method that
 will be called everytime some call your application from the command line.
*/
public override int command_line (ApplicationCommandLine command_line) {
var args = command_line.get_arguments ();

/* blah, blah, blah */

activate ();

return 0;
}

public override void activate () {
if (window == null)
create_window ();
else
add_new_copy_to_window ();

window.present ();
}
}


/* And your application is launched like this */
public static int
main (string[] args) {
var app = new App ();
app.run (args);

return 0;
}
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] regarding Gtk.Application managing a single instance app

2013-02-02 Thread Tal Hadad

First, note you can use GLib.Application in GLib, you don't have to use 
Gtk.Aplication(it's inherit from the first):
http://developer.gnome.org/gio/unstable/GApplication.html

There, you can see example 19A, where it shows you how to activate an Action, 
using Application class.
The action is performed in the primary instance, even if it was activated in 
other process.
You can pass a string parameter to the action.

Good luck
Tal

> From: db...@uci.cu
> To: vala-list@gnome.org
> Date: Fri, 1 Feb 2013 14:21:35 -0500
> Subject: [Vala] regarding Gtk.Application managing a single instance app
> 
> Hello,
> 
> a student of mine has been appointed to develop a copy manager. He
> decided to write the code in Vala and he's been consulting me since. The
> idea is to be able to manage several copy processes in a single window,
> so he's using Gtk.Application for that matter.
> 
> The logic behind the single instance is (as you may suppose) that the
> first time the application is invoked it launches a new window with the
> copy widgets on it. Any new attempt to invoke the application should add
> a new copy to the already existing window (a window exists solely while
> any of the copies it handles is alive, so: if the there is no copy in
> progress any invocation is considered as the first one).
> 
> All that being said what's happening is that the second time the app is
> invoked the "activate" method seems to be running on the first instance
> of the class, and since the new copy information is stored on the
> current instance, there is no new copy being created since the existing
> instance tries to create (again) it's own copy resulting on an logical
> error since the same copy cannot be run twice at the same time.
> 
> Issues related to good programming practices have been discussed with
> the student already, yet I fail to understand how could the new info be
> passed to the existing instance.
> 
> I'm hoping someone can shed some light on this.
> 
> Best regards,
> 
> D.H.Bahr.
> 
> PS: This is the code relevant to the problem:
> 
> /**/
> using GLib;
> using Gtk;
> 
> public class Main : Gtk.Application {
> 
>   private MainView main_view;
>   private string operation_type;
>   private List sources;
>   private string destination;
>   
>   public Main (string operation_type, List sources, string
> destination) {
> Object (application_id: "nova.ncopier", flags:
> ApplicationFlags.FLAGS_NONE);
>   
> this.operation_type = operation_type;
>   
> this.sources = new List ();   
> foreach (unowned string source in sources) {
>   this.sources.append (source);
> }
>   
> this.destination = destination;
>   }
> 
>   public override void activate () {
> if (this.get_windows ().length () == 0) {
>   main_view = new MainView (this.operation_type, this.sources,
> this.destination);
>   this.add_window(main_view);
> } else {
>   for (int i = 0; i < (int) this.sources.length (); i++) {
> stdout.printf ("%d:%s", i, this.sources.nth_data (i));
>   }
>   main_view.add_operation (new OperationView (this.operation_type,
> this.sources, this.destination));
>   main_view.present ();
> }
>   }
> 
>   static int main (string[] args) {
> Gtk.init (ref args);
>   
> if (args.length < 4) {
>   stdout.printf ("incomplete parameters\n");
>   return 1;
> } else {
>   string operation_type = "";
>   List sources = new List ();
>   string destination = "";
>   
>   if (args[1] == "copy") {
> operation_type = "COPYING";
>   }
>   sources.append ("file:" + args[2]);
>   destination = "file:" + args[3];
>   
>   return new Main (operation_type, sources, destination).run ();
> }
>   }
> }
> /***/
> 
> ___
> vala-list mailing list
> vala-list@gnome.org
> https://mail.gnome.org/mailman/listinfo/vala-list
  
___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list


[Vala] regarding Gtk.Application managing a single instance app

2013-02-01 Thread D.H. Bahr
Hello,

a student of mine has been appointed to develop a copy manager. He
decided to write the code in Vala and he's been consulting me since. The
idea is to be able to manage several copy processes in a single window,
so he's using Gtk.Application for that matter.

The logic behind the single instance is (as you may suppose) that the
first time the application is invoked it launches a new window with the
copy widgets on it. Any new attempt to invoke the application should add
a new copy to the already existing window (a window exists solely while
any of the copies it handles is alive, so: if the there is no copy in
progress any invocation is considered as the first one).

All that being said what's happening is that the second time the app is
invoked the "activate" method seems to be running on the first instance
of the class, and since the new copy information is stored on the
current instance, there is no new copy being created since the existing
instance tries to create (again) it's own copy resulting on an logical
error since the same copy cannot be run twice at the same time.

Issues related to good programming practices have been discussed with
the student already, yet I fail to understand how could the new info be
passed to the existing instance.

I'm hoping someone can shed some light on this.

Best regards,

D.H.Bahr.

PS: This is the code relevant to the problem:

/**/
using GLib;
using Gtk;

public class Main : Gtk.Application {

  private MainView main_view;
  private string operation_type;
  private List sources;
  private string destination;

  public Main (string operation_type, List sources, string
destination) {
Object (application_id: "nova.ncopier", flags:
ApplicationFlags.FLAGS_NONE);

this.operation_type = operation_type;

this.sources = new List (); 
foreach (unowned string source in sources) {
  this.sources.append (source);
}

this.destination = destination;
  }

  public override void activate () {
if (this.get_windows ().length () == 0) {
  main_view = new MainView (this.operation_type, this.sources,
this.destination);  
  this.add_window(main_view);
} else {
  for (int i = 0; i < (int) this.sources.length (); i++) {
stdout.printf ("%d:%s", i, this.sources.nth_data (i));
  }
  main_view.add_operation (new OperationView (this.operation_type,
this.sources, this.destination));
  main_view.present ();
}
  }

  static int main (string[] args) {
Gtk.init (ref args);

if (args.length < 4) {
  stdout.printf ("incomplete parameters\n");
  return 1;
} else {
  string operation_type = "";
  List sources = new List ();
  string destination = "";

  if (args[1] == "copy") {
operation_type = "COPYING";
  }
  sources.append ("file:" + args[2]);
  destination = "file:" + args[3];

  return new Main (operation_type, sources, destination).run ();
}
  }
}
/***/

___
vala-list mailing list
vala-list@gnome.org
https://mail.gnome.org/mailman/listinfo/vala-list