nsresult nsIconChannel::Init(nsIURI* aURI) { nsCOMPtr<nsIMozIconURI> iconURI = do_QueryInterface(aURI); NS_ASSERTION(iconURI, "URI is not an nsIMozIconURI"); nsCAutoString stockIcon; iconURI->GetStockIcon(stockIcon); if (stockIcon.IsEmpty()) { #ifdef MOZ_ENABLE_GNOMEUI return InitWithGnome(iconURI); #else return NS_ERROR_NOT_AVAILABLE; #endif } nsCAutoString iconSizeString; iconURI->GetIconSize(iconSizeString); nsCAutoString iconStateString; iconURI->GetIconState(iconStateString); GtkIconSize icon_size = moz_gtk_icon_size(iconSizeString.get()); ensure_stock_image_widget(); gboolean sensitive = strcmp(iconStateString.get(), "disabled"); gtk_widget_set_sensitive (gStockImageWidget, sensitive); GdkPixbuf *icon = gtk_widget_render_icon(gStockImageWidget, stockIcon.get(), icon_size, NULL); #if GTK_CHECK_VERSION(2,4,0) if (!icon) { ensure_icon_factory(); GtkIconSet *icon_set = gtk_icon_set_new(); GtkIconSource *icon_source = gtk_icon_source_new(); gtk_icon_source_set_icon_name(icon_source, stockIcon.get()); gtk_icon_set_add_source(icon_set, icon_source); gtk_icon_factory_add(gIconFactory, stockIcon.get(), icon_set); gtk_icon_set_unref(icon_set); gtk_icon_source_free(icon_source); icon = gtk_widget_render_icon(gStockImageWidget, stockIcon.get(), icon_size, NULL); } #endif if (!icon) return NS_ERROR_NOT_AVAILABLE; nsresult rv = moz_gdk_pixbuf_to_channel(icon, iconURI, getter_AddRefs(mRealChannel)); g_object_unref(icon); return rv; }
static int32_t GetIconSize(nsIMozIconURI* aIconURI) { nsAutoCString iconSizeString; aIconURI->GetIconSize(iconSizeString); if (iconSizeString.IsEmpty()) { uint32_t size; mozilla::DebugOnly<nsresult> rv = aIconURI->GetImageSize(&size); NS_ASSERTION(NS_SUCCEEDED(rv), "GetImageSize failed"); return size; } int size; GtkIconSize icon_size = moz_gtk_icon_size(iconSizeString.get()); gtk_icon_size_lookup(icon_size, &size, nullptr); return size; }
static PRInt32 GetIconSize(nsIMozIconURI *aIconURI) { nsCAutoString iconSizeString; aIconURI->GetIconSize(iconSizeString); if (iconSizeString.IsEmpty()) { PRUint32 size; mozilla::DebugOnly<nsresult> rv = aIconURI->GetImageSize(&size); NS_ASSERTION(NS_SUCCEEDED(rv), "GetImageSize failed"); return size; } else { int size; GtkIconSize icon_size = moz_gtk_icon_size(iconSizeString.get()); gtk_icon_size_lookup(icon_size, &size, NULL); return size; } }
nsresult nsIconChannel::Init(nsIURI* aURI) { nsCOMPtr<nsIMozIconURI> iconURI = do_QueryInterface(aURI); NS_ASSERTION(iconURI, "URI is not an nsIMozIconURI"); nsAutoCString stockIcon; iconURI->GetStockIcon(stockIcon); if (stockIcon.IsEmpty()) { #ifdef MOZ_ENABLE_GNOMEUI return InitWithGnome(iconURI); #else #ifdef MOZ_ENABLE_GIO return InitWithGIO(iconURI); #else return NS_ERROR_NOT_AVAILABLE; #endif #endif } // Search for stockIcon nsAutoCString iconSizeString; iconURI->GetIconSize(iconSizeString); nsAutoCString iconStateString; iconURI->GetIconState(iconStateString); GtkIconSize icon_size = moz_gtk_icon_size(iconSizeString.get()); GtkStateType state = iconStateString.EqualsLiteral("disabled") ? GTK_STATE_INSENSITIVE : GTK_STATE_NORMAL; // First lookup the icon by stock id and text direction. GtkTextDirection direction = GTK_TEXT_DIR_NONE; if (StringEndsWith(stockIcon, NS_LITERAL_CSTRING("-ltr"))) { direction = GTK_TEXT_DIR_LTR; } else if (StringEndsWith(stockIcon, NS_LITERAL_CSTRING("-rtl"))) { direction = GTK_TEXT_DIR_RTL; } bool forceDirection = direction != GTK_TEXT_DIR_NONE; nsAutoCString stockID; bool useIconName = false; if (!forceDirection) { direction = gtk_widget_get_default_direction(); stockID = stockIcon; } else { // GTK versions < 2.22 use icon names from concatenating stock id with // -(rtl|ltr), which is how the moz-icon stock name is interpreted here. stockID = Substring(stockIcon, 0, stockIcon.Length() - 4); // However, if we lookup bidi icons by the stock name, then GTK versions // >= 2.22 will use a bidi lookup convention that most icon themes do not // yet follow. Therefore, we first check to see if the theme supports the // old icon name as this will have bidi support (if found). GtkIconTheme *icon_theme = gtk_icon_theme_get_default(); // Micking what gtk_icon_set_render_icon does with sizes, though it's not // critical as icons will be scaled to suit size. It just means we follow // the same pathes and so share caches. gint width, height; if (gtk_icon_size_lookup(icon_size, &width, &height)) { gint size = NS_MIN(width, height); // We use gtk_icon_theme_lookup_icon() without // GTK_ICON_LOOKUP_USE_BUILTIN instead of gtk_icon_theme_has_icon() so // we don't pick up fallback icons added by distributions for backward // compatibility. GtkIconInfo *icon = gtk_icon_theme_lookup_icon(icon_theme, stockIcon.get(), size, (GtkIconLookupFlags)0); if (icon) { useIconName = true; gtk_icon_info_free(icon); } } } ensure_stock_image_widget(); GtkStyle *style = gtk_widget_get_style(gStockImageWidget); GtkIconSet *icon_set = NULL; if (!useIconName) { icon_set = gtk_style_lookup_icon_set(style, stockID.get()); } if (!icon_set) { // Either we have choosen icon-name lookup for a bidi icon, or stockIcon is // not a stock id so we assume it is an icon name. useIconName = true; // Creating a GtkIconSet is a convenient way to allow the style to // render the icon, possibly with variations suitable for insensitive // states. icon_set = gtk_icon_set_new(); GtkIconSource *icon_source = gtk_icon_source_new(); gtk_icon_source_set_icon_name(icon_source, stockIcon.get()); gtk_icon_set_add_source(icon_set, icon_source); gtk_icon_source_free(icon_source); } GdkPixbuf *icon = gtk_icon_set_render_icon (icon_set, style, direction, state, icon_size, gStockImageWidget, NULL); if (useIconName) { gtk_icon_set_unref(icon_set); } // According to documentation, gtk_icon_set_render_icon() never returns // NULL, but it does return NULL when we have the problem reported here: // https://bugzilla.gnome.org/show_bug.cgi?id=629878#c13 if (!icon) return NS_ERROR_NOT_AVAILABLE; nsresult rv = moz_gdk_pixbuf_to_channel(icon, iconURI, getter_AddRefs(mRealChannel)); g_object_unref(icon); return rv; }
nsresult nsIconChannel::InitWithGnome(nsIMozIconURI *aIconURI) { nsresult rv; if (NS_FAILED(ensure_libgnomeui()) || NS_FAILED(ensure_libgnome()) || NS_FAILED(ensure_libgnomevfs())) { gTriedToLoadGnomeLibs = PR_TRUE; return NS_ERROR_NOT_AVAILABLE; } gTriedToLoadGnomeLibs = PR_TRUE; if (!_gnome_program_get()) { // Get the brandShortName from the string bundle to pass to GNOME // as the application name. This may be used for things such as // the title of grouped windows in the panel. nsCOMPtr<nsIStringBundleService> bundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID); NS_ASSERTION(bundleService, "String bundle service must be present!"); nsCOMPtr<nsIStringBundle> bundle; bundleService->CreateBundle("chrome://branding/locale/brand.properties", getter_AddRefs(bundle)); nsAutoString appName; if (bundle) { bundle->GetStringFromName(NS_LITERAL_STRING("brandShortName").get(), getter_Copies(appName)); } else { NS_WARNING("brand.properties not present, using default application name"); appName.Assign(NS_LITERAL_STRING("Gecko")); } char* empty[] = { "" }; _gnome_init(NS_ConvertUTF16toUTF8(appName).get(), "1.0", 1, empty, NULL, 0, NULL); } nsCAutoString iconSizeString; aIconURI->GetIconSize(iconSizeString); PRUint32 iconSize; if (iconSizeString.IsEmpty()) { rv = aIconURI->GetImageSize(&iconSize); NS_ASSERTION(NS_SUCCEEDED(rv), "GetImageSize failed"); } else { int size; GtkIconSize icon_size = moz_gtk_icon_size(iconSizeString.get()); gtk_icon_size_lookup(icon_size, &size, NULL); iconSize = size; } nsCAutoString type; aIconURI->GetContentType(type); GnomeVFSFileInfo fileInfo = {0}; fileInfo.refcount = 1; // In case some GnomeVFS function addrefs and releases it nsCAutoString spec; nsCOMPtr<nsIURL> url; rv = aIconURI->GetIconURL(getter_AddRefs(url)); if (url) { url->GetAsciiSpec(spec); // Only ask gnome-vfs for a GnomeVFSFileInfo for file: uris, to avoid a // network request PRBool isFile; if (NS_SUCCEEDED(url->SchemeIs("file", &isFile)) && isFile) { _gnome_vfs_get_file_info(spec.get(), &fileInfo, GNOME_VFS_FILE_INFO_DEFAULT); } else { // The filename we get is UTF-8-compatible, which matches gnome expectations. // See also: http://lists.gnome.org/archives/gnome-vfs-list/2004-March/msg00049.html // "Whenever we can detect the charset used for the URI type we try to // convert it to/from utf8 automatically inside gnome-vfs." // I'll interpret that as "otherwise, this field is random junk". nsCAutoString name; url->GetFileName(name); fileInfo.name = g_strdup(name.get()); if (!type.IsEmpty()) { fileInfo.valid_fields = GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE; fileInfo.mime_type = g_strdup(type.get()); } } } if (type.IsEmpty()) { nsCOMPtr<nsIMIMEService> ms(do_GetService("@mozilla.org/mime;1")); if (ms) { nsCAutoString fileExt; aIconURI->GetFileExtension(fileExt); ms->GetTypeFromExtension(fileExt, type); } } // Get the icon theme if (!gIconTheme) { gIconTheme = _gnome_icon_theme_new(); if (!gIconTheme) { _gnome_vfs_file_info_clear(&fileInfo); return NS_ERROR_NOT_AVAILABLE; } } char* name = _gnome_icon_lookup(gIconTheme, NULL, spec.get(), NULL, &fileInfo, type.get(), GNOME_ICON_LOOKUP_FLAGS_NONE, NULL); _gnome_vfs_file_info_clear(&fileInfo); if (!name) return NS_ERROR_NOT_AVAILABLE; // Get the default theme associated with the screen // Do NOT free. GtkIconTheme *theme = gtk_icon_theme_get_default(); if (!theme) { g_free(name); return NS_ERROR_UNEXPECTED; } GError *err = nsnull; GdkPixbuf* buf = gtk_icon_theme_load_icon(theme, name, iconSize, (GtkIconLookupFlags)0, &err); g_free(name); if (!buf) { if (err) g_error_free(err); return NS_ERROR_UNEXPECTED; } GdkPixbuf* scaled = buf; if (gdk_pixbuf_get_width(buf) != iconSize && gdk_pixbuf_get_height(buf) != iconSize) { // scale... scaled = gdk_pixbuf_scale_simple(buf, iconSize, iconSize, GDK_INTERP_BILINEAR); g_object_unref(buf); if (!scaled) return NS_ERROR_OUT_OF_MEMORY; } // XXX Respect icon state rv = moz_gdk_pixbuf_to_channel(scaled, aIconURI, getter_AddRefs(mRealChannel)); g_object_unref(scaled); return rv; }