Correctly detect changes and non-changes. nicely highlight this by an
[gmpc-tagedit.git] / src / plugin.c
blob911a3797e1c58342f080e163fe80ef0171f7b965
1 #include <config.h>
2 #include <string.h>
3 #include <gdk/gdkkeysyms.h>
4 #include <gmpc/plugin.h>
5 #include <libmpd/libmpd.h>
6 #include <glib.h>
7 #include <tag_c.h>
8 #include "gmpc-mpddata-model-tagedit.h"
10 #define CONFIG_NAME "tagedit"
13 static void __save_myself(void);
14 /* The plugin structure */
15 gmpcPlugin plugin;
18 GtkTreeRowReference *te_ref = NULL;
19 /**
20 * Preferences
22 static void music_dir_cover_art_enable_toggle(GtkWidget *wid)
24 int kk = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wid));
25 cfg_set_single_value_as_int(config, CONFIG_NAME, "enable", kk);
27 static void info_entry_edited(GtkWidget *entry)
29 const char *str = gtk_entry_get_text(GTK_ENTRY(entry));
30 if(str)
32 cfg_set_single_value_as_string(config, CONFIG_NAME, "music_root",(char *)str);
36 static void __pref_destroy(GtkWidget *container)
38 gtk_widget_destroy(gtk_bin_get_child(GTK_BIN(container)));
40 static void __pref_construct(GtkWidget *container)
42 GtkWidget *enable_cg = gtk_check_button_new_with_mnemonic("_Enable mpd's music dir as cover art source");
43 GtkWidget *entry = NULL;
44 char *entry_str = cfg_get_single_value_as_string(config, CONFIG_NAME, "music_root");
45 GtkWidget *vbox = gtk_vbox_new(FALSE,6);
47 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(enable_cg),
48 cfg_get_single_value_as_int_with_default(config, CONFIG_NAME, "enable", TRUE));
50 g_signal_connect(G_OBJECT(enable_cg), "toggled", G_CALLBACK(music_dir_cover_art_enable_toggle), NULL);
51 gtk_box_pack_start(GTK_BOX(vbox), enable_cg, FALSE, FALSE, 0);
52 gtk_container_add(GTK_CONTAINER(container), vbox);
54 entry = gtk_entry_new();
55 if(entry_str)
57 gtk_entry_set_text(GTK_ENTRY(entry), entry_str);
58 cfg_free_string(entry_str);
60 gtk_box_pack_start(GTK_BOX(vbox), gtk_label_new("Music Root:"), FALSE, FALSE,0);
61 gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE,0);
62 g_signal_connect(G_OBJECT(entry), "changed", G_CALLBACK(info_entry_edited), NULL);
64 gtk_widget_show_all(container);
68 /**
69 * Taglib functions
72 static mpd_Song * get_song_from_file(const char *root, const char *filename)
74 TagLib_File *file;
75 TagLib_Tag *tag;
76 mpd_Song *song = NULL;
77 gchar *url = g_build_path(G_DIR_SEPARATOR_S, root, filename,NULL);
79 file = taglib_file_new(url);
80 if(file)
82 char *a = NULL;
83 song = mpd_newSong();
84 song->file = g_strdup(filename);
85 tag = taglib_file_tag(file);
86 if(tag)
88 if((a = taglib_tag_title(tag)) && a[0] != '\0')
89 song->title = g_strdup(a);
90 if((a = taglib_tag_album(tag)) && a[0] != '\0')
91 song->album = g_strdup(a);
92 if((a = taglib_tag_artist(tag)) && a[0] != '\0')
93 song->artist = g_strdup(a);
94 if(taglib_tag_track(tag) > 0)
95 song->track = g_strdup_printf("%i", taglib_tag_track(tag));
96 if((a = taglib_tag_genre(tag)) && a[0] != '\0')
97 song->genre = g_strdup(a);
98 if((a = taglib_tag_comment(tag)) && a[0] != '\0')
99 song->comment = g_strdup(a);
100 if(taglib_tag_year(tag) > 0)
101 song->date = g_strdup_printf("%i", taglib_tag_year(tag));
104 taglib_tag_free_strings();
106 taglib_file_free(file);
108 g_free(url);
109 return song;
111 static gboolean __timeout_mpd_update(gchar *url)
113 printf("update: %s\n",url);
114 mpd_database_update_dir(connection, url);
116 return FALSE;
119 static void save_song_to_file(const char *root, const mpd_Song *song)
121 TagLib_File *file;
122 TagLib_Tag *tag;
124 gchar *url = g_build_path(G_DIR_SEPARATOR_S, root, song->file,NULL);
125 file = taglib_file_new(url);
126 if(file)
128 tag = taglib_file_tag(file);
129 if(song->title) taglib_tag_set_title(tag, song->title);
130 if(song->artist) taglib_tag_set_artist(tag, song->artist);
131 if(song->album) taglib_tag_set_album(tag, song->album);
132 if(song->genre) taglib_tag_set_genre(tag, song->genre);
133 if(song->comment) taglib_tag_set_comment(tag, song->comment);
134 if(song->track){
135 guint track = (guint) g_ascii_strtoll(song->track, NULL, 10);
136 taglib_tag_set_track(tag, track);
138 if(song->date){
139 guint year = (guint) g_ascii_strtoll(song->date, NULL, 10);
140 taglib_tag_set_year(tag, year);
142 taglib_file_save(file);
143 taglib_tag_free_strings();
144 taglib_file_free(file);
145 g_timeout_add_seconds_full(G_PRIORITY_DEFAULT ,1, (GSourceFunc)__timeout_mpd_update, g_strdup(song->file), g_free);
146 // g_timeout_add_seconds_full(G_PRIORITY_DEFAULT ,4, (GSourceFunc)__timeout_mpd_update, g_strdup(song->file), g_free);
149 g_free(url);
156 * Browser View
158 static GtkWidget *browser_box = NULL;
159 static GtkWidget *browser_tree = NULL;
160 static GtkTreeModel *browser_model = NULL;
161 static GtkWidget *entries[7];
162 static gulong signal_entries[7];
165 typedef struct _si {
166 mpd_Song *revert;
167 int changed;
168 }si;
170 static void free_si(si *i)
172 printf("free si\n");
173 if(i->revert) mpd_freeSong(i->revert);
174 g_free(i);
176 static void queue_selected_songs_for_edit(GtkMenuItem *item, GmpcMpdDataTreeview *tree)
178 char *root = cfg_get_single_value_as_string(config, CONFIG_NAME, "music_root");
179 MpdData *data = NULL;
180 GList *selected = NULL;
181 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(tree));
182 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
183 if(!browser_model)
184 browser_model = (GtkTreeModel *)gmpc_mpddata_model_tagedit_new();
185 data = gmpc_mpddata_model_steal_mpd_data(GMPC_MPDDATA_MODEL(browser_model));
186 selected = gtk_tree_selection_get_selected_rows(selection, &model);
187 if(selected)
189 GList *iter = g_list_first(selected);
190 for(;iter; iter = g_list_next(iter))
192 GtkTreeIter titer;
194 if(gtk_tree_model_get_iter(model, &titer, iter->data))
196 mpd_Song *song = NULL;
197 gtk_tree_model_get(model, &titer, MPDDATA_MODEL_COL_MPDSONG, &song, -1);
198 if(song && song->file)
200 mpd_Song *edited = get_song_from_file(root, song->file);
201 printf("adding: %s\n", song->file);
202 if(edited)
204 si *i = g_malloc0(sizeof(*i));
205 data = mpd_new_data_struct_append(data);
206 data->type = MPD_DATA_TYPE_SONG;
207 data->song = edited;
208 i->changed = 0;
209 i->revert = mpd_songDup(data->song);
210 data->userdata =i;
211 data->freefunc = free_si;
216 g_list_foreach (selected, (GFunc)gtk_tree_path_free, NULL);
217 g_list_free (selected);
219 gmpc_mpddata_model_set_mpd_data(GMPC_MPDDATA_MODEL(browser_model),data);
220 g_free(root);
223 static int __song_list_option_menu ( GmpcMpdDataTreeview *tree, GtkMenu *menu)
225 GtkWidget *item;
226 char *entry_str = cfg_get_single_value_as_string(config, CONFIG_NAME, "music_root");
227 if(entry_str)
229 item = gtk_image_menu_item_new_with_label("Queue songs for tag edit");
230 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
231 gtk_image_new_from_stock(GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU));
232 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
233 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(queue_selected_songs_for_edit), tree);
234 g_free(entry_str);
236 return 1;
238 static void browser_selection_changed(GtkTreeSelection *selec, gpointer data)
240 GList *selected = gtk_tree_selection_get_selected_rows(selec, &browser_model);
241 int i =0;
242 for(i=0;i< 6;i++)
244 if(signal_entries[i])
245 g_signal_handler_block(G_OBJECT(entries[i]), signal_entries[i]);
246 if(i<4) gtk_entry_set_text(GTK_ENTRY(entries[i]), "");
247 else gtk_spin_button_set_value(GTK_SPIN_BUTTON(entries[i]), 0.0);
249 if(selected)
251 GList *iter = g_list_first(selected);
252 for(;iter; iter = g_list_next(iter))
254 GtkTreeIter titer;
255 if(gtk_tree_model_get_iter(browser_model, &titer, iter->data))
257 mpd_Song *song = NULL;
258 gtk_tree_model_get(browser_model, &titer, MPDDATA_MODEL_COL_MPDSONG, &song, -1);
259 if(song)
261 if(song->title)
263 if(strlen(gtk_entry_get_text(GTK_ENTRY(entries[0]))) == 0)
264 gtk_entry_set_text(GTK_ENTRY(entries[0]), song->title);
266 if(song->artist)
268 if(strlen(gtk_entry_get_text(GTK_ENTRY(entries[1]))) == 0)
269 gtk_entry_set_text(GTK_ENTRY(entries[1]), song->artist);
271 if(song->album)
273 if(strlen(gtk_entry_get_text(GTK_ENTRY(entries[2]))) == 0)
274 gtk_entry_set_text(GTK_ENTRY(entries[2]), song->album);
276 if(song->genre)
278 if(strlen(gtk_entry_get_text(GTK_ENTRY(entries[3]))) == 0)
279 gtk_entry_set_text(GTK_ENTRY(entries[3]), song->genre);
281 if(song->date)
283 guint year = (guint) g_ascii_strtoll(song->date, NULL, 10);
284 if(year && gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(entries[4])) == 0)
285 gtk_spin_button_set_value(GTK_SPIN_BUTTON(entries[4]), (gdouble) year);
287 if(song->track)
289 guint track = (guint) g_ascii_strtoll(song->track, NULL, 10);
290 if(track && gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(entries[5])) == 0)
291 gtk_spin_button_set_value(GTK_SPIN_BUTTON(entries[5]), (gdouble) track);
297 g_list_foreach (selected, (GFunc)gtk_tree_path_free, NULL);
298 g_list_free (selected);
300 for(i=0;i<6;i++)
302 if(signal_entries[i])
303 g_signal_handler_unblock(G_OBJECT(entries[i]), signal_entries[i]);
308 static void __field_changed(GtkWidget *entry, gpointer data)
310 int id = GPOINTER_TO_INT(data);
312 const gchar *text = NULL;
313 gint value = 0;
314 GList *selected = gtk_tree_selection_get_selected_rows(gtk_tree_view_get_selection(GTK_TREE_VIEW(browser_tree)), &browser_model);
315 if(id < 4)
317 text = gtk_entry_get_text(GTK_ENTRY(entry));
319 else if (id < 6)
321 value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(entry));
323 if(selected)
325 GList *iter = g_list_first(selected);
326 for(;iter; iter = g_list_next(iter))
328 GtkTreeIter titer;
329 if(gtk_tree_model_get_iter(browser_model, &titer, iter->data))
331 mpd_Song *song = NULL;
332 si *ud = NULL;
333 gtk_tree_model_get(browser_model, &titer, MPDDATA_MODEL_COL_MPDSONG, &song,MPDDATA_MODEL_USERDATA, &ud, -1);
334 if(song)
336 if(id == 0) {
337 if(song->title == NULL || strcmp(song->title, text) != 0)
339 if(song->title) g_free(song->title);
340 song->title = g_strdup(text);
341 gtk_tree_model_row_changed(browser_model, (GtkTreePath *)iter->data, &titer);
342 if(ud->revert->title && strcmp(text, ud->revert->title) == 0)
343 ud->changed &= ~1;
344 else
345 ud->changed |= 1;
348 else if(id == 1) {
349 if(song->artist == NULL || strcmp(song->artist, text) != 0)
351 if(song->artist) g_free(song->artist);
352 song->artist = g_strdup(text);
353 gtk_tree_model_row_changed(browser_model, (GtkTreePath *)iter->data, &titer);
354 if(ud->revert->artist && strcmp(text, ud->revert->artist) == 0)
355 ud->changed &= ~(1<<1);
356 else
357 ud->changed |= (1<<1);
360 else if(id == 2) {
361 if(song->album == NULL || strcmp(song->album, text) != 0)
363 if(song->album) g_free(song->album);
364 song->album = g_strdup(text);
365 gtk_tree_model_row_changed(browser_model, (GtkTreePath *)iter->data, &titer);
366 if(ud->revert->album && strcmp(text, ud->revert->album) == 0)
367 ud->changed &= ~(1<<2);
368 else
369 ud->changed |= (1<<2);
372 else if(id == 3) {
373 if(song->genre == NULL || strcmp(song->genre, text) != 0)
375 if(song->genre) g_free(song->genre);
376 song->genre = g_strdup(text);
377 if(ud->revert->genre && strcmp(text, ud->revert->genre) == 0)
378 ud->changed &= ~(1<<3);
379 else
380 ud->changed |= (1<<3);
381 gtk_tree_model_row_changed(browser_model, (GtkTreePath *)iter->data, &titer);
384 else if (id == 4) {
385 guint val = 0;
386 if(song->date)
387 val = (guint)g_ascii_strtoll(song->date, NULL, 10);
388 if(val != value)
390 if(song->date ) g_free(song->date );
391 if(value > 0)
392 song->date = g_strdup_printf("%i", value);
393 else
394 song->date = NULL;
397 if(ud->revert->date == NULL && song->date == NULL) ud->changed &= ~(1<<4);
398 else if(ud->revert->date && song->date && strcmp(song->date, ud->revert->date) == 0)
399 ud->changed &= ~(1<<4);
400 else
401 ud->changed |= (1<<4);
402 gtk_tree_model_row_changed(browser_model, (GtkTreePath *)iter->data, &titer);
405 else if (id == 5) {
406 guint val = 0;
407 if(song->track)
408 val = (guint)g_ascii_strtoll(song->track, NULL, 10);
409 if(val != value)
411 if(song->track) g_free(song->track);
412 if(value > 0)
413 song->track = g_strdup_printf("%i", value);
414 else
415 song->track = NULL;
416 if(song->track == NULL && ud->revert->track == NULL)
417 ud->changed &= ~(1<<5);
418 else if(ud->revert->track && song->track && strcmp(song->track, ud->revert->track) == 0)
419 ud->changed &= ~(1<<5);
420 else
421 ud->changed |= (1<<5);
422 gtk_tree_model_row_changed(browser_model, (GtkTreePath *)iter->data, &titer);
427 printf("changed: %i-%i\n", id, ud->changed);
430 g_list_foreach (selected, (GFunc)gtk_tree_path_free, NULL);
431 g_list_free (selected);
435 static void save_all(GtkWidget *button, gpointer data)
437 char *root = cfg_get_single_value_as_string(config, CONFIG_NAME, "music_root");
438 GtkTreeIter iter;
439 if(root && gtk_tree_model_get_iter_first(browser_model, &iter))
442 mpd_Song *song = NULL;
443 si *ud = NULL;
444 gtk_tree_model_get(browser_model, &iter, MPDDATA_MODEL_COL_MPDSONG, &song, MPDDATA_MODEL_USERDATA, &ud,-1);
445 if(song)
447 /* check if song changed */
448 if(ud->changed > 0)
450 printf("saving: %s\n", song->file);
451 save_song_to_file(root, song);
452 ud->changed = 0;
455 }while(gtk_tree_model_iter_next(browser_model, &iter));
457 if(root)g_free(root);
459 static void clear_all(GtkWidget *button, gpointer data)
461 gmpc_mpddata_model_set_mpd_data(GMPC_MPDDATA_MODEL(browser_model), NULL);
464 static void __edit_columns(void)
466 gmpc_mpddata_treeview_edit_columns(GMPC_MPDDATA_TREEVIEW(browser_tree));
468 static int __button_release_event(GtkTreeView *tree, GdkEventButton *event)
470 if(event->button == 3)
472 /* del, crop */
473 GtkWidget *item;
474 GtkWidget *menu = gtk_menu_new();
475 item = gtk_image_menu_item_new_with_label(("Edit Columns"));
476 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
477 gtk_image_new_from_stock(GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU));
478 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
479 g_signal_connect(G_OBJECT(item), "activate",
480 G_CALLBACK(__edit_columns), NULL);
482 gtk_widget_show_all(menu);
483 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,NULL, NULL,0, event->time);
484 return TRUE;
486 return FALSE;
489 static int __key_release_event(GtkWidget *box , GdkEventKey *event, gpointer data)
491 if(event->keyval == GDK_Page_Up||event->keyval == GDK_Page_Down)
493 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(browser_tree));
494 if(gtk_tree_selection_count_selected_rows(selection) == 1)
496 GList *list = gtk_tree_selection_get_selected_rows(selection, &browser_model);
497 if(list)
499 GtkTreePath *path = list->data;
500 if(event->keyval == GDK_Page_Up)
502 if(gtk_tree_path_prev(path)){
503 gtk_tree_selection_unselect_all(selection);
504 gtk_tree_selection_select_path(selection, path);
506 }else{
507 gtk_tree_path_next(path);
508 gtk_tree_selection_unselect_all(selection);
509 gtk_tree_selection_select_path(selection, path);
512 g_list_foreach (list, (GFunc)gtk_tree_path_free, NULL);
513 g_list_free (list);
515 return TRUE;
517 return FALSE;
520 static void __browser_init ( )
522 GtkWidget *sw = NULL, *label;
523 GtkWidget *table = NULL;
524 browser_box = gtk_vbox_new(FALSE, 6);
525 if(!browser_model)
526 browser_model = (GtkTreeModel *)gmpc_mpddata_model_tagedit_new();
527 sw = gtk_scrolled_window_new(NULL, NULL);
528 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
529 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_ETCHED_IN);
530 browser_tree = gmpc_mpddata_treeview_new(CONFIG_NAME, TRUE, browser_model);
531 gmpc_mpddata_treeview_enable_click_fix(GMPC_MPDDATA_TREEVIEW(browser_tree));
532 gtk_container_add(GTK_CONTAINER(sw), browser_tree);
533 gtk_box_pack_start(GTK_BOX(browser_box), sw, TRUE, TRUE,0);
535 g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(browser_tree))),
536 "changed", G_CALLBACK(browser_selection_changed), NULL);
537 /** Add all the fill in fields */
538 table = gtk_table_new(4,6,FALSE);
539 /* title */
540 label = gtk_label_new("Title:");
541 gtk_misc_set_alignment(GTK_MISC(label), 1.0,0.5);
542 gtk_table_attach(GTK_TABLE(table),label, 0,1,0,1,GTK_SHRINK|GTK_FILL, GTK_SHRINK|GTK_FILL, 0,0);
543 entries[0] = gtk_entry_new();
544 gtk_table_attach(GTK_TABLE(table),entries[0], 1,2,0,1,GTK_EXPAND|GTK_FILL, GTK_SHRINK|GTK_FILL, 0,0);
545 signal_entries[0] = g_signal_connect(G_OBJECT(entries[0]), "changed", G_CALLBACK(__field_changed), GINT_TO_POINTER(0));
546 /* artist */
547 label = gtk_label_new("Artist:");
548 gtk_misc_set_alignment(GTK_MISC(label), 1.0,0.5);
549 gtk_table_attach(GTK_TABLE(table),label, 0,1,1,2,GTK_SHRINK|GTK_FILL, GTK_SHRINK|GTK_FILL, 0,0);
550 entries[1] = gtk_entry_new();
551 gtk_table_attach(GTK_TABLE(table),entries[1], 1,2,1,2,GTK_EXPAND|GTK_FILL, GTK_SHRINK|GTK_FILL, 0,0);
552 signal_entries[1] = g_signal_connect(G_OBJECT(entries[1]), "changed", G_CALLBACK(__field_changed), GINT_TO_POINTER(1));
553 /* album */
554 label = gtk_label_new("Album:");
555 gtk_misc_set_alignment(GTK_MISC(label), 1.0,0.5);
556 gtk_table_attach(GTK_TABLE(table),label, 0,1,2,3,GTK_SHRINK|GTK_FILL, GTK_SHRINK|GTK_FILL, 0,0);
557 entries[2] = gtk_entry_new();
558 gtk_table_attach(GTK_TABLE(table),entries[2], 1,2,2,3,GTK_EXPAND|GTK_FILL, GTK_SHRINK|GTK_FILL, 0,0);
559 signal_entries[2] = g_signal_connect(G_OBJECT(entries[2]), "changed", G_CALLBACK(__field_changed), GINT_TO_POINTER(2));
560 /* album */
561 label = gtk_label_new("Genre:");
562 gtk_misc_set_alignment(GTK_MISC(label), 1.0,0.5);
563 gtk_table_attach(GTK_TABLE(table),label, 0,1,3,4,GTK_SHRINK|GTK_FILL, GTK_SHRINK|GTK_FILL, 0,0);
564 entries[3] = gtk_entry_new();
565 gtk_table_attach(GTK_TABLE(table),entries[3], 1,2,3,4,GTK_EXPAND|GTK_FILL, GTK_SHRINK|GTK_FILL, 0,0);
566 signal_entries[3] = g_signal_connect(G_OBJECT(entries[3]), "changed", G_CALLBACK(__field_changed), GINT_TO_POINTER(3));
567 /* album */
568 label = gtk_label_new("Year:");
569 gtk_misc_set_alignment(GTK_MISC(label), 1.0,0.5);
570 gtk_table_attach(GTK_TABLE(table),label, 3,4,0,1,GTK_SHRINK|GTK_FILL, GTK_SHRINK|GTK_FILL, 0,0);
571 entries[4] = gtk_spin_button_new_with_range(0, 3000,1);
572 gtk_table_attach(GTK_TABLE(table),entries[4], 4,5,0,1,GTK_EXPAND|GTK_FILL, GTK_SHRINK|GTK_FILL, 0,0);
573 signal_entries[4] = g_signal_connect(G_OBJECT(entries[4]), "value-changed", G_CALLBACK(__field_changed), GINT_TO_POINTER(4));
574 g_signal_connect(G_OBJECT(entries[4]), "key-press-event", G_CALLBACK(__key_release_event), NULL);
575 /* album */
576 label = gtk_label_new("Track:");
577 gtk_misc_set_alignment(GTK_MISC(label), 1.0,0.5);
578 gtk_table_attach(GTK_TABLE(table),label, 3,4,1,2,GTK_SHRINK|GTK_FILL, GTK_SHRINK|GTK_FILL, 0,0);
579 entries[5] = gtk_spin_button_new_with_range(0, 3000,1);
580 gtk_table_attach(GTK_TABLE(table),entries[5], 4,5,1,2,GTK_EXPAND|GTK_FILL, GTK_SHRINK|GTK_FILL, 0,0);
581 signal_entries[5] = g_signal_connect(G_OBJECT(entries[5]), "value-changed", G_CALLBACK(__field_changed), GINT_TO_POINTER(5));
583 g_signal_connect(G_OBJECT(entries[5]), "key-press-event", G_CALLBACK(__key_release_event), NULL);
587 label = gtk_button_new_from_stock(GTK_STOCK_SAVE);
588 gtk_table_attach(GTK_TABLE(table),label, 5,6,2,3,GTK_SHRINK|GTK_FILL, GTK_SHRINK|GTK_FILL, 0,0);
589 g_signal_connect(G_OBJECT(label), "clicked", G_CALLBACK(save_all), NULL);
591 label = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
592 gtk_table_attach(GTK_TABLE(table),label, 4,5,2,3,GTK_SHRINK|GTK_FILL, GTK_SHRINK|GTK_FILL, 0,0);
593 g_signal_connect(G_OBJECT(label), "clicked", G_CALLBACK(clear_all), NULL);
595 gtk_box_pack_start(GTK_BOX(browser_box), table, FALSE, TRUE,0);
597 g_signal_connect(G_OBJECT(browser_tree), "button-release-event", G_CALLBACK(__button_release_event), NULL);
599 g_signal_connect(G_OBJECT(browser_box), "key-press-event", G_CALLBACK(__key_release_event), NULL);
600 gtk_widget_show_all(browser_box);
601 g_object_ref(browser_box);
603 static void __browser_add ( GtkWidget *cat_tree)
605 GtkTreePath *path = NULL;
606 GtkTreeIter iter;
607 GtkListStore *pl3_tree = (GtkListStore *)gtk_tree_view_get_model(GTK_TREE_VIEW(cat_tree));
608 gint pos = cfg_get_single_value_as_int_with_default(config, CONFIG_NAME,"position",20);
610 if(!cfg_get_single_value_as_int_with_default(config, CONFIG_NAME, "enable", TRUE)) return;
612 debug_printf(DEBUG_INFO,"Adding at position: %i", pos);
613 playlist3_insert_browser(&iter, pos);
614 gtk_list_store_set(GTK_LIST_STORE(pl3_tree), &iter,
615 PL3_CAT_TYPE, plugin.id,
616 PL3_CAT_TITLE, "Tag editor",
617 PL3_CAT_INT_ID, "",
618 PL3_CAT_ICON_ID, "gtk-edit",
619 PL3_CAT_PROC, TRUE,
620 PL3_CAT_ICON_SIZE,GTK_ICON_SIZE_DND,-1);
622 * Clean up old row reference if it exists
624 if (te_ref)
626 gtk_tree_row_reference_free(te_ref);
627 te_ref = NULL;
630 * create row reference
632 path = gtk_tree_model_get_path(GTK_TREE_MODEL(playlist3_get_category_tree_store()), &iter);
633 if (path)
635 te_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(playlist3_get_category_tree_store()), path);
636 gtk_tree_path_free(path);
640 static void __browser_selected (GtkWidget *container)
642 if(browser_box == NULL)
644 __browser_init();
646 gtk_container_add(GTK_CONTAINER(container), browser_box);
648 static void __browser_unselected ( GtkWidget *container)
650 gtk_container_remove(GTK_CONTAINER(container), gtk_bin_get_child(GTK_BIN(container)));
653 static void __destroy()
655 if(browser_box) {
656 g_object_unref(browser_box);
657 browser_box = NULL;
659 if(browser_model){
660 g_object_unref(browser_model);
661 browser_model = NULL;
666 * Get/Set enabled
668 static int ___get_enabled()
670 return cfg_get_single_value_as_int_with_default(config, CONFIG_NAME, "enable", TRUE);
672 static void ___set_enabled(int enabled)
674 if(enabled)
676 /* Add the browser to the left pane, if there is none to begin with */
677 if(te_ref == NULL)
679 __browser_add(GTK_WIDGET(playlist3_get_category_tree_view()));
682 else if (te_ref)
684 /* Remove it from the left pane */
685 GtkTreePath *path = gtk_tree_row_reference_get_path(te_ref);
686 if (path){
687 GtkTreeIter iter;
688 /* for a save of myself */
689 __save_myself();
690 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(playlist3_get_category_tree_store()), &iter, path)){
691 gtk_list_store_remove(playlist3_get_category_tree_store(), &iter);
693 gtk_tree_path_free(path);
694 gtk_tree_row_reference_free(te_ref);
695 te_ref = NULL;
698 cfg_set_single_value_as_int(config, CONFIG_NAME, "enable", enabled);
700 static void __save_myself(void)
702 /* Save the position in the left tree */
703 if (te_ref)
705 GtkTreePath *path = gtk_tree_row_reference_get_path(te_ref);
706 if(path)
708 gint *indices = gtk_tree_path_get_indices(path);
709 debug_printf(DEBUG_INFO,"Saving myself '%s' to position: %i\n",plugin.name, indices[0]);
710 cfg_set_single_value_as_int(config, CONFIG_NAME,"position",indices[0]);
711 gtk_tree_path_free(path);
715 int plugin_api_version = PLUGIN_API_VERSION;
717 gmpcPlBrowserPlugin __browser = {
718 /* browser */
719 .add = __browser_add,
720 .selected = __browser_selected,
721 .unselected = __browser_unselected,
722 .song_list_option_menu = __song_list_option_menu
724 gmpcPrefPlugin __pref = {
725 .construct = __pref_construct,
726 .destroy = __pref_destroy
728 gmpcPlugin plugin = {
729 .name = "Tag Edit",
730 .version = {PLUGIN_MAJOR_VERSION,PLUGIN_MINOR_VERSION,PLUGIN_MICRO_VERSION},
731 .plugin_type = GMPC_PLUGIN_PL_BROWSER,
733 .save_yourself = __save_myself,
734 .destroy = __destroy,
735 .pref = &__pref,
736 .browser = &__browser,
737 .set_enabled = ___set_enabled,
738 .get_enabled = ___get_enabled