Code

list_window: remove list_window_state_t
[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>
35 #include <ncurses.h>
37 static struct screen_browser browser;
39 static void
40 browse_paint(mpdclient_t *c);
42 static void
43 file_repaint(void)
44 {
45         browse_paint(NULL);
46         wrefresh(browser.lw->w);
47 }
49 static void
50 file_repaint_if_active(void)
51 {
52         if (screen_is_visible(&screen_browse))
53                 file_repaint();
54 }
56 /* the db have changed -> update the filelist */
57 static void
58 file_changed_callback(mpdclient_t *c, mpd_unused int event,
59                       mpd_unused gpointer data)
60 {
61         browser.filelist = mpdclient_filelist_update(c, browser.filelist);
62         sync_highlights(c, browser.filelist);
63         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
65         file_repaint_if_active();
66 }
68 /* the playlist have been updated -> fix highlights */
69 static void
70 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
71 {
72         browser_playlist_changed(&browser, c, event, data);
74         file_repaint_if_active();
75 }
77 static int
78 handle_save(screen_t *screen, mpdclient_t *c)
79 {
80         filelist_entry_t *entry;
81         char *defaultname = NULL;
83         if (browser.lw->selected >= filelist_length(browser.filelist))
84                 return -1;
86         entry = filelist_get(browser.filelist, browser.lw->selected);
87         if( entry && entry->entity ) {
88                 mpd_InfoEntity *entity = entry->entity;
89                 if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
90                         mpd_PlaylistFile *plf = entity->info.playlistFile;
91                         defaultname = plf->path;
92                 }
93         }
95         return playlist_save(screen, c, NULL, defaultname);
96 }
98 static int
99 handle_delete(screen_t *screen, mpdclient_t *c)
101         filelist_entry_t *entry;
102         mpd_InfoEntity *entity;
103         mpd_PlaylistFile *plf;
104         char *str, *buf;
105         int key;
107         if (browser.lw->selected >= filelist_length(browser.filelist))
108                 return -1;
110         entry = filelist_get(browser.filelist, browser.lw->selected);
111         if( entry==NULL || entry->entity==NULL )
112                 return -1;
114         entity = entry->entity;
116         if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
117                 screen_status_printf(_("You can only delete playlists!"));
118                 screen_bell();
119                 return -1;
120         }
122         plf = entity->info.playlistFile;
123         str = utf8_to_locale(g_basename(plf->path));
124         buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
125         g_free(str);
126         key = tolower(screen_getch(screen->status_window.w, buf));
127         g_free(buf);
128         if( key==KEY_RESIZE )
129                 screen_resize();
130         if( key != YES[0] ) {
131                 screen_status_printf(_("Aborted!"));
132                 return 0;
133         }
135         if( mpdclient_cmd_delete_playlist_utf8(c, plf->path) )
136                 return -1;
138         screen_status_printf(_("Playlist deleted!"));
139         return 0;
142 static void
143 browse_init(WINDOW *w, int cols, int rows)
145         browser.lw = list_window_init(w, cols, rows);
148 static void
149 browse_resize(int cols, int rows)
151         browser.lw->cols = cols;
152         browser.lw->rows = rows;
155 static void
156 browse_exit(void)
158         if (browser.filelist)
159                 filelist_free(browser.filelist);
160         list_window_free(browser.lw);
163 static void
164 browse_open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
166         if (browser.filelist == NULL) {
167                 browser.filelist = mpdclient_filelist_get(c, "");
168                 mpdclient_install_playlist_callback(c, playlist_changed_callback);
169                 mpdclient_install_browse_callback(c, file_changed_callback);
170         }
173 static const char *
174 browse_title(char *str, size_t size)
176         char *dirname, *parentdir;
178         dirname = g_path_get_dirname(browser.filelist->path);
179         parentdir = g_path_get_basename(dirname);
181         if( parentdir[0] == '.' && strlen(parentdir) == 1 ) {
182                 parentdir = NULL;
183         }
185         g_snprintf(str, size, _("Browse: %s%s%s"),
186                    parentdir ? parentdir : "",
187                    parentdir ? "/" : "",
188                    g_basename(browser.filelist->path));
189         free(dirname);
190         free(parentdir);
191         return str;
194 static void
195 browse_paint(mpd_unused mpdclient_t *c)
197         list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
200 static int
201 browse_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
203         switch(cmd) {
204         case CMD_GO_ROOT_DIRECTORY:
205                 browser_change_directory(&browser, c, NULL, "");
206                 file_repaint();
207                 return 1;
208         case CMD_GO_PARENT_DIRECTORY:
209                 browser_change_directory(&browser, c, NULL, "..");
210                 file_repaint();
211                 return 1;
213         case CMD_DELETE:
214                 handle_delete(screen, c);
215                 file_repaint();
216                 break;
217         case CMD_SAVE_PLAYLIST:
218                 handle_save(screen, c);
219                 break;
220         case CMD_SCREEN_UPDATE:
221                 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
222                 sync_highlights(c, browser.filelist);
223                 list_window_check_selected(browser.lw,
224                                            filelist_length(browser.filelist));
225                 file_repaint();
227                 screen_status_printf(_("Screen updated!"));
228                 return 0;
230         case CMD_DB_UPDATE:
231                 if (c->status == NULL)
232                         return 1;
234                 if (!c->status->updatingDb) {
235                         if (mpdclient_cmd_db_update_utf8(c, browser.filelist->path) == 0) {
236                                 if (strcmp(browser.filelist->path, ""))
237                                         screen_status_printf(_("Database update of %s started!"),
238                                                              browser.filelist->path);
239                                 else
240                                         screen_status_printf(_("Database update started!"));
242                                 /* set updatingDb to make shure the browse callback gets called
243                                  * even if the updated has finished before status is updated */
244                                 c->status->updatingDb = 1;
245                         }
246                 } else
247                         screen_status_printf(_("Database update running..."));
248                 return 1;
250         default:
251                 break;
252         }
254         if (browser_cmd(&browser, screen, c, cmd)) {
255                 file_repaint();
256                 return 1;
257         }
259         return 0;
262 const struct screen_functions screen_browse = {
263         .init = browse_init,
264         .exit = browse_exit,
265         .open = browse_open,
266         .resize = browse_resize,
267         .paint = browse_paint,
268         .cmd = browse_cmd,
269         .get_title = browse_title,
270 };