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