aabac7afa9cc1da019dd3124f27a4eea74e6c574
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;
85 int ret;
87 if (browser.lw->selected >= filelist_length(browser.filelist))
88 return -1;
90 entry = filelist_get(browser.filelist, browser.lw->selected);
91 if( entry && entry->entity ) {
92 mpd_InfoEntity *entity = entry->entity;
93 if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
94 mpd_PlaylistFile *plf = entity->info.playlistFile;
95 defaultname = plf->path;
96 }
97 }
99 defaultname = utf8_to_locale(defaultname);
100 ret = playlist_save(c, NULL, defaultname);
101 g_free(defaultname);
103 return ret;
104 }
106 static int
107 handle_delete(mpdclient_t *c)
108 {
109 filelist_entry_t *entry;
110 mpd_InfoEntity *entity;
111 mpd_PlaylistFile *plf;
112 char *str, *buf;
113 int key;
115 if (browser.lw->selected >= filelist_length(browser.filelist))
116 return -1;
118 entry = filelist_get(browser.filelist, browser.lw->selected);
119 if( entry==NULL || entry->entity==NULL )
120 return -1;
122 entity = entry->entity;
124 if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
125 screen_status_printf(_("You can only delete playlists!"));
126 screen_bell();
127 return -1;
128 }
130 plf = entity->info.playlistFile;
131 str = utf8_to_locale(g_basename(plf->path));
132 buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
133 g_free(str);
134 key = tolower(screen_getch(screen.status_window.w, buf));
135 g_free(buf);
136 if( key != YES[0] ) {
137 screen_status_printf(_("Aborted!"));
138 return 0;
139 }
141 if( mpdclient_cmd_delete_playlist(c, plf->path) )
142 return -1;
144 screen_status_printf(_("Playlist deleted!"));
145 return 0;
146 }
148 static void
149 browse_init(WINDOW *w, int cols, int rows)
150 {
151 browser.lw = list_window_init(w, cols, rows);
152 }
154 static void
155 browse_resize(int cols, int rows)
156 {
157 browser.lw->cols = cols;
158 browser.lw->rows = rows;
159 }
161 static void
162 browse_exit(void)
163 {
164 if (browser.filelist)
165 filelist_free(browser.filelist);
166 list_window_free(browser.lw);
167 }
169 static void
170 browse_open(mpd_unused mpdclient_t *c)
171 {
172 if (browser.filelist == NULL) {
173 browser.filelist = mpdclient_filelist_get(c, "");
174 #ifndef NCMPC_MINI
175 mpdclient_install_playlist_callback(c, playlist_changed_callback);
176 #endif
177 mpdclient_install_browse_callback(c, file_changed_callback);
178 }
179 }
181 static const char *
182 browse_title(char *str, size_t size)
183 {
184 const char *path = NULL, *prev = NULL, *slash = browser.filelist->path;
185 char *path_locale;
187 /* determine the last 2 parts of the path */
188 while ((slash = strchr(slash, '/')) != NULL) {
189 path = prev;
190 prev = ++slash;
191 }
193 if (path == NULL)
194 /* fall back to full path */
195 path = browser.filelist->path;
197 path_locale = utf8_to_locale(path);
198 g_snprintf(str, size, _("Browse: %s"), path_locale);
199 g_free(path_locale);
200 return str;
201 }
203 static void
204 browse_paint(void)
205 {
206 list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
207 }
209 static int
210 browse_cmd(mpdclient_t *c, command_t cmd)
211 {
212 switch(cmd) {
213 case CMD_GO_ROOT_DIRECTORY:
214 browser_change_directory(&browser, c, NULL, "");
215 file_repaint();
216 return 1;
217 case CMD_GO_PARENT_DIRECTORY:
218 browser_change_directory(&browser, c, NULL, "..");
219 file_repaint();
220 return 1;
222 case CMD_DELETE:
223 handle_delete(c);
224 file_repaint();
225 break;
226 case CMD_SAVE_PLAYLIST:
227 handle_save(c);
228 break;
229 case CMD_SCREEN_UPDATE:
230 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
231 #ifndef NCMPC_MINI
232 sync_highlights(c, browser.filelist);
233 #endif
234 list_window_check_selected(browser.lw,
235 filelist_length(browser.filelist));
236 file_repaint();
238 screen_status_printf(_("Screen updated!"));
239 return 0;
241 case CMD_DB_UPDATE:
242 if (c->status == NULL)
243 return 1;
245 if (!c->status->updatingDb) {
246 if (mpdclient_cmd_db_update(c, browser.filelist->path) == 0) {
247 if (strcmp(browser.filelist->path, "")) {
248 char *path_locale =
249 utf8_to_locale(browser.filelist->path);
250 screen_status_printf(_("Database update of %s started!"),
251 path_locale);
252 g_free(path_locale);
253 } else
254 screen_status_printf(_("Database update started!"));
256 /* set updatingDb to make shure the browse callback gets called
257 * even if the updated has finished before status is updated */
258 c->status->updatingDb = 1;
259 }
260 } else
261 screen_status_printf(_("Database update running..."));
262 return 1;
264 default:
265 break;
266 }
268 if (browser_cmd(&browser, c, cmd)) {
269 if (screen_is_visible(&screen_browse))
270 file_repaint();
271 return 1;
272 }
274 return 0;
275 }
277 const struct screen_functions screen_browse = {
278 .init = browse_init,
279 .exit = browse_exit,
280 .open = browse_open,
281 .resize = browse_resize,
282 .paint = browse_paint,
283 .cmd = browse_cmd,
284 .get_title = browse_title,
285 };