Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
history.c
Go to the documentation of this file.
1 /*
2  * history.c
3  * Copyright 2011 John Lindgren
4  *
5  * This file is part of Audacious.
6  *
7  * Audacious is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free Software
9  * Foundation, version 2 or version 3 of the License.
10  *
11  * Audacious is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * Audacious. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The Audacious team does not consider modular code linking to Audacious or
19  * using our public API to be a derived work.
20  */
21 
22 #include <glib.h>
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include <libaudcore/hook.h>
27 
28 #include "main.h"
29 #include "misc.h"
30 
31 #define MAX_ENTRIES 30
32 
33 static GQueue history = G_QUEUE_INIT;
35 
36 static void history_save (void)
37 {
38  if (! modified)
39  return;
40 
41  config_clear_section ("history");
42 
43  GList * node = history.head;
44  for (int i = 0; i < MAX_ENTRIES; i ++)
45  {
46  if (! node)
47  break;
48 
49  char name[32];
50  snprintf (name, sizeof name, "entry%d", i);
51  set_string ("history", name, node->data);
52 
53  node = node->next;
54  }
55 
56  modified = FALSE;
57 }
58 
59 static void history_load (void)
60 {
61  if (loaded)
62  return;
63 
64  for (int i = 0; ; i ++)
65  {
66  char name[32];
67  snprintf (name, sizeof name, "entry%d", i);
68  char * path = get_string ("history", name);
69 
70  if (! path[0])
71  {
72  g_free (path);
73  break;
74  }
75 
76  g_queue_push_tail (& history, path);
77  }
78 
79  loaded = TRUE;
80  hook_associate ("config save", (HookFunction) history_save, NULL);
81 }
82 
83 void history_cleanup (void)
84 {
85  if (! loaded)
86  return;
87 
88  hook_dissociate ("config save", (HookFunction) history_save);
89 
90  g_queue_foreach (& history, (GFunc) g_free, NULL);
91  g_queue_clear (& history);
92 
93  loaded = FALSE;
94  modified = FALSE;
95 }
96 
97 const char * history_get (int entry)
98 {
99  history_load ();
100  return g_queue_peek_nth (& history, entry);
101 }
102 
103 void history_add (const char * path)
104 {
105  history_load ();
106 
107  GList * next;
108  for (GList * node = history.head; node; node = next)
109  {
110  next = node->next;
111  if (! strcmp (node->data, path))
112  {
113  g_free (node->data);
114  g_queue_delete_link (& history, node);
115  }
116  }
117 
118  g_queue_push_head (& history, g_strdup (path));
119  modified = TRUE;
120 }