00001 /* 00002 * File Name: library_impl.cpp 00003 */ 00004 00005 /* 00006 * This file is part of uds-plugin-images. 00007 * 00008 * uds-plugin-images 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-images 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 <iostream> 00030 #include "library_impl.h" 00031 #include "images_scanner.h" 00032 #include "plugin_config.h" 00033 #include "utils.h" 00034 #include "log.h" 00035 00036 namespace images 00037 { 00038 // Plugin library object implementation. 00039 utils::ObjectTable<PluginLibraryImpl> 00040 PluginLibraryImpl::g_instances_table; 00041 00042 // Constructor 00043 PluginLibraryImpl::PluginLibraryImpl() 00044 { 00045 // Initialize all interface method pointers. 00046 query_interface = query_interface_impl; 00047 release = release_impl; 00048 is_supported_document = is_supported_document_impl; 00049 create_document = create_document_impl; 00050 00051 // record the instance. 00052 g_instances_table.add_interface<IPluginUnknown>(this); 00053 g_instances_table.add_interface<IPluginLibrary>(this); 00054 } 00055 00056 PluginLibraryImpl::~PluginLibraryImpl() 00057 { 00058 g_instances_table.remove(this); 00059 } 00060 00061 PluginStatus 00062 PluginLibraryImpl::query_interface_impl(IPluginUnknown *thiz, 00063 const UDSString *id, 00064 void **ptr) 00065 { 00066 // Two steps to query the interface. 00067 // 1. Retrieve object instance from the thiz pointer. 00068 // 2. Retrieve desired interface from given object. 00069 PluginLibraryImpl *instance = g_instances_table.get_object(thiz); 00070 if (g_instances_table.query_interface(instance, id->get_buffer(id), ptr)) 00071 { 00072 return PLUGIN_OK; 00073 } 00074 return PLUGIN_FAIL; 00075 } 00076 00077 int PluginLibraryImpl::release_impl( IPluginUnknown *thiz ) 00078 { 00079 PluginLibraryImpl *instance = g_instances_table.get_object(thiz); 00080 delete instance; 00081 return 0; 00082 } 00083 00084 PluginBool 00085 PluginLibraryImpl::is_supported_document_impl(IPluginUnknown *thiz, 00086 const UDSString *path) 00087 { 00088 assert(path); 00089 00090 LOGPRINTF("%s", path->get_buffer(path)); 00091 00092 if (path) 00093 { 00094 images::ImagesScanner scanner; 00095 if (scanner.is_image(path->get_buffer(path))) 00096 { 00097 return PLUGIN_TRUE; 00098 } 00099 } 00100 00101 return PLUGIN_FALSE; 00102 } 00103 00104 IPluginUnknown * 00105 PluginLibraryImpl::create_document_impl(IPluginUnknown * thiz) 00106 { 00107 PluginLibraryImpl *obj = g_instances_table.get_object(thiz); 00108 00109 if (obj) 00110 { 00111 // Create a new document object. 00112 DocPtr doc = new PluginDocImpl; 00113 00114 // Listen the release signal. 00115 doc->release_signal.add_slot(obj, 00116 &PluginLibraryImpl::on_document_released); 00117 00118 // Store the document. 00119 obj->documents.push_back(doc); 00120 return static_cast<IPluginUnknown *>(doc); 00121 } 00122 return 0; 00123 } 00124 00125 void PluginLibraryImpl::on_document_released(PluginDocImpl * doc) 00126 { 00127 DocumentsIter iter = std::find(documents.begin(), documents.end(), doc); 00128 if (iter != documents.end()) 00129 { 00130 delete *iter; 00131 documents.erase(iter); 00132 } 00133 } 00134 00135 } // namespace images 00136