Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
history.c
Go to the documentation of this file.
00001 /*
00002  * history.c
00003  * Copyright 2011 John Lindgren
00004  *
00005  * This file is part of Audacious.
00006  *
00007  * Audacious is free software: you can redistribute it and/or modify it under
00008  * the terms of the GNU General Public License as published by the Free Software
00009  * Foundation, version 2 or version 3 of the License.
00010  *
00011  * Audacious is distributed in the hope that it will be useful, but WITHOUT ANY
00012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00013  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * Audacious. If not, see <http://www.gnu.org/licenses/>.
00017  *
00018  * The Audacious team does not consider modular code linking to Audacious or
00019  * using our public API to be a derived work.
00020  */
00021 
00022 #include <glib.h>
00023 #include <stdio.h>
00024 #include <string.h>
00025 
00026 #include <libaudcore/hook.h>
00027 
00028 #include "main.h"
00029 #include "misc.h"
00030 
00031 #define MAX_ENTRIES 30
00032 
00033 static GQueue history = G_QUEUE_INIT;
00034 static bool_t loaded, modified;
00035 
00036 static void history_save (void)
00037 {
00038     if (! modified)
00039         return;
00040 
00041     config_clear_section ("history");
00042 
00043     GList * node = history.head;
00044     for (int i = 0; i < MAX_ENTRIES; i ++)
00045     {
00046         if (! node)
00047             break;
00048 
00049         char name[32];
00050         snprintf (name, sizeof name, "entry%d", i);
00051         set_string ("history", name, node->data);
00052 
00053         node = node->next;
00054     }
00055 
00056     modified = FALSE;
00057 }
00058 
00059 static void history_load (void)
00060 {
00061     if (loaded)
00062         return;
00063 
00064     for (int i = 0; ; i ++)
00065     {
00066         char name[32];
00067         snprintf (name, sizeof name, "entry%d", i);
00068         char * path = get_string ("history", name);
00069 
00070         if (! path[0])
00071         {
00072             g_free (path);
00073             break;
00074         }
00075 
00076         g_queue_push_tail (& history, path);
00077     }
00078 
00079     loaded = TRUE;
00080     hook_associate ("config save", (HookFunction) history_save, NULL);
00081 }
00082 
00083 void history_cleanup (void)
00084 {
00085     if (! loaded)
00086         return;
00087 
00088     hook_dissociate ("config save", (HookFunction) history_save);
00089 
00090     g_queue_foreach (& history, (GFunc) g_free, NULL);
00091     g_queue_clear (& history);
00092 
00093     loaded = FALSE;
00094     modified = FALSE;
00095 }
00096 
00097 const char * history_get (int entry)
00098 {
00099     history_load ();
00100     return g_queue_peek_nth (& history, entry);
00101 }
00102 
00103 void history_add (const char * path)
00104 {
00105     history_load ();
00106 
00107     GList * next;
00108     for (GList * node = history.head; node; node = next)
00109     {
00110         next = node->next;
00111         if (! strcmp (node->data, path))
00112         {
00113             g_free (node->data);
00114             g_queue_delete_link (& history, node);
00115         }
00116     }
00117 
00118     g_queue_push_head (& history, g_strdup (path));
00119     modified = TRUE;
00120 }