a934ef2763ffa79c271be662cf05154045d3ef5e
1 /*
2 * (c) 2004 by Kalle Wallin <kaw@linux.se>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
19 #include "config.h"
20 #include "i18n.h"
21 #include "options.h"
22 #include "charset.h"
23 #include "mpdclient.h"
24 #include "command.h"
25 #include "screen.h"
26 #include "screen_utils.h"
27 #include "screen_browser.h"
28 #include "screen_play.h"
29 #include "gcc.h"
31 #include <ctype.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <glib.h>
36 static struct screen_browser browser;
38 static void
39 browse_paint(void);
41 static void
42 file_repaint(void)
43 {
44 browse_paint();
45 wrefresh(browser.lw->w);
46 }
48 static void
49 file_repaint_if_active(void)
50 {
51 if (screen_is_visible(&screen_browse))
52 file_repaint();
53 }
55 /* the db have changed -> update the filelist */
56 static void
57 file_changed_callback(mpdclient_t *c, mpd_unused int event,
58 mpd_unused gpointer data)
59 {
60 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
61 #ifndef NCMPC_MINI
62 sync_highlights(c, browser.filelist);
63 #endif
64 list_window_check_selected(browser.lw, filelist_length(browser.filelist));
66 file_repaint_if_active();
67 }
69 #ifndef NCMPC_MINI
70 /* the playlist have been updated -> fix highlights */
71 static void
72 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
73 {
74 browser_playlist_changed(&browser, c, event, data);
76 file_repaint_if_active();
77 }
78 #endif
80 static int
81 handle_save(mpdclient_t *c)
82 {
83 filelist_entry_t *entry;
84 char *defaultname = NULL;
85 int ret;
87 if (browser.lw->selected >= filelist_length(browser.filelist))
88 return -1;
90 entry = filelist_get(browser.filelist, browser.lw->selected);
91 if( entry && entry->entity ) {
92 mpd_InfoEntity *entity = entry->entity;
93 if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
94 mpd_PlaylistFile *plf = entity->info.playlistFile;
95 defaultname = plf->path;
96 }
97 }
99 defaultname = utf8_to_locale(defaultname);
100 ret = playlist_save(c, NULL, defaultname);
101 g_free(defaultname);
103 return ret;
104 }
106 static int
107 handle_delete(mpdclient_t *c)
108 {
109 filelist_entry_t *entry;
110 mpd_InfoEntity *entity;
111 mpd_PlaylistFile *plf;
112 char *str, *buf;
113 int key;
115 if (browser.lw->selected >= filelist_length(browser.filelist))
116 return -1;
118 entry = filelist_get(browser.filelist, browser.lw->selected);
119 if( entry==NULL || entry->entity==NULL )
120 return -1;
122 entity = entry->entity;
124 if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
125 screen_status_printf(_("You can only delete playlists!"));
126 screen_bell();
127 return -1;
128 }
130 plf = entity->info.playlistFile;
131 str = utf8_to_locale(g_basename(plf->path));
132 buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
133 g_free(str);
134 key = tolower(screen_getch(screen.status_window.w, buf));
135 g_free(buf);
136 if( key != YES[0] ) {
137 screen_status_printf(_("Aborted!"));
138 return 0;
139 }
141 if( mpdclient_cmd_delete_playlist(c, plf->path) )
142 return -1;
144 screen_status_printf(_("Playlist deleted!"));
145 return 0;
146 }
148 static void
149 browse_init(WINDOW *w, int cols, int rows)
150 {
151 browser.lw = list_window_init(w, cols, rows);
152 }
154 static void
155 browse_resize(int cols, int rows)
156 {
157 browser.lw->cols = cols;
158 browser.lw->rows = rows;
159 }
161 static void
162 browse_exit(void)
163 {
164 if (browser.filelist)
165 filelist_free(browser.filelist);
166 list_window_free(browser.lw);
167 }
169 static void
170 browse_open(mpd_unused mpdclient_t *c)
171 {
172 if (browser.filelist == NULL) {
173 browser.filelist = mpdclient_filelist_get(c, "");
174 #ifndef NCMPC_MINI
175 mpdclient_install_playlist_callback(c, playlist_changed_callback);
176 #endif
177 mpdclient_install_browse_callback(c, file_changed_callback);
178 }
179 }
181 static const char *
182 browse_title(char *str, size_t size)
183 {
184 const char *path = NULL, *prev = NULL, *slash = browser.filelist->path;
185 char *path_locale;
187 /* determine the last 2 parts of the path */
188 while ((slash = strchr(slash, '/')) != NULL) {
189 path = prev;
190 prev = ++slash;
191 }
193 if (path == NULL)
194 /* fall back to full path */
195 path = browser.filelist->path;
197 path_locale = utf8_to_locale(path);
198 g_snprintf(str, size, _("Browse: %s"), path_locale);
199 g_free(path_locale);
200 return str;
201 }
203 static void
204 browse_paint(void)
205 {
206 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
207 }
209 static int
210 browse_cmd(mpdclient_t *c, command_t cmd)
211 {
212 switch(cmd) {
213 case CMD_GO_ROOT_DIRECTORY:
214 browser_change_directory(&browser, c, NULL, "");
215 file_repaint();
216 return 1;
217 case CMD_GO_PARENT_DIRECTORY:
218 browser_change_directory(&browser, c, NULL, "..");
219 file_repaint();
220 return 1;
222 case CMD_LOCATE:
223 /* don't let browser_cmd() evaluate the locate command
224 - it's a no-op, and by the way, leads to a
225 segmentation fault in the current implementation */
226 return false;
228 case CMD_DELETE:
229 handle_delete(c);
230 file_repaint();
231 break;
232 case CMD_SAVE_PLAYLIST:
233 handle_save(c);
234 break;
235 case CMD_SCREEN_UPDATE:
236 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
237 #ifndef NCMPC_MINI
238 sync_highlights(c, browser.filelist);
239 #endif
240 list_window_check_selected(browser.lw,
241 filelist_length(browser.filelist));
242 file_repaint();
244 screen_status_printf(_("Screen updated!"));
245 return 0;
247 case CMD_DB_UPDATE:
248 if (c->status == NULL)
249 return 1;
251 if (!c->status->updatingDb) {
252 if (mpdclient_cmd_db_update(c, browser.filelist->path) == 0) {
253 if (strcmp(browser.filelist->path, "")) {
254 char *path_locale =
255 utf8_to_locale(browser.filelist->path);
256 screen_status_printf(_("Database update of %s started!"),
257 path_locale);
258 g_free(path_locale);
259 } else
260 screen_status_printf(_("Database update started!"));
262 /* set updatingDb to make shure the browse callback gets called
263 * even if the updated has finished before status is updated */
264 c->status->updatingDb = 1;
265 }
266 } else
267 screen_status_printf(_("Database update running..."));
268 return 1;
270 default:
271 break;
272 }
274 if (browser_cmd(&browser, c, cmd)) {
275 if (screen_is_visible(&screen_browse))
276 file_repaint();
277 return 1;
278 }
280 return 0;
281 }
283 const struct screen_functions screen_browse = {
284 .init = browse_init,
285 .exit = browse_exit,
286 .open = browse_open,
287 .resize = browse_resize,
288 .paint = browse_paint,
289 .cmd = browse_cmd,
290 .get_title = browse_title,
291 };
293 bool
294 screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
295 {
296 const char *slash, *parent;
297 char *allocated = NULL;
298 bool ret;
299 int i;
301 assert(song != NULL);
302 assert(song->file != NULL);
304 if (strstr(song->file, "//") != NULL)
305 /* an URL? */
306 return false;
308 /* determine the song's parent directory and go there */
310 slash = strrchr(song->file, '/');
311 if (slash != NULL)
312 parent = allocated = g_strndup(song->file, slash - song->file);
313 else
314 parent = "";
316 ret = browser_change_directory(&browser, c, NULL, parent);
317 g_free(allocated);
318 if (!ret)
319 return false;
321 /* select the specified song */
323 i = filelist_find_song(browser.filelist, song);
324 if (i < 0)
325 i = 0;
327 list_window_set_selected(browser.lw, i);
329 /* finally, switch to the file screen */
330 screen_switch(&screen_browse, c);
331 return true;
332 }