~ubuntu-branches/ubuntu/precise/nautilus-actions/precise

« back to all changes in this revision

Viewing changes to utils/nautilus-actions-new-config.c

  • Committer: Bazaar Package Importer
  • Author(s): Christine Spang
  • Date: 2009-08-12 14:04:00 UTC
  • mfrom: (1.1.6 upstream) (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090812140400-ufe3o3jvr62lf0sf
Tags: 1.12.0-1
* New upstream stable release.
  - Actions can now be enabled/disabled: disabled actions will
    never show up in the nautilus context menu
  - Gnome Bugzilla bugs fixed:
    - #325519 asked by Frederic Ruaudel (enabled property)
    - #590398 reported by Pierre Wieser (install doc)
    - #590399 reported by Pierre Wieser (gtk_image_menu_item_set_image)
    - #590709 reported by Claude Paroz (markup in translatable strings)
    - #590711 reported by Claude Paroz (pipe char is ambiguous to translate)
 - Do not install GConf schemas if --disable-schemas-install option has
   been specified
 - New/updated es and fr translations

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Nautilus Actions
3
 
 *
4
 
 * Copyright (C) 2005 The GNOME Foundation
5
 
 * Copyright (C) 2006, 2007, 2008 Frederic Ruaudel and others (see AUTHORS)
6
 
 * Copyright (C) 2009 Pierre Wieser and others (see AUTHORS)
7
 
 *
8
 
 * This Program is free software; you can redistribute it and/or
9
 
 * modify it under the terms of the GNU General Public License as
10
 
 * published by the Free Software Foundation; either version 2 of
11
 
 * the License, or (at your option) any later version.
12
 
 *
13
 
 * This Program is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
 * GNU General Public License for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU General Public
19
 
 * License along with this Library; see the file COPYING.  If not,
20
 
 * write to the Free Software Foundation, Inc., 59 Temple Place,
21
 
 * Suite 330, Boston, MA 02111-1307, USA.
22
 
 *
23
 
 * Authors:
24
 
 *   Frederic Ruaudel <grumz@grumz.net>
25
 
 *   Rodrigo Moya <rodrigo@gnome-db.org>
26
 
 *   Pierre Wieser <pwieser@trychlos.org>
27
 
 *   ... and many others (see AUTHORS)
28
 
 */
29
 
 
30
 
#include <config.h>
31
 
#include <stdlib.h>
32
 
#include <stdio.h>
33
 
#include <glib/gi18n.h>
34
 
#include <glib/gstdio.h>
35
 
#include <unistd.h>
36
 
#include <libnautilus-actions/nautilus-actions-config.h>
37
 
#include <libnautilus-actions/nautilus-actions-config-schema-writer.h>
38
 
#include "nautilus-actions-tools-utils.h"
39
 
 
40
 
static gchar* label = "";
41
 
static gchar* tooltip = "";
42
 
static gchar* icon = "";
43
 
static gchar* command = "";
44
 
static gchar* params = "";
45
 
static gchar** matches = NULL;
46
 
static gboolean match_case = FALSE;
47
 
static gchar** mimetypes = NULL;
48
 
static gboolean isfile = FALSE;
49
 
static gboolean isdir = FALSE;
50
 
static gboolean accept_multiple_files = FALSE;
51
 
static gchar** schemes = NULL;
52
 
static gchar* output_file = NULL;
53
 
 
54
 
static GOptionEntry entries[] =
55
 
{
56
 
        { "label", 'l', 0, G_OPTION_ARG_STRING, &label, N_("The label of the menu item"), N_("LABEL") },
57
 
        { "tooltip", 't', 0, G_OPTION_ARG_STRING, &tooltip, N_("The tooltip of the menu item"), N_("TOOLTIP") },
58
 
        { "icon", 'i', 0, G_OPTION_ARG_STRING, &icon, N_("The icon of the menu item (filename or GTK stock ID)"), N_("ICON") },
59
 
        { "command", 'c', 0, G_OPTION_ARG_FILENAME, &command, N_("The path of the command"), N_("PATH") },
60
 
        { "parameters", 'p', 0, G_OPTION_ARG_STRING, &params, N_("The parameters of the command"), N_("PARAMS") },
61
 
        { "match", 'm', 0, G_OPTION_ARG_STRING_ARRAY, &matches, N_("A pattern to match selected files against. May include wildcards (* or ?) (you must set one option for each pattern you need)"), N_("EXPR") },
62
 
        { "match-case", 'C', 0, G_OPTION_ARG_NONE, &match_case, N_("The path of the command"), N_("PATH") },
63
 
        { "mimetypes", 'T', 0, G_OPTION_ARG_STRING_ARRAY, &mimetypes, N_("A pattern to match selected files' mimetype against. May include wildcards (* or ?) (you must set one option for each pattern you need)"), N_("EXPR") },
64
 
        { "accept-files", 'f', 0, G_OPTION_ARG_NONE, &isfile, N_("Set it if the selection can contain files"), NULL },
65
 
        { "accept-dirs", 'd', 0, G_OPTION_ARG_NONE, &isdir, N_("Set it if the selection can contain folders"), NULL },
66
 
        { "accept-multiple-files", 'M', 0, G_OPTION_ARG_NONE, &accept_multiple_files, N_("Set it if the selection can have several items"), NULL },
67
 
        { "scheme", 's', 0, G_OPTION_ARG_STRING_ARRAY, &schemes, N_("A GnomeVFS scheme where the selected files should be located (you must set it for each scheme you need)"), N_("SCHEME") },
68
 
        { "output-file", 'o', 0, G_OPTION_ARG_FILENAME, &output_file, N_("The path of the file where to save the new GConf schema definition file [default: /tmp/config_UUID.schemas]"), N_("PATH") },
69
 
        { NULL }
70
 
};
71
 
 
72
 
int main (int argc, char** argv)
73
 
{
74
 
        /*GSList* iter;*/
75
 
        GError * error = NULL;
76
 
        GOptionContext* context;
77
 
        gchar* path;
78
 
        gboolean success = FALSE;
79
 
        gchar* contents = NULL;
80
 
        gsize length = 0;
81
 
        NautilusActionsConfigAction* action;
82
 
        NautilusActionsConfigActionProfile* action_profile;
83
 
        GSList* basenames = NULL;
84
 
        GSList* mimetypes_list = NULL;
85
 
        GSList* schemes_list = NULL;
86
 
        int i;
87
 
 
88
 
        g_type_init ();
89
 
 
90
 
        context = g_option_context_new ("");
91
 
 
92
 
#ifdef ENABLE_NLS
93
 
        bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
94
 
# ifdef HAVE_BIND_TEXTDOMAIN_CODESET
95
 
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
96
 
# endif
97
 
        textdomain (GETTEXT_PACKAGE);
98
 
        g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
99
 
#else
100
 
        g_option_context_add_main_entries (context, entries, NULL);
101
 
#endif
102
 
 
103
 
        g_option_context_parse (context, &argc, &argv, &error);
104
 
 
105
 
        if (error != NULL)
106
 
        {
107
 
                fprintf (stderr, _("Syntax error:\n\t- %s\nTry %s --help\n"), error->message, g_get_prgname ());
108
 
                g_error_free (error);
109
 
                exit (EXIT_FAILURE);
110
 
        }
111
 
 
112
 
        action = nautilus_actions_config_action_new_default ();
113
 
        action_profile = nautilus_actions_config_action_get_profile (action, NULL);
114
 
        nautilus_actions_config_action_set_label (action, label);
115
 
        nautilus_actions_config_action_set_tooltip (action, tooltip);
116
 
        nautilus_actions_config_action_set_icon (action, icon);
117
 
        nautilus_actions_config_action_profile_set_path (action_profile, command);
118
 
        nautilus_actions_config_action_profile_set_parameters (action_profile, params);
119
 
 
120
 
        i = 0;
121
 
        while (matches != NULL && matches[i] != NULL)
122
 
        {
123
 
                basenames = g_slist_append (basenames, g_strdup (matches[i]));
124
 
                i++;
125
 
        }
126
 
        nautilus_actions_config_action_profile_set_basenames (action_profile, basenames);
127
 
        g_slist_foreach (basenames, (GFunc) g_free, NULL);
128
 
        g_slist_free (basenames);
129
 
 
130
 
        nautilus_actions_config_action_profile_set_match_case (action_profile, match_case);
131
 
 
132
 
        i = 0;
133
 
        while (mimetypes != NULL && mimetypes[i] != NULL)
134
 
        {
135
 
                mimetypes_list = g_slist_append (mimetypes_list, g_strdup (mimetypes[i]));
136
 
                i++;
137
 
        }
138
 
        nautilus_actions_config_action_profile_set_mimetypes (action_profile, mimetypes_list);
139
 
        g_slist_foreach (mimetypes_list, (GFunc) g_free, NULL);
140
 
        g_slist_free (mimetypes_list);
141
 
 
142
 
        nautilus_actions_config_action_profile_set_is_file (action_profile, isfile);
143
 
        nautilus_actions_config_action_profile_set_is_dir (action_profile, isdir);
144
 
        nautilus_actions_config_action_profile_set_accept_multiple (action_profile, accept_multiple_files);
145
 
 
146
 
        i = 0;
147
 
        while (schemes != NULL && schemes[i] != NULL)
148
 
        {
149
 
                schemes_list = g_slist_append (schemes_list, g_strdup (schemes[i]));
150
 
                i++;
151
 
        }
152
 
        nautilus_actions_config_action_profile_set_schemes (action_profile, schemes_list);
153
 
        g_slist_foreach (schemes_list, (GFunc) g_free, NULL);
154
 
        g_slist_free (schemes_list);
155
 
 
156
 
        NautilusActionsConfigSchemaWriter* schema_configs = nautilus_actions_config_schema_writer_get ();
157
 
        g_object_set (G_OBJECT (schema_configs), "save-path", "/tmp", NULL);
158
 
 
159
 
        printf (_("Creating %s..."), action->label);
160
 
        if (nautilus_actions_config_add_action (NAUTILUS_ACTIONS_CONFIG (schema_configs), action, &error))
161
 
        {
162
 
                success = TRUE;
163
 
                path = nautilus_actions_config_schema_writer_get_saved_filename (schema_configs, action->uuid);
164
 
                if (output_file)
165
 
                {
166
 
                        /* Copy the content of the temporary file into the one asked by the user */
167
 
                        if ((success = g_file_get_contents (path, &contents, &length, &error)))
168
 
                        {
169
 
                                success = nautilus_actions_file_set_contents (output_file, contents, length, &error);
170
 
                                g_free (contents);
171
 
                        }
172
 
 
173
 
                        /* --> Remove the temporary file */
174
 
                        g_unlink (path);
175
 
 
176
 
                        if (!success)
177
 
                        {
178
 
                                printf (_(" Failed: Can't create %s: %s\n"), output_file, error->message);
179
 
                                g_error_free (error);
180
 
                        }
181
 
                        g_free (path);
182
 
                        path = output_file;
183
 
                }
184
 
 
185
 
                if (success)
186
 
                {
187
 
                        printf (_("  OK, saved in %s\n"), path);
188
 
                }
189
 
        }
190
 
        else
191
 
        {
192
 
                printf (_(" Failed: %s\n"), error->message);
193
 
                g_error_free (error);
194
 
        }
195
 
 
196
 
        nautilus_actions_config_action_free (action);
197
 
        g_option_context_free (context);
198
 
 
199
 
        exit (EXIT_SUCCESS);
200
 
}