pdf_library.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: pdf_library.cpp
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-pdf.
00007  *
00008  * uds-plugin-pdf is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * uds-plugin-pdf is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #include "pdf_library.h"
00028 #include "pdf_doc_controller.h"
00029 
00030 #ifdef _WIN32
00031 #include <windows.h>
00032 #else
00033 #include <sys/sysinfo.h>
00034 #endif
00035 
00036 namespace pdf
00037 {
00038 
00039 unsigned long get_system_free_memory()
00040 {
00041 #ifdef _WIN32
00042     MEMORYSTATUS info;
00043     GlobalMemoryStatus(&info);
00044     return info.dwAvailPhys;
00045 #else
00046     struct sysinfo info;
00047     sysinfo(&info);
00048     return info.freeram * info.mem_unit;
00049 #endif
00050 }
00051 
00052 PDFLibrary::PDFLibrary()
00053 : docs()
00054 , thread()
00055 , size_limit(DEFAULT_SIZE_LIMIT)
00056 {
00057     // start the task executing thread
00058     thread.start();
00059 }
00060 
00061 PDFLibrary::~PDFLibrary()
00062 {
00063     // stop the task executing thread
00064     thread.stop();
00065 }
00066 
00067 PDFLibrary& PDFLibrary::instance()
00068 {
00069     static PDFLibrary lib;
00070     return lib;
00071 }
00072 
00073 void PDFLibrary::try_start_thread()
00074 {
00075     thread.start();
00076 }
00077 
00078 void PDFLibrary::try_stop_thread()
00079 {
00080     if (docs.empty())
00081     {
00082         thread.stop();
00083     }
00084 }
00085 
00086 bool PDFLibrary::make_enough_memory(PDFController *doc_ptr,
00087                                     const int page_num,
00088                                     const int length)
00089 {
00090     if (length <= 0)
00091     {
00092         WARNPRINTF("Should NOT make enough memory by length:%d", length);
00093         return false;
00094     }
00095 
00096     // check the free memory in system
00097     unsigned long free_mem = get_system_free_memory();
00098     if (free_mem < static_cast<unsigned long>(length << 1))
00099     {
00100         // release all of the cached bitmaps
00101         DocumentsIter idx = docs.begin();
00102         for (; idx != docs.end(); ++idx)
00103         {
00104             (*idx)->clear_cached_bitmaps();
00105         }
00106     }
00107 
00108     return doc_ptr->make_enough_memory(page_num, length);
00109 }
00110 
00111 void PDFLibrary::add_document(PDFController *doc_ptr)
00112 {
00113     try_start_thread();
00114     docs.push_back(doc_ptr);
00115 }
00116 
00117 void PDFLibrary::remove_tasks_by_document(PDFController *doc)
00118 {
00119     thread.cancel_tasks(doc);
00120 }
00121 
00122 void PDFLibrary::remove_document(PDFController *doc_ptr)
00123 {
00124     DocumentsIter idx = std::find(docs.begin(), docs.end(), doc_ptr);
00125     if (idx != docs.end())
00126     {
00127         docs.erase(idx);
00128     }
00129     try_stop_thread();
00130 }
00131 
00132 void PDFLibrary::thread_add_render_task(Task *task, bool prerender, bool abort_current)
00133 {
00134     if (prerender)
00135     {
00136         get_thread().append_task(task);
00137     }
00138     else
00139     {
00140         // post the rendering task into the thread's task list
00141         get_thread().prepend_task(task, abort_current);
00142     }
00143 }
00144 
00145 void PDFLibrary::thread_add_search_task(Task *task)
00146 {
00147     get_thread().prepend_task(task, true);
00148 }
00149 
00150 void PDFLibrary::thread_cancel_render_tasks(void *user_data)
00151 {
00152     get_thread().clear_all(user_data, TASK_RENDER);
00153 }
00154 
00155 }
00156 
00157 
Generated by  doxygen 1.6.2-20100208