activity.c 12 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
/*
 * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270  e X3270
 * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
 * aplicativos mainframe. Registro no INPI sob o nome G3270.
 *
 * Copyright (C) <2008> <Banco do Brasil S.A.>
 *
 * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
 * os termos da GPL v.2 - Licença Pública Geral  GNU,  conforme  publicado  pela
 * Free Software Foundation.
 *
 * Este programa é distribuído na expectativa de  ser  útil,  mas  SEM  QUALQUER
 * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou  de  ADEQUAÇÃO
 * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
 * obter mais detalhes.
 *
 * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
 * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
 * St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Este programa está nomeado como - e possui - linhas de código.
 *
 * Contatos:
 *
 * perry.werneck@gmail.com	(Alexandre Perry de Souza Werneck)
 * erico.mendonca@gmail.com	(Erico Mascarenhas Mendonça)
 *
 */

/**
 * SECTION:V3270FTActivity
 * @Short_description: A file transfer activity.
 * @Title: V3270FTActivity
 *
 * The #V3270FTActivity object represents a single file transfer operation
 * and contens the file names and the settings used for transfer.
 *
 */

#include <internals.h>
#include <stdlib.h>
#include "private.h"
#include <v3270/filetransfer.h>

/*--[ Widget definition ]----------------------------------------------------------------------------*/

struct _V3270FTActivityClass
{
    GObjectClass parent_class;

};

struct _V3270FTActivity
{
    GObject parent;

    /// @brief Transfer options.
    LIB3270_FT_OPTION options;

    /// @brief Values.
    guint values[LIB3270_FT_VALUE_COUNT];

    /// @brief File names
    struct {
        gchar * local;
        gchar * remote;
    } file;

};

enum
{
    PROP_0,
    PROP_LOCAL,
    PROP_REMOTE
};

G_DEFINE_TYPE(V3270FTActivity, V3270FTActivity, G_TYPE_INITIALLY_UNOWNED);

/*--[ Implement ]------------------------------------------------------------------------------------*/

static void dispose(GObject *object)
{
    debug("activity::%s(%p)",__FUNCTION__,object);

    V3270FTActivity * activity = G_V3270_FT_ACTIVITY(object);

    g_free(activity->file.local);
    g_free(activity->file.remote);

    G_OBJECT_CLASS(V3270FTActivity_parent_class)->dispose(object);

}

static void set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec G_GNUC_UNUSED(*pspec))
{
    switch(prop_id)
    {
    case PROP_LOCAL:
        v3270_ft_activity_set_local_filename(object,g_value_get_string(value));
        break;

    case PROP_REMOTE:
        v3270_ft_activity_set_remote_filename(object,g_value_get_string(value));
        break;
    }

}

static void get_property(GObject *object, guint prop_id, GValue *value, GParamSpec G_GNUC_UNUSED(*pspec))
{
    switch(prop_id)
    {
    case PROP_LOCAL:
        g_value_set_string(value,v3270_ft_activity_get_local_filename(object));
        break;

    case PROP_REMOTE:
        g_value_set_string(value,v3270_ft_activity_get_remote_filename(object));
        break;
    }
}

static void V3270FTActivity_class_init(V3270FTActivityClass *klass)
{
    G_OBJECT_CLASS(klass)->dispose = dispose;

    G_OBJECT_CLASS(klass)->set_property = set_property;
    G_OBJECT_CLASS(klass)->get_property = get_property;

    g_object_class_install_property(
        G_OBJECT_CLASS(klass),
        PROP_LOCAL,
        g_param_spec_string ("local", "Local",
                             _( "Full path of local file" ),
                             FALSE, G_PARAM_READWRITE));

    g_object_class_install_property(
        G_OBJECT_CLASS(klass),
        PROP_LOCAL,
        g_param_spec_string ("remote", "remote",
                             _( "Full path of remote file" ),
                             FALSE, G_PARAM_READWRITE));

}

static void V3270FTActivity_init(V3270FTActivity *widget)
{
    widget->values[LIB3270_FT_VALUE_LRECL] = 0;
    widget->values[LIB3270_FT_VALUE_BLKSIZE] = 0;
    widget->values[LIB3270_FT_VALUE_PRIMSPACE] = 0;
    widget->values[LIB3270_FT_VALUE_SECSPACE] = 0;
    widget->values[LIB3270_FT_VALUE_DFT] = 4096;
}

/**
* v3270_ft_activity_new:
*
* Creates an empty file transfer activity.
*
* Returns: a new #V3270FTActivity.
*/
LIB3270_EXPORT GObject * v3270_ft_activity_new()
{
    return g_object_new(G_TYPE_V3270_FT_ACTIVITY, NULL);
}

LIB3270_EXPORT GObject	* v3270_ft_activity_new_from_filename(const gchar *filename)
{
    GObject *activity = g_object_new(G_TYPE_V3270_FT_ACTIVITY, NULL);

    // Set local filename
    v3270_ft_activity_set_local_filename(activity,filename);

    // Set options
    LIB3270_FT_OPTION options = LIB3270_FT_OPTION_SEND;
    size_t ix;

    for(ix = 0; v3270_text_file_extensions[ix]; ix++)
    {
        if(g_str_has_suffix(filename,v3270_text_file_extensions[ix]))
        {
            options |= (LIB3270_FT_OPTION_ASCII|LIB3270_FT_OPTION_CRLF|LIB3270_FT_OPTION_REMAP);
            break;
        }
    }

    v3270_ft_activity_set_options(activity,options);

    // Set remote filename
    g_autofree gchar * basename	= g_path_get_basename(filename);
    v3270_ft_activity_set_remote_filename(activity,basename);

    return activity;
}


/**
* v3270_ft_activity_get_local_filename:
* @object: a #V3270FTActivity
*
* Get the full  path of the local file.
*
* Returns: the local file path.
*/
LIB3270_EXPORT const gchar * v3270_ft_activity_get_local_filename(const GObject *object)
{
    const gchar *ptr = G_V3270_FT_ACTIVITY(object)->file.local;
    return (ptr ? ptr : "");
}

/**
* v3270_ft_activity_get_remote_filename:
* @object: a #V3270FTActivity
*
* Get the host file name.
*
* Returns: the host file name.
*/
LIB3270_EXPORT const gchar * v3270_ft_activity_get_remote_filename(const GObject *object)
{
    const gchar *ptr = G_V3270_FT_ACTIVITY(object)->file.remote;
    return(ptr ? ptr : "");
}

LIB3270_EXPORT void v3270_ft_activity_set_local_filename(GObject *object, const gchar *filename)
{
    gchar **ptr = & G_V3270_FT_ACTIVITY(object)->file.local;

    g_free(*ptr);
    *ptr = g_strdup(filename);
}

LIB3270_EXPORT void v3270_ft_activity_set_remote_filename(GObject *object, const gchar *filename)
{
    gchar **ptr = & G_V3270_FT_ACTIVITY(object)->file.remote;

    g_free(*ptr);
    *ptr = g_strdup(filename);
}

LIB3270_EXPORT void v3270_ft_activity_set_options(GObject * object, LIB3270_FT_OPTION options)
{
    G_V3270_FT_ACTIVITY(object)->options = options;
}

LIB3270_EXPORT LIB3270_FT_OPTION v3270_ft_activity_get_options(const GObject *object)
{
    return G_V3270_FT_ACTIVITY(object)->options;
}

guint v3270_ft_activity_get_value(const GObject * object,  LIB3270_FT_VALUE id)
{
    return G_V3270_FT_ACTIVITY(object)->values[id];
}

void v3270_ft_activity_set_value(GObject * object, LIB3270_FT_VALUE id, guint value)
{
    G_V3270_FT_ACTIVITY(object)->values[id] = value;
}

static void element_start(GMarkupParseContext G_GNUC_UNUSED(*context), const gchar *element_name, const gchar **names,const gchar **values, V3270FTActivity *activity, GError **error)
{
    size_t ix;

    debug("%s(%s)",__FUNCTION__, element_name);
    if(!g_ascii_strcasecmp(element_name,"file"))
    {
        const gchar *type;
        const gchar *path;

        if(!g_markup_collect_attributes(
                    element_name,names,values,error,
                    G_MARKUP_COLLECT_STRING, "type", &type,
                    G_MARKUP_COLLECT_STRING, "path", &path,
                    G_MARKUP_COLLECT_INVALID
                )) {

            return;

        }

        if(g_ascii_strcasecmp(type,"local") == 0)
            v3270_ft_activity_set_local_filename(G_OBJECT(activity),path);
        else if(g_ascii_strcasecmp(type,"remote") == 0)
            v3270_ft_activity_set_remote_filename(G_OBJECT(activity),path);

    }
    else if(!g_ascii_strcasecmp(element_name,"option"))
    {
        const gchar *name;
        const gchar *value;

        if(!g_markup_collect_attributes(
                    element_name,names,values,error,
                    G_MARKUP_COLLECT_STRING, "name", &name,
                    G_MARKUP_COLLECT_STRING, "value", &value,
                    G_MARKUP_COLLECT_INVALID
                )) {

            return;

        }

        debug("%s.%s(%s,%s)",__FUNCTION__, element_name, name, value);

        for(ix = 0; v3270_activity_list_options[ix].name; ix++)
        {
            if(! (g_ascii_strcasecmp(name,v3270_activity_list_options[ix].name) || g_ascii_strcasecmp(value,v3270_activity_list_options[ix].value)) )
            {
                activity->options |= v3270_activity_list_options[ix].option;
                debug("Setting option %s.%s(%08lx) =%08lx", v3270_activity_list_options[ix].name, v3270_activity_list_options[ix].value, (unsigned int) v3270_activity_list_options[ix].option,(unsigned int) activity->options)
                break;
            }
        }

    }
    else if(!g_ascii_strcasecmp(element_name,"parameter"))
    {
        const gchar *name;
        const gchar *value;

        if(!g_markup_collect_attributes(
                    element_name,names,values,error,
                    G_MARKUP_COLLECT_STRING, "name", &name,
                    G_MARKUP_COLLECT_STRING, "value", &value,
                    G_MARKUP_COLLECT_INVALID
                )) {

            return;

        }

        debug("%s.%s(%s)",__FUNCTION__, element_name, name, value);

        for(ix=0; ix<LIB3270_FT_VALUE_COUNT; ix++)
        {
            if(!g_ascii_strcasecmp(ft_value[ix].name,name))
            {
                activity->values[ix] = atoi(value);
                break;
            }
        }

    }

}

void v3270_ft_activity_set_from_context(GObject * activity, GMarkupParseContext * context)
{
    static const GMarkupParser parser = {
        (void (*)(GMarkupParseContext *, const gchar *, const gchar **, const gchar **, gpointer, GError **)) element_start,
        (void (*)(GMarkupParseContext *, const gchar *, gpointer, GError **)) NULL,
        (void (*)(GMarkupParseContext *, const gchar *, gsize, gpointer, GError **)) NULL,
        (void (*)(GMarkupParseContext *, const gchar *, gsize,  gpointer, GError **)) NULL,
        (void (*)(GMarkupParseContext *, GError *, gpointer)) NULL
    };

    G_V3270_FT_ACTIVITY(activity)->options = 0;

    g_markup_parse_context_push(context,&parser,activity);

}

LIB3270_EXPORT H3270FT * v3270_ft_activity_begin_transfer(GObject * object, H3270 *hSession, const char **message)
{
    V3270FTActivity * activity = G_V3270_FT_ACTIVITY(object);

    return lib3270_ft_new(
               hSession,
               activity->options,
               activity->file.local,
               activity->file.remote,
               activity->values[LIB3270_FT_VALUE_LRECL],
               activity->values[LIB3270_FT_VALUE_BLKSIZE],
               activity->values[LIB3270_FT_VALUE_PRIMSPACE],
               activity->values[LIB3270_FT_VALUE_SECSPACE],
               activity->values[LIB3270_FT_VALUE_DFT],
               message
           );

}

LIB3270_EXPORT void v3270_ft_activity_xml_encode(GObject *object, GString *str)
{
    g_return_if_fail(G_IS_V3270_FT_ACTIVITY(object));

    size_t	  ix;
    V3270FTActivity * activity = G_V3270_FT_ACTIVITY(object);

    g_string_append_printf(
        str,
        "\t<entry>\n\t\t<file type=\'local\' path=\'%s\' />\n\t\t<file type=\'remote\' path=\'%s\' />\n",
        activity->file.local,
        activity->file.remote
    );

    for(ix = 0; v3270_activity_list_options[ix].name; ix++)
    {
        if((activity->options & v3270_activity_list_options[ix].option) == v3270_activity_list_options[ix].option)
            g_string_append_printf(str,"\t\t<option name=\'%s\' value=\'%s\' />\n",v3270_activity_list_options[ix].name,v3270_activity_list_options[ix].value);
    }

    for(ix=0; ix<LIB3270_FT_VALUE_COUNT; ix++)
    {
        g_string_append_printf(str,"\t\t<parameter name=\"%s\" value=\"%u\"/>\n",ft_value[ix].name,activity->values[ix]);
    }

    g_string_append(str,"\t</entry>\n");

}