Applied patch by Joseph "looncraz" Groover from ticket #7445.

This changes how Decorators are managed and applied. The app_server
no longer scans and maintains the available ones himself, but is
simply asked to load a Decorator add-on from a provided path.
The Decorator scanning is moved into DecorInfo and DecorInfoUtil,
private classes in the InterfaceKit. The bin command 'setdecor'
uses those.
I cleaned up all the coding style violations that I could find,
removed chunks of code which didn't make sense (if you never put
a NULL pointer into a list, you don't need to check for this and
so on) and also cleaned up other passages for improved clarity
and simplicity.
I also tested the functionality and it works fine. Would even be
Ok to include in Alpha 3, IMHO. Thanks for the patch!


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@41581 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2011-05-19 15:25:54 +00:00
parent 93e483aeab
commit b46615c55a
32 changed files with 1253 additions and 476 deletions
+192 -54
View File
@@ -1,4 +1,5 @@
/*
* Copyright 2011, Joseph "looncraz" Groover, [email protected]
* Copyright 2007, François Revol, [email protected].
* Distributed under the terms of the MIT license.
*/
@@ -7,87 +8,224 @@
#include <stdio.h>
#include <Application.h>
#include <Bitmap.h>
#include <InterfaceDefs.h>
#include <String.h>
#include <Window.h>
#include <View.h>
class BBitmap;
#include <private/interface/DecorInfo.h>
namespace BPrivate {
int32 count_decorators(void);
int32 get_decorator(void);
status_t get_decorator_name(const int32 &index, BString &name);
status_t get_decorator_preview(const int32 &index, BBitmap *bitmap);
status_t set_decorator(const int32 &index);
void
print_decor_info_header()
{
printf(" Name License\t Description\n");
printf("----------------------------------------------------\n");
}
using namespace BPrivate;
int main(int argc, char **argv)
void
print_decor_summary(DecorInfo* decor, bool isCurrent)
{
if (isCurrent)
printf("*");
printf("%-12s\t%-8s %-30s\n", decor->Name().String(),
decor->LicenseName().String(), decor->ShortDescription().String());
}
void
print_decor_shortcut(DecorInfo* decor, bool isCurrent)
{
if (isCurrent)
printf("*");
printf("%-12s\t%-12s\n", decor->ShortcutName().String(),
decor->Name().String());
}
void
print_decor_info_verbose(DecorInfo* decor, bool isCurrent)
{
printf("Name:\t\t%s\n", decor->Name().String());
printf("Version:\t%f\n", decor->Version());
printf("Author(s):\t%s\n", decor->Authors().String());
printf("Description:\t%s\n", decor->ShortDescription().String());
printf("License:\t%s (%s)\n", decor->LicenseName().String(),
decor->LicenseURL().String());
printf("Support URL:\t%s\n", decor->SupportURL().String());
printf("%s\n", isCurrent ? "Currently in use." : "Currently not in use.");
}
int
main(int argc, char** argv)
{
status_t err;
if (argc < 2) {
printf("usage: %s [-l|-c|decorname]\n", argv[0]);
printf("\t-l: list available decors\n");
printf("\t-s: list shortcut names for available decors\n");
printf("\t-c: give current decor name\n");
printf("\t-i: detailed information about decor\n");
printf("\t-p: see preview window\n");
return 1;
}
// combine remaining args into one string:
BString decoratorName;
for (int i = 2; i < argc; ++i)
decoratorName << argv[i] << " ";
decoratorName.RemoveLast(" ");
BApplication app("application/x-vnd.Haiku-setdecor");
DecorInfoUtility* util = new DecorInfoUtility();
DecorInfo* decor = NULL;
if (util == NULL) {
fprintf(stderr, "error instantiating DecoratorInfoUtility (out of"
" memory?)\n");
return 1;
}
// we want the list
if (!strcmp(argv[1], "-l")) {
int32 i, count;
count = count_decorators();
if (count < 0) {
fprintf(stderr, "error counting decorators: %s\n", strerror(count));
return 1;
}
for (i = 0; i < count; i++) {
BString name;
err = get_decorator_name(i, name);
if (err < 0)
continue;
printf("%s\n", name.String());
// Print default decorator:
print_decor_info_header();
int32 count = util->CountDecorators();
for (int32 i = 0; i < count; ++i) {
decor = util->DecoratorAt(i);
if (decor == NULL) {
fprintf(stderr, "error NULL entry @ %li / %li - BUG BUG BUG\n",
i, count);
// return 2 to track DecorInfoUtility errors
return 2;
}
print_decor_summary(decor, util->IsCurrentDecorator(decor));
}
return 0;
}
// we want the current one
// we want the current decorator
if (!strcmp(argv[1], "-c")) {
int32 i;
BString name;
i = get_decorator();
if (i < 0) {
fprintf(stderr, "error getting current decorator: %s\n", strerror(i));
return 1;
decor = util->CurrentDecorator();
if (decor == NULL) {
fprintf(stderr, "Unable to determine current decorator, sorry! - "
"BUG BUG BUG\n");
return 2;
}
err = get_decorator_name(i, name);
if (err < 0) {
fprintf(stderr, "error getting name of decorator: %s\n", strerror(err));
return 1;
}
printf("%s\n", name.String());
print_decor_info_header();
print_decor_summary(decor, true);
return 0;
}
if (!strcmp(argv[1], "-s")) {
printf(" Shortcut Name\n");
printf("------------------------------------\n");
int32 count = util->CountDecorators();
for (int32 i = 0; i < count; ++i) {
decor = util->DecoratorAt(i);
if (decor == NULL) {
fprintf(stderr, "error NULL entry @ %li / %li - BUG BUG BUG\n",
i, count);
// return 2 to track DecorInfoUtility errors
return 2;
}
print_decor_shortcut(decor, util->IsCurrentDecorator(decor));
}
return 0;
}
// we want detailed information for a specific decorator ( by name or path )
if (!strcmp(argv[1], "-i")) {
if (argc < 3) {
fprintf(stderr, "not enough arguments\n");
return 1;
}
decor = util->FindDecorator(decoratorName.String());
if (decor == NULL) {
fprintf(stderr, "Can't find decor named \"%s\", try again\n",
decoratorName.String());
return 1;
}
print_decor_info_verbose(decor, util->IsCurrentDecorator(decor));
return 0;
}
if (!strcmp(argv[1], "-p")) {
if (argc < 3) {
fprintf(stderr, "not enough arguments\n");
return 1;
}
decor = util->FindDecorator(decoratorName.String());
if (decor == NULL) {
fprintf(stderr, "Can't find decor named \"%s\", try again\n",
decoratorName.String());
return 1;
}
printf("Preparing preview...\n");
BWindow* previewWindow = new BWindow(BRect(150, 150, 390, 490),
decor->Name().String(), B_TITLED_WINDOW, B_NOT_ZOOMABLE
| B_QUIT_ON_WINDOW_CLOSE | B_NOT_RESIZABLE );
previewWindow->AddChild(new BView(previewWindow->Bounds(), "",
B_FOLLOW_ALL, 0));
if (util->Preview(decor, previewWindow) != B_OK) {
fprintf(stderr, "Unable to preview decorator, sorry!\n");
// TODO: more detailed error...
return 1;
}
previewWindow->Show();
app.Run();
return 0;
}
// we want to change it
int32 i, count;
count = count_decorators();
if (count < 0) {
fprintf(stderr, "error counting decorators: %s\n", strerror(count));
decoratorName = "";
for (int i = 1; i < argc; ++i)
decoratorName << argv[i] << " ";
decoratorName.RemoveLast(" ");
decor = util->FindDecorator(decoratorName.String());
if (decor == NULL) {
fprintf(stderr, "no such decorator \"%s\"\n", decoratorName.String());
return 1;
}
for (i = 0; i < count; i++) {
BString name;
err = get_decorator_name(i, name);
if (err < 0)
continue;
if (name == argv[1]) {
err = set_decorator(i);
if (err < 0) {
fprintf(stderr, "error setting decorator: %s\n", strerror(err));
return 1;
}
return 0;
}
if (util->IsCurrentDecorator(decor)) {
printf("\"%s\" is already the current decorator\n",
decor->Name().String());
return 0;
}
fprintf(stderr, "can't find decorator \"%s\"\n", argv[1]);
return 1;
printf("Setting %s as the current decorator...\n", decor->Name().String());
if (util->SetDecorator(decor) != B_OK ) {
fprintf(stderr, "Unable to set decorator, sorry\n\n");
return 1; // todo more detailed error...
}
return 0;
}