File: sj-genres.c

package info (click to toggle)
sound-juicer 3.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,264 kB
  • ctags: 1,014
  • sloc: sh: 11,456; ansic: 8,534; xml: 805; makefile: 229
file content (193 lines) | stat: -rw-r--r-- 4,507 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright (C) 2007 Jonh Wendell <wendell@bani.com.br>
 *
 * Sound Juicer - sj-genres.c
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Jonh Wendell <wendell@bani.com.br>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <stdlib.h>
#include <glib/gi18n.h>

#include "sj-genres.h"

static const char* const known_genres[] = {
  N_("Ambient"),
  N_("Blues"),
  N_("Classical"),
  N_("Country"),
  N_("Dance"),
  N_("Electronica"),
  N_("Folk"),
  N_("Funk"),
  N_("Jazz"),
  N_("Latin"),
  N_("Pop"),
  N_("Rap"),
  N_("Reggae"),
  N_("Rock"),
  N_("Soul"),
  N_("Spoken Word"),
  NULL
};

static gboolean in_array (const char *str, const char** array) {
  const char **list = array;
  gboolean found = FALSE;

  while (*list != NULL)
    if (strcasecmp (str, *list++) == 0)
      return TRUE;

  return found;
}

static char* genre_filename (void) {
  return g_build_filename (g_get_user_config_dir (),
			   "sound-juicer",
			   "genres",
			   NULL);
}

static char** saved_genres (void) {
  char *filename, *file_contents = NULL;
  char **genres_from_file = NULL;
  gboolean success;
  int len;

  filename = genre_filename ();
  success = g_file_get_contents (filename,
				 &file_contents,
				 NULL,
				 NULL);

  g_free (filename);

  if (success) {
    genres_from_file = g_strsplit (file_contents, "\n", 0);
    len = g_strv_length (genres_from_file);
    if (strlen (genres_from_file[len-1]) == 0) {
      g_free (genres_from_file[len-1]);
      genres_from_file[len-1] = NULL;
    }

    g_free (file_contents);
  }

  return genres_from_file;
}

static GtkTreeModel* create_genre_list (void) {
  GtkListStore *store;
  const char * const *g = known_genres;
  char **genres;

  store = gtk_list_store_new (1, G_TYPE_STRING);

  while (*g != NULL) {
    GtkTreeIter iter;
    gtk_list_store_append (store, &iter);
    gtk_list_store_set (store, &iter, 0, _(*g++), -1);
  }

  genres = saved_genres ();
  if (genres) {
    char **list = genres;

    while (*list != NULL) {
      GtkTreeIter iter;
      gtk_list_store_append (store, &iter);
      gtk_list_store_set (store, &iter, 0, *list++, -1);
    }

    g_strfreev (genres);
  }

  return GTK_TREE_MODEL (store);
}

void setup_genre_entry (GtkWidget *entry) {
  GtkEntryCompletion *completion;

  g_return_if_fail (GTK_IS_ENTRY (entry));

  completion = gtk_entry_get_completion (GTK_ENTRY (entry));
  if (completion != NULL)
    g_object_unref (completion);

  completion = gtk_entry_completion_new ();
  gtk_entry_completion_set_model (completion, create_genre_list ());
  gtk_entry_completion_set_text_column (completion, 0);
  gtk_entry_completion_set_inline_completion (completion, TRUE);
  gtk_entry_set_completion (GTK_ENTRY (entry), completion);
}

void save_genre (GtkWidget *entry) {
  const char *genre;
  char **genres;
  int len;
  char *content, *filename, *path;
  GError *error = NULL;

  g_return_if_fail (GTK_IS_ENTRY (entry));

  genre = gtk_entry_get_text (GTK_ENTRY (entry));

  if (in_array ((const char *)genre, (const char **) known_genres))
    return;

  len = 0;
  genres = saved_genres ();
  if (genres) {
    if (in_array ((const char *)genre, (const char **) genres)) {
      g_strfreev (genres);
      return;
    }
    len = g_strv_length (genres);
  }

  genres = realloc (genres, (len + 2) * sizeof (char *));
  genres[len] = g_strjoin (NULL, genre, "\n", NULL);
  genres[len+1] = NULL;

  content = g_strjoinv ("\n", genres);
  filename = genre_filename ();

  path = g_path_get_dirname (filename);
  g_mkdir_with_parents (path, 0755);
  g_free (path);

  g_file_set_contents (filename,
		       content,
		       -1,
		       &error);

  g_free (filename);
  g_free (content);
  g_strfreev (genres);

  if (error) {
    g_warning (_("Error while saving custom genre: %s"), error->message);
    g_error_free (error);
  }

  setup_genre_entry (entry);
}