Skip to content

Commit

Permalink
appmodel: extract AppObjectGenerator base class to its own module
Browse files Browse the repository at this point in the history
Signed-off-by: Balazs Scheidler <[email protected]>
  • Loading branch information
bazsi committed May 21, 2024
1 parent 2f9daeb commit da0a0c1
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 103 deletions.
1 change: 1 addition & 0 deletions modules/appmodel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set (APPMODEL_SOURCES
appmodel-parser.c
appmodel-plugin.c
appmodel-context.c
app-object-generator.c
app-parser-generator.c
application.c
)
Expand Down
2 changes: 2 additions & 0 deletions modules/appmodel/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ modules_appmodel_libappmodel_la_SOURCES = \
modules/appmodel/appmodel-plugin.c \
modules/appmodel/appmodel-context.c \
modules/appmodel/appmodel-context.h \
modules/appmodel/app-object-generator.c \
modules/appmodel/app-object-generator.h \
modules/appmodel/app-parser-generator.c \
modules/appmodel/app-parser-generator.h \
modules/appmodel/application.c \
Expand Down
113 changes: 113 additions & 0 deletions modules/appmodel/app-object-generator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2017 Balabit
* Copyright (c) 2017 Balazs Scheidler <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/

#include "app-object-generator.h"
#include "appmodel.h"

#include <string.h>

gboolean
app_object_generator_is_application_included(AppObjectGenerator *self, const gchar *app_name)
{
/* include everything if we don't have the option */
if (!self->included_apps)
return TRUE;
return strstr(self->included_apps, app_name) != NULL;
}

gboolean
app_object_generator_is_application_excluded(AppObjectGenerator *self, const gchar *app_name)
{
if (!self->excluded_apps)
return FALSE;
return strstr(self->excluded_apps, app_name) != NULL;
}

static gboolean
_parse_auto_parse_arg(AppObjectGenerator *self, CfgArgs *args, const gchar *reference)
{
const gchar *v = cfg_args_get(args, "auto-parse");

if (v)
self->is_parsing_enabled = cfg_process_yesno(v);
else
self->is_parsing_enabled = TRUE;
return TRUE;
}

static gboolean
_parse_auto_parse_exclude_arg(AppObjectGenerator *self, CfgArgs *args, const gchar *reference)
{
const gchar *v = cfg_args_get(args, "auto-parse-exclude");
if (!v)
return TRUE;
self->excluded_apps = g_strdup(v);
return TRUE;
}

static gboolean
_parse_auto_parse_include_arg(AppObjectGenerator *self, CfgArgs *args, const gchar *reference)
{
const gchar *v = cfg_args_get(args, "auto-parse-include");
if (!v)
return TRUE;
self->included_apps = g_strdup(v);
return TRUE;
}

gboolean
app_object_generator_parse_arguments_method(AppObjectGenerator *self, CfgArgs *args, const gchar *reference)
{
g_assert(args != NULL);

if (!_parse_auto_parse_arg(self, args, reference))
return FALSE;
if (!_parse_auto_parse_exclude_arg(self, args, reference))
return FALSE;
if (!_parse_auto_parse_include_arg(self, args, reference))
return FALSE;
return TRUE;
}

static gboolean
_generate(CfgBlockGenerator *s, GlobalConfig *cfg, gpointer args, GString *result, const gchar *reference)
{
AppObjectGenerator *self = (AppObjectGenerator *) s;
CfgArgs *cfgargs = (CfgArgs *)args;

if (!self->parse_arguments(self, cfgargs, reference))
return FALSE;

self->generate_config(self, cfg, result);

return TRUE;
}

void
app_object_generator_init_instance(AppObjectGenerator *self, gint context, const gchar *name)
{
cfg_block_generator_init_instance(&self->super, context, name);
self->super.generate = _generate;
self->parse_arguments = app_object_generator_parse_arguments_method;
}
49 changes: 49 additions & 0 deletions modules/appmodel/app-object-generator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2017 Balabit
* Copyright (c) 2017 Balazs Scheidler <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/

#ifndef APPMODEL_APP_OBJECT_GENERATOR_H_INCLUDED
#define APPMODEL_APP_OBJECT_GENERATOR_H_INCLUDED

#include "plugin.h"

typedef struct _AppObjectGenerator AppObjectGenerator;

struct _AppObjectGenerator
{
CfgBlockGenerator super;
gboolean (*parse_arguments)(AppObjectGenerator *self, CfgArgs *args, const gchar *reference);
void (*generate_config)(AppObjectGenerator *self, GlobalConfig *cfg, GString *result);
const gchar *included_apps;
const gchar *excluded_apps;
gboolean is_parsing_enabled;
};

gboolean app_object_generator_is_application_included(AppObjectGenerator *self, const gchar *app_name);
gboolean app_object_generator_is_application_excluded(AppObjectGenerator *self, const gchar *app_name);

gboolean app_object_generator_parse_arguments_method(AppObjectGenerator *self, CfgArgs *args, const gchar *reference);
void app_object_generator_init_instance(AppObjectGenerator *self, gint context, const gchar *name);


#endif
106 changes: 3 additions & 103 deletions modules/appmodel/app-parser-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,109 +22,9 @@
*
*/

#include "app-parser-generator.h"
#include "app-object-generator.h"
#include "appmodel.h"

#include <string.h>

typedef struct _AppObjectGenerator AppObjectGenerator;

struct _AppObjectGenerator
{
CfgBlockGenerator super;
gboolean (*parse_arguments)(AppObjectGenerator *self, CfgArgs *args, const gchar *reference);
void (*generate_config)(AppObjectGenerator *self, GlobalConfig *cfg, GString *result);
const gchar *included_apps;
const gchar *excluded_apps;
gboolean is_parsing_enabled;
};

static gboolean
_is_application_included(AppObjectGenerator *self, const gchar *app_name)
{
/* include everything if we don't have the option */
if (!self->included_apps)
return TRUE;
return strstr(self->included_apps, app_name) != NULL;
}

static gboolean
_is_application_excluded(AppObjectGenerator *self, const gchar *app_name)
{
if (!self->excluded_apps)
return FALSE;
return strstr(self->excluded_apps, app_name) != NULL;
}

static gboolean
_parse_auto_parse_arg(AppObjectGenerator *self, CfgArgs *args, const gchar *reference)
{
const gchar *v = cfg_args_get(args, "auto-parse");

if (v)
self->is_parsing_enabled = cfg_process_yesno(v);
else
self->is_parsing_enabled = TRUE;
return TRUE;
}

static gboolean
_parse_auto_parse_exclude_arg(AppObjectGenerator *self, CfgArgs *args, const gchar *reference)
{
const gchar *v = cfg_args_get(args, "auto-parse-exclude");
if (!v)
return TRUE;
self->excluded_apps = g_strdup(v);
return TRUE;
}

static gboolean
_parse_auto_parse_include_arg(AppObjectGenerator *self, CfgArgs *args, const gchar *reference)
{
const gchar *v = cfg_args_get(args, "auto-parse-include");
if (!v)
return TRUE;
self->included_apps = g_strdup(v);
return TRUE;
}


static gboolean
app_object_generator_parse_arguments_method(AppObjectGenerator *self, CfgArgs *args, const gchar *reference)
{
g_assert(args != NULL);

if (!_parse_auto_parse_arg(self, args, reference))
return FALSE;
if (!_parse_auto_parse_exclude_arg(self, args, reference))
return FALSE;
if (!_parse_auto_parse_include_arg(self, args, reference))
return FALSE;
return TRUE;
}

static gboolean
_generate(CfgBlockGenerator *s, GlobalConfig *cfg, gpointer args, GString *result, const gchar *reference)
{
AppObjectGenerator *self = (AppObjectGenerator *) s;
CfgArgs *cfgargs = (CfgArgs *)args;

if (!self->parse_arguments(self, cfgargs, reference))
return FALSE;

self->generate_config(self, cfg, result);

return TRUE;
}

void
app_object_generator_init_instance(AppObjectGenerator *self, gint context, const gchar *name)
{
cfg_block_generator_init_instance(&self->super, context, name);
self->super.generate = _generate;
self->parse_arguments = app_object_generator_parse_arguments_method;
}

/* app-parser() */

typedef struct _AppParserGenerator
Expand Down Expand Up @@ -220,10 +120,10 @@ _generate_application(Application *app, gpointer user_data)
if (strcmp(self->topic, app->super.instance) != 0)
return;

if (!_is_application_included(&self->super, app->super.name))
if (!app_object_generator_is_application_included(&self->super, app->super.name))
return;

if (_is_application_excluded(&self->super, app->super.name))
if (app_object_generator_is_application_excluded(&self->super, app->super.name))
return;

if (self->first_app_generated)
Expand Down

0 comments on commit da0a0c1

Please sign in to comment.