[anjuta] pm: Save and restore project shortcuts



commit d23d7872b740050d80d40c3274f5db8a3f71bb4e
Author: Sébastien Granjoux <seb sfo free fr>
Date:   Wed Jan 13 20:00:25 2010 +0100

    pm: Save and restore project shortcuts

 plugins/project-manager/gbf-project-model.c |   22 ++++-
 plugins/project-manager/gbf-project-model.h |    7 ++
 plugins/project-manager/gbf-project-view.c  |  131 +++++++++++++++++++++++++++
 plugins/project-manager/gbf-project-view.h  |    4 +
 plugins/project-manager/gbf-tree-data.c     |   99 ++++++++++++++++++++
 plugins/project-manager/gbf-tree-data.h     |    3 +
 plugins/project-manager/plugin.c            |   31 +++++++
 7 files changed, 294 insertions(+), 3 deletions(-)
---
diff --git a/plugins/project-manager/gbf-project-model.c b/plugins/project-manager/gbf-project-model.c
index 7723311..70018ea 100644
--- a/plugins/project-manager/gbf-project-model.c
+++ b/plugins/project-manager/gbf-project-model.c
@@ -278,7 +278,7 @@ gbf_project_model_instance_init (GbfProjectModel *model)
 
 /* Model data functions ------------ */
 
-static gboolean
+gboolean
 gbf_project_model_remove (GbfProjectModel *model, GtkTreeIter *iter)
 {
 	GtkTreeIter child;
@@ -400,6 +400,7 @@ add_source (GbfProjectModel    	      *model,
 
 static void 
 add_target_shortcut (GbfProjectModel *model,
+                     GtkTreeIter     *shortcut,
 		     GbfTreeData     *target,
 		     GtkTreePath     *before_path)
 {
@@ -445,6 +446,8 @@ add_target_shortcut (GbfProjectModel *model,
 		add_source (model, node, &iter);
 
 	gtk_tree_path_free (root_path);
+
+	if (shortcut) *shortcut = iter;
 }
 
 static void 
@@ -532,7 +535,7 @@ add_target (GbfProjectModel 		*model,
 		case ANJUTA_TARGET_EXECUTABLE:
 		case ANJUTA_TARGET_PYTHON:
 		case ANJUTA_TARGET_JAVA:
-			add_target_shortcut (model, data, NULL);
+			add_target_shortcut (model, NULL, data, NULL);
 			break;
 		default:
 			break;
@@ -965,6 +968,19 @@ gbf_project_model_get_node (GbfProjectModel *model,
 	return gbf_tree_data_get_node (data, model->priv->proj);
 }
 
+void
+gbf_project_model_add_shortcut (GbfProjectModel *model,
+                                GtkTreeIter     *iter,
+                                GtkTreeIter     *before, 
+                                GbfTreeData     *target)
+{
+	GtkTreePath *path;
+
+	path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), before);
+	add_target_shortcut (model, iter, target, path);
+	gtk_tree_path_free (path);
+}
+
 /* DND stuff ------------- */
 
 static gboolean
@@ -1033,7 +1049,7 @@ drag_data_received (GtkTreeDragDest  *drag_dest,
 				else
 				{
 					add_target_shortcut (GBF_PROJECT_MODEL (drag_dest),
-						     	data, dest);
+						     	NULL, data, dest);
 				}
 				retval = TRUE;
 			}
diff --git a/plugins/project-manager/gbf-project-model.h b/plugins/project-manager/gbf-project-model.h
index 77a5017..93e72ca 100644
--- a/plugins/project-manager/gbf-project-model.h
+++ b/plugins/project-manager/gbf-project-model.h
@@ -61,10 +61,17 @@ void             gbf_project_model_set_project       (GbfProjectModel   *model,
 IAnjutaProject  *gbf_project_model_get_project       (GbfProjectModel   *model);
 
 GtkTreePath     *gbf_project_model_get_project_root  (GbfProjectModel   *model);
+gboolean         gbf_project_model_remove            (GbfProjectModel *model,
+                                                      GtkTreeIter *iter);
 gboolean         gbf_project_model_find_tree_data    (GbfProjectModel   *model,
                                                       GtkTreeIter       *iter,
                                                       GbfTreeData       *data);
 AnjutaProjectNode *gbf_project_model_get_node        (GbfProjectModel *model,
                                                       GtkTreeIter     *iter);
 
+void             gbf_project_model_add_shortcut      (GbfProjectModel *model,
+                                                      GtkTreeIter     *iter,
+                                                      GtkTreeIter     *before, 
+                                                      GbfTreeData     *target);
+
 #endif
diff --git a/plugins/project-manager/gbf-project-view.c b/plugins/project-manager/gbf-project-view.c
index 971436c..cffb67d 100644
--- a/plugins/project-manager/gbf-project-view.c
+++ b/plugins/project-manager/gbf-project-view.c
@@ -436,3 +436,134 @@ gbf_project_view_get_selected (GbfProjectView *view, GtkTreeIter* selected)
 
 	return data;
 }
+
+/* Shorcuts functions
+ *---------------------------------------------------------------------------*/
+
+GList *
+gbf_project_view_get_shortcut_list (GbfProjectView *view)
+{
+	GList *list = NULL;
+	GtkTreeModel* model;
+	gboolean valid;
+	GtkTreeIter iter;
+
+	model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
+	
+	for (valid = gtk_tree_model_iter_children (GTK_TREE_MODEL (model), &iter, NULL);
+		valid == TRUE;
+		valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter))
+	{
+		GbfTreeData *data;
+		gtk_tree_model_get (GTK_TREE_MODEL (model), &iter, 
+		    GBF_PROJECT_MODEL_COLUMN_DATA, &data,
+		    -1);
+
+		if ((data->type == GBF_TREE_NODE_SHORTCUT) && (data->shortcut != NULL))
+		{
+			gchar *uri;
+			GtkTreePath *path;
+			gboolean expand;
+
+			uri = gbf_tree_data_get_path (data);
+			path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
+			expand = gtk_tree_view_row_expanded (GTK_TREE_VIEW (view), path);
+			gtk_tree_path_free (path);
+
+			if (uri != NULL)
+			{
+				list = g_list_prepend (list, g_strconcat (expand ? "E " : "C ", uri, NULL));
+			}
+		}
+	}
+	list = g_list_reverse (list);
+	
+	return list;
+}
+
+void
+gbf_project_view_set_shortcut_list (GbfProjectView *view, GList *shortcuts)
+{
+	GtkTreeModel* model;
+
+	model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
+
+	if (shortcuts != NULL)
+	{
+		gboolean valid;
+		GtkTreeIter iter;
+		
+		/* Remove all current shortcuts */
+		for (valid = gtk_tree_model_iter_children (GTK_TREE_MODEL (model), &iter, NULL);
+			valid == TRUE;)
+		{
+			GbfTreeData *data;
+			
+			gtk_tree_model_get (GTK_TREE_MODEL (model), &iter, 
+			    GBF_PROJECT_MODEL_COLUMN_DATA, &data,
+		    	-1);
+
+			if (data->type == GBF_TREE_NODE_SHORTCUT)
+			{
+				valid = gbf_project_model_remove (GBF_PROJECT_MODEL (model), &iter);
+			}
+			else
+			{
+				/* No more shortcut, add saved one */
+				break;
+			}
+		}
+
+		if (valid)
+		{
+			GList *node;
+			
+			for (node = g_list_first (shortcuts); node != NULL; node = g_list_next (node))
+			{
+				GbfTreeData *data;
+				GtkTreeIter shortcut;
+				gboolean expand = FALSE;
+				const gchar *path = (const gchar *)node->data;
+
+				if (strncmp (path, "E ", 2) == 0)
+				{
+					expand = TRUE;
+					path += 2;
+				}
+				else if (strncmp (path, "C ", 2) == 0)
+				{
+					expand = FALSE;
+					path += 2;
+				}
+				data = gbf_tree_data_new_for_path (path);
+		
+				if (gbf_project_model_find_tree_data  (GBF_PROJECT_MODEL (model), &shortcut, data))
+				{
+					GbfTreeData *data;
+			
+					gtk_tree_model_get (GTK_TREE_MODEL (model), &shortcut, 
+			    			GBF_PROJECT_MODEL_COLUMN_DATA, &data,
+		    				-1);
+
+					gbf_project_model_add_shortcut (GBF_PROJECT_MODEL (model),
+					    	&shortcut,
+					    	&iter,
+					    	data);
+
+					if (expand)
+					{
+						GtkTreePath *path;
+
+						path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &shortcut);
+						gtk_tree_view_expand_row (GTK_TREE_VIEW (view), path, FALSE);
+						gtk_tree_path_free (path);
+					}
+				}
+				
+				gbf_tree_data_free (data);
+			}
+		}
+	}
+	
+	return;
+}
diff --git a/plugins/project-manager/gbf-project-view.h b/plugins/project-manager/gbf-project-view.h
index f93034e..e107b72 100644
--- a/plugins/project-manager/gbf-project-view.h
+++ b/plugins/project-manager/gbf-project-view.h
@@ -66,6 +66,10 @@ AnjutaProjectNode          *gbf_project_view_find_selected     (GbfProjectView *
 GbfTreeData                *gbf_project_view_get_selected      (GbfProjectView *view,
                                                                 GtkTreeIter    *selected);
 
+GList                      *gbf_project_view_get_shortcut_list (GbfProjectView *view);
+void                        gbf_project_view_set_shortcut_list (GbfProjectView *view,
+                                                                 GList          *shortcuts);
+
 G_END_DECLS
 
 #endif /* _GBF_PROJECT_VIEW_H_ */
diff --git a/plugins/project-manager/gbf-tree-data.c b/plugins/project-manager/gbf-tree-data.c
index 70218d0..494d418 100644
--- a/plugins/project-manager/gbf-tree-data.c
+++ b/plugins/project-manager/gbf-tree-data.c
@@ -111,6 +111,22 @@ gbf_tree_data_get_uri (GbfTreeData *data)
 	return NULL;
 }
 
+gchar *
+gbf_tree_data_get_path (GbfTreeData *data)
+{
+	gchar *path;
+	gchar *guri;
+	gchar *suri;
+	
+	guri = data->group != NULL ? g_file_get_uri (data->group) : NULL;
+	suri = data->source != NULL ? g_file_get_uri (data->source) : NULL;
+	path = g_strconcat (guri, " ", data->target, " ", suri, NULL);
+	g_free (suri);
+	g_free (guri);
+
+	return path;
+}
+
 gboolean
 gbf_tree_data_equal (GbfTreeData *data_a, GbfTreeData *data_b)
 {
@@ -180,6 +196,89 @@ gbf_tree_data_equal (GbfTreeData *data_a, GbfTreeData *data_b)
 }
 
 GbfTreeData *
+gbf_tree_data_new_for_path (const gchar *path)
+{
+	GbfTreeData *data = g_slice_new0 (GbfTreeData);
+	gchar **uris;
+
+	uris = g_strsplit (path, " ", 3);
+
+	if (uris != NULL)
+	{
+		if ((uris[0] != NULL) && (*uris[0] != '\0'))
+		{
+			data->group = g_file_new_for_uri (uris[0]);
+
+			if ((uris[1] != NULL) && (*uris[1] != '\0'))
+			{
+				data->target = g_strdup (uris[1]);
+
+				if ((uris[2] != NULL) && (*uris[2] != '\0'))
+				{
+					data->source = g_file_new_for_uri (uris[2]);
+				}
+			}
+		}
+	}
+	
+	if (data->source != NULL)
+	{
+		GFileInfo *ginfo;
+
+		data->type = GBF_TREE_NODE_SOURCE;
+		
+		ginfo = g_file_query_info (data->source,
+		    G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
+	    	G_FILE_QUERY_INFO_NONE,
+	    	NULL, NULL);
+		if (ginfo)
+		{
+			data->name = g_strdup (g_file_info_get_display_name (ginfo));
+	       	g_object_unref(ginfo);
+		}
+		else
+		{
+			data->name = g_file_get_basename (data->source);
+		}
+	}
+	else if (data->target != NULL)
+	{
+		data->type = GBF_TREE_NODE_TARGET;
+		
+		data->name = g_strdup (data->target);
+	}
+	else if (data->group != NULL)
+	{
+		GFileInfo *ginfo;
+
+		data->type = GBF_TREE_NODE_GROUP;
+		
+		ginfo = g_file_query_info (data->group,
+		    G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
+	    	G_FILE_QUERY_INFO_NONE,
+	    	NULL, NULL);
+		if (ginfo)
+		{
+			data->name = g_strdup (g_file_info_get_display_name (ginfo));
+	       	g_object_unref(ginfo);
+		}
+		else
+		{
+			data->name = g_file_get_basename (data->group);
+		}
+	}
+	else
+	{
+		data->type = GBF_TREE_NODE_STRING;
+		data->name = g_strdup ("?");
+	}
+
+	g_strfreev (uris);
+
+	return data;
+}
+
+GbfTreeData *
 gbf_tree_data_new_for_uri (const gchar *uri, GbfTreeNodeType type)
 {
 	GbfTreeData *data = g_slice_new0 (GbfTreeData);
diff --git a/plugins/project-manager/gbf-tree-data.h b/plugins/project-manager/gbf-tree-data.h
index b9a7d87..b2e244e 100644
--- a/plugins/project-manager/gbf-tree-data.h
+++ b/plugins/project-manager/gbf-tree-data.h
@@ -57,9 +57,12 @@ AnjutaProjectNode *gbf_tree_data_get_node	    (GbfTreeData		 *data,
 
 gchar	      *gbf_tree_data_get_uri		    (GbfTreeData          *data);
 
+gchar		  *gbf_tree_data_get_path		    (GbfTreeData          *data);
+
 gboolean       gbf_tree_data_equal              (GbfTreeData          *data_a,
                                                  GbfTreeData          *data_b);
 
+GbfTreeData   *gbf_tree_data_new_for_path       (const gchar          *data);
 GbfTreeData   *gbf_tree_data_new_for_uri        (const gchar          *uri,
                                                  GbfTreeNodeType      type);
 GbfTreeData   *gbf_tree_data_new_string         (const gchar          *string);
diff --git a/plugins/project-manager/plugin.c b/plugins/project-manager/plugin.c
index 9575679..4777fc3 100644
--- a/plugins/project-manager/plugin.c
+++ b/plugins/project-manager/plugin.c
@@ -177,6 +177,32 @@ on_session_save (AnjutaShell *shell, AnjutaSessionPhase phase,
 		g_list_free (list);
 	}
 
+	/* Save shortcuts */
+	list = gbf_project_view_get_shortcut_list (GBF_PROJECT_VIEW (plugin->view));
+	if (list != NULL)
+	{
+    	anjuta_session_set_string_list (session, "Project Manager", "Shortcut", list);
+		g_list_foreach (list, (GFunc)g_free, NULL);
+		g_list_free (list);
+	}
+
+}
+
+static void
+on_session_load (AnjutaShell *shell, AnjutaSessionPhase phase, AnjutaSession *session, ProjectManagerPlugin *plugin)
+{
+	GList *list;
+
+	if (phase != ANJUTA_SESSION_PHASE_NORMAL)
+		return;
+
+	list = anjuta_session_get_string_list (session, "Project Manager", "Shortcut");
+	if (list != NULL)
+	{
+		gbf_project_view_set_shortcut_list (GBF_PROJECT_VIEW (plugin->view), list);
+		g_list_foreach (list, (GFunc)g_free, NULL);
+		g_list_free (list);
+	}
 }
 
 static void
@@ -1557,6 +1583,8 @@ project_manager_plugin_activate_plugin (AnjutaPlugin *plugin)
 	/* Connect to save session */
 	g_signal_connect (G_OBJECT (plugin->shell), "save_session",
 					  G_CALLBACK (on_session_save), plugin);
+	g_signal_connect (G_OBJECT (plugin->shell), "load_session",
+					  G_CALLBACK (on_session_load), plugin);
 	g_signal_connect (G_OBJECT (plugin->shell), "exiting",
 					  G_CALLBACK (on_shell_exiting), plugin);
 	profile_manager = anjuta_shell_get_profile_manager (plugin->shell, NULL);
@@ -1593,6 +1621,9 @@ project_manager_plugin_deactivate_plugin (AnjutaPlugin *plugin)
 										  G_CALLBACK (on_session_save),
 										  plugin);
 	g_signal_handlers_disconnect_by_func (G_OBJECT (plugin->shell),
+										  G_CALLBACK (on_session_load),
+										  plugin);
+	g_signal_handlers_disconnect_by_func (G_OBJECT (plugin->shell),
 										  G_CALLBACK (on_shell_exiting),
 										  plugin);
 	g_signal_handlers_disconnect_by_func (G_OBJECT (profile_manager),



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]