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

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

fixed SYS3180 when plugins resides not in current dir. (closes #20, #29)

File size: 5.7 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, except for LUDOC.DLL, which is 'null' plugin
59    struct find_t ffblk;
60    unsigned done = _dos_findfirst( buffer, _A_RDONLY | _A_NORMAL, &ffblk );
61    while ( done == 0 )
62    {
63        if ( stricmp( ffblk.name, "LUDOC.DLL" ) != 0 ) {
64            loadPlugin( path, ffblk.name );
65        }
66        done = _dos_findnext( &ffblk );
67    }
68    _dos_findclose( &ffblk );
69}
70
71PluginManager::~PluginManager()
72{
73    for ( int i = 0; i < plugins->size(); i++ )
74    {
75        PluginInfo *pi = &(*plugins)[ i ];
76        DosFreeModule( pi->handle );
77        //printf( "NAME: %s  EXT: %s  DESC: %s\n", pi->name.c_str(),
78        //        pi->extensions.c_str(), pi->description.c_str() );
79    }
80    delete plugins;
81}
82
83void PluginManager::loadPlugin( const char *path, const char *dllname )
84{
85    // Function pointer variables
86    LuDocument * APIENTRY (*pCreateObject)();
87    char * APIENTRY (*pGetSupportedExtensions)();
88    char * APIENTRY (*pGetDescription)();
89
90    std::string fulldllname = path;
91    fulldllname += dllname;
92
93    // cut DLL name at last point
94    char *lpoint = strrchr( dllname, '.' );
95    *lpoint = '\0';
96
97    // load a DLL
98    HMODULE h = NULLHANDLE;
99    bool res = false;
100    do
101    {
102        if ( DosLoadModule( NULL, 0, fulldllname.c_str(), &h ) != 0 )
103            break;
104        if ( DosQueryProcAddr( h, 0, "createObject", (PFN *)&pCreateObject ) != 0 )
105            break;
106        if ( DosQueryProcAddr( h, 0, "getSupportedExtensions", (PFN *)&pGetSupportedExtensions ) != 0 )
107            break;
108        if ( DosQueryProcAddr( h, 0, "getDescription", (PFN *)&pGetDescription ) != 0 )
109            break;
110
111        res = true;
112    } while (0);
113
114    if ( res )
115    {
116        PluginInfo pi;
117        pi.handle = h;
118        pi.name = dllname;
119        pi.extensions = pGetSupportedExtensions();
120        pi.description = pGetDescription();
121
122        plugins->push_back( pi );
123    }
124}
125
126
127// returns NULL if not suitable plugin found
128// if checkOnly is true - just check if suitable plugin exist
129LuDocument *PluginManager::createDocumentForExt( const char *ext, bool checkOnly )
130{
131    if ( ext == NULL ) {
132        return NULL;
133    }
134
135    LuDocument * APIENTRY (*pCreateObject)();
136
137    for ( int i = 0; i < plugins->size(); i++ )
138    {
139        PluginInfo *pi = &(*plugins)[ i ];
140
141        char *cExt = new char[ strlen( ext ) + 5 ];
142        strcpy( cExt, ";" );
143        strcat( cExt, ext );
144        strcat( cExt, ";" );
145        strupr( cExt );
146
147        char *cExts = new char[ pi->extensions.length() + 5 ];
148        strcpy( cExts, ";" );
149        strcat( cExts, pi->extensions.c_str() );
150        strcat( cExts, ";" );
151        strupr( cExts );
152
153        if ( strstr( cExts, cExt ) != NULL )
154        {
155            if ( DosQueryProcAddr( pi->handle, 0, "createObject",
156                                   (PFN *)&pCreateObject ) == 0 )
157            {
158                delete cExt;
159                delete cExts;
160
161                if ( checkOnly ) {
162                    return (LuDocument *)TRUE;
163                }
164                else {
165                    return pCreateObject();
166                }
167            }
168        }
169
170        delete cExt;
171        delete cExts;
172    }
173
174    return NULL;
175}
176
177
178std::string PluginManager::getExtsMask()
179{
180    std::string cRet = "";
181
182    for ( int i = 0; i < plugins->size(); i++ )
183    {
184        PluginInfo *pi = &(*plugins)[ i ];
185        char *tmpexts = newstrdup( pi->extensions.c_str() );
186        char *p = strtok( tmpexts, ";" );
187        while( p != NULL ) {
188            cRet += "*.";
189            cRet += p;
190            cRet += ";";
191            p = strtok( NULL, ";" );
192        }
193    }
194    return cRet;
195}
196
Note: See TracBrowser for help on using the repository browser.