summaryrefslogtreecommitdiff
path: root/src/app.c
blob: bb0fa8a597bef4989c1ad8cd9dd5706bc1c07562 (plain)
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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * app.c - fichier d'entrée du programme
 *
 * Copyright (C) 2024 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chrysalide is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include <assert.h>
#include <fcntl.h>
#include <gio/gdesktopappinfo.h>
#include <gio/gio.h>
#include <sys/auxv.h>


#include "app.h"
#include "common/io.h"
#include "common/xdg.h"
#include "core/logs.h"
#include "glibext/helpers.h"



#define CHRYSALIDE_APP_ID "re.chrysalide.framework.gui" // REMME



/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Installe au besoin une définition locale pour le système.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void ensure_wm_icon_and_name(void)
{
    GDesktopAppInfo *info;                  /* Information du système      */
    GKeyFile *kfile;                        /* Définition d'application    */
    unsigned long exec_path;                /* Chemin du programme         */
    char *filename;                         /* Nom de fichier à écrire     */
    GBytes *res;                            /* Données brutes d'une image  */
    gsize size;                             /* Taille de ces données       */
    gconstpointer data;                     /* Pointeur vers les données   */
    int fd;                                 /* Flux ouvert en écriture     */

    /* Evaluation du besoin */

    info = g_desktop_app_info_new(CHRYSALIDE_APP_ID ".desktop");

    /**
     * Si l'exécutable n'est pas valide (inconnu de $PATH),
     * la variable info n'est pas initialisée.
     */

    if (info != NULL)
    {
        unref_object(info);
        goto done;
    }

    /* Mise en place d'une définition d'application */

    exec_path = getauxval(AT_EXECFN);
    assert(exec_path != 0);

    kfile = g_key_file_new();

    g_key_file_set_string(kfile, "Desktop Entry", "Name", "Chrysalide");
    g_key_file_set_string(kfile, "Desktop Entry", "Comment[fr]", "Cadriciel de rétronception ciblant principalement les systèmes embarqués");
    g_key_file_set_string(kfile, "Desktop Entry", "Comment", "Reverse Engineering Framework focused on embedded systems");
    g_key_file_set_string(kfile, "Desktop Entry", "Type", "Application");
    g_key_file_set_string(kfile, "Desktop Entry", "Exec", (const char *)exec_path);
    g_key_file_set_string(kfile, "Desktop Entry", "Icon", "chrysalide-logo");
    g_key_file_set_string(kfile, "Desktop Entry", "StartupNotify", "true");
    g_key_file_set_string(kfile, "Desktop Entry", "MimeType", "application/vnd.android.package-archive");

    filename = get_xdg_data_dir("applications/re.chrysalide.framework.gui.desktop", true);

    g_key_file_save_to_file(kfile, filename, NULL);

    free(filename);

    g_key_file_free(kfile);

    /* Ecriture de l'image */

    res = g_resources_lookup_data("/re/chrysalide/framework/images/chrysalide-logo.svg",
                                  G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);
    assert(res != NULL);

    data = g_bytes_get_data(res, &size);

    filename = get_xdg_data_dir("icons/hicolor/scalable/apps/chrysalide-logo.svg", true);

    fd = open(filename, O_WRONLY | O_CREAT);

    if (fd == -1)
        LOG_ERROR_N("open");

    else
    {
        safe_write(fd, data, size);
        close(fd);
    }

    free(filename);

    g_bytes_unref(res);

    /* Sortie */

 done:

    ;

}