summaryrefslogtreecommitdiffstats
path: root/gnome-autoar/autoar-private.c
blob: d80e5e702ea60c9e00493bc5ec62cb63a852d337 (plain) (blame)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* vim: set sw=2 ts=2 sts=2 et: */
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * autoar-private.c
 * Some common functions used in several classes of gnome-autoar
 * This file does NOT declare any new classes and it should NOT
 * be used outside the library itself!
 *
 * Copyright (C) 2013, 2014  Ting-Wei Lan
 *
 * 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 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 */

#include "config.h"
#include "autoar-private.h"

#include "autoar-misc.h"

#include <glib.h>
#include <gobject/gvaluecollector.h>
#include <string.h>

/**
 * SECTION:autoar-common
 * @Short_description: Miscellaneous functions used by gnome-autoar
 * @Title: autoar-common
 * @Include: gnome-autoar/autoar.h
 *
 * Public utility functions used internally by other gnome-autoar functions.
 **/

typedef struct _AutoarCommonSignalData AutoarCommonSignalData;

struct _AutoarCommonSignalData
{
  GValue instance_and_params[3]; /* Maximum number of parameters + 1 */
  gssize used_values; /* Number of GValues to be unset */
  guint signal_id;
  GQuark detail;
};

/**
 * autoar_common_get_filename_extension:
 * @filename: a filename
 *
 * Gets the extension of a filename.
 *
 * Returns: (transfer none): a pointer to the extension of the filename
 **/
G_GNUC_INTERNAL char*
autoar_common_get_filename_extension (const char *filename)
{
  char *dot_location;

  dot_location = strrchr (filename, '.');
  if (dot_location == NULL || dot_location == filename) {
    return (char*)filename;
  }

  if (dot_location - 4 > filename && strncmp (dot_location - 4, ".tar", 4) == 0)
    dot_location -= 4;
  else if (dot_location - 5 > filename && strncmp (dot_location - 5, ".cpio", 5) == 0)
    dot_location -= 5;

  return dot_location;
}

/**
 * autoar_common_get_basename_remove_extension:
 * @filename: a filename
 *
 * Gets the basename of a path without its file name extension.
 *
 * Returns: (transfer full): a new filename without extension. Free the
 * returned string with g_free().
 **/
G_GNUC_INTERNAL char*
autoar_common_get_basename_remove_extension (const char *filename)
{
  char *dot_location;
  char *basename;

  if (filename == NULL) {
    return NULL;
  }

  /* filename must not be directory, so we do not get a bad basename. */
  basename = g_path_get_basename (filename);

  dot_location = autoar_common_get_filename_extension (basename);
  if (dot_location != basename)
    *dot_location = '\0';

  g_debug ("autoar_common_get_basename_remove_extension: %s => %s",
           filename,
           basename);
  return basename;
}

static void
autoar_common_signal_data_free (AutoarCommonSignalData *signal_data)
{
  int i;

  for (i = 0; i < signal_data->used_values; i++)
    g_value_unset (signal_data->instance_and_params + i);

  g_free (signal_data);
}

static gboolean
autoar_common_g_signal_emit_main_context (void *data)
{
  AutoarCommonSignalData *signal_data = data;
  g_signal_emitv (signal_data->instance_and_params,
                  signal_data->signal_id,
                  signal_data->detail,
                  NULL);
  autoar_common_signal_data_free (signal_data);
  return FALSE;
}

/**
 * autoar_common_g_signal_emit:
 * @instance: the instance the signal is being emitted on.
 * @in_thread: %TRUE if you are not call this function inside the main thread.
 * @signal_id: the signal id
 * @detail: the detail
 * @...: parameters to be passed to the signal.
 *
 * This is a wrapper for g_signal_emit(). If @in_thread is %FALSE, this
 * function is the same as g_signal_emit(). If @in_thread is %TRUE, the
 * signal will be emitted from the main thread. This function will send
 * the signal emission job via g_main_context_invoke(), but it does not
 * wait for the signal emission job to be completed. Hence, the signal
 * may emitted after autoar_common_g_signal_emit() is returned.
 **/
G_GNUC_INTERNAL void
autoar_common_g_signal_emit (gpointer instance,
                             gboolean in_thread,
                             guint signal_id,
                             GQuark detail,
                             ...)
{
  va_list ap;

  va_start (ap, detail);
  if (in_thread) {
    int i;
    gchar *error;
    GSignalQuery query;
    AutoarCommonSignalData *data;

    error = NULL;
    data = g_new0 (AutoarCommonSignalData, 1);
    data->signal_id = signal_id;
    data->detail = detail;
    data->used_values = 1;
    g_value_init (data->instance_and_params, G_TYPE_FROM_INSTANCE (instance));
    g_value_set_instance (data->instance_and_params, instance);

    g_signal_query (signal_id, &query);
    if (query.signal_id == 0) {
      autoar_common_signal_data_free (data);
      va_end (ap);
      return;
    }

    for (i = 0; i < query.n_params; i++) {
      G_VALUE_COLLECT_INIT (data->instance_and_params + i + 1,
                            query.param_types[i],
                            ap,
                            0,
                            &error);
      if (error != NULL)
        break;
      data->used_values++;
    }

    if (error == NULL) {
      g_main_context_invoke (NULL, autoar_common_g_signal_emit_main_context, data);
    } else {
      autoar_common_signal_data_free (data);
      g_debug ("G_VALUE_COLLECT_INIT: Error: %s", error);
      g_free (error);
      va_end (ap);
      return;
    }
  } else {
    g_signal_emit_valist (instance, signal_id, detail, ap);
  }
  va_end (ap);
}

/**
 * autoar_common_g_object_unref:
 * @object: a #GObject
 *
 * This is a wrapper for g_object_unref(). If @object is %NULL, this function
 * does nothing. Otherwise, it will call g_object_unref() on the @object.
 **/
G_GNUC_INTERNAL void
autoar_common_g_object_unref (gpointer object)
{
  if (object != NULL)
    g_object_unref (object);
}

/**
 * autoar_common_g_error_new_a:
 * @a: a archive object
 * @pathname: the file which causes error, or %NULL
 *
 * Creates a new #GError with error messages got from libarchive.
 *
 * Returns: (transfer full): a #GError. Free with g_error_free().
 **/
G_GNUC_INTERNAL GError*
autoar_common_g_error_new_a (struct archive *a,
                             const char *pathname)
{
  GError *newerror;
  newerror = g_error_new (AUTOAR_LIBARCHIVE_ERROR,
                          archive_errno (a),
                          "%s%s%s%s",
                          pathname != NULL ? "\'" : "",
                          pathname != NULL ? pathname : "",
                          pathname != NULL ? "\': " : "",
                          archive_error_string (a));
  return newerror;
}

/**
 * autoar_common_g_error_new_a_entry:
 * @a: a archive object
 * @entry: a archive_entry object
 *
 * Gets pathname from @entry and call autoar_common_g_error_new_a().
 *
 * Returns: (transfer full): a #GError. Free with g_error_free().
 **/
G_GNUC_INTERNAL GError*
autoar_common_g_error_new_a_entry (struct archive *a,
                                   struct archive_entry *entry)
{
  return autoar_common_g_error_new_a (a, archive_entry_pathname (entry));
}

/**
 * autoar_common_g_file_get_name:
 * @file: a #GFile
 *
 * Gets a string represents the @file. It will be the path of @file if
 * available. Otherwise, it will be the URI of @file.
 *
 * Returns: (transfer full): a string represents the file. Free the string
 * with g_free().
 **/
G_GNUC_INTERNAL char*
autoar_common_g_file_get_name (GFile *file)
{
  char *name;
  name = g_file_get_path (file);
  if (name == NULL)
    name = g_file_get_uri (file);
  return name;
}