d104341679d26093b7cdc4b1c69da9d64cc1e92d
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"
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <glib.h>
35 static struct screen_browser browser;
37 static void
38 browse_paint(void);
40 static void
41 file_repaint(void)
42 {
43 browse_paint();
44 wrefresh(browser.lw->w);
45 }
47 static void
48 file_repaint_if_active(void)
49 {
50 if (screen_is_visible(&screen_browse))
51 file_repaint();
52 }
54 /* the db have changed -> update the filelist */
55 static void
56 file_changed_callback(mpdclient_t *c, G_GNUC_UNUSED int event,
57 G_GNUC_UNUSED gpointer data)
58 {
59 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
60 #ifndef NCMPC_MINI
61 sync_highlights(c, browser.filelist);
62 #endif
63 list_window_check_selected(browser.lw, filelist_length(browser.filelist));
65 file_repaint_if_active();
66 }
68 #ifndef NCMPC_MINI
69 /* the playlist have been updated -> fix highlights */
70 static void
71 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
72 {
73 browser_playlist_changed(&browser, c, event, data);
75 file_repaint_if_active();
76 }
77 #endif
79 static int
80 handle_save(mpdclient_t *c)
81 {
82 filelist_entry_t *entry;
83 char *defaultname = NULL;
84 int ret;
86 if (browser.lw->selected >= filelist_length(browser.filelist))
87 return -1;
89 entry = filelist_get(browser.filelist, browser.lw->selected);
90 if( entry && entry->entity ) {
91 mpd_InfoEntity *entity = entry->entity;
92 if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
93 mpd_PlaylistFile *plf = entity->info.playlistFile;
94 defaultname = plf->path;
95 }
96 }
98 defaultname = utf8_to_locale(defaultname);
99 ret = playlist_save(c, NULL, defaultname);
100 g_free(defaultname);
102 return ret;
103 }
105 static int
106 handle_delete(mpdclient_t *c)
107 {
108 filelist_entry_t *entry;
109 mpd_InfoEntity *entity;
110 mpd_PlaylistFile *plf;
111 char *str, *buf;
112 int key;
114 if (browser.lw->selected >= filelist_length(browser.filelist))
115 return -1;
117 entry = filelist_get(browser.filelist, browser.lw->selected);
118 if( entry==NULL || entry->entity==NULL )
119 return -1;
121 entity = entry->entity;
123 if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
124 screen_status_printf(_("Deleting this item is not possible"));
125 screen_bell();
126 return -1;
127 }
129 plf = entity->info.playlistFile;
130 str = utf8_to_locale(g_basename(plf->path));
131 buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
132 g_free(str);
133 key = tolower(screen_getch(screen.status_window.w, buf));
134 g_free(buf);
135 if( key != YES[0] ) {
136 screen_status_printf(_("Aborted"));
137 return 0;
138 }
140 if( mpdclient_cmd_delete_playlist(c, plf->path) )
141 return -1;
143 screen_status_printf(_("Playlist deleted"));
144 return 0;
145 }
147 static void
148 browse_init(WINDOW *w, int cols, int rows)
149 {
150 browser.lw = list_window_init(w, cols, rows);
151 }
153 static void
154 browse_resize(int cols, int rows)
155 {
156 browser.lw->cols = cols;
157 browser.lw->rows = rows;
158 }
160 static void
161 browse_exit(void)
162 {
163 if (browser.filelist)
164 filelist_free(browser.filelist);
165 list_window_free(browser.lw);
166 }
168 static void
169 browse_open(G_GNUC_UNUSED mpdclient_t *c)
170 {
171 if (browser.filelist == NULL) {
172 browser.filelist = mpdclient_filelist_get(c, "");
173 #ifndef NCMPC_MINI
174 mpdclient_install_playlist_callback(c, playlist_changed_callback);
175 #endif
176 mpdclient_install_browse_callback(c, file_changed_callback);
177 }
178 }
180 static const char *
181 browse_title(char *str, size_t size)
182 {
183 const char *path = NULL, *prev = NULL, *slash = browser.filelist->path;
184 char *path_locale;
186 /* determine the last 2 parts of the path */
187 while ((slash = strchr(slash, '/')) != NULL) {
188 path = prev;
189 prev = ++slash;
190 }
192 if (path == NULL)
193 /* fall back to full path */
194 path = browser.filelist->path;
196 path_locale = utf8_to_locale(path);
197 g_snprintf(str, size, "%s: %s",
198 /* translators: caption of the browser screen */
199 _("Browse"), path_locale);
200 g_free(path_locale);
201 return str;
202 }
204 static void
205 browse_paint(void)
206 {
207 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
208 }
210 static bool
211 browse_cmd(mpdclient_t *c, command_t cmd)
212 {
213 switch(cmd) {
214 case CMD_GO_ROOT_DIRECTORY:
215 browser_change_directory(&browser, c, NULL, "");
216 file_repaint();
217 return true;
218 case CMD_GO_PARENT_DIRECTORY:
219 browser_change_directory(&browser, c, NULL, "..");
220 file_repaint();
221 return true;
223 case CMD_LOCATE:
224 /* don't let browser_cmd() evaluate the locate command
225 - it's a no-op, and by the way, leads to a
226 segmentation fault in the current implementation */
227 return false;
229 case CMD_DELETE:
230 handle_delete(c);
231 file_repaint();
232 break;
233 case CMD_SAVE_PLAYLIST:
234 handle_save(c);
235 break;
236 case CMD_SCREEN_UPDATE:
237 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
238 #ifndef NCMPC_MINI
239 sync_highlights(c, browser.filelist);
240 #endif
241 list_window_check_selected(browser.lw,
242 filelist_length(browser.filelist));
243 file_repaint();
244 return false;
246 case CMD_DB_UPDATE:
247 if (c->status == NULL)
248 return true;
250 if (!c->status->updatingDb) {
251 if (mpdclient_cmd_db_update(c, browser.filelist->path) == 0) {
252 if (strcmp(browser.filelist->path, "")) {
253 char *path_locale =
254 utf8_to_locale(browser.filelist->path);
255 screen_status_printf(_("Database update of %s started"),
256 path_locale);
257 g_free(path_locale);
258 } else
259 screen_status_printf(_("Database update started"));
261 /* set updatingDb to make shure the browse callback gets called
262 * even if the updated has finished before status is updated */
263 c->status->updatingDb = 1;
264 }
265 } else
266 screen_status_printf(_("Database update running..."));
267 return true;
269 default:
270 break;
271 }
273 if (browser_cmd(&browser, c, cmd)) {
274 if (screen_is_visible(&screen_browse))
275 file_repaint();
276 return true;
277 }
279 return false;
280 }
282 const struct screen_functions screen_browse = {
283 .init = browse_init,
284 .exit = browse_exit,
285 .open = browse_open,
286 .resize = browse_resize,
287 .paint = browse_paint,
288 .cmd = browse_cmd,
289 .get_title = browse_title,
290 };
292 bool
293 screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
294 {
295 const char *slash, *parent;
296 char *allocated = NULL;
297 bool ret;
298 int i;
300 assert(song != NULL);
301 assert(song->file != NULL);
303 if (strstr(song->file, "//") != NULL)
304 /* an URL? */
305 return false;
307 /* determine the song's parent directory and go there */
309 slash = strrchr(song->file, '/');
310 if (slash != NULL)
311 parent = allocated = g_strndup(song->file, slash - song->file);
312 else
313 parent = "";
315 ret = browser_change_directory(&browser, c, NULL, parent);
316 g_free(allocated);
317 if (!ret)
318 return false;
320 /* select the specified song */
322 i = filelist_find_song(browser.filelist, song);
323 if (i < 0)
324 i = 0;
326 list_window_set_selected(browser.lw, i);
328 /* finally, switch to the file screen */
329 screen_switch(&screen_browse, c);
330 return true;
331 }