source: trunk/Lucide/SOURCE/gui/pluginman.cpp @ 140

Last change on this file since 140 was 140, checked in by Eugene Romanenko, 15 years ago

Update to changeset [138]

File size: 5.9 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: CDDL 1.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the COMMON DEVELOPMENT AND
5 * DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use
6 * this file except in compliance with the License. You may obtain a copy of
7 * the License at http://www.sun.com/cddl/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Initial Developer of the Original Code is
15 * Eugene Romanenko, netlabs.org.
16 * Portions created by the Initial Developer are Copyright (C) 2006
17 * the Initial Developer. All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * Alternatively, the contents of this file may be used under the terms of
22 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23 * in which case the provisions of the LGPL are applicable instead of those
24 * above. If you wish to allow use of your version of this file only under the
25 * terms of the LGPL, and not to allow others to use your version of this file
26 * under the terms of the CDDL, indicate your decision by deleting the
27 * provisions above and replace them with the notice and other provisions
28 * required by the LGPL. If you do not delete the provisions above, a recipient
29 * may use your version of this file under the terms of any one of the CDDL
30 * or the LGPL.
31 *
32 * ***** END LICENSE BLOCK ***** */
33
34
35#define INCL_DOS
36#include <os2.h>
37
38#include "pluginman.h"
39#include "luutils.h"
40#include <dos.h>
41#include <ludoc.xh>
42
43
44PluginManager::PluginManager()
45{
46    plugins = new PluginInfoList;
47
48    // Find exe path
49    char buffer[ _MAX_PATH ];
50    char path[ _MAX_PATH ];
51    char drive[ _MAX_DRIVE ];
52    char dir[ _MAX_DIR ];
53    _splitpath( __argv[0], drive, dir, NULL, NULL );
54    _makepath( path, drive, dir, NULL, NULL );
55    strcpy( buffer, path );
56    strcat( buffer, "LU*.DLL" );
57
58    // enum plugins, (LU*.DLL) except for LUDOC.DLL, which is 'null' plugin
59    // and Lucide.dll, which isn't plugin.
60    struct find_t ffblk;
61    unsigned done = _dos_findfirst( buffer, _A_RDONLY | _A_NORMAL, &ffblk );
62    while ( done == 0 )
63    {
64        if ( ( stricmp( ffblk.name, "LUDOC.DLL" ) != 0 ) &&
65             ( stricmp( ffblk.name, "LUCIDE.DLL" ) != 0 ) )
66        {
67            loadPlugin( path, ffblk.name );
68        }
69        done = _dos_findnext( &ffblk );
70    }
71    _dos_findclose( &ffblk );
72}
73
74PluginManager::~PluginManager()
75{
76    for ( int i = 0; i < plugins->size(); i++ )
77    {
78        PluginInfo *pi = &(*plugins)[ i ];
79        DosFreeModule( pi->handle );
80        //printf( "NAME: %s  EXT: %s  DESC: %s\n", pi->name.c_str(),
81        //        pi->extensions.c_str(), pi->description.c_str() );
82    }
83    delete plugins;
84}
85
86void PluginManager::loadPlugin( const char *path, const char *dllname )
87{
88    // Function pointer variables
89    LuDocument * APIENTRY (*pCreateObject)();
90    char * APIENTRY (*pGetSupportedExtensions)();
91    char * APIENTRY (*pGetDescription)();
92
93    std::string fulldllname = path;
94    fulldllname += dllname;
95
96    // cut DLL name at last point
97    char *lpoint = strrchr( dllname, '.' );
98    *lpoint = '\0';
99
100    // load a DLL
101    HMODULE h = NULLHANDLE;
102    bool res = false;
103    do
104    {
105        if ( DosLoadModule( NULL, 0, fulldllname.c_str(), &h ) != 0 )
106            break;
107        if ( DosQueryProcAddr( h, 0, "createObject", (PFN *)&pCreateObject ) != 0 )
108            break;
109        if ( DosQueryProcAddr( h, 0, "getSupportedExtensions", (PFN *)&pGetSupportedExtensions ) != 0 )
110            break;
111        if ( DosQueryProcAddr( h, 0, "getDescription", (PFN *)&pGetDescription ) != 0 )
112            break;
113
114        res = true;
115    } while (0);
116
117    if ( res )
118    {
119        PluginInfo pi;
120        pi.handle = h;
121        pi.name = dllname;
122        pi.extensions = pGetSupportedExtensions();
123        pi.description = pGetDescription();
124
125        plugins->push_back( pi );
126    }
127}
128
129
130// returns NULL if not suitable plugin found
131// if checkOnly is true - just check if suitable plugin exist
132LuDocument *PluginManager::createDocumentForExt( const char *ext, bool checkOnly )
133{
134    if ( ext == NULL ) {
135        return NULL;
136    }
137
138    LuDocument * APIENTRY (*pCreateObject)();
139
140    for ( int i = 0; i < plugins->size(); i++ )
141    {
142        PluginInfo *pi = &(*plugins)[ i ];
143
144        char *cExt = new char[ strlen( ext ) + 5 ];
145        strcpy( cExt, ";" );
146        strcat( cExt, ext );
147        strcat( cExt, ";" );
148        strupr( cExt );
149
150        char *cExts = new char[ pi->extensions.length() + 5 ];
151        strcpy( cExts, ";" );
152        strcat( cExts, pi->extensions.c_str() );
153        strcat( cExts, ";" );
154        strupr( cExts );
155
156        if ( strstr( cExts, cExt ) != NULL )
157        {
158            if ( DosQueryProcAddr( pi->handle, 0, "createObject",
159                                   (PFN *)&pCreateObject ) == 0 )
160            {
161                delete cExt;
162                delete cExts;
163
164                if ( checkOnly ) {
165                    return (LuDocument *)TRUE;
166                }
167                else {
168                    return pCreateObject();
169                }
170            }
171        }
172
173        delete cExt;
174        delete cExts;
175    }
176
177    return NULL;
178}
179
180
181std::string PluginManager::getExtsMask()
182{
183    std::string cRet = "";
184
185    for ( int i = 0; i < plugins->size(); i++ )
186    {
187        PluginInfo *pi = &(*plugins)[ i ];
188        char *tmpexts = newstrdup( pi->extensions.c_str() );
189        char *p = strtok( tmpexts, ";" );
190        while( p != NULL ) {
191            cRet += "*.";
192            cRet += p;
193            cRet += ";";
194            p = strtok( NULL, ";" );
195        }
196    }
197    return cRet;
198}
199
Note: See TracBrowser for help on using the repository browser.