From 7bc72fbc242fa1b9a59cd24b55b7376f5c81ce42 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Granjoux?= Date: Sun, 25 Nov 2012 12:45:06 +0100 Subject: [PATCH] project-wizard: Allow to start a new project from the command line We can use the name of a project wizard template file or tar.gz archive containing all needed files. --- anjuta.desktop.in.in | 4 +- mime/anjuta.xml | 5 + .../project-wizard/anjuta-project-wizard.plugin.in | 2 +- plugins/project-wizard/druid.c | 118 +++++++++++++-------- plugins/project-wizard/druid.h | 3 +- plugins/project-wizard/parser.c | 23 ++-- plugins/project-wizard/parser.h | 2 +- plugins/project-wizard/plugin.c | 26 +++-- plugins/project-wizard/plugin.h | 2 +- 9 files changed, 118 insertions(+), 67 deletions(-) diff --git a/anjuta.desktop.in.in b/anjuta.desktop.in.in index 3c00ddf0c..6c99c4587 100644 --- a/anjuta.desktop.in.in +++ b/anjuta.desktop.in.in @@ -3,14 +3,14 @@ _Name=Anjuta _GenericName=Integrated Development Environment _Comment=Develop software in an integrated development environment _Keywords=IDE;development;programming; -Exec=anjuta +Exec=anjuta %U Icon=anjuta Terminal=false X-MultipleArgs=false Type=Application Categories=GNOME;GTK;Development;IDE; StartupNotify=true -MimeType=application/x-anjuta; +MimeType=application/x-anjuta;application/x-anjuta-project-template;x-anjuta-compressed-project-template X-GNOME-DocPath=anjuta/anjuta-manual.xml X-GNOME-Bugzilla-Bugzilla=GNOME X-GNOME-Bugzilla-Product=anjuta diff --git a/mime/anjuta.xml b/mime/anjuta.xml index 4762002c9..1f22c2359 100644 --- a/mime/anjuta.xml +++ b/mime/anjuta.xml @@ -9,6 +9,11 @@ + + + Anjuta IDE Project Template + + Anjuta IDE Compressed Project Template diff --git a/plugins/project-wizard/anjuta-project-wizard.plugin.in b/plugins/project-wizard/anjuta-project-wizard.plugin.in index d78379095..6011512b7 100644 --- a/plugins/project-wizard/anjuta-project-wizard.plugin.in +++ b/plugins/project-wizard/anjuta-project-wizard.plugin.in @@ -7,7 +7,7 @@ Interfaces=IAnjutaFile,IAnjutaWizard UserActivatable=no [File Loader] -SupportedMimeTypes=application/x-anjuta-compressed-project-template +SupportedMimeTypes=application/x-anjuta-compressed-project-template,application/x-anjuta-project-template [Wizard] _Title=Project diff --git a/plugins/project-wizard/druid.c b/plugins/project-wizard/druid.c index 1a5f3effd..e0625258f 100644 --- a/plugins/project-wizard/druid.c +++ b/plugins/project-wizard/druid.c @@ -119,6 +119,7 @@ struct _NPWDruid NPWPageParser* parser; GList* header_list; NPWHeader* header; + gboolean no_selection; AnjutaAutogen* gen; gboolean busy; }; @@ -134,6 +135,23 @@ enum { /* Helper functon *---------------------------------------------------------------------------*/ +/* Druid GUI functions + *---------------------------------------------------------------------------*/ + +static void +npw_druid_set_busy (NPWDruid *druid, gboolean busy_state) +{ + if (druid->busy == busy_state) + return; + + /* Set busy state */ + if (busy_state) + anjuta_status_busy_push (anjuta_shell_get_status (ANJUTA_PLUGIN (druid->plugin)->shell, NULL)); + else + anjuta_status_busy_pop (anjuta_shell_get_status (ANJUTA_PLUGIN (druid->plugin)->shell, NULL)); + druid->busy = busy_state; +} + /* Create error page *---------------------------------------------------------------------------*/ @@ -382,7 +400,7 @@ cb_druid_insert_project_page (gpointer value, gpointer user_data) /* Fill project selection page */ static gboolean -npw_druid_fill_selection_page (NPWDruid* druid, const gchar *directory) +npw_druid_fill_selection_page (NPWDruid* druid, GFile *templates) { gchar* dir; const gchar * const * sys_dir; @@ -395,17 +413,30 @@ npw_druid_fill_selection_page (NPWDruid* druid, const gchar *directory) /* Create list of projects */ druid->header_list = npw_header_list_new (); - if (directory != NULL) + if (templates != NULL) { - /* Read project template only in specified directory, - * other directories can still be used to get included - * files */ - npw_header_list_readdir (&druid->header_list, directory); - anjuta_autogen_set_library_path (druid->gen, directory); + if (g_file_query_file_type (templates, 0, NULL) == G_FILE_TYPE_DIRECTORY) + { + /* Read project template only in specified directory, + * other directories can still be used to get included + * files */ + gchar *directory = g_file_get_path (templates); + + npw_header_list_readdir (&druid->header_list, directory); + anjuta_autogen_set_library_path (druid->gen, directory); + g_free (directory); + } + else + { + /* templates is a file, so read only it as a project template */ + gchar *filename = g_file_get_path (templates); + npw_header_list_read (&druid->header_list, filename); + g_free (filename); + } } dir = g_build_filename (g_get_user_data_dir (), "anjuta", "project", NULL); - if (directory == NULL) + if (templates == NULL) { /* Read project template in user directory, * normally ~/.local/share/anjuta/project, @@ -418,7 +449,7 @@ npw_druid_fill_selection_page (NPWDruid* druid, const gchar *directory) for (sys_dir = g_get_system_data_dirs (); *sys_dir != NULL; sys_dir++) { dir = g_build_filename (*sys_dir, "anjuta", "project", NULL); - if (directory == NULL) + if (templates == NULL) { /* Read project template in system directory */ npw_header_list_readdir (&druid->header_list, dir); @@ -427,24 +458,32 @@ npw_druid_fill_selection_page (NPWDruid* druid, const gchar *directory) g_free (dir); } - if (directory == NULL) + if (templates == NULL) { /* Read anjuta installation directory */ npw_header_list_readdir (&druid->header_list, PROJECT_WIZARD_DIRECTORY); } anjuta_autogen_set_library_path (druid->gen, PROJECT_WIZARD_DIRECTORY); - if (g_list_length (druid->header_list) == 0) + switch (g_list_length (druid->header_list)) { + case 0: anjuta_util_dialog_error (GTK_WINDOW (ANJUTA_PLUGIN (druid->plugin)->shell),_("Unable to find any project template in %s"), PROJECT_WIZARD_DIRECTORY); return FALSE; + case 1: + druid->header = (NPWHeader *)((GList *)druid->header_list->data)->data; + druid->no_selection = TRUE; + gtk_container_remove (GTK_CONTAINER (druid->window), druid->project_page); + gtk_assistant_insert_page (GTK_ASSISTANT (druid->window), druid->progress_page, 0); + npw_druid_set_busy (druid, FALSE); + break; + default: + /* Add all necessary notebook page */ + druid->no_selection = FALSE; + g_list_foreach (druid->header_list, cb_druid_insert_project_page, druid); + gtk_widget_show_all (GTK_WIDGET (druid->project_book)); } - /* Add all necessary notebook page */ - g_list_foreach (druid->header_list, cb_druid_insert_project_page, druid); - - gtk_widget_show_all (GTK_WIDGET (druid->project_book)); - return TRUE; } @@ -569,7 +608,7 @@ npw_druid_add_new_page (NPWDruid* druid) /* Get page in cache */ current = gtk_assistant_get_current_page (GTK_ASSISTANT (druid->window)); - page = g_queue_peek_nth (druid->page_list, current); + page = g_queue_peek_nth (druid->page_list, current - (druid->no_selection ? 0 : 1) + 1); if (page == NULL) { @@ -641,7 +680,7 @@ npw_druid_remove_following_page (NPWDruid* druid) gtk_container_remove (GTK_CONTAINER (druid->window), widget); - page = g_queue_pop_nth (druid->page_list, current - 1); + page = g_queue_pop_nth (druid->page_list, current - (druid->no_selection ? 0 : 1)); if (page != NULL) npw_page_free (page); } } @@ -752,7 +791,7 @@ npw_druid_save_valid_values (NPWDruid* druid) NPWSaveValidPropertyData data; gboolean ok = TRUE; - current = gtk_assistant_get_current_page (GTK_ASSISTANT (druid->window)) - 2; + current = gtk_assistant_get_current_page (GTK_ASSISTANT (druid->window)) - (druid->no_selection ? 0 : 1) - 1; page = g_queue_peek_nth (druid->page_list, current); data.modified = FALSE; data.parent = GTK_WINDOW (druid->window); @@ -789,23 +828,6 @@ npw_druid_save_valid_values (NPWDruid* druid) return ok; } -/* Druid GUI functions - *---------------------------------------------------------------------------*/ - -static void -npw_druid_set_busy (NPWDruid *druid, gboolean busy_state) -{ - if (druid->busy == busy_state) - return; - - /* Set busy state */ - if (busy_state) - anjuta_status_busy_push (anjuta_shell_get_status (ANJUTA_PLUGIN (druid->plugin)->shell, NULL)); - else - anjuta_status_busy_pop (anjuta_shell_get_status (ANJUTA_PLUGIN (druid->plugin)->shell, NULL)); - druid->busy = busy_state; -} - /* Druid call backs *---------------------------------------------------------------------------*/ @@ -843,14 +865,14 @@ on_druid_get_new_page (AnjutaAutogen* gen, gpointer data) NPWPage* page; current = gtk_assistant_get_current_page (GTK_ASSISTANT (druid->window)); - page = g_queue_peek_nth (druid->page_list, current - 1); + page = g_queue_peek_nth (druid->page_list, current - (druid->no_selection ? 0 : 1)); if (npw_page_get_name (page) == NULL) { /* no page, display finish page */ npw_druid_fill_summary_page (druid); - page = g_queue_pop_nth (druid->page_list, current - 1); + page = g_queue_pop_nth (druid->page_list, current - (druid->no_selection ? 0 : 1)); if (page != NULL) npw_page_free (page); gtk_container_remove (GTK_CONTAINER (druid->window), gtk_assistant_get_nth_page (GTK_ASSISTANT (druid->window), current + 1)); gtk_assistant_set_current_page (GTK_ASSISTANT (druid->window), current + 1); @@ -1021,6 +1043,7 @@ on_druid_real_prepare (GtkAssistant* assistant, GtkWidget *page, NPWDruid* druid gtk_container_remove (GTK_CONTAINER (assistant), druid->error_page); previous--; } + if (druid->no_selection) previous++; /* Generate the next page */ if (previous == PROJECT_PAGE_INDEX) @@ -1136,7 +1159,7 @@ on_druid_finish (GtkAssistant* assistant, NPWDruid* druid) } static GtkWidget* -npw_druid_create_assistant (NPWDruid* druid, const gchar *directory) +npw_druid_create_assistant (NPWDruid* druid, GFile *templates) { AnjutaShell *shell; GtkBuilder *builder; @@ -1172,8 +1195,6 @@ npw_druid_create_assistant (NPWDruid* druid, const gchar *directory) PROPERTY_PAGE, &property_page, NULL); druid->window = GTK_WINDOW (assistant); - g_object_ref (druid->error_page); - g_object_ref (druid->progress_page); gtk_window_set_transient_for (GTK_WINDOW (assistant), GTK_WINDOW (shell)); g_object_unref (builder); @@ -1186,9 +1207,15 @@ npw_druid_create_assistant (NPWDruid* druid, const gchar *directory) /* Remove property page, will be created later as needed */ gtk_container_remove (GTK_CONTAINER (assistant), property_page); + /* Remove error page, could be needed later so keep a ref */ + g_object_ref (druid->error_page); + gtk_container_remove (GTK_CONTAINER (assistant), druid->error_page); + /* Remove progress page, could be needed later so keep a ref */ + g_object_ref (druid->progress_page); + gtk_container_remove (GTK_CONTAINER (assistant), druid->progress_page); /* Setup project selection page */ - if (!npw_druid_fill_selection_page (druid, directory)) + if (!npw_druid_fill_selection_page (druid, templates)) { return NULL; } @@ -1248,7 +1275,7 @@ npw_druid_add_default_property (NPWDruid* druid) *---------------------------------------------------------------------------*/ NPWDruid* -npw_druid_new (NPWPlugin* plugin, const gchar *directory) +npw_druid_new (NPWPlugin* plugin, GFile *templates) { NPWDruid* druid; @@ -1263,13 +1290,14 @@ npw_druid_new (NPWPlugin* plugin, const gchar *directory) druid->plugin = plugin; druid->project_file = NULL; druid->busy = FALSE; + druid->no_selection = FALSE; druid->page_list = g_queue_new (); druid->values = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_free); druid->gen = anjuta_autogen_new (); - druid->plugin = plugin; + plugin->druid = druid; druid->error_extra_widget = NULL; - if (npw_druid_create_assistant (druid, directory) == NULL) + if (npw_druid_create_assistant (druid, templates) == NULL) { npw_druid_free (druid); diff --git a/plugins/project-wizard/druid.h b/plugins/project-wizard/druid.h index f2a47b35a..10d96065d 100644 --- a/plugins/project-wizard/druid.h +++ b/plugins/project-wizard/druid.h @@ -24,11 +24,12 @@ #define ICON_FILE "anjuta-project-wizard-plugin-48.png" #include +#include struct _NPWPlugin; typedef struct _NPWDruid NPWDruid; -NPWDruid* npw_druid_new (struct _NPWPlugin* plugin, const gchar *directory); +NPWDruid* npw_druid_new (struct _NPWPlugin* plugin, GFile *templates); void npw_druid_free (NPWDruid* druid); void npw_druid_show (NPWDruid* druid); diff --git a/plugins/project-wizard/parser.c b/plugins/project-wizard/parser.c index b5d50e738..b782cf8bd 100644 --- a/plugins/project-wizard/parser.c +++ b/plugins/project-wizard/parser.c @@ -580,24 +580,25 @@ npw_header_parser_end_parse (NPWHeaderParser* parser, GError** error) return g_markup_parse_context_end_parse (parser->ctx, error); }*/ -gboolean +NPWHeader * npw_header_list_read (GList** list, const gchar* filename) { gchar* content; gsize len; NPWHeaderParser* parser; NPWHeader* header; + NPWHeader *found; GError* err = NULL; - g_return_val_if_fail (list != NULL, FALSE); - g_return_val_if_fail (filename != NULL, FALSE); + g_return_val_if_fail (list != NULL, NULL); + g_return_val_if_fail (filename != NULL, NULL); if (!g_file_get_contents (filename, &content, &len, &err)) { g_warning ("%s", err->message); g_error_free (err); - return FALSE; + return NULL; } parser = npw_header_parser_new (list, filename); @@ -616,7 +617,7 @@ npw_header_list_read (GList** list, const gchar* filename) g_warning ("Missing project wizard block in %s", filename); npw_header_free (header); - return FALSE; + return NULL; } if (g_error_matches (err, parser_error_quark (), NPW_STOP_PARSING) == FALSE) { @@ -625,17 +626,23 @@ npw_header_list_read (GList** list, const gchar* filename) g_error_free (err); npw_header_free (header); - return FALSE; + return NULL; } g_error_free (err); /* Add header to list if template does not already exist*/ - if (npw_header_list_find_header (*list, header) == NULL) + found = npw_header_list_find_header (*list, header); + if (found == NULL) { *list = npw_header_list_insert_header (*list, header); } + else + { + npw_header_free (header); + header = found; + } - return TRUE; + return header; } diff --git a/plugins/project-wizard/parser.h b/plugins/project-wizard/parser.h index fee8fa934..894e29e73 100644 --- a/plugins/project-wizard/parser.h +++ b/plugins/project-wizard/parser.h @@ -30,7 +30,7 @@ gboolean npw_header_list_readdir (GList** this, const gchar* pathname); -gboolean npw_header_list_read (GList** this, const gchar* filename); +NPWHeader *npw_header_list_read (GList** this, const gchar* filename); typedef struct _NPWPageParser NPWPageParser; diff --git a/plugins/project-wizard/plugin.c b/plugins/project-wizard/plugin.c index 37360e7f2..9b18d0de2 100644 --- a/plugins/project-wizard/plugin.c +++ b/plugins/project-wizard/plugin.c @@ -52,11 +52,7 @@ npw_open_project_template (GFile *destination, GFile *tarfile, gpointer data, GE else { /* Show the project wizard dialog, loading only the new projects */ - gchar *path; - - path = g_file_get_path (destination); - npw_plugin_show_wizard (plugin, path); - g_free (path); + npw_plugin_show_wizard (plugin, destination); } } @@ -118,7 +114,7 @@ npw_install_project_template_with_callback (NPWPlugin *plugin, GFile *file, NPWT /* Display the project wizard selection dialog using only templates in the * specified directory if non NULL */ gboolean -npw_plugin_show_wizard (NPWPlugin *plugin, const gchar *directory) +npw_plugin_show_wizard (NPWPlugin *plugin, GFile *project) { if (plugin->install != NULL) { @@ -127,7 +123,7 @@ npw_plugin_show_wizard (NPWPlugin *plugin, const gchar *directory) else if (plugin->druid == NULL) { /* Create a new project wizard druid */ - npw_druid_new (plugin, directory); + npw_druid_new (plugin, project); } if (plugin->druid != NULL) @@ -136,6 +132,7 @@ npw_plugin_show_wizard (NPWPlugin *plugin, const gchar *directory) npw_druid_show (plugin->druid); } + return TRUE; } @@ -229,8 +226,21 @@ static void ifile_open (IAnjutaFile *ifile, GFile* file, GError **error) { NPWPlugin *plugin = ANJUTA_PLUGIN_NPW (ifile); + GFileInfo *info; - npw_install_project_template_with_callback (plugin, file, npw_open_project_template, error); + info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0, NULL, NULL); + if (info != NULL) + { + if (strcmp(g_file_info_get_content_type (info), "application/x-anjuta-project-template") == 0) + { + npw_plugin_show_wizard (plugin, file); + } + else + { + npw_install_project_template_with_callback (plugin, file, npw_open_project_template, error); + } + g_object_unref (info); + } } static GFile* diff --git a/plugins/project-wizard/plugin.h b/plugins/project-wizard/plugin.h index 2dc32e444..9d9427a39 100644 --- a/plugins/project-wizard/plugin.h +++ b/plugins/project-wizard/plugin.h @@ -52,6 +52,6 @@ IAnjutaMessageView* npw_plugin_create_view (NPWPlugin* plugin); void npw_plugin_append_view (NPWPlugin* plugin, const gchar* text); void npw_plugin_print_view (NPWPlugin* plugin, IAnjutaMessageViewType type, const gchar* summary, const gchar* details); -gboolean npw_plugin_show_wizard (NPWPlugin *plugin, const gchar *template_dir); +gboolean npw_plugin_show_wizard (NPWPlugin *plugin, GFile *project_template); #endif -- 2.11.4.GIT