00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "PageInfo.h"
00020 #include <pthread.h>
00021 #include "PDFViewerLog.h"
00022 #include "controller.h"
00023 #include <math.h>
00024
00025 static const double e = 0.001;
00026
00027 CPageInfo::CPageInfo(void)
00028 {
00029 initialize();
00030 }
00031
00032 CPageInfo::~CPageInfo(void)
00033 {
00034 clear();
00035 }
00036
00037 void CPageInfo::initialize()
00038 {
00039 memset(this, 0, sizeof(CPageInfo));
00040 }
00041
00042 void CPageInfo::clear()
00043 {
00044 if (bitmap)
00045 {
00046 delete bitmap; bitmap = NULL;
00047 }
00048 if (links)
00049 {
00050 delete links; links = NULL;
00051 }
00052 if (text)
00053 {
00054 delete text; text = NULL;
00055 }
00056 }
00057
00058 GBool CPageInfo::isSame(const int pn, const double zoom, const int rot)
00059 {
00060 if (pn == pageNumber && rot == rotate)
00061 {
00062 return (fabs(zoom - pageZoom) < e);
00063 }
00064 return gFalse;
00065 }
00066
00067 double CPageInfo::calZoom()
00068 {
00069 return pageZoom;
00070 }
00072
00074 PageList::PageList()
00075 {
00076 length = 0; list = NULL;
00077 initialize();
00078 }
00079
00080 PageList::~PageList()
00081 {
00082 clear();
00083 delete [] list; list = NULL; length = 0;
00084 pthread_mutex_destroy(&mutex);
00085 }
00086
00087 void PageList::initialize()
00088 {
00089 pthread_mutex_init(&mutex, NULL);
00090 }
00091
00092 void PageList::clear()
00093 {
00094 pthread_mutex_lock(&mutex);
00095 for(int i = 0; i < length ; ++i)
00096 {
00097 if (list[i])
00098 {
00099 delete list[i];
00100 list[i] = NULL;
00101 }
00102 }
00103 pthread_mutex_unlock(&mutex);
00104 }
00105
00106 void PageList::reset(const int size)
00107 {
00108 pthread_mutex_lock(&mutex);
00109 for(int i = 0; i < length ; ++i)
00110 {
00111 if (list[i])
00112 {
00113 delete list[i];
00114 list[i] = NULL;
00115 }
00116 }
00117 length = size;
00118 if (list)
00119 {
00120 delete [] list; list = NULL;
00121 }
00122 list = new PageInfoPtr[size];
00123 memset(list, 0, length * sizeof(PageInfoPtr));
00124 pthread_mutex_unlock(&mutex);
00125 }
00126
00127 void PageList::addPage(CPageInfo * info)
00128 {
00129 if (NULL == info) return;
00130 int index = info->pageNumber;
00131 if (index < 1 || index > length) return;
00132 pthread_mutex_lock(&mutex);
00133 --index;
00134 if (list[index] == NULL)
00135 {
00136 list[index] = info;
00137 }
00138 else
00139 {
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 delete list[index]; list[index] = info;
00153 }
00154 pthread_mutex_unlock(&mutex);
00155 }
00156
00158
00159
00160
00161 void PageList::removeOldestOne(const int center)
00162 {
00163 int index = -1, i;
00164 int min = 0x7fffffff;
00165 CPageInfo * pItem = NULL;
00166 pthread_mutex_lock(&mutex);
00167
00168
00169 for(i = 0; i < center - ProtectWidth && i < length; ++i)
00170 {
00171 pItem = list[i];
00172 if (pItem && min > pItem->timestamp)
00173 {
00174 index = i; min = pItem->timestamp;
00175 }
00176 }
00177 for(i = center + ProtectWidth; i < length; ++i)
00178 {
00179 pItem = list[i];
00180 if (pItem && min > pItem->timestamp)
00181 {
00182 index = i; min = pItem->timestamp;
00183 }
00184 }
00185 if (index >= 0 && index < length)
00186 {
00187 PV_LOGPRINTF("delete page %d", list[index]->pageNumber);
00188 delete list[index]; list[index] = NULL;
00189 pthread_mutex_unlock(&mutex);
00190 return;
00191 }
00192
00193
00194 index = -1;
00195 int dist = 0, pos = -1;
00196 for(i = -ProtectWidth; i <= ProtectWidth; ++i)
00197 {
00198 index = center + i - 1;
00199 if (index < 0) index = 0;
00200 else if (index >= length) index = length - 1;
00201 if (list[index] && abs(index + 1 - center) > dist)
00202 {
00203 pos = index;
00204 dist = abs(index + 1 - center);
00205 }
00206 }
00207 if (pos >= 0 && pos != center - 1)
00208 {
00209 PV_LOGPRINTF("delete page %d\n", list[pos]->pageNumber);
00210 delete list[pos]; list[pos] = NULL;
00211 }
00212 pthread_mutex_unlock(&mutex);
00213 }
00214
00215 CPageInfo * PageList::getPage(const int number)
00216 {
00217 CPageInfo * pItem = NULL;
00218 if (number < 1 || number > length) return NULL;
00219 pthread_mutex_lock(&mutex);
00220 pItem = list[number - 1];
00221 pthread_mutex_unlock(&mutex);
00222 return pItem;
00223 }
00224
00225
00226 CPageInfo * PageList::checkPage(const CPageInfo & info)
00227 {
00228 CPageInfo * pItem = getPage(info.pageNumber);
00229 if (NULL == pItem) return NULL;
00230 if (pItem->isSame(info.pageNumber, info.pageZoom, info.rotate))
00231 {
00232 return pItem;
00233 }
00234 return NULL;
00235 }
00236
00237 CPageInfo * PageList::checkPage(const TodoItem * todo)
00238 {
00239 if (NULL == todo) return NULL;
00240 CPageInfo * pItem = getPage(todo->pageNumber);
00241 if (NULL == pItem) return NULL;
00242 if (pItem->isSame(todo->pageNumber, todo->zoom, todo->rotate))
00243 {
00244 return pItem;
00245 }
00246 return NULL;
00247 }
00248
00249 void PageList::dump()
00250 {
00251 PV_DUMP("\n\nPageList:\n");
00252 CPageInfo * pItem = NULL;
00253 pthread_mutex_lock(&mutex);
00254 for(int i = 0; i < length; ++i)
00255 {
00256 pItem = list[i];
00257 if (pItem)
00258 {
00259 PV_DUMP("page %d\t", pItem->pageNumber);
00260 PV_DUMP("zoom %lf\t", pItem->pageZoom);
00261 PV_DUMP("rotate %d\t", pItem->rotate);
00262 PV_DUMP("time %d\n", pItem->timestamp);
00263 }
00264 }
00265 PV_DUMP("\n\n");
00266 pthread_mutex_unlock(&mutex);
00267 }
00268
00269
00271
00273 TodoItem::TodoItem()
00274 {
00275 zoom = -1;
00276 rotate = 0;
00277 pageNumber = 1;
00278 timeStamp = 0;
00279 }
00280
00281 TodoItem::~TodoItem()
00282 {
00283 }
00284
00285 TodoItem::TodoItem(const TodoItem & right)
00286 : zoom (right.zoom)
00287 , rotate(right.rotate)
00288 , pageNumber(right.pageNumber)
00289 , timeStamp(right.timeStamp)
00290 {
00291 }
00292
00294
00296 TodoList::TodoList()
00297 {
00298 initialize();
00299 }
00300
00301 TodoList::~TodoList()
00302 {
00303 clear();
00304 delete list; list = NULL;
00305 pthread_mutex_destroy(&mutex);
00306 }
00307
00308 void TodoList::initialize()
00309 {
00310 pthread_mutex_init(&mutex, NULL);
00311 list = new GooList();
00312 }
00313
00314 void TodoList::clear()
00315 {
00316 pthread_mutex_lock(&mutex);
00317 TodoItem *pItem = NULL;
00318 while (list->getLength())
00319 {
00320 pItem = (TodoItem *)list->del(0);
00321 delete pItem;
00322 }
00323 pthread_mutex_unlock(&mutex);
00324 }
00325
00326 void TodoList::removeDup(TodoItem * item)
00327 {
00328 int i = 0, length = list->getLength();
00329 TodoItem * p = NULL;
00330 while (i < length)
00331 {
00332 p = (TodoItem *)list->get(i);
00333 if ( p && item && p->pageNumber == item->pageNumber && item->timeStamp > p->timeStamp)
00334 {
00335
00336 list->del(i); --length;
00337 }
00338 else
00339 {
00340 ++i;
00341 }
00342 }
00343 }
00344
00345 void TodoList::pushItem(TodoItem * item, GBool bHead, GBool bRemoveDup)
00346 {
00347 if (NULL == item) return;
00348 pthread_mutex_lock(&mutex);
00349
00350 if (bRemoveDup)
00351 {
00352 removeDup(item);
00353 }
00354 if (bHead)
00355 {
00356 list->insert(0, item);
00357 }
00358 else
00359 {
00360 list->append(item);
00361 }
00362 pthread_mutex_unlock(&mutex);
00363 }
00364
00365 TodoItem * TodoList::popItem()
00366 {
00367 TodoItem * item = NULL;
00368 pthread_mutex_lock(&mutex);
00369 if (list->getLength() > 0)
00370 {
00371 item = (TodoItem *)list->del(0);
00372 }
00373 pthread_mutex_unlock(&mutex);
00374 return item;
00375 }