int cmd_new(int argc, char *argv[]) { playlist *p; char *name; char *filename; int i; if (argc > 2) { paint_error("usage: new [name]"); return 1; } /* defaults */ name = "untitled"; filename = NULL; /* was a name specified? */ if (argc == 2) { /* check for existing playlist with name */ for (i = 0; i < mdb.nplaylists; i++) { if (strcmp(mdb.playlists[i]->name, argv[1]) == 0) { paint_error("playlist \"%s\" already exists.", argv[1]); return 2; } } name = argv[1]; asprintf(&filename, "%s/%s.playlist", mdb.playlist_dir, name); if (filename == NULL) err(1, "cmd_new: asprintf(3) failed"); } /* create & setup playlist */ p = playlist_new(); p->needs_saving = true; p->filename = filename; if ((p->name = strdup(name)) == NULL) err(1, "cmd_new: strdup(3) failed"); /* add playlist to media library and update ui */ medialib_playlist_add(p); ui.library->nrows++; if (p->filename != NULL) playlist_save(p); /* redraw */ paint_library(); paint_message("playlist \"%s\" added", name); return 0; }
static void save_playlists_real (void) { int lists = playlist_count (); const char * folder = get_path (AUD_PATH_PLAYLISTS_DIR); /* save playlists */ char * * order = g_malloc (sizeof (char *) * (lists + 1)); GHashTable * saved = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); for (int i = 0; i < lists; i ++) { int id = playlist_get_unique_id (i); order[i] = g_strdup_printf ("%d", id); if (playlist_get_modified (i)) { char * path = g_strdup_printf ("%s/%d.audpl", folder, id); char * uri = filename_to_uri (path); playlist_save (i, uri); playlist_set_modified (i, FALSE); g_free (path); g_free (uri); } g_hash_table_insert (saved, g_strdup_printf ("%d.audpl", id), NULL); } order[lists] = NULL; char * order_string = g_strjoinv (" ", order); g_strfreev (order); GError * error = NULL; char * order_path = g_strdup_printf ("%s/order", get_path (AUD_PATH_PLAYLISTS_DIR)); char * old_order_string; g_file_get_contents (order_path, & old_order_string, NULL, NULL); if (! old_order_string || strcmp (old_order_string, order_string)) { if (! g_file_set_contents (order_path, order_string, -1, & error)) { fprintf (stderr, "Cannot write to %s: %s\n", order_path, error->message); g_error_free (error); } } g_free (order_string); g_free (order_path); g_free (old_order_string); /* clean up deleted playlists and files from old naming scheme */ char * path = make_playlist_path (0); remove (path); g_free (path); DIR * dir = opendir (folder); if (! dir) goto DONE; struct dirent * entry; while ((entry = readdir (dir))) { if (! g_str_has_suffix (entry->d_name, ".audpl") && ! g_str_has_suffix (entry->d_name, ".xspf")) continue; if (! g_hash_table_lookup_extended (saved, entry->d_name, NULL, NULL)) { char * path = g_strdup_printf ("%s/%s", folder, entry->d_name); remove (path); g_free (path); } } closedir (dir); DONE: g_hash_table_destroy (saved); }
int cmd_write(int argc, char *argv[]) { playlist *dup; char *filename; bool forced; bool will_clobber; int i, clobber_index; if (argc > 2) { paint_error("usage: w[!] [name]"); return 1; } forced = (strcmp(argv[0], "w!") == 0); if (argc == 1) { /* "save" */ /* can't save library or filter results */ if (viewing_playlist == mdb.library || viewing_playlist == mdb.filter_results) { paint_error("use \"w name\" when saving psuedo-playlists like library/filter"); return 2; } /* can't save a new playlist that has no name */ if (viewing_playlist->filename == NULL) { paint_error("use \"w name\" for new playlists"); return 3; } /* do the save... */ playlist_save(viewing_playlist); viewing_playlist->needs_saving = false; paint_library(); paint_message("\"%s\" %d songs written", viewing_playlist->filename, viewing_playlist->nfiles); } else { /* "save as" */ /* build filename for playlist */ asprintf(&filename, "%s/%s.playlist", mdb.playlist_dir, argv[1]); if (filename == NULL) err(1, "cmd_write: asprintf(3) failed"); /* check to see if playlist with that name already exists */ will_clobber = false; clobber_index = -1; for (i = 0; i < mdb.nplaylists; i++) { if (mdb.playlists[i]->filename != NULL && strcmp(mdb.playlists[i]->filename, filename) == 0) { will_clobber = true; clobber_index = i; } } if (will_clobber && !forced) { paint_error("playlist with that name exists (use \"w!\" to overwrite)"); free(filename); return 4; } /* if reached here, we're going to do the save-as... */ /* duplicate playlist */ dup = playlist_dup(viewing_playlist, filename, argv[1]); if (will_clobber) medialib_playlist_remove(clobber_index); else ui.library->nrows++; /* * TODO If the original playlist was a new one, with no name (i.e. if * name == NULL) then we should remove that playlist from the medialib * and display here. */ /* do the save-as... */ playlist_save(dup); medialib_playlist_add(dup); dup->needs_saving = false; viewing_playlist->needs_saving = false; paint_library(); paint_message("\"%s\" %d songs written", filename, viewing_playlist->nfiles); } return 0; }