1 /*
2  * zphoto - a zooming photo album generator.
3  *
4  * Copyright (C) 2000  Satoru Takabayashi <satoru@namazu.org>
5  * All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 
23 #include <stdlib.h>
24 #include <time.h>
25 #include <assert.h>
26 #include <setjmp.h>
27 #include "zphoto.h"
28 #include "config.h"
29 
30 static void
progress_quiet(ZphotoProgress * progress)31 progress_quiet (ZphotoProgress *progress)
32 {
33     /* do nothing */
34 }
35 
36 ZphotoProgress *
zphoto_progress_new(void)37 zphoto_progress_new (void)
38 {
39     ZphotoProgress *progress;
40 
41     progress = zphoto_emalloc(sizeof(ZphotoProgress));
42     progress->func = progress_quiet;
43     progress->data = NULL;
44     progress->abort_p = 0;
45 
46     return progress;
47 }
48 
49 void
zphoto_progress_start(ZphotoProgress * progress,const char * task,const char * task_long,int total)50 zphoto_progress_start (ZphotoProgress *progress,
51                        const char *task, const char *task_long, int total)
52 {
53     assert(total >=0 && task != NULL);
54 
55     progress->current   = 0;
56     progress->previous  = 0;
57     progress->total     = total;
58     progress->task      = zphoto_strdup(task);
59     progress->task_long = zphoto_strdup(task_long);
60     progress->is_finished = 0;
61     progress->start_processer_time =  clock();
62     progress->start_time = time(NULL);
63     progress->file_name = "";
64 
65     zphoto_progress_set(progress, 0, "");
66 }
67 
68 void
zphoto_progress_finish(ZphotoProgress * progress)69 zphoto_progress_finish (ZphotoProgress *progress)
70 {
71     assert(progress->is_finished == 0);
72 
73     progress->current     = progress->total;
74     progress->is_finished = 1;
75     progress->func(progress);
76 
77     free(progress->task);
78     free(progress->task_long);
79 }
80 
81 void
zphoto_progress_destroy(ZphotoProgress * progress)82 zphoto_progress_destroy (ZphotoProgress *progress)
83 {
84     free(progress);
85 }
86 
87 void
zphoto_progress_set(ZphotoProgress * progress,int count,const char * file_name)88 zphoto_progress_set (ZphotoProgress *progress,
89 		     int count, const char *file_name)
90 {
91     assert(count >= progress->previous);
92 
93     progress->current = count;
94     progress->file_name = file_name;
95     progress->func(progress);
96     progress->previous = count;
97 
98     /*
99      * FIXME: No consideration for resource handling.
100      * Some amount of memory will probably be leaked.
101      */
102     if (progress->abort_p) {
103         longjmp(progress->jmpbuf, 1);
104     }
105 }
106 
107 
108 void
zphoto_progress_set_func(ZphotoProgress * progress,ZphotoProgressFunc func)109 zphoto_progress_set_func (ZphotoProgress *progress, ZphotoProgressFunc func)
110 {
111     progress->func = func;
112 }
113 
114 void
zphoto_progress_set_data(ZphotoProgress * progress,void * data)115 zphoto_progress_set_data (ZphotoProgress *progress, void *data)
116 {
117     progress->data = data;
118 }
119 
120 void
zphoto_progress_abort(ZphotoProgress * progress)121 zphoto_progress_abort (ZphotoProgress *progress)
122 {
123     progress->abort_p = 1;
124 }
125 
126