1*5d8c4177SJérôme Duval /* 2*5d8c4177SJérôme Duval * Copyright 2003-2008 Haiku Inc. All rights reserved. 3*5d8c4177SJérôme Duval * Distributed under the terms of the MIT license. 4*5d8c4177SJérôme Duval * 5*5d8c4177SJérôme Duval * Authors: 6*5d8c4177SJérôme Duval * Jérôme Duval 7*5d8c4177SJérôme Duval * Oliver Ruiz Dorantes 8*5d8c4177SJérôme Duval * Atsushi Takamatsu 9*5d8c4177SJérôme Duval */ 10a10cf76eSAxel Dörfler #include "HEventList.h" 11a10cf76eSAxel Dörfler 12*5d8c4177SJérôme Duval #include <Alert.h> 13*5d8c4177SJérôme Duval #include <ColumnTypes.h> 14*5d8c4177SJérôme Duval #include <Entry.h> 15a10cf76eSAxel Dörfler #include <MediaFiles.h> 16a10cf76eSAxel Dörfler #include <Path.h> 17a10cf76eSAxel Dörfler #include <stdio.h> 18a10cf76eSAxel Dörfler 19*5d8c4177SJérôme Duval 20*5d8c4177SJérôme Duval HEventRow::HEventRow(const char* name, const char* path) 21*5d8c4177SJérôme Duval : BRow(), 22*5d8c4177SJérôme Duval fName(name) 23a10cf76eSAxel Dörfler { 24*5d8c4177SJérôme Duval SetPath(path); 25*5d8c4177SJérôme Duval SetField(new BStringField(name), kEventColumn); 26a10cf76eSAxel Dörfler } 27a10cf76eSAxel Dörfler 28*5d8c4177SJérôme Duval 29*5d8c4177SJérôme Duval HEventRow::~HEventRow() 30*5d8c4177SJérôme Duval { 31*5d8c4177SJérôme Duval } 32*5d8c4177SJérôme Duval 33*5d8c4177SJérôme Duval 34*5d8c4177SJérôme Duval void 35*5d8c4177SJérôme Duval HEventRow::SetPath(const char* _path) 36*5d8c4177SJérôme Duval { 37*5d8c4177SJérôme Duval fPath = _path; 38*5d8c4177SJérôme Duval BPath path(_path); 39*5d8c4177SJérôme Duval SetField(new BStringField(_path ? path.Leaf() : "<none>"), kSoundColumn); 40*5d8c4177SJérôme Duval } 41*5d8c4177SJérôme Duval 42*5d8c4177SJérôme Duval 43*5d8c4177SJérôme Duval void 44*5d8c4177SJérôme Duval HEventRow::Remove(const char *type) 45*5d8c4177SJérôme Duval { 46*5d8c4177SJérôme Duval BMediaFiles().RemoveItem(type, Name()); 47*5d8c4177SJérôme Duval } 48*5d8c4177SJérôme Duval 49*5d8c4177SJérôme Duval 50*5d8c4177SJérôme Duval HEventList::HEventList(BRect rect, const char* name) 51*5d8c4177SJérôme Duval : BColumnListView(rect, name, B_FOLLOW_ALL, 0, B_NO_BORDER, true), 52*5d8c4177SJérôme Duval fType(NULL) 53*5d8c4177SJérôme Duval { 54*5d8c4177SJérôme Duval AddColumn(new BStringColumn("Event", 150, 50, 500, B_TRUNCATE_MIDDLE), kEventColumn); 55*5d8c4177SJérôme Duval AddColumn(new BStringColumn("Sound", 150, 50, 500, B_TRUNCATE_END), kSoundColumn); 56*5d8c4177SJérôme Duval } 57*5d8c4177SJérôme Duval 58*5d8c4177SJérôme Duval 59a10cf76eSAxel Dörfler HEventList::~HEventList() 60a10cf76eSAxel Dörfler { 61a10cf76eSAxel Dörfler RemoveAll(); 62a10cf76eSAxel Dörfler delete fType; 63a10cf76eSAxel Dörfler } 64a10cf76eSAxel Dörfler 65*5d8c4177SJérôme Duval 66a10cf76eSAxel Dörfler void 67a10cf76eSAxel Dörfler HEventList::SetType(const char* type) 68a10cf76eSAxel Dörfler { 69a10cf76eSAxel Dörfler RemoveAll(); 70a10cf76eSAxel Dörfler BMediaFiles mfiles; 71a10cf76eSAxel Dörfler mfiles.RewindRefs(type); 72a10cf76eSAxel Dörfler delete fType; 73a10cf76eSAxel Dörfler fType = strdup(type); 74a10cf76eSAxel Dörfler 75a10cf76eSAxel Dörfler BString name; 76a10cf76eSAxel Dörfler entry_ref ref; 77a10cf76eSAxel Dörfler while (mfiles.GetNextRef(&name,&ref) == B_OK) { 78a10cf76eSAxel Dörfler BPath path(&ref); 79*5d8c4177SJérôme Duval if ((path.InitCheck() == B_OK) || (ref.name == NULL) || (strcmp(ref.name, "") == 0)) 80*5d8c4177SJérôme Duval AddRow(new HEventRow(name.String(), path.Path())); 81*5d8c4177SJérôme Duval else 82*5d8c4177SJérôme Duval AddRow(new HEventRow(name.String(), NULL)); 83a10cf76eSAxel Dörfler } 84a10cf76eSAxel Dörfler } 85a10cf76eSAxel Dörfler 86*5d8c4177SJérôme Duval 87a10cf76eSAxel Dörfler void 88a10cf76eSAxel Dörfler HEventList::RemoveAll() 89a10cf76eSAxel Dörfler { 90*5d8c4177SJérôme Duval BRow *row; 91*5d8c4177SJérôme Duval while ((row = RowAt((int32)0, NULL))!=NULL) { 92*5d8c4177SJérôme Duval RemoveRow(row); 93*5d8c4177SJérôme Duval delete row; 94*5d8c4177SJérôme Duval } 95a10cf76eSAxel Dörfler } 96a10cf76eSAxel Dörfler 97a10cf76eSAxel Dörfler 98a10cf76eSAxel Dörfler void 99a10cf76eSAxel Dörfler HEventList::SelectionChanged() 100a10cf76eSAxel Dörfler { 101*5d8c4177SJérôme Duval BColumnListView::SelectionChanged(); 102a10cf76eSAxel Dörfler 103*5d8c4177SJérôme Duval HEventRow* row = (HEventRow *)CurrentSelection(); 104*5d8c4177SJérôme Duval if (row != NULL) { 105a10cf76eSAxel Dörfler entry_ref ref; 106*5d8c4177SJérôme Duval BMediaFiles().GetRefFor(fType, row->Name(), &ref); 107a10cf76eSAxel Dörfler 108a10cf76eSAxel Dörfler BPath path(&ref); 109a10cf76eSAxel Dörfler if ((path.InitCheck()==B_OK) || (ref.name == NULL) || (strcmp(ref.name, "") == 0)) { 110*5d8c4177SJérôme Duval row->SetPath(path.Path()); 111*5d8c4177SJérôme Duval UpdateRow(row); 112a10cf76eSAxel Dörfler } else { 113a10cf76eSAxel Dörfler printf("name %s\n", ref.name); 114*5d8c4177SJérôme Duval BMediaFiles().RemoveRefFor(fType, row->Name(), ref); 115a10cf76eSAxel Dörfler (new BAlert("alert", "No such file or directory", "Ok"))->Go(); 116a10cf76eSAxel Dörfler return; 117a10cf76eSAxel Dörfler } 118a10cf76eSAxel Dörfler BMessage msg(M_EVENT_CHANGED); 119*5d8c4177SJérôme Duval msg.AddString("name", row->Name()); 120*5d8c4177SJérôme Duval msg.AddString("path", row->Path()); 121a10cf76eSAxel Dörfler Window()->PostMessage(&msg); 122a10cf76eSAxel Dörfler } 123a10cf76eSAxel Dörfler } 124a10cf76eSAxel Dörfler 125*5d8c4177SJérôme Duval 126a10cf76eSAxel Dörfler void 127a10cf76eSAxel Dörfler HEventList::SetPath(const char* path) 128a10cf76eSAxel Dörfler { 129*5d8c4177SJérôme Duval HEventRow* row = (HEventRow *)CurrentSelection(); 130*5d8c4177SJérôme Duval if (row != NULL) { 131a10cf76eSAxel Dörfler entry_ref ref; 132a10cf76eSAxel Dörfler BEntry entry(path); 133a10cf76eSAxel Dörfler entry.GetRef(&ref); 134*5d8c4177SJérôme Duval BMediaFiles().SetRefFor(fType, row->Name(), ref); 135a10cf76eSAxel Dörfler 136*5d8c4177SJérôme Duval row->SetPath(path); 137*5d8c4177SJérôme Duval UpdateRow(row); 138a10cf76eSAxel Dörfler } 139a10cf76eSAxel Dörfler } 140