xref: /haiku/src/bin/setdecor.cpp (revision 68ea01249e1e2088933cb12f9c28d4e5c5d1c9ef)
1 /*
2  * Copyright 2011, Joseph "looncraz" Groover, looncraz@satx.rr.com
3  * Copyright 2007, François Revol, revol@free.fr.
4  * Distributed under the terms of the MIT license.
5  */
6 
7 
8 #include <stdio.h>
9 
10 #include <Application.h>
11 #include <Bitmap.h>
12 #include <InterfaceDefs.h>
13 #include <String.h>
14 #include <View.h>
15 #include <Window.h>
16 
17 #include <DecorInfo.h>
18 
19 
20 void
21 print_decor_info_header()
22 {
23 	printf("    Name      License\t    Description\n");
24 	printf("----------------------------------------------------\n");
25 }
26 
27 
28 void
29 print_decor_summary(DecorInfo* decor, bool isCurrent)
30 {
31 	if (isCurrent)
32 		printf("*");
33 
34 	printf("%-12s\t%-8s  %-30s\n", decor->Name().String(),
35 		decor->LicenseName().String(), decor->ShortDescription().String());
36 }
37 
38 
39 void
40 print_decor_shortcut(DecorInfo* decor, bool isCurrent)
41 {
42 	if (isCurrent)
43 		printf("*");
44 
45 	printf("%-12s\t%-12s\n", decor->ShortcutName().String(),
46 		decor->Name().String());
47 }
48 
49 
50 void
51 print_decor_info_verbose(DecorInfo* decor, bool isCurrent)
52 {
53 	printf("Name:\t\t%s\n", decor->Name().String());
54 	printf("Version:\t%f\n", decor->Version());
55 	printf("Author(s):\t%s\n", decor->Authors().String());
56 	printf("Description:\t%s\n", decor->ShortDescription().String());
57 	printf("License:\t%s (%s)\n", decor->LicenseName().String(),
58 		decor->LicenseURL().String());
59 	printf("Support URL:\t%s\n", decor->SupportURL().String());
60 	printf("%s\n", isCurrent ? "Currently in use." : "Currently not in use.");
61 }
62 
63 
64 int
65 main(int argc, char** argv)
66 {
67 	if (argc < 2) {
68 		printf("usage: %s [-l|-c|decorname]\n", argv[0]);
69 		printf("\t-l: list available decors\n");
70 		printf("\t-s: list shortcut names for available decors\n");
71 		printf("\t-c: give current decor name\n");
72 		printf("\t-i: detailed information about decor\n");
73 		printf("\t-p: see preview window\n");
74 		return 1;
75 	}
76 
77 	// combine remaining args into one string:
78 	BString decoratorName;
79 	for (int i = 2; i < argc; ++i)
80 		decoratorName << argv[i] << " ";
81 	decoratorName.RemoveLast(" ");
82 
83 	BApplication app("application/x-vnd.Haiku-setdecor");
84 
85 	DecorInfoUtility* util = new DecorInfoUtility();
86 	DecorInfo* decor = NULL;
87 
88 	if (util == NULL) {
89 		fprintf(stderr, "error instantiating DecoratorInfoUtility (out of"
90 			" memory?)\n");
91 		return 1;
92 	}
93 
94 	// we want the list
95 	if (!strcmp(argv[1], "-l")) {
96 		// Print default decorator:
97 		print_decor_info_header();
98 		int32 count = util->CountDecorators();
99 		for (int32 i = 0; i < count; ++i) {
100 			decor = util->DecoratorAt(i);
101 			if (decor == NULL) {
102 				fprintf(stderr,
103 					"error NULL entry @ %" B_PRIi32 " / %" B_PRIi32
104 					" - BUG BUG BUG\n",
105 					i, count);
106 				// return 2 to track DecorInfoUtility errors
107 				return 2;
108 			}
109 			print_decor_summary(decor, util->IsCurrentDecorator(decor));
110 		}
111 
112 		return 0;
113 	}
114 
115 	// we want the current decorator
116 	if (!strcmp(argv[1], "-c")) {
117 		decor = util->CurrentDecorator();
118 
119 		if (decor == NULL) {
120 			fprintf(stderr, "Unable to determine current decorator, sorry! - "
121 				"BUG BUG BUG\n");
122 			return 2;
123 		}
124 
125 		print_decor_info_header();
126 		print_decor_summary(decor, true);
127 		return 0;
128 	}
129 
130 
131 	if (!strcmp(argv[1], "-s")) {
132 
133 		printf("  Shortcut        Name\n");
134 		printf("------------------------------------\n");
135 
136 		int32 count = util->CountDecorators();
137 		for (int32 i = 0; i < count; ++i) {
138 			decor = util->DecoratorAt(i);
139 			if (decor == NULL) {
140 				fprintf(stderr,
141 					"error NULL entry @ %" B_PRIi32 " / %" B_PRIi32
142 					" - BUG BUG BUG\n",
143 					i, count);
144 				// return 2 to track DecorInfoUtility errors
145 				return 2;
146 			}
147 			print_decor_shortcut(decor, util->IsCurrentDecorator(decor));
148 		}
149 
150 		return 0;
151 	}
152 
153 	// we want detailed information for a specific decorator ( by name or path )
154 	if (!strcmp(argv[1], "-i")) {
155 		if (argc < 3) {
156 			fprintf(stderr, "not enough arguments\n");
157 			return 1;
158 		}
159 
160 		decor = util->FindDecorator(decoratorName.String());
161 
162 		if (decor == NULL) {
163 			fprintf(stderr, "Can't find decor named \"%s\", try again\n",
164 				decoratorName.String());
165 			return 1;
166 		}
167 
168 		print_decor_info_verbose(decor, util->IsCurrentDecorator(decor));
169 		return 0;
170 	}
171 
172 
173 	if (!strcmp(argv[1], "-p")) {
174 		if (argc < 3) {
175 			fprintf(stderr, "not enough arguments\n");
176 			return 1;
177 		}
178 
179 		decor = util->FindDecorator(decoratorName.String());
180 
181 		if (decor == NULL) {
182 			fprintf(stderr, "Can't find decor named \"%s\", try again\n",
183 				decoratorName.String());
184 			return 1;
185 		}
186 
187 		printf("Preparing preview...\n");
188 
189 		BWindow* previewWindow = new BWindow(BRect(150, 150, 390, 490),
190 			decor->Name().String(), B_TITLED_WINDOW, B_NOT_ZOOMABLE
191 				| B_QUIT_ON_WINDOW_CLOSE | B_NOT_RESIZABLE );
192 
193 		previewWindow->AddChild(new BView(previewWindow->Bounds(), "",
194 			B_FOLLOW_ALL, 0));
195 
196 		if (util->Preview(decor, previewWindow) != B_OK) {
197 			fprintf(stderr, "Unable to preview decorator, sorry!\n");
198 			// TODO: more detailed error...
199 			return 1;
200 		}
201 
202 		previewWindow->Show();
203 
204 		app.Run();
205 		return 0;
206 	}
207 
208 	// we want to change it
209 	decoratorName = "";
210 	for (int i = 1; i < argc; ++i)
211 		decoratorName << argv[i] << " ";
212 	decoratorName.RemoveLast(" ");
213 
214 	decor = util->FindDecorator(decoratorName.String());
215 
216 	if (decor == NULL) {
217 		fprintf(stderr, "no such decorator \"%s\"\n", decoratorName.String());
218 		return 1;
219 	}
220 
221 	if (util->IsCurrentDecorator(decor)) {
222 		printf("\"%s\" is already the current decorator\n",
223 			decor->Name().String());
224 		return 0;
225 	}
226 
227 	printf("Setting %s as the current decorator...\n", decor->Name().String());
228 	if (util->SetDecorator(decor) != B_OK ) {
229 		fprintf(stderr, "Unable to set decorator, sorry\n\n");
230 		return 1;	// todo more detailed error...
231 	}
232 
233 	return 0;
234 }
235 
236