michael@0: /* vim:set ts=2 sw=2 sts=2 cin et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "mozilla/DebugOnly.h" michael@0: #include "mozilla/Endian.h" michael@0: #include michael@0: michael@0: #ifdef MOZ_ENABLE_GNOMEUI michael@0: // Older versions of these headers seem to be missing an extern "C" michael@0: extern "C" { michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: } michael@0: #endif michael@0: #ifdef MOZ_ENABLE_GIO michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: michael@0: #include "nsMimeTypes.h" michael@0: #include "nsIMIMEService.h" michael@0: michael@0: #include "nsIStringBundle.h" michael@0: michael@0: #include "nsNetUtil.h" michael@0: #include "nsIURL.h" michael@0: #include "prlink.h" michael@0: michael@0: #include "nsIconChannel.h" michael@0: michael@0: NS_IMPL_ISUPPORTS(nsIconChannel, michael@0: nsIRequest, michael@0: nsIChannel) michael@0: michael@0: #ifdef MOZ_ENABLE_GNOMEUI michael@0: // These let us have a soft dependency on libgnomeui rather than a hard one. These are just basically the prototypes michael@0: // of the functions in the libraries. michael@0: typedef char* (*_GnomeIconLookup_fn)(GtkIconTheme *icon_theme, GnomeThumbnailFactory *thumbnail_factory, michael@0: const char *file_uri, const char *custom_icon, GnomeVFSFileInfo *file_info, michael@0: const char *mime_type, GnomeIconLookupFlags flags, GnomeIconLookupResultFlags *result); michael@0: typedef GnomeIconTheme* (*_GnomeIconThemeNew_fn)(void); michael@0: typedef int (*_GnomeInit_fn)(const char *app_id, const char *app_version, int argc, char **argv, const struct poptOption *options, michael@0: int flags, poptContext *return_ctx); michael@0: typedef GnomeProgram* (*_GnomeProgramGet_fn)(void); michael@0: typedef GnomeVFSResult (*_GnomeVFSGetFileInfo_fn)(const gchar *text_uri, GnomeVFSFileInfo *info, GnomeVFSFileInfoOptions options); michael@0: typedef void (*_GnomeVFSFileInfoClear_fn)(GnomeVFSFileInfo *info); michael@0: michael@0: static PRLibrary* gLibGnomeUI = nullptr; michael@0: static PRLibrary* gLibGnome = nullptr; michael@0: static PRLibrary* gLibGnomeVFS = nullptr; michael@0: static bool gTriedToLoadGnomeLibs = false; michael@0: michael@0: static _GnomeIconLookup_fn _gnome_icon_lookup = nullptr; michael@0: static _GnomeIconThemeNew_fn _gnome_icon_theme_new = nullptr; michael@0: static _GnomeInit_fn _gnome_init = nullptr; michael@0: static _GnomeProgramGet_fn _gnome_program_get = nullptr; michael@0: static _GnomeVFSGetFileInfo_fn _gnome_vfs_get_file_info = nullptr; michael@0: static _GnomeVFSFileInfoClear_fn _gnome_vfs_file_info_clear = nullptr; michael@0: #endif //MOZ_ENABLE_GNOMEUI michael@0: michael@0: static nsresult michael@0: moz_gdk_pixbuf_to_channel(GdkPixbuf* aPixbuf, nsIURI *aURI, michael@0: nsIChannel **aChannel) michael@0: { michael@0: int width = gdk_pixbuf_get_width(aPixbuf); michael@0: int height = gdk_pixbuf_get_height(aPixbuf); michael@0: NS_ENSURE_TRUE(height < 256 && width < 256 && height > 0 && width > 0 && michael@0: gdk_pixbuf_get_colorspace(aPixbuf) == GDK_COLORSPACE_RGB && michael@0: gdk_pixbuf_get_bits_per_sample(aPixbuf) == 8 && michael@0: gdk_pixbuf_get_has_alpha(aPixbuf) && michael@0: gdk_pixbuf_get_n_channels(aPixbuf) == 4, michael@0: NS_ERROR_UNEXPECTED); michael@0: michael@0: const int n_channels = 4; michael@0: gsize buf_size = 2 + n_channels * height * width; michael@0: uint8_t * const buf = (uint8_t*)NS_Alloc(buf_size); michael@0: NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY); michael@0: uint8_t *out = buf; michael@0: michael@0: *(out++) = width; michael@0: *(out++) = height; michael@0: michael@0: const guchar * const pixels = gdk_pixbuf_get_pixels(aPixbuf); michael@0: int rowextra = gdk_pixbuf_get_rowstride(aPixbuf) - width * n_channels; michael@0: michael@0: // encode the RGB data and the A data michael@0: const guchar * in = pixels; michael@0: for (int y = 0; y < height; ++y, in += rowextra) { michael@0: for (int x = 0; x < width; ++x) { michael@0: uint8_t r = *(in++); michael@0: uint8_t g = *(in++); michael@0: uint8_t b = *(in++); michael@0: uint8_t a = *(in++); michael@0: #define DO_PREMULTIPLY(c_) uint8_t(uint16_t(c_) * uint16_t(a) / uint16_t(255)) michael@0: #if MOZ_LITTLE_ENDIAN michael@0: *(out++) = DO_PREMULTIPLY(b); michael@0: *(out++) = DO_PREMULTIPLY(g); michael@0: *(out++) = DO_PREMULTIPLY(r); michael@0: *(out++) = a; michael@0: #else michael@0: *(out++) = a; michael@0: *(out++) = DO_PREMULTIPLY(r); michael@0: *(out++) = DO_PREMULTIPLY(g); michael@0: *(out++) = DO_PREMULTIPLY(b); michael@0: #endif michael@0: #undef DO_PREMULTIPLY michael@0: } michael@0: } michael@0: michael@0: NS_ASSERTION(out == buf + buf_size, "size miscalculation"); michael@0: michael@0: nsresult rv; michael@0: nsCOMPtr stream = michael@0: do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = stream->AdoptData((char*)buf, buf_size); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = NS_NewInputStreamChannel(aChannel, aURI, stream, michael@0: NS_LITERAL_CSTRING(IMAGE_ICON_MS)); michael@0: return rv; michael@0: } michael@0: michael@0: static GtkWidget *gProtoWindow = nullptr; michael@0: static GtkWidget *gStockImageWidget = nullptr; michael@0: #ifdef MOZ_ENABLE_GNOMEUI michael@0: static GnomeIconTheme *gIconTheme = nullptr; michael@0: #endif //MOZ_ENABLE_GNOMEUI michael@0: michael@0: static void michael@0: ensure_stock_image_widget() michael@0: { michael@0: // Only the style of the GtkImage needs to be used, but the widget is kept michael@0: // to track dynamic style changes. michael@0: if (!gProtoWindow) { michael@0: gProtoWindow = gtk_window_new(GTK_WINDOW_POPUP); michael@0: GtkWidget* protoLayout = gtk_fixed_new(); michael@0: gtk_container_add(GTK_CONTAINER(gProtoWindow), protoLayout); michael@0: michael@0: gStockImageWidget = gtk_image_new(); michael@0: gtk_container_add(GTK_CONTAINER(protoLayout), gStockImageWidget); michael@0: michael@0: gtk_widget_ensure_style(gStockImageWidget); michael@0: } michael@0: } michael@0: michael@0: #ifdef MOZ_ENABLE_GNOMEUI michael@0: static nsresult michael@0: ensure_libgnomeui() michael@0: { michael@0: // Attempt to get the libgnomeui symbol references. We do it this way so that stock icons from Init() michael@0: // don't get held back by InitWithGnome()'s libgnomeui dependency. michael@0: if (!gTriedToLoadGnomeLibs) { michael@0: gLibGnomeUI = PR_LoadLibrary("libgnomeui-2.so.0"); michael@0: if (!gLibGnomeUI) michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: michael@0: _gnome_init = (_GnomeInit_fn)PR_FindFunctionSymbol(gLibGnomeUI, "gnome_init_with_popt_table"); michael@0: _gnome_icon_theme_new = (_GnomeIconThemeNew_fn)PR_FindFunctionSymbol(gLibGnomeUI, "gnome_icon_theme_new"); michael@0: _gnome_icon_lookup = (_GnomeIconLookup_fn)PR_FindFunctionSymbol(gLibGnomeUI, "gnome_icon_lookup"); michael@0: michael@0: if (!_gnome_init || !_gnome_icon_theme_new || !_gnome_icon_lookup) { michael@0: PR_UnloadLibrary(gLibGnomeUI); michael@0: gLibGnomeUI = nullptr; michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: } michael@0: michael@0: if (!gLibGnomeUI) michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: static nsresult michael@0: ensure_libgnome() michael@0: { michael@0: if (!gTriedToLoadGnomeLibs) { michael@0: gLibGnome = PR_LoadLibrary("libgnome-2.so.0"); michael@0: if (!gLibGnome) michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: michael@0: _gnome_program_get = (_GnomeProgramGet_fn)PR_FindFunctionSymbol(gLibGnome, "gnome_program_get"); michael@0: if (!_gnome_program_get) { michael@0: PR_UnloadLibrary(gLibGnome); michael@0: gLibGnome = nullptr; michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: } michael@0: michael@0: if (!gLibGnome) michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: static nsresult michael@0: ensure_libgnomevfs() michael@0: { michael@0: if (!gTriedToLoadGnomeLibs) { michael@0: gLibGnomeVFS = PR_LoadLibrary("libgnomevfs-2.so.0"); michael@0: if (!gLibGnomeVFS) michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: michael@0: _gnome_vfs_get_file_info = (_GnomeVFSGetFileInfo_fn)PR_FindFunctionSymbol(gLibGnomeVFS, "gnome_vfs_get_file_info"); michael@0: _gnome_vfs_file_info_clear = (_GnomeVFSFileInfoClear_fn)PR_FindFunctionSymbol(gLibGnomeVFS, "gnome_vfs_file_info_clear"); michael@0: if (!_gnome_vfs_get_file_info || !_gnome_vfs_file_info_clear) { michael@0: PR_UnloadLibrary(gLibGnomeVFS); michael@0: gLibGnomeVFS = nullptr; michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: } michael@0: michael@0: if (!gLibGnomeVFS) michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: michael@0: return NS_OK; michael@0: } michael@0: #endif //MOZ_ENABLE_GNOMEUI michael@0: michael@0: static GtkIconSize michael@0: moz_gtk_icon_size(const char *name) michael@0: { michael@0: if (strcmp(name, "button") == 0) michael@0: return GTK_ICON_SIZE_BUTTON; michael@0: michael@0: if (strcmp(name, "menu") == 0) michael@0: return GTK_ICON_SIZE_MENU; michael@0: michael@0: if (strcmp(name, "toolbar") == 0) michael@0: return GTK_ICON_SIZE_LARGE_TOOLBAR; michael@0: michael@0: if (strcmp(name, "toolbarsmall") == 0) michael@0: return GTK_ICON_SIZE_SMALL_TOOLBAR; michael@0: michael@0: if (strcmp(name, "dnd") == 0) michael@0: return GTK_ICON_SIZE_DND; michael@0: michael@0: if (strcmp(name, "dialog") == 0) michael@0: return GTK_ICON_SIZE_DIALOG; michael@0: michael@0: return GTK_ICON_SIZE_MENU; michael@0: } michael@0: michael@0: #if defined(MOZ_ENABLE_GNOMEUI) || defined(MOZ_ENABLE_GIO) michael@0: static int32_t michael@0: GetIconSize(nsIMozIconURI *aIconURI) michael@0: { michael@0: nsAutoCString iconSizeString; michael@0: michael@0: aIconURI->GetIconSize(iconSizeString); michael@0: if (iconSizeString.IsEmpty()) { michael@0: uint32_t size; michael@0: mozilla::DebugOnly rv = aIconURI->GetImageSize(&size); michael@0: NS_ASSERTION(NS_SUCCEEDED(rv), "GetImageSize failed"); michael@0: return size; michael@0: } else { michael@0: int size; michael@0: michael@0: GtkIconSize icon_size = moz_gtk_icon_size(iconSizeString.get()); michael@0: gtk_icon_size_lookup(icon_size, &size, nullptr); michael@0: return size; michael@0: } michael@0: } michael@0: michael@0: /* Scale icon buffer to preferred size */ michael@0: static nsresult michael@0: ScaleIconBuf(GdkPixbuf **aBuf, int32_t iconSize) michael@0: { michael@0: // Scale buffer only if width or height differ from preferred size michael@0: if (gdk_pixbuf_get_width(*aBuf) != iconSize && michael@0: gdk_pixbuf_get_height(*aBuf) != iconSize) { michael@0: GdkPixbuf *scaled = gdk_pixbuf_scale_simple(*aBuf, iconSize, iconSize, michael@0: GDK_INTERP_BILINEAR); michael@0: // replace original buffer by scaled michael@0: g_object_unref(*aBuf); michael@0: *aBuf = scaled; michael@0: if (!scaled) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef MOZ_ENABLE_GNOMEUI michael@0: nsresult michael@0: nsIconChannel::InitWithGnome(nsIMozIconURI *aIconURI) michael@0: { michael@0: nsresult rv; michael@0: michael@0: if (NS_FAILED(ensure_libgnomeui()) || NS_FAILED(ensure_libgnome()) || NS_FAILED(ensure_libgnomevfs())) { michael@0: gTriedToLoadGnomeLibs = true; michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: gTriedToLoadGnomeLibs = true; michael@0: michael@0: if (!_gnome_program_get()) { michael@0: // Get the brandShortName from the string bundle to pass to GNOME michael@0: // as the application name. This may be used for things such as michael@0: // the title of grouped windows in the panel. michael@0: nsCOMPtr bundleService = michael@0: do_GetService(NS_STRINGBUNDLE_CONTRACTID); michael@0: michael@0: NS_ASSERTION(bundleService, "String bundle service must be present!"); michael@0: michael@0: nsCOMPtr bundle; michael@0: bundleService->CreateBundle("chrome://branding/locale/brand.properties", michael@0: getter_AddRefs(bundle)); michael@0: nsAutoString appName; michael@0: michael@0: if (bundle) { michael@0: bundle->GetStringFromName(MOZ_UTF16("brandShortName"), michael@0: getter_Copies(appName)); michael@0: } else { michael@0: NS_WARNING("brand.properties not present, using default application name"); michael@0: appName.Assign(NS_LITERAL_STRING("Gecko")); michael@0: } michael@0: michael@0: char* empty[] = { "" }; michael@0: _gnome_init(NS_ConvertUTF16toUTF8(appName).get(), michael@0: "1.0", 1, empty, nullptr, 0, nullptr); michael@0: } michael@0: michael@0: uint32_t iconSize = GetIconSize(aIconURI); michael@0: nsAutoCString type; michael@0: aIconURI->GetContentType(type); michael@0: michael@0: GnomeVFSFileInfo fileInfo = {0}; michael@0: fileInfo.refcount = 1; // In case some GnomeVFS function addrefs and releases it michael@0: michael@0: nsAutoCString spec; michael@0: nsCOMPtr url; michael@0: rv = aIconURI->GetIconURL(getter_AddRefs(url)); michael@0: if (url) { michael@0: url->GetAsciiSpec(spec); michael@0: // Only ask gnome-vfs for a GnomeVFSFileInfo for file: uris, to avoid a michael@0: // network request michael@0: bool isFile; michael@0: if (NS_SUCCEEDED(url->SchemeIs("file", &isFile)) && isFile) { michael@0: _gnome_vfs_get_file_info(spec.get(), &fileInfo, GNOME_VFS_FILE_INFO_DEFAULT); michael@0: } michael@0: else { michael@0: // The filename we get is UTF-8-compatible, which matches gnome expectations. michael@0: // See also: http://lists.gnome.org/archives/gnome-vfs-list/2004-March/msg00049.html michael@0: // "Whenever we can detect the charset used for the URI type we try to michael@0: // convert it to/from utf8 automatically inside gnome-vfs." michael@0: // I'll interpret that as "otherwise, this field is random junk". michael@0: nsAutoCString name; michael@0: url->GetFileName(name); michael@0: fileInfo.name = g_strdup(name.get()); michael@0: michael@0: if (!type.IsEmpty()) { michael@0: fileInfo.valid_fields = GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE; michael@0: fileInfo.mime_type = g_strdup(type.get()); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (type.IsEmpty()) { michael@0: nsCOMPtr ms(do_GetService("@mozilla.org/mime;1")); michael@0: if (ms) { michael@0: nsAutoCString fileExt; michael@0: aIconURI->GetFileExtension(fileExt); michael@0: if (!fileExt.IsEmpty()) { michael@0: ms->GetTypeFromExtension(fileExt, type); michael@0: } michael@0: } michael@0: } michael@0: // Get the icon theme michael@0: if (!gIconTheme) { michael@0: gIconTheme = _gnome_icon_theme_new(); michael@0: michael@0: if (!gIconTheme) { michael@0: _gnome_vfs_file_info_clear(&fileInfo); michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: } michael@0: michael@0: char* name = _gnome_icon_lookup(gIconTheme, nullptr, spec.get(), nullptr, michael@0: &fileInfo, type.get(), michael@0: GNOME_ICON_LOOKUP_FLAGS_NONE, nullptr); michael@0: michael@0: _gnome_vfs_file_info_clear(&fileInfo); michael@0: if (!name) michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: michael@0: // Get the default theme associated with the screen michael@0: // Do NOT free. michael@0: GtkIconTheme *theme = gtk_icon_theme_get_default(); michael@0: if (!theme) { michael@0: g_free(name); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: GError *err = nullptr; michael@0: GdkPixbuf* buf = gtk_icon_theme_load_icon(theme, name, iconSize, (GtkIconLookupFlags)0, &err); michael@0: g_free(name); michael@0: michael@0: if (!buf) { michael@0: if (err) michael@0: g_error_free(err); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: rv = ScaleIconBuf(&buf, iconSize); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = moz_gdk_pixbuf_to_channel(buf, aIconURI, michael@0: getter_AddRefs(mRealChannel)); michael@0: g_object_unref(buf); michael@0: return rv; michael@0: } michael@0: #endif // MOZ_ENABLE_GNOMEUI michael@0: michael@0: #ifdef MOZ_ENABLE_GIO michael@0: nsresult michael@0: nsIconChannel::InitWithGIO(nsIMozIconURI *aIconURI) michael@0: { michael@0: GIcon *icon = nullptr; michael@0: nsCOMPtr fileURI; michael@0: michael@0: // Read icon content michael@0: aIconURI->GetIconURL(getter_AddRefs(fileURI)); michael@0: michael@0: // Get icon for file specified by URI michael@0: if (fileURI) { michael@0: bool isFile; michael@0: nsAutoCString spec; michael@0: fileURI->GetAsciiSpec(spec); michael@0: if (NS_SUCCEEDED(fileURI->SchemeIs("file", &isFile)) && isFile) { michael@0: GFile *file = g_file_new_for_uri(spec.get()); michael@0: GFileInfo *fileInfo = g_file_query_info(file, michael@0: G_FILE_ATTRIBUTE_STANDARD_ICON, michael@0: G_FILE_QUERY_INFO_NONE, michael@0: nullptr, nullptr); michael@0: g_object_unref(file); michael@0: if (fileInfo) { michael@0: // icon from g_content_type_get_icon doesn't need unref michael@0: icon = g_file_info_get_icon(fileInfo); michael@0: if (icon) michael@0: g_object_ref(icon); michael@0: g_object_unref(fileInfo); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Try to get icon by using MIME type michael@0: if (!icon) { michael@0: nsAutoCString type; michael@0: aIconURI->GetContentType(type); michael@0: // Try to get MIME type from file extension by using nsIMIMEService michael@0: if (type.IsEmpty()) { michael@0: nsCOMPtr ms(do_GetService("@mozilla.org/mime;1")); michael@0: if (ms) { michael@0: nsAutoCString fileExt; michael@0: aIconURI->GetFileExtension(fileExt); michael@0: ms->GetTypeFromExtension(fileExt, type); michael@0: } michael@0: } michael@0: char *ctype = nullptr; // character representation of content type michael@0: if (!type.IsEmpty()) { michael@0: ctype = g_content_type_from_mime_type(type.get()); michael@0: } michael@0: if (ctype) { michael@0: icon = g_content_type_get_icon(ctype); michael@0: g_free(ctype); michael@0: } michael@0: } michael@0: michael@0: // Get default icon theme michael@0: GtkIconTheme *iconTheme = gtk_icon_theme_get_default(); michael@0: GtkIconInfo *iconInfo = nullptr; michael@0: // Get icon size michael@0: int32_t iconSize = GetIconSize(aIconURI); michael@0: michael@0: if (icon) { michael@0: // Use icon and theme to get GtkIconInfo michael@0: iconInfo = gtk_icon_theme_lookup_by_gicon(iconTheme, michael@0: icon, iconSize, michael@0: (GtkIconLookupFlags)0); michael@0: g_object_unref(icon); michael@0: } michael@0: michael@0: if (!iconInfo) { michael@0: // Mozilla's mimetype lookup failed. Try the "unknown" icon. michael@0: iconInfo = gtk_icon_theme_lookup_icon(iconTheme, michael@0: "unknown", iconSize, michael@0: (GtkIconLookupFlags)0); michael@0: if (!iconInfo) { michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: } michael@0: michael@0: // Create a GdkPixbuf buffer containing icon and scale it michael@0: GdkPixbuf* buf = gtk_icon_info_load_icon(iconInfo, nullptr); michael@0: gtk_icon_info_free(iconInfo); michael@0: if (!buf) { michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: nsresult rv = ScaleIconBuf(&buf, iconSize); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = moz_gdk_pixbuf_to_channel(buf, aIconURI, michael@0: getter_AddRefs(mRealChannel)); michael@0: g_object_unref(buf); michael@0: return rv; michael@0: } michael@0: #endif // MOZ_ENABLE_GIO michael@0: michael@0: nsresult michael@0: nsIconChannel::Init(nsIURI* aURI) michael@0: { michael@0: nsCOMPtr iconURI = do_QueryInterface(aURI); michael@0: NS_ASSERTION(iconURI, "URI is not an nsIMozIconURI"); michael@0: michael@0: nsAutoCString stockIcon; michael@0: iconURI->GetStockIcon(stockIcon); michael@0: if (stockIcon.IsEmpty()) { michael@0: #ifdef MOZ_ENABLE_GNOMEUI michael@0: return InitWithGnome(iconURI); michael@0: #else michael@0: #ifdef MOZ_ENABLE_GIO michael@0: return InitWithGIO(iconURI); michael@0: #else michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: #endif michael@0: #endif michael@0: } michael@0: michael@0: // Search for stockIcon michael@0: nsAutoCString iconSizeString; michael@0: iconURI->GetIconSize(iconSizeString); michael@0: michael@0: nsAutoCString iconStateString; michael@0: iconURI->GetIconState(iconStateString); michael@0: michael@0: GtkIconSize icon_size = moz_gtk_icon_size(iconSizeString.get()); michael@0: GtkStateType state = iconStateString.EqualsLiteral("disabled") ? michael@0: GTK_STATE_INSENSITIVE : GTK_STATE_NORMAL; michael@0: michael@0: // First lookup the icon by stock id and text direction. michael@0: GtkTextDirection direction = GTK_TEXT_DIR_NONE; michael@0: if (StringEndsWith(stockIcon, NS_LITERAL_CSTRING("-ltr"))) { michael@0: direction = GTK_TEXT_DIR_LTR; michael@0: } else if (StringEndsWith(stockIcon, NS_LITERAL_CSTRING("-rtl"))) { michael@0: direction = GTK_TEXT_DIR_RTL; michael@0: } michael@0: michael@0: bool forceDirection = direction != GTK_TEXT_DIR_NONE; michael@0: nsAutoCString stockID; michael@0: bool useIconName = false; michael@0: if (!forceDirection) { michael@0: direction = gtk_widget_get_default_direction(); michael@0: stockID = stockIcon; michael@0: } else { michael@0: // GTK versions < 2.22 use icon names from concatenating stock id with michael@0: // -(rtl|ltr), which is how the moz-icon stock name is interpreted here. michael@0: stockID = Substring(stockIcon, 0, stockIcon.Length() - 4); michael@0: // However, if we lookup bidi icons by the stock name, then GTK versions michael@0: // >= 2.22 will use a bidi lookup convention that most icon themes do not michael@0: // yet follow. Therefore, we first check to see if the theme supports the michael@0: // old icon name as this will have bidi support (if found). michael@0: GtkIconTheme *icon_theme = gtk_icon_theme_get_default(); michael@0: // Micking what gtk_icon_set_render_icon does with sizes, though it's not michael@0: // critical as icons will be scaled to suit size. It just means we follow michael@0: // the same pathes and so share caches. michael@0: gint width, height; michael@0: if (gtk_icon_size_lookup(icon_size, &width, &height)) { michael@0: gint size = std::min(width, height); michael@0: // We use gtk_icon_theme_lookup_icon() without michael@0: // GTK_ICON_LOOKUP_USE_BUILTIN instead of gtk_icon_theme_has_icon() so michael@0: // we don't pick up fallback icons added by distributions for backward michael@0: // compatibility. michael@0: GtkIconInfo *icon = michael@0: gtk_icon_theme_lookup_icon(icon_theme, stockIcon.get(), michael@0: size, (GtkIconLookupFlags)0); michael@0: if (icon) { michael@0: useIconName = true; michael@0: gtk_icon_info_free(icon); michael@0: } michael@0: } michael@0: } michael@0: michael@0: ensure_stock_image_widget(); michael@0: GtkStyle *style = gtk_widget_get_style(gStockImageWidget); michael@0: GtkIconSet *icon_set = nullptr; michael@0: if (!useIconName) { michael@0: icon_set = gtk_style_lookup_icon_set(style, stockID.get()); michael@0: } michael@0: michael@0: if (!icon_set) { michael@0: // Either we have choosen icon-name lookup for a bidi icon, or stockIcon is michael@0: // not a stock id so we assume it is an icon name. michael@0: useIconName = true; michael@0: // Creating a GtkIconSet is a convenient way to allow the style to michael@0: // render the icon, possibly with variations suitable for insensitive michael@0: // states. michael@0: icon_set = gtk_icon_set_new(); michael@0: GtkIconSource *icon_source = gtk_icon_source_new(); michael@0: michael@0: gtk_icon_source_set_icon_name(icon_source, stockIcon.get()); michael@0: gtk_icon_set_add_source(icon_set, icon_source); michael@0: gtk_icon_source_free(icon_source); michael@0: } michael@0: michael@0: GdkPixbuf *icon = michael@0: gtk_icon_set_render_icon(icon_set, style, direction, state, michael@0: icon_size, gStockImageWidget, nullptr); michael@0: if (useIconName) { michael@0: gtk_icon_set_unref(icon_set); michael@0: } michael@0: michael@0: // According to documentation, gtk_icon_set_render_icon() never returns michael@0: // nullptr, but it does return nullptr when we have the problem reported michael@0: // here: https://bugzilla.gnome.org/show_bug.cgi?id=629878#c13 michael@0: if (!icon) michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: michael@0: nsresult rv = moz_gdk_pixbuf_to_channel(icon, iconURI, michael@0: getter_AddRefs(mRealChannel)); michael@0: michael@0: g_object_unref(icon); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: void michael@0: nsIconChannel::Shutdown() { michael@0: if (gProtoWindow) { michael@0: gtk_widget_destroy(gProtoWindow); michael@0: gProtoWindow = nullptr; michael@0: gStockImageWidget = nullptr; michael@0: } michael@0: #ifdef MOZ_ENABLE_GNOMEUI michael@0: if (gIconTheme) { michael@0: g_object_unref(G_OBJECT(gIconTheme)); michael@0: gIconTheme = nullptr; michael@0: } michael@0: gTriedToLoadGnomeLibs = false; michael@0: if (gLibGnomeUI) { michael@0: PR_UnloadLibrary(gLibGnomeUI); michael@0: gLibGnomeUI = nullptr; michael@0: } michael@0: if (gLibGnome) { michael@0: PR_UnloadLibrary(gLibGnome); michael@0: gLibGnome = nullptr; michael@0: } michael@0: if (gLibGnomeVFS) { michael@0: PR_UnloadLibrary(gLibGnomeVFS); michael@0: gLibGnomeVFS = nullptr; michael@0: } michael@0: #endif //MOZ_ENABLE_GNOMEUI michael@0: }