diff options
Diffstat (limited to 'plugins/packagekit')
22 files changed, 5590 insertions, 0 deletions
diff --git a/plugins/packagekit/gs-markdown.c b/plugins/packagekit/gs-markdown.c new file mode 100644 index 0000000..b7be06b --- /dev/null +++ b/plugins/packagekit/gs-markdown.c @@ -0,0 +1,856 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2008 Richard Hughes <richard@hughsie.com> + * Copyright (C) 2015 Kalev Lember <klember@redhat.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include "config.h" + +#include <string.h> +#include <glib.h> + +#include "gs-markdown.h" + +/******************************************************************************* + * + * This is a simple Markdown parser. + * It can output to Pango, HTML or plain text. The following limitations are + * already known, and properly deliberate: + * + * - No code section support + * - No ordered list support + * - No blockquote section support + * - No image support + * - No links or email support + * - No backslash escapes support + * - No HTML escaping support + * - Auto-escapes certain word patterns, like http:// + * + * It does support the rest of the standard pretty well, although it's not + * been run against any conformance tests. The parsing is single pass, with + * a simple enumerated interpretor mode and a single line back-memory. + * + ******************************************************************************/ + +typedef enum { + GS_MARKDOWN_MODE_BLANK, + GS_MARKDOWN_MODE_RULE, + GS_MARKDOWN_MODE_BULLETT, + GS_MARKDOWN_MODE_PARA, + GS_MARKDOWN_MODE_H1, + GS_MARKDOWN_MODE_H2, + GS_MARKDOWN_MODE_UNKNOWN +} GsMarkdownMode; + +typedef struct { + const gchar *em_start; + const gchar *em_end; + const gchar *strong_start; + const gchar *strong_end; + const gchar *code_start; + const gchar *code_end; + const gchar *h1_start; + const gchar *h1_end; + const gchar *h2_start; + const gchar *h2_end; + const gchar *bullet_start; + const gchar *bullet_end; + const gchar *rule; +} GsMarkdownTags; + +struct _GsMarkdown { + GObject parent_instance; + + GsMarkdownMode mode; + GsMarkdownTags tags; + GsMarkdownOutputKind output; + gint max_lines; + gint line_count; + gboolean smart_quoting; + gboolean escape; + gboolean autocode; + gboolean autolinkify; + GString *pending; + GString *processed; +}; + +G_DEFINE_TYPE (GsMarkdown, gs_markdown, G_TYPE_OBJECT) + +/* + * gs_markdown_to_text_line_is_rule: + * + * Horizontal rules are created by placing three or more hyphens, asterisks, + * or underscores on a line by themselves. + * You may use spaces between the hyphens or asterisks. + **/ +static gboolean +gs_markdown_to_text_line_is_rule (const gchar *line) +{ + guint i; + guint len; + guint count = 0; + g_autofree gchar *copy = NULL; + + len = (guint) strlen (line); + if (len == 0) + return FALSE; + + /* replace non-rule chars with ~ */ + copy = g_strdup (line); + g_strcanon (copy, "-*_ ", '~'); + for (i = 0; i < len; i++) { + if (copy[i] == '~') + return FALSE; + if (copy[i] != ' ') + count++; + } + + /* if we matched, return true */ + if (count >= 3) + return TRUE; + return FALSE; +} + +static gboolean +gs_markdown_to_text_line_is_bullet (const gchar *line) +{ + return (g_str_has_prefix (line, "- ") || + g_str_has_prefix (line, "* ") || + g_str_has_prefix (line, "+ ") || + g_str_has_prefix (line, " - ") || + g_str_has_prefix (line, " * ") || + g_str_has_prefix (line, " + ")); +} + +static gboolean +gs_markdown_to_text_line_is_header1 (const gchar *line) +{ + return g_str_has_prefix (line, "# "); +} + +static gboolean +gs_markdown_to_text_line_is_header2 (const gchar *line) +{ + return g_str_has_prefix (line, "## "); +} + +static gboolean +gs_markdown_to_text_line_is_header1_type2 (const gchar *line) +{ + return g_str_has_prefix (line, "==="); +} + +static gboolean +gs_markdown_to_text_line_is_header2_type2 (const gchar *line) +{ + return g_str_has_prefix (line, "---"); +} + +#if 0 +static gboolean +gs_markdown_to_text_line_is_code (const gchar *line) +{ + return (g_str_has_prefix (line, " ") || + g_str_has_prefix (line, "\t")); +} + +static gboolean +gs_markdown_to_text_line_is_blockquote (const gchar *line) +{ + return (g_str_has_prefix (line, "> ")); +} +#endif + +static gboolean +gs_markdown_to_text_line_is_blank (const gchar *line) +{ + guint i; + guint len; + + /* a line with no characters is blank by definition */ + len = (guint) strlen (line); + if (len == 0) + return TRUE; + + /* find if there are only space chars */ + for (i = 0; i < len; i++) { + if (line[i] != ' ' && line[i] != '\t') + return FALSE; + } + + /* if we matched, return true */ + return TRUE; +} + +static gchar * +gs_markdown_replace (const gchar *haystack, + const gchar *needle, + const gchar *replace) +{ + g_auto(GStrv) split = NULL; + split = g_strsplit (haystack, needle, -1); + return g_strjoinv (replace, split); +} + +static gchar * +gs_markdown_strstr_spaces (const gchar *haystack, const gchar *needle) +{ + gchar *found; + const gchar *haystack_new = haystack; + +retry: + /* don't find if surrounded by spaces */ + found = strstr (haystack_new, needle); + if (found == NULL) + return NULL; + + /* start of the string, always valid */ + if (found == haystack) + return found; + + /* end of the string, always valid */ + if (*(found-1) == ' ' && *(found+1) == ' ') { + haystack_new = found+1; + goto retry; + } + return found; +} + +static gchar * +gs_markdown_to_text_line_formatter (const gchar *line, + const gchar *formatter, + const gchar *left, + const gchar *right) +{ + guint len; + gchar *str1; + gchar *str2; + gchar *start = NULL; + gchar *middle = NULL; + gchar *end = NULL; + g_autofree gchar *copy = NULL; + + /* needed to know for shifts */ + len = (guint) strlen (formatter); + if (len == 0) + return NULL; + + /* find sections */ + copy = g_strdup (line); + str1 = gs_markdown_strstr_spaces (copy, formatter); + if (str1 != NULL) { + *str1 = '\0'; + str2 = gs_markdown_strstr_spaces (str1+len, formatter); + if (str2 != NULL) { + *str2 = '\0'; + middle = str1 + len; + start = copy; + end = str2 + len; + } + } + + /* if we found, replace and keep looking for the same string */ + if (start != NULL && middle != NULL && end != NULL) { + g_autofree gchar *temp = NULL; + temp = g_strdup_printf ("%s%s%s%s%s", start, left, middle, right, end); + /* recursive */ + return gs_markdown_to_text_line_formatter (temp, formatter, left, right); + } + + /* not found, keep return as-is */ + return g_strdup (line); +} + +static gchar * +gs_markdown_to_text_line_format_sections (GsMarkdown *self, const gchar *line) +{ + gchar *data = g_strdup (line); + gchar *temp; + + /* bold1 */ + temp = data; + data = gs_markdown_to_text_line_formatter (temp, "**", + self->tags.strong_start, + self->tags.strong_end); + g_free (temp); + + /* bold2 */ + temp = data; + data = gs_markdown_to_text_line_formatter (temp, "__", + self->tags.strong_start, + self->tags.strong_end); + g_free (temp); + + /* italic1 */ + temp = data; + data = gs_markdown_to_text_line_formatter (temp, "*", + self->tags.em_start, + self->tags.em_end); + g_free (temp); + + /* italic2 */ + temp = data; + data = gs_markdown_to_text_line_formatter (temp, "_", + self->tags.em_start, + self->tags.em_end); + g_free (temp); + + /* em-dash */ + temp = data; + data = gs_markdown_replace (temp, " -- ", " — "); + g_free (temp); + + /* smart quoting */ + if (self->smart_quoting) { + temp = data; + data = gs_markdown_to_text_line_formatter (temp, "\"", "“", "”"); + g_free (temp); + + temp = data; + data = gs_markdown_to_text_line_formatter (temp, "'", "‘", "’"); + g_free (temp); + } + + return data; +} + +static gchar * +gs_markdown_to_text_line_format (GsMarkdown *self, const gchar *line) +{ + GString *string; + gboolean mode = FALSE; + gchar *text; + guint i; + g_auto(GStrv) codes = NULL; + + /* optimise the trivial case where we don't have any code tags */ + text = strstr (line, "`"); + if (text == NULL) + return gs_markdown_to_text_line_format_sections (self, line); + + /* we want to parse the code sections without formatting */ + codes = g_strsplit (line, "`", -1); + string = g_string_new (""); + for (i = 0; codes[i] != NULL; i++) { + if (!mode) { + text = gs_markdown_to_text_line_format_sections (self, codes[i]); + g_string_append (string, text); + g_free (text); + mode = TRUE; + } else { + /* just append without formatting */ + g_string_append (string, self->tags.code_start); + g_string_append (string, codes[i]); + g_string_append (string, self->tags.code_end); + mode = FALSE; + } + } + return g_string_free (string, FALSE); +} + +static gboolean +gs_markdown_add_pending (GsMarkdown *self, const gchar *line) +{ + g_autofree gchar *copy = NULL; + + /* would put us over the limit */ + if (self->max_lines > 0 && self->line_count >= self->max_lines) + return FALSE; + + copy = g_strdup (line); + + /* strip leading and trailing spaces */ + g_strstrip (copy); + + /* append */ + g_string_append_printf (self->pending, "%s ", copy); + return TRUE; +} + +static gboolean +gs_markdown_add_pending_header (GsMarkdown *self, const gchar *line) +{ + g_autofree gchar *copy = NULL; + + /* strip trailing # */ + copy = g_strdup (line); + g_strdelimit (copy, "#", ' '); + return gs_markdown_add_pending (self, copy); +} + +static guint +gs_markdown_count_chars_in_word (const gchar *text, gchar find) +{ + guint i; + guint len; + guint count = 0; + + /* get length */ + len = (guint) strlen (text); + if (len == 0) + return 0; + + /* find matching chars */ + for (i = 0; i < len; i++) { + if (text[i] == find) + count++; + } + return count; +} + +static gboolean +gs_markdown_word_is_code (const gchar *text) +{ + /* already code */ + if (g_str_has_prefix (text, "`")) + return FALSE; + if (g_str_has_suffix (text, "`")) + return FALSE; + + /* paths */ + if (g_str_has_prefix (text, "/")) + return TRUE; + + /* bugzillas */ + if (g_str_has_prefix (text, "#")) + return TRUE; + + /* patch files */ + if (g_strrstr (text, ".patch") != NULL) + return TRUE; + if (g_strrstr (text, ".diff") != NULL) + return TRUE; + + /* function names */ + if (g_strrstr (text, "()") != NULL) + return TRUE; + + /* email addresses */ + if (g_strrstr (text, "@") != NULL) + return TRUE; + + /* compiler defines */ + if (text[0] != '_' && + gs_markdown_count_chars_in_word (text, '_') > 1) + return TRUE; + + /* nothing special */ + return FALSE; +} + +static gchar * +gs_markdown_word_auto_format_code (const gchar *text) +{ + guint i; + gchar *temp; + gboolean ret = FALSE; + g_auto(GStrv) words = NULL; + + /* split sentence up with space */ + words = g_strsplit (text, " ", -1); + + /* search each word */ + for (i = 0; words[i] != NULL; i++) { + if (gs_markdown_word_is_code (words[i])) { + temp = g_strdup_printf ("`%s`", words[i]); + g_free (words[i]); + words[i] = temp; + ret = TRUE; + } + } + + /* no replacements, so just return a copy */ + if (!ret) + return g_strdup (text); + + /* join the array back into a string */ + return g_strjoinv (" ", words); +} + +static gboolean +gs_markdown_word_is_url (const gchar *text) +{ + if (g_str_has_prefix (text, "http://")) + return TRUE; + if (g_str_has_prefix (text, "https://")) + return TRUE; + if (g_str_has_prefix (text, "ftp://")) + return TRUE; + return FALSE; +} + +static gchar * +gs_markdown_word_auto_format_urls (const gchar *text) +{ + guint i; + gchar *temp; + gboolean ret = FALSE; + g_auto(GStrv) words = NULL; + + /* split sentence up with space */ + words = g_strsplit (text, " ", -1); + + /* search each word */ + for (i = 0; words[i] != NULL; i++) { + if (gs_markdown_word_is_url (words[i])) { + temp = g_strdup_printf ("<a href=\"%s\">%s</a>", + words[i], words[i]); + g_free (words[i]); + words[i] = temp; + ret = TRUE; + } + } + + /* no replacements, so just return a copy */ + if (!ret) + return g_strdup (text); + + /* join the array back into a string */ + return g_strjoinv (" ", words); +} + +static void +gs_markdown_flush_pending (GsMarkdown *self) +{ + g_autofree gchar *copy = NULL; + g_autofree gchar *temp = NULL; + + /* no data yet */ + if (self->mode == GS_MARKDOWN_MODE_UNKNOWN) + return; + + /* remove trailing spaces */ + while (g_str_has_suffix (self->pending->str, " ")) + g_string_set_size (self->pending, self->pending->len - 1); + + /* pango requires escaping */ + copy = g_strdup (self->pending->str); + if (!self->escape && self->output == GS_MARKDOWN_OUTPUT_PANGO) { + g_strdelimit (copy, "<", '('); + g_strdelimit (copy, ">", ')'); + g_strdelimit (copy, "&", '+'); + } + + /* check words for code */ + if (self->autocode && + (self->mode == GS_MARKDOWN_MODE_PARA || + self->mode == GS_MARKDOWN_MODE_BULLETT)) { + temp = gs_markdown_word_auto_format_code (copy); + g_free (copy); + copy = temp; + } + + /* escape */ + if (self->escape) { + temp = g_markup_escape_text (copy, -1); + g_free (copy); + copy = temp; + } + + /* check words for URLS */ + if (self->autolinkify && + self->output == GS_MARKDOWN_OUTPUT_PANGO && + (self->mode == GS_MARKDOWN_MODE_PARA || + self->mode == GS_MARKDOWN_MODE_BULLETT)) { + temp = gs_markdown_word_auto_format_urls (copy); + g_free (copy); + copy = temp; + } + + /* do formatting */ + temp = gs_markdown_to_text_line_format (self, copy); + if (self->mode == GS_MARKDOWN_MODE_BULLETT) { + g_string_append_printf (self->processed, "%s%s%s\n", + self->tags.bullet_start, + temp, + self->tags.bullet_end); + self->line_count++; + } else if (self->mode == GS_MARKDOWN_MODE_H1) { + g_string_append_printf (self->processed, "%s%s%s\n", + self->tags.h1_start, + temp, + self->tags.h1_end); + } else if (self->mode == GS_MARKDOWN_MODE_H2) { + g_string_append_printf (self->processed, "%s%s%s\n", + self->tags.h2_start, + temp, + self->tags.h2_end); + } else if (self->mode == GS_MARKDOWN_MODE_PARA || + self->mode == GS_MARKDOWN_MODE_RULE) { + g_string_append_printf (self->processed, "%s\n", temp); + self->line_count++; + } + + /* clear */ + g_string_truncate (self->pending, 0); +} + +static gboolean +gs_markdown_to_text_line_process (GsMarkdown *self, const gchar *line) +{ + gboolean ret; + + /* blank */ + ret = gs_markdown_to_text_line_is_blank (line); + if (ret) { + gs_markdown_flush_pending (self); + /* a new line after a list is the end of list, not a gap */ + if (self->mode != GS_MARKDOWN_MODE_BULLETT) + ret = gs_markdown_add_pending (self, "\n"); + self->mode = GS_MARKDOWN_MODE_BLANK; + goto out; + } + + /* header1_type2 */ + ret = gs_markdown_to_text_line_is_header1_type2 (line); + if (ret) { + if (self->mode == GS_MARKDOWN_MODE_PARA) + self->mode = GS_MARKDOWN_MODE_H1; + goto out; + } + + /* header2_type2 */ + ret = gs_markdown_to_text_line_is_header2_type2 (line); + if (ret) { + if (self->mode == GS_MARKDOWN_MODE_PARA) + self->mode = GS_MARKDOWN_MODE_H2; + goto out; + } + + /* rule */ + ret = gs_markdown_to_text_line_is_rule (line); + if (ret) { + gs_markdown_flush_pending (self); + self->mode = GS_MARKDOWN_MODE_RULE; + ret = gs_markdown_add_pending (self, self->tags.rule); + goto out; + } + + /* bullet */ + ret = gs_markdown_to_text_line_is_bullet (line); + if (ret) { + gs_markdown_flush_pending (self); + self->mode = GS_MARKDOWN_MODE_BULLETT; + ret = gs_markdown_add_pending (self, &line[2]); + goto out; + } + + /* header1 */ + ret = gs_markdown_to_text_line_is_header1 (line); + if (ret) { + gs_markdown_flush_pending (self); + self->mode = GS_MARKDOWN_MODE_H1; + ret = gs_markdown_add_pending_header (self, &line[2]); + goto out; + } + + /* header2 */ + ret = gs_markdown_to_text_line_is_header2 (line); + if (ret) { + gs_markdown_flush_pending (self); + self->mode = GS_MARKDOWN_MODE_H2; + ret = gs_markdown_add_pending_header (self, &line[3]); + goto out; + } + + /* paragraph */ + if (self->mode == GS_MARKDOWN_MODE_BLANK || + self->mode == GS_MARKDOWN_MODE_UNKNOWN) { + gs_markdown_flush_pending (self); + self->mode = GS_MARKDOWN_MODE_PARA; + } + + /* add to pending */ + ret = gs_markdown_add_pending (self, line); +out: + /* if we failed to add, we don't know the mode */ + if (!ret) + self->mode = GS_MARKDOWN_MODE_UNKNOWN; + return ret; +} + +static void +gs_markdown_set_output_kind (GsMarkdown *self, GsMarkdownOutputKind output) +{ + g_return_if_fail (GS_IS_MARKDOWN (self)); + + self->output = output; + switch (output) { + case GS_MARKDOWN_OUTPUT_PANGO: + /* PangoMarkup */ + self->tags.em_start = "<i>"; + self->tags.em_end = "</i>"; + self->tags.strong_start = "<b>"; + self->tags.strong_end = "</b>"; + self->tags.code_start = "<tt>"; + self->tags.code_end = "</tt>"; + self->tags.h1_start = "<big>"; + self->tags.h1_end = "</big>"; + self->tags.h2_start = "<b>"; + self->tags.h2_end = "</b>"; + self->tags.bullet_start = "• "; + self->tags.bullet_end = ""; + self->tags.rule = "⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯\n"; + self->escape = TRUE; + self->autolinkify = TRUE; + break; + case GS_MARKDOWN_OUTPUT_HTML: + /* XHTML */ + self->tags.em_start = "<em>"; + self->tags.em_end = "<em>"; + self->tags.strong_start = "<strong>"; + self->tags.strong_end = "</strong>"; + self->tags.code_start = "<code>"; + self->tags.code_end = "</code>"; + self->tags.h1_start = "<h1>"; + self->tags.h1_end = "</h1>"; + self->tags.h2_start = "<h2>"; + self->tags.h2_end = "</h2>"; + self->tags.bullet_start = "<li>"; + self->tags.bullet_end = "</li>"; + self->tags.rule = "<hr>"; + self->escape = TRUE; + self->autolinkify = TRUE; + break; + case GS_MARKDOWN_OUTPUT_TEXT: + /* plain text */ + self->tags.em_start = ""; + self->tags.em_end = ""; + self->tags.strong_start = ""; + self->tags.strong_end = ""; + self->tags.code_start = ""; + self->tags.code_end = ""; + self->tags.h1_start = "["; + self->tags.h1_end = "]"; + self->tags.h2_start = "-"; + self->tags.h2_end = "-"; + self->tags.bullet_start = "* "; + self->tags.bullet_end = ""; + self->tags.rule = " ----- \n"; + self->escape = FALSE; + self->autolinkify = FALSE; + break; + default: + g_warning ("unknown output enum"); + break; + } +} + +void +gs_markdown_set_max_lines (GsMarkdown *self, gint max_lines) +{ + g_return_if_fail (GS_IS_MARKDOWN (self)); + self->max_lines = max_lines; +} + +void +gs_markdown_set_smart_quoting (GsMarkdown *self, gboolean smart_quoting) +{ + g_return_if_fail (GS_IS_MARKDOWN (self)); + self->smart_quoting = smart_quoting; +} + +void +gs_markdown_set_escape (GsMarkdown *self, gboolean escape) +{ + g_return_if_fail (GS_IS_MARKDOWN (self)); + self->escape = escape; +} + +void +gs_markdown_set_autocode (GsMarkdown *self, gboolean autocode) +{ + g_return_if_fail (GS_IS_MARKDOWN (self)); + self->autocode = autocode; +} + +void +gs_markdown_set_autolinkify (GsMarkdown *self, gboolean autolinkify) +{ + g_return_if_fail (GS_IS_MARKDOWN (self)); + self->autolinkify = autolinkify; +} + +gchar * +gs_markdown_parse (GsMarkdown *self, const gchar *markdown) +{ + gboolean ret; + gchar *temp; + guint i; + guint len; + g_auto(GStrv) lines = NULL; + + g_return_val_if_fail (GS_IS_MARKDOWN (self), NULL); + + /* process */ + self->mode = GS_MARKDOWN_MODE_UNKNOWN; + self->line_count = 0; + g_string_truncate (self->pending, 0); + g_string_truncate (self->processed, 0); + lines = g_strsplit (markdown, "\n", -1); + len = g_strv_length (lines); + + /* process each line */ + for (i = 0; i < len; i++) { + ret = gs_markdown_to_text_line_process (self, lines[i]); + if (!ret) + break; + } + gs_markdown_flush_pending (self); + + /* remove trailing \n */ + while (g_str_has_suffix (self->processed->str, "\n")) + g_string_set_size (self->processed, self->processed->len - 1); + + /* get a copy */ + temp = g_strdup (self->processed->str); + g_string_truncate (self->pending, 0); + g_string_truncate (self->processed, 0); + return temp; +} + +static void +gs_markdown_finalize (GObject *object) +{ + GsMarkdown *self; + + g_return_if_fail (GS_IS_MARKDOWN (object)); + + self = GS_MARKDOWN (object); + + g_string_free (self->pending, TRUE); + g_string_free (self->processed, TRUE); + + G_OBJECT_CLASS (gs_markdown_parent_class)->finalize (object); +} + +static void +gs_markdown_class_init (GsMarkdownClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + object_class->finalize = gs_markdown_finalize; +} + +static void +gs_markdown_init (GsMarkdown *self) +{ + self->mode = GS_MARKDOWN_MODE_UNKNOWN; + self->pending = g_string_new (""); + self->processed = g_string_new (""); + self->max_lines = -1; + self->smart_quoting = FALSE; + self->escape = FALSE; + self->autocode = FALSE; +} + +GsMarkdown * +gs_markdown_new (GsMarkdownOutputKind output) +{ + GsMarkdown *self; + self = g_object_new (GS_TYPE_MARKDOWN, NULL); + gs_markdown_set_output_kind (self, output); + return GS_MARKDOWN (self); +} diff --git a/plugins/packagekit/gs-markdown.h b/plugins/packagekit/gs-markdown.h new file mode 100644 index 0000000..51e6233 --- /dev/null +++ b/plugins/packagekit/gs-markdown.h @@ -0,0 +1,41 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2008-2013 Richard Hughes <richard@hughsie.com> + * Copyright (C) 2015 Kalev Lember <klember@redhat.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#pragma once + +#include <glib-object.h> + +G_BEGIN_DECLS + +#define GS_TYPE_MARKDOWN (gs_markdown_get_type ()) + +G_DECLARE_FINAL_TYPE (GsMarkdown, gs_markdown, GS, MARKDOWN, GObject) + +typedef enum { + GS_MARKDOWN_OUTPUT_TEXT, + GS_MARKDOWN_OUTPUT_PANGO, + GS_MARKDOWN_OUTPUT_HTML, + GS_MARKDOWN_OUTPUT_LAST +} GsMarkdownOutputKind; + +GsMarkdown *gs_markdown_new (GsMarkdownOutputKind output); +void gs_markdown_set_max_lines (GsMarkdown *self, + gint max_lines); +void gs_markdown_set_smart_quoting (GsMarkdown *self, + gboolean smart_quoting); +void gs_markdown_set_escape (GsMarkdown *self, + gboolean escape); +void gs_markdown_set_autocode (GsMarkdown *self, + gboolean autocode); +void gs_markdown_set_autolinkify (GsMarkdown *self, + gboolean autolinkify); +gchar *gs_markdown_parse (GsMarkdown *self, + const gchar *text); + +G_END_DECLS diff --git a/plugins/packagekit/gs-packagekit-helper.c b/plugins/packagekit/gs-packagekit-helper.c new file mode 100644 index 0000000..3afadcc --- /dev/null +++ b/plugins/packagekit/gs-packagekit-helper.c @@ -0,0 +1,131 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2016-2018 Richard Hughes <richard@hughsie.com> + * Copyright (C) 2019 Kalev Lember <klember@redhat.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include "config.h" + +#include <glib.h> + +#include "gs-packagekit-helper.h" +#include "packagekit-common.h" + +struct _GsPackagekitHelper { + GObject parent_instance; + GHashTable *apps; + GsApp *progress_app; + GsPlugin *plugin; +}; + +G_DEFINE_TYPE (GsPackagekitHelper, gs_packagekit_helper, G_TYPE_OBJECT) + +void +gs_packagekit_helper_cb (PkProgress *progress, PkProgressType type, gpointer user_data) +{ + GsPackagekitHelper *self = (GsPackagekitHelper *) user_data; + GsPlugin *plugin = gs_packagekit_helper_get_plugin (self); + const gchar *package_id = pk_progress_get_package_id (progress); + GsApp *app = NULL; + + /* optional */ + if (self->progress_app != NULL) + app = self->progress_app; + else if (package_id != NULL) + app = gs_packagekit_helper_get_app_by_id (self, package_id); + + if (type == PK_PROGRESS_TYPE_STATUS) { + PkStatusEnum status = pk_progress_get_status (progress); + GsPluginStatus plugin_status = packagekit_status_enum_to_plugin_status (status); + if (plugin_status != GS_PLUGIN_STATUS_UNKNOWN) + gs_plugin_status_update (plugin, app, plugin_status); + } else if (type == PK_PROGRESS_TYPE_PERCENTAGE) { + gint percentage = pk_progress_get_percentage (progress); + if (app != NULL && percentage >= 0 && percentage <= 100) + gs_app_set_progress (app, (guint) percentage); + } + + /* Only go from TRUE to FALSE - it doesn't make sense for a package + * install to become uncancellable later on */ + if (app != NULL && gs_app_get_allow_cancel (app)) + gs_app_set_allow_cancel (app, pk_progress_get_allow_cancel (progress)); +} + +void +gs_packagekit_helper_add_app (GsPackagekitHelper *self, GsApp *app) +{ + GPtrArray *source_ids = gs_app_get_source_ids (app); + + g_return_if_fail (GS_IS_PACKAGEKIT_HELPER (self)); + g_return_if_fail (GS_IS_APP (app)); + + for (guint i = 0; i < source_ids->len; i++) { + const gchar *source_id = g_ptr_array_index (source_ids, i); + g_hash_table_insert (self->apps, + g_strdup (source_id), + g_object_ref (app)); + } +} + +void +gs_packagekit_helper_set_progress_app (GsPackagekitHelper *self, GsApp *progress_app) +{ + g_set_object (&self->progress_app, progress_app); +} + +GsPlugin * +gs_packagekit_helper_get_plugin (GsPackagekitHelper *self) +{ + g_return_val_if_fail (GS_IS_PACKAGEKIT_HELPER (self), NULL); + return self->plugin; +} + +GsApp * +gs_packagekit_helper_get_app_by_id (GsPackagekitHelper *self, const gchar *package_id) +{ + g_return_val_if_fail (GS_IS_PACKAGEKIT_HELPER (self), NULL); + g_return_val_if_fail (package_id != NULL, NULL); + return g_hash_table_lookup (self->apps, package_id); +} + +static void +gs_packagekit_helper_finalize (GObject *object) +{ + GsPackagekitHelper *self; + + g_return_if_fail (GS_IS_PACKAGEKIT_HELPER (object)); + + self = GS_PACKAGEKIT_HELPER (object); + + g_object_unref (self->plugin); + g_clear_object (&self->progress_app); + g_hash_table_unref (self->apps); + + G_OBJECT_CLASS (gs_packagekit_helper_parent_class)->finalize (object); +} + +static void +gs_packagekit_helper_class_init (GsPackagekitHelperClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + object_class->finalize = gs_packagekit_helper_finalize; +} + +static void +gs_packagekit_helper_init (GsPackagekitHelper *self) +{ + self->apps = g_hash_table_new_full (g_str_hash, g_str_equal, + g_free, (GDestroyNotify) g_object_unref); +} + +GsPackagekitHelper * +gs_packagekit_helper_new (GsPlugin *plugin) +{ + GsPackagekitHelper *self; + self = g_object_new (GS_TYPE_PACKAGEKIT_HELPER, NULL); + self->plugin = g_object_ref (plugin); + return GS_PACKAGEKIT_HELPER (self); +} diff --git a/plugins/packagekit/gs-packagekit-helper.h b/plugins/packagekit/gs-packagekit-helper.h new file mode 100644 index 0000000..94a6ea1 --- /dev/null +++ b/plugins/packagekit/gs-packagekit-helper.h @@ -0,0 +1,35 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2016-2018 Richard Hughes <richard@hughsie.com> + * Copyright (C) 2019 Kalev Lember <klember@redhat.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#pragma once + +#include <glib-object.h> +#include <gnome-software.h> +#include <packagekit-glib2/packagekit.h> + +G_BEGIN_DECLS + +#define GS_TYPE_PACKAGEKIT_HELPER (gs_packagekit_helper_get_type ()) + +G_DECLARE_FINAL_TYPE (GsPackagekitHelper, gs_packagekit_helper, GS, PACKAGEKIT_HELPER, GObject) + +GsPackagekitHelper *gs_packagekit_helper_new (GsPlugin *plugin); +GsPlugin *gs_packagekit_helper_get_plugin (GsPackagekitHelper *self); +void gs_packagekit_helper_add_app (GsPackagekitHelper *self, + GsApp *app); +void gs_packagekit_helper_set_progress_app (GsPackagekitHelper *self, + GsApp *progress_app); +GsApp *gs_packagekit_helper_get_app_by_id (GsPackagekitHelper *self, + const gchar *package_id); +void gs_packagekit_helper_cb (PkProgress *progress, + PkProgressType type, + gpointer user_data); + + +G_END_DECLS diff --git a/plugins/packagekit/gs-plugin-packagekit-history.c b/plugins/packagekit/gs-plugin-packagekit-history.c new file mode 100644 index 0000000..6175316 --- /dev/null +++ b/plugins/packagekit/gs-plugin-packagekit-history.c @@ -0,0 +1,261 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2013 Richard Hughes <richard@hughsie.com> + * Copyright (C) 2015-2018 Kalev Lember <klember@redhat.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <config.h> + +#include <packagekit-glib2/packagekit.h> + +#include <gnome-software.h> + +#include "packagekit-common.h" + +#define GS_PLUGIN_PACKAGEKIT_HISTORY_TIMEOUT 5000 /* ms */ + +/* + * SECTION: + * This returns update history using the system PackageKit instance. + */ + +struct GsPluginData { + GDBusConnection *connection; +}; + +void +gs_plugin_initialize (GsPlugin *plugin) +{ + gs_plugin_alloc_data (plugin, sizeof(GsPluginData)); + + /* need pkgname */ + gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "appstream"); + gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "packagekit-refine"); +} + +void +gs_plugin_destroy (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + if (priv->connection != NULL) + g_object_unref (priv->connection); +} + +static void +gs_plugin_packagekit_refine_add_history (GsApp *app, GVariant *dict) +{ + const gchar *version; + gboolean ret; + guint64 timestamp; + PkInfoEnum info_enum; + g_autoptr(GsApp) history = NULL; + + /* create new history item with same ID as parent */ + history = gs_app_new (gs_app_get_id (app)); + gs_app_set_kind (history, AS_APP_KIND_GENERIC); + gs_app_set_bundle_kind (app, AS_BUNDLE_KIND_PACKAGE); + gs_app_set_name (history, GS_APP_QUALITY_NORMAL, gs_app_get_name (app)); + + /* get the installed state */ + ret = g_variant_lookup (dict, "info", "u", &info_enum); + g_assert (ret); + switch (info_enum) { + case PK_INFO_ENUM_INSTALLING: + gs_app_set_state (history, AS_APP_STATE_INSTALLED); + break; + case PK_INFO_ENUM_REMOVING: + gs_app_set_state (history, AS_APP_STATE_AVAILABLE); + break; + case PK_INFO_ENUM_UPDATING: + gs_app_set_state (history, AS_APP_STATE_UPDATABLE); + break; + default: + g_debug ("ignoring history kind: %s", + pk_info_enum_to_string (info_enum)); + return; + } + + /* set the history time and date */ + ret = g_variant_lookup (dict, "timestamp", "t", ×tamp); + g_assert (ret); + gs_app_set_install_date (history, timestamp); + + /* set the history version number */ + ret = g_variant_lookup (dict, "version", "&s", &version); + g_assert (ret); + gs_app_set_version (history, version); + + /* add the package to the main application */ + gs_app_add_history (app, history); + + /* use the last event as approximation of the package timestamp */ + gs_app_set_install_date (app, timestamp); +} + +gboolean +gs_plugin_setup (GsPlugin *plugin, GCancellable *cancellable, GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + priv->connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, + cancellable, + error); + return priv->connection != NULL; +} + +static gboolean +gs_plugin_packagekit_refine (GsPlugin *plugin, + GsAppList *list, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + gboolean ret; + guint j; + GsApp *app; + guint i = 0; + GVariantIter iter; + GVariant *value; + g_autofree const gchar **package_names = NULL; + g_autoptr(GError) error_local = NULL; + g_autoptr(GVariant) result = NULL; + g_autoptr(GVariant) tuple = NULL; + + /* get an array of package names */ + package_names = g_new0 (const gchar *, gs_app_list_length (list) + 1); + for (j = 0; j < gs_app_list_length (list); j++) { + app = gs_app_list_index (list, j); + package_names[i++] = gs_app_get_source_default (app); + } + + g_debug ("getting history for %u packages", gs_app_list_length (list)); + result = g_dbus_connection_call_sync (priv->connection, + "org.freedesktop.PackageKit", + "/org/freedesktop/PackageKit", + "org.freedesktop.PackageKit", + "GetPackageHistory", + g_variant_new ("(^asu)", package_names, 0), + NULL, + G_DBUS_CALL_FLAGS_NONE, + GS_PLUGIN_PACKAGEKIT_HISTORY_TIMEOUT, + cancellable, + &error_local); + if (result == NULL) { + if (g_error_matches (error_local, + G_DBUS_ERROR, + G_DBUS_ERROR_UNKNOWN_METHOD)) { + g_debug ("No history available as PackageKit is too old: %s", + error_local->message); + + /* just set this to something non-zero so we don't keep + * trying to call GetPackageHistory */ + for (i = 0; i < gs_app_list_length (list); i++) { + app = gs_app_list_index (list, i); + gs_app_set_install_date (app, GS_APP_INSTALL_DATE_UNKNOWN); + } + } else if (g_error_matches (error_local, + G_IO_ERROR, + G_IO_ERROR_CANCELLED)) { + g_set_error (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_CANCELLED, + "Failed to get history: %s", + error_local->message); + return FALSE; + } else if (g_error_matches (error_local, + G_IO_ERROR, + G_IO_ERROR_TIMED_OUT)) { + g_debug ("No history as PackageKit took too long: %s", + error_local->message); + for (i = 0; i < gs_app_list_length (list); i++) { + app = gs_app_list_index (list, i); + gs_app_set_install_date (app, GS_APP_INSTALL_DATE_UNKNOWN); + } + } + g_set_error (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_NOT_SUPPORTED, + "Failed to get history: %s", + error_local->message); + return FALSE; + } + + /* get any results */ + tuple = g_variant_get_child_value (result, 0); + for (i = 0; i < gs_app_list_length (list); i++) { + g_autoptr(GVariant) entries = NULL; + app = gs_app_list_index (list, i); + ret = g_variant_lookup (tuple, + gs_app_get_source_default (app), + "@aa{sv}", + &entries); + if (!ret) { + /* make up a fake entry as we know this package was at + * least installed at some point in time */ + if (gs_app_get_state (app) == AS_APP_STATE_INSTALLED) { + g_autoptr(GsApp) app_dummy = NULL; + app_dummy = gs_app_new (gs_app_get_id (app)); + gs_plugin_packagekit_set_packaging_format (plugin, app); + gs_app_set_metadata (app_dummy, "GnomeSoftware::Creator", + gs_plugin_get_name (plugin)); + gs_app_set_install_date (app_dummy, GS_APP_INSTALL_DATE_UNKNOWN); + gs_app_set_kind (app_dummy, AS_APP_KIND_GENERIC); + gs_app_set_state (app_dummy, AS_APP_STATE_INSTALLED); + gs_app_set_version (app_dummy, gs_app_get_version (app)); + gs_app_add_history (app, app_dummy); + } + gs_app_set_install_date (app, GS_APP_INSTALL_DATE_UNKNOWN); + continue; + } + + /* add history for application */ + g_variant_iter_init (&iter, entries); + while ((value = g_variant_iter_next_value (&iter))) { + gs_plugin_packagekit_refine_add_history (app, value); + g_variant_unref (value); + } + } + return TRUE; +} + +gboolean +gs_plugin_refine (GsPlugin *plugin, + GsAppList *list, + GsPluginRefineFlags flags, + GCancellable *cancellable, + GError **error) +{ + gboolean ret; + guint i; + GsApp *app; + GPtrArray *sources; + g_autoptr(GsAppList) packages = NULL; + + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_HISTORY) == 0) + return TRUE; + + /* add any missing history data */ + packages = gs_app_list_new (); + for (i = 0; i < gs_app_list_length (list); i++) { + app = gs_app_list_index (list, i); + if (g_strcmp0 (gs_app_get_management_plugin (app), "packagekit") != 0) + continue; + sources = gs_app_get_sources (app); + if (sources->len == 0) + continue; + if (gs_app_get_install_date (app) != 0) + continue; + gs_app_list_add (packages, app); + } + if (gs_app_list_length (packages) > 0) { + ret = gs_plugin_packagekit_refine (plugin, + packages, + cancellable, + error); + if (!ret) + return FALSE; + } + return TRUE; +} diff --git a/plugins/packagekit/gs-plugin-packagekit-local.c b/plugins/packagekit/gs-plugin-packagekit-local.c new file mode 100644 index 0000000..53b51ac --- /dev/null +++ b/plugins/packagekit/gs-plugin-packagekit-local.c @@ -0,0 +1,271 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2016 Richard Hughes <richard@hughsie.com> + * Copyright (C) 2018 Kalev Lember <klember@redhat.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <config.h> + +#include <string.h> + +#include <packagekit-glib2/packagekit.h> +#include <gnome-software.h> + +#include "packagekit-common.h" +#include "gs-packagekit-helper.h" + +struct GsPluginData { + PkTask *task; + GMutex task_mutex; +}; + +void +gs_plugin_initialize (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData)); + + g_mutex_init (&priv->task_mutex); + priv->task = pk_task_new (); + pk_client_set_background (PK_CLIENT (priv->task), FALSE); +} + +void +gs_plugin_destroy (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_mutex_clear (&priv->task_mutex); + g_object_unref (priv->task); +} + +static gboolean +gs_plugin_packagekit_refresh_guess_app_id (GsPlugin *plugin, + GsApp *app, + const gchar *filename, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_auto(GStrv) files = NULL; + g_autoptr(PkResults) results = NULL; + g_autoptr(GPtrArray) array = NULL; + g_autoptr(GString) basename_best = g_string_new (NULL); + + /* get file list so we can work out ID */ + files = g_strsplit (filename, "\t", -1); + gs_packagekit_helper_add_app (helper, app); + g_mutex_lock (&priv->task_mutex); + results = pk_client_get_files_local (PK_CLIENT (priv->task), + files, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + gs_utils_error_add_origin_id (error, app); + return FALSE; + } + array = pk_results_get_files_array (results); + if (array->len == 0) { + g_set_error (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_INVALID_FORMAT, + "no files for %s", filename); + return FALSE; + } + + /* find the smallest length desktop file, on the logic that + * ${app}.desktop is going to be better than ${app}-${action}.desktop */ + for (guint i = 0; i < array->len; i++) { + PkFiles *item = g_ptr_array_index (array, i); + gchar **fns = pk_files_get_files (item); + for (guint j = 0; fns[j] != NULL; j++) { + if (g_str_has_prefix (fns[j], "/etc/yum.repos.d/") && + g_str_has_suffix (fns[j], ".repo")) { + gs_app_add_quirk (app, GS_APP_QUIRK_HAS_SOURCE); + } + if (g_str_has_prefix (fns[j], "/usr/share/applications/") && + g_str_has_suffix (fns[j], ".desktop")) { + g_autofree gchar *basename = g_path_get_basename (fns[j]); + if (basename_best->len == 0 || + strlen (basename) < basename_best->len) + g_string_assign (basename_best, basename); + } + } + } + if (basename_best->len > 0) { + gs_app_set_kind (app, AS_APP_KIND_DESKTOP); + gs_app_set_id (app, basename_best->str); + } + + return TRUE; +} + +static void +add_quirks_from_package_name (GsApp *app, const gchar *package_name) +{ + /* these packages don't have a .repo file in their file lists, but + * instead install one through rpm scripts / cron job */ + const gchar *packages_with_repos[] = { + "google-chrome-stable", + "google-earth-pro-stable", + "google-talkplugin", + NULL }; + + if (g_strv_contains (packages_with_repos, package_name)) + gs_app_add_quirk (app, GS_APP_QUIRK_HAS_SOURCE); +} + +static gboolean +gs_plugin_packagekit_local_check_installed (GsPlugin *plugin, + GsApp *app, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + PkBitfield filter; + const gchar *names[] = { gs_app_get_source_default (app), NULL }; + g_autoptr(GPtrArray) packages = NULL; + g_autoptr(PkResults) results = NULL; + + filter = pk_bitfield_from_enums (PK_FILTER_ENUM_NEWEST, + PK_FILTER_ENUM_ARCH, + PK_FILTER_ENUM_INSTALLED, + -1); + results = pk_client_resolve (PK_CLIENT (priv->task), filter, (gchar **) names, + cancellable, NULL, NULL, error); + if (results == NULL) + return FALSE; + packages = pk_results_get_package_array (results); + if (packages->len > 0) { + gs_app_set_state (app, AS_APP_STATE_UNKNOWN); + gs_app_set_state (app, AS_APP_STATE_INSTALLED); + for (guint i = 0; i < packages->len; i++){ + PkPackage *pkg = g_ptr_array_index (packages, i); + gs_app_add_source_id (app, pk_package_get_id (pkg)); + } + } + return TRUE; +} + +gboolean +gs_plugin_file_to_app (GsPlugin *plugin, + GsAppList *list, + GFile *file, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + const gchar *package_id; + PkDetails *item; + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autoptr(PkResults) results = NULL; + g_autofree gchar *content_type = NULL; + g_autofree gchar *filename = NULL; + g_autofree gchar *license_spdx = NULL; + g_auto(GStrv) files = NULL; + g_auto(GStrv) split = NULL; + g_autoptr(GPtrArray) array = NULL; + g_autoptr(GsApp) app = NULL; + const gchar *mimetypes[] = { + "application/x-app-package", + "application/x-deb", + "application/vnd.debian.binary-package", + "application/x-redhat-package-manager", + "application/x-rpm", + NULL }; + + /* does this match any of the mimetypes we support */ + content_type = gs_utils_get_content_type (file, cancellable, error); + if (content_type == NULL) + return FALSE; + if (!g_strv_contains (mimetypes, content_type)) + return TRUE; + + /* get details */ + filename = g_file_get_path (file); + files = g_strsplit (filename, "\t", -1); + g_mutex_lock (&priv->task_mutex); + pk_client_set_cache_age (PK_CLIENT (priv->task), G_MAXUINT); + results = pk_client_get_details_local (PK_CLIENT (priv->task), + files, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) + return FALSE; + + /* get results */ + array = pk_results_get_details_array (results); + if (array->len == 0) { + g_set_error (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_INVALID_FORMAT, + "no details for %s", filename); + return FALSE; + } + if (array->len > 1) { + g_set_error (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_INVALID_FORMAT, + "too many details [%u] for %s", + array->len, filename); + return FALSE; + } + + /* create application */ + item = g_ptr_array_index (array, 0); + app = gs_app_new (NULL); + gs_plugin_packagekit_set_packaging_format (plugin, app); + gs_app_set_metadata (app, "GnomeSoftware::Creator", + gs_plugin_get_name (plugin)); + package_id = pk_details_get_package_id (item); + split = pk_package_id_split (package_id); + if (split == NULL) { + g_set_error (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_INVALID_FORMAT, + "invalid package-id: %s", package_id); + return FALSE; + } + gs_app_set_management_plugin (app, "packagekit"); + gs_app_set_kind (app, AS_APP_KIND_GENERIC); + gs_app_set_bundle_kind (app, AS_BUNDLE_KIND_PACKAGE); + gs_app_set_state (app, AS_APP_STATE_AVAILABLE_LOCAL); + gs_app_set_name (app, GS_APP_QUALITY_LOWEST, split[PK_PACKAGE_ID_NAME]); + gs_app_set_summary (app, GS_APP_QUALITY_LOWEST, + pk_details_get_summary (item)); + gs_app_set_version (app, split[PK_PACKAGE_ID_VERSION]); + gs_app_add_source (app, split[PK_PACKAGE_ID_NAME]); + gs_app_add_source_id (app, package_id); + gs_app_set_description (app, GS_APP_QUALITY_LOWEST, + pk_details_get_description (item)); + gs_app_set_url (app, AS_URL_KIND_HOMEPAGE, pk_details_get_url (item)); + gs_app_set_size_installed (app, pk_details_get_size (item)); + gs_app_set_size_download (app, 0); + license_spdx = as_utils_license_to_spdx (pk_details_get_license (item)); + gs_app_set_license (app, GS_APP_QUALITY_LOWEST, license_spdx); + add_quirks_from_package_name (app, split[PK_PACKAGE_ID_NAME]); + + /* is already installed? */ + if (!gs_plugin_packagekit_local_check_installed (plugin, + app, + cancellable, + error)) + return FALSE; + + /* look for a desktop file so we can use a valid application id */ + if (!gs_plugin_packagekit_refresh_guess_app_id (plugin, + app, + filename, + cancellable, + error)) + return FALSE; + + gs_app_list_add (list, app); + return TRUE; +} diff --git a/plugins/packagekit/gs-plugin-packagekit-offline.c b/plugins/packagekit/gs-plugin-packagekit-offline.c new file mode 100644 index 0000000..7e15720 --- /dev/null +++ b/plugins/packagekit/gs-plugin-packagekit-offline.c @@ -0,0 +1,181 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2013 Richard Hughes <richard@hughsie.com> + * Copyright (C) 2015-2017 Kalev Lember <klember@redhat.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <config.h> + +#include <packagekit-glib2/packagekit.h> + +#include <gnome-software.h> + +#include "packagekit-common.h" + +/* + * SECTION: + * This adds historical updates to the application history. + * + * Note: when this is cleared by one user is is unavailable for all + * other users. + */ + +static gboolean +gs_plugin_packagekit_convert_error (GError **error, + PkErrorEnum error_enum, + const gchar *details) +{ + switch (error_enum) { + case PK_ERROR_ENUM_PACKAGE_DOWNLOAD_FAILED: + case PK_ERROR_ENUM_NO_CACHE: + case PK_ERROR_ENUM_NO_NETWORK: + case PK_ERROR_ENUM_NO_MORE_MIRRORS_TO_TRY: + case PK_ERROR_ENUM_CANNOT_FETCH_SOURCES: + case PK_ERROR_ENUM_UNFINISHED_TRANSACTION: + g_set_error_literal (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_NO_NETWORK, + details); + break; + case PK_ERROR_ENUM_BAD_GPG_SIGNATURE: + case PK_ERROR_ENUM_CANNOT_UPDATE_REPO_UNSIGNED: + case PK_ERROR_ENUM_GPG_FAILURE: + case PK_ERROR_ENUM_MISSING_GPG_SIGNATURE: + case PK_ERROR_ENUM_PACKAGE_CORRUPT: + g_set_error_literal (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_NO_SECURITY, + details); + break; + case PK_ERROR_ENUM_TRANSACTION_CANCELLED: + g_set_error_literal (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_CANCELLED, + details); + break; + case PK_ERROR_ENUM_NO_PACKAGES_TO_UPDATE: + case PK_ERROR_ENUM_UPDATE_NOT_FOUND: + g_set_error_literal (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_NOT_SUPPORTED, + details); + break; + case PK_ERROR_ENUM_NO_SPACE_ON_DEVICE: + g_set_error_literal (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_NO_SPACE, + details); + break; + default: + g_set_error_literal (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_FAILED, + details); + break; + } + return FALSE; +} + +gboolean +gs_plugin_add_updates_historical (GsPlugin *plugin, + GsAppList *list, + GCancellable *cancellable, + GError **error) +{ + guint64 mtime; + guint i; + g_autoptr(GPtrArray) package_array = NULL; + g_autoptr(GError) error_local = NULL; + g_autoptr(PkResults) results = NULL; + PkExitEnum exit_code; + + /* get the results */ + results = pk_offline_get_results (&error_local); + if (results == NULL) { + /* was any offline update attempted */ + if (g_error_matches (error_local, + PK_OFFLINE_ERROR, + PK_OFFLINE_ERROR_NO_DATA)) { + return TRUE; + } + + g_set_error (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_INVALID_FORMAT, + "Failed to get offline update results: %s", + error_local->message); + return FALSE; + } + + /* get the mtime of the results */ + mtime = pk_offline_get_results_mtime (error); + if (mtime == 0) + return FALSE; + + /* only return results if successful */ + exit_code = pk_results_get_exit_code (results); + if (exit_code != PK_EXIT_ENUM_SUCCESS) { + g_autoptr(PkError) error_code = NULL; + + error_code = pk_results_get_error_code (results); + if (error_code == NULL) { + g_set_error (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_FAILED, + "Offline update failed without error_code set"); + return FALSE; + } + + return gs_plugin_packagekit_convert_error (error, + pk_error_get_code (error_code), + pk_error_get_details (error_code)); + } + + /* distro upgrade? */ + if (pk_results_get_role (results) == PK_ROLE_ENUM_UPGRADE_SYSTEM) { + g_autoptr(GsApp) app = NULL; + + app = gs_app_new (NULL); + gs_app_set_from_unique_id (app, "*/*/*/*/system/*"); + gs_app_set_management_plugin (app, "packagekit"); + gs_app_add_quirk (app, GS_APP_QUIRK_IS_WILDCARD); + gs_app_set_state (app, AS_APP_STATE_UNKNOWN); + gs_app_set_kind (app, AS_APP_KIND_OS_UPGRADE); + gs_app_set_bundle_kind (app, AS_BUNDLE_KIND_PACKAGE); + gs_app_set_install_date (app, mtime); + gs_app_set_metadata (app, "GnomeSoftware::Creator", + gs_plugin_get_name (plugin)); + gs_app_list_add (list, app); + + return TRUE; + } + + /* get list of package-ids */ + package_array = pk_results_get_package_array (results); + for (i = 0; i < package_array->len; i++) { + PkPackage *pkg = g_ptr_array_index (package_array, i); + const gchar *package_id; + g_autoptr(GsApp) app = NULL; + g_auto(GStrv) split = NULL; + + app = gs_app_new (NULL); + package_id = pk_package_get_id (pkg); + split = g_strsplit (package_id, ";", 4); + gs_plugin_packagekit_set_packaging_format (plugin, app); + gs_app_add_source (app, split[0]); + gs_app_set_update_version (app, split[1]); + gs_app_set_management_plugin (app, "packagekit"); + gs_app_add_source_id (app, package_id); + gs_app_set_state (app, AS_APP_STATE_UPDATABLE); + gs_app_set_kind (app, AS_APP_KIND_GENERIC); + gs_app_set_bundle_kind (app, AS_BUNDLE_KIND_PACKAGE); + gs_app_set_install_date (app, mtime); + gs_app_set_metadata (app, "GnomeSoftware::Creator", + gs_plugin_get_name (plugin)); + gs_app_list_add (list, app); + } + return TRUE; +} diff --git a/plugins/packagekit/gs-plugin-packagekit-proxy.c b/plugins/packagekit/gs-plugin-packagekit-proxy.c new file mode 100644 index 0000000..a5bbcc7 --- /dev/null +++ b/plugins/packagekit/gs-plugin-packagekit-proxy.c @@ -0,0 +1,316 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2013-2016 Richard Hughes <richard@hughsie.com> + * Copyright (C) 2013 Matthias Clasen <mclasen@redhat.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <config.h> + +#include <packagekit-glib2/packagekit.h> +#include <string.h> +#include <gsettings-desktop-schemas/gdesktop-enums.h> +#include <gnome-software.h> + +/* + * SECTION: + * Sets the session proxy on the system PackageKit instance + */ + +struct GsPluginData { + PkControl *control; + GSettings *settings; + GSettings *settings_http; + GSettings *settings_https; + GSettings *settings_ftp; + GSettings *settings_socks; +}; + +static gchar * +get_proxy_http (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + gboolean ret; + GString *string = NULL; + gint port; + GDesktopProxyMode proxy_mode; + g_autofree gchar *host = NULL; + g_autofree gchar *password = NULL; + g_autofree gchar *username = NULL; + + proxy_mode = g_settings_get_enum (priv->settings, "mode"); + if (proxy_mode != G_DESKTOP_PROXY_MODE_MANUAL) + return NULL; + + host = g_settings_get_string (priv->settings_http, "host"); + if (host == NULL) + return NULL; + + port = g_settings_get_int (priv->settings_http, "port"); + + ret = g_settings_get_boolean (priv->settings_http, + "use-authentication"); + if (ret) { + username = g_settings_get_string (priv->settings_http, + "authentication-user"); + password = g_settings_get_string (priv->settings_http, + "authentication-password"); + } + + /* make PackageKit proxy string */ + string = g_string_new (""); + if (username != NULL || password != NULL) { + if (username != NULL) + g_string_append_printf (string, "%s", username); + if (password != NULL) + g_string_append_printf (string, ":%s", password); + g_string_append (string, "@"); + } + g_string_append (string, host); + if (port > 0) + g_string_append_printf (string, ":%i", port); + return g_string_free (string, FALSE); +} + +static gchar * +get_proxy_https (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + GString *string = NULL; + gint port; + GDesktopProxyMode proxy_mode; + g_autofree gchar *host = NULL; + + proxy_mode = g_settings_get_enum (priv->settings, "mode"); + if (proxy_mode != G_DESKTOP_PROXY_MODE_MANUAL) + return NULL; + + host = g_settings_get_string (priv->settings_https, "host"); + if (host == NULL) + return NULL; + port = g_settings_get_int (priv->settings_https, "port"); + if (port == 0) + return NULL; + + /* make PackageKit proxy string */ + string = g_string_new (host); + if (port > 0) + g_string_append_printf (string, ":%i", port); + return g_string_free (string, FALSE); +} + +static gchar * +get_proxy_ftp (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + GString *string = NULL; + gint port; + GDesktopProxyMode proxy_mode; + g_autofree gchar *host = NULL; + + proxy_mode = g_settings_get_enum (priv->settings, "mode"); + if (proxy_mode != G_DESKTOP_PROXY_MODE_MANUAL) + return NULL; + + host = g_settings_get_string (priv->settings_ftp, "host"); + if (host == NULL) + return NULL; + port = g_settings_get_int (priv->settings_ftp, "port"); + if (port == 0) + return NULL; + + /* make PackageKit proxy string */ + string = g_string_new (host); + if (port > 0) + g_string_append_printf (string, ":%i", port); + return g_string_free (string, FALSE); +} + +static gchar * +get_proxy_socks (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + GString *string = NULL; + gint port; + GDesktopProxyMode proxy_mode; + g_autofree gchar *host = NULL; + + proxy_mode = g_settings_get_enum (priv->settings, "mode"); + if (proxy_mode != G_DESKTOP_PROXY_MODE_MANUAL) + return NULL; + + host = g_settings_get_string (priv->settings_socks, "host"); + if (host == NULL) + return NULL; + port = g_settings_get_int (priv->settings_socks, "port"); + if (port == 0) + return NULL; + + /* make PackageKit proxy string */ + string = g_string_new (host); + if (port > 0) + g_string_append_printf (string, ":%i", port); + return g_string_free (string, FALSE); +} + +static gchar * +get_no_proxy (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + GString *string = NULL; + GDesktopProxyMode proxy_mode; + g_autofree gchar **hosts = NULL; + guint i; + + proxy_mode = g_settings_get_enum (priv->settings, "mode"); + if (proxy_mode != G_DESKTOP_PROXY_MODE_MANUAL) + return NULL; + + hosts = g_settings_get_strv (priv->settings, "ignore-hosts"); + if (hosts == NULL) + return NULL; + + /* make PackageKit proxy string */ + string = g_string_new (""); + for (i = 0; hosts[i] != NULL; i++) { + if (i == 0) + g_string_assign (string, hosts[i]); + else + g_string_append_printf (string, ",%s", hosts[i]); + g_free (hosts[i]); + } + + return g_string_free (string, FALSE); +} + +static gchar * +get_pac (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + GDesktopProxyMode proxy_mode; + gchar *url = NULL; + + proxy_mode = g_settings_get_enum (priv->settings, "mode"); + if (proxy_mode != G_DESKTOP_PROXY_MODE_AUTO) + return NULL; + + url = g_settings_get_string (priv->settings, "autoconfig-url"); + if (url == NULL) + return NULL; + + return url; +} + +static void +set_proxy_cb (GObject *object, GAsyncResult *res, gpointer user_data) +{ + g_autoptr(GError) error = NULL; + if (!pk_control_set_proxy_finish (PK_CONTROL (object), res, &error)) { + if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning ("failed to set proxies: %s", error->message); + } +} + +static void +reload_proxy_settings (GsPlugin *plugin, GCancellable *cancellable) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_autofree gchar *proxy_http = NULL; + g_autofree gchar *proxy_https = NULL; + g_autofree gchar *proxy_ftp = NULL; + g_autofree gchar *proxy_socks = NULL; + g_autofree gchar *no_proxy = NULL; + g_autofree gchar *pac = NULL; + g_autoptr(GError) error = NULL; + g_autoptr(GPermission) permission = NULL; + + /* only if we can achieve the action *without* an auth dialog */ + permission = gs_utils_get_permission ("org.freedesktop.packagekit." + "system-network-proxy-configure", + cancellable, &error); + if (permission == NULL) { + g_debug ("not setting proxy as no permission: %s", error->message); + return; + } + if (!g_permission_get_allowed (permission)) { + g_debug ("not setting proxy as no auth requested"); + return; + } + + proxy_http = get_proxy_http (plugin); + proxy_https = get_proxy_https (plugin); + proxy_ftp = get_proxy_ftp (plugin); + proxy_socks = get_proxy_socks (plugin); + no_proxy = get_no_proxy (plugin); + pac = get_pac (plugin); + + g_debug ("Setting proxies (http: %s, https: %s, ftp: %s, socks: %s, " + "no_proxy: %s, pac: %s)", + proxy_http, proxy_https, proxy_ftp, proxy_socks, + no_proxy, pac); + + pk_control_set_proxy2_async (priv->control, + proxy_http, + proxy_https, + proxy_ftp, + proxy_socks, + no_proxy, + pac, + cancellable, + set_proxy_cb, + plugin); +} + +static void +gs_plugin_packagekit_proxy_changed_cb (GSettings *settings, + const gchar *key, + GsPlugin *plugin) +{ + if (!gs_plugin_get_enabled (plugin)) + return; + reload_proxy_settings (plugin, NULL); +} + +void +gs_plugin_initialize (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData)); + priv->control = pk_control_new (); + priv->settings = g_settings_new ("org.gnome.system.proxy"); + g_signal_connect (priv->settings, "changed", + G_CALLBACK (gs_plugin_packagekit_proxy_changed_cb), plugin); + + priv->settings_http = g_settings_new ("org.gnome.system.proxy.http"); + priv->settings_https = g_settings_new ("org.gnome.system.proxy.https"); + priv->settings_ftp = g_settings_new ("org.gnome.system.proxy.ftp"); + priv->settings_socks = g_settings_new ("org.gnome.system.proxy.socks"); + g_signal_connect (priv->settings_http, "changed", + G_CALLBACK (gs_plugin_packagekit_proxy_changed_cb), plugin); + g_signal_connect (priv->settings_https, "changed", + G_CALLBACK (gs_plugin_packagekit_proxy_changed_cb), plugin); + g_signal_connect (priv->settings_ftp, "changed", + G_CALLBACK (gs_plugin_packagekit_proxy_changed_cb), plugin); + g_signal_connect (priv->settings_socks, "changed", + G_CALLBACK (gs_plugin_packagekit_proxy_changed_cb), plugin); +} + +gboolean +gs_plugin_setup (GsPlugin *plugin, GCancellable *cancellable, GError **error) +{ + reload_proxy_settings (plugin, cancellable); + return TRUE; +} + +void +gs_plugin_destroy (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_object_unref (priv->control); + g_object_unref (priv->settings); + g_object_unref (priv->settings_http); + g_object_unref (priv->settings_https); + g_object_unref (priv->settings_ftp); + g_object_unref (priv->settings_socks); +} diff --git a/plugins/packagekit/gs-plugin-packagekit-refine-repos.c b/plugins/packagekit/gs-plugin-packagekit-refine-repos.c new file mode 100644 index 0000000..4b41c44 --- /dev/null +++ b/plugins/packagekit/gs-plugin-packagekit-refine-repos.c @@ -0,0 +1,123 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2018 Kalev Lember <klember@redhat.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <config.h> + +#include <packagekit-glib2/packagekit.h> +#include <gnome-software.h> + +#include "gs-packagekit-helper.h" +#include "packagekit-common.h" + +/* + * SECTION: + * Uses the system PackageKit instance to return convert repo filenames to + * package-ids. + * + * Requires: | [repos::repo-filename] + * Refines: | [source-id] + */ + +struct GsPluginData { + PkClient *client; + GMutex client_mutex; +}; + +void +gs_plugin_initialize (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData)); + + g_mutex_init (&priv->client_mutex); + priv->client = pk_client_new (); + pk_client_set_background (priv->client, FALSE); + pk_client_set_cache_age (priv->client, G_MAXUINT); + + /* need repos::repo-filename */ + gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "repos"); +} + +void +gs_plugin_destroy (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_mutex_clear (&priv->client_mutex); + g_object_unref (priv->client); +} + +static gboolean +gs_plugin_packagekit_refine_repo_from_filename (GsPlugin *plugin, + GsApp *app, + const gchar *filename, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + const gchar *to_array[] = { NULL, NULL }; + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autoptr(PkResults) results = NULL; + g_autoptr(GPtrArray) packages = NULL; + + to_array[0] = filename; + gs_packagekit_helper_add_app (helper, app); + g_mutex_lock (&priv->client_mutex); + results = pk_client_search_files (priv->client, + pk_bitfield_from_enums (PK_FILTER_ENUM_INSTALLED, -1), + (gchar **) to_array, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->client_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + g_prefix_error (error, "failed to search file %s: ", filename); + return FALSE; + } + + /* get results */ + packages = pk_results_get_package_array (results); + if (packages->len == 1) { + PkPackage *package = g_ptr_array_index (packages, 0); + gs_app_add_source_id (app, pk_package_get_id (package)); + } else { + g_debug ("failed to find one package for repo %s, %s, [%u]", + gs_app_get_id (app), filename, packages->len); + } + return TRUE; +} + +gboolean +gs_plugin_refine (GsPlugin *plugin, + GsAppList *list, + GsPluginRefineFlags flags, + GCancellable *cancellable, + GError **error) +{ + for (guint i = 0; i < gs_app_list_length (list); i++) { + GsApp *app = gs_app_list_index (list, i); + const gchar *fn; + if (gs_app_has_quirk (app, GS_APP_QUIRK_IS_WILDCARD)) + continue; + if (gs_app_get_kind (app) != AS_APP_KIND_SOURCE) + continue; + if (g_strcmp0 (gs_app_get_management_plugin (app), "packagekit") != 0) + continue; + fn = gs_app_get_metadata_item (app, "repos::repo-filename"); + if (fn == NULL) + continue; + /* set the source package name for an installed .repo file */ + if (!gs_plugin_packagekit_refine_repo_from_filename (plugin, + app, + fn, + cancellable, + error)) + return FALSE; + } + + /* success */ + return TRUE; +} diff --git a/plugins/packagekit/gs-plugin-packagekit-refine.c b/plugins/packagekit/gs-plugin-packagekit-refine.c new file mode 100644 index 0000000..68f7eb6 --- /dev/null +++ b/plugins/packagekit/gs-plugin-packagekit-refine.c @@ -0,0 +1,822 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2013-2016 Richard Hughes <richard@hughsie.com> + * Copyright (C) 2015-2018 Kalev Lember <klember@redhat.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <config.h> + +#include <packagekit-glib2/packagekit.h> +#include <gnome-software.h> + +#include "gs-markdown.h" +#include "gs-packagekit-helper.h" +#include "packagekit-common.h" + +/* + * SECTION: + * Uses the system PackageKit instance to return convert filenames to + * package-ids and to also discover update details about a package. + * + * Requires: | [id] + * Refines: | [source-id], [installed] + */ + +struct GsPluginData { + PkControl *control; + PkClient *client; + GMutex client_mutex; +}; + +static void +gs_plugin_packagekit_updates_changed_cb (PkControl *control, GsPlugin *plugin) +{ + gs_plugin_updates_changed (plugin); +} + +static void +gs_plugin_packagekit_repo_list_changed_cb (PkControl *control, GsPlugin *plugin) +{ + gs_plugin_reload (plugin); +} + +void +gs_plugin_initialize (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData)); + + g_mutex_init (&priv->client_mutex); + priv->client = pk_client_new (); + priv->control = pk_control_new (); + g_signal_connect (priv->control, "updates-changed", + G_CALLBACK (gs_plugin_packagekit_updates_changed_cb), plugin); + g_signal_connect (priv->control, "repo-list-changed", + G_CALLBACK (gs_plugin_packagekit_repo_list_changed_cb), plugin); + pk_client_set_background (priv->client, FALSE); + pk_client_set_cache_age (priv->client, G_MAXUINT); + + /* need pkgname and ID */ + gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "appstream"); + gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "packagekit"); +} + +void +gs_plugin_destroy (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_mutex_clear (&priv->client_mutex); + g_object_unref (priv->client); + g_object_unref (priv->control); +} + +void +gs_plugin_adopt_app (GsPlugin *plugin, GsApp *app) +{ + if (gs_app_get_bundle_kind (app) == AS_BUNDLE_KIND_PACKAGE && + gs_app_get_scope (app) == AS_APP_SCOPE_SYSTEM) { + gs_app_set_management_plugin (app, "packagekit"); + gs_plugin_packagekit_set_packaging_format (plugin, app); + return; + } +} + +static gboolean +gs_plugin_packagekit_resolve_packages_with_filter (GsPlugin *plugin, + GsAppList *list, + PkBitfield filter, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + GPtrArray *sources; + GsApp *app; + const gchar *pkgname; + guint i; + guint j; + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autoptr(PkResults) results = NULL; + g_autoptr(GPtrArray) package_ids = NULL; + g_autoptr(GPtrArray) packages = NULL; + + package_ids = g_ptr_array_new_with_free_func (g_free); + for (i = 0; i < gs_app_list_length (list); i++) { + app = gs_app_list_index (list, i); + sources = gs_app_get_sources (app); + for (j = 0; j < sources->len; j++) { + pkgname = g_ptr_array_index (sources, j); + if (pkgname == NULL || pkgname[0] == '\0') { + g_warning ("invalid pkgname '%s' for %s", + pkgname, + gs_app_get_unique_id (app)); + continue; + } + g_ptr_array_add (package_ids, g_strdup (pkgname)); + } + } + if (package_ids->len == 0) + return TRUE; + g_ptr_array_add (package_ids, NULL); + + /* resolve them all at once */ + g_mutex_lock (&priv->client_mutex); + results = pk_client_resolve (priv->client, + filter, + (gchar **) package_ids->pdata, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->client_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + g_prefix_error (error, "failed to resolve package_ids: "); + return FALSE; + } + + /* get results */ + packages = pk_results_get_package_array (results); + + /* if the user types more characters we'll get cancelled - don't go on + * to mark apps as unavailable because packages->len = 0 */ + if (g_cancellable_set_error_if_cancelled (cancellable, error)) { + gs_utils_error_convert_gio (error); + return FALSE; + } + + for (i = 0; i < gs_app_list_length (list); i++) { + app = gs_app_list_index (list, i); + if (gs_app_get_local_file (app) != NULL) + continue; + gs_plugin_packagekit_resolve_packages_app (plugin, packages, app); + } + return TRUE; +} + +static gboolean +gs_plugin_packagekit_resolve_packages (GsPlugin *plugin, + GsAppList *list, + GCancellable *cancellable, + GError **error) +{ + PkBitfield filter; + g_autoptr(GsAppList) resolve2_list = NULL; + + /* first, try to resolve packages with ARCH filter */ + filter = pk_bitfield_from_enums (PK_FILTER_ENUM_NEWEST, + PK_FILTER_ENUM_ARCH, + -1); + if (!gs_plugin_packagekit_resolve_packages_with_filter (plugin, + list, + filter, + cancellable, + error)) { + return FALSE; + } + + /* if any packages remaining in UNKNOWN state, try to resolve them again, + * but this time without ARCH filter */ + resolve2_list = gs_app_list_new (); + for (guint i = 0; i < gs_app_list_length (list); i++) { + GsApp *app = gs_app_list_index (list, i); + if (gs_app_get_state (app) == AS_APP_STATE_UNKNOWN) + gs_app_list_add (resolve2_list, app); + } + filter = pk_bitfield_from_enums (PK_FILTER_ENUM_NEWEST, + PK_FILTER_ENUM_NOT_ARCH, + PK_FILTER_ENUM_NOT_SOURCE, + -1); + if (!gs_plugin_packagekit_resolve_packages_with_filter (plugin, + resolve2_list, + filter, + cancellable, + error)) { + return FALSE; + } + + return TRUE; +} + +static gboolean +gs_plugin_packagekit_refine_from_desktop (GsPlugin *plugin, + GsApp *app, + const gchar *filename, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + const gchar *to_array[] = { NULL, NULL }; + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autoptr(PkResults) results = NULL; + g_autoptr(GPtrArray) packages = NULL; + + to_array[0] = filename; + gs_packagekit_helper_add_app (helper, app); + g_mutex_lock (&priv->client_mutex); + results = pk_client_search_files (priv->client, + pk_bitfield_from_enums (PK_FILTER_ENUM_INSTALLED, -1), + (gchar **) to_array, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->client_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + g_prefix_error (error, "failed to search file %s: ", filename); + return FALSE; + } + + /* get results */ + packages = pk_results_get_package_array (results); + if (packages->len == 1) { + PkPackage *package; + package = g_ptr_array_index (packages, 0); + gs_plugin_packagekit_set_metadata_from_package (plugin, app, package); + } else { + g_warning ("Failed to find one package for %s, %s, [%u]", + gs_app_get_id (app), filename, packages->len); + } + return TRUE; +} + +/* + * gs_plugin_packagekit_fixup_update_description: + * + * Lets assume Fedora is sending us valid markdown, but fall back to + * plain text if this fails. + */ +static gchar * +gs_plugin_packagekit_fixup_update_description (const gchar *text) +{ + gchar *tmp; + g_autoptr(GsMarkdown) markdown = NULL; + + /* nothing to do */ + if (text == NULL) + return NULL; + + /* try to parse */ + markdown = gs_markdown_new (GS_MARKDOWN_OUTPUT_TEXT); + gs_markdown_set_smart_quoting (markdown, FALSE); + gs_markdown_set_autocode (markdown, FALSE); + gs_markdown_set_autolinkify (markdown, FALSE); + tmp = gs_markdown_parse (markdown, text); + if (tmp != NULL) + return tmp; + return g_strdup (text); +} + +static gboolean +gs_plugin_packagekit_refine_updatedetails (GsPlugin *plugin, + GsAppList *list, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + const gchar *package_id; + guint j; + GsApp *app; + guint cnt = 0; + PkUpdateDetail *update_detail; + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autofree const gchar **package_ids = NULL; + g_autoptr(PkResults) results = NULL; + g_autoptr(GPtrArray) array = NULL; + + package_ids = g_new0 (const gchar *, gs_app_list_length (list) + 1); + for (guint i = 0; i < gs_app_list_length (list); i++) { + app = gs_app_list_index (list, i); + package_id = gs_app_get_source_id_default (app); + if (package_id != NULL) + package_ids[cnt++] = package_id; + } + + /* nothing to do */ + if (cnt == 0) + return TRUE; + + /* get any update details */ + g_mutex_lock (&priv->client_mutex); + results = pk_client_get_update_detail (priv->client, + (gchar **) package_ids, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->client_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + g_prefix_error (error, "failed to get update details for %s: ", + package_ids[0]); + return FALSE; + } + + /* set the update details for the update */ + array = pk_results_get_update_detail_array (results); + for (j = 0; j < gs_app_list_length (list); j++) { + app = gs_app_list_index (list, j); + package_id = gs_app_get_source_id_default (app); + for (guint i = 0; i < array->len; i++) { + const gchar *tmp; + g_autofree gchar *desc = NULL; + /* right package? */ + update_detail = g_ptr_array_index (array, i); + if (g_strcmp0 (package_id, pk_update_detail_get_package_id (update_detail)) != 0) + continue; + tmp = pk_update_detail_get_update_text (update_detail); + desc = gs_plugin_packagekit_fixup_update_description (tmp); + if (desc != NULL) + gs_app_set_update_details (app, desc); + break; + } + } + return TRUE; +} + +static gboolean +gs_plugin_packagekit_refine_details2 (GsPlugin *plugin, + GsAppList *list, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + GPtrArray *source_ids; + GsApp *app; + const gchar *package_id; + guint i, j; + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autoptr(GPtrArray) array = NULL; + g_autoptr(GPtrArray) package_ids = NULL; + g_autoptr(PkResults) results = NULL; + g_autoptr(GHashTable) details_collection = NULL; + + package_ids = g_ptr_array_new_with_free_func (g_free); + for (i = 0; i < gs_app_list_length (list); i++) { + app = gs_app_list_index (list, i); + source_ids = gs_app_get_source_ids (app); + for (j = 0; j < source_ids->len; j++) { + package_id = g_ptr_array_index (source_ids, j); + g_ptr_array_add (package_ids, g_strdup (package_id)); + } + } + if (package_ids->len == 0) + return TRUE; + g_ptr_array_add (package_ids, NULL); + + /* get any details */ + g_mutex_lock (&priv->client_mutex); + results = pk_client_get_details (priv->client, + (gchar **) package_ids->pdata, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->client_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + g_autofree gchar *package_ids_str = g_strjoinv (",", (gchar **) package_ids->pdata); + g_prefix_error (error, "failed to get details for %s: ", + package_ids_str); + return FALSE; + } + + /* get the results and copy them into a hash table for fast lookups: + * there are typically 400 to 700 elements in @array, and 100 to 200 + * elements in @list, each with 1 or 2 source IDs to look up (but + * sometimes 200) */ + array = pk_results_get_details_array (results); + details_collection = gs_plugin_packagekit_details_array_to_hash (array); + + /* set the update details for the update */ + for (i = 0; i < gs_app_list_length (list); i++) { + app = gs_app_list_index (list, i); + gs_plugin_packagekit_refine_details_app (plugin, details_collection, app); + } + + return TRUE; +} + +static gboolean +gs_plugin_packagekit_refine_update_urgency (GsPlugin *plugin, + GsAppList *list, + GsPluginRefineFlags flags, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + guint i; + GsApp *app; + const gchar *package_id; + PkBitfield filter; + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autoptr(PkPackageSack) sack = NULL; + g_autoptr(PkResults) results = NULL; + + /* not required */ + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_UPDATE_SEVERITY) == 0) + return TRUE; + + /* get the list of updates */ + filter = pk_bitfield_value (PK_FILTER_ENUM_NONE); + g_mutex_lock (&priv->client_mutex); + results = pk_client_get_updates (priv->client, + filter, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->client_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + g_prefix_error (error, "failed to get updates for urgency: "); + return FALSE; + } + + /* set the update severity for the app */ + sack = pk_results_get_package_sack (results); + for (i = 0; i < gs_app_list_length (list); i++) { + g_autoptr (PkPackage) pkg = NULL; + app = gs_app_list_index (list, i); + if (gs_app_has_quirk (app, GS_APP_QUIRK_IS_WILDCARD)) + continue; + package_id = gs_app_get_source_id_default (app); + if (package_id == NULL) + continue; + pkg = pk_package_sack_find_by_id (sack, package_id); + if (pkg == NULL) + continue; + switch (pk_package_get_info (pkg)) { + case PK_INFO_ENUM_AVAILABLE: + case PK_INFO_ENUM_NORMAL: + case PK_INFO_ENUM_LOW: + case PK_INFO_ENUM_ENHANCEMENT: + gs_app_set_update_urgency (app, AS_URGENCY_KIND_LOW); + break; + case PK_INFO_ENUM_BUGFIX: + gs_app_set_update_urgency (app, AS_URGENCY_KIND_MEDIUM); + break; + case PK_INFO_ENUM_SECURITY: + gs_app_set_update_urgency (app, AS_URGENCY_KIND_CRITICAL); + break; + case PK_INFO_ENUM_IMPORTANT: + gs_app_set_update_urgency (app, AS_URGENCY_KIND_HIGH); + break; + default: + gs_app_set_update_urgency (app, AS_URGENCY_KIND_UNKNOWN); + g_warning ("unhandled info state %s", + pk_info_enum_to_string (pk_package_get_info (pkg))); + break; + } + } + return TRUE; +} + +static gboolean +gs_plugin_refine_app_needs_details (GsPlugin *plugin, GsPluginRefineFlags flags, GsApp *app) +{ + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_LICENSE) > 0 && + gs_app_get_license (app) == NULL) + return TRUE; + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_URL) > 0 && + gs_app_get_url (app, AS_URL_KIND_HOMEPAGE) == NULL) + return TRUE; + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_SIZE) > 0 && + gs_app_get_size_installed (app) == 0) + return TRUE; + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_SIZE) > 0 && + gs_app_get_size_download (app) == 0) + return TRUE; + return FALSE; +} + +static gboolean +gs_plugin_packagekit_refine_details (GsPlugin *plugin, + GsAppList *list, + GsPluginRefineFlags flags, + GCancellable *cancellable, + GError **error) +{ + gboolean ret = TRUE; + g_autoptr(GsAppList) list_tmp = NULL; + + list_tmp = gs_app_list_new (); + for (guint i = 0; i < gs_app_list_length (list); i++) { + GsApp *app = gs_app_list_index (list, i); + if (gs_app_has_quirk (app, GS_APP_QUIRK_IS_WILDCARD)) + continue; + if (g_strcmp0 (gs_app_get_management_plugin (app), "packagekit") != 0) + continue; + if (gs_app_get_source_id_default (app) == NULL) + continue; + if (!gs_plugin_refine_app_needs_details (plugin, flags, app)) + continue; + gs_app_list_add (list_tmp, app); + } + if (gs_app_list_length (list_tmp) == 0) + return TRUE; + ret = gs_plugin_packagekit_refine_details2 (plugin, + list_tmp, + cancellable, + error); + if (!ret) + return FALSE; + return TRUE; +} + +static gboolean +gs_plugin_refine_requires_version (GsApp *app, GsPluginRefineFlags flags) +{ + const gchar *tmp; + tmp = gs_app_get_version (app); + if (tmp != NULL) + return FALSE; + return (flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_VERSION) > 0; +} + +static gboolean +gs_plugin_refine_requires_update_details (GsApp *app, GsPluginRefineFlags flags) +{ + const gchar *tmp; + tmp = gs_app_get_update_details (app); + if (tmp != NULL) + return FALSE; + return (flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_UPDATE_DETAILS) > 0; +} + +static gboolean +gs_plugin_refine_requires_origin (GsApp *app, GsPluginRefineFlags flags) +{ + const gchar *tmp; + tmp = gs_app_get_origin (app); + if (tmp != NULL) + return FALSE; + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_ORIGIN) > 0) + return TRUE; + return FALSE; +} + +static gboolean +gs_plugin_refine_requires_package_id (GsApp *app, GsPluginRefineFlags flags) +{ + const gchar *tmp; + tmp = gs_app_get_source_id_default (app); + if (tmp != NULL) + return FALSE; + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_VERSION) > 0) + return TRUE; + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_LICENSE) > 0) + return TRUE; + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_URL) > 0) + return TRUE; + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_SIZE) > 0) + return TRUE; + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_DESCRIPTION) > 0) + return TRUE; + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_VERSION) > 0) + return TRUE; + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_UPDATE_DETAILS) > 0) + return TRUE; + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_PROVENANCE) > 0) + return TRUE; + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_SETUP_ACTION) > 0) + return TRUE; + return FALSE; +} + +static gboolean +gs_plugin_packagekit_refine_distro_upgrade (GsPlugin *plugin, + GsApp *app, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + guint i; + GsApp *app2; + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autoptr(PkResults) results = NULL; + g_autoptr(GsAppList) list = NULL; + guint cache_age_save; + + gs_packagekit_helper_add_app (helper, app); + + /* ask PK to simulate upgrading the system */ + g_mutex_lock (&priv->client_mutex); + cache_age_save = pk_client_get_cache_age (priv->client); + pk_client_set_cache_age (priv->client, 60 * 60 * 24 * 7); /* once per week */ + results = pk_client_upgrade_system (priv->client, + pk_bitfield_from_enums (PK_TRANSACTION_FLAG_ENUM_SIMULATE, -1), + gs_app_get_version (app), + PK_UPGRADE_KIND_ENUM_COMPLETE, + cancellable, + gs_packagekit_helper_cb, helper, + error); + pk_client_set_cache_age (priv->client, cache_age_save); + g_mutex_unlock (&priv->client_mutex); + + if (!gs_plugin_packagekit_results_valid (results, error)) { + g_prefix_error (error, "failed to refine distro upgrade: "); + return FALSE; + } + list = gs_app_list_new (); + if (!gs_plugin_packagekit_add_results (plugin, list, results, error)) + return FALSE; + + /* add each of these as related applications */ + for (i = 0; i < gs_app_list_length (list); i++) { + app2 = gs_app_list_index (list, i); + if (gs_app_get_state (app2) != AS_APP_STATE_UNAVAILABLE) + continue; + gs_app_add_related (app, app2); + } + return TRUE; +} + +static gboolean +gs_plugin_packagekit_refine_valid_package_name (const gchar *source) +{ + if (g_strstr_len (source, -1, "/") != NULL) + return FALSE; + return TRUE; +} + +static gboolean +gs_plugin_packagekit_refine_name_to_id (GsPlugin *plugin, + GsAppList *list, + GsPluginRefineFlags flags, + GCancellable *cancellable, + GError **error) +{ + g_autoptr(GsAppList) resolve_all = gs_app_list_new (); + for (guint i = 0; i < gs_app_list_length (list); i++) { + GPtrArray *sources; + GsApp *app = gs_app_list_index (list, i); + const gchar *tmp; + if (gs_app_has_quirk (app, GS_APP_QUIRK_IS_WILDCARD)) + continue; + tmp = gs_app_get_management_plugin (app); + if (tmp != NULL && g_strcmp0 (tmp, "packagekit") != 0) + continue; + sources = gs_app_get_sources (app); + if (sources->len == 0) + continue; + tmp = g_ptr_array_index (sources, 0); + if (!gs_plugin_packagekit_refine_valid_package_name (tmp)) + continue; + if (gs_app_get_state (app) == AS_APP_STATE_UNKNOWN || + gs_plugin_refine_requires_package_id (app, flags) || + gs_plugin_refine_requires_origin (app, flags) || + gs_plugin_refine_requires_version (app, flags)) { + gs_app_list_add (resolve_all, app); + } + } + if (gs_app_list_length (resolve_all) > 0) { + if (!gs_plugin_packagekit_resolve_packages (plugin, + resolve_all, + cancellable, + error)) + return FALSE; + } + return TRUE; +} + +static gboolean +gs_plugin_packagekit_refine_filename_to_id (GsPlugin *plugin, + GsAppList *list, + GsPluginRefineFlags flags, + GCancellable *cancellable, + GError **error) +{ + /* not now */ + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_SETUP_ACTION) == 0) + return TRUE; + + for (guint i = 0; i < gs_app_list_length (list); i++) { + g_autofree gchar *fn = NULL; + GsApp *app = gs_app_list_index (list, i); + const gchar *tmp; + if (gs_app_has_quirk (app, GS_APP_QUIRK_IS_WILDCARD)) + continue; + if (gs_app_get_source_id_default (app) != NULL) + continue; + tmp = gs_app_get_management_plugin (app); + if (tmp != NULL && g_strcmp0 (tmp, "packagekit") != 0) + continue; + tmp = gs_app_get_id (app); + if (tmp == NULL) + continue; + switch (gs_app_get_kind (app)) { + case AS_APP_KIND_DESKTOP: + fn = g_strdup_printf ("/usr/share/applications/%s", tmp); + break; + case AS_APP_KIND_ADDON: + fn = g_strdup_printf ("/usr/share/metainfo/%s.metainfo.xml", tmp); + if (!g_file_test (fn, G_FILE_TEST_EXISTS)) { + g_free (fn); + fn = g_strdup_printf ("/usr/share/appdata/%s.metainfo.xml", tmp); + } + break; + default: + break; + } + if (fn == NULL) + continue; + if (!g_file_test (fn, G_FILE_TEST_EXISTS)) { + g_debug ("ignoring %s as does not exist", fn); + continue; + } + if (!gs_plugin_packagekit_refine_from_desktop (plugin, + app, + fn, + cancellable, + error)) + return FALSE; + } + return TRUE; +} + +static gboolean +gs_plugin_packagekit_refine_update_details (GsPlugin *plugin, + GsAppList *list, + GsPluginRefineFlags flags, + GCancellable *cancellable, + GError **error) +{ + g_autoptr(GsAppList) updatedetails_all = gs_app_list_new (); + for (guint i = 0; i < gs_app_list_length (list); i++) { + GsApp *app = gs_app_list_index (list, i); + const gchar *tmp; + if (gs_app_has_quirk (app, GS_APP_QUIRK_IS_WILDCARD)) + continue; + if (gs_app_get_state (app) != AS_APP_STATE_UPDATABLE) + continue; + if (gs_app_get_source_id_default (app) == NULL) + continue; + tmp = gs_app_get_management_plugin (app); + if (tmp != NULL && g_strcmp0 (tmp, "packagekit") != 0) + continue; + if (gs_plugin_refine_requires_update_details (app, flags)) + gs_app_list_add (updatedetails_all, app); + } + if (gs_app_list_length (updatedetails_all) > 0) { + if (!gs_plugin_packagekit_refine_updatedetails (plugin, + updatedetails_all, + cancellable, + error)) + return FALSE; + } + return TRUE; +} + +gboolean +gs_plugin_refine (GsPlugin *plugin, + GsAppList *list, + GsPluginRefineFlags flags, + GCancellable *cancellable, + GError **error) +{ + /* when we need the cannot-be-upgraded applications, we implement this + * by doing a UpgradeSystem(SIMULATE) which adds the removed packages + * to the related-apps list with a state of %AS_APP_STATE_UNAVAILABLE */ + if (flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_UPGRADE_REMOVED) { + for (guint i = 0; i < gs_app_list_length (list); i++) { + GsApp *app = gs_app_list_index (list, i); + if (gs_app_get_kind (app) != AS_APP_KIND_OS_UPGRADE) + continue; + if (!gs_plugin_packagekit_refine_distro_upgrade (plugin, + app, + cancellable, + error)) + return FALSE; + } + } + + /* can we resolve in one go? */ + if (!gs_plugin_packagekit_refine_name_to_id (plugin, list, flags, cancellable, error)) + return FALSE; + + /* set the package-id for an installed desktop file */ + if (!gs_plugin_packagekit_refine_filename_to_id (plugin, list, flags, cancellable, error)) + return FALSE; + + /* any update details missing? */ + if (!gs_plugin_packagekit_refine_update_details (plugin, list, flags, cancellable, error)) + return FALSE; + + /* any package details missing? */ + if (!gs_plugin_packagekit_refine_details (plugin, list, flags, cancellable, error)) + return FALSE; + + /* get the update severity */ + if (!gs_plugin_packagekit_refine_update_urgency (plugin, list, flags, cancellable, error)) + return FALSE; + + for (guint i = 0; i < gs_app_list_length (list); i++) { + GsApp *app = gs_app_list_index (list, i); + + /* only process this app if was created by this plugin */ + if (g_strcmp0 (gs_app_get_management_plugin (app), "packagekit") != 0) + continue; + + /* the scope is always system-wide */ + if (gs_app_get_scope (app) == AS_APP_SCOPE_UNKNOWN) + gs_app_set_scope (app, AS_APP_SCOPE_SYSTEM); + if (gs_app_get_bundle_kind (app) == AS_BUNDLE_KIND_UNKNOWN) + gs_app_set_bundle_kind (app, AS_BUNDLE_KIND_PACKAGE); + } + + /* success */ + return TRUE; +} diff --git a/plugins/packagekit/gs-plugin-packagekit-refresh.c b/plugins/packagekit/gs-plugin-packagekit-refresh.c new file mode 100644 index 0000000..2d9a7e4 --- /dev/null +++ b/plugins/packagekit/gs-plugin-packagekit-refresh.c @@ -0,0 +1,182 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2014-2016 Richard Hughes <richard@hughsie.com> + * Copyright (C) 2015-2018 Kalev Lember <klember@redhat.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <config.h> + +#include <packagekit-glib2/packagekit.h> +#include <gnome-software.h> + +#include "gs-metered.h" +#include "gs-packagekit-helper.h" +#include "packagekit-common.h" + +/* + * SECTION: + * Do a PackageKit UpdatePackages(ONLY_DOWNLOAD) method on refresh and + * also convert any package files to applications the best we can. + */ + +struct GsPluginData { + PkTask *task; + GMutex task_mutex; +}; + +void +gs_plugin_initialize (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData)); + + g_mutex_init (&priv->task_mutex); + priv->task = pk_task_new (); + pk_task_set_only_download (priv->task, TRUE); + pk_client_set_background (PK_CLIENT (priv->task), TRUE); + + /* we can return better results than dpkg directly */ + gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_CONFLICTS, "dpkg"); +} + +void +gs_plugin_destroy (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_mutex_clear (&priv->task_mutex); + g_object_unref (priv->task); +} + +static gboolean +_download_only (GsPlugin *plugin, GsAppList *list, + GCancellable *cancellable, GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_auto(GStrv) package_ids = NULL; + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autoptr(PkPackageSack) sack = NULL; + g_autoptr(PkResults) results2 = NULL; + g_autoptr(PkResults) results = NULL; + + /* get the list of packages to update */ + gs_plugin_status_update (plugin, NULL, GS_PLUGIN_STATUS_WAITING); + + g_mutex_lock (&priv->task_mutex); + /* never refresh the metadata here as this can surprise the frontend if + * we end up downloading a different set of packages than what was + * shown to the user */ + pk_client_set_cache_age (PK_CLIENT (priv->task), G_MAXUINT); + results = pk_client_get_updates (PK_CLIENT (priv->task), + pk_bitfield_value (PK_FILTER_ENUM_NONE), + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + return FALSE; + } + + /* download all the packages */ + sack = pk_results_get_package_sack (results); + if (pk_package_sack_get_size (sack) == 0) + return TRUE; + package_ids = pk_package_sack_get_ids (sack); + for (guint i = 0; i < gs_app_list_length (list); i++) { + GsApp *app = gs_app_list_index (list, i); + gs_packagekit_helper_add_app (helper, app); + } + g_mutex_lock (&priv->task_mutex); + /* never refresh the metadata here as this can surprise the frontend if + * we end up downloading a different set of packages than what was + * shown to the user */ + pk_client_set_cache_age (PK_CLIENT (priv->task), G_MAXUINT); + results2 = pk_task_update_packages_sync (priv->task, + package_ids, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (results2 == NULL) { + gs_plugin_packagekit_error_convert (error); + return FALSE; + } + return TRUE; +} + +gboolean +gs_plugin_download (GsPlugin *plugin, + GsAppList *list, + GCancellable *cancellable, + GError **error) +{ + g_autoptr(GsAppList) list_tmp = gs_app_list_new (); + + /* add any packages */ + for (guint i = 0; i < gs_app_list_length (list); i++) { + GsApp *app = gs_app_list_index (list, i); + GsAppList *related = gs_app_get_related (app); + + /* add this app */ + if (!gs_app_has_quirk (app, GS_APP_QUIRK_IS_PROXY)) + if (g_strcmp0 (gs_app_get_management_plugin (app), "packagekit") == 0) { + gs_app_list_add (list_tmp, app); + continue; + } + + /* add each related app */ + for (guint j = 0; j < gs_app_list_length (related); j++) { + GsApp *app_tmp = gs_app_list_index (related, j); + if (g_strcmp0 (gs_app_get_management_plugin (app_tmp), "packagekit") == 0) + gs_app_list_add (list_tmp, app_tmp); + } + } + + if (gs_app_list_length (list_tmp) == 0) + return TRUE; + + if (!gs_plugin_has_flags (plugin, GS_PLUGIN_FLAGS_INTERACTIVE)) { + g_autoptr(GError) error_local = NULL; + + if (!gs_metered_block_app_list_on_download_scheduler (list_tmp, cancellable, &error_local)) { + g_warning ("Failed to block on download scheduler: %s", + error_local->message); + g_clear_error (&error_local); + } + } + + return _download_only (plugin, list_tmp, cancellable, error); +} + +gboolean +gs_plugin_refresh (GsPlugin *plugin, + guint cache_age, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autoptr(GsApp) app_dl = gs_app_new (gs_plugin_get_name (plugin)); + g_autoptr(PkResults) results = NULL; + + gs_plugin_status_update (plugin, NULL, GS_PLUGIN_STATUS_WAITING); + gs_packagekit_helper_set_progress_app (helper, app_dl); + + g_mutex_lock (&priv->task_mutex); + /* cache age of 1 is user-initiated */ + pk_client_set_background (PK_CLIENT (priv->task), cache_age > 1); + pk_client_set_cache_age (PK_CLIENT (priv->task), cache_age); + /* refresh the metadata */ + results = pk_client_refresh_cache (PK_CLIENT (priv->task), + FALSE /* force */, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + return FALSE; + } + + return TRUE; +} diff --git a/plugins/packagekit/gs-plugin-packagekit-upgrade.c b/plugins/packagekit/gs-plugin-packagekit-upgrade.c new file mode 100644 index 0000000..6442dd7 --- /dev/null +++ b/plugins/packagekit/gs-plugin-packagekit-upgrade.c @@ -0,0 +1,88 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2016 Richard Hughes <richard@hughsie.com> + * Copyright (C) 2016 Kalev Lember <klember@redhat.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <config.h> + +#include <packagekit-glib2/packagekit.h> + +#include <gnome-software.h> + +#include "gs-packagekit-helper.h" +#include "packagekit-common.h" + +struct GsPluginData { + PkTask *task; + GMutex task_mutex; +}; + +void +gs_plugin_initialize (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData)); + + g_mutex_init (&priv->task_mutex); + priv->task = pk_task_new (); + pk_task_set_only_download (priv->task, TRUE); + pk_client_set_background (PK_CLIENT (priv->task), TRUE); + pk_client_set_cache_age (PK_CLIENT (priv->task), 60 * 60 * 24); +} + +void +gs_plugin_destroy (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_mutex_clear (&priv->task_mutex); + g_object_unref (priv->task); +} + +void +gs_plugin_adopt_app (GsPlugin *plugin, GsApp *app) +{ + if (gs_app_get_kind (app) == AS_APP_KIND_OS_UPGRADE) + gs_app_set_management_plugin (app, "packagekit"); +} + +gboolean +gs_plugin_app_upgrade_download (GsPlugin *plugin, + GsApp *app, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autoptr(PkResults) results = NULL; + + /* only process this app if was created by this plugin */ + if (g_strcmp0 (gs_app_get_management_plugin (app), "packagekit") != 0) + return TRUE; + + /* check is distro-upgrade */ + if (gs_app_get_kind (app) != AS_APP_KIND_OS_UPGRADE) + return TRUE; + + /* ask PK to download enough packages to upgrade the system */ + gs_app_set_state (app, AS_APP_STATE_INSTALLING); + gs_packagekit_helper_set_progress_app (helper, app); + g_mutex_lock (&priv->task_mutex); + results = pk_task_upgrade_system_sync (priv->task, + gs_app_get_version (app), + PK_UPGRADE_KIND_ENUM_COMPLETE, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + gs_app_set_state_recover (app); + return FALSE; + } + + /* state is known */ + gs_app_set_state (app, AS_APP_STATE_UPDATABLE); + return TRUE; +} diff --git a/plugins/packagekit/gs-plugin-packagekit-url-to-app.c b/plugins/packagekit/gs-plugin-packagekit-url-to-app.c new file mode 100644 index 0000000..0418920 --- /dev/null +++ b/plugins/packagekit/gs-plugin-packagekit-url-to-app.c @@ -0,0 +1,125 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2017 Canonical Ltd + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <config.h> + +#include <packagekit-glib2/packagekit.h> +#include <gnome-software.h> + +#include "gs-packagekit-helper.h" +#include "packagekit-common.h" + +struct GsPluginData { + PkClient *client; + GMutex client_mutex; +}; + +void +gs_plugin_initialize (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData)); + + g_mutex_init (&priv->client_mutex); + priv->client = pk_client_new (); + + pk_client_set_background (priv->client, FALSE); + pk_client_set_cache_age (priv->client, G_MAXUINT); +} + +void +gs_plugin_destroy (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_mutex_clear (&priv->client_mutex); + g_object_unref (priv->client); +} + +gboolean +gs_plugin_url_to_app (GsPlugin *plugin, + GsAppList *list, + const gchar *url, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + + g_autofree gchar *scheme = NULL; + g_autofree gchar *path = NULL; + const gchar *id = NULL; + const gchar * const *id_like = NULL; + g_auto(GStrv) package_ids = NULL; + g_autoptr(PkResults) results = NULL; + g_autoptr(GsApp) app = NULL; + g_autoptr(GsOsRelease) os_release = NULL; + g_autoptr(GPtrArray) packages = NULL; + g_autoptr(GPtrArray) details = NULL; + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + + path = gs_utils_get_url_path (url); + + /* only do this for apt:// on debian or debian-like distros */ + os_release = gs_os_release_new (error); + if (os_release == NULL) { + g_prefix_error (error, "failed to determine OS information:"); + return FALSE; + } else { + id = gs_os_release_get_id (os_release); + id_like = gs_os_release_get_id_like (os_release); + scheme = gs_utils_get_url_scheme (url); + if (!(g_strcmp0 (scheme, "apt") == 0 && + (g_strcmp0 (id, "debian") == 0 || + g_strv_contains (id_like, "debian")))) { + return TRUE; + } + } + + app = gs_app_new (NULL); + gs_plugin_packagekit_set_packaging_format (plugin, app); + gs_app_add_source (app, path); + gs_app_set_kind (app, AS_APP_KIND_GENERIC); + gs_app_set_bundle_kind (app, AS_BUNDLE_KIND_PACKAGE); + + package_ids = g_new0 (gchar *, 2); + package_ids[0] = g_strdup (path); + + g_mutex_lock (&priv->client_mutex); + results = pk_client_resolve (priv->client, + pk_bitfield_from_enums (PK_FILTER_ENUM_NEWEST, PK_FILTER_ENUM_ARCH, -1), + package_ids, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->client_mutex); + + if (!gs_plugin_packagekit_results_valid (results, error)) { + g_prefix_error (error, "failed to resolve package_ids: "); + return FALSE; + } + + /* get results */ + packages = pk_results_get_package_array (results); + details = pk_results_get_details_array (results); + + if (packages->len >= 1) { + g_autoptr(GHashTable) details_collection = NULL; + + if (gs_app_get_local_file (app) != NULL) + return TRUE; + + details_collection = gs_plugin_packagekit_details_array_to_hash (details); + + gs_plugin_packagekit_resolve_packages_app (plugin, packages, app); + gs_plugin_packagekit_refine_details_app (plugin, details_collection, app); + + gs_app_list_add (list, app); + } else { + g_warning ("no results returned"); + } + + return TRUE; +} diff --git a/plugins/packagekit/gs-plugin-packagekit.c b/plugins/packagekit/gs-plugin-packagekit.c new file mode 100644 index 0000000..c379f94 --- /dev/null +++ b/plugins/packagekit/gs-plugin-packagekit.c @@ -0,0 +1,693 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2013-2016 Richard Hughes <richard@hughsie.com> + * Copyright (C) 2014-2018 Kalev Lember <klember@redhat.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <config.h> + +#include <packagekit-glib2/packagekit.h> + +#include <gnome-software.h> + +#include "packagekit-common.h" +#include "gs-packagekit-helper.h" + +/* + * SECTION: + * Uses the system PackageKit instance to return installed packages, + * sources and the ability to add and remove packages. + * + * Requires: | [source-id] + * Refines: | [source-id], [source], [update-details], [management-plugin] + */ + +struct GsPluginData { + PkTask *task; + GMutex task_mutex; +}; + +void +gs_plugin_initialize (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData)); + + g_mutex_init (&priv->task_mutex); + priv->task = pk_task_new (); + pk_client_set_background (PK_CLIENT (priv->task), FALSE); + pk_client_set_cache_age (PK_CLIENT (priv->task), G_MAXUINT); +} + +void +gs_plugin_destroy (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_mutex_clear (&priv->task_mutex); + g_object_unref (priv->task); +} + +static gboolean +gs_plugin_add_sources_related (GsPlugin *plugin, + GHashTable *hash, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + guint i; + GsApp *app; + GsApp *app_tmp; + PkBitfield filter; + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + const gchar *id; + gboolean ret = TRUE; + g_autoptr(GsAppList) installed = gs_app_list_new (); + g_autoptr(PkResults) results = NULL; + + filter = pk_bitfield_from_enums (PK_FILTER_ENUM_INSTALLED, + PK_FILTER_ENUM_NEWEST, + PK_FILTER_ENUM_ARCH, + PK_FILTER_ENUM_NOT_COLLECTIONS, + -1); + g_mutex_lock (&priv->task_mutex); + results = pk_client_get_packages (PK_CLIENT(priv->task), + filter, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + g_prefix_error (error, "failed to get sources related: "); + return FALSE; + } + ret = gs_plugin_packagekit_add_results (plugin, + installed, + results, + error); + if (!ret) + return FALSE; + for (i = 0; i < gs_app_list_length (installed); i++) { + g_auto(GStrv) split = NULL; + app = gs_app_list_index (installed, i); + split = pk_package_id_split (gs_app_get_source_id_default (app)); + if (split == NULL) { + g_set_error (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_INVALID_FORMAT, + "invalid package-id: %s", + gs_app_get_source_id_default (app)); + return FALSE; + } + if (g_str_has_prefix (split[PK_PACKAGE_ID_DATA], "installed:")) { + id = split[PK_PACKAGE_ID_DATA] + 10; + app_tmp = g_hash_table_lookup (hash, id); + if (app_tmp != NULL) { + g_debug ("found package %s from %s", + gs_app_get_source_default (app), id); + gs_app_add_related (app_tmp, app); + } + } + } + return TRUE; +} + +gboolean +gs_plugin_add_sources (GsPlugin *plugin, + GsAppList *list, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + PkBitfield filter; + PkRepoDetail *rd; + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + const gchar *id; + guint i; + g_autoptr(GHashTable) hash = NULL; + g_autoptr(PkResults) results = NULL; + g_autoptr(GPtrArray) array = NULL; + + /* ask PK for the repo details */ + filter = pk_bitfield_from_enums (PK_FILTER_ENUM_NOT_SOURCE, + PK_FILTER_ENUM_NOT_DEVELOPMENT, + PK_FILTER_ENUM_NOT_SUPPORTED, + -1); + g_mutex_lock (&priv->task_mutex); + results = pk_client_get_repo_list (PK_CLIENT(priv->task), + filter, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) + return FALSE; + hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); + array = pk_results_get_repo_detail_array (results); + for (i = 0; i < array->len; i++) { + g_autoptr(GsApp) app = NULL; + rd = g_ptr_array_index (array, i); + id = pk_repo_detail_get_id (rd); + app = gs_app_new (id); + gs_app_set_management_plugin (app, gs_plugin_get_name (plugin)); + gs_app_set_kind (app, AS_APP_KIND_SOURCE); + gs_app_set_bundle_kind (app, AS_BUNDLE_KIND_PACKAGE); + gs_app_add_quirk (app, GS_APP_QUIRK_NOT_LAUNCHABLE); + gs_app_set_state (app, pk_repo_detail_get_enabled (rd) ? + AS_APP_STATE_INSTALLED : AS_APP_STATE_AVAILABLE); + gs_app_set_name (app, + GS_APP_QUALITY_LOWEST, + pk_repo_detail_get_description (rd)); + gs_app_set_summary (app, + GS_APP_QUALITY_LOWEST, + pk_repo_detail_get_description (rd)); + gs_app_list_add (list, app); + g_hash_table_insert (hash, + g_strdup (id), + (gpointer) app); + } + + /* get every application on the system and add it as a related package + * if it matches */ + return gs_plugin_add_sources_related (plugin, hash, cancellable, error); +} + +static gboolean +gs_plugin_app_origin_repo_enable (GsPlugin *plugin, + GsApp *app, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autoptr(PkResults) results = NULL; + const gchar *repo_id; + + repo_id = gs_app_get_origin (app); + if (repo_id == NULL) { + g_set_error_literal (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_NOT_SUPPORTED, + "origin not set"); + return FALSE; + } + + /* do sync call */ + gs_plugin_status_update (plugin, app, GS_PLUGIN_STATUS_WAITING); + g_mutex_lock (&priv->task_mutex); + results = pk_client_repo_enable (PK_CLIENT (priv->task), + repo_id, + TRUE, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + gs_utils_error_add_origin_id (error, app); + return FALSE; + } + + /* now that the repo is enabled, the app (not the repo!) moves from + * UNAVAILABLE state to AVAILABLE */ + gs_app_set_state (app, AS_APP_STATE_AVAILABLE); + + return TRUE; +} + +static gboolean +gs_plugin_repo_enable (GsPlugin *plugin, + GsApp *app, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autoptr(PkResults) results = NULL; + + /* do sync call */ + gs_plugin_status_update (plugin, app, GS_PLUGIN_STATUS_WAITING); + gs_app_set_state (app, AS_APP_STATE_INSTALLING); + gs_packagekit_helper_add_app (helper, app); + g_mutex_lock (&priv->task_mutex); + results = pk_client_repo_enable (PK_CLIENT (priv->task), + gs_app_get_id (app), + TRUE, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + gs_app_set_state_recover (app); + gs_utils_error_add_origin_id (error, app); + return FALSE; + } + + /* state is known */ + gs_app_set_state (app, AS_APP_STATE_INSTALLED); + + return TRUE; +} + +gboolean +gs_plugin_app_install (GsPlugin *plugin, + GsApp *app, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + GsAppList *addons; + GPtrArray *source_ids; + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + const gchar *package_id; + guint i, j; + g_autofree gchar *local_filename = NULL; + g_auto(GStrv) package_ids = NULL; + g_autoptr(GPtrArray) array_package_ids = NULL; + g_autoptr(PkResults) results = NULL; + + /* only process this app if was created by this plugin */ + if (g_strcmp0 (gs_app_get_management_plugin (app), + gs_plugin_get_name (plugin)) != 0) + return TRUE; + + /* enable repo */ + if (gs_app_get_kind (app) == AS_APP_KIND_SOURCE) + return gs_plugin_repo_enable (plugin, app, cancellable, error); + + /* queue for install if installation needs the network */ + if (!gs_plugin_get_network_available (plugin)) { + gs_app_set_state (app, AS_APP_STATE_QUEUED_FOR_INSTALL); + return TRUE; + } + + if (gs_app_get_state (app) == AS_APP_STATE_UNAVAILABLE) { + /* get everything up front we need */ + source_ids = gs_app_get_source_ids (app); + if (source_ids->len == 0) { + g_set_error_literal (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_NOT_SUPPORTED, + "installing not available"); + return FALSE; + } + package_ids = g_new0 (gchar *, 2); + package_ids[0] = g_strdup (g_ptr_array_index (source_ids, 0)); + + /* enable the repo where the unavailable app is coming from */ + if (!gs_plugin_app_origin_repo_enable (plugin, app, cancellable, error)) + return FALSE; + + gs_app_set_state (app, AS_APP_STATE_INSTALLING); + + /* FIXME: this is a hack, to allow PK time to re-initialize + * everything in order to match an actual result. The root cause + * is probably some kind of hard-to-debug race in the daemon. */ + g_usleep (G_USEC_PER_SEC * 3); + + /* actually install the package */ + gs_packagekit_helper_add_app (helper, app); + g_mutex_lock (&priv->task_mutex); + results = pk_task_install_packages_sync (priv->task, + package_ids, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + gs_app_set_state_recover (app); + return FALSE; + } + + /* state is known */ + gs_app_set_state (app, AS_APP_STATE_INSTALLED); + + /* if we remove the app again later, we should be able to + * cancel the installation if we'd never installed it */ + gs_app_set_allow_cancel (app, TRUE); + + /* no longer valid */ + gs_app_clear_source_ids (app); + return TRUE; + } + + /* get the list of available package ids to install */ + switch (gs_app_get_state (app)) { + case AS_APP_STATE_AVAILABLE: + case AS_APP_STATE_UPDATABLE: + source_ids = gs_app_get_source_ids (app); + if (source_ids->len == 0) { + g_set_error_literal (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_NOT_SUPPORTED, + "installing not available"); + return FALSE; + } + array_package_ids = g_ptr_array_new_with_free_func (g_free); + for (i = 0; i < source_ids->len; i++) { + package_id = g_ptr_array_index (source_ids, i); + if (g_strstr_len (package_id, -1, ";installed") != NULL) + continue; + g_ptr_array_add (array_package_ids, g_strdup (package_id)); + } + + addons = gs_app_get_addons (app); + for (i = 0; i < gs_app_list_length (addons); i++) { + GsApp *addon = gs_app_list_index (addons, i); + + if (!gs_app_get_to_be_installed (addon)) + continue; + + source_ids = gs_app_get_source_ids (addon); + for (j = 0; j < source_ids->len; j++) { + package_id = g_ptr_array_index (source_ids, j); + if (g_strstr_len (package_id, -1, ";installed") != NULL) + continue; + g_ptr_array_add (array_package_ids, g_strdup (package_id)); + } + } + g_ptr_array_add (array_package_ids, NULL); + + if (array_package_ids->len == 0) { + g_set_error_literal (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_NOT_SUPPORTED, + "no packages to install"); + return FALSE; + } + + gs_app_set_state (app, AS_APP_STATE_INSTALLING); + addons = gs_app_get_addons (app); + for (i = 0; i < gs_app_list_length (addons); i++) { + GsApp *addon = gs_app_list_index (addons, i); + if (gs_app_get_to_be_installed (addon)) + gs_app_set_state (addon, AS_APP_STATE_INSTALLING); + } + gs_packagekit_helper_add_app (helper, app); + g_mutex_lock (&priv->task_mutex); + results = pk_task_install_packages_sync (priv->task, + (gchar **) array_package_ids->pdata, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + gs_app_set_state_recover (app); + return FALSE; + } + + /* state is known */ + gs_app_set_state (app, AS_APP_STATE_INSTALLED); + + break; + case AS_APP_STATE_AVAILABLE_LOCAL: + if (gs_app_get_local_file (app) == NULL) { + g_set_error_literal (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_NOT_SUPPORTED, + "local package, but no filename"); + return FALSE; + } + local_filename = g_file_get_path (gs_app_get_local_file (app)); + package_ids = g_strsplit (local_filename, "\t", -1); + + gs_app_set_state (app, AS_APP_STATE_INSTALLING); + gs_packagekit_helper_add_app (helper, app); + g_mutex_lock (&priv->task_mutex); + results = pk_task_install_files_sync (priv->task, + package_ids, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + gs_app_set_state_recover (app); + return FALSE; + } + + /* state is known */ + gs_app_set_state (app, AS_APP_STATE_INSTALLED); + + /* get the new icon from the package */ + gs_app_set_local_file (app, NULL); + gs_app_add_icon (app, NULL); + gs_app_set_pixbuf (app, NULL); + break; + default: + g_set_error (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_NOT_SUPPORTED, + "do not know how to install app in state %s", + as_app_state_to_string (gs_app_get_state (app))); + return FALSE; + } + + /* no longer valid */ + gs_app_clear_source_ids (app); + + return TRUE; +} + +static gboolean +gs_plugin_repo_disable (GsPlugin *plugin, + GsApp *app, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autoptr(PkResults) results = NULL; + + /* do sync call */ + gs_plugin_status_update (plugin, app, GS_PLUGIN_STATUS_WAITING); + gs_app_set_state (app, AS_APP_STATE_REMOVING); + gs_packagekit_helper_add_app (helper, app); + g_mutex_lock (&priv->task_mutex); + results = pk_client_repo_enable (PK_CLIENT (priv->task), + gs_app_get_id (app), + FALSE, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + gs_app_set_state_recover (app); + gs_utils_error_add_origin_id (error, app); + return FALSE; + } + + /* state is known */ + gs_app_set_state (app, AS_APP_STATE_AVAILABLE); + + return TRUE; +} + +gboolean +gs_plugin_app_remove (GsPlugin *plugin, + GsApp *app, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + const gchar *package_id; + GPtrArray *source_ids; + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + guint i; + guint cnt = 0; + g_autoptr(PkResults) results = NULL; + g_auto(GStrv) package_ids = NULL; + + /* only process this app if was created by this plugin */ + if (g_strcmp0 (gs_app_get_management_plugin (app), + gs_plugin_get_name (plugin)) != 0) + return TRUE; + + /* disable repo */ + if (gs_app_get_kind (app) == AS_APP_KIND_SOURCE) + return gs_plugin_repo_disable (plugin, app, cancellable, error); + + /* get the list of available package ids to install */ + source_ids = gs_app_get_source_ids (app); + if (source_ids->len == 0) { + g_set_error_literal (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_NOT_SUPPORTED, + "removing not available"); + return FALSE; + } + package_ids = g_new0 (gchar *, source_ids->len + 1); + for (i = 0; i < source_ids->len; i++) { + package_id = g_ptr_array_index (source_ids, i); + if (g_strstr_len (package_id, -1, ";installed") == NULL) + continue; + package_ids[cnt++] = g_strdup (package_id); + } + if (cnt == 0) { + g_set_error_literal (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_NOT_SUPPORTED, + "no packages to remove"); + return FALSE; + } + + /* do the action */ + gs_app_set_state (app, AS_APP_STATE_REMOVING); + gs_packagekit_helper_add_app (helper, app); + g_mutex_lock (&priv->task_mutex); + results = pk_task_remove_packages_sync (priv->task, + package_ids, + TRUE, GS_PACKAGEKIT_AUTOREMOVE, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) { + gs_app_set_state_recover (app); + return FALSE; + } + + /* state is not known: we don't know if we can re-install this app */ + gs_app_set_state (app, AS_APP_STATE_UNKNOWN); + + /* no longer valid */ + gs_app_clear_source_ids (app); + + return TRUE; +} + +static GsApp * +gs_plugin_packagekit_build_update_app (GsPlugin *plugin, PkPackage *package) +{ + GsApp *app = gs_plugin_cache_lookup (plugin, pk_package_get_id (package)); + if (app != NULL) + return app; + app = gs_app_new (NULL); + gs_plugin_packagekit_set_packaging_format (plugin, app); + gs_app_add_source (app, pk_package_get_name (package)); + gs_app_add_source_id (app, pk_package_get_id (package)); + gs_app_set_name (app, GS_APP_QUALITY_LOWEST, + pk_package_get_name (package)); + gs_app_set_summary (app, GS_APP_QUALITY_LOWEST, + pk_package_get_summary (package)); + gs_app_set_metadata (app, "GnomeSoftware::Creator", + gs_plugin_get_name (plugin)); + gs_app_set_management_plugin (app, "packagekit"); + gs_app_set_update_version (app, pk_package_get_version (package)); + gs_app_set_kind (app, AS_APP_KIND_GENERIC); + gs_app_set_scope (app, AS_APP_SCOPE_SYSTEM); + gs_app_set_bundle_kind (app, AS_BUNDLE_KIND_PACKAGE); + gs_app_set_state (app, AS_APP_STATE_UPDATABLE); + gs_plugin_cache_add (plugin, pk_package_get_id (package), app); + return app; +} + +gboolean +gs_plugin_add_updates (GsPlugin *plugin, + GsAppList *list, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autoptr(PkResults) results = NULL; + g_autoptr(GPtrArray) array = NULL; + + /* do sync call */ + gs_plugin_status_update (plugin, NULL, GS_PLUGIN_STATUS_WAITING); + g_mutex_lock (&priv->task_mutex); + results = pk_client_get_updates (PK_CLIENT (priv->task), + pk_bitfield_value (PK_FILTER_ENUM_NONE), + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) + return FALSE; + + /* add results */ + array = pk_results_get_package_array (results); + for (guint i = 0; i < array->len; i++) { + PkPackage *package = g_ptr_array_index (array, i); + g_autoptr(GsApp) app = NULL; + app = gs_plugin_packagekit_build_update_app (plugin, package); + gs_app_list_add (list, app); + } + return TRUE; +} + +gboolean +gs_plugin_add_search_files (GsPlugin *plugin, + gchar **search, + GsAppList *list, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + PkBitfield filter; + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autoptr(PkResults) results = NULL; + + /* do sync call */ + gs_plugin_status_update (plugin, NULL, GS_PLUGIN_STATUS_WAITING); + filter = pk_bitfield_from_enums (PK_FILTER_ENUM_NEWEST, + PK_FILTER_ENUM_ARCH, + -1); + g_mutex_lock (&priv->task_mutex); + results = pk_client_search_files (PK_CLIENT (priv->task), + filter, + search, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) + return FALSE; + + /* add results */ + return gs_plugin_packagekit_add_results (plugin, list, results, error); +} + +gboolean +gs_plugin_add_search_what_provides (GsPlugin *plugin, + gchar **search, + GsAppList *list, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + PkBitfield filter; + g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin); + g_autoptr(PkResults) results = NULL; + + /* do sync call */ + gs_plugin_status_update (plugin, NULL, GS_PLUGIN_STATUS_WAITING); + filter = pk_bitfield_from_enums (PK_FILTER_ENUM_NEWEST, + PK_FILTER_ENUM_ARCH, + -1); + g_mutex_lock (&priv->task_mutex); + results = pk_client_what_provides (PK_CLIENT (priv->task), + filter, + search, + cancellable, + gs_packagekit_helper_cb, helper, + error); + g_mutex_unlock (&priv->task_mutex); + if (!gs_plugin_packagekit_results_valid (results, error)) + return FALSE; + + /* add results */ + return gs_plugin_packagekit_add_results (plugin, list, results, error); +} + +gboolean +gs_plugin_launch (GsPlugin *plugin, + GsApp *app, + GCancellable *cancellable, + GError **error) +{ + /* only process this app if was created by this plugin */ + if (g_strcmp0 (gs_app_get_management_plugin (app), + gs_plugin_get_name (plugin)) != 0) + return TRUE; + return gs_plugin_app_launch (plugin, app, error); +} diff --git a/plugins/packagekit/gs-plugin-systemd-updates.c b/plugins/packagekit/gs-plugin-systemd-updates.c new file mode 100644 index 0000000..ea8ff1a --- /dev/null +++ b/plugins/packagekit/gs-plugin-systemd-updates.c @@ -0,0 +1,330 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2013 Richard Hughes <richard@hughsie.com> + * Copyright (C) 2015-2016 Kalev Lember <klember@redhat.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <config.h> + +#include <packagekit-glib2/packagekit.h> + +#include "packagekit-common.h" + +#include <gnome-software.h> + +/* + * Mark previously downloaded packages as zero size, and also allow + * scheduling the offline update. + */ + +struct GsPluginData { + GFileMonitor *monitor; + GFileMonitor *monitor_trigger; + GPermission *permission; + gboolean is_triggered; + GHashTable *hash_prepared; + GMutex hash_prepared_mutex; +}; + +void +gs_plugin_initialize (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData)); + gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "packagekit-refresh"); + gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "packagekit-refine"); + gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_BEFORE, "generic-updates"); + g_mutex_init (&priv->hash_prepared_mutex); + priv->hash_prepared = g_hash_table_new_full (g_str_hash, g_str_equal, + g_free, NULL); +} + +void +gs_plugin_destroy (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_hash_table_unref (priv->hash_prepared); + g_mutex_clear (&priv->hash_prepared_mutex); + if (priv->monitor != NULL) + g_object_unref (priv->monitor); + if (priv->monitor_trigger != NULL) + g_object_unref (priv->monitor_trigger); +} + +static void +gs_plugin_systemd_updates_permission_cb (GPermission *permission, + GParamSpec *pspec, + gpointer data) +{ + GsPlugin *plugin = GS_PLUGIN (data); + gboolean ret = g_permission_get_allowed (permission) || + g_permission_get_can_acquire (permission); + gs_plugin_set_allow_updates (plugin, ret); +} + +static gboolean +gs_plugin_systemd_update_cache (GsPlugin *plugin, GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_autoptr(GError) error_local = NULL; + g_auto(GStrv) package_ids = NULL; + g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->hash_prepared_mutex); + + /* invalidate */ + g_hash_table_remove_all (priv->hash_prepared); + + /* get new list of package-ids */ + package_ids = pk_offline_get_prepared_ids (&error_local); + if (package_ids == NULL) { + if (g_error_matches (error_local, + PK_OFFLINE_ERROR, + PK_OFFLINE_ERROR_NO_DATA)) { + return TRUE; + } + g_set_error (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_INVALID_FORMAT, + "Failed to get prepared IDs: %s", + error_local->message); + return FALSE; + } + for (guint i = 0; package_ids[i] != NULL; i++) { + g_hash_table_insert (priv->hash_prepared, + g_strdup (package_ids[i]), + GUINT_TO_POINTER (1)); + } + return TRUE; +} + +static void +gs_plugin_systemd_updates_changed_cb (GFileMonitor *monitor, + GFile *file, GFile *other_file, + GFileMonitorEvent event_type, + gpointer user_data) +{ + GsPlugin *plugin = GS_PLUGIN (user_data); + + /* update UI */ + gs_plugin_systemd_update_cache (plugin, NULL); + gs_plugin_updates_changed (plugin); +} + +static void +gs_plugin_systemd_updates_refresh_is_triggered (GsPlugin *plugin, GCancellable *cancellable) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_autoptr(GFile) file_trigger = NULL; + file_trigger = g_file_new_for_path ("/system-update"); + priv->is_triggered = g_file_query_exists (file_trigger, NULL); + g_debug ("offline trigger is now %s", + priv->is_triggered ? "enabled" : "disabled"); +} + +static void +gs_plugin_systemd_trigger_changed_cb (GFileMonitor *monitor, + GFile *file, GFile *other_file, + GFileMonitorEvent event_type, + gpointer user_data) +{ + GsPlugin *plugin = GS_PLUGIN (user_data); + gs_plugin_systemd_updates_refresh_is_triggered (plugin, NULL); +} + +static void +gs_plugin_systemd_refine_app (GsPlugin *plugin, GsApp *app) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + const gchar *package_id; + g_autoptr(GMutexLocker) locker = NULL; + + /* only process this app if was created by this plugin */ + if (g_strcmp0 (gs_app_get_management_plugin (app), "packagekit") != 0) + return; + + /* the package is already downloaded */ + package_id = gs_app_get_source_id_default (app); + if (package_id == NULL) + return; + locker = g_mutex_locker_new (&priv->hash_prepared_mutex); + if (g_hash_table_lookup (priv->hash_prepared, package_id) != NULL) + gs_app_set_size_download (app, 0); +} + +gboolean +gs_plugin_refine (GsPlugin *plugin, + GsAppList *list, + GsPluginRefineFlags flags, + GCancellable *cancellable, + GError **error) +{ + /* not now */ + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_SIZE) == 0) + return TRUE; + + /* re-read /var/lib/PackageKit/prepared-update */ + if (!gs_plugin_systemd_update_cache (plugin, error)) + return FALSE; + + for (guint i = 0; i < gs_app_list_length (list); i++) { + GsApp *app = gs_app_list_index (list, i); + GsAppList *related = gs_app_get_related (app); + /* refine the app itself */ + gs_plugin_systemd_refine_app (plugin, app); + /* and anything related for proxy apps */ + for (guint j = 0; j < gs_app_list_length (related); j++) { + GsApp *app_related = gs_app_list_index (related, j); + gs_plugin_systemd_refine_app (plugin, app_related); + } + } + + return TRUE; +} + +gboolean +gs_plugin_setup (GsPlugin *plugin, GCancellable *cancellable, GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_autoptr(GFile) file_trigger = NULL; + + /* watch the prepared file */ + priv->monitor = pk_offline_get_prepared_monitor (cancellable, error); + if (priv->monitor == NULL) { + gs_utils_error_convert_gio (error); + return FALSE; + } + g_signal_connect (priv->monitor, "changed", + G_CALLBACK (gs_plugin_systemd_updates_changed_cb), + plugin); + + /* watch the trigger file */ + file_trigger = g_file_new_for_path ("/system-update"); + priv->monitor_trigger = g_file_monitor_file (file_trigger, + G_FILE_MONITOR_NONE, + NULL, + error); + if (priv->monitor_trigger == NULL) { + gs_utils_error_convert_gio (error); + return FALSE; + } + g_signal_connect (priv->monitor_trigger, "changed", + G_CALLBACK (gs_plugin_systemd_trigger_changed_cb), + plugin); + + /* check if we have permission to trigger the update */ + priv->permission = gs_utils_get_permission ( + "org.freedesktop.packagekit.trigger-offline-update", + NULL, NULL); + if (priv->permission != NULL) { + g_signal_connect (priv->permission, "notify", + G_CALLBACK (gs_plugin_systemd_updates_permission_cb), + plugin); + } + + /* get the list of currently downloaded packages */ + return gs_plugin_systemd_update_cache (plugin, error); +} + +static gboolean +_systemd_trigger_app (GsPlugin *plugin, + GsApp *app, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + + /* if we can process this online do not require a trigger */ + if (gs_app_get_state (app) != AS_APP_STATE_UPDATABLE) + return TRUE; + + /* only process this app if was created by this plugin */ + if (g_strcmp0 (gs_app_get_management_plugin (app), "packagekit") != 0) + return TRUE; + + /* already in correct state */ + if (priv->is_triggered) + return TRUE; + + /* trigger offline update */ + if (!pk_offline_trigger (PK_OFFLINE_ACTION_REBOOT, + cancellable, error)) { + gs_plugin_packagekit_error_convert (error); + return FALSE; + } + + /* don't rely on the file monitor */ + gs_plugin_systemd_updates_refresh_is_triggered (plugin, cancellable); + + /* success */ + return TRUE; +} + +gboolean +gs_plugin_update (GsPlugin *plugin, + GsAppList *list, + GCancellable *cancellable, + GError **error) +{ + /* any are us? */ + for (guint i = 0; i < gs_app_list_length (list); i++) { + GsApp *app = gs_app_list_index (list, i); + GsAppList *related = gs_app_get_related (app); + + /* try to trigger this app */ + if (!gs_app_has_quirk (app, GS_APP_QUIRK_IS_PROXY)) { + if (!_systemd_trigger_app (plugin, app, cancellable, error)) + return FALSE; + continue; + } + + /* try to trigger each related app */ + for (guint j = 0; j < gs_app_list_length (related); j++) { + GsApp *app_tmp = gs_app_list_index (related, j); + if (!_systemd_trigger_app (plugin, app_tmp, cancellable, error)) + return FALSE; + } + } + + /* success */ + return TRUE; +} + +gboolean +gs_plugin_update_cancel (GsPlugin *plugin, + GsApp *app, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + + /* only process this app if was created by this plugin */ + if (g_strcmp0 (gs_app_get_management_plugin (app), "packagekit") != 0) + return TRUE; + + /* already in correct state */ + if (!priv->is_triggered) + return TRUE; + + /* cancel offline update */ + if (!pk_offline_cancel (NULL, error)) + return FALSE; + + /* don't rely on the file monitor */ + gs_plugin_systemd_updates_refresh_is_triggered (plugin, cancellable); + + /* success! */ + return TRUE; +} + +gboolean +gs_plugin_app_upgrade_trigger (GsPlugin *plugin, + GsApp *app, + GCancellable *cancellable, + GError **error) +{ + /* only process this app if was created by this plugin */ + if (g_strcmp0 (gs_app_get_management_plugin (app), "packagekit") != 0) + return TRUE; + return pk_offline_trigger_upgrade (PK_OFFLINE_ACTION_REBOOT, cancellable, error); +} diff --git a/plugins/packagekit/gs-self-test.c b/plugins/packagekit/gs-self-test.c new file mode 100644 index 0000000..5dbaf0a --- /dev/null +++ b/plugins/packagekit/gs-self-test.c @@ -0,0 +1,279 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2013-2017 Richard Hughes <richard@hughsie.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include "config.h" + +#include "gnome-software-private.h" + +#include "gs-markdown.h" +#include "gs-test.h" + +static void +gs_markdown_func (void) +{ + gchar *text; + const gchar *markdown; + const gchar *markdown_expected; + g_autoptr(GsMarkdown) md = NULL; + + /* get GsMarkdown object */ + md = gs_markdown_new (GS_MARKDOWN_OUTPUT_PANGO); + g_assert (md); + + markdown = "OEMs\n" + "====\n" + " - Bullett\n"; + markdown_expected = + "<big>OEMs</big>\n" + "• Bullett"; + /* markdown (type2 header) */ + text = gs_markdown_parse (md, markdown); + g_assert_cmpstr (text, ==, markdown_expected); + g_free (text); + + /* markdown (autocode) */ + markdown = "this is http://www.hughsie.com/with_spaces_in_url inline link\n"; + markdown_expected = "this is <tt>http://www.hughsie.com/with_spaces_in_url</tt> inline link"; + gs_markdown_set_autocode (md, TRUE); + text = gs_markdown_parse (md, markdown); + g_assert_cmpstr (text, ==, markdown_expected); + g_free (text); + + /* markdown some invalid header */ + markdown = "*** This software is currently in alpha state ***\n"; + markdown_expected = "<b><i> This software is currently in alpha state </b></i>"; + text = gs_markdown_parse (md, markdown); + g_assert_cmpstr (text, ==, markdown_expected); + g_free (text); + + /* markdown (complex1) */ + markdown = " - This is a *very*\n" + " short paragraph\n" + " that is not usual.\n" + " - Another"; + markdown_expected = + "• This is a <i>very</i> short paragraph that is not usual.\n" + "• Another"; + text = gs_markdown_parse (md, markdown); + g_assert_cmpstr (text, ==, markdown_expected); + g_free (text); + + /* markdown (complex1) */ + markdown = "* This is a *very*\n" + " short paragraph\n" + " that is not usual.\n" + "* This is the second\n" + " bullett point.\n" + "* And the third.\n" + " \n" + "* * *\n" + " \n" + "Paragraph one\n" + "isn't __very__ long at all.\n" + "\n" + "Paragraph two\n" + "isn't much better."; + markdown_expected = + "• This is a <i>very</i> short paragraph that is not usual.\n" + "• This is the second bullett point.\n" + "• And the third.\n" + "⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯\n" + "Paragraph one isn't <b>very</b> long at all.\n" + "Paragraph two isn't much better."; + text = gs_markdown_parse (md, markdown); + g_assert_cmpstr (text, ==, markdown_expected); + g_free (text); + + markdown = "This is a spec file description or\n" + "an **update** description in bohdi.\n" + "\n" + "* * *\n" + "# Big title #\n" + "\n" + "The *following* things 'were' fixed:\n" + "- Fix `dave`\n" + "* Fubar update because of \"security\"\n"; + markdown_expected = + "This is a spec file description or an <b>update</b> description in bohdi.\n" + "⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯\n" + "<big>Big title</big>\n" + "The <i>following</i> things 'were' fixed:\n" + "• Fix <tt>dave</tt>\n" + "• Fubar update because of \"security\""; + /* markdown (complex2) */ + text = gs_markdown_parse (md, markdown); + if (g_strcmp0 (text, markdown_expected) == 0) + g_assert_cmpstr (text, ==, markdown_expected); + g_free (text); + + /* markdown (list with spaces) */ + markdown = "* list seporated with spaces -\n" + " first item\n" + "\n" + "* second item\n" + "\n" + "* third item\n"; + markdown_expected = + "• list seporated with spaces - first item\n" + "• second item\n" + "• third item"; + text = gs_markdown_parse (md, markdown); + g_assert_cmpstr (text, ==, markdown_expected); + g_free (text); + + gs_markdown_set_max_lines (md, 1); + + /* markdown (one line limit) */ + markdown = "* list seporated with spaces -\n" + " first item\n" + "* second item\n"; + markdown_expected = + "• list seporated with spaces - first item"; + text = gs_markdown_parse (md, markdown); + g_assert_cmpstr (text, ==, markdown_expected); + g_free (text); + + gs_markdown_set_max_lines (md, 1); + + /* markdown (escaping) */ + markdown = "* list & <spaces>"; + markdown_expected = + "• list & <spaces>"; + text = gs_markdown_parse (md, markdown); + g_assert_cmpstr (text, ==, markdown_expected); + g_free (text); + + /* markdown (URLs) */ + markdown = "this is the http://www.hughsie.com/ coolest site"; + markdown_expected = + "this is the " + "<a href=\"http://www.hughsie.com/\">http://www.hughsie.com/</a>" + " coolest site"; + text = gs_markdown_parse (md, markdown); + g_assert_cmpstr (text, ==, markdown_expected); + g_free (text); + + /* markdown (free text) */ + gs_markdown_set_escape (md, FALSE); + text = gs_markdown_parse (md, "This isn't a present"); + g_assert_cmpstr (text, ==, "This isn't a present"); + g_free (text); + + /* markdown (autotext underscore) */ + text = gs_markdown_parse (md, "This isn't CONFIG_UEVENT_HELPER_PATH present"); + g_assert_cmpstr (text, ==, "This isn't <tt>CONFIG_UEVENT_HELPER_PATH</tt> present"); + g_free (text); + + /* markdown (end of bullett) */ + markdown = "*Thu Mar 12 12:00:00 2009* Dan Walsh <dwalsh@redhat.com> - 2.0.79-1\n" + "- Update to upstream \n" + " * Netlink socket handoff patch from Adam Jackson.\n" + " * AVC caching of compute_create results by Eric Paris.\n" + "\n" + "*Tue Mar 10 12:00:00 2009* Dan Walsh <dwalsh@redhat.com> - 2.0.78-5\n" + "- Add patch from ajax to accellerate X SELinux \n" + "- Update eparis patch\n"; + markdown_expected = + "<i>Thu Mar 12 12:00:00 2009</i> Dan Walsh <tt><dwalsh@redhat.com></tt> - 2.0.79-1\n" + "• Update to upstream\n" + "• Netlink socket handoff patch from Adam Jackson.\n" + "• AVC caching of compute_create results by Eric Paris.\n" + "<i>Tue Mar 10 12:00:00 2009</i> Dan Walsh <tt><dwalsh@redhat.com></tt> - 2.0.78-5\n" + "• Add patch from ajax to accellerate X SELinux\n" + "• Update eparis patch"; + gs_markdown_set_escape (md, TRUE); + gs_markdown_set_max_lines (md, 1024); + text = gs_markdown_parse (md, markdown); + g_assert_cmpstr (text, ==, markdown_expected); + g_free (text); +} + +static void +gs_plugins_packagekit_local_func (GsPluginLoader *plugin_loader) +{ + g_autoptr(GsApp) app = NULL; + g_autoptr(GError) error = NULL; + g_autofree gchar *fn = NULL; + g_autoptr(GFile) file = NULL; + g_autoptr(GsPluginJob) plugin_job = NULL; + + /* no packagekit, abort */ + if (!gs_plugin_loader_get_enabled (plugin_loader, "packagekit-local")) { + g_test_skip ("not enabled"); + return; + } + + /* load local file */ + fn = gs_test_get_filename (TESTDATADIR, "chiron-1.1-1.fc24.x86_64.rpm"); + g_assert (fn != NULL); + file = g_file_new_for_path (fn); + plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_FILE_TO_APP, + "file", file, + NULL); + app = gs_plugin_loader_job_process_app (plugin_loader, plugin_job, NULL, &error); + gs_test_flush_main_context (); + if (g_error_matches (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_NOT_SUPPORTED)) { + g_test_skip ("rpm files not supported"); + return; + } + g_assert_no_error (error); + g_assert (app != NULL); + g_assert_cmpstr (gs_app_get_source_default (app), ==, "chiron"); + g_assert_cmpstr (gs_app_get_url (app, AS_URL_KIND_HOMEPAGE), ==, "http://127.0.0.1/"); + g_assert_cmpstr (gs_app_get_name (app), ==, "chiron"); + g_assert_cmpstr (gs_app_get_version (app), ==, "1.1-1.fc24"); + g_assert_cmpstr (gs_app_get_summary (app), ==, "Single line synopsis"); + g_assert_cmpstr (gs_app_get_description (app), ==, + "This is the first paragraph in the example " + "package spec file.\n\nThis is the second paragraph."); +} + +int +main (int argc, char **argv) +{ + gboolean ret; + g_autoptr(GError) error = NULL; + g_autoptr(GsPluginLoader) plugin_loader = NULL; + const gchar *allowlist[] = { + "packagekit-local", + NULL + }; + + g_test_init (&argc, &argv, +#if GLIB_CHECK_VERSION(2, 60, 0) + G_TEST_OPTION_ISOLATE_DIRS, +#endif + NULL); + g_setenv ("G_MESSAGES_DEBUG", "all", TRUE); + + /* only critical and error are fatal */ + g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL); + + /* generic tests go here */ + g_test_add_func ("/gnome-software/markdown", gs_markdown_func); + + /* we can only load this once per process */ + plugin_loader = gs_plugin_loader_new (); + gs_plugin_loader_add_location (plugin_loader, LOCALPLUGINDIR); + ret = gs_plugin_loader_setup (plugin_loader, + (gchar**) allowlist, + NULL, + NULL, + &error); + g_assert_no_error (error); + g_assert (ret); + + /* plugin tests go here */ + if (!g_file_test ("/run/ostree-booted", G_FILE_TEST_EXISTS)) { + g_test_add_data_func ("/gnome-software/plugins/packagekit/local", + plugin_loader, + (GTestDataFunc) gs_plugins_packagekit_local_func); + } + + return g_test_run (); +} diff --git a/plugins/packagekit/meson.build b/plugins/packagekit/meson.build new file mode 100644 index 0000000..05ae4d4 --- /dev/null +++ b/plugins/packagekit/meson.build @@ -0,0 +1,249 @@ +cargs = ['-DG_LOG_DOMAIN="GsPluginPackageKit"'] +cargs += ['-DLOCALPLUGINDIR="' + meson.current_build_dir() + '"'] +deps = [ + plugin_libs, + packagekit, +] + +if get_option('mogwai') + deps += [mogwai_schedule_client] +endif + +shared_module( + 'gs_plugin_systemd-updates', + sources : [ + 'gs-plugin-systemd-updates.c', + 'gs-packagekit-helper.c', + 'packagekit-common.c', + ], + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + install : true, + install_dir: plugin_dir, + c_args : cargs, + dependencies : deps, + link_with : [ + libgnomesoftware + ] +) + +shared_module( + 'gs_plugin_packagekit', + sources : [ + 'gs-plugin-packagekit.c', + 'gs-packagekit-helper.c', + 'packagekit-common.c', + ], + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + install : true, + install_dir: plugin_dir, + c_args : cargs, + dependencies : deps, + link_with : [ + libgnomesoftware + ] +) + +shared_module( + 'gs_plugin_packagekit-refine', + sources : [ + 'gs-plugin-packagekit-refine.c', + 'gs-markdown.c', + 'gs-packagekit-helper.c', + 'packagekit-common.c', + ], + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + install : true, + install_dir: plugin_dir, + c_args : cargs, + dependencies : [ plugin_libs, packagekit ], + link_with : [ + libgnomesoftware + ] +) + +shared_module( + 'gs_plugin_packagekit-refine-repos', + sources : [ + 'gs-plugin-packagekit-refine-repos.c', + 'gs-packagekit-helper.c', + 'packagekit-common.c', + ], + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + install : true, + install_dir: plugin_dir, + c_args : cargs, + dependencies : [ plugin_libs, packagekit ], + link_with : [ + libgnomesoftware + ] +) + +shared_module( + 'gs_plugin_packagekit-refresh', + sources : [ + 'gs-plugin-packagekit-refresh.c', + 'gs-packagekit-helper.c', + 'packagekit-common.c', + ], + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + install : true, + install_dir: plugin_dir, + c_args : cargs, + dependencies : [ plugin_libs, packagekit ], + link_with : [ + libgnomesoftware + ] +) + +shared_module( + 'gs_plugin_packagekit-local', + sources : [ + 'gs-plugin-packagekit-local.c', + 'gs-packagekit-helper.c', + 'packagekit-common.c', + ], + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + install : true, + install_dir: plugin_dir, + c_args : cargs, + dependencies : [ plugin_libs, packagekit ], + link_with : [ + libgnomesoftware + ] +) + +shared_module( + 'gs_plugin_packagekit-history', + sources : [ + 'gs-plugin-packagekit-history.c', + 'packagekit-common.c', + ], + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + install : true, + install_dir: plugin_dir, + c_args : cargs, + dependencies : [ plugin_libs, packagekit ], + link_with : [ + libgnomesoftware + ] +) + +shared_module( + 'gs_plugin_packagekit-upgrade', + sources : [ + 'gs-plugin-packagekit-upgrade.c', + 'gs-packagekit-helper.c', + 'packagekit-common.c', + ], + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + install : true, + install_dir: plugin_dir, + c_args : cargs, + dependencies : [ plugin_libs, packagekit ], + link_with : [ + libgnomesoftware + ] +) + +shared_module( + 'gs_plugin_packagekit-offline', +sources : [ + 'gs-plugin-packagekit-offline.c', + 'packagekit-common.c', + ], + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + install : true, + install_dir: plugin_dir, + c_args : cargs, + dependencies : [ plugin_libs, packagekit ], + link_with : [ + libgnomesoftware + ] +) + +shared_module( + 'gs_plugin_packagekit-proxy', +sources : 'gs-plugin-packagekit-proxy.c', + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + install : true, + install_dir: plugin_dir, + c_args : cargs, + dependencies : [ plugin_libs, packagekit ], + link_with : [ + libgnomesoftware + ] +) + +shared_module( + 'gs_plugin_packagekit-url-to-app', + sources : [ + 'gs-plugin-packagekit-url-to-app.c', + 'gs-packagekit-helper.c', + 'packagekit-common.c', + ], + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + install : true, + install_dir: plugin_dir, + c_args : cargs, + dependencies : [ plugin_libs, packagekit ], + link_with : [ + libgnomesoftware + ] +) + +if get_option('tests') + cargs += ['-DTESTDATADIR="' + join_paths(meson.current_source_dir(), 'tests') + '"'] + e = executable( + 'gs-self-test-packagekit', + compiled_schemas, + sources : [ + 'gs-markdown.c', + 'gs-self-test.c' + ], + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + dependencies : [ + plugin_libs, + ], + link_with : [ + libgnomesoftware + ], + c_args : cargs, + ) + test('gs-self-test-packagekit', e, suite: ['plugins', 'packagekit'], env: test_env) +endif diff --git a/plugins/packagekit/packagekit-common.c b/plugins/packagekit/packagekit-common.c new file mode 100644 index 0000000..ed77b34 --- /dev/null +++ b/plugins/packagekit/packagekit-common.c @@ -0,0 +1,543 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2013 Richard Hughes <richard@hughsie.com> + * Copyright (C) 2014-2018 Kalev Lember <klember@redhat.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include "config.h" + +#include <packagekit-glib2/packagekit.h> + +#include <gnome-software.h> + +#include "packagekit-common.h" + +GsPluginStatus +packagekit_status_enum_to_plugin_status (PkStatusEnum status) +{ + GsPluginStatus plugin_status = GS_PLUGIN_STATUS_UNKNOWN; + + switch (status) { + case PK_STATUS_ENUM_SETUP: + case PK_STATUS_ENUM_CANCEL: + case PK_STATUS_ENUM_FINISHED: + case PK_STATUS_ENUM_UNKNOWN: + break; + case PK_STATUS_ENUM_WAIT: + case PK_STATUS_ENUM_WAITING_FOR_LOCK: + case PK_STATUS_ENUM_WAITING_FOR_AUTH: + plugin_status = GS_PLUGIN_STATUS_WAITING; + break; + case PK_STATUS_ENUM_LOADING_CACHE: + case PK_STATUS_ENUM_TEST_COMMIT: + case PK_STATUS_ENUM_RUNNING: + case PK_STATUS_ENUM_SIG_CHECK: + case PK_STATUS_ENUM_REFRESH_CACHE: + plugin_status = GS_PLUGIN_STATUS_SETUP; + break; + case PK_STATUS_ENUM_DOWNLOAD: + case PK_STATUS_ENUM_DOWNLOAD_REPOSITORY: + case PK_STATUS_ENUM_DOWNLOAD_PACKAGELIST: + case PK_STATUS_ENUM_DOWNLOAD_FILELIST: + case PK_STATUS_ENUM_DOWNLOAD_CHANGELOG: + case PK_STATUS_ENUM_DOWNLOAD_GROUP: + case PK_STATUS_ENUM_DOWNLOAD_UPDATEINFO: + plugin_status = GS_PLUGIN_STATUS_DOWNLOADING; + break; + case PK_STATUS_ENUM_INSTALL: + case PK_STATUS_ENUM_UPDATE: + plugin_status = GS_PLUGIN_STATUS_INSTALLING; + break; + case PK_STATUS_ENUM_CLEANUP: + case PK_STATUS_ENUM_REMOVE: + plugin_status = GS_PLUGIN_STATUS_REMOVING; + break; + case PK_STATUS_ENUM_REQUEST: + case PK_STATUS_ENUM_QUERY: + case PK_STATUS_ENUM_INFO: + case PK_STATUS_ENUM_DEP_RESOLVE: + plugin_status = GS_PLUGIN_STATUS_QUERYING; + break; + default: + g_warning ("no mapping for %s", + pk_status_enum_to_string (status)); + break; + } + return plugin_status; +} + +gboolean +gs_plugin_packagekit_error_convert (GError **error) +{ + GError *error_tmp; + + if (error == NULL) + return FALSE; + + /* this are allowed for low-level errors */ + if (gs_utils_error_convert_gio (error)) + return TRUE; + + /* not set */ + error_tmp = *error; + if (error_tmp == NULL) + return FALSE; + + /* already correct */ + if (error_tmp->domain == GS_PLUGIN_ERROR) + return TRUE; + + /* get a local version */ + if (error_tmp->domain != PK_CLIENT_ERROR) + return FALSE; + + /* daemon errors */ + if (error_tmp->code <= 0xff) { + switch (error_tmp->code) { + case PK_CLIENT_ERROR_CANNOT_START_DAEMON: + case PK_CLIENT_ERROR_INVALID_FILE: + case PK_CLIENT_ERROR_NOT_SUPPORTED: + error_tmp->code = GS_PLUGIN_ERROR_NOT_SUPPORTED; + break; + /* this is working around a bug in libpackagekit-glib */ + case PK_ERROR_ENUM_TRANSACTION_CANCELLED: + error_tmp->code = GS_PLUGIN_ERROR_CANCELLED; + break; + default: + error_tmp->code = GS_PLUGIN_ERROR_FAILED; + break; + } + + /* backend errors */ + } else { + switch (error_tmp->code - 0xff) { + case PK_ERROR_ENUM_INVALID_PACKAGE_FILE: + case PK_ERROR_ENUM_NOT_SUPPORTED: + case PK_ERROR_ENUM_PACKAGE_INSTALL_BLOCKED: + error_tmp->code = GS_PLUGIN_ERROR_NOT_SUPPORTED; + break; + case PK_ERROR_ENUM_NO_CACHE: + case PK_ERROR_ENUM_NO_NETWORK: + error_tmp->code = GS_PLUGIN_ERROR_NO_NETWORK; + break; + case PK_ERROR_ENUM_PACKAGE_DOWNLOAD_FAILED: + case PK_ERROR_ENUM_NO_MORE_MIRRORS_TO_TRY: + case PK_ERROR_ENUM_CANNOT_FETCH_SOURCES: + error_tmp->code = GS_PLUGIN_ERROR_DOWNLOAD_FAILED; + break; + case PK_ERROR_ENUM_BAD_GPG_SIGNATURE: + case PK_ERROR_ENUM_CANNOT_INSTALL_REPO_UNSIGNED: + case PK_ERROR_ENUM_CANNOT_UPDATE_REPO_UNSIGNED: + case PK_ERROR_ENUM_GPG_FAILURE: + case PK_ERROR_ENUM_MISSING_GPG_SIGNATURE: + case PK_ERROR_ENUM_NO_LICENSE_AGREEMENT: + case PK_ERROR_ENUM_NOT_AUTHORIZED: + case PK_ERROR_ENUM_RESTRICTED_DOWNLOAD: + error_tmp->code = GS_PLUGIN_ERROR_NO_SECURITY; + break; + case PK_ERROR_ENUM_NO_SPACE_ON_DEVICE: + error_tmp->code = GS_PLUGIN_ERROR_NO_SPACE; + break; + case PK_ERROR_ENUM_CANCELLED_PRIORITY: + case PK_ERROR_ENUM_TRANSACTION_CANCELLED: + error_tmp->code = GS_PLUGIN_ERROR_CANCELLED; + break; + default: + error_tmp->code = GS_PLUGIN_ERROR_FAILED; + break; + } + } + error_tmp->domain = GS_PLUGIN_ERROR; + return TRUE; +} + +gboolean +gs_plugin_packagekit_results_valid (PkResults *results, GError **error) +{ + g_autoptr(PkError) error_code = NULL; + + /* method failed? */ + if (results == NULL) { + gs_plugin_packagekit_error_convert (error); + return FALSE; + } + + /* check error code */ + error_code = pk_results_get_error_code (results); + if (error_code != NULL) { + g_set_error_literal (error, + PK_CLIENT_ERROR, + pk_error_get_code (error_code), + pk_error_get_details (error_code)); + gs_plugin_packagekit_error_convert (error); + return FALSE; + } + + /* all good */ + return TRUE; +} + +gboolean +gs_plugin_packagekit_add_results (GsPlugin *plugin, + GsAppList *list, + PkResults *results, + GError **error) +{ + const gchar *package_id; + guint i; + PkPackage *package; + g_autoptr(GHashTable) installed = NULL; + g_autoptr(PkError) error_code = NULL; + g_autoptr(GPtrArray) array_filtered = NULL; + g_autoptr(GPtrArray) array = NULL; + + g_return_val_if_fail (GS_IS_PLUGIN (plugin), FALSE); + g_return_val_if_fail (GS_IS_APP_LIST (list), FALSE); + + /* check error code */ + error_code = pk_results_get_error_code (results); + if (error_code != NULL) { + g_set_error (error, + GS_PLUGIN_ERROR, + GS_PLUGIN_ERROR_INVALID_FORMAT, + "failed to get-packages: %s, %s", + pk_error_enum_to_string (pk_error_get_code (error_code)), + pk_error_get_details (error_code)); + return FALSE; + } + + /* add all installed packages to a hash */ + installed = g_hash_table_new (g_str_hash, g_str_equal); + array = pk_results_get_package_array (results); + for (i = 0; i < array->len; i++) { + package = g_ptr_array_index (array, i); + if (pk_package_get_info (package) != PK_INFO_ENUM_INSTALLED) + continue; + g_hash_table_insert (installed, + (const gpointer) pk_package_get_name (package), + (const gpointer) pk_package_get_id (package)); + } + + /* if the search returns more than one package with the same name, + * ignore everything with that name except the installed package */ + array_filtered = g_ptr_array_new (); + for (i = 0; i < array->len; i++) { + package = g_ptr_array_index (array, i); + package_id = g_hash_table_lookup (installed, pk_package_get_name (package)); + if (pk_package_get_info (package) == PK_INFO_ENUM_INSTALLED || package_id == NULL) { + g_ptr_array_add (array_filtered, package); + } else { + g_debug ("ignoring available %s as installed %s also reported", + pk_package_get_id (package), package_id); + } + } + + /* process packages */ + for (i = 0; i < array_filtered->len; i++) { + g_autoptr(GsApp) app = NULL; + package = g_ptr_array_index (array_filtered, i); + + app = gs_plugin_cache_lookup (plugin, pk_package_get_id (package)); + if (app == NULL) { + app = gs_app_new (NULL); + gs_plugin_packagekit_set_packaging_format (plugin, app); + gs_app_add_source (app, pk_package_get_name (package)); + gs_app_add_source_id (app, pk_package_get_id (package)); + gs_plugin_cache_add (plugin, pk_package_get_id (package), app); + } + gs_app_set_name (app, + GS_APP_QUALITY_LOWEST, + pk_package_get_name (package)); + gs_app_set_summary (app, + GS_APP_QUALITY_LOWEST, + pk_package_get_summary (package)); + gs_app_set_metadata (app, "GnomeSoftware::Creator", + gs_plugin_get_name (plugin)); + gs_app_set_management_plugin (app, "packagekit"); + gs_app_set_version (app, pk_package_get_version (package)); + switch (pk_package_get_info (package)) { + case PK_INFO_ENUM_INSTALLED: + gs_app_set_state (app, AS_APP_STATE_INSTALLED); + break; + case PK_INFO_ENUM_AVAILABLE: + gs_app_set_state (app, AS_APP_STATE_AVAILABLE); + break; + case PK_INFO_ENUM_INSTALLING: + case PK_INFO_ENUM_UPDATING: + case PK_INFO_ENUM_DOWNGRADING: + case PK_INFO_ENUM_OBSOLETING: + case PK_INFO_ENUM_UNTRUSTED: + break; + case PK_INFO_ENUM_UNAVAILABLE: + case PK_INFO_ENUM_REMOVING: + gs_app_set_state (app, AS_APP_STATE_UNAVAILABLE); + break; + default: + gs_app_set_state (app, AS_APP_STATE_UNKNOWN); + g_warning ("unknown info state of %s", + pk_info_enum_to_string (pk_package_get_info (package))); + } + gs_app_set_kind (app, AS_APP_KIND_GENERIC); + gs_app_set_bundle_kind (app, AS_BUNDLE_KIND_PACKAGE); + gs_app_list_add (list, app); + } + return TRUE; +} + +void +gs_plugin_packagekit_resolve_packages_app (GsPlugin *plugin, + GPtrArray *packages, + GsApp *app) +{ + GPtrArray *sources; + PkPackage *package; + const gchar *pkgname; + guint i, j; + guint number_available = 0; + guint number_installed = 0; + + /* find any packages that match the package name */ + number_installed = 0; + number_available = 0; + sources = gs_app_get_sources (app); + for (j = 0; j < sources->len; j++) { + pkgname = g_ptr_array_index (sources, j); + for (i = 0; i < packages->len; i++) { + package = g_ptr_array_index (packages, i); + if (g_strcmp0 (pk_package_get_name (package), pkgname) == 0) { + gs_plugin_packagekit_set_metadata_from_package (plugin, app, package); + switch (pk_package_get_info (package)) { + case PK_INFO_ENUM_INSTALLED: + number_installed++; + break; + case PK_INFO_ENUM_AVAILABLE: + number_available++; + break; + case PK_INFO_ENUM_UNAVAILABLE: + number_available++; + break; + default: + /* should we expect anything else? */ + break; + } + } + } + } + + /* if *all* the source packages for the app are installed then the + * application is considered completely installed */ + if (number_installed == sources->len && number_available == 0) { + if (gs_app_get_state (app) == AS_APP_STATE_UNKNOWN) + gs_app_set_state (app, AS_APP_STATE_INSTALLED); + } else if (number_installed + number_available == sources->len) { + /* if all the source packages are installed and all the rest + * of the packages are available then the app is available */ + if (gs_app_get_state (app) == AS_APP_STATE_UNKNOWN) + gs_app_set_state (app, AS_APP_STATE_AVAILABLE); + } else if (number_installed + number_available > sources->len) { + /* we have more packages returned than source packages */ + gs_app_set_state (app, AS_APP_STATE_UNKNOWN); + gs_app_set_state (app, AS_APP_STATE_UPDATABLE); + } else if (number_installed + number_available < sources->len) { + g_autofree gchar *tmp = NULL; + /* we have less packages returned than source packages */ + tmp = gs_app_to_string (app); + g_debug ("Failed to find all packages for:\n%s", tmp); + gs_app_set_state (app, AS_APP_STATE_UNKNOWN); + } +} + +void +gs_plugin_packagekit_set_metadata_from_package (GsPlugin *plugin, + GsApp *app, + PkPackage *package) +{ + const gchar *data; + + gs_plugin_packagekit_set_packaging_format (plugin, app); + gs_app_set_management_plugin (app, "packagekit"); + gs_app_add_source (app, pk_package_get_name (package)); + gs_app_add_source_id (app, pk_package_get_id (package)); + + /* set origin */ + if (gs_app_get_origin (app) == NULL) { + data = pk_package_get_data (package); + if (g_str_has_prefix (data, "installed:")) + data += 10; + gs_app_set_origin (app, data); + } + + /* set unavailable state */ + if (pk_package_get_info (package) == PK_INFO_ENUM_UNAVAILABLE) { + gs_app_set_state (app, AS_APP_STATE_UNAVAILABLE); + if (gs_app_get_size_installed (app) == 0) + gs_app_set_size_installed (app, GS_APP_SIZE_UNKNOWABLE); + if (gs_app_get_size_download (app) == 0) + gs_app_set_size_download (app, GS_APP_SIZE_UNKNOWABLE); + } + if (gs_app_get_version (app) == NULL) + gs_app_set_version (app, pk_package_get_version (package)); + gs_app_set_name (app, + GS_APP_QUALITY_LOWEST, + pk_package_get_name (package)); + gs_app_set_summary (app, + GS_APP_QUALITY_LOWEST, + pk_package_get_summary (package)); +} + +/* Hash functions which compare PkPackageIds on NAME, VERSION and ARCH, but not DATA. + * This is because some backends do not append the origin. + * + * Borrowing some implementation details from pk-package-id.c, a package + * ID is a semicolon-separated list of NAME;[VERSION];[ARCH];[DATA], + * so a comparison which ignores DATA is just a strncmp() up to and + * including the final semicolon. + * + * Doing it this way means zero allocations, which allows the hash and + * equality functions to be fast. This is important when dealing with + * large refine() package lists. + * + * The hash and equality functions assume that the IDs they are passed are + * valid. */ +static guint +package_id_hash (gconstpointer key) +{ + const gchar *package_id = key; + gchar *no_data; + gsize i, last_semicolon = 0; + + /* find the last semicolon, which starts the DATA section */ + for (i = 0; package_id[i] != '\0'; i++) { + if (package_id[i] == ';') + last_semicolon = i; + } + + /* exit early if the DATA section was empty */ + if (last_semicolon + 1 == i) + return g_str_hash (package_id); + + /* extract up to (and including) the last semicolon into a local string */ + no_data = g_alloca (last_semicolon + 2); + memcpy (no_data, package_id, last_semicolon + 1); + no_data[last_semicolon + 1] = '\0'; + + return g_str_hash (no_data); +} + +static gboolean +package_id_equal (gconstpointer a, + gconstpointer b) +{ + const gchar *package_id_a = a; + const gchar *package_id_b = b; + gsize i, n_semicolons = 0; + + /* compare up to and including the last semicolon */ + for (i = 0; package_id_a[i] != '\0' && package_id_b[i] != '\0'; i++) { + if (package_id_a[i] != package_id_b[i]) + return FALSE; + if (package_id_a[i] == ';') + n_semicolons++; + if (n_semicolons == 4) + return TRUE; + } + + return package_id_a[i] == package_id_b[i]; +} + +GHashTable * +gs_plugin_packagekit_details_array_to_hash (GPtrArray *array) +{ + g_autoptr(GHashTable) details_collection = NULL; + + details_collection = g_hash_table_new_full (package_id_hash, package_id_equal, + NULL, NULL); + + for (gsize i = 0; i < array->len; i++) { + PkDetails *details = g_ptr_array_index (array, i); + g_hash_table_insert (details_collection, + pk_details_get_package_id (details), + details); + } + + return g_steal_pointer (&details_collection); +} + +void +gs_plugin_packagekit_refine_details_app (GsPlugin *plugin, + GHashTable *details_collection, + GsApp *app) +{ + GPtrArray *source_ids; + PkDetails *details; + const gchar *package_id; + guint j; + guint64 size = 0; + + /* @source_ids can have as many as 200 elements (google-noto); typically + * it has 1 or 2 + * + * @details_collection is typically a large list of apps in the + * repository, on the order of 400 or 700 apps */ + source_ids = gs_app_get_source_ids (app); + for (j = 0; j < source_ids->len; j++) { + package_id = g_ptr_array_index (source_ids, j); + details = g_hash_table_lookup (details_collection, package_id); + if (details == NULL) + continue; + + if (gs_app_get_license (app) == NULL) { + g_autofree gchar *license_spdx = NULL; + license_spdx = as_utils_license_to_spdx (pk_details_get_license (details)); + if (license_spdx != NULL) { + gs_app_set_license (app, + GS_APP_QUALITY_LOWEST, + license_spdx); + } + } + if (gs_app_get_url (app, AS_URL_KIND_HOMEPAGE) == NULL) { + gs_app_set_url (app, + AS_URL_KIND_HOMEPAGE, + pk_details_get_url (details)); + } + if (gs_app_get_description (app) == NULL) { + gs_app_set_description (app, + GS_APP_QUALITY_LOWEST, + pk_details_get_description (details)); + } + size += pk_details_get_size (details); + } + + /* the size is the size of all sources */ + if (gs_app_get_state (app) == AS_APP_STATE_UPDATABLE) { + if (size > 0 && gs_app_get_size_installed (app) == 0) + gs_app_set_size_installed (app, size); + if (size > 0 && gs_app_get_size_download (app) == 0) + gs_app_set_size_download (app, size); + } else if (gs_app_is_installed (app)) { + if (gs_app_get_size_download (app) == 0) + gs_app_set_size_download (app, GS_APP_SIZE_UNKNOWABLE); + if (size > 0 && gs_app_get_size_installed (app) == 0) + gs_app_set_size_installed (app, size); + } else { + if (gs_app_get_size_installed (app) == 0) + gs_app_set_size_installed (app, GS_APP_SIZE_UNKNOWABLE); + if (size > 0 && gs_app_get_size_download (app) == 0) + gs_app_set_size_download (app, size); + } +} + +void +gs_plugin_packagekit_set_packaging_format (GsPlugin *plugin, GsApp *app) +{ + if (gs_plugin_check_distro_id (plugin, "fedora") || + gs_plugin_check_distro_id (plugin, "rhel")) { + gs_app_set_metadata (app, "GnomeSoftware::PackagingFormat", "RPM"); + } else if (gs_plugin_check_distro_id (plugin, "debian") || + gs_plugin_check_distro_id (plugin, "ubuntu")) { + gs_app_set_metadata (app, "GnomeSoftware::PackagingFormat", "deb"); + } +} diff --git a/plugins/packagekit/packagekit-common.h b/plugins/packagekit/packagekit-common.h new file mode 100644 index 0000000..9f52368 --- /dev/null +++ b/plugins/packagekit/packagekit-common.h @@ -0,0 +1,40 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2013 Richard Hughes <richard@hughsie.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#pragma once + +#include <glib.h> +#include <gnome-software.h> + +#include <packagekit-glib2/packagekit.h> + +G_BEGIN_DECLS + +GsPluginStatus packagekit_status_enum_to_plugin_status (PkStatusEnum status); + +gboolean gs_plugin_packagekit_add_results (GsPlugin *plugin, + GsAppList *list, + PkResults *results, + GError **error); +gboolean gs_plugin_packagekit_error_convert (GError **error); +gboolean gs_plugin_packagekit_results_valid (PkResults *results, + GError **error); +void gs_plugin_packagekit_resolve_packages_app (GsPlugin *plugin, + GPtrArray *packages, + GsApp *app); +void gs_plugin_packagekit_set_metadata_from_package (GsPlugin *plugin, + GsApp *app, + PkPackage *package); +GHashTable * gs_plugin_packagekit_details_array_to_hash (GPtrArray *array); +void gs_plugin_packagekit_refine_details_app (GsPlugin *plugin, + GHashTable *details_collection, + GsApp *app); +void gs_plugin_packagekit_set_packaging_format (GsPlugin *plugin, + GsApp *app); + +G_END_DECLS diff --git a/plugins/packagekit/tests/build-rpm.sh b/plugins/packagekit/tests/build-rpm.sh new file mode 100755 index 0000000..90a4163 --- /dev/null +++ b/plugins/packagekit/tests/build-rpm.sh @@ -0,0 +1,2 @@ +rpmbuild -ba chiron.spec +cp ~/rpmbuild/RPMS/*/chiron*.rpm . diff --git a/plugins/packagekit/tests/chiron-1.1-1.fc24.x86_64.rpm b/plugins/packagekit/tests/chiron-1.1-1.fc24.x86_64.rpm Binary files differnew file mode 100644 index 0000000..1453f48 --- /dev/null +++ b/plugins/packagekit/tests/chiron-1.1-1.fc24.x86_64.rpm diff --git a/plugins/packagekit/tests/chiron.spec b/plugins/packagekit/tests/chiron.spec new file mode 100644 index 0000000..6cbba7e --- /dev/null +++ b/plugins/packagekit/tests/chiron.spec @@ -0,0 +1,22 @@ +Summary: Single line synopsis +Name: chiron +Version: 1.1 +Release: 1%{?dist} +URL: http://127.0.0.1/ +License: GPLv2+ + +%description +This is the first paragraph in the example package spec file. + +This is the second paragraph. + +%install +mkdir -p $RPM_BUILD_ROOT/%{_bindir} +touch $RPM_BUILD_ROOT/%{_bindir}/chiron + +%files +%{_bindir}/chiron + +%changelog +* Tue Apr 26 2016 Richard Hughes <richard@hughsie.com> - 1.1-1 +- Initial version |