summaryrefslogtreecommitdiff
path: root/extensions/image-properties/nautilus-image-properties-page-model-provider.c
blob: cf55f109460f81db8c5db6448ccbf34640d0befa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* Copyright (C) 2004 Red Hat, Inc
 * Copyright (c) 2007 Novell, Inc.
 * Copyright (c) 2017 Thomas Bechtold <thomasbechtold@jpberlin.de>
 * Copyright (c) 2018 Ernestas Kulik <ernestask@gnome.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 * XMP support by Hubert Figuiere <hfiguiere@novell.com>
 */

#include "nautilus-image-properties-page-model-provider.h"
#include "nautilus-image-properties-page-model.h"
#include <glib/gi18n.h>
#include <nautilus-extension.h>

#define NAUTILUS_IMAGE_PROPERTIES_PAGE_MODEL_NAME "NautilusImagePropertiesPageModel::property_page"

struct _NautilusImagePropertiesPageModelProvider
{
    GObject parent_instance;

    NautilusPropertyPageModel *page_model;
    GList *files;
};

enum
{
    PROP_0,
    PROP_PAGE_MODEL,
    PROP_FILES,
    N_PROPERTIES
};

static void property_page_provider_iface_init (NautilusPropertyPageModelProviderInterface *iface);

G_DEFINE_DYNAMIC_TYPE_EXTENDED (NautilusImagePropertiesPageModelProvider,
                                nautilus_image_properties_page_model_provider,
                                G_TYPE_OBJECT,
                                0,
                                G_IMPLEMENT_INTERFACE_DYNAMIC (NAUTILUS_TYPE_PROPERTY_PAGE_MODEL_PROVIDER,
                                                               property_page_provider_iface_init))

static gboolean
is_mime_type_supported (const char *mime_type)
{
    g_autoptr (GSList) formats = NULL;

    if (mime_type == NULL)
    {
        return FALSE;
    }

    formats = gdk_pixbuf_get_formats ();

    for (GSList *l = formats; l != NULL; l = l->next)
    {
        g_auto (GStrv) mime_types = NULL;

        mime_types = gdk_pixbuf_format_get_mime_types (l->data);
        if (mime_types == NULL)
        {
            continue;
        }

        if (g_strv_contains ((const char *const *) mime_types, mime_type))
        {
            return TRUE;
        }
    }

    return FALSE;
}

static NautilusPropertyPageModel*
get_model (NautilusPropertyPageModelProvider *provider)
{
    NautilusImagePropertiesPageModelProvider *self;

    self = NAUTILUS_IMAGE_PROPERTIES_PAGE_MODEL_PROVIDER (provider);

    return self->page_model;
}

static void
set_files (NautilusPropertyPageModelProvider *provider,
           GList                             *files)
{
    NautilusImagePropertiesPageModelProvider *self;
    NautilusFileInfo *file_info;
    g_autofree char *mime_type = NULL;

    self = NAUTILUS_IMAGE_PROPERTIES_PAGE_MODEL_PROVIDER (provider);

    if (self->files != NULL)
    {
        g_list_free_full (self->files, g_object_unref);
        self->files = NULL;
    }

    self->files = g_list_copy_deep (files,
                                    (GCopyFunc) g_object_ref, NULL);

    if (self->files == NULL || self->files->next != NULL)
    {
        return;
    }

    file_info = NAUTILUS_FILE_INFO (self->files->data);
    mime_type = nautilus_file_info_get_mime_type (file_info);
    if (!is_mime_type_supported (mime_type))
    {
        return;
    }

    nautilus_image_properties_page_model_load_from_file_info (NAUTILUS_IMAGE_PROPERTIES_PAGE_MODEL (self->page_model),
                                                        file_info);
}

static gboolean
supports_files (NautilusPropertyPageModelProvider *provider)
{
    g_autofree char *mime_type = NULL;
    NautilusFileInfo *file_info;
    NautilusImagePropertiesPageModelProvider *self;

    self = NAUTILUS_IMAGE_PROPERTIES_PAGE_MODEL_PROVIDER (provider);
    if (self->files == NULL || self->files->next != NULL)
    {
        return FALSE;
    }

    file_info = NAUTILUS_FILE_INFO (self->files->data);
    mime_type = nautilus_file_info_get_mime_type (file_info);
    if (!is_mime_type_supported (mime_type))
    {
        return FALSE;
    }

    return TRUE;
}

static void
nautilus_image_properties_page_model_provider_get_property (GObject    *object,
                                                      guint       prop_id,
                                                      GValue     *value,
                                                      GParamSpec *pspec)
{
    NautilusImagePropertiesPageModelProvider *self = NAUTILUS_IMAGE_PROPERTIES_PAGE_MODEL_PROVIDER (object);
    switch (prop_id)
    {
        case PROP_PAGE_MODEL:
        {
            g_value_set_object (value, self->page_model);
        }
        break;

        case PROP_FILES:
        {
            g_value_set_pointer (value, self->files);
        }
        break;

        default:
        {
            g_assert_not_reached ();
        }
    }
}

static void
nautilus_image_properties_page_model_provider_set_property (GObject    *object,
                                                      guint       prop_id,
                                                      const GValue     *value,
                                                      GParamSpec *pspec)
{
    NautilusImagePropertiesPageModelProvider *self = NAUTILUS_IMAGE_PROPERTIES_PAGE_MODEL_PROVIDER (object);
    switch (prop_id)
    {
        case PROP_PAGE_MODEL:
        {
            g_clear_object (&self->page_model);
            self->page_model = NAUTILUS_PROPERTY_PAGE_MODEL (g_value_get_object (value));
        }
        break;

        case PROP_FILES:
        {
            set_files (NAUTILUS_PROPERTY_PAGE_MODEL_PROVIDER (self),
                       g_value_get_pointer (value));
        }
        break;

        default:
        {
            g_assert_not_reached ();
        }
    }
}

static void
property_page_provider_iface_init (NautilusPropertyPageModelProviderInterface *iface)
{
    iface->get_model = get_model;
    iface->set_files = set_files;
    iface->supports_files = supports_files;

}

static void
nautilus_image_properties_page_model_provider_init (NautilusImagePropertiesPageModelProvider *self)
{
    self->page_model = NAUTILUS_PROPERTY_PAGE_MODEL (nautilus_image_properties_page_model_new ());
}

static void
nautilus_image_properties_page_model_provider_class_init (NautilusImagePropertiesPageModelProviderClass *klass)
{
    GObjectClass *oclass;

    oclass = G_OBJECT_CLASS (klass);
    oclass->get_property = nautilus_image_properties_page_model_provider_get_property;
    oclass->set_property = nautilus_image_properties_page_model_provider_set_property;

    g_object_class_override_property (oclass, PROP_PAGE_MODEL, "page-model");
    g_object_class_override_property (oclass, PROP_FILES, "files");
}

static void
nautilus_image_properties_page_model_provider_class_finalize (NautilusImagePropertiesPageModelProviderClass *klass)
{
    (void) klass;
}

void
nautilus_image_properties_page_model_provider_load (GTypeModule *module)
{
    nautilus_image_properties_page_model_provider_register_type (module);
}