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