Make read operations a bit more granular, so we get to execute the main loop a bit...
[giggle.git] / src / giggle-git-write-config.c
blob48c90f4df21d605a53984c675bceb24c3597921f
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2007 Imendio AB
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include <config.h>
23 #include <string.h>
25 #include "giggle-git-write-config.h"
27 typedef struct GiggleGitWriteConfigPriv GiggleGitWriteConfigPriv;
29 struct GiggleGitWriteConfigPriv {
30 gboolean global;
31 gchar *field;
32 gchar *value;
35 static void git_write_config_finalize (GObject *object);
36 static void git_write_config_get_property (GObject *object,
37 guint param_id,
38 GValue *value,
39 GParamSpec *pspec);
40 static void git_write_config_set_property (GObject *object,
41 guint param_id,
42 const GValue *value,
43 GParamSpec *pspec);
45 static gboolean git_write_config_get_command_line (GiggleJob *job,
46 gchar **command_line);
47 static void git_write_config_handle_output (GiggleJob *job,
48 const gchar *output_str,
49 gsize output_len);
52 G_DEFINE_TYPE (GiggleGitWriteConfig, giggle_git_write_config, GIGGLE_TYPE_JOB)
54 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GIGGLE_TYPE_GIT_WRITE_CONFIG, GiggleGitWriteConfigPriv))
56 enum {
57 PROP_0,
58 PROP_GLOBAL,
59 PROP_FIELD,
60 PROP_VALUE,
64 static void
65 giggle_git_write_config_class_init (GiggleGitWriteConfigClass *class)
67 GObjectClass *object_class = G_OBJECT_CLASS (class);
68 GiggleJobClass *job_class = GIGGLE_JOB_CLASS (class);
70 object_class->finalize = git_write_config_finalize;
71 object_class->get_property = git_write_config_get_property;
72 object_class->set_property = git_write_config_set_property;
74 job_class->get_command_line = git_write_config_get_command_line;
75 job_class->handle_output = git_write_config_handle_output;
77 g_object_class_install_property (object_class,
78 PROP_GLOBAL,
79 g_param_spec_boolean ("global",
80 "global",
81 "Whether the setting is global",
82 FALSE,
83 G_PARAM_READWRITE));
84 g_object_class_install_property (object_class,
85 PROP_FIELD,
86 g_param_spec_string ("field",
87 "field",
88 "configuration field to modify",
89 NULL,
90 G_PARAM_READWRITE));
91 g_object_class_install_property (object_class,
92 PROP_VALUE,
93 g_param_spec_string ("value",
94 "value",
95 "value to assign to the field",
96 NULL,
97 G_PARAM_READWRITE));
99 g_type_class_add_private (object_class, sizeof (GiggleGitWriteConfigPriv));
102 static void
103 giggle_git_write_config_init (GiggleGitWriteConfig *config)
107 static void
108 git_write_config_finalize (GObject *object)
110 GiggleGitWriteConfigPriv *priv;
112 priv = GET_PRIV (object);
114 g_free (priv->field);
115 g_free (priv->value);
117 G_OBJECT_CLASS (giggle_git_write_config_parent_class)->finalize (object);
120 static void
121 git_write_config_get_property (GObject *object,
122 guint param_id,
123 GValue *value,
124 GParamSpec *pspec)
126 GiggleGitWriteConfigPriv *priv;
128 priv = GET_PRIV (object);
130 switch (param_id) {
131 case PROP_GLOBAL:
132 g_value_set_boolean (value, priv->global);
133 break;
134 case PROP_FIELD:
135 g_value_set_string (value, priv->field);
136 break;
137 case PROP_VALUE:
138 g_value_set_string (value, priv->value);
139 break;
140 default:
141 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
142 break;
146 static void
147 git_write_config_set_property (GObject *object,
148 guint param_id,
149 const GValue *value,
150 GParamSpec *pspec)
152 GiggleGitWriteConfigPriv *priv;
154 priv = GET_PRIV (object);
156 switch (param_id) {
157 case PROP_GLOBAL:
158 priv->global = g_value_get_boolean (value);
159 break;
160 case PROP_FIELD:
161 if (priv->field) {
162 g_free (priv->field);
164 priv->field = g_value_dup_string (value);
165 break;
166 case PROP_VALUE:
167 if (priv->value) {
168 g_free (priv->value);
170 priv->value = g_value_dup_string (value);
171 break;
172 default:
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
174 break;
178 static gboolean
179 git_write_config_get_command_line (GiggleJob *job,
180 gchar **command_line)
182 GiggleGitWriteConfigPriv *priv;
184 priv = GET_PRIV (job);
186 *command_line = g_strdup_printf (GIT_COMMAND " repo-config %s %s \"%s\"",
187 (priv->global) ? "--global" : "",
188 priv->field, priv->value);
189 return TRUE;
192 static void
193 git_write_config_handle_output (GiggleJob *job,
194 const gchar *output_str,
195 gsize output_len)
197 /* Ignore output */
200 GiggleJob *
201 giggle_git_write_config_new (const gchar *field,
202 const gchar *value)
204 g_return_val_if_fail (field != NULL, NULL);
206 return g_object_new (GIGGLE_TYPE_GIT_WRITE_CONFIG,
207 "field", field,
208 "value", value,
209 NULL);