summaryrefslogtreecommitdiff
path: root/wallpaper/caja-wallpaper-extension.c
blob: 138c5c726ccd5f02ba3368d48b73eaa4ccd01ec1 (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
/*
 *  Caja Wallpaper extension
 *
 *  Copyright (C) 2005 Adam Israel
 *  Copyright (C) 2014 Stefano Karapetsas
 *
 *  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, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *  Authors: Adam Israel <adam@battleaxe.net>
 *           Stefano Karapetsas <stefano@karapetsas.com>
 */

#include <config.h>
#include <string.h>
#include <gio/gio.h>
#include <glib/gi18n-lib.h>
#include <libcaja-extension/caja-extension-types.h>
#include <libcaja-extension/caja-file-info.h>
#include <libcaja-extension/caja-menu-provider.h>
#include "caja-wallpaper-extension.h"

#define WP_SCHEMA "org.mate.background"
#define WP_FILE_KEY "picture-filename"

static GObjectClass *parent_class;

static void
set_wallpaper_callback (CajaMenuItem *item,
              gpointer          user_data)
{
    GList *files;
    GSettings *settings;
    CajaFileInfo *file;
    gchar *uri;
    gchar *filename;

    files = g_object_get_data (G_OBJECT (item), "files");
    file = files->data;

    uri = caja_file_info_get_uri (file);
    filename = g_filename_from_uri(uri, NULL, NULL);

    settings = g_settings_new (WP_SCHEMA);

    g_settings_set_string (settings, WP_FILE_KEY, filename);

    g_object_unref (settings);
    g_free (filename);
    g_free (uri);

}

static gboolean
is_image (CajaFileInfo *file)
{
    gchar   *mimeType;
    gboolean isImage;

    mimeType = caja_file_info_get_mime_type (file);
    isImage = g_str_has_prefix (mimeType, "image/");
    g_free (mimeType);
    return isImage;
}


static GList *
caja_cwe_get_file_items (CajaMenuProvider *provider,
                  GtkWidget            *window,
                  GList                *files)
{
    GList    *items = NULL;
    GList    *scan;
    gboolean  one_item;
    CajaMenuItem *item;

    for (scan = files; scan; scan = scan->next) {
        CajaFileInfo *file = scan->data;
        gchar            *scheme;
        gboolean          local;

        scheme = caja_file_info_get_uri_scheme (file);
        local = strncmp (scheme, "file", 4) == 0;
        g_free (scheme);

        if (!local)
            return NULL;
    }

    one_item = (files != NULL) && (files->next == NULL);
    if (one_item && is_image ((CajaFileInfo *)files->data) &&
        !caja_file_info_is_directory ((CajaFileInfo *)files->data)) {
        item = caja_menu_item_new ("CajaCwe::sendto",
                           _("Set as wallpaper"),
                           _("Set image as the current wallpaper"),
                           NULL);
        g_signal_connect (item,
                  "activate",
                  G_CALLBACK (set_wallpaper_callback),
                provider);
        g_object_set_data_full (G_OBJECT (item),
                    "files",
                    caja_file_info_list_copy (files),
                    (GDestroyNotify) caja_file_info_list_free);
        items = g_list_append (items, item);
    }
    return items;
}


static void
caja_cwe_menu_provider_iface_init (CajaMenuProviderIface *iface)
{
    iface->get_file_items = caja_cwe_get_file_items;
}


static void
caja_cwe_instance_init (CajaCwe *cwe)
{
}


static void
caja_cwe_class_init (CajaCweClass *class)
{
    parent_class = g_type_class_peek_parent (class);
}


static GType cwe_type = 0;


GType
caja_cwe_get_type (void)
{
    return cwe_type;
}


void
caja_cwe_register_type (GTypeModule *module)
{
    static const GTypeInfo info = {
        sizeof (CajaCweClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) caja_cwe_class_init,
        NULL,
        NULL,
        sizeof (CajaCwe),
        0,
        (GInstanceInitFunc) caja_cwe_instance_init,
    };

    static const GInterfaceInfo menu_provider_iface_info = {
        (GInterfaceInitFunc) caja_cwe_menu_provider_iface_init,
        NULL,
        NULL
    };

    cwe_type = g_type_module_register_type (module,
                             G_TYPE_OBJECT,
                             "CajaCwe",
                             &info, 0);

    g_type_module_add_interface (module,
                     cwe_type,
                     CAJA_TYPE_MENU_PROVIDER,
                     &menu_provider_iface_info);
}