kviewshell
DjVuGlobal.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #ifdef HAVE_CONFIG_H
00058 # include "config.h"
00059 #endif
00060 #if NEED_GNUG_PRAGMAS
00061 # pragma implementation
00062 #endif
00063
00070 #ifdef NEED_DJVU_PROGRESS
00071 #include "DjVuGlobal.h"
00072
00073
00074
00075
00076 #include "GOS.h"
00077 #include "GThreads.h"
00078 #include "GException.h"
00079 #include "GContainer.h"
00080 #include <stdlib.h>
00081 #include <stdio.h>
00082 #include <string.h>
00083
00084 #define INITIAL 500
00085 #define INTERVAL 250
00086
00087 class DjVuProgressTask::Data : public GPEnabled
00088 {
00089 public:
00090 djvu_progress_callback *callback;
00091 DjVuProgressTask *head;
00092 const char *gtask;
00093 unsigned long lastsigdate;
00094 Data(djvu_progress_callback *_callback):
00095 callback(_callback), head(0), gtask(0), lastsigdate(0) {}
00096 };
00097
00098
00099 static GPMap<void *,DjVuProgressTask::Data> &
00100 get_map(void)
00101 {
00102 static GPMap<void *,DjVuProgressTask::Data> xmap;
00103 return xmap;
00104 }
00105
00106 djvu_progress_callback *
00107 DjVuProgressTask::set_callback(djvu_progress_callback *_callback)
00108 {
00109 djvu_progress_callback *retval=0;
00110 if(_callback)
00111 {
00112 GMap<void *,GP<DjVuProgressTask::Data> > &map=get_map();
00113 void *threadID=GThread::current();
00114 if(map.contains(threadID))
00115 {
00116 DjVuProgressTask::Data &data=*(map[threadID]);
00117 retval=data.callback;
00118 data.callback=_callback;
00119 data.head=0;
00120 data.gtask=0;
00121 data.lastsigdate=0;
00122 }else
00123 {
00124 map[threadID]=new Data(_callback);
00125 }
00126 }else
00127 {
00128 GMap<void *,GP<DjVuProgressTask::Data> > &map=get_map();
00129 void *threadID=GThread::current();
00130 if(map.contains(threadID))
00131 {
00132 DjVuProgressTask::Data &data=*(map[threadID]);
00133 retval=data.callback;
00134 data.callback=0;
00135 data.head=0;
00136 data.gtask=0;
00137 data.lastsigdate=0;
00138 map.del(threadID);
00139 }
00140 }
00141 return retval;
00142 }
00143
00144 DjVuProgressTask::DjVuProgressTask(const char *xtask,int nsteps)
00145 : task(xtask),parent(0), nsteps(nsteps), runtostep(0), gdata(0), data(0)
00146 {
00147
00148 {
00149 GMap<void *,GP<DjVuProgressTask::Data> > &map=get_map();
00150 void *threadID=GThread::current();
00151 if(map.contains(threadID))
00152 {
00153 gdata=new GP<Data>;
00154 Data &d=*(data=((*(GP<Data> *)gdata)=map[threadID]));
00155 if(d.callback)
00156 {
00157 unsigned long curdate = GOS::ticks();
00158 startdate = curdate;
00159 if (!d.head)
00160 d.lastsigdate = curdate + INITIAL;
00161 parent = d.head;
00162 d.head = this;
00163 }
00164 }
00165 }
00166 }
00167
00168 DjVuProgressTask::~DjVuProgressTask()
00169 {
00170 if (data && data->callback)
00171 {
00172 if (data->head != this)
00173 G_THROW( ERR_MSG("DjVuGlobal.not_compatible") );
00174 data->head = parent;
00175 if (!parent)
00176 {
00177 unsigned long curdate = GOS::ticks();
00178 if((*(data->callback))(data->gtask?data->gtask:"",curdate-startdate, curdate-startdate))
00179 {
00180 G_THROW("INTERRUPT");
00181 }
00182 }
00183 }
00184 delete (GP<Data> *)gdata;
00185 }
00186
00187 void
00188 DjVuProgressTask::run(int tostep)
00189 {
00190 if(data)
00191 {
00192 data->gtask=task;
00193 if ((data->callback)&&(tostep>runtostep))
00194 {
00195 unsigned long curdate = GOS::ticks();
00196 if (curdate > data->lastsigdate + INTERVAL)
00197 signal(curdate, curdate);
00198 runtostep = tostep;
00199 }
00200 }
00201 }
00202
00203 void
00204 DjVuProgressTask::signal(unsigned long curdate, unsigned long estdate)
00205 {
00206 int inprogress = runtostep;
00207 if (inprogress > nsteps)
00208 inprogress = nsteps;
00209 if (inprogress > 0)
00210 {
00211 const unsigned long enddate = startdate+
00212 (unsigned long)(((float)(estdate-startdate) * (float)nsteps) / (float)inprogress);
00213 if (parent)
00214 {
00215 parent->signal(curdate, enddate);
00216 }
00217 else if (data && data->callback && curdate<enddate)
00218 {
00219 if((*(data->callback))(data->gtask?data->gtask:"",curdate-startdate, enddate-startdate))
00220 {
00221 G_THROW("INTERRUPT");
00222 }
00223 data->lastsigdate = curdate;
00224 }
00225 }
00226 }
00227
00228
00229
00230 djvu_progress_callback *
00231 djvu_set_progress_callback( djvu_progress_callback *callback )
00232 {
00233 return DjVuProgressTask::set_callback(callback);
00234 }
00235
00236 int djvu_supports_progress_callback(void) {return 1;}
00237
00238 #else
00239
00240 #ifndef HAS_DJVU_PROGRESS_TYPEDEF
00241 extern "C"
00242 {
00243 void *djvu_set_progress_callback(void *);
00244 int djvu_supports_progress_callback(void);
00245 }
00246 void *djvu_set_progress_callback(void *) { return 0; }
00247 int djvu_supports_progress_callback(void) {return 0;}
00248 #else
00249 int djvu_supports_progress_callback(void) {return 0;}
00250 djvu_progress_callback *
00251 djvu_set_progress_callback( djvu_progress_callback *) { return 0; }
00252 #endif
00253
00254 #endif
00255