3 #include "bcmenuitem.h"
5 #include "commercials.h"
10 #include "mwindowgui.h"
17 static inline int min(int a,int b) { return a<b ? a : b; }
18 static inline int max(int a,int b) { return a>b ? a : b; }
20 #define MAX_SEARCH 100
22 DbSearchItem::DbSearchItem(const char *text, int color)
23 : BC_ListBoxItem(text, color)
28 DbSearchItem::~DbSearchItem()
33 DbWindow(MWindow *mwindow)
36 this->mwindow = mwindow;
37 window_lock = new Mutex("DbWindow::window_lock");
38 db_lock = new Mutex("DbWindow::db_lock");
43 DbWindow::MDb::MDb(DbWindow *d)
44 : Garbage("DbWindow::MDb"), dwin(d)
46 if( !d->mwindow->has_commercials() ) return;
67 window_lock->lock("DbWindow::start1");
68 if( Thread::running() ) {
69 gui->lock_window("DbWindow::start2");
75 gui = new DbWindowGUI(this);
78 window_lock->unlock();
85 if( Thread::running() ) {
86 window_lock->lock("DbWindow::stop");
87 if( gui ) gui->set_done(1);
88 window_lock->unlock();
97 gui->lock_window("DbWindow::run");
98 gui->create_objects();
99 gui->search(MAX_SEARCH,"");
100 gui->start_drawing(0);
101 gui->unlock_window();
103 window_lock->lock("DbWindow::stop");
105 window_lock->unlock();
110 DbWindowTitleText(DbWindowGUI *gui, int x, int y)
111 : BC_CheckBox(x, y, &gui->title_text_enable, _("titles"))
116 DbWindowTitleText::~DbWindowTitleText()
120 int DbWindowTitleText::
123 gui->title_text_enable = get_value();
124 if( !gui->title_text_enable ) gui->info_text->update(1);
130 DbWindowInfoText(DbWindowGUI *gui, int x, int y)
131 : BC_CheckBox(x, y, &gui->info_text_enable, _("info"))
136 DbWindowInfoText::~DbWindowInfoText()
140 int DbWindowInfoText::
143 gui->info_text_enable = get_value();
144 if( !gui->info_text_enable ) gui->title_text->update(1);
150 DbWindowMatchCase(DbWindowGUI *gui, int x, int y)
151 : BC_CheckBox(x, y, &gui->match_case_enable, _("match case"))
156 DbWindowMatchCase::~DbWindowMatchCase()
160 int DbWindowMatchCase::
163 gui->match_case_enable = get_value();
169 DbWindowText(DbWindowGUI *gui, int x, int y, int w)
170 : BC_TextBox(x, y, w, 1, "")
175 DbWindowText::~DbWindowText()
189 switch(get_keypress())
192 gui->search(MAX_SEARCH, get_text());
196 return BC_TextBox::keypress_event();
201 DbWindowScan(MWindow *mwindow)
202 : BC_MenuItem(_("Media DB..."), _("Shift-M"), 'M')
205 this->mwindow = mwindow;
208 DbWindowScan::~DbWindowScan()
215 mwindow->commit_commercial();
216 mwindow->gui->db_window->start();
221 DbWindowStart(DbWindowGUI *gui, int x, int y)
222 : BC_GenericButton(x, y, _("Search"))
227 DbWindowStart::~DbWindowStart()
234 gui->search(MAX_SEARCH,"");
238 DbWindowDeleteItems::
239 DbWindowDeleteItems(DbWindowGUI *gui, int x, int y)
240 : BC_GenericButton(x, y, _("Delete"))
245 DbWindowDeleteItems::~DbWindowDeleteItems()
249 int DbWindowDeleteItems::
252 gui->search_list->set_view_popup(0);
259 DbWindowCancel(DbWindowGUI *gui, int x, int y)
260 : BC_CancelButton(x, y)
265 DbWindowCancel::~DbWindowCancel()
278 DbWindowList(DbWindowGUI *gui, int x, int y, int w, int h)
279 : BC_ListBox(x, y, w, h, LISTBOX_TEXT,
280 (ArrayList<BC_ListBoxItem*> *) &gui->search_items[0],
281 &gui->search_column_titles[0], &gui->search_column_widths[0],
282 sizeof_col, 0, 0, LISTBOX_MULTIPLE)
285 set_sort_column(gui->sort_column);
286 set_sort_order(gui->sort_order);
287 set_allow_drag_column(1);
290 DbWindowList::~DbWindowList()
301 set_view_popup(DbWindowVIcon *vicon)
303 gui->vicon_thread->set_view_popup(vicon);
309 switch(get_keypress()) {
314 gui->del_items->handle_event();
326 gui->sort_events(get_sort_column(), get_sort_order());
327 gui->start_drawing();
335 gui->move_column(get_from_column(), get_to_column());
336 gui->start_drawing();
340 DbWindowVIcon::DbWindowVIcon()
341 : VIcon(SWIDTH, SHEIGHT, VICON_RATE)
347 DbWindowVIcon::~DbWindowVIcon()
351 DbWindowVIconThread::
352 DbWindowVIconThread(DbWindowGUI *gui)
353 : VIconThread(gui->search_list, 4*SWIDTH, 4*SHEIGHT)
359 DbWindowVIconThread::
360 ~DbWindowVIconThread()
363 vicons.remove_all_objects();
366 DbWindowVIcon *DbWindowVIconThread::get_vicon(int i, DbSearchItem *item)
368 while( i >= vicons.size() ) {
369 DbWindowVIcon *vicon = new DbWindowVIcon();
370 vicons.append(vicon);
372 DbWindowVIcon *vicon = vicons[i];
373 vicon->lbox = gui->search_list;
379 void DbWindowVIconThread::drawing_started()
381 if( !list_update ) return;
384 gui->search_list->update_images();
388 VFrame *DbWindowVIcon::frame()
390 if( seq_no >= images.size() )
391 load_frames(lbox->gui->dwindow->mdb);
392 return *images[seq_no];
395 int64_t DbWindowVIcon::set_seq_no(int64_t no)
397 if( no >= clip_size ) no = 0;
402 void DbWindowVIcon::load_frames(DbWindow::MDb *mdb)
404 if( !mdb->attach_rd() ) {
410 void DbWindowVIcon::read_frames(DbWindow::MDb *mdb)
412 while( seq_no >= images.size() ) {
413 int no = images.size();
414 if( no >= prefix_size ) no += suffix_offset;
415 if( mdb->get_sequences(clip_id, no) ) continue;
416 if( mdb->timeline_sequence_no() != no ) continue;
417 int frame_id = mdb->timeline_frame_id();
418 if( frame_id < 0 ) continue;
419 int swidth = (SWIDTH+1) & ~1, sheight = (SHEIGHT+1) & ~1;
420 VIFrame *vifrm = new VIFrame(swidth, sheight, BC_YUV420P);
421 VFrame *img = *vifrm;
422 memset(img->get_y(),0x00,swidth * sheight);
423 memset(img->get_u(),0x80,swidth/2 * sheight/2);
424 memset(img->get_v(),0x80,swidth/2 * sheight/2);
425 uint8_t *yp = img->get_y(); int sw = -1, sh = -1;
426 mdb->get_image(frame_id, yp, sw, sh);
427 images.append(vifrm);
431 int DbWindowVIcon::get_vx()
433 return lbox->get_item_x(item);
435 int DbWindowVIcon::get_vy()
437 return lbox->get_item_y(item);
441 update_image(DbWindowGUI *gui, int clip_id)
443 DbWindow::MDb *mdb = gui->dwindow->mdb;
444 if( !mdb->attach_rd() ) {
445 if( !mdb->clip_id(clip_id) ) {
446 this->clip_id = clip_id;
447 this->clip_size = mdb->clip_size();;
448 this->prefix_size = mdb->clip_prefix_size();
449 this->suffix_offset = mdb->clip_frames() - clip_size;
450 double framerate = mdb->clip_framerate();
451 this->frame_rate = framerate > 0 ? framerate : VICON_RATE;
452 gui->vicon_thread->add_vicon(this);
459 int DbWindowList::update_images()
461 DbWindowVIconThread *thread = gui->vicon_thread;
462 thread->t_heap.remove_all();
464 int i = get_first_visible();
466 while( --k >= 0 && gui->search_columns[k] != col_vicon );
467 if( k >= 0 && i >= 0 ) {
468 int sz = gui->search_results.size();
469 int last = get_last_visible();
470 if( last < sz ) sz = last+1;
471 for( int n=0; i<sz; ++i, ++n ) {
472 DbSearchItem *item = gui->search_items[k][i];
473 DbWindowVIcon *vicon = thread->get_vicon(n, item);
474 vicon->update_image(gui, gui->search_results[i]->id);
483 BC_ListBox::update((ArrayList<BC_ListBoxItem*>*)gui->search_items,
484 &gui->search_column_titles[0], &gui->search_column_widths[0],
494 int idx = get_selection_number(0, 0);
495 if( idx >= 0 && get_selection_number(0, 1) < 0 ) {
496 DbSearchItem *item = gui->search_items[0][idx];
497 set_view_popup(item->vicon);
499 gui->start_drawing(0);
507 int pady = BC_TextBox::calculate_h(this, MEDIUMFONT, 0, 1) + 5;
508 int padx = BC_Title::calculate_w(this, (char*)"X", MEDIUMFONT);
509 int x = padx/2, y = pady/4;
510 text_x = x; text_y = y;
511 BC_Title *title = new BC_Title(text_x, text_y, _("Text:"), MEDIUMFONT, YELLOW);
512 add_subwindow(title); x += title->get_w();
513 search_text = new DbWindowText(this, x, y, get_w()-x-10);
514 add_subwindow(search_text);
515 x = padx; y += pady + 5;
516 title_text = new DbWindowTitleText(this, x, y);
517 add_subwindow(title_text); x += title_text->get_w() + padx;
518 info_text = new DbWindowInfoText(this, x, y);
519 add_subwindow(info_text); x += title_text->get_w() + padx;
520 match_case = new DbWindowMatchCase(this, x, y);
521 add_subwindow(match_case); x += match_case->get_w() + padx;
522 x = padx; y += pady + 5;
524 search_y = get_h() - DbWindowStart::calculate_h() - 10;
525 search_start = new DbWindowStart(this, search_x, search_y);
526 add_subwindow(search_start);
527 del_items_x = search_x + DbWindowStart::calculate_w(this, search_start->get_text()) + 15;
528 del_items_y = search_y;
529 del_items = new DbWindowDeleteItems(this, del_items_x, del_items_y);
530 add_subwindow(del_items);
531 cancel_w = DbWindowCancel::calculate_w();
532 cancel_h = DbWindowCancel::calculate_h();
533 cancel_x = get_w() - cancel_w - 10;
534 cancel_y = get_h() - cancel_h - 10;
535 cancel = new DbWindowCancel(this, cancel_x, cancel_y);
536 add_subwindow(cancel);
537 list_x = x; list_y = y;
538 int list_w = get_w()-10 - list_x;
539 int list_h = min(search_y, cancel_y)-10 - list_y;
540 search_list = new DbWindowList(this, list_x, list_y, list_w, list_h);
541 add_subwindow(search_list);
542 vicon_thread = new DbWindowVIconThread(this);
543 vicon_thread->start();
544 search_list->show_window();
546 canvas_y = search_list->get_title_h();
547 canvas_w = search_list->get_w();
548 canvas_h = search_list->get_h() - canvas_y;
549 canvas = new DbWindowCanvas(this, canvas_x, canvas_y, canvas_w, canvas_h);
550 canvas->use_auxwindow(search_list);
551 canvas->create_objects(0);
552 set_icon(dwindow->mwindow->theme->get_image("record_icon"));
557 DbWindowGUI(DbWindow *dwindow)
558 : BC_Window(_(PROGRAM_NAME ": DbWindow"),
559 dwindow->mwindow->gui->get_abs_cursor_x(1) - 900 / 2,
560 dwindow->mwindow->gui->get_abs_cursor_y(1) - 600 / 2,
563 this->dwindow = dwindow;
569 search_x = search_y = 0;
570 cancel_x = cancel_y = cancel_w = cancel_h = 0;
571 del_items_x = del_items_y = 0;
572 list_x = list_y = list_w = list_h = 0;
573 sort_column = 1; sort_order = 0;
575 title_text_enable = 1;
576 info_text_enable = 1;
577 match_case_enable = 1;
579 search_columns[col_vicon] = col_vicon;
580 search_columns[col_id] = col_id;
581 search_columns[col_length] = col_length;
582 search_columns[col_source] = col_source;
583 search_columns[col_title] = col_title;
584 search_columns[col_start_time] = col_start_time;
585 search_columns[col_access_time] = col_access_time;
586 search_columns[col_access_count] = col_access_count;
587 search_column_titles[col_vicon] = _("vicon");
588 search_column_titles[col_id] = _("Id");
589 search_column_titles[col_length] = _("length");
590 search_column_titles[col_source] = _("Source");
591 search_column_titles[col_title] = C_("Title");
592 search_column_titles[col_start_time] = _("Start time");
593 search_column_titles[col_access_time] = _("Access time");
594 search_column_titles[col_access_count] = _("count");
595 search_column_widths[col_vicon] = 90;
596 search_column_widths[col_id] = 60;
597 search_column_widths[col_length] = 80;
598 search_column_widths[col_source] = 50;
599 search_column_widths[col_title] = 160;
600 search_column_widths[col_start_time] = 140;
601 search_column_widths[col_access_time] = 140;
602 search_column_widths[col_access_count] = 60;
604 for( int i=0; i<sizeof_col; ++i )
605 if( i != col_title ) wd += search_column_widths[i];
606 search_column_widths[col_title] = get_w() - wd - 40;
609 DbWindowGUI::~DbWindowGUI()
611 for( int i=0; i<sizeof_col; ++i )
612 search_items[i].remove_all_objects();
618 resize_event(int w, int h)
621 int cancel_x = w - BC_CancelButton::calculate_w() - 10;
622 int cancel_y = h - BC_CancelButton::calculate_h() - 10;
623 cancel->reposition_window(cancel_x, cancel_y);
625 search_y = h - BC_GenericButton::calculate_h() - 10;
626 search_start->reposition_window(search_x, search_y);
627 del_items_x = search_x + DbWindowStart::calculate_w(this, search_start->get_text()) + 15;
628 del_items_y = search_y;
629 del_items->reposition_window(del_items_x,del_items_y);
630 int list_w = w-10 - list_x;
631 int list_h = min(search_y, cancel_y)-10 - list_y;
632 canvas_w = SWIDTH; canvas_h = SHEIGHT;
633 canvas_x = cancel_x - canvas_w - 15;
634 canvas_y = get_h() - canvas_h - 10;
635 canvas->reposition_window(0, canvas_x, canvas_y, canvas_w, canvas_h);
637 // for( int i=0; i<sizeof_col; ++i )
638 // if( i != col_title ) wd += search_column_widths[i];
639 // search_column_widths[col_title] = w - wd - 40;
640 search_list->reposition_window(list_x, list_y, list_w, list_h);
655 vicon_thread->stop_drawing();
660 start_drawing(int update)
663 vicon_thread->list_update = 1;
664 vicon_thread->start_drawing();
668 int DbWindowGUI::search_string(const char *text, const char *sp)
671 int n = strlen(text);
672 for( const char *cp=sp; *cp!=0; ++cp ) {
673 if( match_case_enable ? !strncmp(text, cp, n) :
674 !strncasecmp(text, cp, n) ) return 1;
680 void DbWindowGUI::search_clips(MediaDb *mdb, int n, const char *text)
682 //Db::write_blocked by(mdb->db.Clip_set);
683 if( !mdb->clips_first_id() ) do {
684 if( (title_text_enable && search_string(text, mdb->clip_title())) ||
685 (info_text_enable && search_string(text, mdb->clip_path())) ) {
686 int id = mdb->clip_id();
687 double system_time = mdb->clip_system_time();
688 double start_time = 0;
689 if( system_time > 0 )
690 start_time = system_time + mdb->clip_position();
691 double access_time = 0; int access_count = 0;
692 if( !mdb->views_clip_id(id) ) {
693 access_time = mdb->views_access_time();
694 access_count = mdb->views_access_count();
696 double length = mdb->clip_frames() / mdb->clip_framerate();
697 search_results.append(new DbWindowItem(id, mdb->clip_title(),
698 mdb->clip_path(), length, start_time, access_time, access_count));
700 } while( --n >= 0 && !mdb->clips_next_id() );
703 void DbWindowGUI::search(int n, const char *text)
706 search_results.remove_all();
707 DbWindow::MDb *mdb = dwindow->mdb;
708 if( !mdb->attach_rd() ) {
709 search_clips(mdb, n, text);
715 int DbWindowGUI::delete_selection(MediaDb *mdb)
718 for( int k=0; k<search_results.size(); ++k ) {
719 if( !search_items[0][k]->get_selected() ) continue;
720 int id = search_results[k]->id;
721 if( mdb->del_clip_set(id) ) {
722 printf(_("failed delete clip id %d\n"),id);
725 search_results.remove_object_number(k);
726 for( int i=0; i<sizeof_col; ++i )
727 search_items[i].remove_object_number(k);
730 if( n > 0 ) mdb->commitDb();
734 void DbWindowGUI::delete_items()
737 DbWindow::MDb *mdb = dwindow->mdb;
738 if( !mdb->attach_wr() ) {
739 delete_selection(mdb);
749 for( int i=0; i<sizeof_col; ++i )
750 search_items[i].remove_all_objects();
752 char text[BCTEXTLEN];
753 int n = search_results.size();
754 for( int i=0; i<n; ++i ) {
755 DbWindowItem *item = search_results[i];
756 for( int k=0; k<sizeof_col; ++k ) {
757 const char *cp = 0; text[0] = 0;
758 switch( search_columns[k] ) {
763 sprintf(text,"%4d", item->id);
767 sprintf(text, "%8.3f", item->length);
776 case col_start_time: {
777 time_t st = item->start_time;
779 struct tm stm; localtime_r(&st,&stm);
780 sprintf(text,"%02d:%02d:%02d %02d/%02d/%02d",
781 stm.tm_hour, stm.tm_min, stm.tm_sec,
782 stm.tm_year%100, stm.tm_mon, stm.tm_mday);
786 case col_access_time: {
787 time_t at = item->access_time;
789 struct tm atm; localtime_r(&at,&atm);
790 sprintf(text,"%02d/%02d/%02d %02d:%02d:%02d",
791 atm.tm_year%100, atm.tm_mon, atm.tm_mday,
792 atm.tm_hour, atm.tm_min, atm.tm_sec);
796 case col_access_count: {
797 sprintf(text,"%4d", item->access_count);
802 DbSearchItem *item = new DbSearchItem(cp, LTYELLOW);
803 if( search_columns[k] == col_vicon ) {
804 item->set_text_w(SWIDTH+10);
805 item->set_text_h(SHEIGHT+5);
806 item->set_searchable(0);
808 search_items[k].append(item);
812 search_list->update();
817 DbWindowItem(int id, const char *source, const char *title,
818 double length, double start_time,
819 double access_time, int access_count)
822 if( !source ) source = "";
823 this->source = new char[strlen(source)+1];
824 strcpy(this->source,source);
825 if( !title ) title = "";
826 this->title = new char[strlen(title)+1];
827 strcpy(this->title,title);
828 this->length = length;
829 this->start_time = start_time;
830 this->access_time = access_time;
831 this->access_count = access_count;
841 #define CmprFn(nm,key) int DbWindowGUI:: \
842 cmpr_##nm(const void *a, const void *b) { \
843 DbWindowItem *&ap = *(DbWindowItem **)a; \
844 DbWindowItem *&bp = *(DbWindowItem **)b; \
845 int n = key; if( !n ) n = ap->no-bp->no; \
849 extern long timezone;
851 CmprFn(id_dn, ap->id - bp->id)
852 CmprFn(id_up, bp->id - ap->id)
853 CmprFn(length_dn, ap->length - bp->length)
854 CmprFn(length_up, bp->length - ap->length)
855 CmprFn(Source_dn, strcasecmp(ap->source, bp->source))
856 CmprFn(source_dn, strcmp(ap->source, bp->source))
857 CmprFn(Source_up, strcasecmp(bp->source, ap->source))
858 CmprFn(source_up, strcmp(bp->source, ap->source))
859 CmprFn(Title_dn, strcasecmp(ap->title, bp->title))
860 CmprFn(title_dn, strcmp(ap->title, bp->title))
861 CmprFn(Title_up, strcasecmp(bp->title, ap->title))
862 CmprFn(title_up, strcmp(bp->title, ap->title))
863 CmprFn(start_time_dn, fmod(ap->start_time-timezone,24*3600) - fmod(bp->start_time-timezone,24*3600))
864 CmprFn(start_time_up, fmod(bp->start_time-timezone,24*3600) - fmod(ap->start_time-timezone,24*3600))
865 CmprFn(access_time_dn, ap->access_time - bp->access_time)
866 CmprFn(access_time_up, bp->access_time - ap->access_time)
867 CmprFn(access_count_dn, ap->access_count - bp->access_count)
868 CmprFn(access_count_up, bp->access_count - ap->access_count)
872 sort_events(int column, int order)
874 sort_column = column; sort_order = order;
875 if( search_columns[sort_column] == col_vicon ) return;
876 int n = search_results.size();
878 DbWindowItem **items = &search_results[0];
879 for( int i=0; i<n; ++i ) items[i]->no = i;
881 int(*cmpr)(const void *, const void *) = 0;
882 switch( search_columns[sort_column] ) {
884 cmpr = sort_order ? cmpr_id_up : cmpr_id_dn;
887 cmpr = sort_order ? cmpr_length_up : cmpr_length_dn;
890 cmpr = match_case_enable ?
891 (sort_order ? cmpr_Source_up : cmpr_Source_dn) :
892 (sort_order ? cmpr_source_up : cmpr_source_dn) ;
895 cmpr = match_case_enable ?
896 (sort_order ? cmpr_Title_up : cmpr_Title_dn) :
897 (sort_order ? cmpr_title_up : cmpr_title_dn) ;
900 cmpr = sort_order ? cmpr_start_time_up : cmpr_start_time_dn;
902 case col_access_time:
903 cmpr = sort_order ? cmpr_access_time_up : cmpr_access_time_dn;
905 case col_access_count:
906 cmpr = sort_order ? cmpr_access_count_up : cmpr_access_count_dn;
910 if( cmpr ) qsort(items, n, sizeof(*items), cmpr);
914 move_column(int src, int dst)
916 if( src == dst ) return;
917 int src_column = search_columns[src];
918 const char *src_column_title = search_column_titles[src];
919 int src_column_width = search_column_widths[src];
921 for( int i=src; i<dst; ++i ) {
922 search_columns[i] = search_columns[i+1];
923 search_column_titles[i] = search_column_titles[i+1];
924 search_column_widths[i] = search_column_widths[i+1];
928 for( int i=src; i>dst; --i ) {
929 search_columns[i] = search_columns[i-1];
930 search_column_titles[i] = search_column_titles[i-1];
931 search_column_widths[i] = search_column_widths[i-1];
934 search_columns[dst] = src_column;
935 search_column_titles[dst] = src_column_title;
936 search_column_widths[dst] = src_column_width;
938 while( --k >= 0 && search_columns[k] != col_vicon );
939 if( k >= 0 ) search_list->set_master_column(k,0);
944 DbWindowCanvas(DbWindowGUI *gui, int x, int y, int w, int h)
945 : Canvas(gui->dwindow->mwindow, gui, x, y, w, h, w, h, 0)
948 this->is_fullscreen = 0;
956 void DbWindowCanvas::
959 BC_WindowBase* wdw = get_canvas();
960 wdw->lock_window("DbWindowCanvas::flash_canvas");
962 wdw->unlock_window();
965 void DbWindowCanvas::
966 draw_frame(VFrame *frame, int x, int y, int w, int h)
968 BC_WindowBase* wdw = get_canvas();
969 int min_y = gui->search_list->get_title_h();
970 int max_y = gui->search_list->get_view_h()+min_y;
971 min_y += 2; max_y -= 2;
972 int sx = 0, sy = 0, sw = frame->get_w(), sh = frame->get_h();
974 if( dy > 0 ) { sy += dy; y = min_y; sh -= dy; h -= dy; }
976 if( dy > 0 ) { sh -= dy; h -= dy; }
979 //wdw->lock_window("DbWindowCanvas::draw_frame");
980 wdw->draw_vframe(frame, x,y,w,(h&~1), sx,sy,sw,sh, 0);
981 //wdw->unlock_window();