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

Last change on this file since 47 was 35, checked in by Eugene Romanenko, 16 years ago

added licence block to source files

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