libgphoto2 photo camera library (libgphoto2) Internals  2.5.23
gphoto2-context.c
Go to the documentation of this file.
1 
24 #define _DEFAULT_SOURCE
25 
26 #include "config.h"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
34 
38 struct _GPContext
39 {
42 
47 
50 
53 
56 
59 
62 
63  unsigned int ref_count;
64 };
65 
73 GPContext *
75 {
76  GPContext *context;
77 
78  context = calloc (1, sizeof (GPContext));
79  if (!context)
80  return (NULL);
81 
82  context->ref_count = 1;
83 
84  return (context);
85 }
86 
92 void
94 {
95  if (!context)
96  return;
97 
98  context->ref_count++;
99 }
100 
101 static void
103 {
104  free (context);
105 }
106 
114 void
116 {
117  if (!context)
118  return;
119 
120  context->ref_count--;
121  if (!context->ref_count)
122  gp_context_free (context);
123 }
124 
134 void
136 {
137  if (!context)
138  return;
139 
140  if (context->idle_func)
141  context->idle_func (context, context->idle_func_data);
142 }
143 
154 unsigned int
155 gp_context_progress_start (GPContext *context, float target,
156  const char *format, ...)
157 {
158  va_list args;
159  char *str;
160  unsigned int id;
161 
162  if (!context)
163  return (0);
164  if (!context->progress_start_func)
165  return (0);
166 
167  va_start (args, format);
168  str = gpi_vsnprintf(format, args);
169  va_end (args);
170 
171  if (!str)
172  return 0;
173 
174  id = context->progress_start_func (context, target, str,
175  context->progress_func_data);
176  free (str);
177  return (id);
178 }
179 
180 void
181 gp_context_progress_update (GPContext *context, unsigned int id, float current)
182 {
183  if (!context)
184  return;
185 
186  if (context->progress_update_func)
187  context->progress_update_func (context, id, current,
188  context->progress_func_data);
189 }
190 
191 void
192 gp_context_progress_stop (GPContext *context, unsigned int id)
193 {
194  if (!context)
195  return;
196 
197  if (context->progress_stop_func)
198  context->progress_stop_func (context, id,
199  context->progress_func_data);
200 }
201 
202 void
203 gp_context_error (GPContext *context, const char *format, ...)
204 {
205  va_list args;
206  char *str;
207 
208  va_start (args, format);
209  str = gpi_vsnprintf(format, args);
210  va_end (args);
211 
212  if (!str)
213  return;
214 
215  /* Log the error message */
216  gp_log( GP_LOG_ERROR, __func__, "%s", str);
217 
218  if (context && context->error_func)
219  context->error_func (context, str, context->error_func_data);
220  free (str);
221 }
222 
223 void
224 gp_context_status (GPContext *context, const char *format, ...)
225 {
226  va_list args;
227  char *str;
228 
229  va_start (args, format);
230  str = gpi_vsnprintf(format, args);
231  va_end (args);
232 
233  if (!str)
234  return;
235 
236  /* Log the status message */
237  GP_LOG_D ("%s", str);
238 
239  if (context && context->status_func)
240  context->status_func (context, str, context->status_func_data);
241  free (str);
242 }
243 
256 void
257 gp_context_message (GPContext *context, const char *format, ...)
258 {
259  va_list args;
260  char *str;
261 
262  va_start (args, format);
263  str = gpi_vsnprintf(format, args);
264  va_end (args);
265 
266  if (!str)
267  return;
268 
269  /* Log the message */
270  GP_LOG_D ("%s", str);
271 
272  if (context && context->message_func)
273  context->message_func (context, str, context->message_func_data);
274  free (str);
275 }
276 
291 gp_context_question (GPContext *context, const char *format, ...)
292 {
293  GPContextFeedback feedback;
294  va_list args;
295  char *str;
296 
297  va_start (args, format);
298  str = gpi_vsnprintf(format, args);
299  va_end (args);
300 
301  if (!str)
302  return GP_CONTEXT_FEEDBACK_OK;
303 
304  feedback = GP_CONTEXT_FEEDBACK_OK;
305  if (context && context->question_func)
306  feedback = context->question_func (context, str, context->question_func_data);
307 
308  free (str);
309 
310  return feedback;
311 }
312 
324 {
325  if (!context)
326  return (GP_CONTEXT_FEEDBACK_OK);
327 
328  if (context->cancel_func)
329  return (context->cancel_func (context,
330  context->cancel_func_data));
331  else
332  return (GP_CONTEXT_FEEDBACK_OK);
333 }
334 
335 void
337  void *data)
338 {
339  if (!context)
340  return;
341 
342  context->idle_func = func;
343  context->idle_func_data = data;
344 }
345 
346 void
348  GPContextProgressStartFunc start_func,
349  GPContextProgressUpdateFunc update_func,
350  GPContextProgressStopFunc stop_func,
351  void *data)
352 {
353  if (!context)
354  return;
355 
356  context->progress_start_func = start_func;
357  context->progress_update_func = update_func;
358  context->progress_stop_func = stop_func;
359  context->progress_func_data = data;
360 }
361 
362 void
364  void *data)
365 {
366  if (!context)
367  return;
368 
369  context->error_func = func;
370  context->error_func_data = data;
371 }
372 
373 void
375  void *data)
376 {
377  if (!context)
378  return;
379 
380  context->question_func = func;
381  context->question_func_data = data;
382 }
383 
384 void
386  void *data)
387 {
388  if (!context)
389  return;
390 
391  context->status_func = func;
392  context->status_func_data = data;
393 }
394 
395 void
397  void *data)
398 {
399  if (!context)
400  return;
401 
402  context->cancel_func = func;
403  context->cancel_func_data = data;
404 }
405 
406 void
408  void *data)
409 {
410  if (!context)
411  return;
412 
413  context->message_func = func;
414  context->message_func_data = data;
415 }
_GPContext::cancel_func
GPContextCancelFunc cancel_func
Definition: gphoto2-context.c:54
_GPContext::progress_func_data
void * progress_func_data
Definition: gphoto2-context.c:46
_GPContext::progress_start_func
GPContextProgressStartFunc progress_start_func
Definition: gphoto2-context.c:43
gp_context_new
GPContext * gp_context_new(void)
Creates a new context.
Definition: gphoto2-context.c:74
gp_context_set_question_func
void gp_context_set_question_func(GPContext *context, GPContextQuestionFunc func, void *data)
Definition: gphoto2-context.c:374
_GPContext::idle_func_data
void * idle_func_data
Definition: gphoto2-context.c:41
_GPContext::ref_count
unsigned int ref_count
Definition: gphoto2-context.c:63
_GPContext::status_func
GPContextStatusFunc status_func
Definition: gphoto2-context.c:57
gp_context_set_cancel_func
void gp_context_set_cancel_func(GPContext *context, GPContextCancelFunc func, void *data)
Definition: gphoto2-context.c:396
gp_context_cancel
GPContextFeedback gp_context_cancel(GPContext *context)
Definition: gphoto2-context.c:323
gp_context_unref
void gp_context_unref(GPContext *context)
Decrements reference count of a context.
Definition: gphoto2-context.c:115
gp_context_question
GPContextFeedback gp_context_question(GPContext *context, const char *format,...)
Ask frontend user a question.
Definition: gphoto2-context.c:291
gp_context_progress_start
unsigned int gp_context_progress_start(GPContext *context, float target, const char *format,...)
Start progress tracking.
Definition: gphoto2-context.c:155
_GPContext::error_func
GPContextErrorFunc error_func
Definition: gphoto2-context.c:48
GPContextMessageFunc
void(* GPContextMessageFunc)(GPContext *context, const char *text, void *data)
Definition: gphoto2-context.h:64
_GPContext::cancel_func_data
void * cancel_func_data
Definition: gphoto2-context.c:55
GPContextProgressStopFunc
void(* GPContextProgressStopFunc)(GPContext *context, unsigned int id, void *data)
Definition: gphoto2-context.h:77
gp_context_set_error_func
void gp_context_set_error_func(GPContext *context, GPContextErrorFunc func, void *data)
Definition: gphoto2-context.c:363
_GPContext::error_func_data
void * error_func_data
Definition: gphoto2-context.c:49
GPContextProgressStartFunc
unsigned int(* GPContextProgressStartFunc)(GPContext *context, float target, const char *text, void *data)
Definition: gphoto2-context.h:69
gp_context_set_progress_funcs
void gp_context_set_progress_funcs(GPContext *context, GPContextProgressStartFunc start_func, GPContextProgressUpdateFunc update_func, GPContextProgressStopFunc stop_func, void *data)
Definition: gphoto2-context.c:347
_GPContext::idle_func
GPContextIdleFunc idle_func
Definition: gphoto2-context.c:40
gp_context_free
static void gp_context_free(GPContext *context)
Definition: gphoto2-context.c:102
gp_context_progress_stop
void gp_context_progress_stop(GPContext *context, unsigned int id)
Definition: gphoto2-context.c:192
GPContextIdleFunc
void(* GPContextIdleFunc)(GPContext *context, void *data)
Definition: gphoto2-context.h:61
gp_context_status
void gp_context_status(GPContext *context, const char *format,...)
Definition: gphoto2-context.c:224
GPContextProgressUpdateFunc
void(* GPContextProgressUpdateFunc)(GPContext *context, unsigned int id, float current, void *data)
Definition: gphoto2-context.h:73
_GPContext::question_func
GPContextQuestionFunc question_func
Definition: gphoto2-context.c:51
gp_context_ref
void gp_context_ref(GPContext *context)
Definition: gphoto2-context.c:93
_GPContext::progress_stop_func
GPContextProgressStopFunc progress_stop_func
Definition: gphoto2-context.c:45
_GPContext::message_func_data
void * message_func_data
Definition: gphoto2-context.c:61
gp_context_set_message_func
void gp_context_set_message_func(GPContext *context, GPContextMessageFunc func, void *data)
Definition: gphoto2-context.c:407
_GPContext::message_func
GPContextMessageFunc message_func
Definition: gphoto2-context.c:60
GP_LOG_ERROR
@ GP_LOG_ERROR
Log message is an error infomation.
Definition: gphoto2-port-log.h:31
gp_context_message
void gp_context_message(GPContext *context, const char *format,...)
Print a message to the context.
Definition: gphoto2-context.c:257
gp_context_error
void gp_context_error(GPContext *context, const char *format,...)
Definition: gphoto2-context.c:203
gp_context_idle
void gp_context_idle(GPContext *context)
Notify frontend of a brief idle time.
Definition: gphoto2-context.c:135
GPContextCancelFunc
GPContextFeedback(* GPContextCancelFunc)(GPContext *context, void *data)
Definition: gphoto2-context.h:67
gp_context_progress_update
void gp_context_progress_update(GPContext *context, unsigned int id, float current)
Definition: gphoto2-context.c:181
GP_CONTEXT_FEEDBACK_OK
@ GP_CONTEXT_FEEDBACK_OK
Definition: gphoto2-context.h:56
GPContextErrorFunc
void(* GPContextErrorFunc)(GPContext *context, const char *text, void *data)
Definition: gphoto2-context.h:62
gphoto2-port-log.h
gp_context_set_idle_func
void gp_context_set_idle_func(GPContext *context, GPContextIdleFunc func, void *data)
Definition: gphoto2-context.c:336
_GPContext::status_func_data
void * status_func_data
Definition: gphoto2-context.c:58
GPContextStatusFunc
void(* GPContextStatusFunc)(GPContext *context, const char *text, void *data)
Definition: gphoto2-context.h:63
GPContextFeedback
enum _GPContextFeedback GPContextFeedback
Return codes that can be returned by progress handling.
GPContextQuestionFunc
GPContextFeedback(* GPContextQuestionFunc)(GPContext *context, const char *text, void *data)
Definition: gphoto2-context.h:65
gp_log
void gp_log(GPLogLevel level, const char *domain, const char *format,...)
Log a debug or error message.
Definition: gphoto2-port-log.c:331
gpi_vsnprintf
char * gpi_vsnprintf(const char *format, va_list args)
Definition: gphoto2-port-log.c:101
_GPContext::progress_update_func
GPContextProgressUpdateFunc progress_update_func
Definition: gphoto2-context.c:44
gphoto2-context.h
Context callback operation functions.
_GPContext::question_func_data
void * question_func_data
Definition: gphoto2-context.c:52
_GPContext
Definition: gphoto2-context.c:39
gp_context_set_status_func
void gp_context_set_status_func(GPContext *context, GPContextStatusFunc func, void *data)
Definition: gphoto2-context.c:385