~vrruiz/libunity-webapps/quantal-messaging

« back to all changes in this revision

Viewing changes to src/context-daemon/unity-webapps-application-info.c

  • Committer: Ken VanDine
  • Date: 2012-07-17 16:21:37 UTC
  • mfrom: (260.241.66 libunity-webapps)
  • Revision ID: ken.vandine@canonical.com-20120717162137-uukqb8y1g6orr4r1
Tags: 1.7.0-0quantal1
releasing version 1.7.0-0quantal1

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.";
19
19
 */
20
20
 
 
21
#include <string.h>
 
22
#include <gio/gio.h>
 
23
#include <libsoup/soup.h>
 
24
 
21
25
#include "unity-webapps-application-info.h"
22
26
 
23
27
#include "unity-webapps-dirs.h"
46
50
 
47
51
#define UNITY_WEBAPPS_APPLICATION_INFO_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), UNITY_WEBAPPS_TYPE_APPLICATION_INFO, UnityWebappsApplicationInfoPrivate))
48
52
 
49
 
#define UNITY_WEBAPPS_APPLICATION_INFO_VALID_CANONICAL_NAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
50
 
 
51
53
static void
52
54
unity_webapps_application_info_finalize (GObject *object)
53
55
{
216
218
static gchar *
217
219
unity_webapps_application_info_canonicalize_string (const gchar *str)
218
220
{
219
 
  gchar *canonicalized, **components;
220
 
 
221
 
  canonicalized = g_strdup (str);
222
 
  g_strcanon (canonicalized, UNITY_WEBAPPS_APPLICATION_INFO_VALID_CANONICAL_NAME_CHARACTERS, '%');
223
 
 
224
 
  components = g_strsplit (canonicalized, "%", 0);
225
 
 
226
 
  g_free (canonicalized);
227
 
 
228
 
  canonicalized = g_strjoinv (NULL, components);
229
 
 
230
 
  g_strfreev (components);
231
 
  
 
221
  gchar *it, *canonicalized;
 
222
  GString *res = g_string_sized_new (strlen (str));
 
223
 
 
224
  for (it = (gchar*)str; *it; it = g_utf8_next_char (it))
 
225
    {
 
226
      gunichar chr = g_utf8_get_char (it);
 
227
      if (g_unichar_isalnum (chr) || g_unichar_isspace (chr))
 
228
        res = g_string_append_c (res, chr);
 
229
    }
 
230
  canonicalized = res->str;
 
231
  g_string_free (res, FALSE);
 
232
 
232
233
  return canonicalized;
233
234
}
234
235
 
308
309
}
309
310
 
310
311
static gchar *
 
312
validate_mime_str (const gchar *in)
 
313
{
 
314
  gchar *out = NULL, **it;
 
315
 
 
316
  if (!in)
 
317
    return NULL;
 
318
 
 
319
  gchar **mimes = g_strsplit (in, ";", -1);
 
320
  GList *registered = g_content_types_get_registered ();
 
321
 
 
322
  for (it = mimes; it && *it; it++)
 
323
    {
 
324
      if (g_list_find_custom (registered, *it, (GCompareFunc)g_strcmp0))
 
325
        {
 
326
          gchar *tmp = g_strconcat (*it, ";", out, NULL);
 
327
          g_free (out);
 
328
          out = tmp;
 
329
        }
 
330
    }
 
331
  g_list_free_full (registered, g_free);
 
332
  g_strfreev (mimes);
 
333
 
 
334
  return out;
 
335
}
 
336
 
 
337
static gchar *
311
338
unity_webapps_application_info_get_desktop_file_contents (UnityWebappsApplicationInfo *info)
312
339
{
313
 
  gchar *contents, *icon_name;
314
 
  
 
340
  gint i;
 
341
  gchar **labels, **pages, **il, **ip;
 
342
  gchar *contents, *icon_name, *mime_types;
 
343
  gchar *name = unity_webapps_application_info_canonicalize_string (info->priv->name);
 
344
  gchar *base64_name = g_base64_encode ((guchar*)info->priv->name, strlen (info->priv->name) + 1);
 
345
 
315
346
  icon_name = unity_webapps_application_info_get_desktop_icon_name (info, TRUE);
316
347
 
317
 
  contents = g_strdup_printf("[Desktop Entry]\nName=%s\nType=Application\nIcon=%s\nMimeType=%s\nExec=unity-webapps-runner -n '%s' -d '%s' %%u", info->priv->name, icon_name, info->priv->mime_types, info->priv->name, info->priv->domain);
 
348
  mime_types = validate_mime_str (info->priv->mime_types);
 
349
  if (!mime_types)
 
350
    mime_types = g_strdup ("");
 
351
 
 
352
  contents = g_strdup_printf ("[Desktop Entry]\nName=%s\nType=Application\nIcon=%s\nMimeType=%s\nActions=S0;S1;S2;S3;S4;S5;S6;S7;S8;S9;S10;\nExec=unity-webapps-runner -n '%s' -d '%s' %%u",
 
353
                              name, icon_name, mime_types,
 
354
                              base64_name, info->priv->domain);
 
355
 
 
356
  unity_webapps_app_db_get_actions (info->priv->name, info->priv->domain, &labels, &pages);
 
357
 
 
358
  for (i = 0, il = labels, ip = pages; il && *il; il++, i++, ip++)
 
359
    {
 
360
      gchar *label = unity_webapps_application_info_canonicalize_string (*il);
 
361
      if (!label)
 
362
        continue;
 
363
 
 
364
      SoupURI *uri = soup_uri_new (*ip);
 
365
      if (!uri)
 
366
        {
 
367
          g_free (label);
 
368
          continue;
 
369
        }
 
370
      gchar *page = soup_uri_to_string (uri, FALSE);
 
371
      soup_uri_free (uri);
 
372
 
 
373
      gchar *tmp = g_strdup_printf ("%s\n[Desktop Action S%d]\nName=%s\nOnlyShowIn=Unity;\nExec=xdg-open '%s'\n",
 
374
                                    contents, i, label, page);
 
375
 
 
376
      g_free (contents);
 
377
      g_free (label);
 
378
      contents = tmp;
 
379
    }
318
380
 
319
381
  g_free (icon_name);
 
382
  g_free (mime_types);
 
383
  g_free (base64_name);
 
384
  g_free (name);
 
385
  g_strfreev (labels);
 
386
  g_strfreev (pages);
320
387
 
321
388
  return contents;
322
389
}