src/midgard_blob.c

00001 /* 
00002  * Copyright (C) 2006 Piotr Pokora <piotr.pokora@infoglob.pl>
00003  *
00004  * This program is free software; you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License as published
00006  * by 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 "midgard/midgard_blob.h"
00020 
00021 struct MidgardBlobPrivate {
00022         MgdObject *attachment;
00023         const gchar *filename;
00024         MidgardConnection *mgd;
00025 };
00026 
00027 /* Instatiate new Midgard Blob. */
00028 MidgardBlob midgard_object_new(MidgardConection *mgd, 
00029                 MidgardObject *attachment)
00030 {
00031         MidgardBlob *self = g_object_new(MIDGARD_TYPE_BLOB, NULL);
00032         self->private->attachment = attachment;
00033 
00034         const gchar *location;
00035         g_object_get(G_OBJECT(attachment), 
00036                         "location",
00037                         (gchar)&location, 
00038                         NULL);
00039 
00040         /* This is duplicated value */
00041         if(location)
00042                 self->private->attachment = location;
00043 }
00044 
00045 /* Returns GIOChannel pointer for the given midgard_blob. */ 
00046 GIOChannel midgard_blob_read(MidgardBlob *self)
00047 {
00048         g_assert(self);
00049         
00050         if(self->private->filename) {
00051                  midgard_set_error(self->private->mgd->_mgd,
00052                                  MGD_GENERIC_ERROR,
00053                                  MGD_ERR_USER_DATA,
00054                                  "Invalid attachment. "
00055                                  "Can not read file from empty location");
00056                  return NULL;
00057         }
00058 
00059         /* FIXME! , get blobdir location */
00060         const gchar *blobdir = "";
00061         gchar *filepath = g_strconcat(blobdir, 
00062                         self->private->filename,
00063                         NULL);
00064 
00065         GError *err = NULL;
00066         GIOChannel *channel =
00067                 g_io_channel_new_file(
00068                                 filepath,
00069                                 "r",
00070                                 &err);  
00071         g_free(filepath);
00072         
00073         if(!channel){
00074                 midgard_set_error(self->mgd->_mgd,
00075                                 MGD_GENERIC_ERROR,
00076                                 MGD_ERR_USER_DATA,
00077                                 " %s ",
00078                                 err->message);
00079                 g_clear_error(&err);
00080                 return NULL;
00081         }
00082 }
00083 
00084 /* Returns GIOChannel pointer for written blob file. */ 
00085 extern GIOChannel midgard_blob_write(MidgardBlob *self,
00086                                 const gchar *content);
00087 
00088 /* Destroy Midgard Blob instance. */ 
00089 extern void midgard_blob_destroy(MidgardBlob *self);
00090 
00091 /* GOBJECT ROUTINES */
00092 
00093 static void _midgard_blob_instance_init(
00094                  GTypeInstance *instance, gpointer g_class)
00095 {
00096         MidgardBlob *self = (MidgardBlob *) instance;
00097         self->private = g_new(MidgardBlobPrivate, 1);
00098         
00099         self->private->attachment = NULL;
00100         self->private->filename = NULL;
00101 }
00102 
00103 static void _midgard_blob_finalize(GObject *object)
00104 {
00105         g_assert(object);
00106 
00107         g_free(self->private->filename);
00108         g_free(self->private);
00109 
00110 }
00111 
00112 static void _midgard_blob_class_init(
00113                 gpointer g_class, gpointer g_class_data)
00114 {
00115         GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
00116         MidgardBlobClass *klass = MIDGARD_BLOB_CLASS (g_class);
00117         
00118         gobject_class->finalize = _midgard_collector_finalize;
00119         klass->read = midgard_blob_read;
00120         klass->write = midgard_blob_write;
00121         klass->destroy = midgard_blob_destroy;
00122 
00123 }
00124 
00125 /* Returns Midgard Blob Gtype value. */
00126 extern GType midgard_blob_get_type(void)
00127 {
00128         static GType type = 0;
00129         if (type == 0) {
00130                 static const GTypeInfo info = {
00131                         sizeof (MidgardBlobClass),
00132                         NULL,           /* base_init */
00133                         NULL,           /* base_finalize */
00134                         (GClassInitFunc) _midgard_blob_class_init,
00135                         NULL,           /* class_finalize */
00136                         NULL,           /* class_data */
00137                         sizeof (MidgardCollector),
00138                         0,              /* n_preallocs */
00139                         (GInstanceInitFunc) _midgard_blob_instance_init /* instance_init */
00140                 };
00141                 type = g_type_register_static (G_TYPE_OBJECT,
00142                                 "midgard_blob",
00143                                 &info, 0);
00144         }
00145         return type;
00146 }
00147 
00148 
00149 

Generated on Thu Feb 22 06:15:17 2007 for midgard-core by  doxygen 1.4.6