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>
35 #include <ncurses.h>
37 static struct screen_browser browser;
39 static void
40 browse_paint(void);
42 static void
43 file_repaint(void)
44 {
45 browse_paint();
46 wrefresh(browser.lw->w);
47 }
49 static void
50 file_repaint_if_active(void)
51 {
52 if (screen_is_visible(&screen_browse))
53 file_repaint();
54 }
56 /* the db have changed -> update the filelist */
57 static void
58 file_changed_callback(mpdclient_t *c, mpd_unused int event,
59 mpd_unused gpointer data)
60 {
61 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
62 sync_highlights(c, browser.filelist);
63 list_window_check_selected(browser.lw, filelist_length(browser.filelist));
65 file_repaint_if_active();
66 }
68 /* the playlist have been updated -> fix highlights */
69 static void
70 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
71 {
72 browser_playlist_changed(&browser, c, event, data);
74 file_repaint_if_active();
75 }
77 static int
78 handle_save(screen_t *screen, mpdclient_t *c)
79 {
80 filelist_entry_t *entry;
81 char *defaultname = NULL;
83 if (browser.lw->selected >= filelist_length(browser.filelist))
84 return -1;
86 entry = filelist_get(browser.filelist, browser.lw->selected);
87 if( entry && entry->entity ) {
88 mpd_InfoEntity *entity = entry->entity;
89 if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
90 mpd_PlaylistFile *plf = entity->info.playlistFile;
91 defaultname = plf->path;
92 }
93 }
95 return playlist_save(screen, c, NULL, defaultname);
96 }
98 static int
99 handle_delete(screen_t *screen, mpdclient_t *c)
100 {
101 filelist_entry_t *entry;
102 mpd_InfoEntity *entity;
103 mpd_PlaylistFile *plf;
104 char *str, *buf;
105 int key;
107 if (browser.lw->selected >= filelist_length(browser.filelist))
108 return -1;
110 entry = filelist_get(browser.filelist, browser.lw->selected);
111 if( entry==NULL || entry->entity==NULL )
112 return -1;
114 entity = entry->entity;
116 if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
117 screen_status_printf(_("You can only delete playlists!"));
118 screen_bell();
119 return -1;
120 }
122 plf = entity->info.playlistFile;
123 str = utf8_to_locale(g_basename(plf->path));
124 buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
125 g_free(str);
126 key = tolower(screen_getch(screen->status_window.w, buf));
127 g_free(buf);
128 if( key != YES[0] ) {
129 screen_status_printf(_("Aborted!"));
130 return 0;
131 }
133 if( mpdclient_cmd_delete_playlist_utf8(c, plf->path) )
134 return -1;
136 screen_status_printf(_("Playlist deleted!"));
137 return 0;
138 }
140 static void
141 browse_init(WINDOW *w, int cols, int rows)
142 {
143 browser.lw = list_window_init(w, cols, rows);
144 }
146 static void
147 browse_resize(int cols, int rows)
148 {
149 browser.lw->cols = cols;
150 browser.lw->rows = rows;
151 }
153 static void
154 browse_exit(void)
155 {
156 if (browser.filelist)
157 filelist_free(browser.filelist);
158 list_window_free(browser.lw);
159 }
161 static void
162 browse_open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
163 {
164 if (browser.filelist == NULL) {
165 browser.filelist = mpdclient_filelist_get(c, "");
166 mpdclient_install_playlist_callback(c, playlist_changed_callback);
167 mpdclient_install_browse_callback(c, file_changed_callback);
168 }
169 }
171 static const char *
172 browse_title(char *str, size_t size)
173 {
174 char *dirname, *parentdir;
176 dirname = g_path_get_dirname(browser.filelist->path);
177 parentdir = g_path_get_basename(dirname);
179 if( parentdir[0] == '.' && strlen(parentdir) == 1 ) {
180 parentdir = NULL;
181 }
183 g_snprintf(str, size, _("Browse: %s%s%s"),
184 parentdir ? parentdir : "",
185 parentdir ? "/" : "",
186 g_basename(browser.filelist->path));
187 free(dirname);
188 free(parentdir);
189 return str;
190 }
192 static void
193 browse_paint(void)
194 {
195 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
196 }
198 static int
199 browse_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
200 {
201 switch(cmd) {
202 case CMD_GO_ROOT_DIRECTORY:
203 browser_change_directory(&browser, c, NULL, "");
204 file_repaint();
205 return 1;
206 case CMD_GO_PARENT_DIRECTORY:
207 browser_change_directory(&browser, c, NULL, "..");
208 file_repaint();
209 return 1;
211 case CMD_DELETE:
212 handle_delete(screen, c);
213 file_repaint();
214 break;
215 case CMD_SAVE_PLAYLIST:
216 handle_save(screen, c);
217 break;
218 case CMD_SCREEN_UPDATE:
219 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
220 sync_highlights(c, browser.filelist);
221 list_window_check_selected(browser.lw,
222 filelist_length(browser.filelist));
223 file_repaint();
225 screen_status_printf(_("Screen updated!"));
226 return 0;
228 case CMD_DB_UPDATE:
229 if (c->status == NULL)
230 return 1;
232 if (!c->status->updatingDb) {
233 if (mpdclient_cmd_db_update_utf8(c, browser.filelist->path) == 0) {
234 if (strcmp(browser.filelist->path, ""))
235 screen_status_printf(_("Database update of %s started!"),
236 browser.filelist->path);
237 else
238 screen_status_printf(_("Database update started!"));
240 /* set updatingDb to make shure the browse callback gets called
241 * even if the updated has finished before status is updated */
242 c->status->updatingDb = 1;
243 }
244 } else
245 screen_status_printf(_("Database update running..."));
246 return 1;
248 default:
249 break;
250 }
252 if (browser_cmd(&browser, screen, c, cmd)) {
253 file_repaint();
254 return 1;
255 }
257 return 0;
258 }
260 const struct screen_functions screen_browse = {
261 .init = browse_init,
262 .exit = browse_exit,
263 .open = browse_open,
264 .resize = browse_resize,
265 .paint = browse_paint,
266 .cmd = browse_cmd,
267 .get_title = browse_title,
268 };