From 315146a49b5570294ca20beca720c4e3f74a86bd Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sat, 21 Apr 2018 13:05:24 +0200
Subject: Handled projects from the command line.

---
 src/gui/editor.c |  5 ++++-
 src/main.c       | 52 ++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 52 insertions(+), 5 deletions(-)

diff --git a/src/gui/editor.c b/src/gui/editor.c
index 11c7f06..7047f32 100644
--- a/src/gui/editor.c
+++ b/src/gui/editor.c
@@ -481,7 +481,10 @@ static void on_destroy_editor(GtkWidget *widget, GObject *ref)
 
     on_focus_out(widget, NULL, ref);
 
-    gtk_main_quit();
+
+    /* Si la boucle principale est bien lancée, on en sort ! */
+    if (gtk_main_level() > 0)
+        gtk_main_quit();
 
 }
 
diff --git a/src/main.c b/src/main.c
index 98d1263..19b0544 100644
--- a/src/main.c
+++ b/src/main.c
@@ -25,6 +25,7 @@
 #include <getopt.h>
 #include <locale.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <gtk/gtk.h>
 
 #include <config.h>
@@ -81,7 +82,7 @@ static void show_chrysalide_help(const char *name)
     printf("\n");
 
     printf("Usage: %s [--help] [--version]\n", base);
-    printf("       %s [--batch] <filename(s)...>\n", base);
+    printf("       %s [args] <filename(s)...>\n", base);
 
     printf("\n");
 
@@ -92,6 +93,7 @@ static void show_chrysalide_help(const char *name)
 
     printf("\t--verbosity=level\tSet the log level (0 for all messages, %u for none).\n", LMT_COUNT);
     printf("\t--batch\t\t\tExit after processing files.\n");
+    printf("\t--project=filename\tOpen an existing project or create a new one.\n");
 
     printf("\n");
 
@@ -149,6 +151,7 @@ int main(int argc, char **argv)
     bool show_version;                      /* Affichage de la version ?   */
     LogMessageType verbosity;               /* Niveau de filtre de message */
     bool batch_mode;                        /* Exécution sans GUI ?        */
+    char *prj_filename;                     /* Chemin vers un projet       */
     int index;                              /* Indice d'argument           */
     int ret;                                /* Bilan d'un appel            */
     GtkWidget *editor;                      /* Fenêtre graphique           */
@@ -161,12 +164,14 @@ int main(int argc, char **argv)
     char *pub;                              /* Chemin de la clef publique  */
 
     bool welcome;                           /* Affichage de la bienvenue ? */
+    GStudyProject *project;                 /* Nouveau projet courant      */
 
     static struct option long_options[] = {
         { "help",       no_argument,        NULL,   'h' },
         { "version",    no_argument,        NULL,   'v' },
         { "verbosity",  required_argument,  NULL,   'V' },
         { "batch",      no_argument,        NULL,   'b' },
+        { "project",    required_argument,  NULL,   'p' },
         { NULL,         0,                  NULL,   0 }
     };
 
@@ -186,10 +191,11 @@ int main(int argc, char **argv)
 
     verbosity = LMT_INFO;
     batch_mode = false;
+    prj_filename = NULL;
 
     while (true)
     {
-        ret = getopt_long(argc, argv, "hvV:b", long_options, &index);
+        ret = getopt_long(argc, argv, "hvV:bp:", long_options, &index);
         if (ret == -1) break;
 
         switch (ret)
@@ -210,6 +216,10 @@ int main(int argc, char **argv)
                 batch_mode = true;
                 break;
 
+            case 'p':
+                prj_filename = optarg;
+                break;
+
         }
 
     }
@@ -287,11 +297,43 @@ int main(int argc, char **argv)
     else
         g_generic_config_get_value(config, MPK_WELCOME_STARTUP, &welcome);
 
-    if (!welcome)
+    if (!welcome && prj_filename == NULL)
         g_idle_add((GSourceFunc)load_last_project, config);
 
     else
-        set_current_project(g_study_project_new());
+    {
+        if (prj_filename == NULL)
+            project = g_study_project_new();
+
+        else
+        {
+            ret = access(prj_filename, R_OK);
+
+            if (ret == 0)
+            {
+                project = g_study_project_open(prj_filename);
+                if (project == NULL) goto bad_project;
+            }
+
+            else
+            {
+                project = g_study_project_new();
+
+                status = g_study_project_save(project, prj_filename);
+
+                if (!status)
+                {
+                    g_object_unref(G_OBJECT(project));
+                    goto bad_project;
+                }
+
+            }
+
+        }
+
+        set_current_project(project);
+
+    }
 
     /* Exécution du programme */
 
@@ -303,6 +345,8 @@ int main(int argc, char **argv)
     else
         gtk_main();
 
+ bad_project:
+
     g_db_server_stop(server);
 
     g_object_unref(G_OBJECT(server));
-- 
cgit v0.11.2-87-g4458