Mercurial > audlegacy
annotate libaudacious/titlestring.c @ 1490:ded11c19cc0a trunk
[svn] - do things in a less hackish way
| author | nenolod |
|---|---|
| date | Thu, 03 Aug 2006 22:54:57 -0700 |
| parents | 705d4c089fce |
| children | 44f67f556b60 |
| rev | line source |
|---|---|
| 0 | 1 /* |
| 2 * Copyright (C) 2001, Espen Skoglund <esk@ira.uka.de> | |
| 3 * Copyright (C) 2001, Haavard Kvaalen <havardk@xmms.org> | |
| 4 * | |
| 5 * This program is free software; you can redistribute it and/or | |
| 6 * modify it under the terms of the GNU General Public License | |
| 7 * as published by the Free Software Foundation; either version 2 | |
| 8 * of the License, or (at your option) any later version. | |
| 9 * | |
| 10 * This program is distributed in the hope that it will be useful, | |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 * GNU General Public License for more details. | |
| 14 * | |
| 15 * You should have received a copy of the GNU General Public License | |
| 16 * along with this program; if not, write to the Free Software | |
|
1458
f12d7e208b43
[svn] Update FSF address in copyright notices. Update autotools templates.
chainsaw
parents:
1350
diff
changeset
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 1459 | 18 * 02110-1301, USA. |
| 0 | 19 * |
| 20 */ | |
| 21 | |
| 22 #ifdef HAVE_CONFIG_H | |
| 23 # include "config.h" | |
| 24 #endif | |
| 25 | |
| 751 | 26 #define GETTEXT_PACKAGE PACKAGE_NAME |
| 0 | 27 |
| 28 #include <glib.h> | |
| 29 #include <glib/gi18n-lib.h> | |
| 30 #include <gtk/gtk.h> | |
| 31 #include <stdio.h> | |
| 32 #include <string.h> | |
| 33 | |
| 34 #include "titlestring.h" | |
| 35 | |
| 36 #define CHECK(input, field) \ | |
| 37 (((gchar *) &input->field - (gchar *) input) < input->__size) | |
| 38 | |
| 39 #define VS(input, field) (CHECK(input, field) ? input->field : NULL) | |
| 40 #define VI(input, field) (CHECK(input, field) ? input->field : 0) | |
| 41 | |
| 42 | |
| 43 BmpTitleInput * | |
| 44 bmp_title_input_new() | |
| 45 { | |
| 46 BmpTitleInput *input; | |
| 47 input = g_new0(BmpTitleInput, 1); | |
| 48 input->__size = XMMS_TITLEINPUT_SIZE; | |
| 49 input->__version = XMMS_TITLEINPUT_VERSION; | |
| 50 return input; | |
| 51 } | |
| 52 | |
| 53 void | |
| 54 bmp_title_input_free(BmpTitleInput * input) | |
| 55 { | |
|
1350
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
56 if (input == NULL) |
| 0 | 57 return; |
| 58 | |
|
1350
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
59 if (input->performer != NULL) |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
60 g_free(input->performer); |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
61 |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
62 if (input->album_name != NULL) |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
63 g_free(input->album_name); |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
64 |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
65 if (input->track_name != NULL) |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
66 g_free(input->track_name); |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
67 |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
68 if (input->date != NULL) |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
69 g_free(input->date); |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
70 |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
71 if (input->genre != NULL) |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
72 g_free(input->genre); |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
73 |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
74 if (input->comment != NULL) |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
75 g_free(input->comment); |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
76 |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
77 if (input->file_name != NULL) |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
78 g_free(input->file_name); |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
79 |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
80 if (input->file_path != NULL) |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
81 g_free(input->file_path); |
|
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
82 |
| 0 | 83 g_free(input); |
| 84 } | |
| 85 | |
| 86 gchar * | |
| 87 xmms_get_titlestring(const gchar * fmt, TitleInput * input) | |
| 88 { | |
| 89 GString *outstr; | |
| 90 const gchar *string; | |
| 91 gchar c, convert[16]; | |
| 92 gint numdigits, numpr, val, i; | |
| 93 gint f_left, f_space, f_zero, someflag, width, precision; | |
| 94 gboolean did_output = FALSE; | |
| 95 gchar digits[] = "0123456789"; | |
| 96 | |
| 97 #define PUTCH(ch) g_string_append_c(outstr, ch) | |
| 98 | |
| 99 #define LEFTPAD(num) \ | |
| 100 G_STMT_START { \ | |
| 101 gint cnt = (num); \ | |
| 102 if ( ! f_left && cnt > 0 ) \ | |
| 103 while ( cnt-- > 0 ) \ | |
| 104 PUTCH(f_zero ? '0' : ' '); \ | |
| 105 } G_STMT_END; | |
| 106 | |
| 107 #define RIGHTPAD(num) \ | |
| 108 G_STMT_START { \ | |
| 109 gint cnt = (num); \ | |
| 110 if ( f_left && cnt > 0 ) \ | |
| 111 while ( cnt-- > 0 ) \ | |
| 112 PUTCH( ' ' ); \ | |
| 113 } G_STMT_END; | |
| 114 | |
| 115 if (fmt == NULL) | |
| 116 return NULL; | |
| 117 | |
| 118 outstr = g_string_new(""); | |
| 119 | |
| 120 for (;;) { | |
| 121 /* Copy characters until we encounter '%'. */ | |
| 122 while ((c = *fmt++) != '%') { | |
| 123 if (c == '\0') | |
| 124 goto Done; | |
| 125 g_string_append_c(outstr, c); | |
| 126 } | |
| 127 | |
| 128 f_left = f_space = f_zero = 0; | |
| 129 someflag = 1; | |
| 130 | |
| 131 | |
| 132 /* Parse flags. */ | |
| 133 while (someflag) { | |
| 134 switch (*fmt) { | |
| 135 case '-': | |
| 136 f_left = 1; | |
| 137 fmt++; | |
| 138 break; | |
| 139 case ' ': | |
| 140 f_space = 1; | |
| 141 fmt++; | |
| 142 break; | |
| 143 case '0': | |
| 144 f_zero = 1; | |
| 145 fmt++; | |
| 146 break; | |
| 147 default: | |
| 148 someflag = 0; | |
| 149 break; | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 | |
| 154 /* Parse field width. */ | |
| 155 if ((c = *fmt) >= '0' && c <= '9') { | |
| 156 width = 0; | |
| 157 while ((c = *fmt++) >= '0' && c <= '9') { | |
| 158 width *= 10; | |
| 159 width += c - '0'; | |
| 160 } | |
| 161 fmt--; | |
| 162 } | |
| 163 else | |
| 164 width = -1; | |
| 165 | |
| 166 | |
| 167 /* Parse precision. */ | |
| 168 if (*fmt == '.') { | |
| 169 if ((c = *++fmt) >= '0' && c <= '9') { | |
| 170 precision = 0; | |
| 171 while ((c = *fmt++) >= '0' && c <= '9') { | |
| 172 precision *= 10; | |
| 173 precision += c - '0'; | |
| 174 } | |
| 175 fmt--; | |
| 176 } | |
| 177 else | |
| 178 precision = -1; | |
| 179 } | |
| 180 else | |
| 181 precision = -1; | |
| 182 | |
| 183 | |
| 184 /* Parse format conversion. */ | |
| 185 switch (c = *fmt++) { | |
| 186 case '}': /* close optional, just ignore */ | |
| 187 continue; | |
| 188 | |
| 189 case '{':{ /* optional entry: %{n:...%} */ | |
| 190 char n = *fmt++; | |
| 191 if (!((n == 'a' && VS(input, album_name)) || | |
| 192 (n == 'c' && VS(input, comment)) || | |
| 193 (n == 'd' && VS(input, date)) || | |
| 194 (n == 'e' && VS(input, file_ext)) || | |
| 195 (n == 'f' && VS(input, file_name)) || | |
| 196 (n == 'F' && VS(input, file_path)) || | |
| 197 (n == 'g' && VS(input, genre)) || | |
| 198 (n == 'n' && VI(input, track_number)) || | |
| 199 (n == 'p' && VS(input, performer)) || | |
| 200 (n == 't' && VS(input, track_name)))) { | |
| 201 int nl = 0; | |
| 202 char c; | |
| 203 while ((c = *fmt++)) /* until end of string */ | |
| 204 if (c == '}') /* if end of opt */ | |
| 205 if (!nl) | |
| 206 break; /* if outmost indent level */ | |
| 207 else | |
| 208 --nl; /* else reduce indent */ | |
| 209 else if (c == '{') | |
| 210 ++nl; /* increase indent */ | |
| 211 } | |
| 212 else | |
| 213 ++fmt; | |
| 214 break; | |
| 215 } | |
| 216 | |
| 217 case 'a': | |
| 218 string = VS(input, album_name); | |
| 219 goto Print_string; | |
| 220 case 'c': | |
| 221 string = VS(input, comment); | |
| 222 goto Print_string; | |
| 223 case 'd': | |
| 224 string = VS(input, date); | |
| 225 goto Print_string; | |
| 226 case 'e': | |
| 227 string = VS(input, file_ext); | |
| 228 goto Print_string; | |
| 229 case 'f': | |
| 230 string = VS(input, file_name); | |
| 231 goto Print_string; | |
| 232 case 'F': | |
| 233 string = VS(input, file_path); | |
| 234 goto Print_string; | |
| 235 case 'g': | |
| 236 string = VS(input, genre); | |
| 237 goto Print_string; | |
| 238 case 'n': | |
| 239 val = VI(input, track_number); | |
| 240 goto Print_number; | |
| 241 case 'p': | |
| 242 string = VS(input, performer); | |
| 243 goto Print_string; | |
| 244 case 't': | |
| 245 string = VS(input, track_name); | |
| 246 | |
| 247 Print_string: | |
| 248 if (string == NULL) | |
| 249 break; | |
| 250 did_output = TRUE; | |
| 251 | |
| 252 numpr = 0; | |
| 253 if (width > 0) { | |
| 254 /* Calculate printed size. */ | |
| 255 numpr = strlen(string); | |
| 256 if (precision >= 0 && precision < numpr) | |
| 257 numpr = precision; | |
| 258 | |
| 259 LEFTPAD(width - numpr); | |
| 260 } | |
| 261 | |
| 262 /* Insert string. */ | |
| 263 if (precision >= 0) { | |
| 264 while (precision-- > 0 && (c = *string++) != '\0') | |
| 265 PUTCH(c); | |
| 266 } | |
| 267 else { | |
| 268 while ((c = *string++) != '\0') | |
| 269 PUTCH(c); | |
| 270 } | |
| 271 | |
| 272 RIGHTPAD(width - numpr); | |
| 273 break; | |
| 274 | |
| 275 case 'y': | |
| 276 val = VI(input, year); | |
| 277 | |
| 278 Print_number: | |
| 279 if (val == 0) | |
| 280 break; | |
| 281 if (c != 'N') | |
| 282 did_output = TRUE; | |
| 283 | |
| 284 /* Create reversed number string. */ | |
| 285 numdigits = 0; | |
| 286 do { | |
| 287 convert[numdigits++] = digits[val % 10]; | |
| 288 val /= 10; | |
| 289 } | |
| 290 while (val > 0); | |
| 291 | |
| 292 numpr = numdigits > precision ? numdigits : precision; | |
| 293 | |
| 294 /* Insert left padding. */ | |
| 295 if (!f_left && width > numpr) { | |
| 296 if (f_zero) | |
| 297 numpr = width; | |
| 298 else | |
| 299 for (i = width - numpr; i-- > 0;) | |
| 300 PUTCH(' '); | |
| 301 } | |
| 302 | |
| 303 /* Insert zero padding. */ | |
| 304 for (i = numpr - numdigits; i-- > 0;) | |
| 305 PUTCH('0'); | |
| 306 | |
| 307 /* Insert number. */ | |
| 308 while (numdigits > 0) | |
| 309 PUTCH(convert[--numdigits]); | |
| 310 | |
| 311 RIGHTPAD(width - numpr); | |
| 312 break; | |
| 313 | |
| 314 case '%': | |
| 315 PUTCH('%'); | |
| 316 break; | |
| 317 | |
| 318 default: | |
| 319 PUTCH('%'); | |
| 320 PUTCH(c); | |
| 321 break; | |
| 322 } | |
| 323 } | |
| 324 | |
| 325 Done: | |
| 326 if (did_output) | |
| 327 return g_string_free(outstr, FALSE); | |
| 328 else | |
| 329 return NULL; | |
| 330 } | |
| 331 | |
| 332 struct _TagDescription { | |
| 333 gchar tag; | |
| 334 gchar *description; | |
| 335 }; | |
| 336 | |
| 337 typedef struct _TagDescription TagDescription; | |
| 338 | |
| 339 static TagDescription tag_descriptions[] = { | |
| 340 {'p', N_("Performer/Artist")}, | |
| 341 {'a', N_("Album")}, | |
| 342 {'g', N_("Genre")}, | |
| 343 {'f', N_("File name")}, | |
| 344 {'F', N_("File path")}, | |
| 345 {'e', N_("File extension")}, | |
| 346 {'t', N_("Track name")}, | |
| 347 {'n', N_("Track number")}, | |
| 348 {'d', N_("Date")}, | |
| 349 {'y', N_("Year")}, | |
| 350 {'c', N_("Comment")} | |
| 351 }; | |
| 352 | |
| 353 gint tag_descriptions_length = | |
| 354 sizeof(tag_descriptions) / sizeof(TagDescription); | |
| 355 | |
| 356 GtkWidget * | |
| 357 xmms_titlestring_descriptions(gchar * tags, gint columns) | |
| 358 { | |
| 359 GtkWidget *table, *label; | |
| 360 gchar tag_str[5]; | |
| 361 gint num = strlen(tags); | |
| 362 gint r = 0, c, i; | |
| 363 | |
| 364 g_return_val_if_fail(tags != NULL, NULL); | |
| 365 g_return_val_if_fail(columns <= num, NULL); | |
| 366 | |
| 367 table = gtk_table_new((num + columns - 1) / columns, columns * 2, FALSE); | |
| 368 gtk_table_set_row_spacings(GTK_TABLE(table), 2); | |
| 369 gtk_table_set_col_spacings(GTK_TABLE(table), 5); | |
| 370 | |
| 371 for (c = 0; c < columns; c++) { | |
| 372 for (r = 0; r < (num + columns - 1 - c) / columns; r++) { | |
| 373 g_snprintf(tag_str, sizeof(tag_str), "%%%c:", *tags); | |
| 374 label = gtk_label_new(tag_str); | |
| 375 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5); | |
| 376 gtk_table_attach(GTK_TABLE(table), label, 2 * c, 2 * c + 1, r, | |
| 377 r + 1, GTK_FILL, GTK_FILL, 0, 0); | |
| 378 gtk_widget_show(label); | |
| 379 | |
| 380 for (i = 0; i < tag_descriptions_length; i++) { | |
| 381 if (*tags == tag_descriptions[i].tag) { | |
| 382 label = gtk_label_new(_(tag_descriptions[i].description)); | |
| 383 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5); | |
| 384 gtk_table_attach(GTK_TABLE(table), label, 2 * c + 1, | |
| 385 2 * c + 2, r, r + 1, | |
| 386 GTK_EXPAND | GTK_FILL, | |
| 387 GTK_EXPAND | GTK_FILL, 0, 0); | |
| 388 gtk_widget_show(label); | |
| 389 break; | |
| 390 } | |
| 391 } | |
| 392 | |
| 393 if (i == tag_descriptions_length) | |
| 394 g_warning("Invalid tag: %c", *tags); | |
| 395 | |
| 396 tags++; | |
| 397 } | |
| 398 | |
| 399 } | |
| 400 | |
| 401 label = gtk_label_new(_("%{n:...%}: Display \"...\" only if element " | |
| 402 "%n is present")); | |
| 403 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5); | |
| 404 gtk_table_attach(GTK_TABLE(table), label, 0, r + 1, | |
| 405 r + 1, r + 2, GTK_FILL, GTK_FILL, 0, 0); | |
| 406 gtk_widget_show(label); | |
| 407 | |
| 408 return table; | |
| 409 } |
