18 new shapewipe transitions from rafa, rework savefile/confirm for nested edl edits
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / channelinfo.C
1 #ifdef HAVE_DVB
2
3 #include "asset.h"
4 #include "batch.h"
5 #include "bctimer.h"
6 #include "channelinfo.h"
7 #include "channel.h"
8 #include "channeldb.h"
9 #include "condition.h"
10 #include "cstrdup.h"
11 #include "devicedvbinput.h"
12 #include "edl.h"
13 #include "edlsession.h"
14 #include "keys.h"
15 #include "mainerror.h"
16 #include "mainmenu.h"
17 #include "mainsession.h"
18 #include "mwindow.h"
19 #include "mwindowgui.h"
20 #include "mutex.h"
21 #include "bcmenuitem.h"
22 #include "record.h"
23 #include "recordconfig.h"
24 #include "theme.h"
25 #include "videodevice.h"
26 #include "videoconfig.h"
27 #include "videodevice.h"
28 #include "libzmpeg3.h"
29
30 #include <ctype.h>
31
32 static inline int min(int a,int b) { return a<b ? a : b; }
33
34 static inline int max(int a,int b) { return a>b ? a : b; }
35
36
37 ChanSearch::ChanSearch(ChannelInfo *iwindow)
38  : Thread(1, 0, 0)
39 {
40         this->iwindow = iwindow;
41         window_lock = new Mutex("ChanSearch::window_lock");
42         gui = 0;
43 }
44
45 ChanSearch::~ChanSearch()
46 {
47         stop();
48         delete gui;
49         delete window_lock;
50 }
51
52 void ChanSearch::start()
53 {
54         window_lock->lock("ChanSearch::start1");
55         if( Thread::running() ) {
56                 gui->lock_window("ChanSearch::start2");
57                 gui->raise_window();
58                 gui->unlock_window();
59         }
60         else {
61                 delete gui;
62                 gui = new ChanSearchGUI(this);
63                 Thread::start();
64         }
65         window_lock->unlock();
66 }
67
68 void ChanSearch::stop()
69 {
70         if( Thread::running() ) {
71                 window_lock->lock("ChanSearch::stop");
72                 if( gui ) gui->set_done(1);
73                 window_lock->unlock();
74                 Thread::cancel();
75         }
76         Thread::join();
77 }
78
79 void ChanSearch::run()
80 {
81         gui->lock_window("ChanSearch::run");
82         gui->create_objects();
83         gui->unlock_window();
84         gui->run_window();
85         window_lock->lock("ChanSearch::stop");
86         delete gui;  gui = 0;
87         window_lock->unlock();
88 }
89
90
91 ChanSearchTitleText::ChanSearchTitleText(ChanSearchGUI *gui, int x, int y)
92  : BC_CheckBox(x, y, &gui->title_text_enable, _("titles"))
93 {
94         this->gui = gui;
95 }
96
97 ChanSearchTitleText::~ChanSearchTitleText()
98 {
99 }
100
101 int ChanSearchTitleText::handle_event()
102 {
103         gui->title_text_enable = get_value();
104         if( !gui->title_text_enable ) gui->info_text->update(1);
105         return 1;
106 }
107
108
109 ChanSearchInfoText::ChanSearchInfoText(ChanSearchGUI *gui, int x, int y)
110  : BC_CheckBox(x, y, &gui->info_text_enable, _("info"))
111 {
112         this->gui = gui;
113 }
114
115 ChanSearchInfoText::~ChanSearchInfoText()
116 {
117 }
118
119 int ChanSearchInfoText::handle_event()
120 {
121         gui->info_text_enable = get_value();
122         if( !gui->info_text_enable ) gui->title_text->update(1);
123         return 1;
124 }
125
126
127 ChanSearchMatchCase::ChanSearchMatchCase(ChanSearchGUI *gui, int x, int y)
128  : BC_CheckBox(x, y, &gui->match_case_enable, _("match case"))
129 {
130         this->gui = gui;
131 }
132
133 ChanSearchMatchCase::~ChanSearchMatchCase()
134 {
135 }
136
137 int ChanSearchMatchCase::handle_event()
138 {
139         gui->match_case_enable = get_value();
140         return 1;
141 }
142
143
144 ChanSearchText::ChanSearchText(ChanSearchGUI *gui, int x, int y, int w)
145  : BC_TextBox(x, y, w, 1, "")
146 {
147         this->gui = gui;
148 }
149
150 ChanSearchText::~ChanSearchText()
151 {
152 }
153
154
155 int ChanSearchText::handle_event()
156 {
157         return 1;
158 }
159
160 int ChanSearchText::keypress_event()
161 {
162         switch(get_keypress())
163         {
164         case RETURN:
165                 gui->search();
166                 return 1;
167         }
168
169         return BC_TextBox::keypress_event();
170 }
171
172
173 ChanSearchStart::ChanSearchStart(ChanSearchGUI *gui, int x, int y)
174  : BC_GenericButton(x, y, _("Search"))
175 {
176         this->gui = gui;
177 }
178
179 ChanSearchStart::~ChanSearchStart()
180 {
181 }
182
183 int ChanSearchStart::handle_event()
184 {
185         gui->search();
186         return 1;
187 }
188
189
190 ChanSearchCancel::ChanSearchCancel(ChanSearchGUI *gui, int x, int y)
191  : BC_CancelButton(x, y)
192 {
193         this->gui = gui;
194 }
195
196 ChanSearchCancel::~ChanSearchCancel()
197 {
198 }
199
200 int ChanSearchCancel::handle_event()
201 {
202         gui->set_done(1);
203         return 1;
204 }
205
206
207 ChanSearchList::ChanSearchList(ChanSearchGUI *gui, int x, int y, int w, int h)
208  : BC_ListBox(x, y, w, h, LISTBOX_TEXT, &gui->search_items[0],
209                 &gui->search_column_titles[0], &gui->search_column_widths[0],
210                 lengthof(gui->search_items))
211 {
212         this->gui = gui;
213         set_sort_column(gui->sort_column);
214         set_sort_order(gui->sort_order);
215         set_allow_drag_column(1);
216 }
217
218 ChanSearchList::~ChanSearchList()
219 {
220 }
221
222 int ChanSearchList::handle_event()
223 {
224         if( get_double_click() ) {
225                 ChannelPanel *panel = gui->panel;
226                 panel->lock_window("ChanSearchList::handle_event");
227                 ChannelEvent *item = gui->highlighted_event;
228                 if( item ) {
229                         item->text_color(-1);
230                         item->draw_face();
231                 }
232                 int i = get_highlighted_item();
233                 item = gui->search_results.get(i);
234                 int x = item->x0-panel->x0 - panel->frame_w/2 + item->get_w()/2;
235                 if( x < 0 ) x = 0;
236                 int w = panel->x1 - panel->x0;
237                 if( x > w ) x = w;
238                 panel->time_line_scroll->update_value(x);
239                 panel->set_x_scroll(x);
240                 item->text_color(YELLOW);
241                 item->draw_face();
242                 gui->highlighted_event = item;
243                 panel->unlock_window();
244         }
245         return 1;
246 }
247
248 int ChanSearchList::sort_order_event()
249 {
250         gui->sort_events(get_sort_column(), get_sort_order());
251         return 1;
252 }
253
254 int ChanSearchList::move_column_event()
255 {
256         gui->move_column(get_from_column(), get_to_column());
257         return 1;
258 }
259
260 void ChanSearchGUI::create_objects()
261 {
262         int xs10 = xS(10), xs20 = xS(20);
263         int ys5 = yS(5), ys10 = yS(10);
264         lock_window("ChanSearchGUI::create_objects");
265         int pady = BC_TextBox::calculate_h(this, MEDIUMFONT, 0, 1) + ys5;
266         int padx = BC_Title::calculate_w(this, (char*)"X", MEDIUMFONT);
267         int x = padx/2, y = pady/4;
268         BC_Title *title = new BC_Title(text_x, text_y, _("Text:"), MEDIUMFONT, YELLOW);
269         add_subwindow(title);  x += title->get_w();
270         text_x = x;  text_y = y;
271         search_text = new ChanSearchText(this, x, y, get_w()-x-xs10);
272         add_subwindow(search_text);
273         x = padx;  y += pady + ys5;
274         title_text = new ChanSearchTitleText(this, x, y);
275         add_subwindow(title_text);  x += title_text->get_w() + padx;
276         info_text = new ChanSearchInfoText(this, x, y);
277         add_subwindow(info_text);  x += title_text->get_w() + padx;
278         match_case = new ChanSearchMatchCase(this, x, y);
279         add_subwindow(match_case); x += match_case->get_w() + padx;
280         results_x = x + xs20;  results_y = y + ys5;
281         results = new BC_Title(results_x, results_y, " ", MEDIUMFONT, YELLOW);
282         add_subwindow(results);
283         x = padx;  y += pady + ys5;
284         search_x = xs10;
285         search_y = get_h() - BC_GenericButton::calculate_h() - ys10;
286         search_start = new ChanSearchStart(this, search_x, search_y);
287         add_subwindow(search_start);
288         cancel_w = ChanSearchCancel::calculate_w();
289         cancel_h = ChanSearchCancel::calculate_h();
290         cancel_x = get_w() - cancel_w - xs10;
291         cancel_y = get_h() - cancel_h - ys10;
292         cancel = new ChanSearchCancel(this, cancel_x, cancel_y);
293         add_subwindow(cancel);
294         list_x = x;  list_y = y;
295         int list_w = get_w()-xs10 - list_x;
296         int list_h = min(search_y, cancel_y)-ys10 - list_y;
297         search_list = new ChanSearchList(this, list_x, list_y, list_w, list_h);
298         add_subwindow(search_list);
299         search_list->show_window();
300         int clktip_x = list_x;
301         int clktip_y = list_y + list_h + ys5;
302         click_tip = new BC_Title(clktip_x, clktip_y, _("dbl clk row to find title"));
303         add_subwindow(click_tip);
304
305         set_icon(iwindow->mwindow->theme->get_image("record_icon"));
306         search_text->activate();
307         unlock_window();
308 }
309
310 #define CSW_W xS(500)
311 #define CSW_H yS(300)
312
313 ChanSearchGUI::ChanSearchGUI(ChanSearch *cswindow)
314  : BC_Window(_(PROGRAM_NAME ": ChanSearch"),
315         cswindow->iwindow->gui->get_abs_cursor_x(1) - CSW_W/2,
316         cswindow->iwindow->gui->get_abs_cursor_y(1) - CSW_H/2,
317         CSW_W, CSW_H, xS(400), yS(300))
318 {
319         this->cswindow = cswindow;
320         this->iwindow = cswindow->iwindow;
321         this->panel = cswindow->iwindow->gui->panel;
322
323         search_text = 0;
324         title_text = 0;
325         info_text = 0;
326         match_case = 0;
327         search_start = 0;
328         search_list = 0;
329         cancel = 0;
330         results = 0;
331
332         search_x = search_y = text_x = text_y = 0;
333         cancel_x = cancel_y = cancel_w = cancel_h = 0;
334         list_x = list_y = list_w = list_h = 0;
335         results_x = results_y = 0;
336         sort_column = sort_order = 0;
337
338         title_text_enable = 1;
339         info_text_enable = 0;
340         match_case_enable = 0;
341         highlighted_event = 0;
342
343         search_columns[0] = 0;
344         search_columns[1] = 1;
345         search_columns[2] = 2;
346         search_column_titles[0] = _("Source");
347         search_column_titles[1] = C_("Title");
348         search_column_titles[2] = _("Start time");
349         search_column_widths[0] = xS(120);
350         search_column_widths[2] = xS(120);
351         search_column_widths[1] = get_w()-search_column_widths[0]-search_column_widths[2]-xS(32);
352 }
353
354 ChanSearchGUI::~ChanSearchGUI()
355 {
356
357         ChannelEvent *item = highlighted_event;
358         if( item ) {
359                 panel->lock_window("ChanSearchGUI::~ChanSearchGUI");
360                 item->text_color(-1);
361                 item->draw_face();
362                 panel->unlock_window();
363         }
364         for( int i=0; i<lengthof(search_items); ++i )
365                 search_items[i].remove_all_objects();
366 }
367
368 int ChanSearchGUI::resize_event(int w, int h)
369 {
370         int xs5 = xS(5), xs10 = xS(10);
371         int ys10 = yS(10);
372         search_text->reposition_window(text_x, text_y, w-text_x-10);
373         int cancel_x = w - BC_CancelButton::calculate_w() - xs10;
374         int cancel_y = h - BC_CancelButton::calculate_h() - ys10;
375         cancel->reposition_window(cancel_x, cancel_y);
376         search_x = xs10;
377         search_y = h - BC_GenericButton::calculate_h() - ys10;
378         search_start->reposition_window(search_x, search_y);
379         int list_w = w-xs10 - list_x;
380         int list_h = min(search_y, cancel_y)-ys10 - list_y;
381         search_column_widths[1] = w-search_column_widths[0]-search_column_widths[2]-xS(32);
382         search_list->reposition_window(list_x, list_y, list_w, list_h);
383         int clktip_x = list_x;
384         int clktip_y = list_y + list_h + xs5;
385         click_tip->reposition_window(clktip_x, clktip_y);
386         update();
387         return 1;
388 }
389
390 int ChanSearchGUI::close_event()
391 {
392         set_done(1);
393         return 1;
394 }
395
396 int ChanSearchGUI::search(const char *sp)
397 {
398         char const *tp = search_text->get_text();
399         int n = strlen(tp);
400         for( const char *cp=sp; *cp!=0; ++cp ) {
401                 if( match_case_enable ? !strncmp(tp, cp, n) :
402                         !strncasecmp(tp, cp, n) ) return 1;
403         }
404         return 0;
405 }
406
407 void ChanSearchGUI::search()
408 {
409         search_results.remove_all();
410
411         int n = panel->channel_event_items.size();
412         for( int i=0; i<n; ++i ) {
413                 ChannelEvent *item = panel->channel_event_items.get(i);
414                 if( (title_text_enable && search(item->get_text())) ||
415                     (info_text_enable  && search(item->get_tooltip()) ) )
416                         search_results.append(item);
417         }
418
419         update();
420 }
421
422 void ChanSearchGUI::update()
423 {
424
425         for( int i=0; i<lengthof(search_items); ++i )
426                 search_items[i].remove_all_objects();
427
428         char text[BCTEXTLEN];
429         int n = search_results.size();
430         for( int i=0; i<n; ++i ) {
431                 ChannelEvent *item = search_results.get(i);
432                 for( int k=0; k<lengthof(search_columns); ++k ) {
433                         const char *cp = 0;
434                         switch( search_columns[k] ) {
435                         case 0:  cp = item->channel->title;  break;
436                         case 1:  cp = item->get_text();      break;
437                         case 2: {
438                                 struct tm stm;  localtime_r(&item->start_time,&stm);
439                                 double seconds = stm.tm_hour*3600 + stm.tm_min*60 + stm.tm_sec;
440                                 Units::totext(text, seconds, TIME_HMS3);
441                                 cp = text;                   break; }
442                         }
443                         if( cp ) search_items[k].append(new BC_ListBoxItem(cp, LTYELLOW));
444                 }
445         }
446
447         search_list->update(search_items, &search_column_titles[0],
448                         &search_column_widths[0], lengthof(search_items));
449         sprintf(text, _("%d found"), n);
450         results->update(text);
451 }
452
453
454 #define CmprFn(nm,key) int ChanSearchGUI:: \
455 cmpr_##nm(const void *a, const void *b) { \
456   ChannelEvent *&ap = *(ChannelEvent **)a; \
457   ChannelEvent *&bp = *(ChannelEvent **)b; \
458   int n = key; if( !n ) n = ap->no-bp->no; \
459   return n; \
460 }
461
462 CmprFn(Text_dn, strcasecmp(ap->get_text(), bp->get_text()))
463 CmprFn(text_dn, strcmp(ap->get_text(), bp->get_text()))
464 CmprFn(Text_up, strcasecmp(bp->get_text(), ap->get_text()))
465 CmprFn(text_up, strcmp(bp->get_text(), ap->get_text()))
466 CmprFn(time_dn, ap->start_time - bp->start_time)
467 CmprFn(time_up, bp->start_time - ap->start_time)
468 CmprFn(Title_dn, strcasecmp(ap->channel->title, bp->channel->title))
469 CmprFn(title_dn, strcmp(ap->channel->title, bp->channel->title))
470 CmprFn(Title_up, strcasecmp(bp->channel->title, ap->channel->title))
471 CmprFn(title_up, strcmp(bp->channel->title, ap->channel->title))
472
473
474 void ChanSearchGUI::sort_events(int column, int order)
475 {
476         sort_column = column;  sort_order = order;
477         int n = search_results.size();
478         if( !n ) return;
479         ChannelEvent **events = &search_results.values[0];
480         for( int i=0; i<n; ++i ) events[i]->no = i;
481
482         int(*cmpr)(const void *, const void *) = 0;
483         switch( search_columns[sort_column] ) {
484         case 0: cmpr = match_case_enable ?
485                         (sort_order ? cmpr_Title_up : cmpr_Title_dn) :
486                         (sort_order ? cmpr_title_up : cmpr_title_dn) ;
487                 break;
488         case 1: cmpr = match_case_enable ?
489                         (sort_order ? cmpr_Text_up : cmpr_Text_dn) :
490                         (sort_order ? cmpr_text_up : cmpr_text_dn) ;
491                 break;
492         case 2: cmpr = sort_order ? cmpr_time_up : cmpr_time_dn;
493                 break;
494         }
495
496         if( cmpr ) {
497                 ChannelEvent **events = &search_results.values[0];
498                 qsort(events, n, sizeof(*events), cmpr);
499         }
500         update();
501 }
502
503 void ChanSearchGUI::move_column(int src, int dst)
504 {
505         if( src == dst ) return;
506         int src_column = search_columns[src];
507         const char *src_column_title = search_column_titles[src];
508         int src_column_width = search_column_widths[src];
509         if( src < dst ) {
510                 for( int i=src; i<dst; ++i ) {
511                         search_columns[i] = search_columns[i+1];
512                         search_column_titles[i] = search_column_titles[i+1];
513                         search_column_widths[i] = search_column_widths[i+1];
514                 }
515         }
516         else {
517                 for( int i=src; i>dst; --i  ) {
518                         search_columns[i] = search_columns[i-1];
519                         search_column_titles[i] = search_column_titles[i-1];
520                         search_column_widths[i] = search_column_widths[i-1];
521                 }
522         }
523         search_columns[dst] = src_column;
524         search_column_titles[dst] = src_column_title;
525         search_column_widths[dst] = src_column_width;
526         update();
527 }
528
529
530 ChannelProgress::ChannelProgress(ChannelInfoGUI *gui,
531                 int x, int y, int w, int h, int len)
532  : Thread(1, 0, 0),
533    BC_SubWindow(x, y, w, h)
534 {
535         this->gui = gui;
536         eta = 0;
537         bar = 0;
538         done = 0;
539         length = len;
540         value = 0.;
541
542         eta_timer = new Timer;
543 }
544
545 ChannelProgress::~ChannelProgress()
546 {
547         stop();
548         delete eta_timer;
549 }
550
551 void ChannelProgress::create_objects()
552 {
553         int w = BC_Title::calculate_w(gui, (char*)"XXXXX", MEDIUMFONT);
554         eta = new BC_Title(0, 0, "+0:00", MEDIUMFONT, -1, 0, w);
555         add_subwindow(eta);
556         int x = eta->get_w();
557         bar = new BC_ProgressBar(x, 0, get_w()-x, length);
558         add_subwindow(bar);
559         start();
560 }
561
562 void ChannelProgress::start()
563 {
564         done = 0;
565         eta_timer->update();
566         Thread::start();
567 }
568
569 void ChannelProgress::stop()
570 {
571         if( Thread::running() ) {
572                 done = 1;
573                 Thread::cancel();
574         }
575         Thread::join();
576 }
577
578 void ChannelProgress::run()
579 {
580         gui->init_wait();
581         while( !done ) {
582                 if( update() ) break;
583                 enable_cancel();
584                 Timer::delay(500);
585                 disable_cancel();
586         }
587 }
588
589 int ChannelProgress::update()
590 {
591         double elapsed = (double)eta_timer->get_scaled_difference(1000) / 1000.;
592         double estimate = value < 1 ? 0 : (length/value-1)*elapsed;
593         char text[BCTEXTLEN], *cp = &text[0];
594         Units::totext(cp, estimate, TIME_MS1);
595         gui->lock_window("ChannelProgress::update");
596         eta->update(cp);
597         bar->update(value);
598         gui->unlock_window();
599         return value < length ? 0 : 1;
600 }
601
602
603
604 TimeLineItem::TimeLineItem(ChannelPanel *panel, int x, int y, char *text)
605  : BC_Title(x-panel->x_scroll, 0, text, MEDIUMFONT,
606                 x==panel->x_now ? GREEN : YELLOW)
607 {
608         this->panel = panel;
609         x0 = x;  y0 = y;
610 }
611
612 TimeLineItem::~TimeLineItem()
613 {
614 }
615
616
617 TimeLine::TimeLine(ChannelPanel *panel)
618  : BC_SubWindow(panel->frame_x, 0, panel->frame_w, panel->path_h)
619 {
620         this->panel = panel;
621 }
622
623 TimeLine::~TimeLine()
624 {
625 }
626
627 int TimeLine::resize_event(int w, int h)
628 {
629         clear_box(0,0, w,h);
630         return 1;
631 }
632
633
634 ChannelDataItem::ChannelDataItem(ChannelPanel *panel, int x, int y, int w,
635         int color, const char *text)
636  : BC_Title(x, y-panel->y_scroll, text, MEDIUMFONT, color)
637 {
638         this->panel = panel;
639         x0 = x;  y0 = y;
640         tip_info = 0;
641         set_force_tooltip(1);
642 }
643
644 ChannelDataItem::~ChannelDataItem()
645 {
646         delete [] tip_info;
647 }
648
649 int ChannelDataItem::repeat_event(int64_t duration)
650 {
651         if( tip_info && cursor_above() &&
652                 duration == get_resources()->tooltip_delay ) {
653                 show_tooltip();
654                 return 1;
655         }
656         return 0;
657 }
658
659 void ChannelDataItem::set_tooltip(const char *tip)
660 {
661         BC_Title::set_tooltip(tip_info = tip ? cstrdup(tip) : 0);
662 }
663
664
665 ChannelData::ChannelData(ChannelPanel *panel, int x, int y, int w, int h)
666  : BC_SubWindow(x, y, w, h, LTBLACK)
667 {
668         this->panel = panel;
669 }
670
671 ChannelData::~ChannelData()
672 {
673 }
674
675 int ChannelData::resize_event(int w, int h)
676 {
677         clear_box(0,0, w,h);
678         return 1;
679 }
680
681
682 ChannelScroll::ChannelScroll(ChannelPanel *panel, int x, int y, int h)
683  : BC_ScrollBar(x, y, SCROLL_VERT, h, 0, 0, 0)
684 {
685         this->panel = panel;
686 }
687
688 ChannelScroll::~ChannelScroll()
689 {
690 }
691
692 int ChannelScroll::handle_event()
693 {
694         panel->set_y_scroll(get_value());
695         return 1;
696 }
697
698
699 TimeLineScroll::TimeLineScroll(ChannelPanel *panel, int x, int y, int w)
700  : BC_ScrollBar(x, y, SCROLL_HORIZ, w, 0, 0, 0)
701 {
702         this->panel = panel;
703 }
704
705 TimeLineScroll::~TimeLineScroll()
706 {
707 }
708
709 int TimeLineScroll::handle_event()
710 {
711         panel->set_x_scroll(get_value());
712         return 1;
713 }
714
715
716 ChannelEventLine::ChannelEventLine(ChannelPanel *panel,
717         int x, int y, int w, int h, int color)
718  : BC_SubWindow(0, y, w, h, color)
719 {
720         this->panel = panel;
721         x0 = x;  y0 = y;
722 }
723
724 ChannelEventLine::~ChannelEventLine()
725 {
726 }
727
728 void ChannelEventLine::resize(int w, int h)
729 {
730         resize_window(w, h);
731 }
732
733
734 ChannelEvent::ChannelEvent(ChannelEventLine *channel_line, Channel *channel,
735         time_t start_time, time_t end_time, int x, int y, int w, const char *text)
736  : BC_GenericButton( x-channel_line->panel->x_scroll,
737                 y-channel_line->panel->y_scroll, w, text)
738 {
739         this->channel_line = channel_line;
740         this->channel = channel;
741         this->start_time = start_time;
742         this->end_time = end_time;
743         x0 = x;  y0 = y;  no = 0;
744         tip_info = 0;
745         set_force_tooltip(1);
746 }
747
748 ChannelEvent::~ChannelEvent()
749 {
750         delete [] tip_info;
751 }
752
753 int ChannelEvent::handle_event()
754 {
755         ChannelInfoGUI *gui = channel_line->panel->gui;
756         Batch *batch = gui->batch_bay->get_editing_batch();
757         char *path = batch->asset->path;
758         int len = sizeof(batch->asset->path)-1;
759         const char *text = get_text();
760         const char *dir = gui->channel_dir->get_directory();
761         int i = 0;
762         while( i<len && *dir ) path[i++] = *dir++;
763         if( i > 0 && dir[i-1] != '/' ) path[i++] = '/';
764         while( i<len && *text ) {
765                 int ch = *text++;
766                 if( !isalnum(ch) ) ch = '_';
767                 path[i++] = ch;
768         }
769         if( i < len ) path[i++] = '.';
770         if( i < len ) path[i++] = 't';
771         if( i < len ) path[i++] = 's';
772         path[i] = 0;
773         int early = (int)gui->early_time->get_time();
774         int late = (int)gui->late_time->get_time();
775         time_t st = start_time + early;
776         struct tm stm;  localtime_r(&st,&stm);
777         batch->record_mode = RECORD_TIMED;
778         batch->enabled = 1;
779         batch->start_day = stm.tm_wday;
780         batch->start_time = stm.tm_hour*3600 + stm.tm_min*60 + stm.tm_sec;
781         batch->duration = end_time - start_time - early + late;
782         batch->channel = channel;
783         gui->batch_bay->update_batches();
784         gui->update_channel_tools();
785         return 1;
786 }
787
788 void ChannelEvent::set_tooltip(const char *tip)
789 {
790         BC_GenericButton::set_tooltip(tip_info = tip ? cstrdup(tip) : 0);
791 }
792
793
794 ChannelFrame::ChannelFrame(ChannelPanel *panel)
795  : BC_SubWindow(panel->frame_x, panel->frame_y,
796                 panel->frame_w, panel->frame_h, BLACK)
797 {
798         this->panel = panel;
799 }
800
801 ChannelFrame::~ChannelFrame()
802 {
803 }
804
805
806 void ChannelPanel::create_objects()
807 {
808         iwd = BC_ScrollBar::get_span(SCROLL_VERT);
809         iht = BC_ScrollBar::get_span(SCROLL_HORIZ);
810
811         frame_x = path_w;  frame_y = path_h;
812         frame_w = iwindow_w-frame_x - iwd;
813         frame_h = iwindow_h-frame_y - iht;
814
815         time_line = new TimeLine(this);
816         add_subwindow(time_line);
817         time_line_scroll = new TimeLineScroll(this, frame_x, iwindow_h-iht, frame_w);
818         add_subwindow(time_line_scroll);
819         channel_data = new ChannelData(this, 0, frame_y, path_w, frame_h);
820         add_subwindow(channel_data);
821         channel_frame = new ChannelFrame(this);
822         add_subwindow(channel_frame);
823         channel_scroll = new ChannelScroll(this, iwindow_w-iwd, frame_y, frame_h);
824         add_subwindow(channel_scroll);
825 }
826
827 ChannelPanel::ChannelPanel(ChannelInfoGUI *gui,
828         int x, int y, int w, int h)
829  : BC_SubWindow(x, y, w, h)
830 {
831         this->gui = gui;
832         gettimeofday(&tv, &tz);
833         st_org = ((tv.tv_sec+1800-1) / 1800) * 1800 - 1800;
834         x_now = (tv.tv_sec-st_org)/1800 * hhr_w;
835         path_w = gui->path_w;  path_h = gui->path_h;
836         x0 = y0 = x1 = y1 = t0 = t1 = 0;
837         x_scroll = y_scroll = 0;
838         x_moved = y_moved = 0;
839         iwindow_w = w;  iwindow_h = h;
840         hhr_w = BC_GenericButton::calculate_w(gui, (char*)"XXXXXXXX") + 5;
841 }
842
843 ChannelPanel::~ChannelPanel()
844 {
845 }
846
847 ChannelEventLine *ChannelPanel::NewChannelLine(int y, int h, int color)
848 {
849         ChannelEventLine *channel_line =
850                  new ChannelEventLine(this, 0, y, frame_w, h, color);
851         channel_frame->add_subwindow(channel_line);
852         channel_line_items.append(channel_line);
853         return channel_line;
854 }
855
856 void ChannelPanel::resize(int w, int h)
857 {
858         frame_w = (iwindow_w=w) - frame_x - iwd;
859         frame_h = (iwindow_h=h) - frame_y - iht;
860         time_line->resize_window(frame_w, path_h);
861         time_line_scroll->reposition_window(frame_x, frame_y+frame_h, frame_w);
862         time_line_scroll->update_length(x1-x0, x_scroll-x0, frame_w,0);
863         time_line_scroll->update_value(-x0);
864         channel_data->resize_window(path_w, frame_h);
865         channel_frame->resize_window(frame_w, frame_h);
866         int nitems = channel_line_items.size();
867         for( int i=0; i<nitems; ++i )
868                 channel_line_items.values[i]->resize_window(frame_w, path_h);
869         channel_scroll->reposition_window(frame_x+frame_w, frame_y, frame_h);
870         channel_scroll->update_length(y1-y0, y_scroll-y0, frame_h,0);
871         flush();
872 }
873
874 int ChannelPanel::button_press_event()
875 {
876         if(get_buttonpress() == 3 && cursor_inside()) {
877                 gui->lock_window("ChannelPanel::button_press_event");
878                 gui->channel_search->start();
879                 gui->unlock_window();
880                 return 1;
881         }
882         return 0;
883 }
884
885 void ChannelPanel::bounding_box(int ix0, int iy0, int ix1, int iy1)
886 {
887         int x_changed = 0, y_changed = 0;
888         if( ix0 < x0 ) { x0 = ix0;  x_changed = 1; }
889         if( iy0 < y0 ) { y0 = iy0;  y_changed = 1; }
890         if( ix1 > x1 ) { x1 = ix1;  x_changed = 1; }
891         if( iy1 > y1 ) { y1 = iy1;  y_changed = 1; }
892         if( x_changed ) {
893                 time_line_update(x0, x1);
894                 time_line_scroll->update_length(x1-x0, x_scroll-x0, frame_w, 0);
895                 if( !x_moved ) time_line_scroll->update_value(-x0);
896         }
897         if( y_changed )
898                 channel_scroll->update_length(y1-y0, y_scroll-y0, frame_h, 0);
899         if( x_changed || y_changed ) flush();
900 }
901
902 void ChannelPanel::set_x_scroll(int v)
903 {
904         x_moved = 1;
905         x_scroll = v + x0;
906         reposition();
907 }
908
909 void ChannelPanel::set_y_scroll(int v)
910 {
911         y_moved = 1;
912         y_scroll = v + y0;
913         reposition();
914 }
915
916 void ChannelPanel::reposition()
917 {
918         int n, i;
919         n = time_line_items.size();
920         for( i=0; i<n; ++i ) {
921                 TimeLineItem *item = time_line_items[i];
922                 if( item->get_x() == item->x0-x_scroll &&
923                     item->get_y() == item->y0-y_scroll) continue;
924                 item->reposition_window(item->x0-x_scroll, item->y0);
925         }
926         n = channel_data_items.size();
927         for( i=0; i<n; ++i ) {
928                 ChannelDataItem *item = channel_data_items[i];
929                 if( item->get_x() == item->x0-x_scroll &&
930                     item->get_y() == item->y0-y_scroll) continue;
931                 item->reposition_window(item->x0, item->y0-y_scroll);
932         }
933         n = channel_line_items.size();
934         for( i=0; i<n; ++i ) {
935                 ChannelEventLine *item = channel_line_items[i];
936                 if( item->get_x() == item->x0-x_scroll &&
937                     item->get_y() == item->y0-y_scroll) continue;
938                 item->reposition_window(item->x0, item->y0-y_scroll);
939         }
940         n = channel_event_items.size();
941         for( i=0; i<n; ++i ) {
942                 ChannelEvent *item = channel_event_items[i];
943                 if( item->get_x() == item->x0-x_scroll &&
944                     item->get_y() == item->y0-y_scroll) continue;
945                 item->reposition_window(item->x0-x_scroll, item->y0);
946         }
947 }
948
949 void ChannelPanel::get_xtime(int x, char *cp)
950 {
951         time_t xt = st_org + (x/hhr_w) * 1800;
952         if( !strcmp(tzname[0],"UTC") ) xt -= tz.tz_minuteswest*60;
953         struct tm xtm;  localtime_r(&xt,&xtm);
954         cp += sprintf(cp,"%02d:%02d",xtm.tm_hour, xtm.tm_min);
955         if( !xtm.tm_hour && !xtm.tm_min ) {
956                 sprintf(cp,"(%3.3s) ",&_("sunmontuewedthufrisat")[xtm.tm_wday*3]);
957         }
958 }
959
960 void ChannelPanel::time_line_update(int ix0, int ix1)
961 {
962         int x;  char text[BCTEXTLEN];
963         for( x=t0; x>=ix0; x-=hhr_w ) {
964                 get_xtime(x, text);  t0 = x;
965                 TimeLineItem *time_line_item = new TimeLineItem(this, t0, 0, text);
966                 time_line->add_subwindow(time_line_item);
967                 time_line_items.insert(time_line_item,0);
968         }
969         for( x=t1; x<ix1; x+=hhr_w ) {
970                 get_xtime(x, text);  t1 = x;
971                 TimeLineItem *time_line_item = new TimeLineItem(this, t1, 0, text);
972                 time_line->add_subwindow(time_line_item);
973                 time_line_items.append(time_line_item);
974         }
975 }
976
977 ChannelInfoCron::
978 ChannelInfoCron(ChannelInfoGUI *gui, int x, int y, int *value)
979  : BC_CheckBox(x, y, value, gui->cron_caption)
980 {
981         this->gui = gui;
982         set_tooltip(_("activate batch record when ok pressed"));
983 }
984
985 ChannelInfoCron::
986 ~ChannelInfoCron()
987 {
988 }
989
990 int ChannelInfoCron::
991 handle_event()
992 {
993         gui->iwindow->cron_enable = get_value();
994         return 1;
995 }
996
997
998 ChannelInfoPowerOff::ChannelInfoPowerOff(ChannelInfoGUI *gui, int x, int y, int *value)
999  : BC_CheckBox(x, y , value, gui->power_caption)
1000 {
1001         this->gui = gui;
1002         set_tooltip(_("poweroff system when batch record done"));
1003 }
1004
1005 ChannelInfoPowerOff::~ChannelInfoPowerOff()
1006 {
1007 }
1008
1009 int ChannelInfoPowerOff::handle_event()
1010 {
1011         gui->iwindow->poweroff_enable = get_value();
1012         return 1;
1013 }
1014
1015
1016 ChannelInfoFind::ChannelInfoFind(ChannelInfoGUI *gui, int x, int y)
1017  : BC_GenericButton(x, y, _("Find"))
1018 {
1019         this->gui = gui;
1020         set_tooltip(_("search event titles/info"));
1021 }
1022
1023 ChannelInfoFind::~ChannelInfoFind()
1024 {
1025 }
1026
1027 int ChannelInfoFind::handle_event()
1028 {
1029         gui->lock_window("ChannelInfoFind::handle_event");
1030         gui->channel_search->start();
1031         gui->unlock_window();
1032         return 1;
1033 }
1034
1035 void ChannelThread::start()
1036 {
1037         if( !Thread::running() ) {
1038                 done = 0;
1039                 Thread::start();
1040         }
1041 }
1042
1043 void ChannelThread::stop()
1044 {
1045         if( Thread::running() ) {
1046                 done = 1;
1047                 Thread::cancel();
1048         }
1049         Thread::join();
1050 }
1051
1052 ChannelThread::ChannelThread(ChannelInfoGUI *gui)
1053  : Thread(1, 0, 0)
1054 {
1055         this->gui = gui;
1056         this->iwindow = gui->iwindow;
1057         this->panel = gui->panel;
1058
1059         fd = 0;
1060         done = 0;
1061 }
1062
1063 ChannelThread::~ChannelThread()
1064 {
1065         stop();
1066 }
1067
1068 int ChannelThread::load_ident(int n, int y, char *ident)
1069 {
1070         ChannelDataItem *data_item = new ChannelDataItem(panel, 0, y,
1071                 panel->path_w, !n ? YELLOW : LTYELLOW, ident);
1072         panel->channel_data->add_subwindow(data_item);
1073         panel->channel_data_items.append(data_item);
1074         if( !n ) {
1075                 char info[BCTEXTLEN];
1076                 int i = mpeg3_dvb_get_chan_info(fd, n, -1, 0, info, sizeof(info)-1);
1077                 while( --i >= 0 && (info[i]=='\n' || info[i]==' ') ) info[i] = 0;
1078                 if( info[0] ) data_item->set_tooltip(info);
1079         }
1080         return 0;
1081 }
1082
1083 int ChannelThread::load_info(Channel *channel, ChannelEventLine *channel_line)
1084 {
1085         int n = channel->element;
1086         int ord = 0, i = 0;
1087
1088         while( ord < 0x80 ) {
1089                 char info[65536], *cp = &info[0];
1090                 int len = mpeg3_dvb_get_chan_info(fd,n,ord,i++,cp,sizeof(info)-1);
1091                 if( len < 0 ) { i = 0;  ++ord;  continue; }
1092                 char *bp = cp;  cp += len;
1093                 struct tm stm;  memset(&stm,0,sizeof(stm));
1094                 struct tm etm;  memset(&etm,0,sizeof(etm));
1095                 int k, l = sscanf(bp,"%d:%d:%d-%d:%d:%d %n",
1096                         &stm.tm_hour, &stm.tm_min, &stm.tm_sec,
1097                         &etm.tm_hour, &etm.tm_min, &etm.tm_sec,
1098                         &k);
1099                 if( l != 6 ) {
1100                         printf(_("bad scan time: %s\n"),info);
1101                         continue;
1102                 }
1103                 char *title = (bp += k);
1104                 while( bp<cp && *bp!='\n' ) ++bp;
1105                 char *dp = bp;  *bp++ = 0;
1106                 if( !*title ) {
1107                         printf(_("bad title: %s\n"),info);
1108                         continue;
1109                 }
1110                 char stm_wday[4];  memset(&stm_wday,0,sizeof(stm_wday));
1111                 l = sscanf(bp, "(%3s) %d/%d/%d", &stm_wday[0],
1112                         &stm.tm_year, &stm.tm_mon, &stm.tm_mday);
1113                 if( l != 4 ) {
1114                         printf(_("bad scan date: %s\n"),info);
1115                         continue;
1116                 }
1117                 while( bp<cp && *bp!='\n' ) ++bp;
1118                 *bp = 0;
1119                 etm.tm_year = (stm.tm_year -= 1900);
1120                 etm.tm_mon = --stm.tm_mon;
1121                 etm.tm_mday = stm.tm_mday;
1122                 stm.tm_isdst = etm.tm_isdst = -1;
1123                 time_t st = mktime(&stm);
1124                 time_t et = mktime(&etm);
1125                 if( et < st ) et += 24*3600;
1126                 if( et < st ) {
1127                         printf(_("end before start: %s\n"),info);
1128                         continue;
1129                 }
1130                 if( panel->st_org - et > 24*3600*2) {
1131                         printf(_("end time early: %s\n"),info);
1132                         continue;
1133                 }
1134                 if( st - panel->st_org > 24*3600*12 ) {
1135                         printf(_("start time late: %s\n"),info);
1136                         continue;
1137                 }
1138                 time_t st_min = (st - panel->st_org)/60;
1139                 time_t et_min = (et - panel->st_org)/60;
1140                 int dt = et_min - st_min;
1141                 if( dt <= 0 ) {
1142                         printf(_("zero duration: %s\n"),info);
1143                         continue;
1144                 }
1145
1146                 int w = (dt * panel->hhr_w) / 30;
1147                 int x = (st_min * panel->hhr_w) / 30;
1148                 int y = channel_line->y0;
1149                 panel->bounding_box(x, y, x+w, y+panel->path_h);
1150                 ChannelEvent *channel_event =
1151                         new ChannelEvent(channel_line, channel,
1152                                         st, et, x, 0, w, title);
1153                 channel_line->add_subwindow(channel_event);
1154                 panel->channel_event_items.append(channel_event);
1155
1156                 *dp = '\n';  *bp++ = '\n';
1157                 for( char *lp=bp; bp<cp; ++bp ) {
1158                         if( *bp == '\n' || ((bp-lp)>=60 && *bp==' ') )
1159                                 *(lp=bp) = '\n';
1160                 }
1161                 *cp = 0;
1162                 for( bp=&info[0]; --cp>=bp && (*cp=='\n' || *cp==' '); *cp=0 );
1163                 if( info[0] ) channel_event->set_tooltip(info);
1164         }
1165         return 0;
1166 }
1167
1168 void ChannelThread::run()
1169 {
1170         gui->init_wait();
1171         Channel *channel = 0;
1172         int y = 0;
1173         int nchannels = total_channels();
1174         enable_cancel();
1175
1176         for(int ch=0; !done && ch<nchannels; gui->update_progress(++ch) ) {
1177                 Channel *chan = get_channel(ch);
1178                 if( !chan ) continue;
1179                 disable_cancel();
1180                 iwindow->vdevice_lock->lock("ChannelThread::run");
1181                 DeviceDVBInput *dvb_input = iwindow->dvb_input;
1182                 if( !channel || chan->entry != channel->entry ) {
1183                         int retry = 3;
1184                         while( dvb_input->set_channel(chan) && --retry >= 0 );
1185                         if( retry >= 0 ) {
1186                                 channel = chan;
1187                                 if( y ) {
1188                                         gui->lock_window();
1189                                         y += panel->separator(y);
1190                                         gui->unlock_window();
1191                                 }
1192                         }
1193                         else
1194                                 channel = 0;
1195                 }
1196                 else if( chan->element == channel->element )
1197                         chan = 0;
1198                 else
1199                         channel = chan;
1200                 if( !done && chan && channel && (fd=dvb_input->get_src()) ) {
1201                         gui->lock_window();
1202                         load_ident(chan->element, y, chan->title);
1203                         ChannelEventLine *channel_line = panel->NewChannelLine(y);
1204                         load_info(chan, channel_line);
1205                         channel_line->show_window();
1206                         gui->unlock_window();
1207                         dvb_input->put_src();
1208                         y += panel->path_h;
1209                 }
1210                 iwindow->vdevice_lock->unlock();
1211                 enable_cancel();
1212         }
1213
1214         gui->lock_window("ChannelProgress::run");
1215         gui->channel_status->hide_window();
1216         gui->unlock_window();
1217
1218         disable_cancel();
1219         iwindow->close_vdevice();
1220 }
1221
1222
1223 ChannelInfoOK::ChannelInfoOK(ChannelInfoGUI *gui, int x, int y)
1224  : BC_OKButton(x, y)
1225 {
1226         this->gui = gui;
1227         set_tooltip(_("end channel info, start record"));
1228 }
1229
1230 ChannelInfoOK::~ChannelInfoOK()
1231 {
1232 }
1233
1234 int ChannelInfoOK::button_press_event()
1235 {
1236         if(get_buttonpress() == 1 && is_event_win() && cursor_inside()) {
1237                 gui->stop(0);
1238                 return 1;
1239         }
1240         return 0;
1241 }
1242
1243 int ChannelInfoOK::keypress_event()
1244 {
1245         return 0;
1246 }
1247
1248
1249 ChannelInfoCancel::ChannelInfoCancel(ChannelInfoGUI *gui, int x, int y)
1250  : BC_CancelButton(x, y)
1251 {
1252         this->gui = gui;
1253 }
1254
1255 ChannelInfoCancel::~ChannelInfoCancel()
1256 {
1257 }
1258
1259 int ChannelInfoCancel::button_press_event()
1260 {
1261         if(get_buttonpress() == 1 && is_event_win() && cursor_inside()) {
1262                 gui->stop(1);
1263                 return 1;
1264         }
1265         return 0;
1266 }
1267
1268
1269 ChannelInfoGUIBatches::ChannelInfoGUIBatches(ChannelInfoGUI *gui,
1270         int x, int y, int w, int h)
1271  : RecordBatchesGUI(gui->iwindow->record_batches, x, y, w, h)
1272 {
1273         this->gui = gui;
1274 }
1275
1276 ChannelInfoGUIBatches::~ChannelInfoGUIBatches()
1277 {
1278 }
1279
1280 int ChannelInfoGUIBatches::selection_changed()
1281 {
1282         return RecordBatchesGUI::selection_changed();
1283 }
1284
1285 int ChannelInfoGUIBatches::handle_event()
1286 {
1287         if( get_double_click() )
1288                 gui->update_channel_tools();
1289         return 1;
1290 }
1291
1292
1293 void ChannelInfoGUI::create_objects()
1294 {
1295         int xs10 = xS(10);
1296         int ys5 = yS(5), ys10 = yS(10);
1297         lock_window("ChannelInfoGUI::create_objects");
1298         panel = new ChannelPanel(this,0,0,panel_w,panel_h);
1299         add_subwindow(panel);
1300         panel->create_objects();
1301         int items = iwindow->channeldb->size();
1302         if( items < 1 ) items = 1;
1303         progress = new ChannelProgress(this, 0, 0, path_w, path_h, items);
1304         add_subwindow(progress);
1305         progress->create_objects();
1306         ok = new ChannelInfoOK(this, ok_x, ok_y);
1307         add_subwindow(ok);
1308         channel_cron = new ChannelInfoCron(this, cron_x, cron_y, &iwindow->cron_enable);
1309         add_subwindow(channel_cron);
1310         channel_poweroff = new ChannelInfoPowerOff(this,
1311                  power_x, power_y, &iwindow->poweroff_enable);
1312         add_subwindow(channel_poweroff);
1313         channel_find = new ChannelInfoFind(this, find_x, find_y);
1314         add_subwindow(channel_find);
1315         cancel = new ChannelInfoCancel(this, cancel_x, cancel_y);
1316         add_subwindow(cancel);
1317         batch_bay = new ChannelInfoGUIBatches(this,bay_x, bay_y, bay_w, bay_h);
1318         add_subwindow(batch_bay);
1319         iwindow->record_batches.gui = batch_bay;
1320         batch_bay->set_current_batch(-1);
1321         batch_bay->update_batches(-1);
1322
1323         pad = BC_TextBox::calculate_h(this, MEDIUMFONT, 0, 1) + ys5;
1324         x0 = bay_x+bay_w + xs10;
1325         y0 = bay_y+ys10;
1326         int ww = 0;
1327         int x = x0;
1328         int y = y0;
1329
1330         add_subwindow(directory_title = new BC_Title(x, y, _("Directory:")));
1331         ww = max(directory_title->get_w(), ww);   y += pad;
1332         add_subwindow(path_title = new BC_Title(x, y, _("Path:")));
1333         ww = max(path_title->get_w(), ww);       y += pad;
1334         add_subwindow(start_title = new BC_Title(x, y, _("Start:")));
1335         ww = max(start_title->get_w(), ww);      y += pad;
1336         add_subwindow(duration_title = new BC_Title(x, y, _("Duration:")));
1337         ww = max(duration_title->get_w(), ww);   y += pad;
1338         add_subwindow(source_title = new BC_Title(x, y, _("Source:")));
1339         ww = max(source_title->get_w(), ww);     y += pad;
1340         title_w = ww;
1341
1342         int x1 = x0 + title_w + pad;
1343         x = x1;  y = y0;  ww = 0;
1344
1345         char *dir = iwindow->record_batches.get_default_directory();
1346         add_subwindow(channel_dir = new ChannelDir(this, dir, x, y));
1347         ww = max(channel_dir->get_w(), ww);     y += pad;
1348         add_subwindow(channel_path = new ChannelPath(this, x, y));
1349         ww = max(channel_path->get_w(), ww);   y += pad;
1350         channel_start = new ChannelStart(this, x, y);
1351         channel_start->create_objects();
1352         ww = max(channel_start->get_w(), ww);   y += pad;
1353         int w = BC_Title::calculate_w(this, (char*)"+00:00:00+", MEDIUMFONT);
1354         channel_duration = new ChannelDuration(this, x, y, w);
1355         channel_duration->create_objects();
1356         int x2 = x + channel_duration->get_w();
1357         double *early_margin = iwindow->record_batches.get_early_margin();
1358         early_time = new ChannelEarlyTime(this, x2, y, early_margin);
1359         early_time->create_objects();
1360         x2 += early_time->get_w();
1361         double *late_margin = iwindow->record_batches.get_late_margin();
1362         late_time = new ChannelLateTime(this, x2, y, late_margin);
1363         late_time->create_objects();
1364         x2 += late_time->get_w();
1365         ww = max(x2-x1, ww);                    y += pad;
1366         channel_source = new ChannelSource(this, x, y);
1367         channel_source->create_objects();
1368         ww = max(channel_source->get_w(), ww);  y += pad;
1369         data_w = x1-x0 + ww;
1370
1371         x = x0 + pad;
1372         add_subwindow(channel_clear_batch = new ChannelClearBatch(this, x, y));
1373         x += channel_clear_batch->get_w() + xs10;
1374         add_subwindow(channel_new_batch = new ChannelNewBatch(this, x, y));
1375         x += channel_new_batch->get_w() + xs10;
1376         add_subwindow(channel_delete_batch = new ChannelDeleteBatch(this, x, y));
1377         x += channel_delete_batch->get_w();
1378         ww = max(x-x0, ww);
1379         data_w = max(ww, data_w);
1380
1381         channel_status = new ChannelStatus(this, x0+data_w, y0);
1382         add_subwindow(channel_status);
1383         channel_status->create_objects();
1384         status_w = channel_status->get_w();
1385
1386         channel_search = new ChanSearch(iwindow);
1387         show_window();
1388         unlock_window();
1389 }
1390
1391 ChannelInfoGUI::ChannelInfoGUI(ChannelInfo *iwindow,
1392         int x, int y, int w, int h)
1393  : BC_Window(_(PROGRAM_NAME ": Channel Info"), x, y,
1394         w, h, xS(600), yS(400), 1, 0, 0 , -1,
1395         iwindow->mwindow->get_cwindow_display())
1396 {
1397         int xs10 = xS(10), xs20 = xS(20);
1398         int ys5 = yS(5), ys10 = yS(10);
1399         this->iwindow = iwindow;
1400         panel = 0;
1401         batch_bay =0;
1402         channel_dir = 0;
1403         channel_path = 0;
1404         channel_start = 0;
1405         channel_duration = 0;
1406         channel_source = 0;
1407         channel_clear_batch = 0;
1408         channel_new_batch = 0;
1409         channel_delete_batch = 0;
1410         early_time = late_time = 0;
1411         directory_title = 0;
1412         path_title = 0;
1413         start_title = 0;
1414         duration_title = 0;
1415         source_title = 0;
1416         cron_caption = _("Start Cron");
1417         power_caption = _("Poweroff");
1418         ok = 0;
1419         cancel = 0;
1420
1421         path_w = 16*BC_Title::calculate_w(this, (char*)"X", MEDIUMFONT);
1422         path_h = BC_TextBox::calculate_h(this, MEDIUMFONT, 0, 1);
1423         x0 = y0 = title_w = data_w = status_w = pad = 0;
1424         ok_w = BC_OKButton::calculate_w();
1425         ok_h = BC_OKButton::calculate_h();
1426         ok_x = xs10;
1427         ok_y = h - ok_h - ys10;
1428         BC_CheckBox::calculate_extents(this, &cron_w, &cron_h, cron_caption);
1429         cron_x = ok_x;
1430         cron_y = ok_y - cron_h - ys10;
1431         BC_CheckBox::calculate_extents(this, &power_w, &power_h, power_caption);
1432         power_x = cron_x;
1433         power_y = cron_y - power_h - ys5;
1434         find_h = BC_GenericButton::calculate_h();
1435         find_x = power_x;
1436         find_y = power_y - find_h - ys10;
1437         cancel_w = BC_CancelButton::calculate_w();
1438         cancel_h = BC_CancelButton::calculate_h();
1439         cancel_x = w - cancel_w - xs10,
1440         cancel_y = h - cancel_h - ys10;
1441         max_bay_w = xS(700);
1442         bay_h = yS(150);
1443         bay_x = ok_w + xs20;
1444         int x1 = cron_x+cron_w + xs10;
1445         if( x1 > bay_x ) bay_x = x1;
1446         x1 = power_x+power_w + xs10;
1447         if( x1 > bay_x ) bay_x = x1;
1448         bay_y = h - bay_h;
1449         // data_w,status_w zero, updated in create_objects
1450         bay_w = (w-bay_x) - (data_w+xs10) - (max(cancel_w, status_w)+xs20);
1451         if( bay_w > max_bay_w ) bay_w = max_bay_w;
1452         panel_w = w;
1453         panel_h = h - bay_h;
1454 }
1455
1456 ChannelInfoGUI::~ChannelInfoGUI()
1457 {
1458         progress->stop();
1459         channel_search->stop();
1460         flush();  sync();
1461         delete channel_status;
1462         delete channel_search;
1463         delete channel_start;
1464         delete channel_duration;
1465         delete early_time;
1466         delete late_time;
1467         delete channel_source;
1468 }
1469
1470 void ChannelInfoGUI::stop(int v)
1471 {
1472         if( !iwindow->gui_done ) {
1473                 iwindow->gui_done = 1;
1474                 set_done(v);
1475         }
1476 }
1477
1478 int ChannelInfoGUI::translation_event()
1479 {
1480         iwindow->mwindow->session->cswindow_x = get_x();
1481         iwindow->mwindow->session->cswindow_y = get_y();
1482         return 0;
1483 }
1484
1485 int ChannelInfoGUI::resize_event(int w, int h)
1486 {
1487         int xs10 = xS(10), xs20 = xS(20);
1488         int ys5 = yS(5), ys10 = yS(10);
1489         iwindow->mwindow->session->cswindow_w = w;
1490         iwindow->mwindow->session->cswindow_h = h;
1491         panel_w = w;
1492         panel_h = h - bay_h;
1493         panel->resize(panel_w,panel_h);
1494         panel->reposition_window(0,0,panel_w,panel_h);
1495         ok_x = xs10;
1496         ok_y = h - ok_h - ys10;
1497         ok->reposition_window(ok_x, ok_y);
1498         cron_x = ok_x;
1499         cron_y = ok_y - cron_h - ys10;
1500         channel_cron->reposition_window(cron_x, cron_y);
1501         power_x = cron_x;
1502         power_y = cron_y - power_h - ys5;
1503         channel_poweroff->reposition_window(power_x, power_y);
1504         find_x = power_x;
1505         find_y = power_y - find_h - ys10;
1506         channel_find->reposition_window(find_x, find_y);
1507         cancel_x = w - cancel_w - xs10,
1508         cancel_y = h - cancel_h - ys10;
1509         cancel->reposition_window(cancel_x, cancel_y);
1510         bay_x = ok_w + xs20;
1511         int x1 = cron_x+cron_w + xs10;
1512         if( x1 > bay_x ) bay_x = x1;
1513         x1 = power_x+power_w + xs10;
1514         if( x1 > bay_x ) bay_x = x1;
1515         bay_y = h - bay_h;
1516         bay_w = (w-bay_x) - (data_w+xs10) - (max(cancel_w, status_w)+xs20);
1517         if( bay_w > max_bay_w ) bay_w = max_bay_w;
1518         batch_bay->reposition_window(bay_x, bay_y, bay_w, bay_h);
1519
1520         int x0 = bay_x+bay_w + xs10;
1521         int y0 = bay_y+ys10;
1522         int x = x0;
1523         int y = y0;
1524
1525         directory_title->reposition_window(x, y);  y += pad;
1526         path_title->reposition_window(x, y);       y += pad;
1527         start_title->reposition_window(x, y);      y += pad;
1528         duration_title->reposition_window(x, y);   y += pad;
1529         source_title->reposition_window(x, y);
1530
1531         x = x0 + title_w + pad;
1532         y = y0;
1533
1534         channel_dir->reposition_window(x, y);      y += pad;
1535         channel_path->reposition_window(x, y);     y += pad;
1536         channel_start->reposition_window(x, y);    y += pad;
1537         channel_duration->reposition_window(x, y);
1538         int x2 = x + channel_duration->get_w();
1539         early_time->reposition_window(x2, y);
1540         x2 += early_time->get_w();
1541         late_time->reposition_window(x2, y);       y += pad;
1542         channel_source->reposition_window(x, y);   y += pad;
1543
1544         x = x0 + pad;
1545         channel_clear_batch->reposition_window(x, y);
1546         x += channel_clear_batch->get_w() + xs10;
1547         channel_new_batch->reposition_window(x, y);
1548         x += channel_new_batch->get_w() + xs10;
1549         channel_delete_batch->reposition_window(x, y);
1550
1551         y = y0;
1552         x = x0 + data_w;
1553         channel_status->reposition_window(x, y);
1554         return 1;
1555 }
1556
1557 int ChannelInfoGUI::close_event()
1558 {
1559         stop(1);
1560         return 1;
1561 }
1562
1563 void ChannelInfoGUI::update_channel_tools()
1564 {
1565         Batch *batch = batch_bay->get_editing_batch();
1566         channel_path->update(batch->asset->path);
1567         channel_start->update(&batch->start_day, &batch->start_time);
1568         channel_duration->update(0, &batch->duration);
1569         channel_source->update(batch->get_source_text());
1570         flush();
1571 }
1572
1573 void ChannelInfoGUI::incr_event(int start_time_incr, int duration_incr)
1574 {
1575         Batch *batch = batch_bay->get_editing_batch();
1576         batch->start_time += start_time_incr;
1577         batch->duration += duration_incr;
1578         batch_bay->update_batches();
1579         update_channel_tools();
1580 }
1581
1582
1583 ChannelInfo::ChannelInfo(MWindow *mwindow)
1584  : Thread(1, 0, 0),
1585    record_batches(mwindow)
1586 {
1587         this->mwindow = mwindow;
1588         this->record = mwindow->gui->record;
1589         scan_lock = new Condition(0,"ChannelInfo::scan_lock");
1590         window_lock = new Mutex("ChannelInfo::window_lock");
1591         vdevice_lock = new Mutex("ChannelInfo::vdevice_lock");
1592         progress_lock = new Mutex("ChannelInfo::progress_lock");
1593
1594         vdevice = 0;
1595         dvb_input = 0;
1596         gui = 0;
1597         thread = 0;
1598         channeldb = 0;
1599         cron_enable = 1;
1600         poweroff_enable = 0;
1601         item = 0;
1602         done = 1;
1603         gui_done = 1;
1604
1605         start();
1606 }
1607
1608 ChannelInfo::~ChannelInfo()
1609 {
1610         stop();
1611         delete thread;
1612         delete gui;  gui = 0;
1613         delete vdevice;
1614         record_batches.clear();
1615         delete channeldb;
1616         delete scan_lock;
1617         delete window_lock;
1618         delete vdevice_lock;
1619         delete progress_lock;
1620 }
1621
1622 void ChannelInfo::run_scan()
1623 {
1624         window_lock->lock("ChannelInfo::run_scan");
1625         if( gui ) {
1626                 gui->lock_window("ChannelInfo::run_scan");
1627                 gui->raise_window();
1628                 gui->unlock_window();
1629         }
1630         else
1631                 scan_lock->unlock();
1632         window_lock->unlock();
1633 }
1634
1635 void ChannelInfo::toggle_scan()
1636 {
1637         window_lock->lock("ChannelInfo::toggle_scan");
1638         if( gui )
1639                 gui->stop(1);
1640         else
1641                 scan_lock->unlock();
1642         window_lock->unlock();
1643 }
1644
1645 void ChannelInfo::start()
1646 {
1647         if( !Thread::running() ) {
1648                 done = 0;
1649                 Thread::start();
1650         }
1651 }
1652
1653 void ChannelInfo::stop()
1654 {
1655         if( Thread::running() ) {
1656                 done = 1;
1657                 scan_lock->unlock();
1658                 window_lock->lock("ChannelInfo::stop");
1659                 if( gui ) gui->stop(1);
1660                 window_lock->unlock();
1661                 Thread::cancel();
1662         }
1663         Thread::join();
1664 }
1665
1666 void ChannelInfo::run()
1667 {
1668         int root_w = mwindow->gui->get_root_w(1);
1669         int root_h = mwindow->gui->get_root_h(1);
1670
1671         while( !done ) {
1672                 scan_lock->reset();
1673                 scan_lock->lock();
1674                 if( done ) break;
1675                 if( record->Thread::running() ) {
1676                         char string[BCTEXTLEN];
1677                         sprintf(string,_("Recording in progress\n"));
1678                         MainError::show_error(string);
1679                         continue;
1680                 }
1681                 EDLSession *session = mwindow->edl->session;
1682                 VideoInConfig *vconfig_in = session->vconfig_in;
1683                 if( vconfig_in->driver != CAPTURE_DVB ) {
1684                         char string[BCTEXTLEN];
1685                         sprintf(string,_("capture driver not dvb\n"));
1686                         MainError::show_error(string);
1687                         continue;
1688                 }
1689                 int x = mwindow->session->cswindow_x;
1690                 int y = mwindow->session->cswindow_y;
1691                 int w = mwindow->session->cswindow_w;
1692                 int h = mwindow->session->cswindow_h;
1693                 if( w < xS(600) ) w = xS(600);
1694                 if( h < yS(400) ) h = yS(400);
1695                 int scr_x = mwindow->gui->get_screen_x(1, -1);
1696                 int scr_w = mwindow->gui->get_screen_w(1, -1);
1697                 if( x < scr_x ) x = scr_x;
1698                 if( x > scr_x+scr_w ) x = scr_x+scr_w;
1699                 if( x+w > root_w ) x = root_w - w;
1700                 if( x < 0 ) { x = 0;  w = scr_w; }
1701                 if( y+h > root_h ) y = root_h - h;
1702                 if( y < 0 ) { y = 0;  h = root_h; }
1703                 if( y+h > root_h ) h = root_h-y;
1704                 mwindow->session->cswindow_x = x;
1705                 mwindow->session->cswindow_y = y;
1706                 mwindow->session->cswindow_w = w;
1707                 mwindow->session->cswindow_h = h;
1708                 cron_enable = 1;
1709                 poweroff_enable = 0;
1710                 vdevice = new VideoDevice(mwindow);
1711                 int result = vdevice->open_input(vconfig_in, 0, 0, 1., vconfig_in->in_framerate);
1712                 dvb_input = result ? 0 : (DeviceDVBInput *)vdevice->mpeg_device();
1713                 if( dvb_input ) {
1714                         channeldb = new ChannelDB;
1715                         VideoDevice::load_channeldb(channeldb, vconfig_in);
1716                         record_batches.load_defaults(channeldb);
1717                         item = 0;
1718                         window_lock->lock("ChannelInfo::run 0");
1719                         gui_done = 0;
1720                         gui = new ChannelInfoGUI(this, x, y, w, h);
1721                         gui->lock_window("ChannelInfo::gui_create_objects");
1722                         gui->create_objects();
1723                         gui->set_icon(mwindow->theme->get_image("record_icon"));
1724                         gui->reposition_window(x, y);
1725                         gui->resize_event(w, h);
1726                         dvb_input->set_signal_status(gui->channel_status);
1727                         gui->unlock_window();
1728                         thread = new ChannelThread(gui);
1729                         thread->start();
1730                         window_lock->unlock();
1731                         result = gui->run_window();
1732                         delete thread;  thread = 0;
1733                         close_vdevice();
1734                         gui->lock_window();
1735                         gui->unlock_window();
1736                         window_lock->lock("ChannelInfo::run 1");
1737                         delete gui;  gui = 0;
1738                         window_lock->unlock();
1739                         record_batches.save_defaults(channeldb);
1740                         if( !result ) {
1741                                 record_batches.save_default_channel(channeldb);
1742                                 record->start();
1743                                 record->init_lock->lock();
1744                                 if( cron_enable ) {
1745                                         record->set_power_off(poweroff_enable);
1746                                         record->start_cron_thread();
1747                                 }
1748                         }
1749                         record_batches.clear();
1750                         delete channeldb;  channeldb = 0;
1751                 }
1752                 else {
1753                         close_vdevice();
1754                         char string[BCTEXTLEN];
1755                         sprintf(string,_("cannot open dvb video device\n"));
1756                         MainError::show_error(string);
1757                 }
1758         }
1759 }
1760
1761 void ChannelInfo::close_vdevice()
1762 {
1763         vdevice_lock->lock("ChannelInfo::close_vdevice");
1764         progress_lock->lock("ChannelInfo::close_vdevice");
1765         if( vdevice ) {
1766                 vdevice->close_all();
1767                 delete vdevice;  vdevice = 0;
1768         }
1769         progress_lock->unlock();
1770         vdevice_lock->unlock();
1771 }
1772
1773 Batch *ChannelInfo::new_batch()
1774 {
1775         Batch *batch = new Batch(gui->iwindow->mwindow, 0);
1776         batch->create_objects();
1777         batch->calculate_news();
1778         gui->iwindow->record_batches.append(batch);
1779         return batch;
1780 }
1781
1782 void ChannelInfo::delete_batch()
1783 {
1784 // Abort if one batch left
1785         int edit_batch = editing_batch();
1786         int total_batches = record_batches.total();
1787         if( total_batches > 1 && edit_batch < total_batches ) {
1788                 Batch *batch = record_batches[edit_batch];
1789                 record_batches.remove(batch);  delete batch;
1790         }
1791 }
1792
1793 ChannelScan::ChannelScan(MWindow *mwindow)
1794  : BC_MenuItem(_("Scan..."), _("Ctrl-Alt-s"), 's')
1795 {
1796         set_ctrl();
1797         set_alt();
1798         this->mwindow = mwindow;
1799 }
1800
1801 ChannelScan::~ChannelScan()
1802 {
1803 }
1804
1805 int ChannelScan::handle_event()
1806 {
1807         mwindow->gui->channel_info->run_scan();
1808         return 1;
1809 }
1810
1811
1812 ChannelDir::ChannelDir(ChannelInfoGUI *gui, const char *dir, int x, int y)
1813  : RecordBatchesGUI::Dir(gui->iwindow->record_batches, dir, x, y)
1814 {
1815         this->gui = gui;
1816 }
1817
1818
1819 ChannelPath::ChannelPath(ChannelInfoGUI *gui, int x, int y)
1820  : RecordBatchesGUI::Path(gui->iwindow->record_batches, x, y)
1821 {
1822         this->gui = gui;
1823 }
1824
1825
1826 ChannelStart::ChannelStart(ChannelInfoGUI *gui, int x, int y)
1827  : RecordBatchesGUI::StartTime(gui, gui->iwindow->record_batches, x, y)
1828 {
1829         this->gui = gui;
1830 }
1831
1832
1833 ChannelDuration::ChannelDuration(ChannelInfoGUI *gui, int x, int y, int w)
1834  : RecordBatchesGUI::Duration(gui, gui->iwindow->record_batches, x, y, w)
1835 {
1836         this->gui = gui;
1837 }
1838
1839
1840 ChannelEarlyTime::ChannelEarlyTime(ChannelInfoGUI *gui, int x, int y,
1841         double *output_time)
1842  : TimeEntryTumbler(gui, x, y, 0, output_time, 15, TIME_MS2, xS(75))
1843 {
1844         this->gui = gui;
1845 }
1846
1847 int ChannelEarlyTime::handle_up_event()
1848 {
1849         gui->incr_event(incr,-incr);
1850         return TimeEntryTumbler::handle_up_event();
1851 }
1852
1853 int ChannelEarlyTime::handle_down_event()
1854 {
1855         gui->incr_event(-incr,incr);
1856         return TimeEntryTumbler::handle_down_event();
1857 }
1858
1859
1860 ChannelLateTime::ChannelLateTime(ChannelInfoGUI *gui, int x, int y,
1861         double *output_time)
1862  : TimeEntryTumbler(gui, x, y, 0, output_time, 15, TIME_MS2, xS(75))
1863 {
1864         this->gui = gui;
1865 }
1866
1867 int ChannelLateTime::handle_up_event()
1868 {
1869         gui->incr_event(0,incr);
1870         return TimeEntryTumbler::handle_up_event();
1871 }
1872
1873 int ChannelLateTime::handle_down_event()
1874 {
1875         gui->incr_event(0,-incr);
1876         return TimeEntryTumbler::handle_down_event();
1877 }
1878
1879
1880 ChannelSource::ChannelSource(ChannelInfoGUI *gui, int x, int y)
1881  : RecordBatchesGUI::Source(gui, gui->iwindow->record_batches, x, y)
1882 {
1883         this->gui = gui;
1884 }
1885
1886 void ChannelSource::create_objects()
1887 {
1888         RecordBatchesGUI::Source::create_objects();
1889         ChannelDB *channeldb = gui->iwindow->channeldb;
1890         sources.remove_all_objects();
1891         for(int i = 0; i < channeldb->size(); i++) {
1892                 sources.append(new BC_ListBoxItem(channeldb->get(i)->title));
1893         }
1894         update_list(&sources);
1895 }
1896
1897 int ChannelSource::handle_event()
1898 {
1899         int chan_no = get_number();
1900         Channel *channel = gui->iwindow->channeldb->get(chan_no);
1901         if( channel ) {
1902                 Batch *batch = batches.get_editing_batch();
1903                 if( batch ) batch->channel = channel;
1904                 update(channel->title);
1905         }
1906         return RecordBatchesGUI::Source::handle_event();
1907 }
1908
1909
1910 ChannelClearBatch::ChannelClearBatch(ChannelInfoGUI *gui, int x, int y)
1911  : RecordBatchesGUI::ClearBatch(gui->iwindow->record_batches, x, y)
1912 {
1913         this->gui = gui;
1914         set_tooltip(_("Delete all clips."));
1915 }
1916
1917 int ChannelClearBatch::handle_event()
1918 {
1919         gui->iwindow->record_batches.clear();
1920         gui->iwindow->new_batch();
1921         gui->batch_bay->set_current_batch(-1);
1922         gui->batch_bay->set_editing_batch(0);
1923         gui->batch_bay->update_batches(0);
1924         return RecordBatchesGUI::ClearBatch::handle_event();
1925 }
1926
1927
1928 ChannelNewBatch::ChannelNewBatch(ChannelInfoGUI *gui, int x, int y)
1929  : RecordBatchesGUI::NewBatch(gui->iwindow->record_batches, x, y)
1930 {
1931         this->gui = gui;
1932         set_tooltip(_("Create new clip."));
1933 }
1934 int ChannelNewBatch::handle_event()
1935 {
1936         gui->iwindow->new_batch();
1937         return RecordBatchesGUI::NewBatch::handle_event();
1938 }
1939
1940
1941 ChannelDeleteBatch::ChannelDeleteBatch(ChannelInfoGUI *gui, int x, int y)
1942  : RecordBatchesGUI::DeleteBatch(gui->iwindow->record_batches, x, y)
1943 {
1944         this->gui = gui;
1945         set_tooltip(_("Delete clip."));
1946 }
1947
1948 int ChannelDeleteBatch::handle_event()
1949 {
1950         gui->iwindow->delete_batch();
1951         return RecordBatchesGUI::DeleteBatch::handle_event();
1952 }
1953
1954 #endif
1955