15d8c4177SJérôme Duval /* 2*987acd11SAdrien Destugues * Copyright 2003-2008 Haiku Inc. All rights reserved. 35d8c4177SJérôme Duval * Distributed under the terms of the MIT license. 45d8c4177SJérôme Duval * 55d8c4177SJérôme Duval * Authors: 65d8c4177SJérôme Duval * Jérôme Duval 75d8c4177SJérôme Duval * Oliver Ruiz Dorantes 85d8c4177SJérôme Duval * Atsushi Takamatsu 95d8c4177SJérôme Duval */ 10a10cf76eSAxel Dörfler #include "HEventList.h" 11a10cf76eSAxel Dörfler 125d8c4177SJérôme Duval #include <Alert.h> 135d8c4177SJérôme Duval #include <ColumnTypes.h> 145d8c4177SJé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 195d8c4177SJérôme Duval 205d8c4177SJérôme Duval HEventRow::HEventRow(const char* name, const char* path) 215d8c4177SJérôme Duval : BRow(), 225d8c4177SJérôme Duval fName(name) 23a10cf76eSAxel Dörfler { 245d8c4177SJérôme Duval SetField(new BStringField(name), kEventColumn); 25d27b2466SRene Gollent SetPath(path); 26a10cf76eSAxel Dörfler } 27a10cf76eSAxel Dörfler 285d8c4177SJérôme Duval 295d8c4177SJérôme Duval HEventRow::~HEventRow() 305d8c4177SJérôme Duval { 315d8c4177SJérôme Duval } 325d8c4177SJérôme Duval 335d8c4177SJérôme Duval 345d8c4177SJérôme Duval void 355d8c4177SJérôme Duval HEventRow::SetPath(const char* _path) 365d8c4177SJérôme Duval { 375d8c4177SJérôme Duval fPath = _path; 385d8c4177SJérôme Duval BPath path(_path); 395d8c4177SJérôme Duval SetField(new BStringField(_path ? path.Leaf() : "<none>"), kSoundColumn); 405d8c4177SJérôme Duval } 415d8c4177SJérôme Duval 425d8c4177SJérôme Duval 435d8c4177SJérôme Duval void 445d8c4177SJérôme Duval HEventRow::Remove(const char *type) 455d8c4177SJérôme Duval { 465d8c4177SJérôme Duval BMediaFiles().RemoveItem(type, Name()); 475d8c4177SJérôme Duval } 485d8c4177SJérôme Duval 495d8c4177SJérôme Duval 505d8c4177SJérôme Duval HEventList::HEventList(BRect rect, const char* name) 51d27b2466SRene Gollent : BColumnListView(rect, name, B_FOLLOW_ALL, 0, B_PLAIN_BORDER, true), 525d8c4177SJérôme Duval fType(NULL) 535d8c4177SJérôme Duval { 545d8c4177SJérôme Duval AddColumn(new BStringColumn("Event", 150, 50, 500, B_TRUNCATE_MIDDLE), kEventColumn); 555d8c4177SJérôme Duval AddColumn(new BStringColumn("Sound", 150, 50, 500, B_TRUNCATE_END), kSoundColumn); 565d8c4177SJérôme Duval } 575d8c4177SJérôme Duval 585d8c4177SJé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 655d8c4177SJé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); 79d27b2466SRene Gollent if ((path.InitCheck() != B_OK) || (ref.name == NULL) || (strcmp(ref.name, "") == 0)) 805d8c4177SJérôme Duval AddRow(new HEventRow(name.String(), NULL)); 81d27b2466SRene Gollent else 82d27b2466SRene Gollent AddRow(new HEventRow(name.String(), path.Path())); 83a10cf76eSAxel Dörfler } 84a10cf76eSAxel Dörfler } 85a10cf76eSAxel Dörfler 865d8c4177SJérôme Duval 87a10cf76eSAxel Dörfler void 88a10cf76eSAxel Dörfler HEventList::RemoveAll() 89a10cf76eSAxel Dörfler { 905d8c4177SJérôme Duval BRow *row; 915d8c4177SJérôme Duval while ((row = RowAt((int32)0, NULL))!=NULL) { 925d8c4177SJérôme Duval RemoveRow(row); 935d8c4177SJérôme Duval delete row; 945d8c4177SJérôme Duval } 95a10cf76eSAxel Dörfler } 96a10cf76eSAxel Dörfler 97a10cf76eSAxel Dörfler 98a10cf76eSAxel Dörfler void 99a10cf76eSAxel Dörfler HEventList::SelectionChanged() 100a10cf76eSAxel Dörfler { 1015d8c4177SJérôme Duval BColumnListView::SelectionChanged(); 102a10cf76eSAxel Dörfler 1035d8c4177SJérôme Duval HEventRow* row = (HEventRow *)CurrentSelection(); 1045d8c4177SJérôme Duval if (row != NULL) { 105a10cf76eSAxel Dörfler entry_ref ref; 1065d8c4177SJé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)) { 1105d8c4177SJérôme Duval row->SetPath(path.Path()); 1115d8c4177SJérôme Duval UpdateRow(row); 112a10cf76eSAxel Dörfler } else { 113a10cf76eSAxel Dörfler printf("name %s\n", ref.name); 1145d8c4177SJérôme Duval BMediaFiles().RemoveRefFor(fType, row->Name(), ref); 1150b2fbbfaSAdrien Destugues (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); 1195d8c4177SJérôme Duval msg.AddString("name", row->Name()); 1205d8c4177SJérôme Duval msg.AddString("path", row->Path()); 121a10cf76eSAxel Dörfler Window()->PostMessage(&msg); 122a10cf76eSAxel Dörfler } 123a10cf76eSAxel Dörfler } 124a10cf76eSAxel Dörfler 1255d8c4177SJérôme Duval 126a10cf76eSAxel Dörfler void 127a10cf76eSAxel Dörfler HEventList::SetPath(const char* path) 128a10cf76eSAxel Dörfler { 1295d8c4177SJérôme Duval HEventRow* row = (HEventRow *)CurrentSelection(); 1305d8c4177SJérôme Duval if (row != NULL) { 131a10cf76eSAxel Dörfler entry_ref ref; 132a10cf76eSAxel Dörfler BEntry entry(path); 133a10cf76eSAxel Dörfler entry.GetRef(&ref); 1345d8c4177SJérôme Duval BMediaFiles().SetRefFor(fType, row->Name(), ref); 135a10cf76eSAxel Dörfler 1365d8c4177SJérôme Duval row->SetPath(path); 1375d8c4177SJérôme Duval UpdateRow(row); 138a10cf76eSAxel Dörfler } 139a10cf76eSAxel Dörfler } 140