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  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions, and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions, and the following disclaimer in the documentation
13  * provided with the distribution.
14  *
15  * This software is provided "as is" and without any warranty, express or
16  * implied. In no event shall the authors be liable for any damages arising from
17  * the use of this software.
18  */
19 
20 #include <glib.h>
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include <libaudcore/hook.h>
25 
26 #include "main.h"
27 #include "misc.h"
28 
29 #define MAX_ENTRIES 30
30 
31 static GQueue history = G_QUEUE_INIT;
33 
34 static void history_save (void)
35 {
36  if (! modified)
37  return;
38 
39  config_clear_section ("history");
40 
41  GList * node = history.head;
42  for (int i = 0; i < MAX_ENTRIES; i ++)
43  {
44  if (! node)
45  break;
46 
47  char name[32];
48  snprintf (name, sizeof name, "entry%d", i);
49  set_string ("history", name, node->data);
50 
51  node = node->next;
52  }
53 
54  modified = FALSE;
55 }
56 
57 static void history_load (void)
58 {
59  if (loaded)
60  return;
61 
62  for (int i = 0; ; i ++)
63  {
64  char name[32];
65  snprintf (name, sizeof name, "entry%d", i);
66  char * path = get_string ("history", name);
67 
68  if (! path[0])
69  {
70  g_free (path);
71  break;
72  }
73 
74  g_queue_push_tail (& history, path);
75  }
76 
77  loaded = TRUE;
78  hook_associate ("config save", (HookFunction) history_save, NULL);
79 }
80 
81 void history_cleanup (void)
82 {
83  if (! loaded)
84  return;
85 
86  hook_dissociate ("config save", (HookFunction) history_save);
87 
88  g_queue_foreach (& history, (GFunc) g_free, NULL);
89  g_queue_clear (& history);
90 
91  loaded = FALSE;
92  modified = FALSE;
93 }
94 
95 const char * history_get (int entry)
96 {
97  history_load ();
98  return g_queue_peek_nth (& history, entry);
99 }
100 
101 void history_add (const char * path)
102 {
103  history_load ();
104 
105  GList * next;
106  for (GList * node = history.head; node; node = next)
107  {
108  next = node->next;
109  if (! strcmp (node->data, path))
110  {
111  g_free (node->data);
112  g_queue_delete_link (& history, node);
113  }
114  }
115 
116  g_queue_push_head (& history, g_strdup (path));
117  modified = TRUE;
118 }
void config_clear_section(const char *section)
Definition: config.c:169
static void history_load(void)
Definition: history.c:57
char * get_string(const char *section, const char *name)
Definition: config.c:270
void set_string(const char *section, const char *name, const char *value)
Definition: config.c:221
void(* HookFunction)(void *data, void *user)
Definition: hook.h:23
EXPORT void hook_associate(const char *name, HookFunction func, void *user)
Definition: hook.c:36
#define FALSE
Definition: core.h:35
Index Index bool_t
Definition: playlist-api.h:122
#define hook_dissociate(n, f)
Definition: hook.h:34
const char * name
Definition: plugin-init.c:38
#define NULL
Definition: core.h:27
#define TRUE
Definition: core.h:37
static GQueue history
Definition: history.c:31
static bool_t loaded
Definition: history.c:32
static bool_t modified
Definition: history.c:32
#define MAX_ENTRIES
Definition: history.c:29
void history_cleanup(void)
Definition: history.c:81
static void history_save(void)
Definition: history.c:34
Index Index play entry
Definition: playlist-api.h:144
void history_add(const char *path)
Definition: history.c:101
const char * history_get(int entry)
Definition: history.c:95