bool TRefFilter::Filter(const entry_ref *theRef, BNode *theNode, struct stat_beos *theStat, const char *mimetype) { // Create BEntry and traverse to get source ref BEntry entry(theRef, true); if (entry.InitCheck() != B_OK) { ERROR("TRefFilter::Filter() - Error creating BEntry -\n"); return false; } // Create a node from ref BNode localNode(theRef); if (localNode.InitCheck() != B_OK) { ERROR("TRefFilter::Filter() - Error creating BNode -\n"); return false; } // Get node info BNodeInfo nodeInfo(&localNode); if (nodeInfo.InitCheck() != B_OK) { ERROR("TRefFilter::Filter() - Error getting BNodeInfo -\n"); return false; } // Get stat info struct stat st; if (entry.GetStat(&st) != B_OK) { ERROR("TRefFilter::Filter() - Error getting stat info -\n"); return false; } switch(m_FilterType) { case kAudioFilter: { // Allow directories if (S_ISDIR(st.st_mode)) return true; // Allow audio if (IsAudio(nodeInfo)) return true; } break; case kAudioAiffFilter: { // Allow directories if (S_ISDIR(st.st_mode)) return true; // Allow audio if (IsAudioAiff(nodeInfo)) return true; } break; case kImageFilter: { // Allow directories if (S_ISDIR(st.st_mode)) return true; // Allow images if (IsImage(nodeInfo)) return true; } break; case kTextFilter: { // Allow directories if (S_ISDIR(st.st_mode)) return true; // Allow text if (IsText(nodeInfo)) return true; } break; case kVideoFilter: { // Allow directories if (S_ISDIR(st.st_mode)) return true; // Allow video if (IsVideo(nodeInfo)) return true; } break; case kCueSheetFilter: { // Allow directories if (S_ISDIR(st.st_mode)) return true; // Allow CueSheets if (IsCueSheet(nodeInfo)) return true; } break; case kDirectoryFilter: { // Allow directories if (S_ISDIR(st.st_mode)) return true; } break; default: return true; } // Fail if we get here return false; }
nsresult nsIconChannel::MakeInputStream(nsIInputStream** _retval, PRBool nonBlocking) { nsXPIDLCString contentType; nsCAutoString filePath; nsCAutoString fileExtension; nsCOMPtr<nsIFile> localFile; // File we want an icon for PRUint32 desiredImageSize; nsresult rv = ExtractIconInfoFromUrl(getter_AddRefs(localFile), &desiredImageSize, contentType, fileExtension); NS_ENSURE_SUCCESS(rv, rv); PRUint32 iconSize = 16; if (desiredImageSize > 16) iconSize = 32; PRUint32 alphaBytesPerRow = (iconSize / 8); if (iconSize % 32 != 0) alphaBytesPerRow = ((iconSize / 32) + 1) * 4; PRBool fileExists = PR_FALSE; if (localFile) { localFile->GetNativePath(filePath); localFile->Exists(&fileExists); } // Get the native icon. // 1) If it is for an actual local file, BNodeInfo::GetTrackerIcon. // 2) If the local file does not exist, use the content type // and BMimeType::GetIcon BBitmap nativeIcon(BRect(0, 0, iconSize - 1, iconSize - 1), B_CMAP8); if (!nativeIcon.IsValid()) return NS_ERROR_OUT_OF_MEMORY; PRBool gotBitmap = PR_FALSE; if (fileExists) { BNode localNode(filePath.get()); // BeOS doesn't MIME type foreign files immediately - // If there is no type attribute then we can force an identify if (localNode.ReadAttr("BEOS:TYPE", B_STRING_TYPE, 0, NULL, 0) != B_OK) update_mime_info(filePath.get(), 0, 1, 1); BNodeInfo localNodeInfo(&localNode); if (iconSize == 16) { if (localNodeInfo.GetTrackerIcon(&nativeIcon, B_MINI_ICON) == B_OK) gotBitmap = PR_TRUE; } else { if (localNodeInfo.GetTrackerIcon(&nativeIcon, B_LARGE_ICON) == B_OK) gotBitmap = PR_TRUE; } } // If we haven't got a bitmap yet, use the content type if (!gotBitmap) { // If no content type specified, use mozilla's mime service to guess a mime type if (contentType.IsEmpty()) { nsCOMPtr<nsIMIMEService> mimeService (do_GetService("@mozilla.org/mime;1", &rv)); if (NS_SUCCEEDED(rv)) mimeService->GetTypeFromExtension(fileExtension, contentType); // If unrecognised extension - set to generic file if (contentType.IsEmpty()) contentType = "application/octet-stream"; } // Create BeOS-Native MIME type info - if unheard of, set to generic file BMimeType mimeType(contentType.get()); if (!mimeType.IsInstalled()) mimeType.SetTo("application/octet-stream"); if (iconSize == 16) { if (mimeType.GetIcon(&nativeIcon, B_MINI_ICON) == B_OK) gotBitmap = PR_TRUE; } else { if (mimeType.GetIcon(&nativeIcon, B_LARGE_ICON) == B_OK) gotBitmap = PR_TRUE; } } if (!gotBitmap) return NS_ERROR_NOT_AVAILABLE; BScreen mainScreen(B_MAIN_SCREEN_ID); if (!mainScreen.IsValid()) return NS_ERROR_NOT_AVAILABLE; // Got a bitmap and color space info - convert data to mozilla's icon format PRUint32 iconLength = 2 + iconSize * iconSize * 4; uint8 *buffer = new uint8[iconLength]; if (!buffer) return NS_ERROR_OUT_OF_MEMORY; uint8* destByte = buffer; *(destByte++) = iconSize; *(destByte++) = iconSize; // RGB data uint8* sourceByte = (uint8*)nativeIcon.Bits(); for(PRUint32 iconRow = 0; iconRow < iconSize; iconRow++) { sourceByte = (uint8*)nativeIcon.Bits() + nativeIcon.BytesPerRow() * iconRow; for(PRUint32 iconCol = 0; iconCol < iconSize; iconCol++) { if (*sourceByte != B_TRANSPARENT_MAGIC_CMAP8) { rgb_color colorVal = mainScreen.ColorForIndex(*sourceByte); #ifdef IS_LITTLE_ENDIAN *(destByte++) = colorVal.blue; *(destByte++) = colorVal.green; *(destByte++) = colorVal.red; *(destByte++) = uint8(255); #else *(destByte++) = uint8(255); *(destByte++) = colorVal.red; *(destByte++) = colorVal.green; *(destByte++) = colorVal.blue; #endif } else { *destByte++ = 0; *destByte++ = 0; *destByte++ = 0; *destByte++ = 0; } // original code had a conditional here: // if (iconCol < iconSize - 1) // Leaving this comment in case complications arise later sourceByte++; } } NS_ASSERTION(buffer + iconLength == destByte, "size miscalculation"); // Now, create a pipe and stuff our data into it nsCOMPtr<nsIInputStream> inStream; nsCOMPtr<nsIOutputStream> outStream; rv = NS_NewPipe(getter_AddRefs(inStream), getter_AddRefs(outStream), iconLength, iconLength, nonBlocking); if (NS_SUCCEEDED(rv)) { PRUint32 written; rv = outStream->Write((char*)buffer, iconLength, &written); if (NS_SUCCEEDED(rv)) NS_ADDREF(*_retval = inStream); } delete [] buffer; return rv; }