2007-09-05 16:49:41 -07:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
|
|
|
*
|
2007-09-06 10:01:05 -07:00
|
|
|
* Copyright (C) 2007 Grzegorz DÄ…browski <gdx@o2.pl>
|
2007-09-05 16:49:41 -07:00
|
|
|
*
|
|
|
|
* 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 <gmodule.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <pk-backend.h>
|
|
|
|
|
2007-09-06 10:01:05 -07:00
|
|
|
#include <sqlite3.h>
|
|
|
|
#include <libbox/libbox-db.h>
|
|
|
|
#include <libbox/libbox-db-utils.h>
|
|
|
|
#include <libbox/libbox-db-repos.h>
|
|
|
|
|
2007-09-05 16:49:41 -07:00
|
|
|
/**
|
|
|
|
* backend_initalize:
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
backend_initalize (PkBackend *backend)
|
|
|
|
{
|
|
|
|
g_return_if_fail (backend != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* backend_destroy:
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
backend_destroy (PkBackend *backend)
|
|
|
|
{
|
|
|
|
g_return_if_fail (backend != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-09-06 10:01:05 -07:00
|
|
|
add_packages_from_list (PkBackend *backend, GList *list)
|
2007-09-05 16:49:41 -07:00
|
|
|
{
|
2007-09-06 10:01:05 -07:00
|
|
|
PackageSearch *package = NULL;
|
|
|
|
GList *li = NULL;
|
|
|
|
gchar *pkg_string = NULL;
|
2007-09-05 16:49:41 -07:00
|
|
|
|
2007-09-06 10:01:05 -07:00
|
|
|
for (li = list; li != NULL; li = li->next) {
|
|
|
|
package = (PackageSearch*)li->data;
|
|
|
|
pkg_string = pk_package_id_build(package->package, package->version, package->arch, package->reponame);
|
2007-09-05 16:49:41 -07:00
|
|
|
|
2007-09-06 10:01:05 -07:00
|
|
|
pk_backend_package (backend, package->installed, pkg_string, package->description);
|
|
|
|
|
|
|
|
g_free(pkg_string);
|
|
|
|
}
|
2007-09-05 16:49:41 -07:00
|
|
|
}
|
|
|
|
|
2007-09-06 10:01:05 -07:00
|
|
|
/* TODO: rewrite and share this code */
|
2007-09-05 16:49:41 -07:00
|
|
|
static void
|
2007-09-06 10:01:05 -07:00
|
|
|
parse_filter (const gchar *filter, gboolean *installed, gboolean *available,
|
|
|
|
gboolean *devel, gboolean *nondevel, gboolean *gui, gboolean *text)
|
2007-09-05 16:49:41 -07:00
|
|
|
{
|
2007-09-06 10:01:05 -07:00
|
|
|
gchar **sections = NULL;
|
|
|
|
gint i = 0;
|
|
|
|
|
|
|
|
*installed = TRUE;
|
|
|
|
*available = TRUE;
|
|
|
|
*devel = TRUE;
|
|
|
|
*nondevel = TRUE;
|
|
|
|
*gui = TRUE;
|
|
|
|
*text = TRUE;
|
|
|
|
|
|
|
|
sections = g_strsplit (filter, ";", 0);
|
|
|
|
while (sections[i]) {
|
|
|
|
if (strcmp(sections[i], "installed") == 0)
|
|
|
|
*available = FALSE;
|
|
|
|
if (strcmp(sections[i], "~installed") == 0)
|
|
|
|
*installed = FALSE;
|
|
|
|
if (strcmp(sections[i], "devel") == 0)
|
|
|
|
*nondevel = FALSE;
|
|
|
|
if (strcmp(sections[i], "~devel") == 0)
|
|
|
|
*devel = FALSE;
|
|
|
|
if (strcmp(sections[i], "gui") == 0)
|
|
|
|
*text = FALSE;
|
|
|
|
if (strcmp(sections[i], "~gui") == 0)
|
|
|
|
*gui = FALSE;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
g_strfreev (sections);
|
2007-09-05 16:49:41 -07:00
|
|
|
}
|
|
|
|
|
2007-09-06 10:01:05 -07:00
|
|
|
static gboolean
|
|
|
|
find_packages (PkBackend *backend, const gchar *search, const gchar *filter, gint mode)
|
2007-09-05 16:49:41 -07:00
|
|
|
{
|
2007-09-06 10:01:05 -07:00
|
|
|
GList *list = NULL;
|
|
|
|
sqlite3 *db = NULL;
|
|
|
|
gint devel_filter = 0;
|
|
|
|
gboolean installed;
|
|
|
|
gboolean available;
|
|
|
|
gboolean devel;
|
|
|
|
gboolean nondevel;
|
|
|
|
gboolean gui;
|
|
|
|
gboolean text;
|
|
|
|
|
|
|
|
pk_task_set_job_role (task, PK_TASK_ROLE_QUERY, search);
|
|
|
|
parse_filter (filter, &installed, &available, &devel, &nondevel, &gui, &text);
|
|
|
|
|
|
|
|
if (devel == TRUE) {
|
|
|
|
devel_filter = devel_filter | PKG_DEVEL;
|
|
|
|
}
|
|
|
|
if (nondevel == TRUE) {
|
|
|
|
devel_filter = devel_filter | PKG_NON_DEVEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pk_backend_change_job_status (backend, PK_TASK_STATUS_QUERY);
|
|
|
|
pk_backend_no_percentage_updates (backend);
|
|
|
|
|
|
|
|
db = box_db_open ("/");
|
|
|
|
box_db_attach_repo (db, "/", "core");
|
|
|
|
box_db_repos_init (db);
|
|
|
|
|
|
|
|
if (mode == 1) {
|
|
|
|
/* TODO: allow filtering */
|
|
|
|
/* TODO: make it more async */
|
|
|
|
list = box_db_repos_search_file (db, search);
|
|
|
|
add_packages_from_list (backend, list);
|
|
|
|
box_db_repos_package_list_free (list);
|
|
|
|
pk_backend_finished (backend, PK_TASK_EXIT_SUCCESS);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (installed == FALSE && available == FALSE) {
|
|
|
|
pk_backend_error_code (backend, PK_TASK_ERROR_CODE_UNKNOWN, "invalid search mode");
|
|
|
|
pk_backend_finished (backend, PK_TASK_EXIT_FAILED);
|
|
|
|
} else {
|
|
|
|
/* TODO: make it more async */
|
|
|
|
if (installed == TRUE && available == TRUE) {
|
|
|
|
list = box_db_repos_packages_search_all(db, (gchar *)search, devel_filter);
|
|
|
|
} else if (installed == TRUE) {
|
|
|
|
list = box_db_repos_packages_search_installed(db, (gchar *)search, devel_filter);
|
|
|
|
} else if (available == TRUE) {
|
|
|
|
list = box_db_repos_packages_search_available(db, (gchar *)search, devel_filter);
|
|
|
|
}
|
|
|
|
add_packages_from_list (backend, list);
|
|
|
|
box_db_repos_package_list_free (list);
|
|
|
|
pk_backend_finished (backend, PK_TASK_EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
box_db_detach_repo(db, "core");
|
|
|
|
box_db_close(db);
|
|
|
|
|
|
|
|
return TRUE;
|
2007-09-05 16:49:41 -07:00
|
|
|
}
|
|
|
|
|
2007-09-06 10:01:05 -07:00
|
|
|
static GList*
|
|
|
|
find_package_by_id (PkPackageId *pi)
|
2007-09-05 16:49:41 -07:00
|
|
|
{
|
2007-09-06 10:01:05 -07:00
|
|
|
sqlite3 *db = NULL;
|
|
|
|
GList *list;
|
|
|
|
|
|
|
|
db = box_db_open("/");
|
|
|
|
box_db_attach_repo(db, "/", "core");
|
|
|
|
box_db_repos_init(db);
|
|
|
|
|
|
|
|
/* only one element is returned */
|
|
|
|
list = box_db_repos_packages_search_by_data(db, pi->name, pi->version);
|
|
|
|
if (list == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
box_db_detach_repo(db, "core");
|
|
|
|
box_db_close(db);
|
|
|
|
|
|
|
|
return list;
|
2007-09-05 16:49:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-09-06 10:01:05 -07:00
|
|
|
* backend_get_description:
|
2007-09-05 16:49:41 -07:00
|
|
|
*/
|
|
|
|
static void
|
2007-09-06 10:01:05 -07:00
|
|
|
backend_get_description (PkBackend *backend, const gchar *package_id)
|
2007-09-05 16:49:41 -07:00
|
|
|
{
|
2007-09-06 10:01:05 -07:00
|
|
|
PkPackageId *pi;
|
|
|
|
PackageSearch *ps;
|
|
|
|
GList *list;
|
|
|
|
|
2007-09-05 16:49:41 -07:00
|
|
|
g_return_if_fail (backend != NULL);
|
2007-09-06 10:01:05 -07:00
|
|
|
|
|
|
|
pi = pk_package_id_new_from_string (package_id);
|
|
|
|
if (pi == NULL) {
|
|
|
|
pk_backend_error_code (backend, PK_TASK_ERROR_CODE_PACKAGE_ID_INVALID, "invalid package id");
|
|
|
|
pk_backend_finished (backend, PK_TASK_EXIT_FAILED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
list = find_package_by_id (pi);
|
|
|
|
ps = (PackageSearch*) list->data;
|
|
|
|
if (list == NULL) {
|
|
|
|
pk_backend_error_code (backend, PK_TASK_ERROR_CODE_PACKAGE_ID_INVALID, "cannot find package by id");
|
|
|
|
pk_backend_finished (backend, PK_TASK_EXIT_FAILED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pk_backend_set_job_role (backend, PK_TASK_ROLE_QUERY, package_id);
|
|
|
|
pk_backend_description (backend, pi->name, PK_TASK_GROUP_OTHER, ps->description, "");
|
|
|
|
|
|
|
|
pk_package_id_free (pi);
|
|
|
|
box_db_repos_package_list_free (list);
|
|
|
|
|
|
|
|
pk_backend_finished (backend, PK_TASK_EXIT_SUCCESS);
|
|
|
|
return;
|
2007-09-05 16:49:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-09-06 10:01:05 -07:00
|
|
|
* backend_get_updates:
|
2007-09-05 16:49:41 -07:00
|
|
|
*/
|
|
|
|
static void
|
2007-09-06 10:01:05 -07:00
|
|
|
backend_get_updates (PkBackend *backend)
|
2007-09-05 16:49:41 -07:00
|
|
|
{
|
2007-09-06 10:01:05 -07:00
|
|
|
GList *list = NULL;
|
|
|
|
sqlite3 *db = NULL;
|
|
|
|
|
2007-09-05 16:49:41 -07:00
|
|
|
g_return_if_fail (backend != NULL);
|
2007-09-06 10:01:05 -07:00
|
|
|
|
|
|
|
pk_backend_set_job_role (backend, PK_TASK_ROLE_QUERY, NULL);
|
|
|
|
pk_backend_change_job_status (backend, PK_TASK_STATUS_QUERY);
|
|
|
|
|
|
|
|
db = box_db_open ("/");
|
|
|
|
box_db_attach_repo (db, "/", "core");
|
|
|
|
box_db_repos_init (db);
|
|
|
|
|
|
|
|
/* TODO: make it more async */
|
|
|
|
list = box_db_repos_packages_for_upgrade (db);
|
|
|
|
add_packages_from_list (backend, list);
|
|
|
|
box_db_repos_package_list_free (list);
|
|
|
|
|
|
|
|
pk_backend_finished (backend, PK_TASK_EXIT_SUCCESS);
|
|
|
|
|
|
|
|
box_db_detach_repo (db, "core");
|
|
|
|
box_db_close (db);
|
2007-09-05 16:49:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-09-06 10:01:05 -07:00
|
|
|
* backend_refresh_cache:
|
2007-09-05 16:49:41 -07:00
|
|
|
*/
|
|
|
|
static void
|
2007-09-06 10:01:05 -07:00
|
|
|
backend_refresh_cache (PkBackend *backend, gboolean force)
|
2007-09-05 16:49:41 -07:00
|
|
|
{
|
|
|
|
g_return_if_fail (backend != NULL);
|
2007-09-06 10:01:05 -07:00
|
|
|
/* check network state */
|
|
|
|
if (pk_backend_network_is_online (backend) == FALSE) {
|
|
|
|
pk_backend_error_code (backend, PK_TASK_ERROR_CODE_NO_NETWORK, "Cannot refresh cache whilst offline");
|
|
|
|
pk_backend_finished (backend, PK_TASK_EXIT_FAILED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pk_backend_set_job_role (backend, PK_TASK_ROLE_REFRESH_CACHE, NULL);
|
|
|
|
pk_backend_spawn_helper (backend, "refresh-cache.sh", NULL);
|
2007-09-05 16:49:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* backend_search_file:
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
backend_search_file (PkBackend *backend, const gchar *filter, const gchar *search)
|
|
|
|
{
|
|
|
|
g_return_if_fail (backend != NULL);
|
2007-09-06 10:01:05 -07:00
|
|
|
find_packages (backend, search, filter, 1);
|
2007-09-05 16:49:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* backend_search_name:
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
backend_search_name (PkBackend *backend, const gchar *filter, const gchar *search)
|
|
|
|
{
|
|
|
|
g_return_if_fail (backend != NULL);
|
2007-09-06 10:01:05 -07:00
|
|
|
find_packages (backend, search, filter, 0);
|
2007-09-05 16:49:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PK_BACKEND_OPTIONS (
|
2007-09-06 10:01:05 -07:00
|
|
|
"Dummy Backend", /* description */
|
|
|
|
"0.0.1", /* version */
|
|
|
|
"Grzegorz DÄ…browski <gdx@o2.pl>", /* author */
|
|
|
|
backend_initalize, /* initalize */
|
|
|
|
backend_destroy, /* destroy */
|
|
|
|
NULL, /* cancel_job_try */
|
|
|
|
NULL, /* get_depends */
|
|
|
|
backend_get_description, /* get_description */
|
|
|
|
NULL, /* get_requires */
|
|
|
|
backend_get_updates, /* get_updates */
|
|
|
|
NULL, /* install_package */
|
|
|
|
backend_refresh_cache, /* refresh_cache */
|
|
|
|
NULL, /* remove_package */
|
|
|
|
NULL, /* search_details */
|
|
|
|
backend_search_file, /* search_file */
|
|
|
|
NULL, /* search_group */
|
|
|
|
backend_search_name, /* search_name */
|
|
|
|
NULL, /* update_package */
|
|
|
|
NULL /* update_system */
|
2007-09-05 16:49:41 -07:00
|
|
|
);
|
|
|
|
|