status_t
TerminalThemesAddon::ApplyThemeHaiku(BMessage &theme, uint32 flags)
{
	BMessage termpref;
	BMessage lines;
	status_t err;
	int32 ival;
	rgb_color color;
	BString s;
	int i;

	err = MyMessage(theme, termpref);
	if (err)
		return err;

	for (i = 0; i < NENTS(sHaikuPrefsMapInt32); i++) {
		if (termpref.FindInt32(sHaikuPrefsMapInt32[i].name, &ival) < B_OK)
			ival = sHaikuPrefsMapInt32[i].def;
		s = "";
		s << ival;
		lines.AddString(sHaikuPrefsMapInt32[i].pref, s.String());
	}

	for (i = 0; i < NENTS(sHaikuPrefsMapColors); i++) {
		if (FindRGBColor(termpref, sHaikuPrefsMapColors[i].name, 0, &color) != B_OK)
			color = sHaikuPrefsMapColors[i].def;
		s = "";
		s << color.red << ", " << color.green << ", " << color.blue;
		lines.AddString(sHaikuPrefsMapColors[i].pref, s.String());
	}

	BFont tFont;
	for (i = 0; FindFont(termpref, TP_FONT, i, &tFont) == B_OK; i++) {
		// at least set the size
		s = "";
		s << (int32)tFont.Size();
		lines.AddString(PREF_HALF_FONT_SIZE, s.String());
		//tFont.PrintToStream();
		//printf("fixed: %d\n", tFont.IsFixed());
		//printf("f&h fixed: %d\n", tFont.IsFullAndHalfFixed());
		// don't even try to set the font if it's not there or not fixed
		if (!tFont.IsFixed() && !tFont.IsFullAndHalfFixed())
			continue;
		font_family ff;
		font_style fs;
		tFont.GetFamilyAndStyle(&ff, &fs);
		s = "";
		s << ff;
		lines.AddString(PREF_HALF_FONT_FAMILY, s.String());
		s = "";
		s << fs;
		lines.AddString(PREF_HALF_FONT_STYLE, s.String());
	}

	if (flags & UI_THEME_SETTINGS_SAVE && AddonFlags() & Z_THEME_ADDON_DO_SAVE) {
		SaveHaikuTerminalSettings(lines);
	}

	if (flags & UI_THEME_SETTINGS_APPLY && AddonFlags() & Z_THEME_ADDON_DO_APPLY) {
		BList teamList;
		app_info ainfo;
		int32 count, i;
		be_roster->GetAppList(&teamList);
		count = teamList.CountItems();
		for (i = 0; i < count; i++) {
			if (be_roster->GetRunningAppInfo((team_id)((addr_t)teamList.ItemAt(i)), &ainfo) == B_OK) {
				if (!strcmp(ainfo.signature, kHaikuTerminalAppSig)) {
					err = B_OK;
					//XXX: WRITEME
/*					BMessage msg(MSG_R5_SET_PREF);
					BMessenger msgr(NULL, ainfo.team);
					tp.x = 0;
					tp.y = 0;
					
					//msg.AddData("", 'UBYT', &(tp.p), sizeof(struct tpref));
					msg.AddData("", 'UBYT', &(tp), sizeof(struct termprefs));
					msg.AddSpecifier("Window", 0L);
					err = msgr.SendMessage(&msg);
*/
				}
			}
		}
	}
	
	return B_OK;
}