decoder.c

Go to the documentation of this file.
00001 /*
00002  Copyright (C) 2005-2007 Erik van Pienbroek
00003 
00004  This program is free software; you can redistribute it and/or modify
00005  it under the terms of the GNU General Public License as published by
00006  the Free Software Foundation; either version 2 of the License, or
00007  (at your option) any later version.
00008 
00009  This program is distributed in the hope that it will be useful,
00010  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00012  GNU General Public License for more details.
00013 
00014  You should have received a copy of the GNU General Public License
00015  along with this program; if not, write to the Free Software
00016  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00017 */
00018 
00019 #include 
00020 #include 
00021 #include 
00022 #include 
00023 #include 
00024 
00025 #include "nntpgrab_internal.h"
00026 #include "nntpgrab_utils.h"
00027 #include "decoder.h"
00028 #include "plugin_decoder.h"
00029 #include "nntpgrab_plugin_decoder.h"
00030 
00031 static PluginDecoderExportedFuncs plugin_funcs;
00032 
00033 typedef struct DecoderClass DecoderClass;
00034 
00035 static GType decoder_get_type (void);
00036 
00037 struct Decoder
00038 {
00039     GObject parent;
00040 
00041     GStaticRWLock rwlock;
00042     Configuration *config;
00043 };
00044 
00045 struct DecoderClass
00046 {
00047     GObjectClass parent;
00048 };
00049 
00050 G_DEFINE_TYPE(Decoder, decoder, G_TYPE_OBJECT);
00051 
00052 static void
00053 decoder_init (Decoder *obj)
00054 {
00055     g_static_rw_lock_init(&obj->rwlock);
00056 }
00057 
00058 static void
00059 decoder_finalize (GObject *obj)
00060 {
00061     Decoder *decoder = DECODER(obj);
00062 
00063     g_static_rw_lock_writer_lock(&decoder->rwlock);
00064 
00065     g_object_unref(decoder->config);
00066 
00067     g_static_rw_lock_writer_unlock(&decoder->rwlock);
00068     g_static_rw_lock_free(&decoder->rwlock);
00069 }
00070 
00071 static void
00072 decoder_class_init (DecoderClass *klass)
00073 {
00074     GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
00075 
00076     gobject_class->finalize = decoder_finalize;
00077 }
00078 
00079 gboolean
00080 decoder_initialize(char **errmsg)
00081 {
00082     GModule *plugin = NULL;
00083     PluginDecoderImportedFuncs imported_funcs;
00084     plugin_decoder_init_func init_func;
00085     char *filename;
00086 
00087 #ifdef WIN32
00088 #define EXT "-0.dll"
00089 #else
00090 #define EXT ".so"
00091 #endif
00092 
00093     if (g_getenv("NNTPGRAB_LIBDIR")) {
00094         filename = g_strdup_printf("%s/libnntpgrab_plugin_decoder%s", g_getenv("NNTPGRAB_LIBDIR"), EXT);
00095     } else {
00096         filename = g_strdup_printf("%s/libnntpgrab_plugin_decoder%s", PLUGIN_DIR, EXT);
00097     }
00098 
00099     plugin = g_module_open(filename, G_MODULE_BIND_LOCAL);
00100 
00101     g_free(filename);
00102 
00103     if (!plugin) {
00104         if (errmsg) {
00105             *errmsg = g_strdup(g_module_error());
00106         }
00107 
00108         return FALSE;
00109     }
00110 
00111     imported_funcs.version = NNTPGRAB_PLUGIN_API_VERSION;
00112 #if 0
00113     imported_funcs.update_progress = update_progress_callback;
00114     imported_funcs.notify_download_complete = download_complete_callback;
00115     imported_funcs.notify_part_not_available = part_not_available_callback;
00116     imported_funcs.notify_connecting = connecting_callback;
00117     imported_funcs.notify_connected = connected_callback;
00118     imported_funcs.notify_disconnect = disconnect_callback;
00119 #endif
00120 
00121     if (!g_module_symbol(plugin, "nntpgrab_plugin_decoder_get_version", (gpointer *) &plugin_funcs.get_version)) {
00122         if (errmsg) {
00123             *errmsg = g_strdup(g_module_error());
00124         }
00125 
00126         return FALSE;
00127     }
00128 
00129     if (plugin_funcs.get_version() != NNTPGRAB_PLUGIN_API_VERSION) {
00130         if (errmsg) {
00131             *errmsg = g_strdup_printf(_("Decoder Plugin API mismatch (Plugin API version = %i, expected = %i)"), plugin_funcs.get_version(), NNTPGRAB_PLUGIN_API_VERSION);
00132         }
00133 
00134         return FALSE;
00135     }
00136 
00137     /* All required methods are found, lets start */
00138     if (!g_module_symbol(plugin, "nntpgrab_plugin_decoder_initialize", (gpointer *) &init_func)) {
00139         if (errmsg) {
00140             *errmsg = g_strdup(g_module_error());
00141         }
00142 
00143         return FALSE;
00144     }
00145 
00146     if (!g_module_symbol(plugin, "nntpgrab_plugin_decoder_decode_file", (gpointer *) &plugin_funcs.decode_file)) {
00147         if (errmsg) {
00148             *errmsg = g_strdup(g_module_error());
00149         }
00150 
00151         return FALSE;
00152     }
00153 
00154     if (!init_func(imported_funcs)) {
00155         if (errmsg) {
00156             *errmsg = g_strdup(_("Decoder Plugin initialization failed!"));
00157         }
00158 
00159         return FALSE;
00160     }
00161 
00162     nntpgrab_core_emit_debug_message(_("Decoder plugin initialized"));
00163 
00164     return TRUE;
00165 }
00166 
00167 Decoder *
00168 decoder_new(Configuration *config)
00169 {
00170     Decoder *decoder;
00171 
00172     decoder = g_object_new(DECODER_TYPE_OBJECT, NULL);
00173     decoder->config = config;
00174     g_object_ref(config);
00175 
00176     return decoder;
00177 }
00178 
00179 void
00180 decoder_destroy(Decoder *obj)
00181 {
00182     g_object_unref(obj);
00183 }
00184 
00185 gboolean
00186 decoder_decode_file(Decoder *obj, const char *collection_name, const NNTPFile *file, int *saved_errno)
00187 {
00188     Decoder *decoder = DECODER(obj);
00189     ConfigOpts opts;
00190     char *msg;
00191     gboolean retval;
00192     char *errmsg = NULL;
00193 
00194     msg = g_strdup_printf(_("Trying to decode file with from collection '%s' with subject = '%s'"), collection_name, file->subject);
00195     nntpgrab_core_emit_debug_message(msg);
00196     g_free(msg);
00197 
00198     g_static_rw_lock_reader_lock(&decoder->rwlock);
00199 
00200     opts = configuration_get_opts(decoder->config);
00201 
00202     g_static_rw_lock_reader_unlock(&decoder->rwlock);
00203 
00204     retval = plugin_funcs.decode_file(collection_name, file, opts.temp_directory, opts.download_directory, saved_errno, &errmsg);
00205 
00206     msg = g_strdup_printf(_("Decoder returned %i"), retval);
00207     nntpgrab_core_emit_debug_message(msg);
00208     g_free(msg);
00209 
00210     return retval;
00211 }
00212 

Generated on Sun Oct 12 01:45:29 2008 for NNTPGrab by  1.5.4