xref: /haiku/src/bin/desklink/desklink.cpp (revision 2222d0559df303a9846a2fad53741f8b20b14d7c)
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 <Deskbar.h>
22 #include <MimeType.h>
23 #include <Roster.h>
24 #include <String.h>
25 
26 #include "DeskButton.h"
27 #include "VolumeWindow.h"
28 
29 
30 const char *kAppSignature = "application/x-vnd.Haiku-desklink";
31 	// the application signature used by the replicant to find the
32 	// supporting code
33 
34 
35 int
36 main(int, char **argv)
37 {
38 	BApplication app(kAppSignature);
39 	bool atLeastOnePath = false;
40 	BList titleList;
41 	BList actionList;
42 	BDeskbar deskbar;
43 	status_t err = B_OK;
44 
45 	for (int32 i = 1; argv[i]!=NULL; i++) {
46 		if (strcmp(argv[i], "--help") == 0)
47 			break;
48 
49 		if (strcmp(argv[i], "--list") == 0) {
50 			int32 count = deskbar.CountItems();
51 
52 			printf("Deskbar items:\n");
53 
54 			for (int32 i = 0; i < count; i++) {
55 				const char *name = NULL;
56 				if (deskbar.GetItemInfo(i, &name) == B_OK) {
57 					printf("Item %ld: '%s'\n", i, name);
58 					free((void *)name);
59 				}
60 			}
61 			return 0;
62 		}
63 
64 		if (strcmp(argv[i], "--add-volume") == 0) {
65 			entry_ref ref;
66 			if (get_ref_for_path(argv[0], &ref) == B_OK) {
67 				deskbar.AddItem(&ref);
68 			}
69 			return 0;
70 		}
71 
72 		if (strcmp(argv[i], "--volume-control") == 0) {
73 			BWindow* window = new VolumeWindow(BRect(200, 150, 400, 200));
74 			window->Show();
75 
76 			wait_for_thread(window->Thread(), NULL);
77 			return 0;
78 		}
79 
80 		if (strncmp(argv[i], "--remove", 8) == 0) {
81 			BString replicant = "DeskButton";
82 			if (strncmp(argv[i] + 8, "=", 1) == 0) {
83 				if (strlen(argv[i] + 9) > 0) {
84 					replicant = argv[i] + 9;
85 				} else {
86 					printf("desklink: Missing replicant name.\n");
87 					return 1;
88 				}
89 			}
90 			int32 found = 0;
91 			int32 found_id;
92 			while (deskbar.GetItemInfo(replicant.String(), &found_id) == B_OK) {
93 				err = deskbar.RemoveItem(found_id);
94 				if (err != B_OK) {
95 					printf("desklink: Error removing replicant id %ld: %s\n",
96 						found_id, strerror(err));
97 					break;
98 				}
99 				found++;
100 			}
101 			printf("Removed %ld items.\n", found);
102 			return err;
103 		}
104 
105 		if (strncmp(argv[i], "cmd=", 4) == 0) {
106 			BString *title = new BString(argv[i] + 4);
107 			int32 index = title->FindFirst(':');
108 			if (index <= 0) {
109 				printf("desklink: usage: cmd=title:action\n");
110 			} else {
111 				title->Truncate(index);
112 				BString *action = new BString(argv[i] + 4);
113 				action->Remove(0, index+1);
114 				titleList.AddItem(title);
115 				actionList.AddItem(action);
116 			}
117 			continue;
118 		}
119 
120 		atLeastOnePath = true;
121 
122 		BEntry entry(argv[i], true);
123 		entry_ref ref;
124 
125 		if (entry.Exists()) {
126 			entry.GetRef(&ref);
127 		} else if (BMimeType::IsValid(argv[i])) {
128 			if (be_roster->FindApp(argv[i], &ref) != B_OK) {
129 				printf("desklink: cannot find '%s'\n", argv[i]);
130 				return 1;
131 			}
132 		} else {
133 			printf("desklink: cannot find '%s'\n", argv[i]);
134 			return 1;
135 		}
136 
137 		err = deskbar.AddItem(&ref);
138 		if (err != B_OK) {
139 			err = deskbar.AddItem(new DeskButton(BRect(0, 0, 15, 15),
140 				&ref, "DeskButton", titleList, actionList));
141 			if (err != B_OK) {
142 				printf("desklink: Deskbar refuses link to '%s': %s\n", argv[i], strerror(err));
143 				return 1;
144 			}
145 		}
146 
147 		titleList.MakeEmpty();
148 		actionList.MakeEmpty();
149 	}
150 
151 	if (!atLeastOnePath) {
152 		printf(	"usage: desklink { [ --list|--remove|[cmd=title:action ... ] [ path|signature ] } ...\n"
153 			"--list: list all Deskbar addons.\n"
154 			"--remove: remove all desklink addons.\n"
155 			"--remove=name: remove all 'name' addons.\n");
156 		return 1;
157 	}
158 
159 	return 0;
160 }
161