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;
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 return playlist_save(c, NULL, defaultname);
99 }
101 static int
102 handle_delete(mpdclient_t *c)
103 {
104 filelist_entry_t *entry;
105 mpd_InfoEntity *entity;
106 mpd_PlaylistFile *plf;
107 char *str, *buf;
108 int key;
110 if (browser.lw->selected >= filelist_length(browser.filelist))
111 return -1;
113 entry = filelist_get(browser.filelist, browser.lw->selected);
114 if( entry==NULL || entry->entity==NULL )
115 return -1;
117 entity = entry->entity;
119 if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
120 screen_status_printf(_("You can only delete playlists!"));
121 screen_bell();
122 return -1;
123 }
125 plf = entity->info.playlistFile;
126 str = utf8_to_locale(g_basename(plf->path));
127 buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
128 g_free(str);
129 key = tolower(screen_getch(screen.status_window.w, buf));
130 g_free(buf);
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 }
149 static void
150 browse_resize(int cols, int rows)
151 {
152 browser.lw->cols = cols;
153 browser.lw->rows = rows;
154 }
156 static void
157 browse_exit(void)
158 {
159 if (browser.filelist)
160 filelist_free(browser.filelist);
161 list_window_free(browser.lw);
162 }
164 static void
165 browse_open(mpd_unused mpdclient_t *c)
166 {
167 if (browser.filelist == NULL) {
168 browser.filelist = mpdclient_filelist_get(c, "");
169 #ifndef NCMPC_MINI
170 mpdclient_install_playlist_callback(c, playlist_changed_callback);
171 #endif
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 *dirname, *parentdir;
181 dirname = g_path_get_dirname(browser.filelist->path);
182 parentdir = g_path_get_basename(dirname);
184 if( parentdir[0] == '.' && strlen(parentdir) == 1 ) {
185 parentdir = NULL;
186 }
188 g_snprintf(str, size, _("Browse: %s%s%s"),
189 parentdir ? parentdir : "",
190 parentdir ? "/" : "",
191 g_basename(browser.filelist->path));
192 free(dirname);
193 free(parentdir);
194 return str;
195 }
197 static void
198 browse_paint(void)
199 {
200 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
201 }
203 static int
204 browse_cmd(mpdclient_t *c, command_t cmd)
205 {
206 switch(cmd) {
207 case CMD_GO_ROOT_DIRECTORY:
208 browser_change_directory(&browser, c, NULL, "");
209 file_repaint();
210 return 1;
211 case CMD_GO_PARENT_DIRECTORY:
212 browser_change_directory(&browser, c, NULL, "..");
213 file_repaint();
214 return 1;
216 case CMD_DELETE:
217 handle_delete(c);
218 file_repaint();
219 break;
220 case CMD_SAVE_PLAYLIST:
221 handle_save(c);
222 break;
223 case CMD_SCREEN_UPDATE:
224 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
225 #ifndef NCMPC_MINI
226 sync_highlights(c, browser.filelist);
227 #endif
228 list_window_check_selected(browser.lw,
229 filelist_length(browser.filelist));
230 file_repaint();
232 screen_status_printf(_("Screen updated!"));
233 return 0;
235 case CMD_DB_UPDATE:
236 if (c->status == NULL)
237 return 1;
239 if (!c->status->updatingDb) {
240 if (mpdclient_cmd_db_update_utf8(c, browser.filelist->path) == 0) {
241 if (strcmp(browser.filelist->path, ""))
242 screen_status_printf(_("Database update of %s started!"),
243 browser.filelist->path);
244 else
245 screen_status_printf(_("Database update started!"));
247 /* set updatingDb to make shure the browse callback gets called
248 * even if the updated has finished before status is updated */
249 c->status->updatingDb = 1;
250 }
251 } else
252 screen_status_printf(_("Database update running..."));
253 return 1;
255 default:
256 break;
257 }
259 if (browser_cmd(&browser, c, cmd)) {
260 file_repaint();
261 return 1;
262 }
264 return 0;
265 }
267 const struct screen_functions screen_browse = {
268 .init = browse_init,
269 .exit = browse_exit,
270 .open = browse_open,
271 .resize = browse_resize,
272 .paint = browse_paint,
273 .cmd = browse_cmd,
274 .get_title = browse_title,
275 };