-
Notifications
You must be signed in to change notification settings - Fork 7
/
gs-plugin-eos.c
211 lines (176 loc) · 5.88 KB
/
gs-plugin-eos.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2016-2018 Endless Mobile, Inc
*
* Authors:
* Joaquim Rocha <jrocha@endlessm.com>
*
* Licensed under the GNU General Public License Version 2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <config.h>
#include <gnome-software.h>
#include <glib/gi18n.h>
#include <gs-plugin.h>
#include "gs-plugin-eos.h"
#define METADATA_SYS_DESKTOP_FILE "EndlessOS::system-desktop-file"
/*
* SECTION:
* Plugin to improve GNOME Software integration in the EOS desktop.
*
* This plugin runs entirely in the main thread.
*/
struct _GsPluginEos
{
GsPlugin parent;
};
G_DEFINE_TYPE (GsPluginEos, gs_plugin_eos, GS_TYPE_PLUGIN)
static void
gs_plugin_eos_init (GsPluginEos *self)
{
/* let the flatpak plugin run first so we deal with the apps
* in a more complete/refined state */
gs_plugin_add_rule (GS_PLUGIN (self), GS_PLUGIN_RULE_RUN_AFTER, "flatpak");
}
static gboolean
app_is_flatpak (GsApp *app)
{
return gs_app_get_bundle_kind (app) == AS_BUNDLE_KIND_FLATPAK;
}
/* Copy of the implementation of gs_flatpak_app_get_ref_name(). */
static const gchar *
app_get_flatpak_ref_name (GsApp *app)
{
return gs_app_get_metadata_item (app, "flatpak::RefName");
}
static char *
get_desktop_file_id (GsApp *app)
{
const char *desktop_file_id =
gs_app_get_metadata_item (app, METADATA_SYS_DESKTOP_FILE);
if (!desktop_file_id) {
if (app_is_flatpak (app)) {
/* ensure we add the .desktop suffix to the app's ref name
* since in Flatpak the app ID can have that suffix already
* or not, depending on how the appdata has been generated */
const char *ref_name = app_get_flatpak_ref_name (app);
return g_strconcat (ref_name, ".desktop", NULL);
}
/* just use the app ID if this is not a Flatpak app */
desktop_file_id = gs_app_get_id (app);
}
g_assert (desktop_file_id != NULL);
return g_strdup (desktop_file_id);
}
void
gs_plugin_adopt_app (GsPlugin *plugin, GsApp *app)
{
if (app_is_flatpak (app))
return;
gs_app_set_management_plugin (app, plugin);
}
static void
refine_core_app (GsApp *app)
{
if (app_is_flatpak (app) ||
(gs_app_get_scope (app) == AS_COMPONENT_SCOPE_UNKNOWN))
return;
if (gs_app_get_kind (app) == AS_COMPONENT_KIND_OPERATING_SYSTEM ||
gs_app_get_kind (app) == AS_COMPONENT_KIND_WEB_APP)
return;
/* Hide non-installed apt packages, as they can’t actually be installed.
* The installed ones are pre-installed in the image, and can’t be
* removed. We only allow flatpaks to be removed. */
if (!gs_app_is_installed (app)) {
gs_app_add_quirk (app, GS_APP_QUIRK_HIDE_EVERYWHERE);
} else {
gs_app_add_quirk (app, GS_APP_QUIRK_COMPULSORY);
}
}
static void
gs_plugin_eos_refine_async (GsPlugin *plugin,
GsAppList *list,
GsPluginRefineFlags flags,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
g_autoptr(GTask) task = NULL;
task = g_task_new (plugin, cancellable, callback, user_data);
g_task_set_source_tag (task, gs_plugin_eos_refine_async);
for (guint i = 0; i < gs_app_list_length (list); i++) {
GsApp *app = gs_app_list_index (list, i);
refine_core_app (app);
}
g_task_return_boolean (task, TRUE);
}
static gboolean
gs_plugin_eos_refine_finish (GsPlugin *plugin,
GAsyncResult *result,
GError **error)
{
return g_task_propagate_boolean (G_TASK (result), error);
}
static gboolean
launch_with_sys_desktop_file (GsApp *app,
GError **error)
{
GdkDisplay *display;
g_autoptr(GAppLaunchContext) context = NULL;
g_autofree char *desktop_file_id = get_desktop_file_id (app);
g_autoptr(GDesktopAppInfo) app_info =
gs_utils_get_desktop_app_info (desktop_file_id);
g_autoptr(GError) local_error = NULL;
gboolean ret;
display = gdk_display_get_default ();
context = G_APP_LAUNCH_CONTEXT (gdk_display_get_app_launch_context (display));
ret = g_app_info_launch (G_APP_INFO (app_info), NULL, context, &local_error);
if (!ret) {
g_warning ("Could not launch %s: %s", gs_app_get_unique_id (app),
local_error->message);
g_set_error (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_FAILED,
_("Could not launch this application."));
}
return ret;
}
gboolean
gs_plugin_launch (GsPlugin *plugin,
GsApp *app,
GCancellable *cancellable,
GError **error)
{
/* if the app is one of the system ones, we simply launch it through the
* plugin's app launcher */
if (gs_app_has_quirk (app, GS_APP_QUIRK_COMPULSORY) &&
!app_is_flatpak (app))
return gs_plugin_app_launch (plugin, app, error);
/* for apps that have a special desktop file (e.g. Google Chrome) */
if (gs_app_get_metadata_item (app, METADATA_SYS_DESKTOP_FILE))
return launch_with_sys_desktop_file (app, error);
return TRUE;
}
static void
gs_plugin_eos_class_init (GsPluginEosClass *klass)
{
GsPluginClass *plugin_class = GS_PLUGIN_CLASS (klass);
plugin_class->refine_async = gs_plugin_eos_refine_async;
plugin_class->refine_finish = gs_plugin_eos_refine_finish;
}
GType
gs_plugin_query_type (void)
{
return GS_TYPE_PLUGIN_EOS;
}