Code

75e4d163111e4fadec0d503f7577c3bd4aecff46
[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"
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)
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;
143 static void
144 browse_init(WINDOW *w, int cols, int rows)
146         browser.lw = list_window_init(w, cols, rows);
149 static void
150 browse_resize(int cols, int rows)
152         browser.lw->cols = cols;
153         browser.lw->rows = rows;
156 static void
157 browse_exit(void)
159         if (browser.filelist)
160                 filelist_free(browser.filelist);
161         list_window_free(browser.lw);
164 static void
165 browse_open(mpd_unused mpdclient_t *c)
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         }
176 static const char *
177 browse_title(char *str, size_t size)
179         const char *path = NULL, *prev = NULL, *slash = browser.filelist->path;
181         /* determine the last 2 parts of the path */
182         while ((slash = strchr(slash, '/')) != NULL) {
183                 path = prev;
184                 prev = ++slash;
185         }
187         if (path == NULL)
188                 /* fall back to full path */
189                 path = browser.filelist->path;
191         g_snprintf(str, size, _("Browse: %s"), path);
192         return str;
195 static void
196 browse_paint(void)
198         list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
201 static int
202 browse_cmd(mpdclient_t *c, command_t cmd)
204         switch(cmd) {
205         case CMD_GO_ROOT_DIRECTORY:
206                 browser_change_directory(&browser, c, NULL, "");
207                 file_repaint();
208                 return 1;
209         case CMD_GO_PARENT_DIRECTORY:
210                 browser_change_directory(&browser, c, NULL, "..");
211                 file_repaint();
212                 return 1;
214         case CMD_DELETE:
215                 handle_delete(c);
216                 file_repaint();
217                 break;
218         case CMD_SAVE_PLAYLIST:
219                 handle_save(c);
220                 break;
221         case CMD_SCREEN_UPDATE:
222                 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
223 #ifndef NCMPC_MINI
224                 sync_highlights(c, browser.filelist);
225 #endif
226                 list_window_check_selected(browser.lw,
227                                            filelist_length(browser.filelist));
228                 file_repaint();
230                 screen_status_printf(_("Screen updated!"));
231                 return 0;
233         case CMD_DB_UPDATE:
234                 if (c->status == NULL)
235                         return 1;
237                 if (!c->status->updatingDb) {
238                         if (mpdclient_cmd_db_update_utf8(c, browser.filelist->path) == 0) {
239                                 if (strcmp(browser.filelist->path, ""))
240                                         screen_status_printf(_("Database update of %s started!"),
241                                                              browser.filelist->path);
242                                 else
243                                         screen_status_printf(_("Database update started!"));
245                                 /* set updatingDb to make shure the browse callback gets called
246                                  * even if the updated has finished before status is updated */
247                                 c->status->updatingDb = 1;
248                         }
249                 } else
250                         screen_status_printf(_("Database update running..."));
251                 return 1;
253         default:
254                 break;
255         }
257         if (browser_cmd(&browser, c, cmd)) {
258                 file_repaint();
259                 return 1;
260         }
262         return 0;
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         .cmd = browse_cmd,
272         .get_title = browse_title,
273 };