1// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2//
3//  Copyright (c) 2001-2003, OpenBeOS
4//
5//  This software is part of the OpenBeOS distribution and is covered
6//  by the MIT License.
7//
8//
9//  File:        installsound.cpp
10//  Author:      J��r��me Duval
11//  Description: manages sound events
12//
13// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <Application.h>
18#include <Entry.h>
19#include <MediaFiles.h>
20#include <Path.h>
21#include <String.h>
22
23void
24usage(void)
25{
26	printf("installsound eventname filename\n");
27	printf("        installs a new named sound event in the Sounds preferences panel.\n");
28	printf("installsound --list\n");
29	printf("        lists all sound events.\n");
30	printf("installsound --test eventname\n");
31	printf("        prints the file for the given event name, or nothing and returns error if none.\n");
32	printf("installsound --clear eventname\n");
33	printf("        clears a named event in the Sounds preferences panel.\n");
34	printf("installsound --remove eventname\n");
35	printf("        removes a named event from the Sounds preferences panel.\n");
36	exit(1);
37}
38
39int main(int argc, const char **argv)
40{
41	// Make sure we have the minimum number of arguments.
42	if (argc < 2) usage();
43
44	BApplication app("application/x-vnd.be.installsound");
45
46	BMediaFiles mfiles;
47
48	if(strcmp(argv[1], "--list")==0) {
49		mfiles.RewindRefs(BMediaFiles::B_SOUNDS);
50
51		BString name;
52		entry_ref ref;
53		while(mfiles.GetNextRef(&name,&ref) == B_OK) {
54			printf("%s:\t%s\n", name.String(),BPath(&ref).Path());
55		}
56	} else {
57		if (argc != 3) usage();
58
59		if(strcmp(argv[1], "--test")==0) {
60			entry_ref ref;
61			if(mfiles.GetRefFor(BMediaFiles::B_SOUNDS, argv[2], &ref) == B_OK)
62				printf("%s\n", BPath(&ref).Path());
63			else {
64				printf("%s: No such sound event\n", argv[2]);
65				exit(1);
66			}
67		} else if(strcmp(argv[1], "--clear")==0) {
68			entry_ref ref;
69			if(mfiles.GetRefFor(BMediaFiles::B_SOUNDS, argv[2], &ref) == B_OK)
70				mfiles.RemoveRefFor(BMediaFiles::B_SOUNDS, argv[2], ref);
71			else {
72				printf("clear %s: No such sound event\n", argv[2]);
73				exit(1);
74			}
75		} else if(strcmp(argv[1], "--remove")==0) {
76			if(mfiles.RemoveItem(BMediaFiles::B_SOUNDS, argv[2]) != B_OK) {
77				printf("remove %s: No such sound event\n", argv[2]);
78				exit(1);
79			}
80		} else {
81			entry_ref ref;
82			if(get_ref_for_path(argv[2], &ref)!=B_OK || !BEntry(&ref, true).Exists()) {
83				printf("error: %s not found\n", argv[2]);
84				exit(1);
85			}
86
87			mfiles.SetRefFor(BMediaFiles::B_SOUNDS, argv[1], ref);
88		}
89	}
90
91	exit(0);
92}
93