5 const string log_domain
= "Gmpc.DataView";
7 /** The Default column width. */
8 const int default_column_width
= 200;
12 * * List of column ids to show.
13 * * Name of each column
14 * * Default set of enabled columns.
16 const int NUM_COLS
= 20;
17 const int[] gmpc_data_view_col_ids
= {
18 Gmpc
.MpdData
.ColumnTypes
.MARKUP
,
19 Gmpc
.MpdData
.ColumnTypes
.SONG_ARTIST
, /* album name */
20 Gmpc
.MpdData
.ColumnTypes
.SONG_ALBUM
, /* album name */
21 Gmpc
.MpdData
.ColumnTypes
.SONG_TITLE
, /* song title */
22 Gmpc
.MpdData
.ColumnTypes
.SONG_TITLEFILE
, /* song title */
23 Gmpc
.MpdData
.ColumnTypes
.SONG_GENRE
, /* song genre */
24 Gmpc
.MpdData
.ColumnTypes
.SONG_TRACK
, /* song track */
25 Gmpc
.MpdData
.ColumnTypes
.SONG_NAME
, /* stream name */
26 Gmpc
.MpdData
.ColumnTypes
.SONG_COMPOSER
, /* composer name */
27 Gmpc
.MpdData
.ColumnTypes
.SONG_PERFORMER
, /* performer */
28 Gmpc
.MpdData
.ColumnTypes
.SONG_DATE
, /* date */
29 Gmpc
.MpdData
.ColumnTypes
.SONG_LENGTH_FORMAT
, /* length formatted */
30 Gmpc
.MpdData
.ColumnTypes
.SONG_DISC
, /* disc */
31 Gmpc
.MpdData
.ColumnTypes
.SONG_COMMENT
, /* comment */
32 Gmpc
.MpdData
.ColumnTypes
.ICON_ID
, /* icon id */
33 Gmpc
.MpdData
.ColumnTypes
.SONG_POS
,
34 Gmpc
.MpdData
.ColumnTypes
.SONG_ALBUMARTIST
,
35 Gmpc
.MpdData
.ColumnTypes
.PATH_EXTENSION
, /* Extension */
36 Gmpc
.MpdData
.ColumnTypes
.PATH_DIRECTORY
, /* Directory */
37 Gmpc
.MpdData
.ColumnTypes
.SONG_PRIORITY
39 const string[] gmpc_data_view_col_names
= {
62 const bool[] gmpc_data_view_col_enabled
= {
86 const int[] gmpc_data_view_col_position
= {
109 public class Gmpc
.DataView
: Gtk
.TreeView
111 private Gtk
.TreeViewColumn
[] tree_columns
= new Gtk
.TreeViewColumn
[NUM_COLS
];
113 * If we are a play-queue we should treat the content.
114 * slightly different.
115 * e.g. add-replace will be play-crop
117 public bool is_play_queue
{get; set; default=false;}
120 * The name of the treeview.
121 * This is used to store the column layout.
123 public string uid
{get; set; default="default";}
127 * Construction function.
129 public DataView(string name
)
131 log(log_domain
, LogLevelFlags
.LEVEL_INFO
, "Constructing dataview: "+name
);
134 // Connect row activated signal.
135 this
.row_activated
.connect(__row_activated
);
136 this
.key_release_event
.connect(__key_release_event_callback
);
137 // When it getst he destroy signal.
138 this
.destroy
.connect(column_store_state
);
140 this
.set_rules_hint(true);
142 this
.get_selection().set_mode(Gtk
.SelectionMode
.MULTIPLE
);
143 this
.set_fixed_height_mode(true);
157 * Internal functions.
161 * Store the position, visibility and width of the columns
163 private void column_store_state()
165 // Save the position of the columns
166 var columns
= get_columns();
168 foreach(var column
in columns
)
170 int col_index
= column
.get_data("index");
171 int width
= column
.get_width();
172 config
.set_int(uid
+"-colpos", gmpc_data_view_col_names
[col_index
], index
);
173 config
.set_bool(uid
+"-colshow", gmpc_data_view_col_names
[col_index
], column
.visible
);
174 // Only store width if bigger then 0.
176 config
.set_int(uid
+"-colsize",gmpc_data_view_col_names
[col_index
], width
);
181 // Hack to make vala not destroy the menu directly.
182 private Gtk
.Menu column_selection_menu
= null;
183 private void column_show_selection_menu()
185 column_selection_menu
= new Gtk
.Menu();
186 foreach(var col
in tree_columns
)
188 int index
= col
.get_data("index");
189 // Do not show the icon id in the selection list.
190 if(gmpc_data_view_col_ids
[index
] == MpdData
.ColumnTypes
.ICON_ID
) continue;
191 var item
= new Gtk
.CheckMenuItem
.with_label(gmpc_data_view_col_names
[index
]);
193 item
.set_active(true);
195 // On activation toggle the state.
196 item
.activate
.connect((source
) => {
197 col
.visible
= (source as Gtk
.CheckMenuItem
).get_active();
199 column_selection_menu
.append(item
);
201 column_selection_menu
.show_all();
202 column_selection_menu
.popup(null, null, null, 0, Gtk
.get_current_event_time());
205 * Populate the treeview with the right columns.
206 * The treeview should have a name now.
208 private void column_populate()
210 for(int i
= 0; i
< NUM_COLS
; i
++)
212 Gtk
.TreeViewColumn col
= new Gtk
.TreeViewColumn();
213 col
.set_data("index", i
);
214 if(gmpc_data_view_col_ids
[i
] == Gmpc
.MpdData
.ColumnTypes
.ICON_ID
)
219 var renderer
= new Gtk
.CellRendererPixbuf();
220 renderer
.xalign
= 0.0f
;
222 col
.pack_start(renderer
, true);
223 col
.set_attributes(renderer
, "icon-name", Gmpc
.MpdData
.ColumnTypes
.ICON_ID
);
224 col
.set_resizable(false);
225 col
.set_fixed_width(20);
226 col
.clickable
= true;
227 // If the user clicks on the column, show dropdown allowing to enable/disable columns.
228 col
.clicked
.connect((source
) => {
229 column_show_selection_menu();
235 col
.set_title(gmpc_data_view_col_names
[i
]);
236 var renderer
= new Gtk
.CellRendererText();
237 renderer
.ellipsize
= Pango
.EllipsizeMode
.END
;
238 renderer
.weight_set
= true;
240 col
.pack_start(renderer
, true);
241 col
.set_attributes(renderer
, "text", gmpc_data_view_col_ids
[i
]);
242 col
.set_resizable(true);
245 // col.set_cell_data_func(renderer, (Gtk.CellLayoutDataFunc)highlight_row_current_song_playing);
248 int width
= config
.get_int_with_default(uid
+"-colsize",gmpc_data_view_col_names
[i
], default_column_width
);
249 // Do not set to size 0, then revert back to 200.
250 col
.set_fixed_width(width
>0?width
:default_column_width
);
252 col
.set_sizing(Gtk
.TreeViewColumnSizing
.FIXED
);
253 col
.set_reorderable(true);
256 int pos
= config
.get_int_with_default(uid
+"-colpos", gmpc_data_view_col_names
[i
], gmpc_data_view_col_position
[i
]);
257 this
.tree_columns
[pos
] = col
;
259 // Add the columns (in right order)
260 for(int i
= 0; i
< NUM_COLS
; i
++) {
261 int index
= this
.tree_columns
[i
].get_data("index");
262 this
.insert_column(this
.tree_columns
[i
], i
);
263 this
.tree_columns
[i
].set_visible(config
.get_bool_with_default(uid
+"-colshow", gmpc_data_view_col_names
[index
], gmpc_data_view_col_enabled
[index
]));
272 * Function handles the row-activate signal.
274 private void __row_activated (Gtk
.TreePath path
, Gtk
.TreeViewColumn col
)
276 Gtk
.TreeModel? model
= this
.get_model();
280 if(!model
.get_iter(out iter
, path
)) return;
282 /* If we are play-queue, play the selected song. */
284 model
.get(iter
, Gmpc
.MpdData
.ColumnTypes
.SONG_ID
, out song_id
);
285 MPD
.Player
.play_id(Gmpc
.server
, song_id
);
287 /* If we are a song browser, add the path and play it. */
289 model
.get(iter
, Gmpc
.MpdData
.ColumnTypes
.PATH
, out song_path
);
290 MpdInteraction
.play_path(song_path
);
295 * Check if current row is playing.
298 private void highlight_row_current_song_playing(Gtk
.TreeViewColumn col
, Gtk
.CellRenderer renderer
, Gtk
.TreeModel model
, Gtk
.TreeIter iter
)
300 (renderer as Gtk
.CellRendererText
).weight
= Pango
.Weight
.BOLD
;
305 * Handle keyboard input.
307 private bool __key_release_event_callback(Gdk
.EventKey event
)
309 if(event
.keyval
== Gdk
.Key_y
)
311 // Copy data to clipboard
314 else if (event
.keyval
== Gdk
.Key_c
)
316 // Cut (if available) into clipboard
318 else if (event
.keyval
== Gdk
.Key_P
)
322 else if (event
.keyval
== Gdk
.Key_p
)
326 else if (event
.keyval
== Gdk
.Key_Escape
)
331 // Commands specific to play_queue
334 if (event
.keyval
== Gdk
.Key_Q
)
337 return selected_songs_remove_priority();
339 else if (event
.keyval
== Gdk
.Key_q
)
342 return selected_songs_raise_priority();
344 else if (event
.keyval
== Gdk
.Key_d
)
346 if(!selected_songs_remove())
348 // Detach model (for some reason keeping it attached
349 // Makes thing break, work-around for now)
351 var model
= get_model();
354 MPD
.PlayQueue
.clear(server
);
363 if(event
.keyval
== Gdk
.Key_i
)
372 * Interaction on selected songs.
374 // Set priority on the selected songs.
375 private bool selected_songs_raise_priority()
377 if(server
.check_command_allowed("prioid") != MPD
.Server
.Command
.ALLOWED
) return false;
378 var selection
= this
.get_selection();
380 if(selection
.count_selected_rows() > 254) {
381 Gmpc
.Messages
.show(_("You can only queue 254 songs at the time."), Gmpc
.Messages
.Level
.WARNING
);
387 foreach(var path
in selection
.get_selected_rows(out model
))
390 if(model
.get_iter(out iter
, path
))
393 model
.get(iter
,Gmpc
.MpdData
.ColumnTypes
.SONG_ID
, out song_id
);
394 MPD
.PlayQueue
.set_priority(server
, song_id
, priority
--);
399 // Remove the set priority from the selected songs.
400 private bool selected_songs_remove_priority()
402 if(server
.check_command_allowed("prioid") != MPD
.Server
.Command
.ALLOWED
) return false;
403 var selection
= this
.get_selection();
406 foreach(var path
in selection
.get_selected_rows(out model
))
409 if(model
.get_iter(out iter
, path
))
412 model
.get(iter
,Gmpc
.MpdData
.ColumnTypes
.SONG_ID
, out song_id
);
413 MPD
.PlayQueue
.set_priority(server
, song_id
, 0);
418 // Remove the selected songs from the play queue.
419 private bool selected_songs_remove()
421 int deleted_rows
= 0;
422 var selection
= this
.get_selection();
425 foreach(var path
in selection
.get_selected_rows(out model
))
428 if(model
.get_iter(out iter
, path
))
431 model
.get(iter
,Gmpc
.MpdData
.ColumnTypes
.SONG_ID
, out song_id
);
432 MPD
.PlayQueue
.queue_delete_id(server
, song_id
);
436 MPD
.PlayQueue
.queue_commit(server
);
437 return (deleted_rows
> 0);