1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/* ncmpc (Ncurses MPD Client)
* (c) 2004-2009 The Music Player Daemon Project
* Project homepage: http://musicpd.org
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "i18n.h"
#include "options.h"
#include "charset.h"
#include "mpdclient.h"
#include "command.h"
#include "screen.h"
#include "screen_utils.h"
#include "screen_browser.h"
#include "screen_play.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
static struct screen_browser browser;
static void
browse_paint(void);
static void
file_repaint(void)
{
browse_paint();
wrefresh(browser.lw->w);
}
static void
file_repaint_if_active(void)
{
if (screen_is_visible(&screen_browse))
file_repaint();
}
/* the db have changed -> update the filelist */
static void
file_changed_callback(mpdclient_t *c, G_GNUC_UNUSED int event,
G_GNUC_UNUSED gpointer data)
{
browser.filelist = mpdclient_filelist_update(c, browser.filelist);
#ifndef NCMPC_MINI
sync_highlights(c, browser.filelist);
#endif
list_window_check_selected(browser.lw, filelist_length(browser.filelist));
file_repaint_if_active();
}
#ifndef NCMPC_MINI
/* the playlist have been updated -> fix highlights */
static void
playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
{
browser_playlist_changed(&browser, c, event, data);
file_repaint_if_active();
}
#endif
static int
handle_save(mpdclient_t *c)
{
filelist_entry_t *entry;
char *defaultname = NULL;
int ret;
if (browser.lw->selected >= filelist_length(browser.filelist))
return -1;
entry = filelist_get(browser.filelist, browser.lw->selected);
if( entry && entry->entity ) {
mpd_InfoEntity *entity = entry->entity;
if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
mpd_PlaylistFile *plf = entity->info.playlistFile;
defaultname = plf->path;
}
}
defaultname = utf8_to_locale(defaultname);
ret = playlist_save(c, NULL, defaultname);
g_free(defaultname);
return ret;
}
static int
handle_delete(mpdclient_t *c)
{
filelist_entry_t *entry;
mpd_InfoEntity *entity;
mpd_PlaylistFile *plf;
char *str, *buf;
int key;
if (browser.lw->selected >= filelist_length(browser.filelist))
return -1;
entry = filelist_get(browser.filelist, browser.lw->selected);
if( entry==NULL || entry->entity==NULL )
return -1;
entity = entry->entity;
if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
/* translators: the "delete" command is only possible
for playlists; the user attempted to delete a song
or a directory or something else */
screen_status_printf(_("Deleting this item is not possible"));
screen_bell();
return -1;
}
plf = entity->info.playlistFile;
str = utf8_to_locale(g_basename(plf->path));
buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
g_free(str);
key = tolower(screen_getch(screen.status_window.w, buf));
g_free(buf);
if( key != YES[0] ) {
/* translators: a dialog was aborted by the user */
screen_status_printf(_("Aborted"));
return 0;
}
if( mpdclient_cmd_delete_playlist(c, plf->path) )
return -1;
/* translators: MPD deleted the playlist, as requested by the
user */
screen_status_printf(_("Playlist deleted"));
return 0;
}
static void
browse_init(WINDOW *w, int cols, int rows)
{
browser.lw = list_window_init(w, cols, rows);
}
static void
browse_resize(int cols, int rows)
{
browser.lw->cols = cols;
browser.lw->rows = rows;
}
static void
browse_exit(void)
{
if (browser.filelist)
filelist_free(browser.filelist);
list_window_free(browser.lw);
}
static void
browse_open(G_GNUC_UNUSED mpdclient_t *c)
{
if (browser.filelist == NULL) {
browser.filelist = mpdclient_filelist_get(c, "");
#ifndef NCMPC_MINI
mpdclient_install_playlist_callback(c, playlist_changed_callback);
#endif
mpdclient_install_browse_callback(c, file_changed_callback);
}
}
static const char *
browse_title(char *str, size_t size)
{
const char *path = NULL, *prev = NULL, *slash = browser.filelist->path;
char *path_locale;
/* determine the last 2 parts of the path */
while ((slash = strchr(slash, '/')) != NULL) {
path = prev;
prev = ++slash;
}
if (path == NULL)
/* fall back to full path */
path = browser.filelist->path;
path_locale = utf8_to_locale(path);
g_snprintf(str, size, "%s: %s",
/* translators: caption of the browser screen */
_("Browse"), path_locale);
g_free(path_locale);
return str;
}
static void
browse_paint(void)
{
list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
}
static bool
browse_cmd(mpdclient_t *c, command_t cmd)
{
switch(cmd) {
case CMD_GO_ROOT_DIRECTORY:
browser_change_directory(&browser, c, NULL, "");
file_repaint();
return true;
case CMD_GO_PARENT_DIRECTORY:
browser_change_directory(&browser, c, NULL, "..");
file_repaint();
return true;
case CMD_LOCATE:
/* don't let browser_cmd() evaluate the locate command
- it's a no-op, and by the way, leads to a
segmentation fault in the current implementation */
return false;
case CMD_DELETE:
handle_delete(c);
file_repaint();
break;
case CMD_SAVE_PLAYLIST:
handle_save(c);
break;
case CMD_SCREEN_UPDATE:
browser.filelist = mpdclient_filelist_update(c, browser.filelist);
#ifndef NCMPC_MINI
sync_highlights(c, browser.filelist);
#endif
list_window_check_selected(browser.lw,
filelist_length(browser.filelist));
file_repaint();
return false;
case CMD_DB_UPDATE:
if (c->status == NULL)
return true;
if (!c->status->updatingDb) {
if (mpdclient_cmd_db_update(c, browser.filelist->path) == 0) {
if (strcmp(browser.filelist->path, "")) {
char *path_locale =
utf8_to_locale(browser.filelist->path);
screen_status_printf(_("Database update of %s started"),
path_locale);
g_free(path_locale);
} else
screen_status_printf(_("Database update started"));
/* set updatingDb to make shure the browse callback gets called
* even if the updated has finished before status is updated */
c->status->updatingDb = 1;
}
} else
screen_status_printf(_("Database update running..."));
return true;
default:
break;
}
if (browser_cmd(&browser, c, cmd)) {
if (screen_is_visible(&screen_browse))
file_repaint();
return true;
}
return false;
}
const struct screen_functions screen_browse = {
.init = browse_init,
.exit = browse_exit,
.open = browse_open,
.resize = browse_resize,
.paint = browse_paint,
.cmd = browse_cmd,
.get_title = browse_title,
};
bool
screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
{
const char *slash, *parent;
char *allocated = NULL;
bool ret;
int i;
assert(song != NULL);
assert(song->file != NULL);
if (strstr(song->file, "//") != NULL)
/* an URL? */
return false;
/* determine the song's parent directory and go there */
slash = strrchr(song->file, '/');
if (slash != NULL)
parent = allocated = g_strndup(song->file, slash - song->file);
else
parent = "";
ret = browser_change_directory(&browser, c, NULL, parent);
g_free(allocated);
if (!ret)
return false;
/* select the specified song */
i = filelist_find_song(browser.filelist, song);
if (i < 0)
i = 0;
list_window_set_selected(browser.lw, i);
/* finally, switch to the file screen */
screen_switch(&screen_browse, c);
return true;
}
|