<commit_before>/*
 * Copyright © 2022 <NAME>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       <NAME> <<EMAIL>>
 */

#include "config.h"

#ifdef HAVE_GLIB_2_66

#include <glib.h>
#include <gio/gdesktopappinfo.h>

#include "dynamic-launcher.h"

static char *
find_renamed_app_id (const char *old_app_id)
{
  g_autofree char *renamed_to = NULL;
  GList *app_infos = g_app_info_get_all ();
  for (GList *l = app_infos; l; l = l->next)
    {
      GDesktopAppInfo *info = l->data;
      g_auto(GStrv) renamed_from = NULL;
      renamed_from = g_desktop_app_info_get_string_list (info, "X-Flatpak-RenamedFrom", NULL);
      if (renamed_from == NULL)
        continue;
      if (!g_strv_contains ((const char * const *)renamed_from, old_app_id))
        continue;

      renamed_to = g_desktop_app_info_get_string (info, "X-Flatpak");
      break;
    }

  g_list_free_full (app_infos, g_object_unref);
  return g_steal_pointer (&renamed_to);
}

/*
 * It's possible an app was renamed using Flatpak's end-of-life-rebase
 * mechanism, and either (a) the app was installed system-wide and the update
 * was applied by another user, so the migration for this user has to happen
 * when this binary runs at the start of the session, or (b) the version of
 * Flatpak is not new enough for the migration of the launchers to be handled
 * by Flatpak, so x-d-p has to do it.
 *
 * This function also handles deleting the launchers in case the parent app has
 * been uninstalled.
 */
static gboolean
migrate_renamed_app_launchers (void)
{
  g_autoptr(GFile) desktop_dir = NULL;<commit_msg>rewrite-launchers: Fix X-Flatpak-RenamedFrom check<commit_after>/*
 * Copyright © 2022 <NAME>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       <NAME> <<EMAIL>>
 */

#include "config.h"

#ifdef HAVE_GLIB_2_66

#include <glib.h>
#include <gio/gdesktopappinfo.h>

#include "dynamic-launcher.h"

static char *
find_renamed_app_id (const char *old_app_id)
{
  g_autofree char *renamed_to = NULL;
  g_autofree char *desktop_id = NULL;

  desktop_id = g_strconcat (old_app_id, ".desktop", NULL);

  GList *app_infos = g_app_info_get_all ();
  for (GList *l = app_infos; l; l = l->next)
    {
      GDesktopAppInfo *info = l->data;
      g_auto(GStrv) renamed_from = NULL;
      renamed_from = g_desktop_app_info_get_string_list (info, "X-Flatpak-RenamedFrom", NULL);
      if (renamed_from == NULL)
        continue;
      if (!g_strv_contains ((const char * const *)renamed_from, desktop_id))
        continue;

      renamed_to = g_desktop_app_info_get_string (info, "X-Flatpak");
      break;
    }

  g_list_free_full (app_infos, g_object_unref);
  return g_steal_pointer (&renamed_to);
}

/*
 * It's possible an app was renamed using Flatpak's end-of-life-rebase
 * mechanism, and either (a) the app was installed system-wide and the update
 * was applied by another user, so the migration for this user has to happen
 * when this binary runs at the start of the session, or (b) the version of
 * Flatpak is not new enough for the migration of the launchers to be handled
 * by Flatpak, so x-d-p has to do it.
 *
 * This function also handles deleting the launchers in case the parent app has
 * been uninstalled.
 */
static gboolean
migrate_renamed_app_launchers (void)
{
  g_autoptr(GFile) desktop_dir = NULL;
{'fraction_non_alphanumeric': 0.08708971553610503, 'fraction_numerical': 0.00350109409190372, 'mean_word_length': 3.7367875647668396, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4590961', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1460, 'n_words': 547}