int cmd_tab ( int argc, char ** argv, mpd_Connection * conn ) { mpd_InfoEntity * entity; struct mpd_song *song; mpd_sendListallCommand(conn,""); printErrorAndExit(conn); while((entity = mpd_getNextInfoEntity(conn))) { if(entity->type==MPD_INFO_ENTITY_TYPE_SONG) { song = entity->info.song; if(argc==1) { if(strncmp(song->file,argv[0], strlen(argv[0]))==0) { printf("%s\n", charset_from_utf8(song->file)); } } else printf("%s\n", charset_from_utf8(song->file)); } mpd_freeInfoEntity(entity); } my_finishCommand(conn); return 0; }
int cmd_ls ( int argc, char ** argv, mpd_Connection * conn ) { mpd_InfoEntity * entity; const char *ls; int i = 0; if (argc > 0) ls = charset_to_utf8(argv[i]); else ls = strdup(""); do { mpd_sendLsInfoCommand(conn,ls); printErrorAndExit(conn); while((entity = mpd_getNextInfoEntity(conn))) { if(entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) { mpd_Directory * dir = entity->info.directory; printf("%s\n", charset_from_utf8(dir->path)); } else if(entity->type==MPD_INFO_ENTITY_TYPE_SONG) { struct mpd_song *song = entity->info.song; printf("%s\n", charset_from_utf8(song->file)); } mpd_freeInfoEntity(entity); } my_finishCommand(conn); } while (++i < argc && (ls = charset_to_utf8(argv[i])) != NULL); return 0; }
int cmd_lsplaylists ( int argc, char ** argv, mpd_Connection * conn ) { mpd_InfoEntity * entity; const char * ls = ""; int i = 0; if(argc>0) ls = charset_to_utf8(argv[i]); do { mpd_sendLsInfoCommand(conn,ls); printErrorAndExit(conn); while((entity = mpd_getNextInfoEntity(conn))) { if(entity->type== MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) { mpd_PlaylistFile * pl = entity->info.playlistFile; printf("%s\n", charset_from_utf8(pl->path)); } mpd_freeInfoEntity(entity); } my_finishCommand(conn); } while (++i < argc && (ls = charset_to_utf8(argv[i])) != NULL); return 0; }
int cmd_loadtab ( int argc, char ** argv, mpd_Connection * conn ) { mpd_InfoEntity * entity; char * sp; mpd_PlaylistFile * pl; mpd_sendLsInfoCommand(conn,""); printErrorAndExit(conn); while((entity = mpd_getNextInfoEntity(conn))) { if(entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) { pl = entity->info.playlistFile; sp = pl->path; while((sp = strchr(sp,' '))) *sp = '_'; if(argc==1) { if(strncmp(pl->path,argv[0], strlen(argv[0]))==0) { printf("%s\n", charset_from_utf8(pl->path)); } } } mpd_freeInfoEntity(entity); } my_finishCommand(conn); return 0; }
int cmd_lstab ( int argc, char ** argv, mpd_Connection * conn ) { mpd_InfoEntity * entity; mpd_Directory * dir; mpd_sendListallCommand(conn,""); printErrorAndExit(conn); while((entity = mpd_getNextInfoEntity(conn))) { if(entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) { dir = entity->info.directory; if(argc==1) { if(strncmp(dir->path,argv[0], strlen(argv[0]))==0) { printf("%s\n", charset_from_utf8(dir->path)); } } } mpd_freeInfoEntity(entity); } my_finishCommand(conn); return 0; }
int cmd_list(int argc, char **argv, struct mpd_connection *conn) { enum mpd_tag_type type = get_search_type(argv[0]); if (type == MPD_TAG_UNKNOWN) return -1; --argc; ++argv; mpd_search_db_tags(conn, type); if (argc > 0 && !add_constraints(argc, argv, conn)) return -1; if (!mpd_search_commit(conn)) printErrorAndExit(conn); struct mpd_pair *pair; while ((pair = mpd_recv_pair_tag(conn, type)) != NULL) { printf("%s\n", charset_from_utf8(pair->value)); mpd_return_pair(conn, pair); } my_finishCommand(conn); return 0; }
int cmd_tab ( int argc, char ** argv, struct mpd_connection *conn ) { struct mpd_song *song; char empty[] = ""; char *dir = empty; char *tmp = NULL; if (argc == 1) { if (strrchr(argv[0], '/')) { dir = strdup(argv[0]); if (!dir) return 0; tmp = strrchr(dir, '/'); if (tmp) *tmp = '\0'; // XXX: It's unpossible for tmp to be NULL. } } if (!mpd_send_list_all(conn, dir)) printErrorAndExit(conn); if (*dir) free(dir); while ((song = mpd_recv_song(conn)) != NULL) { if (argc != 1 || strncmp(mpd_song_get_uri(song), argv[0], strlen(argv[0])) == 0) printf("%s\n", charset_from_utf8(mpd_song_get_uri(song))); mpd_song_free(song); } my_finishCommand(conn); return 0; }
int cmd_load ( int argc, char ** argv, mpd_Connection * conn ) { int i; char * sp; char * dp; mpd_InfoEntity * entity; mpd_PlaylistFile * pl; for(i=0;i<argc;i++) { sp = argv[i]; while((sp = strchr(sp,' '))) *sp = '_'; } mpd_sendLsInfoCommand(conn,""); printErrorAndExit(conn); while((entity = mpd_getNextInfoEntity(conn))) { if(entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) { pl = entity->info.playlistFile; dp = sp = strdup(charset_from_utf8(pl->path)); while((sp = strchr(sp,' '))) *sp = '_'; for(i=0;i<argc;i++) { if(strcmp(dp,argv[i])==0) strcpy(argv[i], charset_from_utf8(pl->path)); } free(dp); mpd_freeInfoEntity(entity); } } my_finishCommand(conn); mpd_sendCommandListBegin(conn); printErrorAndExit(conn); for(i=0;i<argc;i++) { printf("loading: %s\n",argv[i]); mpd_sendLoadCommand(conn,charset_to_utf8(argv[i])); printErrorAndExit(conn); } mpd_sendCommandListEnd(conn); my_finishCommand(conn); return 0; }
static void getFilename(RESULT * result) { const char *value = NULL; mpd_update(); if (currentSong != NULL) { value = mpd_song_get_uri(currentSong); } if (value) SetResult(&result, R_STRING, charset_from_utf8(value)); else SetResult(&result, R_STRING, ""); }
static void getAlbum(RESULT * result) { const char *value = NULL; mpd_update(); if (currentSong != NULL) { value = mpd_song_get_tag(currentSong, MPD_TAG_ALBUM, 0); } if (value) SetResult(&result, R_STRING, charset_from_utf8(value)); else SetResult(&result, R_STRING, ""); }
int cmd_list ( int argc, char ** argv, mpd_Connection * conn ) { Constraint *constraints; int numconstraints = 0; int type; int i; char *tag; type = get_search_type(argv[0]); if (type < 0) return -1; argc -= 1; argv += 1; if (argc > 0) { if (argc % 2 != 0) { DIE("arguments must be a tag type and " "optional pairs of search types and queries\n"); } numconstraints = get_constraints(argc, argv, &constraints); if (numconstraints < 0) return -1; } mpd_startFieldSearch(conn, type); if (argc > 0) { for (i = 0; i < numconstraints; i++) { mpd_addConstraintSearch(conn, constraints[i].type, charset_to_utf8(constraints[i].query)); } free(constraints); } mpd_commitSearch(conn); printErrorAndExit(conn); while ((tag = mpd_getNextTag(conn, type))) { printErrorAndExit(conn); printf("%s\n", charset_from_utf8(tag)); free(tag); } my_finishCommand(conn); return 0; }
int cmd_add (int argc, char ** argv, struct mpd_connection *conn ) { if (contains_absolute_path(argc, argv) && !path_prepare(conn)) printErrorAndExit(conn); int i; if (!mpd_command_list_begin(conn, false)) printErrorAndExit(conn); for(i=0;i<argc;i++) { strip_trailing_slash(argv[i]); const char *path = argv[i]; const char *relative_path = to_relative_path(path); if (relative_path != NULL) path = relative_path; if (options.verbosity >= V_VERBOSE) printf("adding: %s\n", path); mpd_send_add(conn, charset_to_utf8(path)); } if (!mpd_command_list_end(conn)) printErrorAndExit(conn); if (!mpd_response_finish(conn)) { #if LIBMPDCLIENT_CHECK_VERSION(2,4,0) if (mpd_connection_get_error(conn) == MPD_ERROR_SERVER) { /* check which of the arguments has failed */ unsigned location = mpd_connection_get_server_error_location(conn); if (location < (unsigned)argc) { /* we've got a valid location from the server */ const char *message = mpd_connection_get_error_message(conn); message = charset_from_utf8(message); fprintf(stderr, "error adding %s: %s\n", argv[location], message); exit(EXIT_FAILURE); } } #endif printErrorAndExit(conn); } return 0; }
void printErrorAndExit(struct mpd_connection *conn) { assert(mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS); const char *message = mpd_connection_get_error_message(conn); #if 0 if (mpd_connection_get_error(conn) == MPD_ERROR_SERVER) /* messages received from the server are UTF-8; the rest is either US-ASCII or locale */ message = charset_from_utf8(message); #endif fprintf(stderr, "mpd error: %s\n", message); mpd_connection_free(conn); exit(EXIT_FAILURE); }
/* Returns a newly-allocated string in the locale's encoding. */ char* id3_string_decode(uint8_t encoding, const char* text) { switch (encoding) { case ID3_ENCODING_ISO_8859_1: return strdup(text); case ID3_ENCODING_UTF8: return charset_from_utf8((char *) text); case ID3_ENCODING_UTF16: return convert_from_utf16((const uint8_t *) text); case ID3_ENCODING_UTF16BE: return convert_from_utf16be((const uint8_t *) text); default: return NULL; } }
static void mpd_printerror(const char *cmd) { const char *s; if (conn) { //assert(mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS); s = mpd_connection_get_error_message(conn); if (mpd_connection_get_error(conn) == MPD_ERROR_SERVER) /* messages received from the server are UTF-8; the rest is either US-ASCII or locale */ s = charset_from_utf8(s); error("[MPD] %s to [%s]:[%i] failed : [%s]", cmd, host, iport, s); mpd_connection_free(conn); conn = NULL; } }
int cmd_loadtab ( int argc, char ** argv, struct mpd_connection *conn ) { struct mpd_playlist *pl; if (argc != 1) return 0; if (!mpd_send_list_meta(conn, NULL)) printErrorAndExit(conn); while ((pl = mpd_recv_playlist(conn)) != NULL) { if (strncmp(mpd_playlist_get_path(pl), argv[0], strlen(argv[0])) == 0) printf("%s\n", charset_from_utf8(mpd_playlist_get_path(pl))); mpd_playlist_free(pl); } my_finishCommand(conn); return 0; }
int cmd_lstab ( int argc, char ** argv, struct mpd_connection *conn ) { struct mpd_directory *dir; if (argc != 1) return 0; if (!mpd_send_list_all(conn, NULL)) printErrorAndExit(conn); while ((dir = mpd_recv_directory(conn)) != NULL) { if (strncmp(mpd_directory_get_path(dir), argv[0], strlen(argv[0])) == 0) printf("%s\n", charset_from_utf8(mpd_directory_get_path(dir))); mpd_directory_free(dir); } my_finishCommand(conn); return 0; }
void print_status(struct mpd_connection *conn) { if (!mpd_command_list_begin(conn, true) || !mpd_send_status(conn) || !mpd_send_current_song(conn) || !mpd_command_list_end(conn)) printErrorAndExit(conn); struct mpd_status *status = mpd_recv_status(conn); if (status == NULL) printErrorAndExit(conn); if (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE) { if (!mpd_response_next(conn)) printErrorAndExit(conn); struct mpd_song *song = mpd_recv_song(conn); if (song != NULL) { pretty_print_song(song); printf("\n"); mpd_song_free(song); } if (mpd_status_get_state(status) == MPD_STATE_PLAY) printf("[playing]"); else printf("[paused] "); printf(" #%i/%u %3i:%02i/%i:%02i (%u%%)\n", mpd_status_get_song_pos(status) + 1, mpd_status_get_queue_length(status), mpd_status_get_elapsed_time(status) / 60, mpd_status_get_elapsed_time(status) % 60, mpd_status_get_total_time(status) / 60, mpd_status_get_total_time(status) % 60, elapsed_percent(status)); } if (mpd_status_get_update_id(status) > 0) printf("Updating DB (#%u) ...\n", mpd_status_get_update_id(status)); if (mpd_status_get_volume(status) >= 0) printf("volume:%3i%c ", mpd_status_get_volume(status), '%'); else { printf("volume: n/a "); } printf("repeat: "); if (mpd_status_get_repeat(status)) printf("on "); else printf("off "); printf("random: "); if (mpd_status_get_random(status)) printf("on "); else printf("off "); printf("single: "); if (mpd_status_get_single(status)) printf("on "); else printf("off "); printf("consume: "); if (mpd_status_get_consume(status)) printf("on \n"); else printf("off\n"); if (mpd_status_get_error(status) != NULL) printf("ERROR: %s\n", charset_from_utf8(mpd_status_get_error(status))); mpd_status_free(status); if (!mpd_response_finish(conn)) printErrorAndExit(conn); }
void mpd_query_status(struct mpd_connection *conn) { struct mpd_status *status; struct mpd_song *song; const struct mpd_audio_format *audio; if (!conn) return; if (!mpd_command_list_begin(conn, true) || !mpd_send_status(conn) || !mpd_send_current_song(conn) || !mpd_command_list_end(conn)) { mpd_printerror("queue_commands"); return; } status = mpd_recv_status(conn); if (status == NULL) { mpd_printerror("recv_status"); return; } if (currentSong != NULL) { mpd_song_free(currentSong); currentSong = NULL; } if (!mpd_response_next(conn)) { mpd_printerror("response_next"); return; } song = mpd_recv_song(conn); if (song != NULL) { currentSong = mpd_song_dup(song); mpd_song_free(song); l_elapsedTimeSec = mpd_status_get_elapsed_time(status); l_totalTimeSec = mpd_status_get_total_time(status); l_bitRate = mpd_status_get_kbit_rate(status); } else { l_elapsedTimeSec = 0; l_totalTimeSec = 0; l_bitRate = 0; } l_state = mpd_status_get_state(status); l_repeatEnabled = mpd_status_get_repeat(status); l_randomEnabled = mpd_status_get_random(status); l_singleEnabled = mpd_status_get_single(status); l_consumeEnabled = mpd_status_get_consume(status); l_volume = mpd_status_get_volume(status); l_currentSongPos = mpd_status_get_song_pos(status) + 1; l_playlistLength = mpd_status_get_queue_length(status); audio = mpd_status_get_audio_format(status); if (audio) { l_sampleRate = audio->sample_rate; l_channels = audio->channels; } else { l_sampleRate = 0; l_channels = 0; } if (mpd_status_get_error(status) != NULL) error("[MPD] query status : %s", charset_from_utf8(mpd_status_get_error(status))); mpd_status_free(status); if (!mpd_response_finish(conn)) { mpd_printerror("response_finish"); return; } }
/** * g_filename_from_uri: * @uri: a uri describing a filename (escaped, encoded in UTF-8). * @hostname: Location to store hostname for the URI, or %NULL. * If there is no hostname in the URI, %NULL will be * stored in this location. * @error: location to store the error occuring, or %NULL to ignore * errors. Any of the errors in #GConvertError may occur. * * Converts an escaped UTF-8 encoded URI to a local filename in the * encoding used for filenames. * * Return value: a newly-allocated string holding the resulting * filename, or %NULL on an error. **/ gchar * filename_from_uri (const char *uri, char **hostname, GError **error) { const char *path_part; const char *host_part; char *unescaped_hostname; char *result; char *filename; int offs; #ifdef G_OS_WIN32 char *p, *slash; #endif if (hostname) *hostname = NULL; if (!has_case_prefix (uri, "file:/")) { g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI, _("The URI '%s' is not an absolute URI using the file scheme"), uri); return NULL; } path_part = uri + strlen ("file:"); if (strchr (path_part, '#') != NULL) { g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI, _("The local file URI '%s' may not include a '#'"), uri); return NULL; } if (has_case_prefix (path_part, "///")) path_part += 2; else if (has_case_prefix (path_part, "//")) { path_part += 2; host_part = path_part; path_part = strchr (path_part, '/'); if (path_part == NULL) { g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI, _("The URI '%s' is invalid"), uri); return NULL; } unescaped_hostname = g_unescape_uri_string (host_part, path_part - host_part, "", TRUE); if (unescaped_hostname == NULL || !hostname_validate (unescaped_hostname)) { g_free (unescaped_hostname); g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI, _("The hostname of the URI '%s' is invalid"), uri); return NULL; } if (hostname) *hostname = unescaped_hostname; else g_free (unescaped_hostname); } filename = g_unescape_uri_string (path_part, -1, "/", FALSE); if (filename == NULL) { g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI, _("The URI '%s' contains invalidly escaped characters"), uri); return NULL; } offs = 0; #ifdef G_OS_WIN32 /* Drop localhost */ if (hostname && *hostname != NULL && g_ascii_strcasecmp (*hostname, "localhost") == 0) { g_free (*hostname); *hostname = NULL; } /* Turn slashes into backslashes, because that's the canonical spelling */ p = filename; while ((slash = strchr (p, '/')) != NULL) { *slash = '\\'; p = slash + 1; } /* Windows URIs with a drive letter can be like "file://host/c:/foo" * or "file://host/c|/foo" (some Netscape versions). In those cases, start * the filename from the drive letter. */ if (g_ascii_isalpha (filename[1])) { if (filename[2] == ':') offs = 1; else if (filename[2] == '|') { filename[2] = ':'; offs = 1; } } #endif /* This is where we differ from glib2.0.6: we use gtkpod's charset_from_utf8() instead of glib's g_filename_from_utf8() */ result = charset_from_utf8 (filename + offs); g_free (filename); return result; }