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 "support.h"
24 #include "mpdclient.h"
25 #include "command.h"
26 #include "screen.h"
27 #include "screen_utils.h"
28 #include "screen_browser.h"
29 #include "screen_play.h"
30 #include "gcc.h"
32 #include <ctype.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <glib.h>
36 #include <ncurses.h>
38 static struct screen_browser browser;
40 static void
41 browse_paint(mpdclient_t *c);
43 static void
44 file_repaint(void)
45 {
46 browse_paint(NULL);
47 wrefresh(browser.lw->w);
48 }
50 static void
51 file_repaint_if_active(void)
52 {
53 if (screen_is_visible(&screen_browse))
54 file_repaint();
55 }
57 /* the db have changed -> update the filelist */
58 static void
59 file_changed_callback(mpdclient_t *c, mpd_unused int event,
60 mpd_unused gpointer data)
61 {
62 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
63 sync_highlights(c, browser.filelist);
64 list_window_check_selected(browser.lw, filelist_length(browser.filelist));
66 file_repaint_if_active();
67 }
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 }
78 static int
79 handle_save(screen_t *screen, mpdclient_t *c)
80 {
81 filelist_entry_t *entry;
82 char *defaultname = NULL;
84 if (browser.lw->selected >= filelist_length(browser.filelist))
85 return -1;
87 entry = filelist_get(browser.filelist, browser.lw->selected);
88 if( entry && entry->entity ) {
89 mpd_InfoEntity *entity = entry->entity;
90 if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
91 mpd_PlaylistFile *plf = entity->info.playlistFile;
92 defaultname = plf->path;
93 }
94 }
96 return playlist_save(screen, c, NULL, defaultname);
97 }
99 static int
100 handle_delete(screen_t *screen, mpdclient_t *c)
101 {
102 filelist_entry_t *entry;
103 mpd_InfoEntity *entity;
104 mpd_PlaylistFile *plf;
105 char *str, *buf;
106 int key;
108 if (browser.lw->selected >= filelist_length(browser.filelist))
109 return -1;
111 entry = filelist_get(browser.filelist, browser.lw->selected);
112 if( entry==NULL || entry->entity==NULL )
113 return -1;
115 entity = entry->entity;
117 if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
118 screen_status_printf(_("You can only delete playlists!"));
119 screen_bell();
120 return -1;
121 }
123 plf = entity->info.playlistFile;
124 str = utf8_to_locale(basename(plf->path));
125 buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
126 g_free(str);
127 key = tolower(screen_getch(screen->status_window.w, buf));
128 g_free(buf);
129 if( key==KEY_RESIZE )
130 screen_resize();
131 if( key != YES[0] ) {
132 screen_status_printf(_("Aborted!"));
133 return 0;
134 }
136 if( mpdclient_cmd_delete_playlist_utf8(c, plf->path) )
137 return -1;
139 screen_status_printf(_("Playlist deleted!"));
140 return 0;
141 }
143 static void
144 browse_init(WINDOW *w, int cols, int rows)
145 {
146 browser.lw = list_window_init(w, cols, rows);
147 browser.lw_state = list_window_init_state();
148 }
150 static void
151 browse_resize(int cols, int rows)
152 {
153 browser.lw->cols = cols;
154 browser.lw->rows = rows;
155 }
157 static void
158 browse_exit(void)
159 {
160 if (browser.filelist)
161 filelist_free(browser.filelist);
162 list_window_free(browser.lw);
163 list_window_free_state(browser.lw_state);
164 }
166 static void
167 browse_open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
168 {
169 if (browser.filelist == NULL) {
170 browser.filelist = mpdclient_filelist_get(c, "");
171 mpdclient_install_playlist_callback(c, playlist_changed_callback);
172 mpdclient_install_browse_callback(c, file_changed_callback);
173 }
174 }
176 static const char *
177 browse_title(char *str, size_t size)
178 {
179 char *pathcopy;
180 char *parentdir;
182 pathcopy = strdup(browser.filelist->path);
183 parentdir = dirname(pathcopy);
184 parentdir = basename(parentdir);
186 if( parentdir[0] == '.' && strlen(parentdir) == 1 ) {
187 parentdir = NULL;
188 }
190 g_snprintf(str, size, _("Browse: %s%s%s"),
191 parentdir ? parentdir : "",
192 parentdir ? "/" : "",
193 basename(browser.filelist->path));
194 free(pathcopy);
195 return str;
196 }
198 static void
199 browse_paint(mpd_unused mpdclient_t *c)
200 {
201 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
202 }
204 static int
205 browse_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
206 {
207 switch(cmd) {
208 case CMD_GO_ROOT_DIRECTORY:
209 browser_change_directory(&browser, c, NULL, "");
210 file_repaint();
211 return 1;
212 case CMD_GO_PARENT_DIRECTORY:
213 browser_change_directory(&browser, c, NULL, "..");
214 file_repaint();
215 return 1;
217 case CMD_DELETE:
218 handle_delete(screen, c);
219 file_repaint();
220 break;
221 case CMD_SAVE_PLAYLIST:
222 handle_save(screen, c);
223 break;
224 case CMD_SCREEN_UPDATE:
225 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
226 sync_highlights(c, browser.filelist);
227 list_window_check_selected(browser.lw,
228 filelist_length(browser.filelist));
229 file_repaint();
231 screen_status_printf(_("Screen updated!"));
232 return 0;
234 case CMD_DB_UPDATE:
235 if (c->status == NULL)
236 return 1;
238 if (!c->status->updatingDb) {
239 if (mpdclient_cmd_db_update_utf8(c, browser.filelist->path) == 0) {
240 if (strcmp(browser.filelist->path, ""))
241 screen_status_printf(_("Database update of %s started!"),
242 browser.filelist->path);
243 else
244 screen_status_printf(_("Database update started!"));
246 /* set updatingDb to make shure the browse callback gets called
247 * even if the updated has finished before status is updated */
248 c->status->updatingDb = 1;
249 }
250 } else
251 screen_status_printf(_("Database update running..."));
252 return 1;
254 default:
255 break;
256 }
258 if (browser_cmd(&browser, screen, c, cmd)) {
259 file_repaint();
260 return 1;
261 }
263 return 0;
264 }
266 const struct screen_functions screen_browse = {
267 .init = browse_init,
268 .exit = browse_exit,
269 .open = browse_open,
270 .resize = browse_resize,
271 .paint = browse_paint,
272 .cmd = browse_cmd,
273 .get_title = browse_title,
274 };