Back to index

lightning-sunbird  0.9+nobinonly
nsIconChannel.cpp
Go to the documentation of this file.
00001 /* vim:set ts=2 sw=2 sts=2 cin et: */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Mozilla icon channel for gnome.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Christian Biesinger <cbiesinger@web.de>.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <stdlib.h>
00039 #include <unistd.h>
00040 
00041 // Older versions of these headers seem to be missing an extern "C"
00042 extern "C" {
00043 #include <libgnome/libgnome.h>
00044 #include <libgnomeui/gnome-icon-theme.h>
00045 #include <libgnomeui/gnome-icon-lookup.h>
00046 #include <libgnomeui/gnome-ui-init.h>
00047 
00048 #include <libgnomevfs/gnome-vfs-file-info.h>
00049 #include <libgnomevfs/gnome-vfs-ops.h>
00050 }
00051 
00052 #include <gtk/gtkwidget.h>
00053 #include <gtk/gtkiconfactory.h>
00054 #include <gtk/gtkimage.h>
00055 #include <gtk/gtkwindow.h>
00056 #include <gtk/gtkfixed.h>
00057 #include <gtk/gtkversion.h>
00058 
00059 #include "nsIMIMEService.h"
00060 
00061 #include "nsIStringBundle.h"
00062 
00063 #include "nsEscape.h"
00064 #include "nsNetUtil.h"
00065 #include "nsIURL.h"
00066 #include "nsIStringStream.h"
00067 
00068 #include "nsIconChannel.h"
00069 
00070 NS_IMPL_ISUPPORTS2(nsIconChannel,
00071                    nsIRequest,
00072                    nsIChannel)
00073 
00074 static nsresult
00075 moz_gdk_pixbuf_to_channel(GdkPixbuf* aPixbuf, nsIURI *aURI,
00076                           nsIChannel **aChannel)
00077 {
00078   int width = gdk_pixbuf_get_width(aPixbuf);
00079   int height = gdk_pixbuf_get_height(aPixbuf);
00080   NS_ENSURE_TRUE(height < 256 && width < 256 && height > 0 && width > 0 &&
00081                  gdk_pixbuf_get_colorspace(aPixbuf) == GDK_COLORSPACE_RGB &&
00082                  gdk_pixbuf_get_bits_per_sample(aPixbuf) == 8 &&
00083                  gdk_pixbuf_get_has_alpha(aPixbuf) &&
00084                  gdk_pixbuf_get_n_channels(aPixbuf) == 4,
00085                  NS_ERROR_UNEXPECTED);
00086 
00087   const int n_channels = 4;
00088   gsize buf_size = 3 + n_channels * height * width;
00089   PRUint8 * const buf = (PRUint8*)NS_Alloc(buf_size);
00090   NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY);
00091   PRUint8 *out = buf;
00092 
00093   *(out++) = width;
00094   *(out++) = height;
00095   *(out++) = 8; // bits of alpha per pixel
00096   
00097   const guchar * const pixels = gdk_pixbuf_get_pixels(aPixbuf);
00098   int rowextra = gdk_pixbuf_get_rowstride(aPixbuf) - width * n_channels;
00099 
00100   // encode the RGB data and the A data
00101   const guchar * in = pixels;
00102   PRUint8 *alpha_out = out + height * width * 3;
00103 #ifdef DEBUG
00104   PRUint8 * const alpha_start = alpha_out;
00105 #endif
00106   for (int y = 0; y < height; ++y, in += rowextra) {
00107     for (int x = 0; x < width; ++x) {
00108       *(out++) = *(in++); // R
00109       *(out++) = *(in++); // G
00110       *(out++) = *(in++); // B
00111       *(alpha_out++) = *(in++); // A
00112     }
00113   }
00114 
00115   NS_ASSERTION(out == alpha_start && alpha_out == buf + buf_size,
00116                "size miscalculation");
00117 
00118   nsresult rv;
00119   nsCOMPtr<nsIInputStream> stream;
00120   rv = NS_NewByteInputStream(getter_AddRefs(stream), (char*)buf, buf_size);
00121   NS_ENSURE_SUCCESS(rv, rv);
00122 
00123   nsCOMPtr<nsIStringInputStream> sstream = do_QueryInterface(stream);
00124   sstream->AdoptData((char*)buf, buf_size); // previous call was |ShareData|
00125 
00126   rv = NS_NewInputStreamChannel(aChannel, aURI, stream,
00127                                 NS_LITERAL_CSTRING("image/icon"));
00128   return rv;
00129 }
00130 
00131 static GtkWidget *gProtoWindow = nsnull;
00132 static GtkWidget *gStockImageWidget = nsnull;
00133 static GnomeIconTheme *gIconTheme = nsnull;
00134 
00135 #if GTK_CHECK_VERSION(2,4,0)
00136 static GtkIconFactory *gIconFactory = nsnull;
00137 #endif
00138 
00139 static void
00140 ensure_stock_image_widget()
00141 {
00142   if (!gProtoWindow) {
00143     gProtoWindow = gtk_window_new(GTK_WINDOW_POPUP);
00144     gtk_widget_realize(gProtoWindow);
00145     GtkWidget* protoLayout = gtk_fixed_new();
00146     gtk_container_add(GTK_CONTAINER(gProtoWindow), protoLayout);
00147 
00148     gStockImageWidget = gtk_image_new();
00149     gtk_container_add(GTK_CONTAINER(protoLayout), gStockImageWidget);
00150     gtk_widget_realize(gStockImageWidget);
00151   }
00152 }
00153 
00154 #if GTK_CHECK_VERSION(2,4,0)
00155 static void
00156 ensure_icon_factory()
00157 {
00158   if (!gIconFactory) {
00159     gIconFactory = gtk_icon_factory_new();
00160     gtk_icon_factory_add_default(gIconFactory);
00161     g_object_unref(gIconFactory);
00162   }
00163 }
00164 #endif
00165 
00166 static GtkIconSize
00167 moz_gtk_icon_size(const char *name)
00168 {
00169   if (strcmp(name, "button") == 0)
00170     return GTK_ICON_SIZE_BUTTON;
00171 
00172   if (strcmp(name, "menu") == 0)
00173     return GTK_ICON_SIZE_MENU;
00174 
00175   if (strcmp(name, "toolbar") == 0)
00176     return GTK_ICON_SIZE_LARGE_TOOLBAR;
00177 
00178   if (strcmp(name, "toolbarsmall") == 0)
00179     return GTK_ICON_SIZE_SMALL_TOOLBAR;
00180 
00181   if (strcmp(name, "dialog") == 0)
00182     return GTK_ICON_SIZE_DIALOG;
00183 
00184   return GTK_ICON_SIZE_MENU;
00185 }
00186 
00187 nsresult
00188 nsIconChannel::InitWithGnome(nsIMozIconURI *aIconURI)
00189 {
00190   nsresult rv;
00191   
00192   if (!gnome_program_get()) {
00193     // Get the brandShortName from the string bundle to pass to GNOME
00194     // as the application name.  This may be used for things such as
00195     // the title of grouped windows in the panel.
00196     nsCOMPtr<nsIStringBundleService> bundleService = 
00197       do_GetService(NS_STRINGBUNDLE_CONTRACTID);
00198 
00199     NS_ASSERTION(bundleService, "String bundle service must be present!");
00200 
00201     nsCOMPtr<nsIStringBundle> bundle;
00202     bundleService->CreateBundle("chrome://branding/locale/brand.properties",
00203                                 getter_AddRefs(bundle));
00204     nsXPIDLString appName;
00205 
00206     if (bundle) {
00207       bundle->GetStringFromName(NS_LITERAL_STRING("brandShortName").get(),
00208                                 getter_Copies(appName));
00209     } else {
00210       NS_WARNING("brand.properties not present, using default application name");
00211       appName.AssignLiteral("Gecko");
00212     }
00213 
00214     char* empty[] = { "" };
00215     gnome_init(NS_ConvertUTF16toUTF8(appName).get(), "1.0", 1, empty);
00216   }
00217 
00218   nsCAutoString iconSizeString;
00219   aIconURI->GetIconSize(iconSizeString);
00220 
00221   PRUint32 iconSize;
00222 
00223   if (iconSizeString.IsEmpty()) {
00224     rv = aIconURI->GetImageSize(&iconSize);
00225     NS_ASSERTION(NS_SUCCEEDED(rv), "GetImageSize failed");
00226   } else {
00227     int size;
00228     
00229     GtkIconSize icon_size = moz_gtk_icon_size(iconSizeString.get());
00230     gtk_icon_size_lookup(icon_size, &size, NULL);
00231     iconSize = size;
00232   }
00233 
00234   nsCAutoString type;
00235   aIconURI->GetContentType(type);
00236 
00237   GnomeVFSFileInfo fileInfo = {0};
00238   fileInfo.refcount = 1; // In case some GnomeVFS function addrefs and releases it
00239 
00240   nsCAutoString spec;
00241   nsCOMPtr<nsIURI> fileURI;
00242   rv = aIconURI->GetIconFile(getter_AddRefs(fileURI));
00243   if (fileURI) {
00244     fileURI->GetAsciiSpec(spec);
00245     // Only ask gnome-vfs for a GnomeVFSFileInfo for file: uris, to avoid a
00246     // network request
00247     PRBool isFile;
00248     if (NS_SUCCEEDED(fileURI->SchemeIs("file", &isFile)) && isFile) {
00249       gnome_vfs_get_file_info(spec.get(), &fileInfo, GNOME_VFS_FILE_INFO_DEFAULT);
00250     }
00251     else {
00252       // We have to get a leaf name from our uri...
00253       nsCOMPtr<nsIURL> url(do_QueryInterface(fileURI));
00254       if (url) {
00255         nsCAutoString name;
00256         // The filename we get is UTF-8-compatible, which matches gnome expectations.
00257         // See also: http://lists.gnome.org/archives/gnome-vfs-list/2004-March/msg00049.html
00258         // "Whenever we can detect the charset used for the URI type we try to
00259         //  convert it to/from utf8 automatically inside gnome-vfs."
00260         // I'll interpret that as "otherwise, this field is random junk".
00261         url->GetFileName(name);
00262         fileInfo.name = g_strdup(name.get());
00263       }
00264       // If this is no nsIURL, nothing we can do really.
00265 
00266       if (!type.IsEmpty()) {
00267         fileInfo.valid_fields = GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
00268         fileInfo.mime_type = g_strdup(type.get());
00269       }
00270     }
00271   }
00272 
00273 
00274   if (type.IsEmpty()) {
00275     nsCOMPtr<nsIMIMEService> ms(do_GetService("@mozilla.org/mime;1"));
00276     if (ms) {
00277       nsCAutoString fileExt;
00278       aIconURI->GetFileExtension(fileExt);
00279       ms->GetTypeFromExtension(fileExt, type);
00280     }
00281   }
00282 
00283   // Get the icon theme
00284   if (!gIconTheme) {
00285     gIconTheme = gnome_icon_theme_new();
00286     if (!gIconTheme) {
00287       gnome_vfs_file_info_clear(&fileInfo);
00288       return NS_ERROR_NOT_AVAILABLE;
00289     }
00290   }
00291 
00292   char* name = gnome_icon_lookup(gIconTheme, NULL, spec.get(), NULL, &fileInfo,
00293                                  type.get(), GNOME_ICON_LOOKUP_FLAGS_NONE,
00294                                  NULL);
00295   gnome_vfs_file_info_clear(&fileInfo);
00296   if (!name) {
00297     return NS_ERROR_NOT_AVAILABLE;
00298   }
00299  
00300   char* file = gnome_icon_theme_lookup_icon(gIconTheme, name, iconSize,
00301                                             NULL, NULL);
00302   g_free(name);
00303   if (!file)
00304     return NS_ERROR_NOT_AVAILABLE;
00305 
00306   // Create a GdkPixbuf buffer and scale it
00307   GError *err = nsnull;
00308   GdkPixbuf* buf = gdk_pixbuf_new_from_file(file, &err);
00309   g_free(file);
00310   if (!buf) {
00311     if (err)
00312       g_error_free(err);
00313     return NS_ERROR_UNEXPECTED;
00314   }
00315 
00316   GdkPixbuf* scaled = buf;
00317   if (gdk_pixbuf_get_width(buf)  != iconSize &&
00318       gdk_pixbuf_get_height(buf) != iconSize) {
00319     // scale...
00320     scaled = gdk_pixbuf_scale_simple(buf, iconSize, iconSize,
00321                                      GDK_INTERP_BILINEAR);
00322     gdk_pixbuf_unref(buf);
00323     if (!scaled)
00324       return NS_ERROR_OUT_OF_MEMORY;
00325   }
00326 
00327   // XXX Respect icon state
00328   
00329   rv = moz_gdk_pixbuf_to_channel(scaled, aIconURI,
00330                                  getter_AddRefs(mRealChannel));
00331   gdk_pixbuf_unref(scaled);
00332   return rv;
00333 }
00334 
00335 nsresult
00336 nsIconChannel::Init(nsIURI* aURI)
00337 {
00338   nsCOMPtr<nsIMozIconURI> iconURI = do_QueryInterface(aURI);
00339   NS_ASSERTION(iconURI, "URI is not an nsIMozIconURI");
00340 
00341   nsCAutoString stockIcon;
00342   iconURI->GetStockIcon(stockIcon);
00343   if (stockIcon.IsEmpty()) {
00344     return InitWithGnome(iconURI);
00345   }
00346 
00347   nsCAutoString iconSizeString;
00348   iconURI->GetIconSize(iconSizeString);
00349 
00350   nsCAutoString iconStateString;
00351   iconURI->GetIconState(iconStateString);
00352 
00353   GtkIconSize icon_size = moz_gtk_icon_size(iconSizeString.get());
00354    
00355   ensure_stock_image_widget();
00356 
00357   gboolean sensitive = strcmp(iconStateString.get(), "disabled");
00358   gtk_widget_set_sensitive (gStockImageWidget, sensitive);
00359 
00360   GdkPixbuf *icon = gtk_widget_render_icon(gStockImageWidget, stockIcon.get(),
00361                                            icon_size, NULL);
00362 #if GTK_CHECK_VERSION(2,4,0)
00363   if (!icon) {
00364     ensure_icon_factory();
00365       
00366     GtkIconSet *icon_set = gtk_icon_set_new();
00367     GtkIconSource *icon_source = gtk_icon_source_new();
00368     
00369     gtk_icon_source_set_icon_name(icon_source, stockIcon.get());
00370     gtk_icon_set_add_source(icon_set, icon_source);
00371     gtk_icon_factory_add(gIconFactory, stockIcon.get(), icon_set);
00372     gtk_icon_set_unref(icon_set);
00373     gtk_icon_source_free(icon_source);
00374 
00375     icon = gtk_widget_render_icon(gStockImageWidget, stockIcon.get(),
00376                                   icon_size, NULL);
00377   }
00378 #endif
00379 
00380   if (!icon)
00381     return NS_ERROR_NOT_AVAILABLE;
00382   
00383   nsresult rv = moz_gdk_pixbuf_to_channel(icon, iconURI,
00384                                           getter_AddRefs(mRealChannel));
00385 
00386   gdk_pixbuf_unref(icon);
00387 
00388   return rv;
00389 }
00390 
00391 void
00392 nsIconChannel::Shutdown() {
00393   if (gProtoWindow) {
00394     gtk_widget_destroy(gProtoWindow);
00395     gProtoWindow = nsnull;
00396     gStockImageWidget = nsnull;
00397   }
00398   if (gIconTheme) {
00399     g_object_unref(G_OBJECT(gIconTheme));
00400     gIconTheme = nsnull;
00401   }
00402 }