236 lines
5.8 KiB
C
236 lines
5.8 KiB
C
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
|
*
|
|
* Copyright (C) 2007 Richard Hughes <richard@hughsie.com>
|
|
*
|
|
* Licensed under the GNU General Public License Version 2
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program 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 General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#ifdef HAVE_UNISTD_H
|
|
#include <unistd.h>
|
|
#endif /* HAVE_UNISTD_H */
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include "pk-debug.h"
|
|
#include "pk-task-common.h"
|
|
|
|
/**
|
|
* pk_task_filter_check_part:
|
|
**/
|
|
gboolean
|
|
pk_task_filter_check_part (const gchar *filter)
|
|
{
|
|
if (filter == NULL) {
|
|
return FALSE;
|
|
}
|
|
if (strlen (filter) == 0) {
|
|
return FALSE;
|
|
}
|
|
if (strcmp (filter, "none") == 0) {
|
|
return TRUE;
|
|
}
|
|
if (strcmp (filter, "installed") == 0) {
|
|
return TRUE;
|
|
}
|
|
if (strcmp (filter, "~installed") == 0) {
|
|
return TRUE;
|
|
}
|
|
if (strcmp (filter, "devel") == 0) {
|
|
return TRUE;
|
|
}
|
|
if (strcmp (filter, "~devel") == 0) {
|
|
return TRUE;
|
|
}
|
|
if (strcmp (filter, "gui") == 0) {
|
|
return TRUE;
|
|
}
|
|
if (strcmp (filter, "~gui") == 0) {
|
|
return TRUE;
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
/**
|
|
* pk_task_filter_check:
|
|
**/
|
|
gboolean
|
|
pk_task_filter_check (const gchar *filter)
|
|
{
|
|
gchar **sections;
|
|
guint i;
|
|
guint length;
|
|
gboolean ret;
|
|
|
|
if (filter == NULL) {
|
|
pk_warning ("filter null");
|
|
return FALSE;
|
|
}
|
|
if (strlen (filter) == 0) {
|
|
pk_warning ("filter zero length");
|
|
return FALSE;
|
|
}
|
|
|
|
/* split by delimeter ';' */
|
|
sections = g_strsplit (filter, ";", 0);
|
|
length = g_strv_length (sections);
|
|
ret = FALSE;
|
|
for (i=0; i<length; i++) {
|
|
/* only one wrong part is enough to fail the filter */
|
|
if (strlen (sections[i]) == 0) {
|
|
goto out;
|
|
}
|
|
if (pk_task_filter_check_part (sections[i]) == FALSE) {
|
|
goto out;
|
|
}
|
|
}
|
|
ret = TRUE;
|
|
out:
|
|
g_strfreev (sections);
|
|
return ret;
|
|
}
|
|
|
|
/***************************************************************************
|
|
*** MAKE CHECK TESTS ***
|
|
***************************************************************************/
|
|
#ifdef PK_BUILD_TESTS
|
|
#include <libselftest.h>
|
|
|
|
void
|
|
libst_task_common (LibSelfTest *test)
|
|
{
|
|
gboolean ret;
|
|
gchar *text;
|
|
const gchar *temp;
|
|
|
|
if (libst_start (test, "PkTaskCommon", CLASS_AUTO) == FALSE) {
|
|
return;
|
|
}
|
|
|
|
/************************************************************
|
|
**************** FILTERS ******************
|
|
************************************************************/
|
|
temp = NULL;
|
|
libst_title (test, "test a fail filter (null)");
|
|
ret = pk_task_filter_check (temp);
|
|
if (ret == FALSE) {
|
|
libst_success (test, NULL);
|
|
} else {
|
|
libst_failed (test, "passed the filter '%s'", temp);
|
|
}
|
|
|
|
/************************************************************/
|
|
temp = "";
|
|
libst_title (test, "test a fail filter ()");
|
|
ret = pk_task_filter_check (temp);
|
|
if (ret == FALSE) {
|
|
libst_success (test, NULL);
|
|
} else {
|
|
libst_failed (test, "passed the filter '%s'", temp);
|
|
}
|
|
|
|
/************************************************************/
|
|
temp = ";";
|
|
libst_title (test, "test a fail filter (;)");
|
|
ret = pk_task_filter_check (temp);
|
|
if (ret == FALSE) {
|
|
libst_success (test, NULL);
|
|
} else {
|
|
libst_failed (test, "passed the filter '%s'", temp);
|
|
}
|
|
|
|
/************************************************************/
|
|
temp = "moo";
|
|
libst_title (test, "test a fail filter (invalid)");
|
|
ret = pk_task_filter_check (temp);
|
|
if (ret == FALSE) {
|
|
libst_success (test, NULL);
|
|
} else {
|
|
libst_failed (test, "passed the filter '%s'", temp);
|
|
}
|
|
|
|
/************************************************************/
|
|
temp = "moo;foo";
|
|
libst_title (test, "test a fail filter (invalid, multiple)");
|
|
ret = pk_task_filter_check (temp);
|
|
if (ret == FALSE) {
|
|
libst_success (test, NULL);
|
|
} else {
|
|
libst_failed (test, "passed the filter '%s'", temp);
|
|
}
|
|
|
|
/************************************************************/
|
|
temp = "gui;;";
|
|
libst_title (test, "test a fail filter (valid then zero length)");
|
|
ret = pk_task_filter_check (temp);
|
|
if (ret == FALSE) {
|
|
libst_success (test, NULL);
|
|
} else {
|
|
libst_failed (test, "passed the filter '%s'", temp);
|
|
}
|
|
|
|
/************************************************************/
|
|
temp = "none";
|
|
libst_title (test, "test a pass filter (none)");
|
|
ret = pk_task_filter_check (temp);
|
|
if (ret == TRUE) {
|
|
libst_success (test, NULL);
|
|
} else {
|
|
libst_failed (test, "failed the filter '%s'", temp);
|
|
}
|
|
|
|
/************************************************************/
|
|
temp = "gui";
|
|
libst_title (test, "test a pass filter (single)");
|
|
ret = pk_task_filter_check (temp);
|
|
if (ret == TRUE) {
|
|
libst_success (test, NULL);
|
|
} else {
|
|
libst_failed (test, "failed the filter '%s'", temp);
|
|
}
|
|
|
|
/************************************************************/
|
|
temp = "devel;~gui";
|
|
libst_title (test, "test a pass filter (multiple)");
|
|
ret = pk_task_filter_check (temp);
|
|
if (ret == TRUE) {
|
|
libst_success (test, NULL);
|
|
} else {
|
|
libst_failed (test, "failed the filter '%s'", temp);
|
|
}
|
|
|
|
/************************************************************/
|
|
temp = "~gui;~installed";
|
|
libst_title (test, "test a pass filter (multiple2)");
|
|
ret = pk_task_filter_check (temp);
|
|
if (ret == TRUE) {
|
|
libst_success (test, NULL);
|
|
} else {
|
|
libst_failed (test, "failed the filter '%s'", temp);
|
|
}
|
|
|
|
libst_end (test);
|
|
}
|
|
#endif
|
|
|