Commit | Line | Data |
---|---|---|
c133bd3c RK |
1 | /* |
2 | * This file is part of DisOrder | |
3 | * Copyright (C) 2006-2008 Richard Kettlewell | |
4 | * | |
e7eb3a27 | 5 | * This program is free software: you can redistribute it and/or modify |
c133bd3c | 6 | * it under the terms of the GNU General Public License as published by |
e7eb3a27 | 7 | * the Free Software Foundation, either version 3 of the License, or |
c133bd3c RK |
8 | * (at your option) any later version. |
9 | * | |
e7eb3a27 RK |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
c133bd3c | 15 | * You should have received a copy of the GNU General Public License |
e7eb3a27 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
c133bd3c | 17 | */ |
132a5a4a RK |
18 | /** @file disobedience/queue-menu.c |
19 | * @brief Disobedience queue widget popup menu | |
20 | */ | |
c133bd3c | 21 | #include "disobedience.h" |
6982880f | 22 | #include "popup.h" |
c133bd3c RK |
23 | #include "queue-generic.h" |
24 | ||
25 | /* Select All */ | |
26 | ||
6982880f RK |
27 | int ql_selectall_sensitive(void *extra) { |
28 | struct queuelike *ql = extra; | |
c133bd3c RK |
29 | return !!ql->q; |
30 | } | |
31 | ||
32 | void ql_selectall_activate(GtkMenuItem attribute((unused)) *menuitem, | |
33 | gpointer user_data) { | |
34 | struct queuelike *ql = user_data; | |
35 | ||
36 | gtk_tree_selection_select_all(ql->selection); | |
37 | } | |
38 | ||
39 | /* Select None */ | |
40 | ||
6982880f RK |
41 | int ql_selectnone_sensitive(void *extra) { |
42 | struct queuelike *ql = extra; | |
c133bd3c RK |
43 | return gtk_tree_selection_count_selected_rows(ql->selection) > 0; |
44 | } | |
45 | ||
46 | void ql_selectnone_activate(GtkMenuItem attribute((unused)) *menuitem, | |
47 | gpointer user_data) { | |
48 | struct queuelike *ql = user_data; | |
49 | ||
50 | gtk_tree_selection_unselect_all(ql->selection); | |
51 | } | |
52 | ||
53 | /* Properties */ | |
54 | ||
6982880f RK |
55 | int ql_properties_sensitive(void *extra) { |
56 | struct queuelike *ql = extra; | |
c133bd3c RK |
57 | return gtk_tree_selection_count_selected_rows(ql->selection) > 0; |
58 | } | |
59 | ||
60 | void ql_properties_activate(GtkMenuItem attribute((unused)) *menuitem, | |
61 | gpointer user_data) { | |
62 | struct queuelike *ql = user_data; | |
63 | struct vector v[1]; | |
64 | GtkTreeIter iter[1]; | |
65 | ||
66 | vector_init(v); | |
67 | gtk_tree_model_get_iter_first(GTK_TREE_MODEL(ql->store), iter); | |
68 | for(struct queue_entry *q = ql->q; q; q = q->next) { | |
69 | if(gtk_tree_selection_iter_is_selected(ql->selection, iter)) | |
70 | vector_append(v, (char *)q->track); | |
71 | gtk_tree_model_iter_next(GTK_TREE_MODEL(ql->store), iter); | |
72 | } | |
73 | if(v->nvec) | |
d8b71e03 | 74 | properties(v->nvec, (const char **)v->vec, ql->view); |
c133bd3c RK |
75 | } |
76 | ||
77 | /* Scratch */ | |
78 | ||
6982880f | 79 | int ql_scratch_sensitive(void attribute((unused)) *extra) { |
c9fb35f4 RK |
80 | return !!(last_state & DISORDER_PLAYING) |
81 | && right_scratchable(last_rights, config->username, playing_track); | |
82 | } | |
83 | ||
ac56170f | 84 | static void ql_scratch_completed(void attribute((unused)) *v, |
abf99697 RK |
85 | const char *err) { |
86 | if(err) | |
87 | popup_protocol_error(0, err); | |
c133bd3c RK |
88 | } |
89 | ||
90 | void ql_scratch_activate(GtkMenuItem attribute((unused)) *menuitem, | |
91 | gpointer attribute((unused)) user_data) { | |
ac56170f | 92 | disorder_eclient_scratch_playing(client, ql_scratch_completed, 0); |
c133bd3c RK |
93 | } |
94 | ||
95 | /* Remove */ | |
96 | ||
ac56170f RK |
97 | static void ql_remove_sensitive_callback(GtkTreeModel *model, |
98 | GtkTreePath attribute((unused)) *path, | |
99 | GtkTreeIter *iter, | |
100 | gpointer data) { | |
83fb99f9 | 101 | struct queue_entry *q = ql_iter_to_q(model, iter); |
22717074 RK |
102 | const int removable = (q != playing_track |
103 | && right_removable(last_rights, config->username, q)); | |
104 | int *const counts = data; | |
105 | ++counts[removable]; | |
106 | } | |
107 | ||
6982880f RK |
108 | int ql_remove_sensitive(void *extra) { |
109 | struct queuelike *ql = extra; | |
22717074 RK |
110 | int counts[2] = { 0, 0 }; |
111 | gtk_tree_selection_selected_foreach(ql->selection, | |
ac56170f | 112 | ql_remove_sensitive_callback, |
22717074 RK |
113 | counts); |
114 | /* Remove will work if we have at least some removable tracks selected, and | |
115 | * no unremovable ones */ | |
116 | return counts[1] > 0 && counts[0] == 0; | |
117 | } | |
118 | ||
ac56170f | 119 | static void ql_remove_completed(void attribute((unused)) *v, |
abf99697 RK |
120 | const char *err) { |
121 | if(err) | |
122 | popup_protocol_error(0, err); | |
22717074 RK |
123 | } |
124 | ||
ac56170f RK |
125 | static void ql_remove_activate_callback(GtkTreeModel *model, |
126 | GtkTreePath attribute((unused)) *path, | |
127 | GtkTreeIter *iter, | |
128 | gpointer attribute((unused)) data) { | |
83fb99f9 | 129 | struct queue_entry *q = ql_iter_to_q(model, iter); |
22717074 | 130 | |
c10dd9af RK |
131 | if(q != playing_track) |
132 | disorder_eclient_remove(client, q->id, ql_remove_completed, q); | |
c133bd3c RK |
133 | } |
134 | ||
135 | void ql_remove_activate(GtkMenuItem attribute((unused)) *menuitem, | |
22717074 RK |
136 | gpointer user_data) { |
137 | struct queuelike *ql = user_data; | |
138 | gtk_tree_selection_selected_foreach(ql->selection, | |
ac56170f | 139 | ql_remove_activate_callback, |
22717074 | 140 | 0); |
c133bd3c RK |
141 | } |
142 | ||
143 | /* Play */ | |
144 | ||
6982880f RK |
145 | int ql_play_sensitive(void *extra) { |
146 | struct queuelike *ql = extra; | |
ac56170f RK |
147 | return (last_rights & RIGHT_PLAY) |
148 | && gtk_tree_selection_count_selected_rows(ql->selection) > 0; | |
c133bd3c RK |
149 | } |
150 | ||
abf99697 RK |
151 | static void ql_play_completed(void attribute((unused)) *v, const char *err) { |
152 | if(err) | |
153 | popup_protocol_error(0, err); | |
c133bd3c RK |
154 | } |
155 | ||
ac56170f RK |
156 | static void ql_play_activate_callback(GtkTreeModel *model, |
157 | GtkTreePath attribute((unused)) *path, | |
158 | GtkTreeIter *iter, | |
159 | gpointer attribute((unused)) data) { | |
83fb99f9 | 160 | struct queue_entry *q = ql_iter_to_q(model, iter); |
ac56170f RK |
161 | |
162 | disorder_eclient_play(client, q->track, ql_play_completed, q); | |
163 | } | |
164 | ||
165 | void ql_play_activate(GtkMenuItem attribute((unused)) *menuitem, | |
166 | gpointer user_data) { | |
167 | struct queuelike *ql = user_data; | |
168 | gtk_tree_selection_selected_foreach(ql->selection, | |
169 | ql_play_activate_callback, | |
170 | 0); | |
171 | } | |
c133bd3c | 172 | |
c133bd3c | 173 | /** @brief Called when a button is released over a queuelike */ |
6982880f | 174 | gboolean ql_button_release(GtkWidget *widget, |
c133bd3c RK |
175 | GdkEventButton *event, |
176 | gpointer user_data) { | |
177 | struct queuelike *ql = user_data; | |
178 | ||
179 | if(event->type == GDK_BUTTON_PRESS | |
180 | && event->button == 3) { | |
be909398 | 181 | /* Right button click. */ |
6982880f RK |
182 | ensure_selected(GTK_TREE_VIEW(widget), event); |
183 | popup(&ql->menu, event, ql->menuitems, ql->nmenuitems, ql); | |
c133bd3c RK |
184 | return TRUE; /* hide the click from other widgets */ |
185 | } | |
186 | ||
187 | return FALSE; | |
188 | } | |
189 | ||
a49c2c0f RK |
190 | /* Adopt */ |
191 | ||
192 | static void ql_adopt_sensitive_callback(GtkTreeModel *model, | |
193 | GtkTreePath attribute((unused)) *path, | |
194 | GtkTreeIter *iter, | |
195 | gpointer data) { | |
196 | struct queue_entry *const q = ql_iter_to_q(model, iter); | |
197 | int *const count = data; | |
198 | ||
199 | if(*count < 0) | |
200 | return; | |
201 | if(q->origin == origin_random) | |
202 | ++*count; | |
203 | else | |
204 | *count = -1; | |
205 | } | |
206 | ||
207 | /** @brief Determine whether we're pointing at an adoptable track */ | |
208 | int ql_adopt_sensitive(void *extra) { | |
209 | struct queuelike *ql = extra; | |
210 | int count = 0; | |
211 | ||
212 | /* We'll need RIGHT_PLAY */ | |
213 | if(!(last_rights & RIGHT_PLAY)) | |
214 | return FALSE; | |
215 | /* Check that (1) only random tracks are selected (2) at least something is | |
216 | * selected */ | |
217 | gtk_tree_selection_selected_foreach(ql->selection, | |
218 | ql_adopt_sensitive_callback, | |
219 | &count); | |
220 | if(count <= 0) | |
221 | return FALSE; | |
222 | return TRUE; | |
223 | } | |
224 | ||
225 | static void ql_adopt_completed(void attribute((unused)) *v, const char *err) { | |
226 | if(err) | |
227 | popup_protocol_error(0, err); | |
228 | } | |
229 | ||
230 | static void ql_adopt_activate_callback(GtkTreeModel *model, | |
231 | GtkTreePath attribute((unused)) *path, | |
232 | GtkTreeIter *iter, | |
233 | gpointer attribute((unused)) data) { | |
234 | struct queue_entry *const q = ql_iter_to_q(model, iter); | |
235 | ||
236 | disorder_eclient_adopt(client, ql_adopt_completed, q->id, q); | |
237 | } | |
238 | ||
239 | /** @brief Called to adopt a track */ | |
240 | void ql_adopt_activate(GtkMenuItem attribute((unused)) *menuitem, | |
241 | gpointer user_data) { | |
242 | struct queuelike *ql = user_data; | |
243 | gtk_tree_selection_selected_foreach(ql->selection, | |
244 | ql_adopt_activate_callback, | |
245 | NULL); | |
246 | } | |
247 | ||
ee7552f8 | 248 | struct tabtype *ql_tabtype(struct queuelike *ql) { |
abf99697 | 249 | static const struct tabtype queuelike_tabtype = { |
6982880f RK |
250 | ql_properties_sensitive, |
251 | ql_selectall_sensitive, | |
252 | ql_selectnone_sensitive, | |
253 | ql_properties_activate, | |
254 | ql_selectall_activate, | |
255 | ql_selectnone_activate, | |
ee7552f8 RK |
256 | 0, |
257 | 0 | |
258 | }; | |
259 | ||
abf99697 | 260 | ql->tabtype = queuelike_tabtype; |
54156c62 RK |
261 | ql->tabtype.extra = ql; |
262 | return &ql->tabtype; | |
ee7552f8 RK |
263 | } |
264 | ||
c133bd3c RK |
265 | /* |
266 | Local Variables: | |
267 | c-basic-offset:2 | |
268 | comment-column:40 | |
269 | fill-column:79 | |
270 | indent-tabs-mode:nil | |
271 | End: | |
272 | */ |