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