15d8c4177SJérôme Duval /*
2987acd11SAdrien 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>
13404b95c4SStephan Aßmus #include <Catalog.h>
145d8c4177SJérôme Duval #include <ColumnTypes.h>
155d8c4177SJérôme Duval #include <Entry.h>
16404b95c4SStephan Aßmus #include <Locale.h>
17a10cf76eSAxel Dörfler #include <MediaFiles.h>
18a10cf76eSAxel Dörfler #include <Path.h>
19a10cf76eSAxel Dörfler #include <stdio.h>
20a10cf76eSAxel Dörfler
215d8c4177SJérôme Duval
22546208a5SOliver Tappe #undef B_TRANSLATION_CONTEXT
23546208a5SOliver Tappe #define B_TRANSLATION_CONTEXT "HEventList"
24404b95c4SStephan Aßmus
25404b95c4SStephan Aßmus
HEventRow(const char * name,const char * path)265d8c4177SJérôme Duval HEventRow::HEventRow(const char* name, const char* path)
27bf7db4bbSAlexandre Deckner :
28bf7db4bbSAlexandre Deckner BRow(),
295d8c4177SJérôme Duval fName(name)
30a10cf76eSAxel Dörfler {
315d8c4177SJérôme Duval SetField(new BStringField(name), kEventColumn);
32d27b2466SRene Gollent SetPath(path);
33a10cf76eSAxel Dörfler }
34a10cf76eSAxel Dörfler
355d8c4177SJérôme Duval
~HEventRow()365d8c4177SJérôme Duval HEventRow::~HEventRow()
375d8c4177SJérôme Duval {
385d8c4177SJérôme Duval }
395d8c4177SJérôme Duval
405d8c4177SJérôme Duval
415d8c4177SJérôme Duval void
SetPath(const char * _path)425d8c4177SJérôme Duval HEventRow::SetPath(const char* _path)
435d8c4177SJérôme Duval {
445d8c4177SJérôme Duval fPath = _path;
455d8c4177SJérôme Duval BPath path(_path);
46dd4b6c23SMatt Madia SetField(new BStringField(_path ? path.Leaf() : B_TRANSLATE("<none>")),
47404b95c4SStephan Aßmus kSoundColumn);
485d8c4177SJérôme Duval }
495d8c4177SJérôme Duval
505d8c4177SJérôme Duval
515d8c4177SJérôme Duval void
Remove(const char * type)525d8c4177SJérôme Duval HEventRow::Remove(const char* type)
535d8c4177SJérôme Duval {
545d8c4177SJérôme Duval BMediaFiles().RemoveItem(type, Name());
555d8c4177SJérôme Duval }
565d8c4177SJérôme Duval
575d8c4177SJérôme Duval
HEventList(const char * name)58404b95c4SStephan Aßmus HEventList::HEventList(const char* name)
59bf7db4bbSAlexandre Deckner :
60a154553cSJanus BColumnListView(name, B_NAVIGABLE, B_PLAIN_BORDER, true),
615d8c4177SJérôme Duval fType(NULL)
625d8c4177SJérôme Duval {
6363208ae8SJoachim Seemer AddColumn(new BStringColumn(B_TRANSLATE("Event"), 180, 50, 500,
64dd4b6c23SMatt Madia B_TRUNCATE_MIDDLE), kEventColumn);
6563208ae8SJoachim Seemer AddColumn(new BStringColumn(B_TRANSLATE("Sound"), 130, 50, 500,
66dd4b6c23SMatt Madia B_TRUNCATE_END), kSoundColumn);
675d8c4177SJérôme Duval }
685d8c4177SJérôme Duval
695d8c4177SJérôme Duval
~HEventList()70a10cf76eSAxel Dörfler HEventList::~HEventList()
71a10cf76eSAxel Dörfler {
72a10cf76eSAxel Dörfler RemoveAll();
73*863eb82bSMurai Takashi free(fType);
74a10cf76eSAxel Dörfler }
75a10cf76eSAxel Dörfler
765d8c4177SJérôme Duval
77a10cf76eSAxel Dörfler void
SetType(const char * type)78a10cf76eSAxel Dörfler HEventList::SetType(const char* type)
79a10cf76eSAxel Dörfler {
80a10cf76eSAxel Dörfler RemoveAll();
81a10cf76eSAxel Dörfler BMediaFiles mfiles;
82a10cf76eSAxel Dörfler mfiles.RewindRefs(type);
83*863eb82bSMurai Takashi free(fType);
84a10cf76eSAxel Dörfler fType = strdup(type);
85a10cf76eSAxel Dörfler
86a10cf76eSAxel Dörfler BString name;
87a10cf76eSAxel Dörfler entry_ref ref;
88a10cf76eSAxel Dörfler while (mfiles.GetNextRef(&name,&ref) == B_OK) {
89a10cf76eSAxel Dörfler BPath path(&ref);
90bf7db4bbSAlexandre Deckner if (path.InitCheck() != B_OK || ref.name == NULL
91bf7db4bbSAlexandre Deckner || strcmp(ref.name, "") == 0)
925d8c4177SJérôme Duval AddRow(new HEventRow(name.String(), NULL));
93d27b2466SRene Gollent else
94d27b2466SRene Gollent AddRow(new HEventRow(name.String(), path.Path()));
95a10cf76eSAxel Dörfler }
9657810fe9SAdrien Destugues
9757810fe9SAdrien Destugues ResizeAllColumnsToPreferred();
98a10cf76eSAxel Dörfler }
99a10cf76eSAxel Dörfler
1005d8c4177SJérôme Duval
101a10cf76eSAxel Dörfler void
RemoveAll()102a10cf76eSAxel Dörfler HEventList::RemoveAll()
103a10cf76eSAxel Dörfler {
1045d8c4177SJérôme Duval BRow* row;
1055d8c4177SJérôme Duval while ((row = RowAt((int32)0, NULL)) != NULL) {
1065d8c4177SJérôme Duval RemoveRow(row);
1075d8c4177SJérôme Duval delete row;
1085d8c4177SJérôme Duval }
109a10cf76eSAxel Dörfler }
110a10cf76eSAxel Dörfler
111a10cf76eSAxel Dörfler
112a10cf76eSAxel Dörfler void
SelectionChanged()113a10cf76eSAxel Dörfler HEventList::SelectionChanged()
114a10cf76eSAxel Dörfler {
1155d8c4177SJérôme Duval BColumnListView::SelectionChanged();
116a10cf76eSAxel Dörfler
1175d8c4177SJérôme Duval HEventRow* row = (HEventRow*)CurrentSelection();
1185d8c4177SJérôme Duval if (row != NULL) {
119a10cf76eSAxel Dörfler entry_ref ref;
1205d8c4177SJérôme Duval BMediaFiles().GetRefFor(fType, row->Name(), &ref);
121a10cf76eSAxel Dörfler
122a10cf76eSAxel Dörfler BPath path(&ref);
123bf7db4bbSAlexandre Deckner if (path.InitCheck() == B_OK || ref.name == NULL
124bf7db4bbSAlexandre Deckner || strcmp(ref.name, "") == 0) {
1255d8c4177SJérôme Duval row->SetPath(path.Path());
1265d8c4177SJérôme Duval UpdateRow(row);
127a10cf76eSAxel Dörfler } else {
128a10cf76eSAxel Dörfler printf("name %s\n", ref.name);
1295d8c4177SJérôme Duval BMediaFiles().RemoveRefFor(fType, row->Name(), ref);
130bf7db4bbSAlexandre Deckner BAlert* alert = new BAlert("alert",
131dd4b6c23SMatt Madia B_TRANSLATE("No such file or directory"), B_TRANSLATE("OK"));
132aed35104SHumdinger alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
133bf7db4bbSAlexandre Deckner alert->Go();
134a10cf76eSAxel Dörfler return;
135a10cf76eSAxel Dörfler }
136a10cf76eSAxel Dörfler BMessage msg(M_EVENT_CHANGED);
1375d8c4177SJérôme Duval msg.AddString("name", row->Name());
1385d8c4177SJérôme Duval msg.AddString("path", row->Path());
139a10cf76eSAxel Dörfler Window()->PostMessage(&msg);
140a10cf76eSAxel Dörfler }
141a10cf76eSAxel Dörfler }
142a10cf76eSAxel Dörfler
1435d8c4177SJérôme Duval
144a10cf76eSAxel Dörfler void
SetPath(const char * path)145a10cf76eSAxel Dörfler HEventList::SetPath(const char* path)
146a10cf76eSAxel Dörfler {
1475d8c4177SJérôme Duval HEventRow* row = (HEventRow*)CurrentSelection();
1485d8c4177SJérôme Duval if (row != NULL) {
149a10cf76eSAxel Dörfler entry_ref ref;
150a10cf76eSAxel Dörfler BEntry entry(path);
151a10cf76eSAxel Dörfler entry.GetRef(&ref);
1525d8c4177SJérôme Duval BMediaFiles().SetRefFor(fType, row->Name(), ref);
153a10cf76eSAxel Dörfler
1545d8c4177SJérôme Duval row->SetPath(path);
1555d8c4177SJérôme Duval UpdateRow(row);
156a10cf76eSAxel Dörfler }
157a10cf76eSAxel Dörfler }
158