[easytag/wip/glist: 6/12] Refactor list handling in scan.c
- From: David King <davidk src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [easytag/wip/glist: 6/12] Refactor list handling in scan.c
- Date: Mon, 27 May 2013 19:12:39 +0000 (UTC)
commit b723449503d4d9a0073577e4bd94863a42aa7d06
Author: David King <amigadave amigadave com>
Date: Thu May 23 19:59:56 2013 +0100
Refactor list handling in scan.c
src/scan.c | 166 +++++++++++++++++++++++-------------------------------------
1 files changed, 64 insertions(+), 102 deletions(-)
---
diff --git a/src/scan.c b/src/scan.c
index e6f6f8e..d3d842d 100644
--- a/src/scan.c
+++ b/src/scan.c
@@ -295,6 +295,7 @@ static void
Scan_Tag_With_Mask (ET_File *ETFile)
{
GList *fill_tag_list = NULL;
+ GList *l;
gchar **dest = NULL;
gchar *mask; // The 'mask' in the entry
gchar *filename_utf8;
@@ -312,9 +313,10 @@ Scan_Tag_With_Mask (ET_File *ETFile)
// Process this mask with file
fill_tag_list = Scan_Generate_New_Tag_From_Mask(ETFile,mask);
- while (fill_tag_list)
+
+ for (l = fill_tag_list; l != NULL; l = g_list_next (l))
{
- Scan_Mask_Item *mask_item = fill_tag_list->data;
+ Scan_Mask_Item *mask_item = l->data;
// Get the target entry for this code
dest = Scan_Return_File_Tag_Field_From_Mask_Code(FileTag,mask_item->code);
@@ -323,9 +325,8 @@ Scan_Tag_With_Mask (ET_File *ETFile)
if ( dest && ( OVERWRITE_TAG_FIELD || *dest==NULL || strlen(*dest)==0 ) )
ET_Set_Field_File_Tag_Item(dest,mask_item->string);
- if (!fill_tag_list->next) break;
- fill_tag_list = fill_tag_list->next;
}
+
Scan_Free_File_Fill_Tag_List(fill_tag_list);
// Set the default text to comment
@@ -569,6 +570,7 @@ void Scan_Fill_Tag_Generate_Preview (void)
gchar *mask = NULL;
gchar *preview_text = NULL;
GList *fill_tag_list = NULL;
+ GList *l;
if (!ETCore->ETFileDisplayedList
|| !ScannerWindow || !RenameFileMaskCombo || !FillTagPreviewLabel
@@ -581,9 +583,9 @@ void Scan_Fill_Tag_Generate_Preview (void)
preview_text = g_strdup("");
fill_tag_list = Scan_Generate_New_Tag_From_Mask(ETCore->ETFileDisplayed,mask);
- while (fill_tag_list)
+ for (l = fill_tag_list; l != NULL; l = g_list_next (l))
{
- Scan_Mask_Item *mask_item = fill_tag_list->data;
+ Scan_Mask_Item *mask_item = l->data;
gchar *tmp_code = g_strdup_printf("%c",mask_item->code);
gchar *tmp_string = g_markup_printf_escaped("%s",mask_item->string); // To avoid problem with
strings containing characters like '&'
gchar *tmp_preview_text = preview_text;
@@ -594,13 +596,11 @@ void Scan_Fill_Tag_Generate_Preview (void)
g_free(tmp_string);
g_free(tmp_preview_text);
- if (!fill_tag_list->next) break;
- fill_tag_list = fill_tag_list->next;
-
tmp_preview_text = preview_text;
preview_text = g_strconcat(tmp_preview_text," || ",NULL);
g_free(tmp_preview_text);
}
+
Scan_Free_File_Fill_Tag_List(fill_tag_list);
if (GTK_IS_LABEL(FillTagPreviewLabel))
@@ -625,20 +625,20 @@ void Scan_Fill_Tag_Generate_Preview (void)
static void
Scan_Free_File_Fill_Tag_List (GList *list)
{
- // Free the list
- list = g_list_first(list);
- while (list)
+ GList *l;
+
+ list = g_list_first (list);
+
+ for (l = list; l != NULL; l = g_list_next (l))
{
- if (list->data)
+ if (l->data)
{
- g_free(((Scan_Mask_Item *)list->data)->string);
- g_free( (Scan_Mask_Item *)list->data );
+ g_free (((Scan_Mask_Item *)l->data)->string);
+ g_free ((Scan_Mask_Item *)l->data);
}
- if (!list->next) break;
- list = list->next;
}
- g_list_free(list);
- list = NULL;
+
+ g_list_free (list);
}
@@ -985,20 +985,20 @@ void Scan_Rename_File_Generate_Preview (void)
static void
Scan_Free_File_Rename_List (GList *list)
{
- // Free the list
- list = g_list_last(list);
- while (list)
+ GList *l;
+
+ list = g_list_first (list);
+
+ for (l = list; l != NULL; l = g_list_next (l))
{
- if (list->data)
+ if (l->data)
{
- g_free(((File_Mask_Item *)list->data)->string);
- g_free( (File_Mask_Item *)list->data );
+ g_free (((File_Mask_Item *)l->data)->string);
+ g_free ((File_Mask_Item *)l->data);
}
- if (!list->prev) break;
- list = list->prev;
}
- g_list_free(list);
- list = NULL;
+
+ g_list_free (list);
}
/*
@@ -3371,13 +3371,11 @@ Process_Fields_Check_Button_Toggled (GtkWidget *object, GList *list)
if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(object)) )
{
- while (list)
+ for (; list != NULL; list = g_list_next (list))
{
if ( list->data!=NULL && list->data!=object )
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(list->data),FALSE);
i++;
- if (!list->next) break;
- list = list->next;
}
}
}
@@ -3534,10 +3532,8 @@ Mask_Editor_List_Row_Selected (GtkTreeSelection* selection, gpointer data)
/*
* At some point, we might get called when no rows are selected?
*/
- if (g_list_length(selectedRows) == 0)
+ if (!selectedRows)
{
- g_list_foreach(selectedRows, (GFunc) gtk_tree_path_free, NULL);
- g_list_free(selectedRows);
g_signal_handlers_unblock_by_func(G_OBJECT(MaskEditorEntry),
G_CALLBACK(Mask_Editor_Entry_Changed),NULL);
return;
@@ -3560,6 +3556,8 @@ Mask_Editor_List_Row_Selected (GtkTreeSelection* selection, gpointer data)
g_signal_handlers_unblock_by_func(G_OBJECT(MaskEditorEntry),
G_CALLBACK(Mask_Editor_Entry_Changed),NULL);
+
+ g_list_free_full (selectedRows, (GDestroyNotify)gtk_tree_path_free);
}
@@ -3594,11 +3592,11 @@ Mask_Editor_List_Duplicate (void)
{
gchar *text = NULL;
GList *selectedRows;
+ GList *l;
GList *toInsert = NULL;
GtkTreeSelection *selection;
GtkTreeIter rowIter;
GtkTreeModel *treeModel;
- gboolean valid;
g_return_if_fail (MaskEditorList != NULL);
@@ -3606,53 +3604,39 @@ Mask_Editor_List_Duplicate (void)
selectedRows = gtk_tree_selection_get_selected_rows(selection, NULL);
treeModel = gtk_tree_view_get_model(GTK_TREE_VIEW(MaskEditorList));
- if (g_list_length(selectedRows) == 0)
+ if (!selectedRows)
{
Log_Print(LOG_ERROR,_("Copy: No row selected"));
- g_list_foreach(selectedRows, (GFunc) gtk_tree_path_free, NULL);
- g_list_free(selectedRows);
return;
}
/* Loop through selected rows, duplicating them into a GList
* We cannot directly insert because the paths in selectedRows
* get out of date after an insertion */
- while (selectedRows)
+ for (l = selectedRows; l != NULL; l = g_list_next (l))
{
- valid = gtk_tree_model_get_iter(treeModel, &rowIter, (GtkTreePath*) selectedRows->data);
- if (valid)
+ if (gtk_tree_model_get_iter (treeModel, &rowIter,
+ (GtkTreePath*)l->data))
{
gtk_tree_model_get(treeModel, &rowIter, MASK_EDITOR_TEXT, &text, -1);
- toInsert = g_list_append(toInsert, text);
+ toInsert = g_list_prepend (toInsert, text);
}
-
- selectedRows = selectedRows->next;
- if (!selectedRows) break;
}
- /* Duplicate the relevant entries, by looping through the list backwards
- * (to preserve original order) */
- toInsert = g_list_last(toInsert);
- while (toInsert)
+ for (l = toInsert; l != NULL; l = g_list_next (l))
{
- gtk_list_store_insert(GTK_LIST_STORE(treeModel), &rowIter, 0);
- gtk_list_store_set(GTK_LIST_STORE(treeModel), &rowIter, MASK_EDITOR_TEXT, (gchar*) toInsert->data,
-1);
- g_free(toInsert->data);
-
- toInsert = toInsert->prev;
- if (!toInsert) break;
+ gtk_list_store_insert_with_values (GTK_LIST_STORE(treeModel), &rowIter,
+ 0, MASK_EDITOR_TEXT,
+ (gchar *)l->data, -1);
}
+
// Set focus to the last inserted line
if (toInsert)
Mask_Editor_List_Set_Row_Visible(treeModel,&rowIter);
/* Free data no longer needed */
- selectedRows = g_list_first(selectedRows);
- toInsert = g_list_first(toInsert);
- g_list_foreach(selectedRows, (GFunc) gtk_tree_path_free, NULL);
- g_list_free(selectedRows);
- g_list_foreach(toInsert, (GFunc) g_free, NULL);
- g_list_free(toInsert);
+ g_list_free_full (selectedRows, (GDestroyNotify)gtk_tree_path_free);
+ g_list_free_full (toInsert, (GDestroyNotify)g_free);
}
static void
@@ -3749,12 +3733,11 @@ Mask_Editor_List_Move_Up (void)
{
GtkTreeSelection *selection;
GList *selectedRows;
- GList *selectedRowsCopy;
+ GList *l;
GtkTreeIter currentFile;
GtkTreeIter nextFile;
GtkTreePath *currentPath;
GtkTreeModel *treemodel;
- gboolean valid;
g_return_if_fail (MaskEditorList != NULL);
@@ -3762,21 +3745,16 @@ Mask_Editor_List_Move_Up (void)
treemodel = gtk_tree_view_get_model(GTK_TREE_VIEW(MaskEditorList));
selectedRows = gtk_tree_selection_get_selected_rows(selection, NULL);
- if (g_list_length(selectedRows) == 0)
+ if (!selectedRows)
{
Log_Print(LOG_ERROR,_("Move Up: No row selected"));
- g_list_foreach(selectedRows, (GFunc)gtk_tree_path_free, NULL);
- g_list_free(selectedRows);
return;
}
- selectedRowsCopy = selectedRows;
-
- while (selectedRows)
+ for (l = selectedRows; l != NULL; l = g_list_next (l))
{
- currentPath = (GtkTreePath*) selectedRows->data;
- valid = gtk_tree_model_get_iter(treemodel, ¤tFile, currentPath);
- if (valid)
+ currentPath = (GtkTreePath *)l->data;
+ if (gtk_tree_model_get_iter(treemodel, ¤tFile, currentPath))
{
/* Find the entry above the node... */
if (gtk_tree_path_prev(currentPath))
@@ -3786,13 +3764,9 @@ Mask_Editor_List_Move_Up (void)
gtk_list_store_swap(GTK_LIST_STORE(treemodel), ¤tFile, &nextFile);
}
}
-
- selectedRows = selectedRows->next;
- if (!selectedRows) break;
}
- g_list_foreach(selectedRowsCopy, (GFunc)gtk_tree_path_free, NULL);
- g_list_free(selectedRowsCopy);
+ g_list_free_full (selectedRows, (GDestroyNotify)gtk_tree_path_free);
}
/*
@@ -3803,12 +3777,11 @@ Mask_Editor_List_Move_Down (void)
{
GtkTreeSelection *selection;
GList *selectedRows;
- GList *selectedRowsCopy;
+ GList *l;
GtkTreeIter currentFile;
GtkTreeIter nextFile;
GtkTreePath *currentPath;
GtkTreeModel *treemodel;
- gboolean valid;
g_return_if_fail (MaskEditorList != NULL);
@@ -3816,34 +3789,26 @@ Mask_Editor_List_Move_Down (void)
treemodel = gtk_tree_view_get_model(GTK_TREE_VIEW(MaskEditorList));
selectedRows = gtk_tree_selection_get_selected_rows(selection, NULL);
- if (g_list_length(selectedRows) == 0)
+ if (!selectedRows)
{
Log_Print(LOG_ERROR,_("Move Down: No row selected"));
- g_list_foreach(selectedRows, (GFunc)gtk_tree_path_free, NULL);
- g_list_free(selectedRows);
return;
}
- selectedRowsCopy = selectedRows;
-
- while (selectedRows)
+ for (l = selectedRows; l != NULL; l = g_list_next (l))
{
- currentPath = (GtkTreePath*) selectedRows->data;
- valid = gtk_tree_model_get_iter(treemodel, ¤tFile, currentPath);
- if (valid)
+ currentPath = (GtkTreePath *)l->data;
+
+ if (gtk_tree_model_get_iter(treemodel, ¤tFile, currentPath))
{
/* Find the entry below the node and swap the two nodes by iter */
gtk_tree_path_next(currentPath);
if (gtk_tree_model_get_iter(treemodel, &nextFile, currentPath))
gtk_list_store_swap(GTK_LIST_STORE(treemodel), ¤tFile, &nextFile);
}
-
- if (!selectedRows->next) break;
- selectedRows = selectedRows->next;
}
- g_list_foreach(selectedRowsCopy, (GFunc)gtk_tree_path_free, NULL);
- g_list_free(selectedRowsCopy);
+ g_list_free_full (selectedRows, (GDestroyNotify)gtk_tree_path_free);
}
/*
@@ -3962,7 +3927,6 @@ Mask_Editor_Entry_Changed (void)
GList *selectedRows;
GtkTreeIter row;
const gchar* text;
- gboolean valid;
g_return_if_fail (MaskEditorList != NULL);
@@ -3970,22 +3934,20 @@ Mask_Editor_Entry_Changed (void)
treemodel = gtk_tree_view_get_model(GTK_TREE_VIEW(MaskEditorList));
selectedRows = gtk_tree_selection_get_selected_rows(selection, NULL);
- if (g_list_length(selectedRows) == 0)
+ if (!selectedRows)
{
- g_list_foreach(selectedRows, (GFunc) gtk_tree_path_free, NULL);
- g_list_free(selectedRows);
return;
}
firstSelected = (GtkTreePath *)g_list_first(selectedRows)->data;
text = gtk_entry_get_text(GTK_ENTRY(MaskEditorEntry));
- valid = gtk_tree_model_get_iter(treemodel, &row, firstSelected);
- if (valid)
+ if (gtk_tree_model_get_iter (treemodel, &row, firstSelected))
+ {
gtk_list_store_set(GTK_LIST_STORE(treemodel), &row, MASK_EDITOR_TEXT, text, -1);
+ }
- g_list_foreach(selectedRows, (GFunc) gtk_tree_path_free, NULL);
- g_list_free(selectedRows);
+ g_list_free_full (selectedRows, (GDestroyNotify)gtk_tree_path_free);
}
/*
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]