libroot/posix/stdio: Remove unused portions.
[haiku.git] / src / bin / setdecor.cpp
blobbbb1dac0c4f063331ed2fc7e9857f7ab918d33f7
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 */
8 #include <stdio.h>
10 #include <Application.h>
11 #include <Bitmap.h>
12 #include <InterfaceDefs.h>
13 #include <String.h>
14 #include <View.h>
15 #include <Window.h>
17 #include <DecorInfo.h>
20 void
21 print_decor_info_header()
23 printf(" Name License\t Description\n");
24 printf("----------------------------------------------------\n");
28 void
29 print_decor_summary(DecorInfo* decor, bool isCurrent)
31 if (isCurrent)
32 printf("*");
34 printf("%-12s\t%-8s %-30s\n", decor->Name().String(),
35 decor->LicenseName().String(), decor->ShortDescription().String());
39 void
40 print_decor_shortcut(DecorInfo* decor, bool isCurrent)
42 if (isCurrent)
43 printf("*");
45 printf("%-12s\t%-12s\n", decor->ShortcutName().String(),
46 decor->Name().String());
50 void
51 print_decor_info_verbose(DecorInfo* decor, bool isCurrent)
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.");
64 int
65 main(int argc, char** argv)
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;
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(" ");
83 BApplication app("application/x-vnd.Haiku-setdecor");
85 DecorInfoUtility* util = new DecorInfoUtility();
86 DecorInfo* decor = NULL;
88 if (util == NULL) {
89 fprintf(stderr, "error instantiating DecoratorInfoUtility (out of"
90 " memory?)\n");
91 return 1;
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;
107 print_decor_summary(decor, util->IsCurrentDecorator(decor));
110 return 0;
113 // we want the current decorator
114 if (!strcmp(argv[1], "-c")) {
115 decor = util->CurrentDecorator();
117 if (decor == NULL) {
118 fprintf(stderr, "Unable to determine current decorator, sorry! - "
119 "BUG BUG BUG\n");
120 return 2;
123 print_decor_info_header();
124 print_decor_summary(decor, true);
125 return 0;
129 if (!strcmp(argv[1], "-s")) {
131 printf(" Shortcut Name\n");
132 printf("------------------------------------\n");
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;
143 print_decor_shortcut(decor, util->IsCurrentDecorator(decor));
146 return 0;
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;
156 decor = util->FindDecorator(decoratorName.String());
158 if (decor == NULL) {
159 fprintf(stderr, "Can't find decor named \"%s\", try again\n",
160 decoratorName.String());
161 return 1;
164 print_decor_info_verbose(decor, util->IsCurrentDecorator(decor));
165 return 0;
169 if (!strcmp(argv[1], "-p")) {
170 if (argc < 3) {
171 fprintf(stderr, "not enough arguments\n");
172 return 1;
175 decor = util->FindDecorator(decoratorName.String());
177 if (decor == NULL) {
178 fprintf(stderr, "Can't find decor named \"%s\", try again\n",
179 decoratorName.String());
180 return 1;
183 printf("Preparing preview...\n");
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 );
189 previewWindow->AddChild(new BView(previewWindow->Bounds(), "",
190 B_FOLLOW_ALL, 0));
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;
198 previewWindow->Show();
200 app.Run();
201 return 0;
204 // we want to change it
205 decoratorName = "";
206 for (int i = 1; i < argc; ++i)
207 decoratorName << argv[i] << " ";
208 decoratorName.RemoveLast(" ");
210 decor = util->FindDecorator(decoratorName.String());
212 if (decor == NULL) {
213 fprintf(stderr, "no such decorator \"%s\"\n", decoratorName.String());
214 return 1;
217 if (util->IsCurrentDecorator(decor)) {
218 printf("\"%s\" is already the current decorator\n",
219 decor->Name().String());
220 return 0;
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...
229 return 0;