kpilot Library API Documentation

jplugin.cpp

00001 /***************************************************************************
00002                           jplugin.cpp  -  description
00003                              -------------------
00004     begin               : Sat Mar 16 2002
00005     copyright           : (C) 2002 by reinhold
00006     email               : reinhold@albert
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                       *
00011  * JPilotPlugin Proxy Copyright (C) 2002 by Reinhold Kainhofer           *
00012  * JPilot Plugin API Copyright (C) 1999 by Judd Montgomery               *
00013  * KPilot Conduit API Copyright by Dan Pilone, Adriaan de Groot         *
00014  *                                                                       *
00015  *   This program is free software; you can redistribute it and/or modify  *
00016  *   it under the terms of the GNU General Public License as published by  *
00017  *   the Free Software Foundation; either version 2 of the License, or   *
00018  *   (at your option) any later version.                                   *
00019  *                                                                       *
00020  ***************************************************************************/
00021 
00022 #include <stdlib.h>
00023 //#include <dlfcn.h>
00024 #include <string.h>
00025 #include <klibloader.h>
00026 #include "jplugin.h"
00027 #include "options.h"
00028 
00029 extern "C" {
00030     #include "JPilotAPI/libplugin.h"
00031 }
00032 
00033 int jpilot_logf(int level, char *format, ...){
00034     FUNCTIONSETUP;
00035     return 0;
00036 }
00037 
00038 
00039 JPlugin::JPlugin(){
00040     FUNCTIONSETUP;
00041     init_info_null(&info);
00042     loaded=false;
00043 }
00044 JPlugin::JPlugin(QString fn) {
00045     FUNCTIONSETUP;
00046     init_info_null(&info);
00047     loaded=load(fn);
00048 }
00049 JPlugin::~JPlugin() {
00050     FUNCTIONSETUP;
00051     unload();
00052 }
00053 
00054 bool JPlugin::unload() {
00055     FUNCTIONSETUP;
00056     exit_info(&info);
00057     return true;
00058 }
00059 
00060 bool JPlugin::load(QString path) {
00061     FUNCTIONSETUP;
00062     if (loaded) unload();
00063     loaded=get_plugin_info(&info, path)>0;
00064     if (!loaded) {
00065         #ifdef DEBUG
00066         DEBUGCONDUIT<<"Error loading the plugin "<<path<<endl;
00067         #endif
00068         exit_info(&info);
00069     }
00070     return loaded;
00071 }
00072 
00073 
00074 
00075 void JPlugin::init_info_null(struct plugin_s *p) {
00076     FUNCTIONSETUP;
00077     loaded=false;
00078     lib = NULL;
00079     p->fullpath = QString::null;
00080     p->sync_on = 1;
00081     p->name = QString::null;
00082     p->db_name = QString::null;
00083     p->number = 0;
00084 }
00085 
00086 void JPlugin::exit_info(struct plugin_s *p) {
00087     FUNCTIONSETUP;
00088     if (loaded) exit_cleanup();
00089     if (loaded && lib) KLibLoader::self()->unloadLibrary(info.fullpath);
00090     init_info_null(p);
00091     loaded=false;
00092 }
00093 
00094 
00095 bool JPlugin::get_plugin_info(struct plugin_s *p, QString path) {
00096     FUNCTIONSETUP;
00097     void *h;
00098     const char *err;
00099     // use a string of length 100 to prevent a buffer overflow, although JPilot allows only 50 chars
00100     char name[100];
00101     int version, major_version, minor_version;
00102     T_versionM plugin_versionM;
00103     KLibLoader*ll=KLibLoader::self();
00104 
00105 
00106     init_info_null(p);
00107     // TODO: Remove the .so so that dependent libs are loaded, too!!!
00108     QString libname=path;
00109     if (path.right(3)==".so") libname=path.left(path.length()-3);
00110     lib = ll->library(libname);
00111 //  h = dlopen(libname, RTLD_NOW);
00112     if (!lib) {
00113         #ifdef DEBUG
00114         DEBUGCONDUIT<<"open failed on plugin ["<<libname<<"]\n error ["<<ll->lastErrorMessage()<<"]"<<endl;
00115         #endif
00116         return false;
00117     }
00118     p->fullpath=lib->fileName();
00119     #ifdef DEBUG
00120     DEBUGCONDUIT<<"opened plugin ["<<libname<<"]"<<endl;
00121     #endif
00122 
00123     /* plugin_versionM */
00124     plugin_versionM = (T_versionM)(lib->symbol("plugin_version"));
00125     if (plugin_versionM==NULL) {
00126         err = ll->lastErrorMessage();
00127         #ifdef DEBUG
00128         DEBUGCONDUIT<<"plugin_version: ["<<err<<"],  plugin is invalid: ["<<libname<<"]"<<endl;
00129         #endif
00130         ll->unloadLibrary(p->fullpath);
00131         lib=NULL;
00132         return false;
00133     }
00134     plugin_versionM(&major_version, &minor_version);
00135     version=major_version*1000+minor_version;
00136     if ((major_version <= 0) && (minor_version < 99)) {
00137         #ifdef DEBUG
00138         DEBUGCONDUIT<<"Plugin:["<<libname<<"]: plugin version ("<<major_version<<"."<< minor_version<<" too old..."<<endl;
00139         #endif
00140         ll->unloadLibrary(libname);
00141         lib=NULL;
00142         return false;
00143     }
00144     #ifdef DEBUG
00145     DEBUGCONDUIT <<"This plugin is version ("<<major_version<<"."<<minor_version<<")"<<endl;
00146     #endif
00147 
00148     /* plugin_get_name */
00149     T_get_name plugin_get_name = (T_get_name)(lib->symbol("plugin_get_name"));
00150     if (plugin_get_name==NULL) {
00151         err = ll->lastErrorMessage();
00152         #ifdef DEBUG
00153         DEBUGCONDUIT<<"plugin_get_name: ["<<err<<"],  plugin is invalid: ["<<libname<<"]"<<endl;
00154         #endif
00155         ll->unloadLibrary(libname);
00156         lib=NULL;
00157         return false;
00158     } else {
00159         plugin_get_name(name, 50);
00160         name[50]='\0';
00161         p->name = name;
00162     }
00163 
00164 
00165     #ifdef DEBUG
00166     DEBUGCONDUIT <<"Before loading menu name"<<endl;
00167     #endif
00168     /* plugin_get_menu_name */
00169     T_get_menu_name plugin_get_menu_name = (T_get_menu_name)(lib->symbol("plugin_get_menu_name"));
00170     if (plugin_get_menu_name!=NULL) {
00171         plugin_get_menu_name(name, 50);
00172         #ifdef DEBUG
00173         DEBUGCONDUIT <<"Menu name function successfully loaded: "<<name<<endl;
00174         #endif
00175         name[50]='\0';
00176         p->menu_name = name;
00177     }
00178 
00179     #ifdef DEBUG
00180     DEBUGCONDUIT <<"Before loading help name"<<endl;
00181     #endif
00182     /* plugin_get_help_name */
00183     T_get_help_name plugin_get_help_name = (T_get_help_name)(lib->symbol("plugin_get_help_name"));
00184     if (plugin_get_help_name!=NULL) {
00185         plugin_get_help_name(name, 50);
00186         #ifdef DEBUG
00187         DEBUGCONDUIT <<"Help name function successfully loaded: "<<name<<endl;
00188         #endif
00189         name[50]='\0';
00190         p->help_name = name;
00191     }
00192 
00193         #ifdef DEBUG
00194         DEBUGCONDUIT <<"Before loading db name"<<endl;
00195         #endif
00196     /* plugin_get_db_name */
00197     name[0]='\0';
00198     T_get_db_name plugin_get_db_name = (T_get_db_name)(lib->symbol("plugin_get_db_name"));
00199     if (plugin_get_db_name!=NULL) {
00200         plugin_get_db_name(name, 50);
00201         name[50]='\0';
00202         #ifdef DEBUG
00203         DEBUGCONDUIT <<"DB name function successfully loaded: "<<name<<endl;
00204         #endif
00205     }
00206     p->db_name = name;
00207     
00208     #ifdef DEBUG
00209     DEBUGCONDUIT<<"Finished loading symbols from JPilot plugin ("<<libname<<")"<<endl;
00210     #endif
00211     return true;
00212 }
00213 
00214 bool JPlugin::hasGui() {
00215     FUNCTIONSETUP;
00216     void *func = lib->symbol("plugin_gui");
00217     return (func!=NULL);
00218 }
00219 
00220 /***************************************************************************
00221  * These functions just lookup the callback functions from the plugin
00222  * and execute it if available. If not, -1 is returned, but no crash
00223  * or exception *should* occur...
00224  ***************************************************************************/
00225 
00226 int JPlugin::startup(jp_startup_info*si) {
00227     FUNCTIONSETUP;
00228     #ifdef DEBUG
00229     DEBUGCONDUIT<<"startup for conduit "<<info.fullpath<<endl;
00230     #endif
00231     void *func = lib->symbol("plugin_startup");
00232     if (func) return ((T_startup)func)(si);
00233     else {
00234         #ifdef DEBUG
00235         DEBUGCONDUIT<<"Callback "<<fname<<" not found in plugin "<<info.name<<endl;
00236         #endif
00237         return -1;
00238     }
00239 }
00240 
00241 int JPlugin::gui(GtkWidget*vbox, GtkWidget*hbox, unsigned int uID) {
00242     FUNCTIONSETUP;
00243     #ifdef DEBUG
00244     DEBUGCONDUIT<<"gui for conduit "<<info.fullpath<<endl;
00245     #endif
00246     void *func = lib->symbol("plugin_gui");
00247     if (func) return ((T_gui)func)(vbox, hbox, uID);
00248     else {
00249         #ifdef DEBUG
00250         DEBUGCONDUIT<<"Callback "<<fname<<" not found in plugin "<<info.name<<endl;
00251         #endif
00252         return -1;
00253     }
00254 }
00255 
00256 int JPlugin::gui_cleanup() {
00257     FUNCTIONSETUP;
00258     #ifdef DEBUG
00259     DEBUGCONDUIT<<"gui_cleanup for conduit "<<info.fullpath<<endl;
00260     #endif
00261     void *func = lib->symbol("plugin_gui_cleanup");
00262     if (func) return ((T_gui_cleanup)func)();
00263     else {
00264         #ifdef DEBUG
00265         DEBUGCONDUIT<<"Callback "<<fname<<" not found in plugin "<<info.name<<endl;
00266         #endif
00267         return -1;
00268     }
00269 }
00270 
00271 int JPlugin::help(char** text, int*width, int*height) {
00272     FUNCTIONSETUP;
00273     #ifdef DEBUG
00274     DEBUGCONDUIT<<"help for conduit "<<info.fullpath<<endl;
00275     #endif
00276     void *func = lib->symbol("plugin_help");
00277     if (func) return ((T_help)func)(text, width, height);
00278     else {
00279         #ifdef DEBUG
00280         DEBUGCONDUIT<<"Callback "<<fname<<" not found in plugin "<<info.name<<endl;
00281         #endif
00282         return -1;
00283     }
00284 }
00285 
00286 int JPlugin::pre_sync() {
00287     FUNCTIONSETUP;
00288     #ifdef DEBUG
00289     DEBUGCONDUIT<<"pre_sync for conduit "<<info.fullpath<<endl;
00290     #endif
00291     void *func = lib->symbol("plugin_pre_sync");
00292     if (func) return ((T_pre_sync)func)();
00293     else {
00294         #ifdef DEBUG
00295         DEBUGCONDUIT<<"Callback "<<fname<<" not found in plugin "<<info.name<<endl;
00296         #endif
00297         return -1;
00298     }
00299 }
00300 
00301 int JPlugin::sync(int sd) {
00302     FUNCTIONSETUP;
00303     #ifdef DEBUG
00304     DEBUGCONDUIT<<"sync for conduit "<<info.fullpath<<endl;
00305     #endif
00306     void *func = lib->symbol("plugin_sync");
00307     if (func) return ((T_sync)func)(sd);
00308     else {
00309         #ifdef DEBUG
00310         DEBUGCONDUIT<<"Callback "<<fname<<" not found in plugin "<<info.name<<endl;
00311         #endif
00312         return -1;
00313     }
00314 }
00315 
00316 int JPlugin::post_sync() {
00317     FUNCTIONSETUP;
00318     #ifdef DEBUG
00319     DEBUGCONDUIT<<"post_sync for conduit "<<info.fullpath<<endl;
00320     #endif
00321     void *func = lib->symbol("plugin_post_sync");
00322     if (func) return ((T_post_sync)func)();
00323     else {
00324         #ifdef DEBUG
00325         DEBUGCONDUIT<<"Callback "<<fname<<" not found in plugin "<<info.name<<endl;
00326         #endif
00327         return -1;
00328     }
00329 }
00330 
00331 int JPlugin::exit_cleanup() {
00332     FUNCTIONSETUP;
00333     #ifdef DEBUG
00334     DEBUGCONDUIT<<"exit_cleanup for conduit "<<info.fullpath<<endl;
00335     #endif
00336     void *func = lib->symbol("plugin_exit_cleanup");
00337     if (func) return ((T_exit_cleanup)func)();
00338     else {
00339         #ifdef DEBUG
00340         DEBUGCONDUIT<<"Callback "<<fname<<" not found in plugin "<<info.name<<endl;
00341         #endif
00342         return -1;
00343     }
00344 }
00345 
KDE Logo
This file is part of the documentation for kpilot Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:36:47 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003