Code

po: improved translatable strings for easier translation
[ncmpc.git] / src / screen_file.c
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"
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <glib.h>
35 static struct screen_browser browser;
37 static void
38 browse_paint(void);
40 static void
41 file_repaint(void)
42 {
43         browse_paint();
44         wrefresh(browser.lw->w);
45 }
47 static void
48 file_repaint_if_active(void)
49 {
50         if (screen_is_visible(&screen_browse))
51                 file_repaint();
52 }
54 /* the db have changed -> update the filelist */
55 static void
56 file_changed_callback(mpdclient_t *c, G_GNUC_UNUSED int event,
57                       G_GNUC_UNUSED gpointer data)
58 {
59         browser.filelist = mpdclient_filelist_update(c, browser.filelist);
60 #ifndef NCMPC_MINI
61         sync_highlights(c, browser.filelist);
62 #endif
63         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
65         file_repaint_if_active();
66 }
68 #ifndef NCMPC_MINI
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 }
77 #endif
79 static int
80 handle_save(mpdclient_t *c)
81 {
82         filelist_entry_t *entry;
83         char *defaultname = NULL;
84         int ret;
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         defaultname = utf8_to_locale(defaultname);
99         ret = playlist_save(c, NULL, defaultname);
100         g_free(defaultname);
102         return ret;
105 static int
106 handle_delete(mpdclient_t *c)
108         filelist_entry_t *entry;
109         mpd_InfoEntity *entity;
110         mpd_PlaylistFile *plf;
111         char *str, *buf;
112         int key;
114         if (browser.lw->selected >= filelist_length(browser.filelist))
115                 return -1;
117         entry = filelist_get(browser.filelist, browser.lw->selected);
118         if( entry==NULL || entry->entity==NULL )
119                 return -1;
121         entity = entry->entity;
123         if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
124                 screen_status_printf(_("You can only delete playlists"));
125                 screen_bell();
126                 return -1;
127         }
129         plf = entity->info.playlistFile;
130         str = utf8_to_locale(g_basename(plf->path));
131         buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
132         g_free(str);
133         key = tolower(screen_getch(screen.status_window.w, buf));
134         g_free(buf);
135         if( key != YES[0] ) {
136                 screen_status_printf(_("Aborted"));
137                 return 0;
138         }
140         if( mpdclient_cmd_delete_playlist(c, plf->path) )
141                 return -1;
143         screen_status_printf(_("Playlist deleted"));
144         return 0;
147 static void
148 browse_init(WINDOW *w, int cols, int rows)
150         browser.lw = list_window_init(w, cols, rows);
153 static void
154 browse_resize(int cols, int rows)
156         browser.lw->cols = cols;
157         browser.lw->rows = rows;
160 static void
161 browse_exit(void)
163         if (browser.filelist)
164                 filelist_free(browser.filelist);
165         list_window_free(browser.lw);
168 static void
169 browse_open(G_GNUC_UNUSED mpdclient_t *c)
171         if (browser.filelist == NULL) {
172                 browser.filelist = mpdclient_filelist_get(c, "");
173 #ifndef NCMPC_MINI
174                 mpdclient_install_playlist_callback(c, playlist_changed_callback);
175 #endif
176                 mpdclient_install_browse_callback(c, file_changed_callback);
177         }
180 static const char *
181 browse_title(char *str, size_t size)
183         const char *path = NULL, *prev = NULL, *slash = browser.filelist->path;
184         char *path_locale;
186         /* determine the last 2 parts of the path */
187         while ((slash = strchr(slash, '/')) != NULL) {
188                 path = prev;
189                 prev = ++slash;
190         }
192         if (path == NULL)
193                 /* fall back to full path */
194                 path = browser.filelist->path;
196         path_locale = utf8_to_locale(path);
197         g_snprintf(str, size, _("Browse: %s"), path_locale);
198         g_free(path_locale);
199         return str;
202 static void
203 browse_paint(void)
205         list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
208 static bool
209 browse_cmd(mpdclient_t *c, command_t cmd)
211         switch(cmd) {
212         case CMD_GO_ROOT_DIRECTORY:
213                 browser_change_directory(&browser, c, NULL, "");
214                 file_repaint();
215                 return true;
216         case CMD_GO_PARENT_DIRECTORY:
217                 browser_change_directory(&browser, c, NULL, "..");
218                 file_repaint();
219                 return true;
221         case CMD_LOCATE:
222                 /* don't let browser_cmd() evaluate the locate command
223                    - it's a no-op, and by the way, leads to a
224                    segmentation fault in the current implementation */
225                 return false;
227         case CMD_DELETE:
228                 handle_delete(c);
229                 file_repaint();
230                 break;
231         case CMD_SAVE_PLAYLIST:
232                 handle_save(c);
233                 break;
234         case CMD_SCREEN_UPDATE:
235                 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
236 #ifndef NCMPC_MINI
237                 sync_highlights(c, browser.filelist);
238 #endif
239                 list_window_check_selected(browser.lw,
240                                            filelist_length(browser.filelist));
241                 file_repaint();
242                 return false;
244         case CMD_DB_UPDATE:
245                 if (c->status == NULL)
246                         return true;
248                 if (!c->status->updatingDb) {
249                         if (mpdclient_cmd_db_update(c, browser.filelist->path) == 0) {
250                                 if (strcmp(browser.filelist->path, "")) {
251                                         char *path_locale =
252                                                 utf8_to_locale(browser.filelist->path);
253                                         screen_status_printf(_("Database update of %s started"),
254                                                              path_locale);
255                                         g_free(path_locale);
256                                 } else
257                                         screen_status_printf(_("Database update started"));
259                                 /* set updatingDb to make shure the browse callback gets called
260                                  * even if the updated has finished before status is updated */
261                                 c->status->updatingDb = 1;
262                         }
263                 } else
264                         screen_status_printf(_("Database update running..."));
265                 return true;
267         default:
268                 break;
269         }
271         if (browser_cmd(&browser, c, cmd)) {
272                 if (screen_is_visible(&screen_browse))
273                         file_repaint();
274                 return true;
275         }
277         return false;
280 const struct screen_functions screen_browse = {
281         .init = browse_init,
282         .exit = browse_exit,
283         .open = browse_open,
284         .resize = browse_resize,
285         .paint = browse_paint,
286         .cmd = browse_cmd,
287         .get_title = browse_title,
288 };
290 bool
291 screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
293         const char *slash, *parent;
294         char *allocated = NULL;
295         bool ret;
296         int i;
298         assert(song != NULL);
299         assert(song->file != NULL);
301         if (strstr(song->file, "//") != NULL)
302                 /* an URL? */
303                 return false;
305         /* determine the song's parent directory and go there */
307         slash = strrchr(song->file, '/');
308         if (slash != NULL)
309                 parent = allocated = g_strndup(song->file, slash - song->file);
310         else
311                 parent = "";
313         ret = browser_change_directory(&browser, c, NULL, parent);
314         g_free(allocated);
315         if (!ret)
316                 return false;
318         /* select the specified song */
320         i = filelist_find_song(browser.filelist, song);
321         if (i < 0)
322                 i = 0;
324         list_window_set_selected(browser.lw, i);
326         /* finally, switch to the file screen */
327         screen_switch(&screen_browse, c);
328         return true;