📄 cddb_lookup.c
字号:
intcddb_connection_setup(cddb_conn_t ** conn) { if ((*conn = cddb_new()) == NULL) { fprintf(stderr, "cddb_lookup.c: cddb_connection_setup(): cddb_new error\n"); cddb_thread_state = CDDB_THREAD_ERROR; return 1; } cddb_set_server_name(*conn, options.cddb_server); cddb_set_timeout(*conn, options.cddb_timeout); cddb_set_charset(*conn, "UTF-8"); if (options.cddb_use_proxy) { cddb_http_proxy_enable(*conn); cddb_set_http_proxy_server_name(*conn, options.cddb_proxy); cddb_set_http_proxy_server_port(*conn, options.cddb_proxy_port); } else { cddb_http_proxy_disable(*conn); if (options.cddb_use_http) { cddb_http_enable(*conn); cddb_set_server_port(*conn, 80); } else { cddb_http_disable(*conn); cddb_set_server_port(*conn, 888); } } return 0;}void *cddb_thread(void * arg) { cddb_conn_t * conn = NULL; cddb_disc_t * disc = NULL; cddb_track_t * track = NULL; int i; if (cddb_connection_setup(&conn) == 1) { return NULL; } if ((disc = cddb_disc_new()) == NULL) { fprintf(stderr, "cddb_lookup.c: cddb_thread(): cddb_disc_new error\n"); cddb_thread_state = CDDB_THREAD_ERROR; return NULL; } cddb_disc_set_length(disc, record_length); for (i = 0; i < track_count; i++) { track = cddb_track_new(); cddb_track_set_frame_offset(track, frames[i]); cddb_disc_add_track(disc, track); } record_count = cddb_query(conn, disc); if (record_count < 0) { cddb_thread_state = CDDB_THREAD_ERROR; return NULL; } if (record_count == 0) { cddb_destroy(conn); cddb_disc_destroy(disc); if (cddb_query_aborted) { free(frames); } libcddb_shutdown(); cddb_thread_state = CDDB_THREAD_SUCCESS; return NULL; } if ((records = (cddb_disc_t **)malloc(sizeof(cddb_disc_t *) * record_count)) == NULL) { fprintf(stderr, "cddb_lookup.c: cddb_thread(): malloc error\n"); cddb_thread_state = CDDB_THREAD_ERROR; return NULL; } cddb_read(conn, disc); records[0] = cddb_disc_clone(disc); progress_counter = 1; for (i = 1; i < record_count && !cddb_query_aborted; i++) { cddb_query_next(conn, disc); cddb_read(conn, disc); records[i] = cddb_disc_clone(disc); progress_counter = i + 1; } cddb_destroy(conn); cddb_disc_destroy(disc); if (cddb_query_aborted) { int j; for (j = 0; j < i; j++) { cddb_disc_destroy(records[j]); } libcddb_shutdown(); free(records); free(frames); return NULL; } cddb_thread_state = CDDB_THREAD_SUCCESS; return NULL;}void *cddb_submit_thread(void * arg) { cddb_conn_t * conn = NULL; cddb_disc_t * disc = NULL; cddb_track_t * track = NULL; int i; struct timespec req_time; struct timespec rem_time; req_time.tv_sec = 0; req_time.tv_nsec = 500000000; if (cddb_connection_setup(&conn) == 1) { goto cleanup; } if (options.cddb_email[0] == '\0' || !cddb_set_email_address(conn, options.cddb_email)) { g_idle_add(create_cddb_write_error_dialog, _("The email address provided for submission is invalid.")); goto cleanup; } cddb_http_enable(conn); cddb_set_server_port(conn, 80); if ((disc = cddb_disc_new()) == NULL) { fprintf(stderr, "cddb_lookup.c: cddb_submit_thread(): cddb_disc_new error\n"); goto cleanup; } cddb_disc_set_length(disc, record_length); for (i = 0; i < track_count; i++) { track = cddb_track_new(); cddb_track_set_frame_offset(track, frames[i]); cddb_disc_add_track(disc, track); } if (cddb_disc_calc_discid(disc) == 0) { fprintf(stderr, "cddb_lookup.c: cddb_submit_thread(): cddb_disc_calc_discid error\n"); cddb_disc_destroy(disc); goto cleanup; } cddb_data_locked = 1; g_idle_add(create_cddb_submit_dialog, (gpointer) disc); while (cddb_data_locked) { nanosleep(&req_time, &rem_time); } if (cddb_thread_state == CDDB_THREAD_SUCCESS) { if (!cddb_write(conn, disc)) { g_idle_add(create_cddb_write_error_dialog, _("An error occurred while submitting the record to the CDDB server.")); } } cddb_disc_destroy(disc); cleanup: cddb_destroy(conn); libcddb_shutdown(); free(frames); cddb_thread_state = CDDB_THREAD_FREE; return NULL;}static gintcddb_timeout_callback(gpointer data) { int i; char text[MAXLEN]; if (cddb_query_aborted) { destroy_progress_window(); cddb_thread_state = CDDB_THREAD_FREE; return FALSE; } if (cddb_thread_state == 0) { if (progress_prev < progress_counter) { snprintf(text, MAXLEN, _("%d of %d"), progress_counter, record_count); gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progbar), (double)progress_counter / record_count); gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progbar), text); progress_prev = progress_counter; } return TRUE; } else { destroy_progress_window(); if (cddb_thread_state == -1) { GtkWidget * dialog; dialog = gtk_message_dialog_new(GTK_WINDOW(browser_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, _("An error occurred while attempting " "to connect to the CDDB server.")); gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER); gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT); gtk_container_set_border_width(GTK_CONTAINER(dialog), 5); aqualung_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(dialog); } else if (cddb_thread_state == 1) { if (record_count == 0) { GtkWidget * dialog; dialog = gtk_message_dialog_new(GTK_WINDOW(browser_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE, _("No matching record found.")); gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER); gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT); gtk_container_set_border_width(GTK_CONTAINER(dialog), 5); aqualung_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(dialog); } else { create_cddb_dialog(); for (i = 0; i < record_count; i++) { cddb_disc_destroy(records[i]); } free(records); libcddb_shutdown(); } } } free(frames); cddb_thread_state = CDDB_THREAD_FREE; return FALSE;}voidcddb_get(GtkTreeIter * iter) { cddb_thread_state = CDDB_THREAD_BUSY; iter_record = *iter; if (init_query_data()) { cddb_thread_state = CDDB_THREAD_FREE; return; } create_progress_window(); cddb_query_aborted = 0; progress_counter = 0; progress_prev = 0; AQUALUNG_THREAD_CREATE(cddb_thread_id, NULL, cddb_thread, NULL) g_timeout_add(100, cddb_timeout_callback, NULL);}voidcddb_get_batch(build_record_t * record, int cddb_title, int cddb_artist, int cddb_record) { int i, j; build_track_t * ptrack = NULL; map_t * map_artist = NULL; map_t * map_record = NULL; map_t * map_year = NULL; map_t ** map_tracks = NULL; char tmp[MAXLEN]; cddb_thread_state = CDDB_THREAD_BUSY; if (init_query_data_from_tracklist(record->tracks)) { cddb_thread_state = CDDB_THREAD_FREE; return; } cddb_query_aborted = 0; progress_counter = 0; progress_prev = 0; cddb_thread(NULL); if (cddb_thread_state != CDDB_THREAD_SUCCESS || record_count == 0) { cddb_thread_state = CDDB_THREAD_FREE; return; } if ((map_tracks = (map_t **)malloc(sizeof(map_t *) * track_count)) == NULL) { fprintf(stderr, "cddb_lookup.c: cddb_get_batch(): malloc error\n"); cddb_thread_state = CDDB_THREAD_FREE; return; } for (i = 0; i < track_count; i++) { map_tracks[i] = NULL; } for (i = 0; i < record_count; i++) { if (cddb_artist && !record->artist_valid) { strncpy(tmp, cddb_disc_get_artist(records[i]), MAXLEN-1); map_put(&map_artist, tmp); } if (cddb_record && !record->record_valid) { strncpy(tmp, cddb_disc_get_title(records[i]), MAXLEN-1); map_put(&map_record, tmp); } if (!record->year_valid) { int y = cddb_disc_get_year(records[i]); if (is_valid_year(y)) { snprintf(tmp, MAXLEN-1, "%d", y); map_put(&map_year, tmp); } } if (cddb_title) { for (j = 0, ptrack = record->tracks; ptrack && j < track_count; j++, ptrack = ptrack->next) { if (!ptrack->valid) { strncpy(tmp, cddb_track_get_title(cddb_disc_get_track(records[i], j)), MAXLEN-1); map_put(map_tracks + j, tmp); } } } } if (map_artist) { char * max = map_get_max(map_artist); if (max) { strncpy(record->artist, max, MAXLEN-1); record->artist_valid = 1; } } if (map_record) { char * max = map_get_max(map_record); if (max) { strncpy(record->record, max, MAXLEN-1); record->record_valid = 1; } } if (map_year) { char * max = map_get_max(map_year); if (max) { strncpy(record->year, max, MAXLEN-1); record->year_valid = 1; } } for (j = 0, ptrack = record->tracks; ptrack && j < track_count; j++, ptrack = ptrack->next) { if (!ptrack->valid) { char * max = map_get_max(map_tracks[j]); if (max) { strncpy(ptrack->name, max, MAXLEN-1); ptrack->valid = 1; } } map_free(map_tracks[j]); } map_free(map_artist); map_free(map_record); map_free(map_year); free(map_tracks); for (i = 0; i < record_count; i++) { cddb_disc_destroy(records[i]); } free(records); libcddb_shutdown(); free(frames); cddb_thread_state = CDDB_THREAD_FREE;}voidcddb_submit(GtkTreeIter * iter) { cddb_thread_state = CDDB_THREAD_BUSY; iter_record = *iter; if (init_query_data()) { cddb_thread_state = CDDB_THREAD_FREE; return; } AQUALUNG_THREAD_CREATE(cddb_thread_id, NULL, cddb_submit_thread, NULL)}#endif /* HAVE_CDDB */// vim: shiftwidth=8:tabstop=8:softtabstop=8 :
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -