gio/ docs/reference/gio Merged gio-standalone into glib.
[platform/upstream/glib.git] / gio / gmountoperation.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include "gmountoperation.h"
28 #include "gio-marshal.h"
29 #include "glibintl.h"
30
31 G_DEFINE_TYPE (GMountOperation, g_mount_operation, G_TYPE_OBJECT);
32
33 enum {
34   ASK_PASSWORD,
35   ASK_QUESTION,
36   REPLY,
37   LAST_SIGNAL
38 };
39
40 static guint signals[LAST_SIGNAL] = { 0 };
41
42 struct _GMountOperationPrivate {
43   char *password;
44   char *user;
45   char *domain;
46   gboolean anonymous;
47   GPasswordSave password_save;
48   int choice;
49 };
50
51 static void
52 g_mount_operation_finalize (GObject *object)
53 {
54   GMountOperation *operation;
55   GMountOperationPrivate *priv;
56
57   operation = G_MOUNT_OPERATION (object);
58
59   priv = operation->priv;
60   
61   g_free (priv->password);
62   g_free (priv->user);
63   g_free (priv->domain);
64   
65   if (G_OBJECT_CLASS (g_mount_operation_parent_class)->finalize)
66     (*G_OBJECT_CLASS (g_mount_operation_parent_class)->finalize) (object);
67 }
68
69 static gboolean
70 boolean_handled_accumulator (GSignalInvocationHint *ihint,
71                              GValue                *return_accu,
72                              const GValue          *handler_return,
73                              gpointer               dummy)
74 {
75   gboolean continue_emission;
76   gboolean signal_handled;
77   
78   signal_handled = g_value_get_boolean (handler_return);
79   g_value_set_boolean (return_accu, signal_handled);
80   continue_emission = !signal_handled;
81   
82   return continue_emission;
83 }
84
85 static gboolean
86 ask_password (GMountOperation *op,
87               const char      *message,
88               const char      *default_user,
89               const char      *default_domain,
90               GPasswordFlags   flags)
91 {
92   return FALSE;
93 }
94   
95 static gboolean
96 ask_question (GMountOperation *op,
97               const char      *message,
98               const char      *choices[])
99 {
100   return FALSE;
101 }
102
103 static void
104 g_mount_operation_class_init (GMountOperationClass *klass)
105 {
106   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
107   
108   g_type_class_add_private (klass, sizeof (GMountOperationPrivate));
109   
110   gobject_class->finalize = g_mount_operation_finalize;
111   
112   klass->ask_password = ask_password;
113   klass->ask_question = ask_question;
114   
115   signals[ASK_PASSWORD] =
116     g_signal_new (I_("ask_password"),
117                   G_TYPE_FROM_CLASS (gobject_class),
118                   G_SIGNAL_RUN_LAST,
119                   G_STRUCT_OFFSET (GMountOperationClass, ask_password),
120                   boolean_handled_accumulator, NULL,
121                   _gio_marshal_BOOLEAN__STRING_STRING_STRING_INT,
122                   G_TYPE_BOOLEAN, 4,
123                   G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT);
124   
125   signals[ASK_QUESTION] =
126     g_signal_new (I_("ask_question"),
127                   G_TYPE_FROM_CLASS (gobject_class),
128                   G_SIGNAL_RUN_LAST,
129                   G_STRUCT_OFFSET (GMountOperationClass, ask_question),
130                   boolean_handled_accumulator, NULL,
131                   _gio_marshal_BOOLEAN__STRING_POINTER,
132                   G_TYPE_BOOLEAN, 2,
133                   G_TYPE_STRING, G_TYPE_POINTER);
134
135   signals[REPLY] =
136     g_signal_new (I_("reply"),
137                   G_TYPE_FROM_CLASS (gobject_class),
138                   G_SIGNAL_RUN_LAST,
139                   G_STRUCT_OFFSET (GMountOperationClass, reply),
140                   NULL, NULL,
141                   g_cclosure_marshal_VOID__BOOLEAN,
142                   G_TYPE_NONE, 1,
143                   G_TYPE_BOOLEAN);
144 }
145
146 static void
147 g_mount_operation_init (GMountOperation *operation)
148 {
149   operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
150                                                  G_TYPE_MOUNT_OPERATION,
151                                                  GMountOperationPrivate);
152 }
153
154 /**
155  * g_mount_operation_new:
156  * 
157  * Returns: a new #GMountOperation.
158  **/
159 GMountOperation *
160 g_mount_operation_new (void)
161 {
162   return g_object_new (G_TYPE_MOUNT_OPERATION, NULL);
163 }
164
165 /**
166  * g_mount_operation_get_username
167  * @op:
168  * 
169  * Returns: 
170  **/
171 const char *
172 g_mount_operation_get_username (GMountOperation *op)
173 {
174   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
175   return op->priv->user;
176 }
177
178 /**
179  * g_mount_operation_set_username:
180  * @op:
181  * @username: input username.
182  * 
183  **/
184 void
185 g_mount_operation_set_username (GMountOperation *op,
186                                 const char      *username)
187 {
188   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
189   g_free (op->priv->user);
190   op->priv->user = g_strdup (username);
191 }
192
193 /**
194  * g_mount_operation_get_password:
195  * @op:
196  * 
197  * Returns:  
198  **/
199 const char *
200 g_mount_operation_get_password (GMountOperation *op)
201 {
202   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
203   return op->priv->password;
204 }
205
206 /**
207  * g_mount_operation_set_password:
208  * @op: the given #GMountOperation.
209  * @password: password to set.
210  * 
211  * Sets the mount operation's password to @password.  
212  *
213  **/
214 void
215 g_mount_operation_set_password (GMountOperation *op,
216                                 const char      *password)
217 {
218   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
219   g_free (op->priv->password);
220   op->priv->password = g_strdup (password);
221 }
222
223 /**
224  * g_mount_operation_get_anonymous:
225  * @op:
226  * 
227  * Returns: %TRUE if mount operation is anonymous. 
228  **/
229 gboolean
230 g_mount_operation_get_anonymous (GMountOperation *op)
231 {
232   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), FALSE);
233   return op->priv->anonymous;
234 }
235
236 /**
237  * g_mount_operation_set_anonymous:
238  * @op: the given #GMountOperation.
239  * @anonymous: boolean value.
240  * 
241  **/  
242 void
243 g_mount_operation_set_anonymous (GMountOperation *op,
244                                  gboolean         anonymous)
245 {
246   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
247   op->priv->anonymous = anonymous;
248 }
249
250 /**
251  * g_mount_operation_get_domain:
252  * @op:
253  * 
254  * Returns: a const string set to the domain.
255  **/
256 const char *
257 g_mount_operation_get_domain (GMountOperation *op)
258 {
259   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
260   return op->priv->domain;
261 }
262
263 /**
264  * g_mount_operation_set_domain:
265  * @op: the given #GMountOperation.
266  * @domain: the domain to set.
267  * 
268  * Sets the mount operation's domain. 
269  **/  
270 void
271 g_mount_operation_set_domain (GMountOperation *op,
272                               const char      *domain)
273 {
274   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
275   g_free (op->priv->domain);
276   op->priv->domain = g_strdup (domain);
277 }
278
279 /**
280  * g_mount_operation_get_password_save:
281  * @op: the given #GMountOperation.
282  *
283  * Returns: #GPasswordSave. 
284  **/  
285
286 GPasswordSave
287 g_mount_operation_get_password_save (GMountOperation *op)
288 {
289   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), G_PASSWORD_SAVE_NEVER);
290   return op->priv->password_save;
291 }
292
293 /**
294  * g_mount_operation_set_password_save
295  * @op:
296  * @save: #GPasswordSave
297  * 
298  **/   
299 void
300 g_mount_operation_set_password_save (GMountOperation *op,
301                                      GPasswordSave    save)
302 {
303   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
304   op->priv->password_save = save;
305 }
306
307 /**
308  * g_mount_operation_get_choice:
309  * @op:
310  * 
311  * Returns: 
312  **/
313 int
314 g_mount_operation_get_choice (GMountOperation *op)
315 {
316   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), 0);
317   return op->priv->choice;
318 }
319
320 /**
321  * g_mount_operation_set_choice:
322  * @op:
323  * @choice:
324  *  
325  **/
326 void
327 g_mount_operation_set_choice (GMountOperation *op,
328                               int choice)
329 {
330   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
331   op->priv->choice = choice;
332 }
333
334 /**
335  * g_mount_operation_reply:
336  * @op: #GMountOperation.
337  * @abort: boolean.
338  * 
339  * Emits the #GMountOperation::Reply signal with the abort flag set to
340  * @abort.
341  **/
342 void
343 g_mount_operation_reply (GMountOperation *op,
344                          gboolean         abort)
345 {
346   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
347   g_signal_emit (op, signals[REPLY], 0, abort);
348 }