xref: /haiku/src/apps/resedit/ResourceRoster.cpp (revision 557791732c7b202948e9cc036ccdfb6bbbd6a909)
1 /*
2  * Copyright (c) 2005-2010, Haiku, Inc.
3  * Distributed under the terms of the MIT license.
4  *
5  * Author:
6  *		DarkWyrm <darkwyrm@gmail.com>
7  */
8 #include "ResourceRoster.h"
9 
10 #include <Application.h>
11 #include <Bitmap.h>
12 #include <Directory.h>
13 #include <Entry.h>
14 #include <image.h>
15 #include <Path.h>
16 #include <Roster.h>
17 #include <String.h>
18 
19 #include "InternalEditors.h"
20 #include "ResourceData.h"
21 #include "ResFields.h"
22 
23 // For the MakeFieldForType temp code
24 #include <TranslatorFormats.h>
25 #include <TranslationUtils.h>
26 #include <DataIO.h>
27 #include <Mime.h>
28 #include <TypeConstants.h>
29 
30 class EditorInfo
31 {
32 public:
33 					EditorInfo(const image_id &id, const char *name,
34 								create_editor *allocator);
35 					~EditorInfo(void);
36 
ID(void) const37 	status_t		ID(void) const { return fID; }
Name(void) const38 	const char *	Name(void) const { return fName.String(); }
39 	Editor *		Instantiate(void);
40 
41 private:
42 	image_id		fID;
43 	BString			fName;
44 	create_editor *	fAllocator;
45 };
46 
EditorInfo(const image_id & id,const char * name,create_editor * allocator)47 EditorInfo::EditorInfo(const image_id &id, const char *name,
48 						create_editor *allocator)
49   :	fID(id),
50   	fName(name),
51   	fAllocator(allocator)
52 {
53 }
54 
55 
~EditorInfo(void)56 EditorInfo::~EditorInfo(void)
57 {
58 }
59 
60 
61 Editor *
Instantiate(void)62 EditorInfo::Instantiate(void)
63 {
64 	return fAllocator();
65 }
66 
67 
ResourceRoster(void)68 ResourceRoster::ResourceRoster(void)
69 {
70 }
71 
72 
~ResourceRoster(void)73 ResourceRoster::~ResourceRoster(void)
74 {
75 }
76 
77 
78 BField *
MakeFieldForType(const int32 & type,const char * data,const size_t & length)79 ResourceRoster::MakeFieldForType(const int32 &type, const char *data,
80 								const size_t &length)
81 {
82 	// temporary code until editors are done
83 	switch (type) {
84 		case B_MIME_STRING_TYPE:
85 			return new StringPreviewField(data);
86 		case B_GIF_FORMAT:
87 		case B_PPM_FORMAT:
88 		case B_TGA_FORMAT:
89 		case B_BMP_FORMAT:
90 		case B_TIFF_FORMAT:
91 		case B_PNG_FORMAT:
92 		case B_JPEG_FORMAT: {
93 			BMemoryIO memio(data, length);
94 			BBitmap *bitmap = BTranslationUtils::GetBitmap(&memio);
95 			if (bitmap) {
96 				BitmapPreviewField *field = new BitmapPreviewField(bitmap);
97 				return field;
98 			}
99 			break;
100 		}
101 		default:
102 			return NULL;
103 	}
104 
105 	return NULL;
106 }
107 
108 
109 void
LoadEditors(void)110 ResourceRoster::LoadEditors(void)
111 {
112 	app_info info;
113 	be_app->GetAppInfo(&info);
114 
115 	BDirectory dir;
116 	BEntry entry(&info.ref);
117 	entry.GetParent(&dir);
118 	entry.SetTo(&dir, "addons");
119 	dir.SetTo(&entry);
120 
121 	entry_ref ref;
122 	dir.Rewind();
123 	while (dir.GetNextRef(&ref) == B_OK) {
124 		BPath path(&ref);
125 
126 		image_id addon = load_add_on(path.Path());
127 		if (addon < 0)
128 			continue;
129 
130 		char *temp;
131 		if (get_image_symbol(addon, "description", B_SYMBOL_TYPE_DATA, (void **)(&temp)) != B_OK) {
132 			unload_add_on(addon);
133 			continue;
134 		}
135 
136 		create_editor *createFunc;
137 		if (get_image_symbol(addon, "instantiate_editor", B_SYMBOL_TYPE_TEXT, (void **)(&createFunc)) != B_OK) {
138 			delete temp;
139 			unload_add_on(addon);
140 			continue;
141 		}
142 
143 		if (createFunc && temp)
144 			fList.AddItem(new EditorInfo(addon, temp, createFunc));
145 
146 		delete temp;
147 	}
148 }
149 
150 void
SpawnEditor(ResourceData * data,BHandler * handler)151 ResourceRoster::SpawnEditor(ResourceData *data, BHandler *handler)
152 {
153 	// temporary code until editors are done
154 	switch (data->GetType()) {
155 		case B_STRING_TYPE:
156 		case B_MIME_STRING_TYPE: {
157 			StringEditor *strEd = new StringEditor(BRect(100, 100, 400, 200),
158 													data, handler);
159 			strEd->Show();
160 			break;
161 		}
162 		case B_GIF_FORMAT:
163 		case B_PPM_FORMAT:
164 		case B_TGA_FORMAT:
165 		case B_BMP_FORMAT:
166 		case B_TIFF_FORMAT:
167 		case B_PNG_FORMAT:
168 		case B_JPEG_FORMAT: {
169 			ImageEditor *imgEd = new ImageEditor(BRect(100, 100, 300, 200),
170 													data, handler);
171 			imgEd->Show();
172 			break;
173 		}
174 		default:
175 			break;
176 	}
177 }
178 
179