2007-03-22 10:30:00 -07:00
|
|
|
/* vim:set ts=2 sw=2 sts=2 cin et: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is the Mozilla icon channel for gnome.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Christian Biesinger <cbiesinger@web.de>.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2004
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2010-07-15 01:14:11 -07:00
|
|
|
* Nils Maier <MaierMan@web.de>
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2007-12-07 16:08:09 -08:00
|
|
|
#ifdef MOZ_ENABLE_GNOMEUI
|
2007-03-22 10:30:00 -07:00
|
|
|
// Older versions of these headers seem to be missing an extern "C"
|
|
|
|
extern "C" {
|
|
|
|
#include <libgnome/libgnome.h>
|
|
|
|
#include <libgnomeui/gnome-icon-theme.h>
|
|
|
|
#include <libgnomeui/gnome-icon-lookup.h>
|
|
|
|
|
|
|
|
#include <libgnomevfs/gnome-vfs-file-info.h>
|
|
|
|
#include <libgnomevfs/gnome-vfs-ops.h>
|
|
|
|
}
|
2007-12-07 16:08:09 -08:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-01-21 07:24:46 -08:00
|
|
|
#include <gtk/gtk.h>
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsIMIMEService.h"
|
|
|
|
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIURL.h"
|
2007-12-04 14:08:41 -08:00
|
|
|
#include "prlink.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsIconChannel.h"
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(nsIconChannel,
|
|
|
|
nsIRequest,
|
|
|
|
nsIChannel)
|
|
|
|
|
2007-12-07 16:08:09 -08:00
|
|
|
#ifdef MOZ_ENABLE_GNOMEUI
|
2007-12-04 14:08:41 -08:00
|
|
|
// These let us have a soft dependency on libgnomeui rather than a hard one. These are just basically the prototypes
|
|
|
|
// of the functions in the libraries.
|
|
|
|
typedef char* (*_GnomeIconLookup_fn)(GtkIconTheme *icon_theme, GnomeThumbnailFactory *thumbnail_factory,
|
|
|
|
const char *file_uri, const char *custom_icon, GnomeVFSFileInfo *file_info,
|
|
|
|
const char *mime_type, GnomeIconLookupFlags flags, GnomeIconLookupResultFlags *result);
|
|
|
|
typedef GnomeIconTheme* (*_GnomeIconThemeNew_fn)(void);
|
|
|
|
typedef int (*_GnomeInit_fn)(const char *app_id, const char *app_version, int argc, char **argv, const struct poptOption *options,
|
|
|
|
int flags, poptContext *return_ctx);
|
2007-12-07 16:08:09 -08:00
|
|
|
typedef GnomeProgram* (*_GnomeProgramGet_fn)(void);
|
|
|
|
typedef GnomeVFSResult (*_GnomeVFSGetFileInfo_fn)(const gchar *text_uri, GnomeVFSFileInfo *info, GnomeVFSFileInfoOptions options);
|
|
|
|
typedef void (*_GnomeVFSFileInfoClear_fn)(GnomeVFSFileInfo *info);
|
2007-12-04 14:08:41 -08:00
|
|
|
|
|
|
|
static PRLibrary* gLibGnomeUI = nsnull;
|
2007-12-07 16:08:09 -08:00
|
|
|
static PRLibrary* gLibGnome = nsnull;
|
|
|
|
static PRLibrary* gLibGnomeVFS = nsnull;
|
|
|
|
static PRBool gTriedToLoadGnomeLibs = PR_FALSE;
|
2007-12-04 14:08:41 -08:00
|
|
|
|
|
|
|
static _GnomeIconLookup_fn _gnome_icon_lookup = nsnull;
|
|
|
|
static _GnomeIconThemeNew_fn _gnome_icon_theme_new = nsnull;
|
|
|
|
static _GnomeInit_fn _gnome_init = nsnull;
|
2007-12-07 16:08:09 -08:00
|
|
|
static _GnomeProgramGet_fn _gnome_program_get = nsnull;
|
|
|
|
static _GnomeVFSGetFileInfo_fn _gnome_vfs_get_file_info = nsnull;
|
|
|
|
static _GnomeVFSFileInfoClear_fn _gnome_vfs_file_info_clear = nsnull;
|
|
|
|
#endif
|
2007-12-04 14:08:41 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static nsresult
|
|
|
|
moz_gdk_pixbuf_to_channel(GdkPixbuf* aPixbuf, nsIURI *aURI,
|
|
|
|
nsIChannel **aChannel)
|
|
|
|
{
|
|
|
|
int width = gdk_pixbuf_get_width(aPixbuf);
|
|
|
|
int height = gdk_pixbuf_get_height(aPixbuf);
|
|
|
|
NS_ENSURE_TRUE(height < 256 && width < 256 && height > 0 && width > 0 &&
|
|
|
|
gdk_pixbuf_get_colorspace(aPixbuf) == GDK_COLORSPACE_RGB &&
|
|
|
|
gdk_pixbuf_get_bits_per_sample(aPixbuf) == 8 &&
|
|
|
|
gdk_pixbuf_get_has_alpha(aPixbuf) &&
|
|
|
|
gdk_pixbuf_get_n_channels(aPixbuf) == 4,
|
|
|
|
NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
const int n_channels = 4;
|
|
|
|
gsize buf_size = 2 + n_channels * height * width;
|
|
|
|
PRUint8 * const buf = (PRUint8*)NS_Alloc(buf_size);
|
|
|
|
NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
PRUint8 *out = buf;
|
|
|
|
|
|
|
|
*(out++) = width;
|
|
|
|
*(out++) = height;
|
|
|
|
|
|
|
|
const guchar * const pixels = gdk_pixbuf_get_pixels(aPixbuf);
|
|
|
|
int rowextra = gdk_pixbuf_get_rowstride(aPixbuf) - width * n_channels;
|
|
|
|
|
|
|
|
// encode the RGB data and the A data
|
|
|
|
const guchar * in = pixels;
|
|
|
|
for (int y = 0; y < height; ++y, in += rowextra) {
|
|
|
|
for (int x = 0; x < width; ++x) {
|
|
|
|
PRUint8 r = *(in++);
|
|
|
|
PRUint8 g = *(in++);
|
|
|
|
PRUint8 b = *(in++);
|
|
|
|
PRUint8 a = *(in++);
|
|
|
|
#define DO_PREMULTIPLY(c_) PRUint8(PRUint16(c_) * PRUint16(a) / PRUint16(255))
|
|
|
|
#ifdef IS_LITTLE_ENDIAN
|
|
|
|
*(out++) = DO_PREMULTIPLY(b);
|
|
|
|
*(out++) = DO_PREMULTIPLY(g);
|
|
|
|
*(out++) = DO_PREMULTIPLY(r);
|
|
|
|
*(out++) = a;
|
|
|
|
#else
|
|
|
|
*(out++) = a;
|
|
|
|
*(out++) = DO_PREMULTIPLY(r);
|
|
|
|
*(out++) = DO_PREMULTIPLY(g);
|
|
|
|
*(out++) = DO_PREMULTIPLY(b);
|
|
|
|
#endif
|
|
|
|
#undef DO_PREMULTIPLY
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(out == buf + buf_size, "size miscalculation");
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIStringInputStream> stream =
|
|
|
|
do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stream->AdoptData((char*)buf, buf_size);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = NS_NewInputStreamChannel(aChannel, aURI, stream,
|
|
|
|
NS_LITERAL_CSTRING("image/icon"));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *gProtoWindow = nsnull;
|
|
|
|
static GtkWidget *gStockImageWidget = nsnull;
|
2007-12-07 16:08:09 -08:00
|
|
|
#ifdef MOZ_ENABLE_GNOMEUI
|
2007-03-22 10:30:00 -07:00
|
|
|
static GnomeIconTheme *gIconTheme = nsnull;
|
2007-12-07 16:08:09 -08:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static void
|
|
|
|
ensure_stock_image_widget()
|
|
|
|
{
|
2011-02-03 09:22:33 -08:00
|
|
|
// Only the style of the GtkImage needs to be used, but the widget is kept
|
|
|
|
// to track dynamic style changes.
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!gProtoWindow) {
|
|
|
|
gProtoWindow = gtk_window_new(GTK_WINDOW_POPUP);
|
|
|
|
GtkWidget* protoLayout = gtk_fixed_new();
|
|
|
|
gtk_container_add(GTK_CONTAINER(gProtoWindow), protoLayout);
|
|
|
|
|
|
|
|
gStockImageWidget = gtk_image_new();
|
|
|
|
gtk_container_add(GTK_CONTAINER(protoLayout), gStockImageWidget);
|
2011-02-03 09:22:09 -08:00
|
|
|
|
|
|
|
gtk_widget_ensure_style(gStockImageWidget);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-07 16:08:09 -08:00
|
|
|
#ifdef MOZ_ENABLE_GNOMEUI
|
2007-12-04 14:08:41 -08:00
|
|
|
static nsresult
|
|
|
|
ensure_libgnomeui()
|
|
|
|
{
|
|
|
|
// Attempt to get the libgnomeui symbol references. We do it this way so that stock icons from Init()
|
|
|
|
// don't get held back by InitWithGnome()'s libgnomeui dependency.
|
2007-12-07 16:08:09 -08:00
|
|
|
if (!gTriedToLoadGnomeLibs) {
|
2007-12-04 14:08:41 -08:00
|
|
|
gLibGnomeUI = PR_LoadLibrary("libgnomeui-2.so.0");
|
|
|
|
if (!gLibGnomeUI)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
_gnome_init = (_GnomeInit_fn)PR_FindFunctionSymbol(gLibGnomeUI, "gnome_init_with_popt_table");
|
|
|
|
_gnome_icon_theme_new = (_GnomeIconThemeNew_fn)PR_FindFunctionSymbol(gLibGnomeUI, "gnome_icon_theme_new");
|
|
|
|
_gnome_icon_lookup = (_GnomeIconLookup_fn)PR_FindFunctionSymbol(gLibGnomeUI, "gnome_icon_lookup");
|
|
|
|
|
2010-07-15 01:14:11 -07:00
|
|
|
if (!_gnome_init || !_gnome_icon_theme_new || !_gnome_icon_lookup) {
|
2007-12-04 14:08:41 -08:00
|
|
|
PR_UnloadLibrary(gLibGnomeUI);
|
|
|
|
gLibGnomeUI = nsnull;
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gLibGnomeUI)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-12-07 16:08:09 -08:00
|
|
|
static nsresult
|
|
|
|
ensure_libgnome()
|
|
|
|
{
|
|
|
|
if (!gTriedToLoadGnomeLibs) {
|
|
|
|
gLibGnome = PR_LoadLibrary("libgnome-2.so.0");
|
|
|
|
if (!gLibGnome)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
_gnome_program_get = (_GnomeProgramGet_fn)PR_FindFunctionSymbol(gLibGnome, "gnome_program_get");
|
|
|
|
if (!_gnome_program_get) {
|
|
|
|
PR_UnloadLibrary(gLibGnome);
|
|
|
|
gLibGnome = nsnull;
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gLibGnome)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
ensure_libgnomevfs()
|
|
|
|
{
|
|
|
|
if (!gTriedToLoadGnomeLibs) {
|
|
|
|
gLibGnomeVFS = PR_LoadLibrary("libgnomevfs-2.so.0");
|
|
|
|
if (!gLibGnomeVFS)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
_gnome_vfs_get_file_info = (_GnomeVFSGetFileInfo_fn)PR_FindFunctionSymbol(gLibGnomeVFS, "gnome_vfs_get_file_info");
|
|
|
|
_gnome_vfs_file_info_clear = (_GnomeVFSFileInfoClear_fn)PR_FindFunctionSymbol(gLibGnomeVFS, "gnome_vfs_file_info_clear");
|
|
|
|
if (!_gnome_vfs_get_file_info || !_gnome_vfs_file_info_clear) {
|
|
|
|
PR_UnloadLibrary(gLibGnomeVFS);
|
|
|
|
gLibGnomeVFS = nsnull;
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gLibGnomeVFS)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static GtkIconSize
|
|
|
|
moz_gtk_icon_size(const char *name)
|
|
|
|
{
|
|
|
|
if (strcmp(name, "button") == 0)
|
|
|
|
return GTK_ICON_SIZE_BUTTON;
|
|
|
|
|
|
|
|
if (strcmp(name, "menu") == 0)
|
|
|
|
return GTK_ICON_SIZE_MENU;
|
|
|
|
|
|
|
|
if (strcmp(name, "toolbar") == 0)
|
|
|
|
return GTK_ICON_SIZE_LARGE_TOOLBAR;
|
|
|
|
|
|
|
|
if (strcmp(name, "toolbarsmall") == 0)
|
|
|
|
return GTK_ICON_SIZE_SMALL_TOOLBAR;
|
|
|
|
|
2007-12-04 17:08:23 -08:00
|
|
|
if (strcmp(name, "dnd") == 0)
|
|
|
|
return GTK_ICON_SIZE_DND;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (strcmp(name, "dialog") == 0)
|
|
|
|
return GTK_ICON_SIZE_DIALOG;
|
|
|
|
|
|
|
|
return GTK_ICON_SIZE_MENU;
|
|
|
|
}
|
|
|
|
|
2007-12-07 16:08:09 -08:00
|
|
|
#ifdef MOZ_ENABLE_GNOMEUI
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
nsIconChannel::InitWithGnome(nsIMozIconURI *aIconURI)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2007-12-04 14:08:41 -08:00
|
|
|
|
2007-12-07 16:08:09 -08:00
|
|
|
if (NS_FAILED(ensure_libgnomeui()) || NS_FAILED(ensure_libgnome()) || NS_FAILED(ensure_libgnomevfs())) {
|
|
|
|
gTriedToLoadGnomeLibs = PR_TRUE;
|
2007-12-04 14:08:41 -08:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2007-12-07 16:08:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
gTriedToLoadGnomeLibs = PR_TRUE;
|
|
|
|
|
|
|
|
if (!_gnome_program_get()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// 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[] = { "" };
|
2007-12-04 14:08:41 -08:00
|
|
|
_gnome_init(NS_ConvertUTF16toUTF8(appName).get(), "1.0", 1, empty, NULL, 0, NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2010-05-13 13:20:30 -07:00
|
|
|
nsCOMPtr<nsIURL> url;
|
|
|
|
rv = aIconURI->GetIconURL(getter_AddRefs(url));
|
|
|
|
if (url) {
|
|
|
|
url->GetAsciiSpec(spec);
|
2007-03-22 10:30:00 -07:00
|
|
|
// Only ask gnome-vfs for a GnomeVFSFileInfo for file: uris, to avoid a
|
|
|
|
// network request
|
|
|
|
PRBool isFile;
|
2010-05-13 13:20:30 -07:00
|
|
|
if (NS_SUCCEEDED(url->SchemeIs("file", &isFile)) && isFile) {
|
2007-12-07 16:08:09 -08:00
|
|
|
_gnome_vfs_get_file_info(spec.get(), &fileInfo, GNOME_VFS_FILE_INFO_DEFAULT);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
2010-05-13 13:20:30 -07:00
|
|
|
// 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());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
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) {
|
2007-12-04 14:08:41 -08:00
|
|
|
gIconTheme = _gnome_icon_theme_new();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!gIconTheme) {
|
2007-12-07 16:08:09 -08:00
|
|
|
_gnome_vfs_file_info_clear(&fileInfo);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-04 14:08:41 -08:00
|
|
|
char* name = _gnome_icon_lookup(gIconTheme, NULL, spec.get(), NULL, &fileInfo,
|
|
|
|
type.get(), GNOME_ICON_LOOKUP_FLAGS_NONE,
|
|
|
|
NULL);
|
|
|
|
|
2007-12-07 16:08:09 -08:00
|
|
|
_gnome_vfs_file_info_clear(&fileInfo);
|
2007-12-04 14:08:41 -08:00
|
|
|
if (!name)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2010-07-15 01:14:11 -07:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2007-12-04 14:08:41 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
GError *err = nsnull;
|
2010-07-15 01:14:11 -07:00
|
|
|
GdkPixbuf* buf = gtk_icon_theme_load_icon(theme, name, iconSize, (GtkIconLookupFlags)0, &err);
|
|
|
|
g_free(name);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
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);
|
2009-02-02 09:49:58 -08:00
|
|
|
g_object_unref(buf);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!scaled)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX Respect icon state
|
|
|
|
|
|
|
|
rv = moz_gdk_pixbuf_to_channel(scaled, aIconURI,
|
|
|
|
getter_AddRefs(mRealChannel));
|
2009-02-02 09:49:58 -08:00
|
|
|
g_object_unref(scaled);
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
2007-12-07 16:08:09 -08:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
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()) {
|
2007-12-07 16:08:09 -08:00
|
|
|
#ifdef MOZ_ENABLE_GNOMEUI
|
2007-03-22 10:30:00 -07:00
|
|
|
return InitWithGnome(iconURI);
|
2007-12-07 16:08:09 -08:00
|
|
|
#else
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString iconSizeString;
|
|
|
|
iconURI->GetIconSize(iconSizeString);
|
|
|
|
|
|
|
|
nsCAutoString iconStateString;
|
|
|
|
iconURI->GetIconState(iconStateString);
|
|
|
|
|
|
|
|
GtkIconSize icon_size = moz_gtk_icon_size(iconSizeString.get());
|
2011-02-03 09:22:33 -08:00
|
|
|
GtkStateType state = iconStateString.EqualsLiteral("disabled") ?
|
|
|
|
GTK_STATE_INSENSITIVE : GTK_STATE_NORMAL;
|
|
|
|
|
2011-02-03 09:24:33 -08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2011-02-08 14:31:00 -08:00
|
|
|
PRBool forceDirection = direction != GTK_TEXT_DIR_NONE;
|
2011-02-03 09:24:33 -08:00
|
|
|
nsCAutoString stockID;
|
2011-02-08 14:31:00 -08:00
|
|
|
PRBool useIconName = PR_FALSE;
|
|
|
|
if (!forceDirection) {
|
2011-02-03 09:24:33 -08:00
|
|
|
direction = gtk_widget_get_default_direction();
|
|
|
|
stockID = stockIcon;
|
2011-02-08 14:31:00 -08:00
|
|
|
} 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 = PR_TRUE;
|
|
|
|
gtk_icon_info_free(icon);
|
|
|
|
}
|
|
|
|
}
|
2011-02-03 09:24:33 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
ensure_stock_image_widget();
|
2011-02-03 09:22:33 -08:00
|
|
|
GtkStyle *style = gtk_widget_get_style(gStockImageWidget);
|
2011-02-08 14:31:00 -08:00
|
|
|
GtkIconSet *icon_set = NULL;
|
|
|
|
if (!useIconName) {
|
|
|
|
icon_set = gtk_style_lookup_icon_set(style, stockID.get());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-02-08 14:31:00 -08:00
|
|
|
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 = PR_TRUE;
|
2011-02-03 09:22:33 -08:00
|
|
|
// 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();
|
2007-03-22 10:30:00 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-02-03 09:22:33 -08:00
|
|
|
GdkPixbuf *icon =
|
2011-02-03 09:24:33 -08:00
|
|
|
gtk_icon_set_render_icon (icon_set, style, direction, state,
|
2011-02-03 09:22:33 -08:00
|
|
|
icon_size, gStockImageWidget, NULL);
|
2011-02-08 14:31:00 -08:00
|
|
|
if (useIconName) {
|
|
|
|
gtk_icon_set_unref(icon_set);
|
|
|
|
}
|
2011-02-03 09:22:33 -08:00
|
|
|
|
2011-02-08 14:30:42 -08:00
|
|
|
// According to documentation, gtk_icon_set_render_icon() never returns
|
|
|
|
// NULL, but it does return NULL when we have the problem reported here:
|
2011-02-03 09:22:33 -08:00
|
|
|
// https://bugzilla.gnome.org/show_bug.cgi?id=629878#c13
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!icon)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
nsresult rv = moz_gdk_pixbuf_to_channel(icon, iconURI,
|
|
|
|
getter_AddRefs(mRealChannel));
|
|
|
|
|
2009-02-02 09:49:58 -08:00
|
|
|
g_object_unref(icon);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsIconChannel::Shutdown() {
|
|
|
|
if (gProtoWindow) {
|
|
|
|
gtk_widget_destroy(gProtoWindow);
|
|
|
|
gProtoWindow = nsnull;
|
|
|
|
gStockImageWidget = nsnull;
|
|
|
|
}
|
2007-12-07 16:08:09 -08:00
|
|
|
#ifdef MOZ_ENABLE_GNOMEUI
|
2007-03-22 10:30:00 -07:00
|
|
|
if (gIconTheme) {
|
|
|
|
g_object_unref(G_OBJECT(gIconTheme));
|
|
|
|
gIconTheme = nsnull;
|
|
|
|
}
|
2007-12-07 16:08:09 -08:00
|
|
|
gTriedToLoadGnomeLibs = PR_FALSE;
|
2007-12-04 14:08:41 -08:00
|
|
|
if (gLibGnomeUI) {
|
|
|
|
PR_UnloadLibrary(gLibGnomeUI);
|
|
|
|
gLibGnomeUI = nsnull;
|
|
|
|
}
|
2007-12-07 16:08:09 -08:00
|
|
|
if (gLibGnome) {
|
|
|
|
PR_UnloadLibrary(gLibGnome);
|
|
|
|
gLibGnome = nsnull;
|
|
|
|
}
|
|
|
|
if (gLibGnomeVFS) {
|
|
|
|
PR_UnloadLibrary(gLibGnomeVFS);
|
|
|
|
gLibGnomeVFS = nsnull;
|
|
|
|
}
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|