pdf/plugin_impl/library_impl.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: library_impl.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 <cassert>
00028 #include <string.h>
00029 #include "library_impl.h"
00030 
00031 namespace pdf
00032 {
00033 
00034 /// @brief Internal file extension table.
00035 /// dummy file is a index file which contains a list of file names.
00036 static const std::string g_ext_table[] = 
00037 {
00038     ".pdf"
00039 };
00040 static const int EXT_TABLE_SIZE = 
00041                  sizeof(g_ext_table) / sizeof(g_ext_table[0]);
00042 
00043 // Plugin library object implementation.
00044 utils::ObjectTable<PluginLibraryImpl> 
00045 PluginLibraryImpl::g_instances_table;
00046 
00047 // Constructor
00048 PluginLibraryImpl::PluginLibraryImpl()
00049 {
00050     // g_thread_init(0);
00051 
00052     // Initialize all interface method pointers.
00053     query_interface         = query_interface_impl;
00054     release                 = release_impl;
00055     is_supported_document   = is_supported_document_impl;
00056     create_document         = create_document_impl;
00057 
00058     // record the instance.
00059     g_instances_table.add_interface<IPluginUnknown>(this);
00060     g_instances_table.add_interface<IPluginLibrary>(this);
00061 }
00062 
00063 PluginLibraryImpl::~PluginLibraryImpl()
00064 {
00065     g_instances_table.remove(this);
00066 }
00067 
00068 PluginStatus 
00069 PluginLibraryImpl::query_interface_impl(IPluginUnknown    *thiz,
00070                                         const UDSString   *id,
00071                                         void              **ptr)
00072 {
00073     // Two steps to query the interface.
00074     // 1. Retrieve object instance from the thiz pointer. 
00075     // 2. Retrieve desired interface from given object.
00076     PluginLibraryImpl *instance = g_instances_table.get_object(thiz);
00077     if (g_instances_table.query_interface(instance, id->get_buffer(id), ptr))
00078     {
00079         return PLUGIN_OK;
00080     }
00081     return PLUGIN_FAIL;
00082 }
00083 
00084 int PluginLibraryImpl::release_impl( IPluginUnknown  *thiz )
00085 {
00086     PluginLibraryImpl *instance = g_instances_table.get_object(thiz);
00087     delete instance;
00088     return 0;
00089 }
00090 
00091 PluginBool 
00092 PluginLibraryImpl::is_supported_document_impl(IPluginUnknown    *thiz, 
00093                                               const UDSString   *path)
00094 {
00095     // TODO. Refine this method
00096     // Estimate the first several bytes?
00097     if (path != 0)
00098     {
00099         const char * pos = strrchr(path->get_buffer(path), '.');
00100         for(int i = 0; i < EXT_TABLE_SIZE; ++i)
00101         {
00102             if (pos)
00103             {
00104                 if (strncmp(pos, 
00105                             g_ext_table[i].c_str(), 
00106                             g_ext_table[i].length()) == 0)
00107                 {
00108                     return PLUGIN_TRUE;
00109                 }
00110             }
00111         }
00112     }
00113     return PLUGIN_FALSE;
00114 }
00115 
00116 IPluginUnknown * 
00117 PluginLibraryImpl::create_document_impl(IPluginUnknown * thiz)
00118 {
00119     PluginLibraryImpl *obj = g_instances_table.get_object(thiz);
00120 
00121     if (obj)
00122     {
00123         // Create a new document object.
00124         DocPtr doc = new PluginDocImpl;
00125 
00126         // Listen the release signal.
00127         doc->release_signal.add_slot(obj, 
00128             &PluginLibraryImpl::on_document_released);
00129 
00130         // Store the document.
00131         obj->documents.push_back(doc);
00132         return static_cast<IPluginUnknown *>(doc);
00133     }
00134     return 0;
00135 }
00136 
00137 void PluginLibraryImpl::on_document_released(PluginDocImpl * doc)
00138 {
00139     DocumentsIter iter = std::find(documents.begin(), documents.end(), doc);
00140     if (iter != documents.end())
00141     {
00142         delete *iter;
00143         documents.erase(iter);
00144     }
00145 }
00146 
00147 }   // namespace pdf
00148 
Generated by  doxygen 1.6.2-20100208