1/*
2 * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
3 * Copyright (C) 2007 Holger Hans Peter Freyther
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1.  Redistributions of source code must retain the above copyright
10 *     notice, this list of conditions and the following disclaimer.
11 * 2.  Redistributions in binary form must reproduce the above copyright
12 *     notice, this list of conditions and the following disclaimer in the
13 *     documentation and/or other materials provided with the distribution.
14 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15 *     its contributors may be used to endorse or promote products derived
16 *     from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "config.h"
31#include "Icon.h"
32
33#include "GraphicsContext.h"
34#include "MIMETypeRegistry.h"
35#include "PlatformContextCairo.h"
36
37#include <gtk/gtk.h>
38
39#include <wtf/PassRefPtr.h>
40#include <wtf/text/CString.h>
41
42namespace WebCore {
43
44Icon::Icon()
45    : m_icon(0)
46{
47}
48
49Icon::~Icon()
50{
51    if (m_icon)
52        g_object_unref(m_icon);
53}
54
55static String lookupIconName(String MIMEType)
56{
57    /*
58     Lookup an appropriate icon according to either the Icon Naming Spec
59     or conventional Gnome icon names respectively.
60
61     See http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
62
63     The icon theme is probed for the following names:
64     1. media-subtype
65     2. gnome-mime-media-subtype
66     3. media-x-generic
67     4. gnome-mime-media
68
69     In the worst case it falls back to the stock file icon.
70    */
71    int pos = MIMEType.find('/');
72    if(pos >= 0) {
73        String media = MIMEType.substring(0, pos);
74        String subtype = MIMEType.substring(pos + 1);
75        GtkIconTheme* iconTheme = gtk_icon_theme_get_default();
76        String iconName = media + "-" + subtype;
77        if(gtk_icon_theme_has_icon(iconTheme, iconName.utf8().data()))
78            return iconName;
79        iconName = "gnome-mime-" + media + "-" + subtype;
80        if(gtk_icon_theme_has_icon(iconTheme, iconName.utf8().data()))
81            return iconName;
82        iconName = media + "-x-generic";
83        if(gtk_icon_theme_has_icon(iconTheme, iconName.utf8().data()))
84            return iconName;
85        iconName = media + "gnome-mime-" + media;
86        if(gtk_icon_theme_has_icon(iconTheme, iconName.utf8().data()))
87            return iconName;
88    }
89    return GTK_STOCK_FILE;
90}
91
92// FIXME: Move the code to ChromeClient::iconForFiles().
93PassRefPtr<Icon> Icon::createIconForFiles(const Vector<String>& filenames)
94{
95    if (filenames.isEmpty())
96        return 0;
97
98    if (filenames.size() == 1) {
99        if (!g_path_skip_root(filenames[0].utf8().data()))
100            return 0;
101
102        String MIMEType = MIMETypeRegistry::getMIMETypeForPath(filenames[0]);
103        String iconName = lookupIconName(MIMEType);
104
105        RefPtr<Icon> icon = adoptRef(new Icon);
106        icon->m_icon = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), iconName.utf8().data(), 16, GTK_ICON_LOOKUP_USE_BUILTIN, 0);
107        if (!icon->m_icon)
108            return 0;
109        return icon.release();
110    }
111
112    //FIXME: Implement this
113    return 0;
114}
115
116void Icon::paint(GraphicsContext* context, const IntRect& rect)
117{
118    if (context->paintingDisabled())
119        return;
120
121    // TODO: Scale/clip the image if necessary.
122    cairo_t* cr = context->platformContext()->cr();
123    cairo_save(cr);
124    gdk_cairo_set_source_pixbuf(cr, m_icon, rect.x(), rect.y());
125    cairo_paint(cr);
126    cairo_restore(cr);
127}
128
129}
130