xref: /haiku/src/bin/setdecor.cpp (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
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, "error NULL entry @ %li / %li - BUG BUG BUG\n",
103 					i, count);
104 				// return 2 to track DecorInfoUtility errors
105 				return 2;
106 			}
107 			print_decor_summary(decor, util->IsCurrentDecorator(decor));
108 		}
109 
110 		return 0;
111 	}
112 
113 	// we want the current decorator
114 	if (!strcmp(argv[1], "-c")) {
115 		decor = util->CurrentDecorator();
116 
117 		if (decor == NULL) {
118 			fprintf(stderr, "Unable to determine current decorator, sorry! - "
119 				"BUG BUG BUG\n");
120 			return 2;
121 		}
122 
123 		print_decor_info_header();
124 		print_decor_summary(decor, true);
125 		return 0;
126 	}
127 
128 
129 	if (!strcmp(argv[1], "-s")) {
130 
131 		printf("  Shortcut        Name\n");
132 		printf("------------------------------------\n");
133 
134 		int32 count = util->CountDecorators();
135 		for (int32 i = 0; i < count; ++i) {
136 			decor = util->DecoratorAt(i);
137 			if (decor == NULL) {
138 				fprintf(stderr, "error NULL entry @ %li / %li - BUG BUG BUG\n",
139 					i, count);
140 				// return 2 to track DecorInfoUtility errors
141 				return 2;
142 			}
143 			print_decor_shortcut(decor, util->IsCurrentDecorator(decor));
144 		}
145 
146 		return 0;
147 	}
148 
149 	// we want detailed information for a specific decorator ( by name or path )
150 	if (!strcmp(argv[1], "-i")) {
151 		if (argc < 3) {
152 			fprintf(stderr, "not enough arguments\n");
153 			return 1;
154 		}
155 
156 		decor = util->FindDecorator(decoratorName.String());
157 
158 		if (decor == NULL) {
159 			fprintf(stderr, "Can't find decor named \"%s\", try again\n",
160 				decoratorName.String());
161 			return 1;
162 		}
163 
164 		print_decor_info_verbose(decor, util->IsCurrentDecorator(decor));
165 		return 0;
166 	}
167 
168 
169 	if (!strcmp(argv[1], "-p")) {
170 		if (argc < 3) {
171 			fprintf(stderr, "not enough arguments\n");
172 			return 1;
173 		}
174 
175 		decor = util->FindDecorator(decoratorName.String());
176 
177 		if (decor == NULL) {
178 			fprintf(stderr, "Can't find decor named \"%s\", try again\n",
179 				decoratorName.String());
180 			return 1;
181 		}
182 
183 		printf("Preparing preview...\n");
184 
185 		BWindow* previewWindow = new BWindow(BRect(150, 150, 390, 490),
186 			decor->Name().String(), B_TITLED_WINDOW, B_NOT_ZOOMABLE
187 				| B_QUIT_ON_WINDOW_CLOSE | B_NOT_RESIZABLE );
188 
189 		previewWindow->AddChild(new BView(previewWindow->Bounds(), "",
190 			B_FOLLOW_ALL, 0));
191 
192 		if (util->Preview(decor, previewWindow) != B_OK) {
193 			fprintf(stderr, "Unable to preview decorator, sorry!\n");
194 			// TODO: more detailed error...
195 			return 1;
196 		}
197 
198 		previewWindow->Show();
199 
200 		app.Run();
201 		return 0;
202 	}
203 
204 	// we want to change it
205 	decoratorName = "";
206 	for (int i = 1; i < argc; ++i)
207 		decoratorName << argv[i] << " ";
208 	decoratorName.RemoveLast(" ");
209 
210 	decor = util->FindDecorator(decoratorName.String());
211 
212 	if (decor == NULL) {
213 		fprintf(stderr, "no such decorator \"%s\"\n", decoratorName.String());
214 		return 1;
215 	}
216 
217 	if (util->IsCurrentDecorator(decor)) {
218 		printf("\"%s\" is already the current decorator\n",
219 			decor->Name().String());
220 		return 0;
221 	}
222 
223 	printf("Setting %s as the current decorator...\n", decor->Name().String());
224 	if (util->SetDecorator(decor) != B_OK ) {
225 		fprintf(stderr, "Unable to set decorator, sorry\n\n");
226 		return 1;	// todo more detailed error...
227 	}
228 
229 	return 0;
230 }
231 
232