summaryrefslogtreecommitdiff
path: root/tests/examples/ges-ui.c
blob: 0d79e0528c3c28b95e17fab9ba069ab86fe5f68c (plain)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
/* GStreamer Editing Services
 * Copyright (C) 2010 Brandon Lewis <brandon.lewis@collabora.co.uk>
 *               2010 Nokia Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <ges/ges.h>

/* Application Data ********************************************************/

/**
 * Contains most of the application data so that signal handlers
 * and other callbacks have easy access.
 */

typedef struct App
{
  /* back-end objects */
  GESTimeline *timeline;
  GESPipeline *pipeline;
  GESLayer *layer;
  GESTrack *audio_track;
  GESTrack *video_track;
  guint audio_tracks;
  guint video_tracks;

  /* application state */
  gchar *pending_uri;
  int n_objects;

  int n_selected;
  GList *selected_objects;
  GType selected_type;
  gboolean first_selected;
  gboolean last_selected;

  gboolean ignore_input;
  GstState state;

  GtkListStore *model;
  GtkTreeSelection *selection;

  /* widgets */
  GtkWidget *main_window;
  GtkWidget *add_effect_dlg;
  GtkWidget *properties;
  GtkWidget *filesource_properties;
  GtkWidget *text_properties;
  GtkWidget *generic_duration;
  GtkWidget *background_properties;
  GtkWidget *audio_effect_entry;
  GtkWidget *video_effect_entry;

  GtkHScale *duration;
  GtkHScale *in_point;
  GtkHScale *volume;

  GtkAction *add_file;
  GtkAction *add_effect;
  GtkAction *add_test;
  GtkAction *add_title;
  GtkAction *add_transition;
  GtkAction *delete;
  GtkAction *play;
  GtkAction *stop;
  GtkAction *move_up;
  GtkAction *move_down;
  GtkToggleAction *audio_track_action;
  GtkToggleAction *video_track_action;

  GtkComboBox *halign;
  GtkComboBox *valign;
  GtkComboBox *background_type;

  GtkEntry *text;
  GtkEntry *seconds;

  GtkSpinButton *frequency;
} App;

static int n_instances = 0;

/* Prototypes for auto-connected signal handlers ***************************/

/**
 * These are declared non-static for signal auto-connection
 */

gboolean window_delete_event_cb (GtkObject * window, GdkEvent * event,
    App * app);
void new_activate_cb (GtkMenuItem * item, App * app);
void open_activate_cb (GtkMenuItem * item, App * app);
void save_as_activate_cb (GtkMenuItem * item, App * app);
void launch_project_activate_cb (GtkMenuItem * item, App * app);
void quit_item_activate_cb (GtkMenuItem * item, App * app);
void delete_activate_cb (GtkAction * item, App * app);
void play_activate_cb (GtkAction * item, App * app);
void stop_activate_cb (GtkAction * item, App * app);
void move_up_activate_cb (GtkAction * item, App * app);
void move_down_activate_cb (GtkAction * item, App * app);
void add_effect_activate_cb (GtkAction * item, App * app);
void add_file_activate_cb (GtkAction * item, App * app);
void add_text_activate_cb (GtkAction * item, App * app);
void add_test_activate_cb (GtkAction * item, App * app);
void audio_track_activate_cb (GtkToggleAction * item, App * app);
void video_track_activate_cb (GtkToggleAction * item, App * app);
void add_transition_activate_cb (GtkAction * item, App * app);
void app_selection_changed_cb (GtkTreeSelection * selection, App * app);
void halign_changed_cb (GtkComboBox * widget, App * app);
void valign_changed_cb (GtkComboBox * widget, App * app);
void background_type_changed_cb (GtkComboBox * widget, App * app);
void frequency_value_changed_cb (GtkSpinButton * widget, App * app);
void on_apply_effect_cb (GtkButton * button, App * app);
void on_cancel_add_effect_cb (GtkButton * button, App * app);
gboolean add_effect_dlg_delete_event_cb (GtkWidget * widget, GdkEvent * event,
    gpointer * app);

gboolean
duration_scale_change_value_cb (GtkRange * range,
    GtkScrollType unused, gdouble value, App * app);

gboolean
in_point_scale_change_value_cb (GtkRange * range,
    GtkScrollType unused, gdouble value, App * app);

gboolean
volume_change_value_cb (GtkRange * range,
    GtkScrollType unused, gdouble value, App * app);

/* UI state functions *******************************************************/

/**
 * Update properties of UI elements that depend on more than one thing.
 */

static void
update_effect_sensitivity (App * app)
{
  GList *i;
  gboolean ok = TRUE;

  /* effects will work for multiple FileSource */
  for (i = app->selected_objects; i; i = i->next) {
    if (!GES_IS_URI_CLIP (i->data)) {
      ok = FALSE;
      break;
    }
  }

  gtk_action_set_sensitive (app->add_effect,
      ok && (app->n_selected > 0) && (app->state != GST_STATE_PLAYING)
      && (app->state != GST_STATE_PAUSED));
}

static void
update_delete_sensitivity (App * app)
{
  /* delete will work for multiple items */
  gtk_action_set_sensitive (app->delete,
      (app->n_selected > 0) && (app->state != GST_STATE_PLAYING)
      && (app->state != GST_STATE_PAUSED));
}

static void
update_add_transition_sensitivity (App * app)
{
  gtk_action_set_sensitive (app->add_transition,
      (app->state != GST_STATE_PLAYING) && (app->state != GST_STATE_PAUSED));
}

static void
update_move_up_down_sensitivity (App * app)
{
  gboolean can_move;

  can_move = (app->n_selected == 1) &&
      (app->state != GST_STATE_PLAYING) && (app->state != GST_STATE_PAUSED);

  gtk_action_set_sensitive (app->move_up, can_move && (!app->first_selected));
  gtk_action_set_sensitive (app->move_down, can_move && (!app->last_selected));
}

static void
update_play_sensitivity (App * app)
{
  gtk_action_set_sensitive (app->play, app->n_objects);
}

/* Backend callbacks ********************************************************/

static void
test_source_notify_volume_changed_cb (GESClip * clip, GParamSpec *
    unused G_GNUC_UNUSED, App * app)
{
  gdouble volume;

  g_object_get (G_OBJECT (clip), "volume", &volume, NULL);

  gtk_range_set_value (GTK_RANGE (app->volume), volume);
}

static void
layer_notify_valid_changed_cb (GObject * object, GParamSpec * unused
    G_GNUC_UNUSED, App * app)
{
  update_play_sensitivity (app);
}

static gboolean
find_row_for_object (GtkListStore * model, GtkTreeIter * ret, GESClip * clip)
{
  gtk_tree_model_get_iter_first ((GtkTreeModel *) model, ret);

  while (gtk_list_store_iter_is_valid (model, ret)) {
    GESClip *clip2;
    gtk_tree_model_get ((GtkTreeModel *) model, ret, 2, &clip2, -1);
    if (clip2 == clip) {
      gst_object_unref (clip2);
      return TRUE;
    }
    gst_object_unref (clip2);
    gtk_tree_model_iter_next ((GtkTreeModel *) model, ret);
  }
  return FALSE;
}

/* this callback is registered for every clip, and updates the
 * corresponding duration cell in the model */
static void
clip_notify_duration_cb (GESClip * clip,
    GParamSpec * arg G_GNUC_UNUSED, App * app)
{
  GtkTreeIter iter;
  guint64 duration = 0;

  g_object_get (clip, "duration", &duration, NULL);
  find_row_for_object (app->model, &iter, clip);
  gtk_list_store_set (app->model, &iter, 1, duration, -1);
}

/* these guys are only connected to filesources that are the target of the
 * current selection */

static void
filesource_notify_duration_cb (GESClip * clip,
    GParamSpec * arg G_GNUC_UNUSED, App * app)
{
  guint64 duration, max_inpoint;
  duration = GES_TIMELINE_ELEMENT_DURATION (clip);
  max_inpoint = GES_TIMELINE_ELEMENT_MAX_DURATION (clip) - duration;

  gtk_range_set_value (GTK_RANGE (app->duration), duration);
  gtk_range_set_fill_level (GTK_RANGE (app->in_point), max_inpoint);

  if (max_inpoint < GES_TIMELINE_ELEMENT_INPOINT (clip))
    g_object_set (clip, "in-point", max_inpoint, NULL);

}

static void
filesource_notify_max_duration_cb (GESClip * clip,
    GParamSpec * arg G_GNUC_UNUSED, App * app)
{
  gtk_range_set_range (GTK_RANGE (app->duration), 0, (gdouble)
      GES_TIMELINE_ELEMENT_MAX_DURATION (clip));
  gtk_range_set_range (GTK_RANGE (app->in_point), 0, (gdouble)
      GES_TIMELINE_ELEMENT_MAX_DURATION (clip));
}

static void
filesource_notify_in_point_cb (GESClip * clip,
    GParamSpec * arg G_GNUC_UNUSED, App * app)
{
  gtk_range_set_value (GTK_RANGE (app->in_point),
      GES_TIMELINE_ELEMENT_INPOINT (clip));
}

static void
app_update_first_last_selected (App * app)
{
  GtkTreePath *path;

  /* keep track of whether the first or last items are selected */
  path = gtk_tree_path_new_from_indices (0, -1);
  app->first_selected =
      gtk_tree_selection_path_is_selected (app->selection, path);
  gtk_tree_path_free (path);

  path = gtk_tree_path_new_from_indices (app->n_objects - 1, -1);
  app->last_selected =
      gtk_tree_selection_path_is_selected (app->selection, path);
  gtk_tree_path_free (path);
}

static void
object_count_changed (App * app)
{
  app_update_first_last_selected (app);
  update_move_up_down_sensitivity (app);
  update_play_sensitivity (app);
}

static void
title_source_text_changed_cb (GESClip * clip,
    GParamSpec * arg G_GNUC_UNUSED, App * app)
{
  GtkTreeIter iter;
  gchar *text;

  g_object_get (clip, "text", &text, NULL);
  if (text) {
    find_row_for_object (app->model, &iter, clip);
    gtk_list_store_set (app->model, &iter, 0, text, -1);
  }
}

static void
layer_object_added_cb (GESLayer * layer, GESClip * clip, App * app)
{
  GtkTreeIter iter;
  gchar *description;

  GST_INFO ("layer clip added cb %p %p %p", layer, clip, app);

  gtk_list_store_append (app->model, &iter);

  if (GES_IS_URI_CLIP (clip)) {
    g_object_get (G_OBJECT (clip), "uri", &description, NULL);
    gtk_list_store_set (app->model, &iter, 0, description, 2, clip, -1);
  }

  else if (GES_IS_TITLE_CLIP (clip)) {
    gtk_list_store_set (app->model, &iter, 2, clip, -1);
    g_signal_connect (G_OBJECT (clip), "notify::text",
        G_CALLBACK (title_source_text_changed_cb), app);
    title_source_text_changed_cb (clip, NULL, app);
  }

  else if (GES_IS_TEST_CLIP (clip)) {
    gtk_list_store_set (app->model, &iter, 2, clip, 0, "Test Source", -1);
  }

  else if (GES_IS_BASE_TRANSITION_CLIP (clip)) {
    gtk_list_store_set (app->model, &iter, 2, clip, 0, "Transition", -1);
  }

  g_signal_connect (G_OBJECT (clip), "notify::duration",
      G_CALLBACK (clip_notify_duration_cb), app);
  clip_notify_duration_cb (clip, NULL, app);

  app->n_objects++;
  object_count_changed (app);
}

static void
layer_object_removed_cb (GESLayer * layer, GESClip * clip, App * app)
{
  GtkTreeIter iter;

  GST_INFO ("layer clip removed cb %p %p %p", layer, clip, app);

  if (!find_row_for_object (GTK_LIST_STORE (app->model), &iter, clip)) {
    g_print ("clip deleted but we don't own it");
    return;
  }
  app->n_objects--;
  object_count_changed (app);

  gtk_list_store_remove (app->model, &iter);
}

static void
layer_object_moved_cb (GESClip * layer, GESClip * clip,
    gint old, gint new, App * app)
{
  GtkTreeIter a, b;
  GtkTreePath *path;

  /* we can take the old position as given, but the new position might have to
   * be adjusted. */
  new = new < 0 ? (app->n_objects - 1) : new;

  path = gtk_tree_path_new_from_indices (old, -1);
  gtk_tree_model_get_iter (GTK_TREE_MODEL (app->model), &a, path);
  gtk_tree_path_free (path);

  path = gtk_tree_path_new_from_indices (new, -1);
  gtk_tree_model_get_iter (GTK_TREE_MODEL (app->model), &b, path);
  gtk_tree_path_free (path);

  gtk_list_store_swap (app->model, &a, &b);
  app_selection_changed_cb (app->selection, app);
  update_move_up_down_sensitivity (app);
}

static void
pipeline_state_changed_cb (App * app)
{
  gboolean playing_or_paused;

  if (app->state == GST_STATE_PLAYING)
    gtk_action_set_stock_id (app->play, GTK_STOCK_MEDIA_PAUSE);
  else
    gtk_action_set_stock_id (app->play, GTK_STOCK_MEDIA_PLAY);

  update_delete_sensitivity (app);
  update_add_transition_sensitivity (app);
  update_move_up_down_sensitivity (app);

  playing_or_paused = (app->state == GST_STATE_PLAYING) ||
      (app->state == GST_STATE_PAUSED);

  gtk_action_set_sensitive (app->add_file, !playing_or_paused);
  gtk_action_set_sensitive (app->add_title, !playing_or_paused);
  gtk_action_set_sensitive (app->add_test, !playing_or_paused);
  gtk_action_set_sensitive ((GtkAction *) app->audio_track_action,
      !playing_or_paused);
  gtk_action_set_sensitive ((GtkAction *) app->video_track_action,
      !playing_or_paused);
  gtk_widget_set_sensitive (app->properties, !playing_or_paused);
}

static void
project_bus_message_cb (GstBus * bus, GstMessage * message,
    GMainLoop * mainloop)
{
  switch (GST_MESSAGE_TYPE (message)) {
    case GST_MESSAGE_ERROR:
      g_printerr ("ERROR\n");
      g_main_loop_quit (mainloop);
      break;
    case GST_MESSAGE_EOS:
      g_printerr ("Done\n");
      g_main_loop_quit (mainloop);
      break;
    default:
      break;
  }
}

static void
bus_message_cb (GstBus * bus, GstMessage * message, App * app)
{
  const GstStructure *s;
  s = gst_message_get_structure (message);

  switch (GST_MESSAGE_TYPE (message)) {
    case GST_MESSAGE_ERROR:
      g_print ("ERROR\n");
      break;
    case GST_MESSAGE_EOS:
      gst_element_set_state (GST_ELEMENT (app->pipeline), GST_STATE_READY);
      break;
    case GST_MESSAGE_STATE_CHANGED:
      if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (app->pipeline)) {
        GstState old, new, pending;
        gst_message_parse_state_changed (message, &old, &new, &pending);
        app->state = new;
        pipeline_state_changed_cb (app);
      }
      break;
    default:
      break;
  }
}

/* Static UI Callbacks ******************************************************/

static gboolean
check_time (const gchar * time)
{
  static GRegex *re = NULL;

  if (!re) {
    if (NULL == (re =
            g_regex_new ("^[0-9][0-9]:[0-5][0-9]:[0-5][0-9](\\.[0-9]+)?$",
                G_REGEX_EXTENDED, 0, NULL)))
      return FALSE;
  }

  if (g_regex_match (re, time, 0, NULL))
    return TRUE;
  return FALSE;
}

static guint64
str_to_time (const gchar * str)
{
  guint64 ret;
  guint64 h, m;
  gdouble s;
  gchar buf[15];

  buf[0] = str[0];
  buf[1] = str[1];
  buf[2] = '\0';

  h = strtoull (buf, NULL, 10);

  buf[0] = str[3];
  buf[1] = str[4];
  buf[2] = '\0';

  m = strtoull (buf, NULL, 10);

  strncpy (buf, &str[6], sizeof (buf));
  s = strtod (buf, NULL);

  ret = (h * 3600 * GST_SECOND) +
      (m * 60 * GST_SECOND) + ((guint64) (s * GST_SECOND));

  return ret;
}

static void
text_notify_text_changed_cb (GtkEntry * widget, GParamSpec * unused, App * app)
{
  GList *tmp;
  const gchar *text;

  if (app->ignore_input)
    return;

  text = gtk_entry_get_text (widget);

  for (tmp = app->selected_objects; tmp; tmp = tmp->next) {
    g_object_set (G_OBJECT (tmp->data), "text", text, NULL);
  }
}

static void
seconds_notify_text_changed_cb (GtkEntry * widget, GParamSpec * unused,
    App * app)
{
  GList *tmp;
  const gchar *text;

  if (app->ignore_input)
    return;

  text = gtk_entry_get_text (app->seconds);

  if (!check_time (text)) {
    gtk_entry_set_icon_from_stock (app->seconds,
        GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_DIALOG_WARNING);
  } else {
    gtk_entry_set_icon_from_stock (app->seconds,
        GTK_ENTRY_ICON_SECONDARY, NULL);
    for (tmp = app->selected_objects; tmp; tmp = tmp->next) {
      g_object_set (GES_CLIP (tmp->data), "duration",
          (guint64) str_to_time (text), NULL);
    }
  }
}

static void
duration_cell_func (GtkTreeViewColumn * column, GtkCellRenderer * renderer,
    GtkTreeModel * model, GtkTreeIter * iter, gpointer user)
{
  gchar buf[30];
  guint64 duration;

  gtk_tree_model_get (model, iter, 1, &duration, -1);
  g_snprintf (buf, sizeof (buf), "%u:%02u:%02u.%09u", GST_TIME_ARGS (duration));
  g_object_set (renderer, "text", &buf, NULL);
}

/* UI Initialization ********************************************************/

static void
connect_to_filesource (GESClip * clip, App * app)
{
  g_signal_connect (G_OBJECT (clip), "notify::max-duration",
      G_CALLBACK (filesource_notify_max_duration_cb), app);
  filesource_notify_max_duration_cb (clip, NULL, app);

  g_signal_connect (G_OBJECT (clip), "notify::duration",
      G_CALLBACK (filesource_notify_duration_cb), app);
  filesource_notify_duration_cb (clip, NULL, app);

  g_signal_connect (G_OBJECT (clip), "notify::in-point",
      G_CALLBACK (filesource_notify_in_point_cb), app);
  filesource_notify_in_point_cb (clip, NULL, app);
}

static void
disconnect_from_filesource (GESClip * clip, App * app)
{
  g_signal_handlers_disconnect_by_func (G_OBJECT (clip),
      filesource_notify_duration_cb, app);

  g_signal_handlers_disconnect_by_func (G_OBJECT (clip),
      filesource_notify_max_duration_cb, app);
}

static void
connect_to_title_source (GESClip * clip, App * app)
{
  GESTitleClip *titleclip;
  titleclip = GES_TITLE_CLIP (clip);
  gtk_combo_box_set_active (app->halign,
      ges_title_clip_get_halignment (titleclip));
  gtk_combo_box_set_active (app->valign,
      ges_title_clip_get_valignment (titleclip));
  gtk_entry_set_text (app->text, ges_title_clip_get_text (titleclip));
}

static void
disconnect_from_title_source (GESClip * clip, App * app)
{
}

static void
connect_to_test_source (GESClip * clip, App * app)
{
  GObjectClass *klass;
  GParamSpecDouble *pspec;

  GESTestClip *testclip;
  testclip = GES_TEST_CLIP (clip);
  gtk_combo_box_set_active (app->background_type,
      ges_test_clip_get_vpattern (testclip));

  g_signal_connect (G_OBJECT (testclip), "notify::volume",
      G_CALLBACK (test_source_notify_volume_changed_cb), app);
  test_source_notify_volume_changed_cb (clip, NULL, app);

  klass = G_OBJECT_GET_CLASS (G_OBJECT (testclip));

  pspec = G_PARAM_SPEC_DOUBLE (g_object_class_find_property (klass, "volume"));
  gtk_range_set_range (GTK_RANGE (app->volume), pspec->minimum, pspec->maximum);

  pspec = G_PARAM_SPEC_DOUBLE (g_object_class_find_property (klass, "freq"));
  gtk_spin_button_set_range (app->frequency, pspec->minimum, pspec->maximum);
  gtk_spin_button_set_value (app->frequency,
      ges_test_clip_get_frequency (GES_TEST_CLIP (clip)));
}

static void
disconnect_from_test_source (GESClip * clip, App * app)
{
  g_signal_handlers_disconnect_by_func (G_OBJECT (clip),
      test_source_notify_volume_changed_cb, app);
}

static void
connect_to_object (GESClip * clip, App * app)
{
  gchar buf[30];
  guint64 duration;

  app->ignore_input = TRUE;

  duration = GES_TIMELINE_ELEMENT_DURATION (clip);
  g_snprintf (buf, sizeof (buf), "%02u:%02u:%02u.%09u",
      GST_TIME_ARGS (duration));
  gtk_entry_set_text (app->seconds, buf);

  if (GES_IS_URI_CLIP (clip)) {
    connect_to_filesource (clip, app);
  } else if (GES_IS_TITLE_CLIP (clip)) {
    connect_to_title_source (clip, app);
  } else if (GES_IS_TEST_CLIP (clip)) {
    connect_to_test_source (clip, app);
  }

  app->ignore_input = FALSE;
}

static void
disconnect_from_object (GESClip * clip, App * app)
{
  if (GES_IS_URI_CLIP (clip)) {
    disconnect_from_filesource (clip, app);
  } else if (GES_IS_TITLE_CLIP (clip)) {
    disconnect_from_title_source (clip, app);
  } else if (GES_IS_TEST_CLIP (clip)) {
    disconnect_from_test_source (clip, app);
  }
}

static GtkListStore *
get_video_patterns (void)
{
  GEnumClass *enum_class;
  GESTestClip *tr;
  GESTestClipClass *klass;
  GParamSpec *pspec;
  GEnumValue *v;
  GtkListStore *m;
  GtkTreeIter i;

  m = gtk_list_store_new (1, G_TYPE_STRING);

  tr = ges_test_clip_new ();
  klass = GES_TEST_CLIP_GET_CLASS (tr);

  pspec = g_object_class_find_property (G_OBJECT_CLASS (klass), "vpattern");

  enum_class = G_ENUM_CLASS (g_type_class_ref (pspec->value_type));

  for (v = enum_class->values; v->value_nick != NULL; v++) {
    gtk_list_store_append (m, &i);
    gtk_list_store_set (m, &i, 0, v->value_name, -1);
  }

  g_type_class_unref (enum_class);
  gst_object_unref (tr);

  return m;
}

#define GET_WIDGET(dest,name,type) {\
  if (!(dest =\
    type(gtk_builder_get_object(builder, name))))\
        goto fail;\
}


static void
layer_added_cb (GESTimeline * timeline, GESLayer * layer, App * app)
{
  if (!GES_IS_LAYER (layer)) {
    GST_ERROR ("This timeline contains a layer type other than "
        "GESLayer. Timeline editing disabled");
    return;
  }

  if (!(app->layer)) {
    app->layer = layer;
  }

  if (layer != app->layer) {
    GST_ERROR ("This demo doesn't support editing timelines with multiple"
        " layers");
    return;
  }

  g_signal_connect (app->layer, "clip-added",
      G_CALLBACK (layer_object_added_cb), app);
  g_signal_connect (app->layer, "clip-removed",
      G_CALLBACK (layer_object_removed_cb), app);
  g_signal_connect (app->layer, "object-moved",
      G_CALLBACK (layer_object_moved_cb), app);
  g_signal_connect (app->layer, "notify::valid",
      G_CALLBACK (layer_notify_valid_changed_cb), app);
}

static void
update_track_actions (App * app)
{
  g_signal_handlers_disconnect_by_func (app->audio_track_action,
      audio_track_activate_cb, app);
  g_signal_handlers_disconnect_by_func (app->video_track_action,
      video_track_activate_cb, app);
  gtk_toggle_action_set_active (app->audio_track_action, app->audio_tracks);
  gtk_toggle_action_set_active (app->video_track_action, app->video_tracks);
  gtk_action_set_sensitive ((GtkAction *) app->audio_track_action,
      app->audio_tracks <= 1);
  gtk_action_set_sensitive ((GtkAction *) app->video_track_action,
      app->video_tracks <= 1);
  g_signal_connect (G_OBJECT (app->audio_track_action), "activate",
      G_CALLBACK (audio_track_activate_cb), app);
  g_signal_connect (G_OBJECT (app->video_track_action), "activate",
      G_CALLBACK (video_track_activate_cb), app);
}

static void
track_added_cb (GESTimeline * timeline, GESTrack * track, App * app)
{
  if (track->type == GES_TRACK_TYPE_AUDIO) {
    app->audio_tracks++;
    if (!app->audio_track)
      app->audio_track = track;
  }
  if (track->type == GES_TRACK_TYPE_VIDEO) {
    app->video_tracks++;
    if (!app->video_track)
      app->video_track = track;
  }


  update_track_actions (app);
}

static void
track_removed_cb (GESTimeline * timeline, GESTrack * track, App * app)
{
  if (track->type == GES_TRACK_TYPE_AUDIO)
    app->audio_tracks--;
  if (track->type == GES_TRACK_TYPE_VIDEO)
    app->video_tracks--;

  update_track_actions (app);
}

static gboolean
create_ui (App * app)
{
  GtkBuilder *builder;
  GtkTreeView *timeline;
  GtkTreeViewColumn *duration_col;
  GtkCellRenderer *duration_renderer;
  GtkCellRenderer *background_type_renderer;
  GtkListStore *backgrounds;
  GstBus *bus;

  /* construct widget tree */

  builder = gtk_builder_new ();
  gtk_builder_add_from_file (builder, "ges-ui.glade", NULL);

  /* get a bunch of widgets from the XML tree */

  GET_WIDGET (timeline, "timeline_treeview", GTK_TREE_VIEW);
  GET_WIDGET (app->properties, "properties", GTK_WIDGET);
  GET_WIDGET (app->filesource_properties, "filesource_properties", GTK_WIDGET);
  GET_WIDGET (app->text_properties, "text_properties", GTK_WIDGET);
  GET_WIDGET (app->main_window, "window", GTK_WIDGET);
  GET_WIDGET (app->add_effect_dlg, "add_effect_dlg", GTK_WIDGET);
  GET_WIDGET (app->audio_effect_entry, "entry1", GTK_WIDGET);
  GET_WIDGET (app->video_effect_entry, "entry2", GTK_WIDGET);
  GET_WIDGET (app->duration, "duration_scale", GTK_HSCALE);
  GET_WIDGET (app->in_point, "in_point_scale", GTK_HSCALE);
  GET_WIDGET (app->halign, "halign", GTK_COMBO_BOX);
  GET_WIDGET (app->valign, "valign", GTK_COMBO_BOX);
  GET_WIDGET (app->text, "text", GTK_ENTRY);
  GET_WIDGET (duration_col, "duration_column", GTK_TREE_VIEW_COLUMN);
  GET_WIDGET (duration_renderer, "duration_renderer", GTK_CELL_RENDERER);
  GET_WIDGET (app->add_file, "add_file", GTK_ACTION);
  GET_WIDGET (app->add_effect, "add_effect", GTK_ACTION);
  GET_WIDGET (app->add_title, "add_text", GTK_ACTION);
  GET_WIDGET (app->add_test, "add_test", GTK_ACTION);
  GET_WIDGET (app->add_transition, "add_transition", GTK_ACTION);
  GET_WIDGET (app->delete, "delete", GTK_ACTION);
  GET_WIDGET (app->play, "play", GTK_ACTION);
  GET_WIDGET (app->stop, "stop", GTK_ACTION);
  GET_WIDGET (app->move_up, "move_up", GTK_ACTION);
  GET_WIDGET (app->move_down, "move_down", GTK_ACTION);
  GET_WIDGET (app->seconds, "seconds", GTK_ENTRY);
  GET_WIDGET (app->generic_duration, "generic_duration", GTK_WIDGET);
  GET_WIDGET (app->background_type, "background_type", GTK_COMBO_BOX);
  GET_WIDGET (app->background_properties, "background_properties", GTK_WIDGET);
  GET_WIDGET (app->frequency, "frequency", GTK_SPIN_BUTTON);
  GET_WIDGET (app->volume, "volume", GTK_HSCALE);
  GET_WIDGET (app->audio_track_action, "audio_track", GTK_TOGGLE_ACTION);
  GET_WIDGET (app->video_track_action, "video_track", GTK_TOGGLE_ACTION);

  /* get text notifications */

  g_signal_connect (app->text, "notify::text",
      G_CALLBACK (text_notify_text_changed_cb), app);

  g_signal_connect (app->seconds, "notify::text",
      G_CALLBACK (seconds_notify_text_changed_cb), app);

  /* we care when the tree selection changes */

  if (!(app->selection = gtk_tree_view_get_selection (timeline)))
    goto fail;

  gtk_tree_selection_set_mode (app->selection, GTK_SELECTION_MULTIPLE);

  g_signal_connect (app->selection, "changed",
      G_CALLBACK (app_selection_changed_cb), app);

  /* create the model for the treeview */

  if (!(app->model =
          gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_UINT64, G_TYPE_OBJECT)))
    goto fail;

  gtk_tree_view_set_model (timeline, GTK_TREE_MODEL (app->model));

  /* register custom cell data function */

  gtk_tree_view_column_set_cell_data_func (duration_col, duration_renderer,
      duration_cell_func, NULL, NULL);

  /* initialize combo boxes */

  if (!(backgrounds = get_video_patterns ()))
    goto fail;

  if (!(background_type_renderer = gtk_cell_renderer_text_new ()))
    goto fail;

  gtk_combo_box_set_model (app->background_type, (GtkTreeModel *)
      backgrounds);

  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (app->background_type),
      background_type_renderer, FALSE);

  gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (app->background_type),
      background_type_renderer, "text", 0);

  g_signal_connect (app->timeline, "layer-added", G_CALLBACK
      (layer_added_cb), app);
  g_signal_connect (app->timeline, "track-added", G_CALLBACK
      (track_added_cb), app);
  g_signal_connect (app->timeline, "track-removed", G_CALLBACK
      (track_removed_cb), app);

  /* register callbacks on GES objects */
  bus = gst_pipeline_get_bus (GST_PIPELINE (app->pipeline));
  gst_bus_add_signal_watch (bus);
  g_signal_connect (bus, "message", G_CALLBACK (bus_message_cb), app);

  /* success */
  gtk_builder_connect_signals (builder, app);
  gst_object_unref (G_OBJECT (builder));
  return TRUE;

fail:
  gst_object_unref (G_OBJECT (builder));
  return FALSE;
}

#undef GET_WIDGET

/* application methods ******************************************************/

static void selection_foreach (GtkTreeModel * model, GtkTreePath * path,
    GtkTreeIter * iter, gpointer user);

static void
app_toggle_playpause (App * app)
{
  if (app->state != GST_STATE_PLAYING) {
    gst_element_set_state (GST_ELEMENT (app->pipeline), GST_STATE_PLAYING);
  } else {
    gst_element_set_state (GST_ELEMENT (app->pipeline), GST_STATE_PAUSED);
  }
}

static void
app_stop_playback (App * app)
{
  if ((app->state != GST_STATE_NULL) && (app->state != GST_STATE_READY)) {
    gst_element_set_state (GST_ELEMENT (app->pipeline), GST_STATE_READY);
  }
}

typedef struct
{
  GList *objects;
  guint n;
} select_info;

static void
app_update_selection (App * app)
{
  GList *cur;
  GType type;
  select_info info = { NULL, 0 };

  /* clear old selection */
  for (cur = app->selected_objects; cur; cur = cur->next) {
    disconnect_from_object (cur->data, app);
    gst_object_unref (cur->data);
    cur->data = NULL;
  }
  g_list_free (app->selected_objects);
  app->selected_objects = NULL;
  app->n_selected = 0;

  /* get new selection */
  gtk_tree_selection_selected_foreach (GTK_TREE_SELECTION (app->selection),
      selection_foreach, &info);
  app->selected_objects = info.objects;
  app->n_selected = info.n;

  type = G_TYPE_NONE;
  if (app->selected_objects) {
    type = G_TYPE_FROM_INSTANCE (app->selected_objects->data);
    for (cur = app->selected_objects; cur; cur = cur->next) {
      if (type != G_TYPE_FROM_INSTANCE (cur->data)) {
        type = G_TYPE_NONE;
        break;
      }
    }
  }

  if (type != G_TYPE_NONE) {
    for (cur = app->selected_objects; cur; cur = cur->next) {
      connect_to_object (cur->data, app);
    }
  }

  app->selected_type = type;
  app_update_first_last_selected (app);
}

static void
selection_foreach (GtkTreeModel * model, GtkTreePath * path, GtkTreeIter
    * iter, gpointer user)
{
  select_info *info = (select_info *) user;
  GESClip *clip;

  gtk_tree_model_get (model, iter, 2, &clip, -1);
  info->objects = g_list_append (info->objects, clip);

  info->n++;
  return;
}

static GList *
app_get_selected_objects (App * app)
{
  return g_list_copy (app->selected_objects);
}

static void
app_delete_objects (App * app, GList * objects)
{
  GList *cur;

  for (cur = objects; cur; cur = cur->next) {
    ges_layer_remove_clip (app->layer, GES_CLIP (cur->data));
    cur->data = NULL;
  }

  g_list_free (objects);
}

/* the following two methods assume exactly one clip is selected and that the
 * requested action is valid */

static void
app_move_selected_up (App * app)
{
  GST_FIXME ("This function is not implement, please implement it :)");
}

static void
app_add_effect_on_selected_clips (App * app, const gchar * bin_desc)
{
  GList *objects, *tmp;
  GESTrackElement *effect = NULL;

  /* No crash if the video is playing */
  gst_element_set_state (GST_ELEMENT (app->pipeline), GST_STATE_PAUSED);
  objects = ges_layer_get_clips (app->layer);

  for (tmp = objects; tmp; tmp = tmp->next) {
    effect = GES_TRACK_ELEMENT (ges_effect_new (bin_desc));
    ges_container_add (GES_CONTAINER (tmp->data),
        GES_TIMELINE_ELEMENT (effect));
    gst_object_unref (tmp->data);
  }
}

gboolean
add_effect_dlg_delete_event_cb (GtkWidget * widget, GdkEvent * event,
    gpointer * app)
{
  gtk_widget_hide_all (((App *) app)->add_effect_dlg);
  return TRUE;
}

void
on_cancel_add_effect_cb (GtkButton * button, App * app)
{
  gtk_widget_hide_all (app->add_effect_dlg);
}

void
on_apply_effect_cb (GtkButton * button, App * app)
{
  const gchar *effect;

  effect = gtk_entry_get_text (GTK_ENTRY (app->video_effect_entry));
  if (g_strcmp0 (effect, ""))
    app_add_effect_on_selected_clips (app, effect);

  gtk_entry_set_text (GTK_ENTRY (app->video_effect_entry), "");

  effect = gtk_entry_get_text (GTK_ENTRY (app->audio_effect_entry));
  if (g_strcmp0 (effect, ""))
    app_add_effect_on_selected_clips (app, effect);

  gtk_entry_set_text (GTK_ENTRY (app->audio_effect_entry), "");

  gtk_widget_hide_all (app->add_effect_dlg);
}

static void
app_move_selected_down (App * app)
{
  GST_FIXME ("This function is not implement, please implement it :)");
}

static void
app_add_file (App * app, gchar * uri)
{
  GESClip *clip;

  GST_DEBUG ("adding file %s", uri);

  clip = GES_CLIP (ges_uri_clip_new (uri));
  ges_timeline_element_set_start (GES_TIMELINE_ELEMENT (clip),
      ges_layer_get_duration (app->layer));

  ges_layer_add_clip (app->layer, clip);
}

static void
app_launch_project (App * app, gchar * uri)
{
  GESTimeline *timeline;
  GMainLoop *mainloop;
  GESPipeline *pipeline;
  GstBus *bus;
  GESProject *project;

  uri = g_strsplit (uri, "//", 2)[1];
  printf ("we will launch this uri : %s\n", uri);
  project = ges_project_new (uri);
  timeline = GES_TIMELINE (ges_asset_extract (GES_ASSET (project), NULL));
  pipeline = ges_pipeline_new ();
  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
  mainloop = g_main_loop_new (NULL, FALSE);

  ges_pipeline_set_timeline (pipeline, timeline);
  ges_pipeline_set_mode (pipeline, TIMELINE_MODE_PREVIEW_VIDEO);
  gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
  gst_bus_add_signal_watch (bus);
  g_signal_connect (bus, "message", G_CALLBACK (project_bus_message_cb),
      mainloop);
  g_main_loop_run (mainloop);
  g_object_unref (project);
}

static void
app_add_title (App * app)
{
  GESClip *clip;

  GST_DEBUG ("adding title");

  clip = GES_CLIP (ges_title_clip_new ());
  g_object_set (G_OBJECT (clip), "duration", GST_SECOND,
      "start", ges_layer_get_duration (app->layer), NULL);
  ges_layer_add_clip (app->layer, clip);
}

static void
app_add_test (App * app)
{
  GESClip *clip;

  GST_DEBUG ("adding test");

  clip = GES_CLIP (ges_test_clip_new ());
  g_object_set (G_OBJECT (clip), "duration", GST_SECOND,
      "start", ges_layer_get_duration (app->layer), NULL);

  ges_layer_add_clip (app->layer, clip);
}

static void
app_add_transition (App * app)
{
  GST_FIXME ("This function is not implement, please implement it :)");
}

static void
app_save_to_uri (App * app, gchar * uri)
{
  ges_timeline_save_to_uri (app->timeline, uri, NULL, FALSE, NULL);
}

static void
app_add_audio_track (App * app)
{
  if (app->audio_tracks)
    return;

  app->audio_track = GES_TRACK (ges_audio_track_new ());
  ges_timeline_add_track (app->timeline, app->audio_track);
}

static void
app_remove_audio_track (App * app)
{
  if (!app->audio_tracks)
    return;

  ges_timeline_remove_track (app->timeline, app->audio_track);
  app->audio_track = NULL;
}

static void
app_add_video_track (App * app)
{
  if (app->video_tracks)
    return;

  app->video_track = GES_TRACK (ges_video_track_new ());
  ges_timeline_add_track (app->timeline, app->video_track);
}

static void
app_remove_video_track (App * app)
{
  if (!app->video_tracks)
    return;

  ges_timeline_remove_track (app->timeline, app->video_track);
  app->video_track = NULL;
}

static void
app_dispose (App * app)
{
  if (app) {
    if (app->pipeline) {
      gst_element_set_state (GST_ELEMENT (app->pipeline), GST_STATE_NULL);
      gst_object_unref (app->pipeline);
    }

    g_free (app);
  }

  n_instances--;

  if (n_instances == 0) {
    gtk_main_quit ();
  }
}

static App *
app_init (void)
{
  App *ret;
  ret = g_new0 (App, 1);
  n_instances++;

  ret->selected_type = G_TYPE_NONE;

  if (!ret)
    return NULL;

  if (!(ret->timeline = ges_timeline_new ()))
    goto fail;

  if (!(ret->pipeline = ges_pipeline_new ()))
    goto fail;

  if (!ges_pipeline_set_timeline (ret->pipeline, ret->timeline))
    goto fail;

  if (!(create_ui (ret)))
    goto fail;

  return ret;

fail:
  app_dispose (ret);
  return NULL;
}

static App *
app_new (void)
{
  App *ret;
  GESTrack *a = NULL, *v = NULL;

  ret = app_init ();

  /* add base audio and video track */

  if (!(a = GES_TRACK (ges_audio_track_new ())))
    goto fail;

  if (!(ges_timeline_add_track (ret->timeline, a)))
    goto fail;

  if (!(v = GES_TRACK (ges_video_track_new ())))
    goto fail;

  if (!(ges_timeline_add_track (ret->timeline, v)))
    goto fail;

  if (!(ret->layer = ges_layer_new ()))
    goto fail;

  if (!(ges_timeline_add_layer (ret->timeline, ret->layer)))
    goto fail;

  ret->audio_track = a;
  ret->video_track = v;
  return ret;

fail:

  if (a)
    gst_object_unref (a);
  if (v)
    gst_object_unref (v);
  app_dispose (ret);
  return NULL;
}

static gboolean
load_file_async (App * app)
{
  ges_timeline_load_from_uri (app->timeline, app->pending_uri, NULL);

  g_free (app->pending_uri);
  app->pending_uri = NULL;

  return FALSE;
}

static gboolean
app_new_from_uri (gchar * uri)
{
  App *ret;

  ret = app_init ();
  ret->pending_uri = g_strdup (uri);
  g_idle_add ((GSourceFunc) load_file_async, ret);

  return FALSE;
}

/* UI callbacks  ************************************************************/

gboolean
window_delete_event_cb (GtkObject * window, GdkEvent * event, App * app)
{
  app_dispose (app);
  return FALSE;
}

void
new_activate_cb (GtkMenuItem * item, App * app)
{
  app_new ();
}

void
launch_project_activate_cb (GtkMenuItem * item, App * app)
{
  GtkFileChooserDialog *dlg;
  GtkFileFilter *filter;

  GST_DEBUG ("add file signal handler");

  filter = gtk_file_filter_new ();
  gtk_file_filter_set_name (filter, "pitivi projects");
  gtk_file_filter_add_pattern (filter, "*.xptv");
  dlg = (GtkFileChooserDialog *)
      gtk_file_chooser_dialog_new ("Preview Project...",
      GTK_WINDOW (app->main_window),
      GTK_FILE_CHOOSER_ACTION_OPEN,
      GTK_STOCK_CANCEL,
      GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dlg), filter);

  g_object_set (G_OBJECT (dlg), "select-multiple", FALSE, NULL);

  if (gtk_dialog_run ((GtkDialog *) dlg) == GTK_RESPONSE_OK) {
    gchar *uri;
    uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dlg));
    gtk_widget_destroy ((GtkWidget *) dlg);
    app_launch_project (app, uri);
  }
}

void
open_activate_cb (GtkMenuItem * item, App * app)
{
  GtkFileChooserDialog *dlg;

  GST_DEBUG ("add file signal handler");

  dlg = (GtkFileChooserDialog *) gtk_file_chooser_dialog_new ("Open Project...",
      GTK_WINDOW (app->main_window),
      GTK_FILE_CHOOSER_ACTION_OPEN,
      GTK_STOCK_CANCEL,
      GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);

  g_object_set (G_OBJECT (dlg), "select-multiple", FALSE, NULL);

  if (gtk_dialog_run ((GtkDialog *) dlg) == GTK_RESPONSE_OK) {
    gchar *uri;
    uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dlg));
    app_new_from_uri (uri);
    g_free (uri);
  }
  gtk_widget_destroy ((GtkWidget *) dlg);
}

void
save_as_activate_cb (GtkMenuItem * item, App * app)
{
  GtkFileChooserDialog *dlg;

  GST_DEBUG ("save as signal handler");

  dlg = (GtkFileChooserDialog *)
      gtk_file_chooser_dialog_new ("Save project as...",
      GTK_WINDOW (app->main_window), GTK_FILE_CHOOSER_ACTION_SAVE,
      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_OK,
      NULL);

  g_object_set (G_OBJECT (dlg), "select-multiple", FALSE, NULL);

  if (gtk_dialog_run ((GtkDialog *) dlg) == GTK_RESPONSE_OK) {
    gchar *uri;
    uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dlg));
    app_save_to_uri (app, uri);
    g_free (uri);
  }
  gtk_widget_destroy ((GtkWidget *) dlg);
}

void
quit_item_activate_cb (GtkMenuItem * item, App * app)
{
  gtk_main_quit ();
}

void
delete_activate_cb (GtkAction * item, App * app)
{
  /* get a gslist of selected track elements */
  GList *objects = NULL;

  objects = app_get_selected_objects (app);
  app_delete_objects (app, objects);
}

void
add_effect_activate_cb (GtkAction * item, App * app)
{
  gtk_widget_show_all (app->add_effect_dlg);
}

void
add_file_activate_cb (GtkAction * item, App * app)
{
  GtkFileChooserDialog *dlg;

  GST_DEBUG ("add file signal handler");

  dlg = (GtkFileChooserDialog *) gtk_file_chooser_dialog_new ("Add File...",
      GTK_WINDOW (app->main_window),
      GTK_FILE_CHOOSER_ACTION_OPEN,
      GTK_STOCK_CANCEL,
      GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);

  g_object_set (G_OBJECT (dlg), "select-multiple", TRUE, NULL);

  if (gtk_dialog_run ((GtkDialog *) dlg) == GTK_RESPONSE_OK) {
    GSList *uris;
    GSList *cur;
    uris = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (dlg));
    for (cur = uris; cur; cur = cur->next)
      app_add_file (app, cur->data);
    g_slist_free (uris);
  }
  gtk_widget_destroy ((GtkWidget *) dlg);
}

void
add_text_activate_cb (GtkAction * item, App * app)
{
  app_add_title (app);
}

void
add_test_activate_cb (GtkAction * item, App * app)
{
  app_add_test (app);
}

void
add_transition_activate_cb (GtkAction * item, App * app)
{
  app_add_transition (app);
}

void
play_activate_cb (GtkAction * item, App * app)
{
  app_toggle_playpause (app);
}

void
stop_activate_cb (GtkAction * item, App * app)
{
  app_stop_playback (app);
}

void
move_up_activate_cb (GtkAction * item, App * app)
{
  app_move_selected_up (app);
}

void
move_down_activate_cb (GtkAction * item, App * app)
{
  app_move_selected_down (app);
}

void
audio_track_activate_cb (GtkToggleAction * item, App * app)
{
  if (gtk_toggle_action_get_active (item)) {
    app_add_audio_track (app);
  } else {
    app_remove_audio_track (app);
  }
}

void
video_track_activate_cb (GtkToggleAction * item, App * app)
{
  if (gtk_toggle_action_get_active (item)) {
    app_add_video_track (app);
  } else {
    app_remove_video_track (app);
  }
}

void
app_selection_changed_cb (GtkTreeSelection * selection, App * app)
{
  app_update_selection (app);

  update_delete_sensitivity (app);
  update_effect_sensitivity (app);
  update_add_transition_sensitivity (app);
  update_move_up_down_sensitivity (app);

  gtk_widget_set_visible (app->properties, app->n_selected > 0);

  gtk_widget_set_visible (app->filesource_properties,
      app->selected_type == GES_TYPE_URI_CLIP);

  gtk_widget_set_visible (app->text_properties,
      app->selected_type == GES_TYPE_TITLE_CLIP);

  gtk_widget_set_visible (app->generic_duration,
      app->selected_type != G_TYPE_NONE &&
      app->selected_type != G_TYPE_INVALID);

  gtk_widget_set_visible (app->background_properties,
      app->selected_type == GES_TYPE_TEST_CLIP);
}

gboolean
duration_scale_change_value_cb (GtkRange * range, GtkScrollType unused,
    gdouble value, App * app)
{
  GList *i;

  for (i = app->selected_objects; i; i = i->next) {
    guint64 duration, maxduration;
    maxduration = GES_TIMELINE_ELEMENT_MAX_DURATION (i->data);
    duration = (value < maxduration ? (value > 0 ? value : 0) : maxduration);
    g_object_set (G_OBJECT (i->data), "duration", (guint64) duration, NULL);
  }
  return TRUE;
}

gboolean
in_point_scale_change_value_cb (GtkRange * range, GtkScrollType unused,
    gdouble value, App * app)
{
  GList *i;

  for (i = app->selected_objects; i; i = i->next) {
    guint64 in_point, maxduration;
    maxduration = GES_TIMELINE_ELEMENT_MAX_DURATION (i->data) -
        GES_TIMELINE_ELEMENT_DURATION (i->data);
    in_point = (value < maxduration ? (value > 0 ? value : 0) : maxduration);
    g_object_set (G_OBJECT (i->data), "in-point", (guint64) in_point, NULL);
  }
  return TRUE;
}

void
halign_changed_cb (GtkComboBox * widget, App * app)
{
  GList *tmp;
  int active;

  if (app->ignore_input)
    return;

  active = gtk_combo_box_get_active (app->halign);

  for (tmp = app->selected_objects; tmp; tmp = tmp->next) {
    g_object_set (G_OBJECT (tmp->data), "halignment", active, NULL);
  }
}

void
valign_changed_cb (GtkComboBox * widget, App * app)
{
  GList *tmp;
  int active;

  if (app->ignore_input)
    return;

  active = gtk_combo_box_get_active (app->valign);

  for (tmp = app->selected_objects; tmp; tmp = tmp->next) {
    g_object_set (G_OBJECT (tmp->data), "valignment", active, NULL);
  }
}

void
background_type_changed_cb (GtkComboBox * widget, App * app)
{
  GList *tmp;
  gint p;

  if (app->ignore_input)
    return;

  p = gtk_combo_box_get_active (widget);

  for (tmp = app->selected_objects; tmp; tmp = tmp->next) {
    g_object_set (G_OBJECT (tmp->data), "vpattern", (gint) p, NULL);
  }
}

void
frequency_value_changed_cb (GtkSpinButton * widget, App * app)
{
  GList *tmp;
  gdouble value;

  if (app->ignore_input)
    return;

  value = gtk_spin_button_get_value (widget);

  for (tmp = app->selected_objects; tmp; tmp = tmp->next) {
    g_object_set (G_OBJECT (tmp->data), "freq", (gdouble) value, NULL);
  }
}

gboolean
volume_change_value_cb (GtkRange * widget, GtkScrollType unused, gdouble
    value, App * app)
{
  GList *tmp;

  value = value >= 0 ? (value <= 2.0 ? value : 2.0) : 0;

  for (tmp = app->selected_objects; tmp; tmp = tmp->next) {
    g_object_set (G_OBJECT (tmp->data), "volume", (gdouble) value, NULL);
  }
  return TRUE;
}

/* main *********************************************************************/

int
main (int argc, char *argv[])
{
  App *app;

  /* intialize GStreamer and GES */
  gst_init (&argc, &argv);
  ges_init ();

  /* initialize UI */
  gtk_init (&argc, &argv);

  if ((app = app_new ())) {
    gtk_main ();
  }

  return 0;
}