xref: /haiku/src/bin/desklink/desklink.cpp (revision a162e7af99792f851398580f9f8f1014117b2f79)
1 /*
2  * Copyright 2003-2009, Haiku. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Jérôme Duval
7  *		François Revol
8  *		Marcus Overhagen
9  *		Jonas Sundström
10  */
11 
12 //! VolumeControl and link items in Deskbar
13 
14 #include "desklink.h"
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 
19 #include <Alert.h>
20 #include <Application.h>
21 #include <ControlLook.h>
22 #include <Deskbar.h>
23 #include <MimeType.h>
24 #include <Roster.h>
25 #include <String.h>
26 
27 #include "DeskButton.h"
28 #include "VolumeWindow.h"
29 
30 
31 const char *kAppSignature = "application/x-vnd.Haiku-desklink";
32 	// the application signature used by the replicant to find the
33 	// supporting code
34 
35 
36 int
main(int,char ** argv)37 main(int, char **argv)
38 {
39 	BApplication app(kAppSignature);
40 	bool atLeastOnePath = false;
41 	BList titleList;
42 	BList actionList;
43 	BDeskbar deskbar;
44 	status_t err = B_OK;
45 
46 	for (int32 i = 1; argv[i]!=NULL; i++) {
47 		if (strcmp(argv[i], "--help") == 0)
48 			break;
49 
50 		if (strcmp(argv[i], "--list") == 0) {
51 			int32 count = deskbar.CountItems();
52 			int32 found = 0;
53 			int32 j = 0;
54 			printf("Deskbar items:\n");
55 
56 			while (found < count) {
57 				const char *name = NULL;
58 				if (deskbar.GetItemInfo(j, &name) == B_OK) {
59 					printf("Item %" B_PRId32 ": '%s'\n", j, name);
60 					free((void *)name);
61 					found++;
62 				}
63 				j++;
64 			}
65 			return 0;
66 		}
67 
68 		if (strcmp(argv[i], "--add-volume") == 0) {
69 			entry_ref ref;
70 			if (get_ref_for_path(argv[0], &ref) == B_OK) {
71 				deskbar.AddItem(&ref);
72 			}
73 			return 0;
74 		}
75 
76 		if (strcmp(argv[i], "--volume-control") == 0) {
77 			BWindow* window = new VolumeWindow(BRect(200, 150, 400, 200));
78 			window->Show();
79 
80 			wait_for_thread(window->Thread(), NULL);
81 			return 0;
82 		}
83 
84 		if (strncmp(argv[i], "--remove", 8) == 0) {
85 			BString replicant = "DeskButton";
86 			if (strncmp(argv[i] + 8, "=", 1) == 0) {
87 				if (strlen(argv[i] + 9) > 0) {
88 					replicant = argv[i] + 9;
89 				} else {
90 					printf("desklink: Missing replicant name.\n");
91 					return 1;
92 				}
93 			}
94 			int32 found = 0;
95 			int32 found_id;
96 			while (deskbar.GetItemInfo(replicant.String(), &found_id) == B_OK) {
97 				err = deskbar.RemoveItem(found_id);
98 				if (err != B_OK) {
99 					printf("desklink: Error removing replicant id "
100 						"%" B_PRId32 ": %s\n", found_id, strerror(err));
101 					break;
102 				}
103 				found++;
104 			}
105 			printf("Removed %" B_PRId32 " items.\n", found);
106 			return err;
107 		}
108 
109 		if (strncmp(argv[i], "cmd=", 4) == 0) {
110 			BString *title = new BString(argv[i] + 4);
111 			int32 index = title->FindFirst(':');
112 			if (index <= 0) {
113 				printf("desklink: usage: cmd=title:action\n");
114 			} else {
115 				title->Truncate(index);
116 				BString *action = new BString(argv[i] + 4);
117 				action->Remove(0, index+1);
118 				titleList.AddItem(title);
119 				actionList.AddItem(action);
120 			}
121 			continue;
122 		}
123 
124 		atLeastOnePath = true;
125 
126 		BEntry entry(argv[i], true);
127 		entry_ref ref;
128 
129 		if (entry.Exists()) {
130 			entry.GetRef(&ref);
131 		} else if (BMimeType::IsValid(argv[i])) {
132 			if (be_roster->FindApp(argv[i], &ref) != B_OK) {
133 				printf("desklink: cannot find '%s'\n", argv[i]);
134 				return 1;
135 			}
136 		} else {
137 			printf("desklink: cannot find '%s'\n", argv[i]);
138 			return 1;
139 		}
140 
141 		err = deskbar.AddItem(&ref);
142 		if (err != B_OK) {
143 			const float height = deskbar.MaxItemHeight();
144 			err = deskbar.AddItem(new DeskButton(BRect(BPoint(0, 0),
145 					BSize(height, height)),
146 				&ref, ref.name, titleList, actionList));
147 			if (err != B_OK) {
148 				printf("desklink: Deskbar refuses link to '%s': %s\n", argv[i], strerror(err));
149 				return 1;
150 			}
151 		}
152 
153 		titleList.MakeEmpty();
154 		actionList.MakeEmpty();
155 	}
156 
157 	if (!atLeastOnePath) {
158 		printf(	"usage: desklink { [ --list|--remove|[cmd=title:action ... ] [ path|signature ] } ...\n"
159 			"--add-volume: install volume control into Deskbar.\n"
160 			"--volume-control: show window with global volume control.\n"
161 			"--list: list all Deskbar addons.\n"
162 			"--remove: remove all desklink addons.\n"
163 			"--remove=name: remove all 'name' addons.\n");
164 		return 1;
165 	}
166 
167 	return 0;
168 }
169