1 /*
2 * $Id$
3 *
4 * (c) 2004 by Kalle Wallin <kaw@linux.se>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
21 #include "config.h"
22 #include "ncmpc.h"
23 #include "options.h"
24 #include "support.h"
25 #include "mpdclient.h"
26 #include "command.h"
27 #include "screen.h"
28 #include "screen_utils.h"
29 #include "screen_browser.h"
30 #include "screen_play.h"
31 #include "gcc.h"
33 #include <ctype.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <glib.h>
37 #include <ncurses.h>
39 static struct screen_browser browser;
41 /* the db have changed -> update the filelist */
42 static void
43 file_changed_callback(mpdclient_t *c, mpd_unused int event,
44 mpd_unused gpointer data)
45 {
46 D("screen_file.c> filelist_callback() [%d]\n", event);
47 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
48 sync_highlights(c, browser.filelist);
49 list_window_check_selected(browser.lw, browser.filelist->length);
50 }
52 /* the playlist have been updated -> fix highlights */
53 static void
54 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
55 {
56 browser_playlist_changed(&browser, c, event, data);
57 }
59 static int
60 handle_save(screen_t *screen, mpdclient_t *c)
61 {
62 filelist_entry_t *entry;
63 char *defaultname = NULL;
65 entry = g_list_nth_data(browser.filelist->list, browser.lw->selected);
66 if( entry && entry->entity ) {
67 mpd_InfoEntity *entity = entry->entity;
68 if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
69 mpd_PlaylistFile *plf = entity->info.playlistFile;
70 defaultname = plf->path;
71 }
72 }
74 return playlist_save(screen, c, NULL, defaultname);
75 }
77 static int
78 handle_delete(screen_t *screen, mpdclient_t *c)
79 {
80 filelist_entry_t *entry;
81 mpd_InfoEntity *entity;
82 mpd_PlaylistFile *plf;
83 char *str, *buf;
84 int key;
86 entry = g_list_nth_data(browser.filelist->list,browser. lw->selected);
87 if( entry==NULL || entry->entity==NULL )
88 return -1;
90 entity = entry->entity;
92 if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
93 screen_status_printf(_("You can only delete playlists!"));
94 screen_bell();
95 return -1;
96 }
98 plf = entity->info.playlistFile;
99 str = utf8_to_locale(basename(plf->path));
100 buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
101 g_free(str);
102 key = tolower(screen_getch(screen->status_window.w, buf));
103 g_free(buf);
104 if( key==KEY_RESIZE )
105 screen_resize();
106 if( key != YES[0] ) {
107 screen_status_printf(_("Aborted!"));
108 return 0;
109 }
111 if( mpdclient_cmd_delete_playlist_utf8(c, plf->path) )
112 return -1;
114 screen_status_printf(_("Playlist deleted!"));
115 return 0;
116 }
118 static void
119 browse_init(WINDOW *w, int cols, int rows)
120 {
121 browser.lw = list_window_init(w, cols, rows);
122 browser.lw_state = list_window_init_state();
123 }
125 static void
126 browse_resize(int cols, int rows)
127 {
128 browser.lw->cols = cols;
129 browser.lw->rows = rows;
130 }
132 static void
133 browse_exit(void)
134 {
135 if (browser.filelist)
136 mpdclient_filelist_free(browser.filelist);
137 list_window_free(browser.lw);
138 list_window_free_state(browser.lw_state);
139 }
141 static void
142 browse_open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
143 {
144 if (browser.filelist == NULL) {
145 browser.filelist = mpdclient_filelist_get(c, "");
146 mpdclient_install_playlist_callback(c, playlist_changed_callback);
147 mpdclient_install_browse_callback(c, file_changed_callback);
148 }
149 }
151 static const char *
152 browse_title(char *str, size_t size)
153 {
154 char *pathcopy;
155 char *parentdir;
157 pathcopy = strdup(browser.filelist->path);
158 parentdir = dirname(pathcopy);
159 parentdir = basename(parentdir);
161 if( parentdir[0] == '.' && strlen(parentdir) == 1 ) {
162 parentdir = NULL;
163 }
165 g_snprintf(str, size, _("Browse: %s%s%s"),
166 parentdir ? parentdir : "",
167 parentdir ? "/" : "",
168 basename(browser.filelist->path));
169 free(pathcopy);
170 return str;
171 }
173 static void
174 browse_paint(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
175 {
176 browser.lw->clear = 1;
178 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
179 wnoutrefresh(browser.lw->w);
180 }
182 static void
183 browse_update(screen_t *screen, mpdclient_t *c)
184 {
185 if (browser.filelist->updated) {
186 browse_paint(screen, c);
187 browser.filelist->updated = FALSE;
188 return;
189 }
191 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
192 wnoutrefresh(browser.lw->w);
193 }
195 static int
196 browse_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
197 {
198 switch(cmd) {
199 case CMD_PLAY:
200 browser_handle_enter(&browser, c);
201 return 1;
202 case CMD_GO_ROOT_DIRECTORY:
203 return browser_change_directory(&browser, c, NULL, "");
204 break;
205 case CMD_GO_PARENT_DIRECTORY:
206 return browser_change_directory(&browser, c, NULL, "..");
207 break;
208 case CMD_SELECT:
209 if (browser_handle_select(&browser, c) == 0) {
210 /* continue and select next item... */
211 cmd = CMD_LIST_NEXT;
212 }
213 break;
214 case CMD_DELETE:
215 handle_delete(screen, c);
216 break;
217 case CMD_SAVE_PLAYLIST:
218 handle_save(screen, c);
219 break;
220 case CMD_SCREEN_UPDATE:
221 screen->painted = 0;
222 browser.lw->clear = 1;
223 browser.lw->repaint = 1;
224 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
225 list_window_check_selected(browser.lw,
226 browser.filelist->length);
227 screen_status_printf(_("Screen updated!"));
228 return 1;
229 case CMD_DB_UPDATE:
230 if (c->status == NULL)
231 return 1;
233 if (!c->status->updatingDb) {
234 if (mpdclient_cmd_db_update_utf8(c, browser.filelist->path) == 0) {
235 if (strcmp(browser.filelist->path, ""))
236 screen_status_printf(_("Database update of %s started!"),
237 browser.filelist->path);
238 else
239 screen_status_printf(_("Database update started!"));
241 /* set updatingDb to make shure the browse callback gets called
242 * even if the updated has finished before status is updated */
243 c->status->updatingDb = 1;
244 }
245 } else
246 screen_status_printf(_("Database update running..."));
247 return 1;
248 case CMD_LIST_FIND:
249 case CMD_LIST_RFIND:
250 case CMD_LIST_FIND_NEXT:
251 case CMD_LIST_RFIND_NEXT:
252 return screen_find(screen,
253 browser.lw, browser.filelist->length,
254 cmd, browser_lw_callback,
255 browser.filelist);
256 case CMD_MOUSE_EVENT:
257 return browser_handle_mouse_event(&browser, c);
258 default:
259 break;
260 }
262 return list_window_cmd(browser.lw, browser.filelist->length, cmd);
263 }
265 const struct screen_functions screen_browse = {
266 .init = browse_init,
267 .exit = browse_exit,
268 .open = browse_open,
269 .resize = browse_resize,
270 .paint = browse_paint,
271 .update = browse_update,
272 .cmd = browse_cmd,
273 .get_title = browse_title,
274 };