moonshot-identity-manager-app: Do not create 2 GtkWindows
[moonshot-ui.git] / src / moonshot-identity-manager-app.vala
index d366e97..f9fcfb2 100644 (file)
@@ -1,19 +1,93 @@
 using Gtk;
 
-class IdentityManagerApp : Window {
-    private MainWindow main_window;
-    
-    public IdentityManagerApp () {
-        main_window = new MainWindow();
-        main_window.show();
+
+class IdentityManagerApp {
+    public IdentityManagerModel model;
+    private IdentityManagerView view;
+    private MoonshotServer ipc_server;
+#if OS_MACOS
+       public OSXApplication osxApp;
+#endif
+    private const int WINDOW_WIDTH = 400;
+    private const int WINDOW_HEIGHT = 500;
+    public void show() {
+        view.show();    
     }
+    public IdentityManagerApp () {
+        model = new IdentityManagerModel(this);
+        view = new IdentityManagerView(this);
+        init_ipc_server ();
+#if OS_MACOS
+               osxApp = OSXApplication.get_instance();
+               osxApp.ns_application_open_file.connect(ipc_server.install_from_file);
+#endif
+        view.show();
+    }   
     
-/*    public int run(string[] args){
-        GLib.Application.run(args);
-     }*/
-    public static int main(string[] args)
+#if IPC_MSRPC
+    private void init_ipc_server ()
+    {
+        // Errors will currently be sent via g_log - ie. to an
+        // obtrusive message box, on Windows
+        //
+        this.ipc_server = MoonshotServer.get_instance ();
+        MoonshotServer.start (this.view);
+    }
+#elif IPC_DBUS_GLIB
+    private void init_ipc_server ()
+    {
+        try {
+            var conn = DBus.Bus.get (DBus.BusType.SESSION);
+            dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
+                                                       "/org/freedesktop/DBus",
+                                                       "org.freedesktop.DBus");
+
+            // try to register service in session bus
+            uint reply = bus.request_name ("org.janet.Moonshot", (uint) 0);
+            assert (reply == DBus.RequestNameReply.PRIMARY_OWNER);
+
+            this.ipc_server = new MoonshotServer (this.view);
+            conn.register_object ("/org/janet/moonshot", ipc_server);
+        }
+        catch (DBus.Error e)
+        {
+            stderr.printf ("%s\n", e.message);
+        }
+    }
+#else
+    private void bus_acquired_cb (DBusConnection conn)
     {
+        try {
+            conn.register_object ("/org/janet/moonshot", ipc_server);
+        }
+        catch (Error e)
+        {
+            stderr.printf ("%s\n", e.message);
+        }
+    }
+
+    private void init_ipc_server ()
+    {
+        this.ipc_server = new MoonshotServer (this.view);
+        GLib.Bus.own_name (GLib.BusType.SESSION,
+                           "org.janet.Moonshot",
+                           GLib.BusNameOwnerFlags.NONE,
+                           bus_acquired_cb,
+                           (conn, name) => {},
+                           (conn, name) => {
+                               error ("Couldn't own name %s on DBus.", name);
+                           });
+    }
+#endif
+}
+
+
+public static int main(string[] args){
         Gtk.init(ref args);
+               stdout.printf("Hello\n");
+        foreach (string arg in args)
+                       stdout.printf("arg %s\n", arg);
 
 #if OS_WIN32
         // Force specific theme settings on Windows without requiring a gtkrc file
@@ -25,14 +99,14 @@ class IdentityManagerApp : Window {
         Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
         Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
         Intl.textdomain (Config.GETTEXT_PACKAGE);
-        
+       
+          
         var app = new IdentityManagerApp();
         
-//        app.show();
+        app.show();
+
         Gtk.main();
 
         return 0;
     }
-}