27d9205784b80cd431e4afdc263c145119f707f7
[rugen] / src / page_clips.c
1 /*
2 * page_clips.c -- Clips Page Handling
3 * Copyright (C) 2002-2003 Charles Yates <charles.yates@pandora.be>
4 * Copyright (C) 2010 Dan Dennedy <dan@dennedy.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30
31 #include "interface.h"
32 #include "support.h"
33 #include "dv1394app.h"
34 #include "page.h"
35
36 typedef struct
37 {
38 struct page_t parent;
39 dv1394app app;
40 GtkWidget *widget;
41 mvcp dv;
42 char *path;
43 int unit;
44 int generation;
45 int clip;
46
47 // TODO: This comes out later
48 int mode;
49 GtkWidget *modes[ 4 ];
50 }
51 *page_clips, page_clips_t;
52
53 static GtkWidget *this_page_get_widget( page_clips this );
54
55 static void list_clips( page_clips this, char *path )
56 {
57 GtkWidget *treeview = lookup_widget( this_page_get_widget( this ), "list_clips" );
58 GtkWidget *dirview = lookup_widget( this_page_get_widget( this ), "list_dir" );
59 GtkWidget *label = lookup_widget( this_page_get_widget( this ), "label_directory" );
60 if ( path != NULL )
61 {
62 mvcp_dir dir = mvcp_dir_init( this->dv, path );
63 GtkListStore *dir_store = NULL;
64 GtkListStore *clip_store = NULL;
65 GtkTreeIter iter;
66 int index;
67 mvcp_dir_entry_t entry;
68
69 free( this->path );
70 this->path = strdup( path );
71
72 gtk_label_set_text( GTK_LABEL( label ), this->path );
73
74 if ( gtk_tree_view_get_model( GTK_TREE_VIEW( treeview ) ) == NULL )
75 {
76 GtkCellRenderer *renderer;
77 GtkTreeViewColumn *column;
78
79 clip_store = gtk_list_store_new( 2, G_TYPE_STRING, G_TYPE_STRING );
80 gtk_tree_view_set_model( GTK_TREE_VIEW( treeview ), GTK_TREE_MODEL( clip_store ) );
81
82 renderer = gtk_cell_renderer_text_new( );
83 column = gtk_tree_view_column_new_with_attributes ( "Description", renderer, "text", 0, NULL);
84 gtk_tree_view_column_set_sort_column_id( column, 0 );
85 gtk_tree_view_append_column( GTK_TREE_VIEW( treeview ), column );
86
87 dir_store = gtk_list_store_new( 2, G_TYPE_STRING, G_TYPE_STRING );
88 gtk_tree_view_set_model( GTK_TREE_VIEW( dirview ), GTK_TREE_MODEL( dir_store ) );
89
90 renderer = gtk_cell_renderer_text_new( );
91 column = gtk_tree_view_column_new_with_attributes ( "Description", renderer, "text", 0, NULL);
92 gtk_tree_view_column_set_sort_column_id( column, 0 );
93 gtk_tree_view_append_column( GTK_TREE_VIEW( dirview ), column );
94 }
95 else
96 {
97 clip_store = GTK_LIST_STORE( gtk_tree_view_get_model( GTK_TREE_VIEW( treeview ) ) );
98 dir_store = GTK_LIST_STORE( gtk_tree_view_get_model( GTK_TREE_VIEW( dirview ) ) );
99 gtk_list_store_clear( clip_store );
100 gtk_list_store_clear( dir_store );
101 }
102
103 if ( strcmp( path, "/" ) )
104 {
105 gtk_list_store_append( clip_store, &iter );
106 gtk_list_store_set( clip_store, &iter, 0, "..", -1 );
107 gtk_list_store_append( dir_store, &iter );
108 gtk_list_store_set( dir_store, &iter, 0, "..", -1 );
109 }
110
111 for ( index = 0; index < mvcp_dir_count( dir ); index ++ )
112 {
113 mvcp_dir_get( dir, index, &entry );
114 if ( strchr( entry.name, '/' ) )
115 {
116 gtk_list_store_append( dir_store, &iter );
117 gtk_list_store_set( dir_store, &iter, 0, entry.name, -1 );
118 }
119 else
120 {
121 gtk_list_store_append( clip_store, &iter );
122 gtk_list_store_set( clip_store, &iter, 0, entry.name, -1 );
123 }
124 }
125
126 mvcp_dir_close( dir );
127 }
128 else
129 {
130 gtk_label_set_text( GTK_LABEL( label ), "Disconnected" );
131 if ( gtk_tree_view_get_model( GTK_TREE_VIEW( treeview ) ) )
132 {
133 GtkListStore *list_store = GTK_LIST_STORE( gtk_tree_view_get_model( GTK_TREE_VIEW( treeview ) ) );
134 gtk_list_store_clear( list_store );
135 list_store = GTK_LIST_STORE( gtk_tree_view_get_model( GTK_TREE_VIEW( dirview ) ) );
136 gtk_list_store_clear( list_store );
137 treeview = lookup_widget( this_page_get_widget( this ), "treeview1" );
138 gtk_list_store_clear( list_store );
139 }
140 }
141 }
142
143 static void list_queue( page_clips this, int clip )
144 {
145 GtkWidget *treeview = lookup_widget( this_page_get_widget( this ), "treeview1" );
146 mvcp_list list = mvcp_list_init( this->dv, dv1394app_get_selected_unit( this->app ) );
147 GtkListStore *list_store = NULL;
148 GtkTreeIter iter;
149 GtkTreePath *path;
150 int index;
151 mvcp_list_entry_t entry;
152
153 if ( gtk_tree_view_get_model( GTK_TREE_VIEW( treeview ) ) == NULL )
154 {
155 GtkCellRenderer *renderer;
156 GtkTreeViewColumn *column;
157
158 list_store = gtk_list_store_new( 6, G_TYPE_BOOLEAN, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INT );
159 gtk_tree_view_set_model( GTK_TREE_VIEW( treeview ), GTK_TREE_MODEL( list_store ) );
160
161 renderer = gtk_cell_renderer_toggle_new( );
162 column = gtk_tree_view_column_new_with_attributes ( "", renderer, "active", 0, NULL);
163 gtk_tree_view_append_column( GTK_TREE_VIEW( treeview ), column );
164
165 renderer = gtk_cell_renderer_text_new( );
166 column = gtk_tree_view_column_new_with_attributes ( "In", renderer, "text", 1, NULL);
167 gtk_tree_view_append_column( GTK_TREE_VIEW( treeview ), column );
168
169 renderer = gtk_cell_renderer_text_new( );
170 column = gtk_tree_view_column_new_with_attributes ( "Out", renderer, "text", 2, NULL);
171 gtk_tree_view_append_column( GTK_TREE_VIEW( treeview ), column );
172
173 renderer = gtk_cell_renderer_text_new( );
174 column = gtk_tree_view_column_new_with_attributes ( "Length", renderer, "text", 3, NULL);
175 gtk_tree_view_append_column( GTK_TREE_VIEW( treeview ), column );
176
177 renderer = gtk_cell_renderer_text_new( );
178 column = gtk_tree_view_column_new_with_attributes ( "Clip", renderer, "text", 4, NULL);
179 gtk_tree_view_append_column( GTK_TREE_VIEW( treeview ), column );
180 }
181 else
182 {
183 list_store = GTK_LIST_STORE( gtk_tree_view_get_model( GTK_TREE_VIEW( treeview ) ) );
184 gtk_list_store_clear( list_store );
185 }
186
187 this->generation = list->generation;
188
189 for ( index = 0; index < mvcp_list_count( list ); index ++ )
190 {
191 mvcp_list_get( list, index, &entry );
192 gtk_list_store_append( list_store, &iter );
193 gtk_list_store_set( list_store, &iter, 0, index == clip, 1, ( int )entry.in, 2, ( int )entry.out, 3, ( int )entry.size, 4, entry.full, 5, entry.clip, -1 );
194 }
195
196 this->clip = clip;
197 if ( clip < mvcp_list_count( list ) )
198 {
199 path = gtk_tree_path_new_from_indices( this->clip, -1 );
200 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW( treeview ), path, NULL, TRUE, 0.5, 0 );
201 gtk_tree_path_free( path );
202 }
203
204 mvcp_list_close( list );
205 }
206
207 static void list_active( page_clips this, int clip )
208 {
209 GtkWidget *treeview = lookup_widget( this_page_get_widget( this ), "treeview1" );
210 GtkListStore *list_store = GTK_LIST_STORE( gtk_tree_view_get_model( GTK_TREE_VIEW( treeview ) ) );
211 GtkTreePath *path = gtk_tree_path_new_from_indices( this->clip, -1 );
212 GtkTreeIter iter;
213
214 gtk_tree_model_get_iter( GTK_TREE_MODEL (list_store), &iter, path );
215 gtk_tree_path_free( path );
216 gtk_list_store_set( list_store, &iter, 0, FALSE, -1 );
217
218 this->clip = clip;
219 path = gtk_tree_path_new_from_indices( this->clip, -1 );
220 gtk_tree_view_scroll_to_cell( GTK_TREE_VIEW( treeview ), path, NULL, TRUE, 0.5, 0 );
221 gtk_tree_model_get_iter( GTK_TREE_MODEL (list_store), &iter, path );
222 gtk_tree_path_free( path );
223 gtk_list_store_set( list_store, &iter, 0, TRUE, -1 );
224 }
225
226 static gboolean on_ok( GtkWidget *dummy, gpointer data )
227 {
228 page_clips this = data;
229 GtkWidget *widget = lookup_widget( this_page_get_widget( this ), "list_clips" );
230 GtkTreeSelection *select = gtk_tree_view_get_selection( GTK_TREE_VIEW( widget ) );
231 GtkTreeModel *model;
232 GtkTreeIter iter;
233 gchar *text;
234
235 if ( gtk_tree_selection_get_selected( select, &model, &iter ) )
236 {
237 gtk_tree_model_get( model, &iter, 0, &text, -1 );
238
239 if ( !strcmp( text, ".." ) )
240 {
241 char *temp = strdup( this->path );
242 temp[ strlen( temp ) - 1 ] = '\0';
243 *( strrchr( temp, '/' ) + 1 ) = '\0';
244 list_clips( this, temp );
245 free( temp );
246 }
247 else
248 {
249 char *temp = malloc( strlen( this->path ) + strlen( text ) + 1 );
250 strcpy( temp, this->path );
251 strcat( temp, text );
252 switch( this->mode )
253 {
254 case 0:
255 mvcp_unit_load_back( this->dv, dv1394app_get_selected_unit( this->app ), temp );
256 mvcp_unit_play( this->dv, dv1394app_get_selected_unit( this->app ) );
257 break;
258 case 1:
259 mvcp_unit_load( this->dv, dv1394app_get_selected_unit( this->app ), temp );
260 break;
261 case 2:
262 mvcp_unit_append( this->dv, dv1394app_get_selected_unit( this->app ), temp, -1, -1 );
263 break;
264 case 3:
265 mvcp_unit_clip_insert( this->dv, dv1394app_get_selected_unit( this->app ), mvcp_relative, 1, temp, -1, -1 );
266 break;
267 }
268
269 free( temp );
270 }
271
272 g_free( text );
273 }
274
275 return TRUE;
276 }
277
278 static gboolean on_dir( GtkWidget *dummy, gpointer data )
279 {
280 page_clips this = data;
281 GtkWidget *widget = lookup_widget( this_page_get_widget( this ), "list_dir" );
282 GtkTreeSelection *select = gtk_tree_view_get_selection( GTK_TREE_VIEW( widget ) );
283 GtkTreeModel *model;
284 GtkTreeIter iter;
285 gchar *text;
286
287 if ( gtk_tree_selection_get_selected( select, &model, &iter ) )
288 {
289 gtk_tree_model_get( model, &iter, 0, &text, -1 );
290
291 if ( !strcmp( text, ".." ) )
292 {
293 char *temp = strdup( this->path );
294 temp[ strlen( temp ) - 1 ] = '\0';
295 *( strrchr( temp, '/' ) + 1 ) = '\0';
296 list_clips( this, temp );
297 free( temp );
298 }
299 else if ( text[ strlen( text ) - 1 ] == '/' )
300 {
301 char *temp = malloc( strlen( this->path ) + strlen( text ) + 1 );
302 strcpy( temp, this->path );
303 strcat( temp, text );
304 list_clips( this, temp );
305 free( temp );
306 }
307
308 g_free( text );
309 }
310
311 return TRUE;
312 }
313
314 static gboolean on_queue_item( GtkWidget *dummy, gpointer data )
315 {
316 page_clips this = data;
317 GtkWidget *widget = lookup_widget( this_page_get_widget( this ), "treeview1" );
318 GtkTreeSelection *select = gtk_tree_view_get_selection( GTK_TREE_VIEW( widget ) );
319 GtkTreeModel *model;
320 GtkTreeIter iter;
321 int clip;
322
323 if ( gtk_tree_selection_get_selected( select, &model, &iter ) )
324 {
325 gtk_tree_model_get( model, &iter, 5, &clip, -1 );
326 mvcp_unit_clip_goto( this->dv, dv1394app_get_selected_unit( this->app ), mvcp_absolute, clip, 0 );
327 mvcp_unit_play( this->dv, dv1394app_get_selected_unit( this->app ) );
328 }
329
330 return TRUE;
331 }
332
333 static gboolean on_clip_selected( GtkWidget *widget, GdkEventButton *event, gpointer data )
334 {
335 if ( event->button==1 && event->type==GDK_2BUTTON_PRESS )
336 return on_ok( widget, data );
337 return FALSE;
338 }
339
340 static gboolean on_clip_key_press( GtkWidget *widget, GdkEventKey *event, gpointer data )
341 {
342 if ( event->keyval == GDK_Return )
343 return on_ok( widget, data );
344 return FALSE;
345 }
346
347 static gboolean on_dir_selected( GtkWidget *widget, GdkEventButton *event, gpointer data )
348 {
349 if ( event->button==1 && event->type==GDK_2BUTTON_PRESS )
350 return on_dir( widget, data );
351 return FALSE;
352 }
353
354 static gboolean on_dir_key_press( GtkWidget *widget, GdkEventKey *event, gpointer data )
355 {
356 if ( event->keyval == GDK_Return )
357 return on_dir( widget, data );
358 return FALSE;
359 }
360
361 static gboolean on_queue_selected( GtkWidget *widget, GdkEventButton *event, gpointer data )
362 {
363 if ( event->button==1 && event->type==GDK_2BUTTON_PRESS )
364 return on_queue_item( widget, data );
365 return FALSE;
366 }
367
368 static gboolean on_queue_key_press( GtkWidget *widget, GdkEventKey *event, gpointer data )
369 {
370 if ( event->keyval == GDK_Return )
371 return on_queue_item( widget, data );
372 return FALSE;
373 }
374
375 static gboolean on_home( GtkWidget *button, gpointer data )
376 {
377 page_clips this = data;
378 list_clips( this, "/" );
379 return TRUE;
380 }
381
382 static gboolean on_refresh( GtkWidget *button, gpointer data )
383 {
384 page_clips this = data;
385 char *temp = strdup( this->path );
386 list_clips( this, temp );
387 free( temp );
388
389 return TRUE;
390 }
391
392 static gboolean on_up( GtkWidget *dummy, gpointer data )
393 {
394 page_clips this = data;
395 mvcp_unit_clip_move( this->dv, dv1394app_get_selected_unit( this->app ), mvcp_relative, 0, mvcp_relative, -1 );
396 return TRUE;
397 }
398
399 static gboolean on_down( GtkWidget *dummy, gpointer data )
400 {
401 page_clips this = data;
402 mvcp_unit_clip_move( this->dv, dv1394app_get_selected_unit( this->app ), mvcp_relative, 0, mvcp_relative, 1 );
403 return TRUE;
404 }
405
406 static gboolean on_remove( GtkWidget *dummy, gpointer data )
407 {
408 page_clips this = data;
409 mvcp_unit_remove_current_clip( this->dv, dv1394app_get_selected_unit( this->app ) );
410 return TRUE;
411 }
412
413 static gboolean on_clean( GtkWidget *dummy, gpointer data )
414 {
415 page_clips this = data;
416 mvcp_unit_clean( this->dv, dv1394app_get_selected_unit( this->app ) );
417 return TRUE;
418 }
419
420 void on_mode_change( GtkMenuItem *menuitem, gpointer data )
421 {
422 page_clips this = data;
423 int index = 0;
424
425 for ( index = 0; index < 4; index ++ )
426 if ( GTK_WIDGET( menuitem ) == this->modes[ index ] )
427 break;
428
429 this->mode = index;
430 }
431
432 static GtkWidget *this_page_get_widget( page_clips this )
433 {
434 if ( this->widget == NULL )
435 this->widget = create_page_clips( );
436 return this->widget;
437 }
438
439 static void this_page_get_toolbar_info( page this, GtkIconSize size, GtkWidget **icon, char **label )
440 {
441 *icon = gtk_image_new_from_stock( "gtk-find", size );
442 *label = "_Clips";
443 }
444
445 static void this_page_on_connect( page_clips this )
446 {
447 this->dv = mvcp_init( dv1394app_get_parser( this->app ) );
448 list_clips( this, "/" );
449 }
450
451 static void this_page_on_unit_change( page_clips this, int unit )
452 {
453 if ( unit != this->unit )
454 this->unit = unit;
455 }
456
457 static void this_page_on_disconnect( page_clips this )
458 {
459 list_clips( this, NULL );
460 mvcp_close( this->dv );
461 }
462
463 static void this_page_show_status( page_clips this, mvcp_status status )
464 {
465 if ( status->status != unit_disconnected )
466 {
467 if ( this->generation != status->generation )
468 list_queue( this, status->clip_index );
469 else if ( this->clip != status->clip_index )
470 list_active( this, status->clip_index );
471 }
472 }
473
474 static void this_page_close( page_clips this )
475 {
476 if ( this != NULL )
477 free( this );
478 }
479
480 page page_clips_init( dv1394app app )
481 {
482 page_clips this = calloc( 1, sizeof( page_clips_t ) );
483 GtkWidget *widget;
484 int index = 0;
485
486 this->parent.get_widget = ( GtkWidget *(*)( page ) )this_page_get_widget;
487 this->parent.get_toolbar_info = this_page_get_toolbar_info;
488 this->parent.on_connect = ( void (*)( page ) )this_page_on_connect;
489 this->parent.on_unit_change = ( void (*)( page, int ) )this_page_on_unit_change;
490 this->parent.on_disconnect = ( void (*)( page ) )this_page_on_disconnect;
491 this->parent.show_status = ( void (*)( page, mvcp_status ) )this_page_show_status;
492 this->parent.close = ( void (*)( page ) )this_page_close;
493 this->app = app;
494 this->generation = -1;
495
496 widget = lookup_widget( this_page_get_widget( this ), "list_clips" );
497 g_signal_connect( G_OBJECT( widget ), "button-press-event", G_CALLBACK( on_clip_selected ), this );
498 g_signal_connect( G_OBJECT( widget ), "key-press-event", G_CALLBACK( on_clip_key_press ), this );
499 widget = lookup_widget( this_page_get_widget( this ), "list_dir" );
500 g_signal_connect( G_OBJECT( widget ), "button-press-event", G_CALLBACK( on_dir_selected ), this );
501 g_signal_connect( G_OBJECT( widget ), "key-press-event", G_CALLBACK( on_dir_key_press ), this );
502 widget = lookup_widget( this_page_get_widget( this ), "button_clips_refresh" );
503 gtk_signal_connect( GTK_OBJECT( widget ), "clicked", GTK_SIGNAL_FUNC( on_refresh ), this );
504 widget = lookup_widget( this_page_get_widget( this ), "button_clips_home" );
505 gtk_signal_connect( GTK_OBJECT( widget ), "clicked", GTK_SIGNAL_FUNC( on_home ), this );
506 widget = lookup_widget( this_page_get_widget( this ), "button_up" );
507 gtk_signal_connect( GTK_OBJECT( widget ), "clicked", GTK_SIGNAL_FUNC( on_up ), this );
508 widget = lookup_widget( this_page_get_widget( this ), "button_down" );
509 gtk_signal_connect( GTK_OBJECT( widget ), "clicked", GTK_SIGNAL_FUNC( on_down ), this );
510 widget = lookup_widget( this_page_get_widget( this ), "button_remove" );
511 gtk_signal_connect( GTK_OBJECT( widget ), "clicked", GTK_SIGNAL_FUNC( on_remove ), this );
512 widget = lookup_widget( this_page_get_widget( this ), "button_clean" );
513 gtk_signal_connect( GTK_OBJECT( widget ), "clicked", GTK_SIGNAL_FUNC( on_clean ), this );
514
515 widget = lookup_widget( this_page_get_widget( this ), "table4" );
516 gtk_widget_show( widget );
517
518 widget = lookup_widget( this_page_get_widget( this ), "treeview1" );
519 g_signal_connect( G_OBJECT( widget ), "button-press-event", G_CALLBACK( on_queue_selected ), this );
520 g_signal_connect( G_OBJECT( widget ), "key-press-event", G_CALLBACK( on_queue_key_press ), this );
521
522 for ( index = 0; index < 4; index ++ )
523 {
524 char item[ 256 ];
525 sprintf( item, "mode_%d", index );
526 widget = lookup_widget( this_page_get_widget( this ), item );
527 gtk_signal_connect( GTK_OBJECT( widget ), "activate", GTK_SIGNAL_FUNC( on_mode_change ), this );
528 this->modes[ index ] = widget;
529 }
530
531 return ( page )this;
532 }