1 /*
2  * Copyright (C) 2003 2004 2010, Magnus Hjorth
3  *
4  * This file is part of mhWaveEdit.
5  *
6  * mhWaveEdit is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * mhWaveEdit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mhWaveEdit; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 
22 #include <config.h>
23 
24 #include <string.h>
25 #include "historybox.h"
26 #include "inifile.h"
27 
28 #define HISTORY_BOX_MAXENTRIES 20
29 
30 typedef struct {
31      gchar *name;
32      GList *entries;
33 } HistoryBoxHistory;
34 
35 static GHashTable *history_hash = NULL; /* key=gchar* (malloc'd), value=GList* */
36 
history_box_init(HistoryBox * box)37 static void history_box_init(HistoryBox *box)
38 {
39 }
40 
history_box_class_init(GtkObjectClass * klass)41 static void history_box_class_init(GtkObjectClass *klass)
42 {
43 }
44 
load_history(HistoryBoxHistory * hist)45 static void load_history(HistoryBoxHistory *hist)
46 {
47      gint i;
48      gchar c[128],*d;
49      for (i=1; i<=HISTORY_BOX_MAXENTRIES; i++) {
50 	  g_snprintf(c,sizeof(c),"history_%s_%d",hist->name,i);
51 	  d = inifile_get(c,NULL);
52 	  if (!d) break;
53 	  hist->entries = g_list_append(hist->entries,g_strdup(d));
54      }
55 }
56 
save_history(HistoryBoxHistory * hist)57 static void save_history(HistoryBoxHistory *hist)
58 {
59      gint i;
60      gchar c[128];
61      GList *l;
62      for (i=1,l=hist->entries; l!=NULL; i++,l=l->next) {
63 	  g_snprintf(c,sizeof(c),"history_%s_%d",hist->name,i);
64 	  inifile_set(c,l->data);
65      }
66      g_snprintf(c,sizeof(c),"history_%s_%d",hist->name,i);
67      inifile_set(c,NULL);
68      g_assert(i<=HISTORY_BOX_MAXENTRIES+1);
69 }
70 
history_box_get_type(void)71 GtkType history_box_get_type(void)
72 {
73      static GtkType id = 0;
74      if (!id) {
75 	  GtkTypeInfo info = {
76 	       "HistoryBox",
77 	       sizeof(HistoryBox),
78 	       sizeof(HistoryBoxClass),
79 	       (GtkClassInitFunc) history_box_class_init,
80 	       (GtkObjectInitFunc) history_box_init
81 	  };
82 	  id = gtk_type_unique(gtk_combo_get_type(),&info);
83      }
84      return id;
85 }
86 
history_box_new(gchar * historyname,gchar * value)87 GtkWidget *history_box_new(gchar *historyname, gchar *value)
88 {
89      HistoryBox *hb = gtk_type_new(history_box_get_type());
90      history_box_set_history(hb, historyname);
91      history_box_set_value(hb, value);
92      return GTK_WIDGET(hb);
93 }
94 
history_box_get_value(HistoryBox * box)95 gchar *history_box_get_value(HistoryBox *box)
96 {
97      return (gchar *)gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(box)->entry));
98 }
99 
history_box_set_history(HistoryBox * box,gchar * historyname)100 void history_box_set_history(HistoryBox *box, gchar *historyname)
101 {
102      HistoryBoxHistory *hist;
103      if (!history_hash) history_hash=g_hash_table_new(g_str_hash,g_str_equal);
104      hist = g_hash_table_lookup(history_hash, historyname);
105      if (!hist) {
106 	  hist = g_malloc(sizeof(*hist));
107 	  hist->name = g_strdup(historyname);
108 	  hist->entries = NULL;
109 	  load_history(hist);
110 	  g_hash_table_insert(history_hash,hist->name,hist);
111      }
112      if (hist->entries != NULL)
113 	  gtk_combo_set_popdown_strings(GTK_COMBO(box),hist->entries);
114      else
115 	  gtk_list_clear_items(GTK_LIST(GTK_COMBO(box)->list),0,-1);
116      box->history = hist;
117 }
118 
history_box_set_value(HistoryBox * box,gchar * value)119 void history_box_set_value(HistoryBox *box, gchar *value)
120 {
121      gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(box)->entry),value);
122 }
123 
history_box_rotate_history(HistoryBox * box)124 void history_box_rotate_history(HistoryBox *box)
125 {
126      gchar *v;
127      HistoryBoxHistory *hist=box->history;
128      GList *l,*n;
129      v = g_strdup(history_box_get_value(box));
130      for (l=hist->entries; l!=NULL; l=n) {
131 	  n = l->next;
132 	  if (!strcmp(l->data,v)) {
133 	       g_free(l->data);
134 	       hist->entries = g_list_remove_link(hist->entries,l);
135 	       g_list_free(l);
136 	  }
137      }
138      hist->entries = g_list_prepend(hist->entries, v);
139      l = g_list_nth(hist->entries, HISTORY_BOX_MAXENTRIES-1);
140      if (l && l->next) {
141 	  g_assert(l->next->next == NULL);
142 	  g_free(l->next->data);
143 	  g_list_free_1(l->next);
144 	  l->next = NULL;
145      }
146      gtk_combo_set_popdown_strings(GTK_COMBO(box),hist->entries);
147      save_history(hist);
148 }
149