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