1/* This file is part of Gradio.
2 *
3 * Gradio is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * Gradio is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with Gradio.  If not, see <http://www.gnu.org/licenses/>.
15 */
16
17namespace Gradio{
18
19	[GtkTemplate (ui = "/de/haecker-felix/gradio/ui/selection-toolbar.ui")]
20	public class SelectionToolbar : Gtk.Box{
21
22		[GtkChild] private Gtk.Button AddButton;
23		[GtkChild] private Gtk.Button RemoveButton;
24		[GtkChild] private Gtk.Button DetailsButton;
25		[GtkChild] private Gtk.Button PlayButton;
26		[GtkChild] private Gtk.MenuButton CollectionButton;
27		[GtkChild] private Gtk.Button VoteButton;
28		[GtkChild] private Gtk.Button EditButton;
29		[GtkChild] private Gtk.Stack SelectionStack;
30		[GtkChild] private Gtk.Image InfoImage;
31
32		[GtkChild] private Gtk.MenuButton RenameButton;
33		[GtkChild] private Gtk.Entry RenameEntry;
34		[GtkChild] private Gtk.Popover RenamePopover;
35
36		private OrganizeCollectionPopover collection_dialog;
37
38		private MainWindow window;
39
40		public SelectionToolbar(MainWindow w){
41			window = w;
42			window.notify["current-selection"].connect(update_buttons);
43
44			collection_dialog = new OrganizeCollectionPopover();
45			CollectionButton.set_popover(collection_dialog);
46			CollectionButton.toggled.connect(collection_dialog.update_collections);
47		}
48
49		private void update_buttons(){
50			// Hide all buttons
51			AddButton.set_visible(false);
52			RemoveButton.set_visible(false);
53			DetailsButton.set_visible(false);
54			PlayButton.set_visible(false);
55			CollectionButton.set_visible(false);
56			VoteButton.set_visible(false);
57			EditButton.set_visible(false);
58			InfoImage.set_visible(false);
59			RenameButton.set_visible(false);
60
61			if(window.current_selection.get_n_items() == 0)
62				SelectionStack.set_visible_child_name("no-actions");
63			else
64				SelectionStack.set_visible_child_name("actions");
65
66			// Selection contains ONLY radio stations
67			if(window.current_selection.contains_radio_station_item() && !window.current_selection.contains_collection_item()){
68				if(window.current_selection.get_n_items() == 1){
69					DetailsButton.set_visible(true);
70					PlayButton.set_visible(true);
71					VoteButton.set_visible(true);
72					EditButton.set_visible(true);
73				}
74			}
75
76			// Selection contains ONLY collections
77			if(window.current_selection.contains_collection_item() && !window.current_selection.contains_radio_station_item()){
78				if(window.current_selection.get_n_items() == 1){
79					DetailsButton.set_visible(true);
80					RenameButton.set_visible(true);
81				}
82			}
83
84
85			int library_count = library_items();
86			int non_library_count = non_library_items();
87
88			// Selection contains more library items than non library items
89			if(library_count > non_library_count){
90				RemoveButton.set_visible(true);
91
92				// ... but no collection item!
93				if(!window.current_selection.contains_collection_item())
94					CollectionButton.set_visible(true);
95
96				if(non_library_count != 0){
97					InfoImage.set_visible(true);
98					InfoImage.set_tooltip_text((_("%i radio station(s) not present in library.").printf(non_library_count)));
99				}
100			}
101
102			// Selection contains more non library items than library items
103			if(library_count <= non_library_count){
104				AddButton.set_visible(true);
105
106				if(library_count != 0){
107					InfoImage.set_visible(true);
108					InfoImage.set_tooltip_text((_("%i radio station(s) already added to library.").printf(library_count)));
109				}
110			}
111		}
112
113		private int non_library_items(){
114			int count = 0;
115
116			foreach(Gd.MainBoxItem item in window.current_selection){
117				if(!App.library.contains_item(item)) count++;
118			}
119
120			return count;
121		}
122
123		private int library_items(){
124			int count = 0;
125
126			foreach(Gd.MainBoxItem item in window.current_selection){
127				if(App.library.contains_item(item)) count++;
128			}
129
130			return count;
131		}
132
133		[GtkCallback]
134		public void RemoveButton_clicked (Gtk.Button button) {
135			StationModel selection = window.current_selection;
136
137			foreach(Gd.MainBoxItem item in selection){
138				if(Util.is_collection_item(int.parse(item.id)))
139					App.library.remove_collection((Collection)item);
140				else
141					App.library.remove_radio_station((RadioStation)item);
142			}
143
144			App.window.set_selection_mode(false);
145		}
146
147		[GtkCallback]
148		public void AddButton_clicked (Gtk.Button button) {
149			StationModel selection = window.current_selection;
150
151			foreach(Gd.MainBoxItem item in selection){
152				if(!Util.is_collection_item(int.parse(item.id)))
153					App.library.add_radio_station((RadioStation)item);
154			}
155
156			App.window.set_selection_mode(false);
157		}
158
159		[GtkCallback]
160		public void DetailsButton_clicked (Gtk.Button button) {
161			Gd.MainBoxItem item = (Gd.MainBoxItem)window.current_selection.get_item(0);
162
163			if(Util.is_collection_item(int.parse(item.id))){
164				Collection collection = (Collection)item;
165				App.window.details_box.set_collection(collection);
166			}else{
167				RadioStation station = (RadioStation)item;
168				App.window.details_box.set_station(station);
169			}
170
171			App.window.details_box.set_visible(true);
172			App.window.set_selection_mode(false);
173		}
174
175		[GtkCallback]
176		public void PlayButton_clicked (Gtk.Button button) {
177			Gd.MainBoxItem item = (Gd.MainBoxItem)window.current_selection.get_item(0);
178
179			if(!Util.is_collection_item(int.parse(item.id))){
180				RadioStation station = (RadioStation)item;
181				App.player.station = station;
182			}
183
184			App.window.set_selection_mode(false);
185		}
186
187		[GtkCallback]
188		public void VoteButton_clicked (Gtk.Button button) {
189			Gd.MainBoxItem item = (Gd.MainBoxItem)window.current_selection.get_item(0);
190
191			// TODO: If you vote a station, instantly update likes value in the mainbox
192			if(!Util.is_collection_item(int.parse(item.id))){
193				RadioStation station = (RadioStation)item;
194				station.vote();
195			}
196
197			App.window.set_selection_mode(false);
198		}
199
200		[GtkCallback]
201		public void EditButton_clicked (Gtk.Button button) {
202			Gd.MainBoxItem item = (Gd.MainBoxItem)window.current_selection.get_item(0);
203
204			if(!Util.is_collection_item(int.parse(item.id))){
205				StationEditorDialog editor_dialog = new StationEditorDialog.edit((RadioStation)item);
206				editor_dialog.set_transient_for(App.window);
207				editor_dialog.set_modal(true);
208				editor_dialog.set_visible(true);
209			}
210
211			App.window.set_selection_mode(false);
212		}
213
214		[GtkCallback]
215		public void ShareButton_clicked (Gtk.Button button) {
216			// TODO: implement share button
217		}
218
219		[GtkCallback]
220		public void RenameSaveButton_clicked (Gtk.Button button) {
221			Gd.MainBoxItem item = (Gd.MainBoxItem)window.current_selection.get_item(0);
222
223			if(Util.is_collection_item(int.parse(item.id))){
224				Collection collection = (Collection)item;
225				App.library.rename_collection(collection, RenameEntry.get_text());
226			}
227
228			RenameEntry.set_text("");
229			RenamePopover.hide();
230
231			App.window.set_selection_mode(false);
232		}
233	}
234}
235