Beispiel #1
0
BTranslatorRoster*
BTranslatorRoster::Default()
{
	static int32 lock = 0;

	if (sDefaultRoster != NULL)
		return sDefaultRoster;

	if (atomic_add(&lock, 1) != 0) {
		// Just wait for the default translator to be instantiated
		while (sDefaultRoster == NULL)
			snooze(10000);

		atomic_add(&lock, -1);
		return sDefaultRoster;
	}

	// If the default translators have not been loaded,
	// create a new BTranslatorRoster for them, and load them.
	if (sDefaultRoster == NULL) {
		BTranslatorRoster* roster = new BTranslatorRoster();
		roster->AddTranslators(NULL);

		sDefaultRoster = roster;
			// this will unlock any other threads waiting for
			// the default roster to become available
	}

	atomic_add(&lock, -1);
	return sDefaultRoster;
}
Beispiel #2
0
// Function taken from Haiku ShowImage,
// function originally written by Michael Pfeiffer
bool
SlideShowSaver::IsImage(const entry_ref *pref)
{
	if (!pref)
		return false;

	if (ent_is_dir(pref) != B_OK)
		// if ref is erroneous or a directory, return false
		return false;

	BFile file(pref, B_READ_ONLY);
	if (file.InitCheck() != B_OK)
		return false;

	BTranslatorRoster *proster = BTranslatorRoster::Default();
	if (!proster)
		return false;

	BMessage ioExtension;
	if (ioExtension.AddInt32("/documentIndex", 1) != B_OK)
		return false;

	translator_info info;
	memset(&info, 0, sizeof(translator_info));
	if (proster->Identify(&file, &ioExtension, &info, 0, NULL,
		B_TRANSLATOR_BITMAP) != B_OK)
		return false;

	return true;
}
Beispiel #3
0
// ---------------------------------------------------------------
// GetDefaultSettings
//
// Attempts to find the translator settings for 
// the translator named kTranslatorName with a version of
// translatorVersion.
//
// Preconditions:
//
// Parameters: kTranslatorName, the name of the translator
//                              the settings are for
//             translatorVersion, the version of the translator
//                                to retrieve
//
// Postconditions:
//
// Returns: NULL, if anything went wrong
//          BMessage * of configuration data for kTranslatorName
// ---------------------------------------------------------------
BMessage *
BTranslationUtils::GetDefaultSettings(const char *kTranslatorName,
	int32 translatorVersion)
{
	BTranslatorRoster *roster = BTranslatorRoster::Default();
	translator_id *translators = NULL;
	int32 numTranslators = 0;
	if (roster == NULL
		|| roster->GetAllTranslators(&translators, &numTranslators) != B_OK)
		return NULL;

	// Cycle through all of the default translators
	// looking for a translator that matches the name and version
	// that I was given
	BMessage *pMessage = NULL;
	const char *currentTranName = NULL, *currentTranInfo = NULL;
	int32 currentTranVersion = 0;
	for (int i = 0; i < numTranslators; i++) {

		if (roster->GetTranslatorInfo(translators[i], &currentTranName,
			&currentTranInfo, &currentTranVersion) == B_OK) {

			if (currentTranVersion == translatorVersion
				&& strcmp(currentTranName, kTranslatorName) == 0) {
				pMessage = GetDefaultSettings(translators[i], roster);
				break;				
			}
		}
	}
	
	delete[] translators;
	return pMessage;
}
void MediaRoutingView::_changeBackground(
	entry_ref *ref)
{
	D_METHOD(("MediaRoutingView::_changeBackground()\n"));

	status_t error;
	BBitmap *background = 0; 
	BFile file(ref, B_READ_ONLY); 
	error = file.InitCheck();
	if (!error)
	{
		BTranslatorRoster *roster = BTranslatorRoster::Default(); 
		BBitmapStream stream; 
		error = roster->Translate(&file, NULL, NULL, &stream, B_TRANSLATOR_BITMAP);
		if (!error)
		{
			stream.DetachBitmap(&background); 
			setBackgroundBitmap(background);
			Invalidate();

			// [e.moon 1dec99] persistence, yay
			m_backgroundBitmapEntry.SetTo(ref);
		}
	}
	delete background;
}
status_t
DataTranslationsWindow::_ShowConfigView(int32 id)
{
	// Shows the config panel for the translator with the given id

	if (id < 0)
		return B_BAD_VALUE;

	BTranslatorRoster* roster = BTranslatorRoster::Default();

	if (fConfigView) {
		fRightBox->RemoveChild(fConfigView);
		delete fConfigView;
		fConfigView = NULL;
	}

	BMessage emptyMsg;
	BRect rect(0, 0, 200, 233);
	status_t ret = roster->MakeConfigurationView(id, &emptyMsg,
		&fConfigView, &rect);

	if (ret != B_OK)
		return ret;

	fConfigView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
		// force config views to all have the same color
	fRightBox->AddChild(fConfigView);

	return B_OK;
}
Beispiel #6
0
const char*
Utility::_GetMimeString(uint32 imageType) const
{
	const char *dummy = "";
	translator_id* translators = NULL;
	int32 numTranslators = 0;
	BTranslatorRoster* roster = BTranslatorRoster::Default();
	status_t status = roster->GetAllTranslators(&translators, &numTranslators);
	if (status != B_OK)
		return dummy;

	for (int32 x = 0; x < numTranslators; x++) {
		const translation_format* formats = NULL;
		int32 numFormats;

		if (roster->GetOutputFormats(x, &formats, &numFormats) == B_OK) {
			for (int32 i = 0; i < numFormats; ++i) {
				if (formats[i].type == imageType) {
					delete [] translators;
					return formats[i].MIME;
				}
			}
		}
	}
	delete [] translators;
	return dummy;
}
// Reads the installed translators and adds them to our BListView
status_t
DataTranslationsWindow::_PopulateListView()
{
	BTranslatorRoster* roster = BTranslatorRoster::Default();

	// Get all Translators on the system. Gives us the number of translators
	// installed in num_translators and a reference to the first one
	int32 numTranslators;
	translator_id* translators = NULL;
	roster->GetAllTranslators(&translators, &numTranslators);

	float maxWidth = 0;

	for (int32 i = 0; i < numTranslators; i++) {
		// Getting the first three Infos: Name, Info & Version
		int32 version;
		const char* name;
		const char* info;
		roster->GetTranslatorInfo(translators[i], &name, &info, &version);
		fTranslatorListView->AddItem(new TranslatorItem(translators[i], name));
		maxWidth = std::max(maxWidth, fTranslatorListView->StringWidth(name));
	}

	fTranslatorListView->SortItems();

	fTranslatorListView->SetExplicitSize(BSize(maxWidth + 20, B_SIZE_UNSET));

	delete[] translators;
	return B_OK;
}
void
ImageView::SaveImageAtDropLocation(BMessage *pmsg)
{
	// Find the location and name of the drop and
	// write the image file there
	BBitmapStream stream(fpbitmap);

	StatusCheck chk;
		// throw an exception if this is assigned
		// anything other than B_OK

	try {
		entry_ref dirref;
		chk = pmsg->FindRef("directory", &dirref);
		const char *filename;
		chk = pmsg->FindString("name", &filename);

		BDirectory dir(&dirref);
		BFile file(&dir, filename, B_WRITE_ONLY | B_CREATE_FILE);
		chk = file.InitCheck();

		BTranslatorRoster *proster = BTranslatorRoster::Default();
		chk = proster->Translate(&stream, NULL, NULL, &file, B_TGA_FORMAT);

	} catch (StatusNotOKException) {
		BAlert *palert = new BAlert(NULL,
			B_TRANSLATE("Sorry, unable to write the image file."),
			B_TRANSLATE("OK"));
		palert->Go();
	}

	stream.DetachBitmap(&fpbitmap);
}
Beispiel #9
0
status_t
SlideShowSaver::SetImage(const entry_ref *pref)
{
	entry_ref ref;
	if (!pref)
		ref = fCurrentRef;
	else
		ref = *pref;

	BTranslatorRoster *proster = BTranslatorRoster::Default();
	if (!proster)
		return B_ERROR;

	if (ent_is_dir(pref) != B_OK)
		// if ref is erroneous or a directory, return error
		return B_ERROR;

	BFile file(&ref, B_READ_ONLY);
	translator_info info;
	memset(&info, 0, sizeof(translator_info));
	BMessage ioExtension;
	//if (ref != fCurrentRef)
		// if new image, reset to first document
	//	fDocumentIndex = 1;
	if (ioExtension.AddInt32("/documentIndex", 1 /*fDocumentIndex*/) != B_OK)
		return B_ERROR;
	if (proster->Identify(&file, &ioExtension, &info, 0, NULL,
		B_TRANSLATOR_BITMAP) != B_OK)
		return B_ERROR;

	// Translate image data and create a new ShowImage window
	BBitmapStream outstream;
	if (proster->Translate(&file, &info, &ioExtension, &outstream,
		B_TRANSLATOR_BITMAP) != B_OK)
		return B_ERROR;
	BBitmap *newBitmap = NULL;
	if (outstream.DetachBitmap(&newBitmap) != B_OK)
		return B_ERROR;

	// Now that I've successfully loaded the new bitmap,
	// I can be sure it is safe to delete the old one,
	// and clear everything
	delete fBitmap;
	fBitmap = newBitmap;
	newBitmap = NULL;
	fCurrentRef = ref;

	// Get path to use in caption
	fCaption = "<< Unable to read the path >>";
	BEntry entry(&fCurrentRef);
	if (entry.InitCheck() == B_OK) {
		BPath path(&entry);
		if (path.InitCheck() == B_OK) {
			fCaption = path.Path();
		}
	}

	return B_OK;
}
Beispiel #10
0
status_t
ThemeManager::SetThemeScreenShot(int32 id, BBitmap *bitmap)
{
	FENTRY;
	status_t err;
	BMessage msg;
	BString name;
	BString themepath;
	BMessage *theme;
	
	if (id < 0)
		return EINVAL;
	theme = (BMessage *)fThemeList.ItemAt(id);
	if (!theme)
		return EINVAL;
	
	// TODO
	err = theme->FindMessage(Z_THEME_INFO_MESSAGE, &msg);
	if (err) {
		msg.MakeEmpty();
		theme->AddMessage(Z_THEME_INFO_MESSAGE, &msg);
	}
	err = ThemeLocation(id, themepath);
	if (err)
		return err;
	err = msg.FindString(Z_THEME_SCREENSHOT_FILENAME, &name);
	if (!err) {
		BPath spath(themepath.String());
		spath.Append(name.String());
		BEntry ent(spath.Path());
		if (ent.InitCheck() == B_OK)
			ent.Remove();
	}
	
	name = "screenshot.png";
	err = msg.ReplaceString(Z_THEME_SCREENSHOT_FILENAME, name);
	if (err)
		err = msg.AddString(Z_THEME_SCREENSHOT_FILENAME, name);
	if (err)
		return err;
	
	// save the BBitmap to a png
	BPath spath(themepath.String());
	spath.Append(name.String());
	BFile shotfile(spath.Path(), B_WRITE_ONLY|B_CREATE_FILE);
	if (shotfile.InitCheck() != B_OK)
		return shotfile.InitCheck();
	BTranslatorRoster *troster = BTranslatorRoster::Default();
	BBitmapStream bmstream(bitmap);
	err = troster->Translate(&bmstream, NULL, NULL, &shotfile, 'PNG '/* XXX: hack, should find by mime type */);
	if (err)
		return err;
	
	err = theme->ReplaceMessage(Z_THEME_INFO_MESSAGE, &msg);
	msg.PrintToStream();
	return err;
}
Beispiel #11
0
void
PictureView::_BeginDrag(BPoint sourcePoint)
{
	BBitmap* bitmap = _CopyPicture(128);
	if (bitmap == NULL)
		return;

	// fill the drag message
	BMessage drag(B_SIMPLE_DATA);
	drag.AddInt32("be:actions", B_COPY_TARGET);
	drag.AddInt32("be:actions", B_TRASH_TARGET);

	// name the clip after person name, if any
	BString name = B_TRANSLATE("%name% picture");
	name.ReplaceFirst("%name%", Window() ? Window()->Title() :
		B_TRANSLATE("Unnamed person"));
	drag.AddString("be:clip_name", name.String());

	BTranslatorRoster* roster = BTranslatorRoster::Default();
	if (roster == NULL) {
		delete bitmap;
		return;
	}

	int32 infoCount;
	translator_info* info;
	BBitmapStream stream(bitmap);
	if (roster->GetTranslators(&stream, NULL, &info, &infoCount) == B_OK) {
		for (int32 i = 0; i < infoCount; i++) {
			const translation_format* formats;
			int32 count;
			roster->GetOutputFormats(info[i].translator, &formats, &count);
			for (int32 j = 0; j < count; j++) {
				if (strcmp(formats[j].MIME, "image/x-be-bitmap") != 0) {
					// needed to send data in message
					drag.AddString("be:types", formats[j].MIME);
					// needed to pass data via file
					drag.AddString("be:filetypes", formats[j].MIME);
					drag.AddString("be:type_descriptions", formats[j].name);
				}
			}
		}
	}
	stream.DetachBitmap(&bitmap);

	// we also support "Passing Data via File" protocol
	drag.AddString("be:types", B_FILE_MIME_TYPE);

	sourcePoint -= fPictureRect.LeftTop();

	SetMouseEventMask(B_POINTER_EVENTS);

	DragMessage(&drag, bitmap, B_OP_ALPHA, sourcePoint);
	bitmap = NULL;
}
void
ShowImageWindow::_SaveToFile(BMessage* message)
{
	// Read in where the file should be saved
	entry_ref dirRef;
	if (message->FindRef("directory", &dirRef) != B_OK)
		return;

	const char* filename;
	if (message->FindString("name", &filename) != B_OK)
		return;

	// Read in the translator and type to be used
	// to save the output image
	translator_id outTranslator;
	uint32 outType;
	if (message->FindInt32(kTranslatorField,
			reinterpret_cast<int32 *>(&outTranslator)) != B_OK
		|| message->FindInt32(kTypeField,
			reinterpret_cast<int32 *>(&outType)) != B_OK)
		return;

	// Find the translator_format information needed to
	// write a MIME attribute for the image file
	BTranslatorRoster* roster = BTranslatorRoster::Default();
	const translation_format* outFormat = NULL;
	int32 outCount = 0;
	if (roster->GetOutputFormats(outTranslator, &outFormat, &outCount) != B_OK
		|| outCount < 1)
		return;

	int32 i;
	for (i = 0; i < outCount; i++) {
		if (outFormat[i].group == B_TRANSLATOR_BITMAP && outFormat[i].type
				== outType)
			break;
	}
	if (i == outCount)
		return;

	// Write out the image file
	BDirectory dir(&dirRef);
	fImageView->SaveToFile(&dir, filename, NULL, &outFormat[i]);

	// Store Save directory in settings;
	ShowImageSettings* settings = my_app->Settings();
	if (settings->Lock()) {
		BPath path(&dirRef);
		settings->SetString("SaveDirectory", path.Path());
		settings->Unlock();
	}
}
BBitmap * RBitmapLoader::TranslateBitmap(const char *name)
{
	 BPath path(&mAppDir, name);
	 BFile file(path.Path(), B_READ_ONLY); 
	 BTranslatorRoster *roster; 
	 BBitmapStream stream; 
	 BBitmap *result = NULL; 
	 roster = BTranslatorRoster::Default();
	 if (roster->Translate(&file, NULL, NULL, &stream,  B_TRANSLATOR_BITMAP) < B_OK) 
		 result = NULL; 
	 else stream.DetachBitmap(&result); 
	 return result; 
}
Beispiel #14
0
void
ResView::AddResource(const entry_ref &ref)
{
	BFile file(&ref, B_READ_ONLY);
	if (file.InitCheck() != B_OK)
		return;
	
	BString mime;
	file.ReadAttrString("BEOS:TYPE", &mime);
	
	if (mime == "application/x-be-resource") {
		BMessage msg(B_REFS_RECEIVED);
		msg.AddRef("refs", &ref);
		be_app->PostMessage(&msg);
		return;
	}
	
	type_code fileType = 0;
	
	BTranslatorRoster *roster = BTranslatorRoster::Default();
	translator_info info;
	if (roster->Identify(&file, NULL, &info, 0, mime.String()) == B_OK)
		fileType = info.type;
	else
		fileType = B_RAW_TYPE;
	
	int32 lastID = -1;
	for (int32 i = 0; i < fDataList.CountItems(); i++) {
		ResourceData *resData = (ResourceData*)fDataList.ItemAt(i);
		if (resData->GetType() == fileType && resData->GetID() > lastID)
			lastID = resData->GetID();
	}
	
	off_t fileSize;
	file.GetSize(&fileSize);
	
	if (fileSize < 1)
		return;
	
	char *fileData = (char *)malloc(fileSize);
	file.Read(fileData, fileSize);
	
	ResourceData *resData = new ResourceData(fileType, lastID + 1, ref.name,
											fileData, fileSize);
	fDataList.AddItem(resData);
	
	ResDataRow *row = new ResDataRow(resData);
	fListView->AddRow(row);
	
	SetSaveStatus(FILE_DIRTY);
}
/*=============================================================================================*\
|	FetchBitmap																					|
+-----------------------------------------------------------------------------------------------+
|	Effet: Converie une image en un BBitmap. La couleur de transparence est celle du pixel dans	|
|			le coin superieur gauche.															|
|	Entree:																						|
|		char *pzFileName: Le path du fichier image a convertir.									|
|		bool bTran: True si on utilise la transparence, false sinon.							|
|	Sortie:																						|
|		BBitmap *: Le pointeur le bitmap de l'image. NULL si la conversion a echouer.			|
\*=============================================================================================*/
BBitmap*
BitmapCatalog::FetchBitmap(char* pzFileName, bool bTrans) 
{ 


	BFile file(pzFileName, B_READ_ONLY); 
	BTranslatorRoster *roster = BTranslatorRoster::Default(); 
	BBitmapStream stream; 
	BBitmap *result = NULL; 
	if (roster->Translate(&file, NULL, NULL, &stream, B_TRANSLATOR_BITMAP) < B_OK) 
		return NULL; 
	stream.DetachBitmap(&result); 


//  OliverESP: 7 x 1 so -> #include <TranslationUtils.h> //OliverESP:
//			   less code and works
	//BBitmap *result = BTranslationUtils::GetBitmapFile(pzFileName);
	
	if (result == NULL)
		return NULL;

	if(!bTrans)
		return result;
	
	int32 iLenght = result->BitsLength() / 4;
	int32 i;
	int32 * cBit = (int32*)result->Bits();
	int32 backColor = cBit[result->Bounds().IntegerWidth() - 1];
	int32 iTrans = 0;
      
	//Determine le mode de definition de couleur
	switch(result->ColorSpace())
	{
	case B_RGB32:		iTrans = B_TRANSPARENT_MAGIC_RGBA32; break;
	case B_RGB32_BIG:	iTrans = B_TRANSPARENT_MAGIC_RGBA32_BIG; break;
	default:			break; //TODO: Major screwup here!
	}

	if (iTrans)
	{
		for(i = 0; i < iLenght; i++)
		{
			if(cBit[i] == backColor)
				cBit[i] = iTrans;
		}
	}

	return result; 
}
Beispiel #16
0
status_t
DataTranslationsWindow::_ShowConfigView(int32 id)
{
	// Shows the config panel for the translator with the given id

	if (id < 0)
		return B_BAD_VALUE;

	BTranslatorRoster *roster = BTranslatorRoster::Default();

	// fConfigView is NULL the first time this function
	// is called, prevent a segment fault
	if (fConfigView)
		fRightBox->RemoveChild(fConfigView);

	BMessage emptyMsg;
	BRect rect(0, 0, 200, 233);
	status_t ret = roster->MakeConfigurationView(id, &emptyMsg, &fConfigView, &rect);
	if (ret != B_OK) {
		fRightBox->RemoveChild(fConfigView);
		return ret;
	}

	BRect configRect(fRightBox->Bounds());
	configRect.InsetBy(3, 3);
	configRect.bottom -= 45;
	float width = 0, height = 0;
	if ((fConfigView->Flags() & B_SUPPORTS_LAYOUT) != 0) {
		BSize configSize = fConfigView->ExplicitPreferredSize();
		width = configSize.Width();
		height = configSize.Height();
	} else {
		fConfigView->GetPreferredSize(&width, &height);
	}
	float widen = max_c(0, width - configRect.Width());
	float heighten = max_c(0, height - configRect.Height());
	if (widen > 0 || heighten > 0) {
		ResizeBy(widen, heighten);
		configRect.right += widen;
		configRect.bottom += heighten;
	}
	fConfigView->MoveTo(configRect.left, configRect.top);
	fConfigView->ResizeTo(configRect.Width(), configRect.Height());
	fConfigView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
		// force config views to all have the same color
	fRightBox->AddChild(fConfigView);

	return B_OK;
}
Beispiel #17
0
status_t
AddTranslationItems(BMenu* intoMenu, uint32 fromType)
{

	BTranslatorRoster* use;
	char* translatorTypeName;
	const char* translatorIdName;

	use = BTranslatorRoster::Default();
	translatorIdName = "be:translator";
	translatorTypeName = (char *)"be:type";
	translator_id* ids = NULL;
	int32 count = 0;

	status_t err = use->GetAllTranslators(&ids, &count);
	if (err < B_OK)
		return err;

	for (int tix = 0; tix < count; tix++) {
		const translation_format* formats = NULL;
		int32 num_formats = 0;
		bool ok = false;
		err = use->GetInputFormats(ids[tix], &formats, &num_formats);
		if (err == B_OK)
			for (int iix = 0; iix < num_formats; iix++) {
				if (formats[iix].type == fromType) {
					ok = true;
					break;
				}
			}

		if (!ok)
			continue;

		err = use->GetOutputFormats(ids[tix], &formats, &num_formats);
		if (err == B_OK)
			for (int oix = 0; oix < num_formats; oix++) {
 				if (formats[oix].type != fromType) {
					BMessage* itemmsg;
					itemmsg = new BMessage(msg_translate);
					itemmsg->AddInt32(translatorIdName, ids[tix]);
					itemmsg->AddInt32(translatorTypeName, formats[oix].type);
					intoMenu->AddItem(new BMenuItem(formats[oix].name, itemmsg));
				}
			}
	}
	delete[] ids;
	return B_OK;
}
Beispiel #18
0
status_t
PictureView::_LoadPicture(const entry_ref* ref)
{
	BFile file;
	status_t status = file.SetTo(ref, B_READ_ONLY);
	if (status != B_OK)
		return status;

	off_t fileSize;
	status = file.GetSize(&fileSize);
	if (status != B_OK)
		return status;

	// Check that we've at least some data to translate...
	if (fileSize < 1)
		return B_OK;

	translator_info info;
	memset(&info, 0, sizeof(translator_info));
	BMessage ioExtension;

	BTranslatorRoster* roster = BTranslatorRoster::Default();
	if (roster == NULL)
		return B_ERROR;

	status = roster->Identify(&file, &ioExtension, &info, 0, NULL,
		B_TRANSLATOR_BITMAP);

	BBitmapStream stream;

	if (status == B_OK) {
		status = roster->Translate(&file, &info, &ioExtension, &stream,
			B_TRANSLATOR_BITMAP);
	}
	if (status != B_OK)
		return status;

	BBitmap* picture = NULL;
	if (stream.DetachBitmap(&picture) != B_OK
		|| picture == NULL)
		return B_ERROR;

	// Remember image format so we could store using the same
	fPictureMIMEType = info.MIME;
	fPictureType = info.type;

	_SetPicture(picture);
	return B_OK;
}
Beispiel #19
0
void
PersonView::Save()
{
	BFile file(fRef, B_READ_WRITE);
	if (file.InitCheck() != B_NO_ERROR)
		return;

	fSaving = true;

	int32 count = fControls.CountItems();
	for (int32 i = 0; i < count; i++) {
		AttributeTextControl* control = fControls.ItemAt(i);
		const char* value = control->Text();
		file.WriteAttr(control->Attribute().String(), B_STRING_TYPE, 0,
			value, strlen(value) + 1);
		control->Update();
	}

	// Write the picture, if any, in the person file content
	if (fPictureView) {
		// Trim any previous content
		file.Seek(0, SEEK_SET);
		file.SetSize(0);

		BBitmap* picture = fPictureView->Bitmap();
		if (picture) {
			BBitmapStream stream(picture);
			// Detach *our* bitmap from stream to avoid its deletion
			// at stream object destruction
			stream.DetachBitmap(&picture);

			BTranslatorRoster* roster = BTranslatorRoster::Default();
			roster->Translate(&stream, NULL, NULL, &file,
				fPictureView->SuggestedType(), B_TRANSLATOR_BITMAP,
				fPictureView->SuggestedMIMEType());

		}

		fPictureView->Update();
	}

	file.GetModificationTime(&fLastModificationTime);

	fSaving = false;
}
Beispiel #20
0
/*!
	Save the screenshot to the file with the specified filename and type.
	Note that any existing file with the same filename will be overwritten
	without warning.
*/
status_t
Utility::Save(BBitmap** screenshot, const char* fileName, uint32 imageType)
	const
{
	BString fileNameString(fileName);

	// Generate a default filename when none is given
	if (fileNameString.Compare("") == 0) {
		BPath homePath;
		if (find_directory(B_USER_DIRECTORY, &homePath) != B_OK)
			return B_ERROR;

		BEntry entry;
		int32 index = 1;
		BString extension = GetFileNameExtension(imageType);
		do {
			fileNameString.SetTo(homePath.Path());
			fileNameString << "/" << B_TRANSLATE(sDefaultFileNameBase) << index++ 
				<< extension;
			entry.SetTo(fileNameString.String());
		} while (entry.Exists());
	}

	// Create the file
	BFile file(fileNameString, B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
	if (file.InitCheck() != B_OK)
		return B_ERROR;

	// Write the screenshot bitmap to the file
	BBitmapStream stream(*screenshot);
	BTranslatorRoster* roster = BTranslatorRoster::Default();
	roster->Translate(&stream, NULL, NULL, &file, imageType,
		B_TRANSLATOR_BITMAP);
	*screenshot = NULL;

	// Set the file MIME attribute
	BNodeInfo nodeInfo(&file);
	if (nodeInfo.InitCheck() != B_OK)
		return B_ERROR;

	nodeInfo.SetType(_GetMimeString(imageType));

	return B_OK;
}
status_t
DataTranslationsWindow::_ShowConfigView(int32 id)
{
	// Shows the config panel for the translator with the given id

	if (id < 0)
		return B_BAD_VALUE;

	BTranslatorRoster* roster = BTranslatorRoster::Default();

	if (fConfigView != NULL) {
		fRightBox->RemoveChild(fConfigView);
		delete fConfigView;
		fConfigView = NULL;
		fInfoText = NULL;
		if (fRelease != NULL) {
			fRelease->Release();
			fRelease = NULL;
		}
	}

	BMessage emptyMessage;
	BRect rect(0.0f, 0.0f, 200.0f, 233.0f);
	status_t result = roster->MakeConfigurationView(id, &emptyMessage,
		&fConfigView, &rect);

	if (result != B_OK)
		return result;

	fConfigView->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
		// force config views to all have the same color
	fRightBox->AddChild(fConfigView);

	// for default 12pt font size: 597 ≈ (0.85 * 12 * 49)
	fConfigView->SetExplicitMinSize(
		BSize(ceilf(be_control_look->DefaultItemSpacing() * 49)
			- fTranslatorListView->Frame().Width(), B_SIZE_UNSET));

	// Make sure the translator's image doesn't get unloaded while we are still
	// showing a config view whose code is in the image
	fRelease = roster->AcquireTranslator(id);

	return B_OK;
}
Beispiel #22
0
ImageView::ImageView(BPositionIO *image)
	:
	BView(BRect(0, 0, 1, 1), "image_view", B_FOLLOW_NONE, B_WILL_DRAW),
	fSuccess(true)
{
	if (!image) {
		fSuccess = false;
		return;
	}
	// Initialize and translate the image
	BTranslatorRoster *roster = BTranslatorRoster::Default();
	BBitmapStream stream;
	if (roster->Translate(image, NULL, NULL, &stream, B_TRANSLATOR_BITMAP)
			< B_OK) {
		fSuccess = false;
		return;
	}
	stream.DetachBitmap(&fImage);
}
/*=============================================================================================*\
|	FetchBitmap																					|
+-----------------------------------------------------------------------------------------------+
|	Effet: Converie une image en un BBitmap. La couleur de transparence est celle du pixel dans	|
|			le coin superieur gauche.															|
|	Entree:																						|
|		char *pzFileName: Le path du fichier image a convertir.									|
|		bool bTran: True si on utilise la transparence, false sinon.							|
|	Sortie:																						|
|		BBitmap *: Le pointeur le bitmap de l'image. NULL si la conversion a echouer.			|
\*=============================================================================================*/
BBitmap * BeNetBitmapCatalog::FetchBitmap(char *pzFileName, bool bTrans) 
{ 
      BFile file(pzFileName, B_READ_ONLY); 
      BTranslatorRoster *roster = BTranslatorRoster::Default(); 
      BBitmapStream stream; 
      BBitmap *result = NULL; 
      if (roster->Translate(&file, NULL, NULL, &stream, 
            B_TRANSLATOR_BITMAP) < B_OK) 
         return NULL; 
      stream.DetachBitmap(&result); 
            
      
      if(!bTrans) return result;
      int32 iLenght = result->BitsLength() / 4;
      int32 i;
      int32 * cBit = (int32)result->Bits();
      int32 backColor = cBit[result->Bounds().IntegerWidth() - 1];
      int32 iTrans = 0;
      
      //Determine le mode de definition de couleur
      switch(result->ColorSpace())
      {
      		case B_RGB32:{
      				iTrans = B_TRANSPARENT_MAGIC_RGBA32;
      			}break;
      		case B_RGB32_BIG:{
      				iTrans = B_TRANSPARENT_MAGIC_RGBA32_BIG;
      			}break;
       }
      
      if(iTrans)
      {
     	for(i = 0; i < iLenght; i++)
      	{
      		if(cBit[i] == backColor)
      		{
     			cBit[i] = B_TRANSPARENT_MAGIC_RGBA32_BIG;
      		}
      	}
      }
      
      return result; 
}//Fin de FetchBitmap.
Beispiel #24
0
DataTranslationsWindow::DataTranslationsWindow()
	: BWindow(BRect(0, 0, 550, 350), "DataTranslations", B_TITLED_WINDOW,
		B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE | B_NOT_RESIZABLE
			| B_AUTO_UPDATE_SIZE_LIMITS)
{
	MoveTo(dynamic_cast<DataTranslationsApplication *>(be_app)->WindowCorner());

	_SetupViews();

	// Make sure that the window isn't positioned off screen
	BScreen screen;
	BRect screenFrame = screen.Frame();
	if (!screenFrame.Contains(Frame()))
		CenterOnScreen();

	BTranslatorRoster *roster = BTranslatorRoster::Default();
	roster->StartWatching(this);

	Show();
}
DataTranslationsWindow::DataTranslationsWindow()
	:
	BWindow(BRect(0, 0, 550, 350), B_TRANSLATE_SYSTEM_NAME("DataTranslations"),
		B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE
		| B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS)
{
	MoveTo(DataTranslationsSettings::Instance()->WindowCorner());

	_SetupViews();

	// Make sure that the window isn't positioned off screen
	BScreen screen;
	BRect screenFrame = screen.Frame();
	if (!screenFrame.Contains(Frame()))
		CenterOnScreen();

	BTranslatorRoster* roster = BTranslatorRoster::Default();
	roster->StartWatching(this);

	Show();
}
Beispiel #26
0
// Reads the installed translators and adds them to our BListView
status_t
DataTranslationsWindow::_PopulateListView()
{
	BTranslatorRoster *roster = BTranslatorRoster::Default();

	// Get all Translators on the system. Gives us the number of translators
	// installed in num_translators and a reference to the first one
	int32 numTranslators;
	translator_id *translators = NULL;
	roster->GetAllTranslators(&translators, &numTranslators);

	for (int32 i = 0; i < numTranslators; i++) {
		// Getting the first three Infos: Name, Info & Version
		int32 version;
		const char *name, *info;
		roster->GetTranslatorInfo(translators[i], &name, &info, &version);
		fTranslatorListView->AddItem(new TranslatorItem(translators[i], name));
	}

	delete[] translators;
	return B_OK;
}
status_t
DataTranslationsWindow::_GetTranslatorInfo(int32 id, const char*& name,
	const char*& info, int32& version, BPath& path)
{
	// Returns information about the translator with the given id

	if (id < 0)
		return B_BAD_VALUE;

	BTranslatorRoster* roster = BTranslatorRoster::Default();
	if (roster->GetTranslatorInfo(id, &name, &info, &version) != B_OK)
		return B_ERROR;

	// Get the translator's path
	entry_ref ref;
	if (roster->GetRefFor(id, &ref) == B_OK) {
		BEntry entry(&ref);
		path.SetTo(&entry);
	} else
		path.Unset();

	return B_OK;
}
Beispiel #28
0
void
PictureView::_HandleDrop(BMessage* msg)
{
	entry_ref dirRef;
	BString name, type;
	bool saveToFile = msg->FindString("be:filetypes", &type) == B_OK
		&& msg->FindRef("directory", &dirRef) == B_OK
		&& msg->FindString("name", &name) == B_OK;

	bool sendInMessage = !saveToFile
		&& msg->FindString("be:types", &type) == B_OK;

	if (!sendInMessage && !saveToFile)
		return;

	BBitmap* bitmap = fPicture;
	if (bitmap == NULL)
		return;

	BTranslatorRoster* roster = BTranslatorRoster::Default();
	if (roster == NULL)
		return;

	BBitmapStream stream(bitmap);

	// find translation format we're asked for
	translator_info* outInfo;
	int32 outNumInfo;
	bool found = false;
	translation_format format;

	if (roster->GetTranslators(&stream, NULL, &outInfo, &outNumInfo) == B_OK) {
		for (int32 i = 0; i < outNumInfo; i++) {
			const translation_format* formats;
			int32 formatCount;
			roster->GetOutputFormats(outInfo[i].translator, &formats,
					&formatCount);
			for (int32 j = 0; j < formatCount; j++) {
				if (strcmp(formats[j].MIME, type.String()) == 0) {
					format = formats[j];
					found = true;
					break;
				}
			}
		}
	}

	if (!found) {
		stream.DetachBitmap(&bitmap);
		return;
	}

	if (sendInMessage) {

		BMessage reply(B_MIME_DATA);
		BMallocIO memStream;
		if (roster->Translate(&stream, NULL, NULL, &memStream,
			format.type) == B_OK) {
			reply.AddData(format.MIME, B_MIME_TYPE, memStream.Buffer(),
				memStream.BufferLength());
			msg->SendReply(&reply);
		}

	} else {

		BDirectory dir(&dirRef);
		BFile file(&dir, name.String(), B_WRITE_ONLY | B_CREATE_FILE
			| B_ERASE_FILE);

		if (file.InitCheck() == B_OK
			&& roster->Translate(&stream, NULL, NULL, &file,
				format.type) == B_OK) {
			BNodeInfo nodeInfo(&file);
			if (nodeInfo.InitCheck() == B_OK)
				nodeInfo.SetType(type.String());
		} else {
			BString text = B_TRANSLATE("The file '%name%' could not "
				"be written.");
			text.ReplaceFirst("%name%", name);
			BAlert* alert = new BAlert(B_TRANSLATE("Error"), text.String(),
				B_TRANSLATE("OK"), NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
			alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
			alert->Go();
		}
	}

	// Detach, as we don't want our fPicture to be deleted
	stream.DetachBitmap(&bitmap);
}
void
DataTranslationsApplication::RefsReceived(BMessage* message)
{
	BTranslatorRoster* roster = BTranslatorRoster::Default();

	BPath path;
	status_t status = find_directory(B_USER_ADDONS_DIRECTORY, &path, true);
	if (status != B_OK) {
		_InstallError("translator", status);
		return;
	}

	BDirectory target;
	status = target.SetTo(path.Path());
	if (status == B_OK) {
		if (!target.Contains("Translators"))
			status = target.CreateDirectory("Translators", &target);
		else
			status = target.SetTo(&target, "Translators");
	}
	if (status != B_OK) {
		_InstallError("translator", status);
		return;
	}

	int32 i = 0;
	entry_ref ref;
	while (message->FindRef("refs", i++, &ref) == B_OK) {
		if (!roster->IsTranslator(&ref)) {
			_NoTranslatorError(ref.name);
			continue;
		}

		BEntry entry(&ref, true);
		status = entry.InitCheck();
		if (status != B_OK) {
			_InstallError(ref.name, status);
			continue;
		}

		if (target.Contains(ref.name)) {
			BString string(
				B_TRANSLATE("An item named '%name' already exists in the "
				"Translators folder! Shall the existing translator be "
				"overwritten?"));
			string.ReplaceAll("%name", ref.name);

			BAlert* alert = new BAlert(B_TRANSLATE("DataTranslations - Note"),
				string.String(), B_TRANSLATE("Cancel"),
				B_TRANSLATE("Overwrite"));
			alert->SetShortcut(0, B_ESCAPE);
			if (alert->Go() != 1)
				continue;

			// the original file will be replaced
		}

		// find out whether we need to copy it or not

		status = _Install(target, entry);
		if (status == B_OK) {
			BAlert* alert = new BAlert(B_TRANSLATE("DataTranslations - Note"),
				B_TRANSLATE("The new translator has been installed "
					"successfully."), B_TRANSLATE("OK"));
			alert->Go(NULL);
		} else
			_InstallError(ref.name, status);
	}
}
DataTranslationsWindow::~DataTranslationsWindow()
{
	BTranslatorRoster* roster = BTranslatorRoster::Default();
	roster->StopWatching(this);
}