~webapps/libunity-webapps/2.4

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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * unity-webapps-action-invoker.c
 * Copyright (C) Canonical LTD 2011
 * 
 * Author: Robert Carr <racarr@canonical.com>
 * 
unity-webapps 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 3 of the License, or
 * (at your option) any later version.
 * 
 * unity-webapps 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, see <http://www.gnu.org/licenses/>.";
 */
#include <stdlib.h>

#include "unity-webapps-service.h"
#include "unity-webapps-context.h"

#include <glib/gstdio.h>

#include <time.h>

#include <libdbusmenu-glib/menuitem.h>
#include <libdbusmenu-glib/client.h>

static UnityWebappsService *service = NULL;

static gchar *name = NULL;
static gchar *domain = NULL;
const gchar *action = NULL;

static GOptionEntry option_entries[] =
	{
		{ "name", 'n',0,G_OPTION_ARG_STRING, &name, "Application name", NULL },
		{ "domain", 'd',0, G_OPTION_ARG_STRING, &domain, "Application domain", NULL},
		{ "action", 'a', 0, G_OPTION_ARG_STRING, &action, "Action path", NULL}
	};

static gchar **
split_action_path (const gchar *action_path)
{
  gchar **components;
  
  if (action_path[0] != '/')
    {
      return NULL;
    }
  
  components = g_strsplit (action_path+1, "/", -1);
  
  if (g_strv_length (components) > 3)
    {
      g_strfreev (components);
      return NULL;
    }
  
  return components;
}

static DbusmenuMenuitem *
menuitem_find_child_by_label (DbusmenuMenuitem *item,
			      const gchar *label)
{
  const GList *children;
  const GList *walk;
  
  children = dbusmenu_menuitem_get_children (item);
  
  for (walk = children;
       walk != NULL;
       walk = walk->next)
    {
      DbusmenuMenuitem *child;
      
      const gchar *child_label;
      
      child = (DbusmenuMenuitem *)walk->data;
      child_label = dbusmenu_menuitem_property_get (child, "label");
      
      if (g_strcmp0 (child_label, label) == 0)
	{
	  return child;
	}
    }
  
  return NULL;
}


static void
invoke_action_by_path (DbusmenuMenuitem *root, const gchar *action_path)
{
  gchar **components;
  DbusmenuMenuitem *item;
  gint i, length;
  
  components = split_action_path (action_path);
  
  length = g_strv_length (components);
  
  if (length < 0)
	{
	  return;
	}
  
  item = root;
  
  for (i = 0; i < length; i++)
	{
	  DbusmenuMenuitem *child;
	  gchar *component;
	  
	  component = components[i];
	  
	  child = menuitem_find_child_by_label (item, component);
	  
	  if (child == NULL)
		{
		  g_error ("Action not found");
		  
		  exit (1);
		}
	  
	  item = child;
	}
  
  g_message("Invoking item: %s", components[i-1]);
  dbusmenu_menuitem_handle_event (item, "clicked", NULL, time(NULL));
  
  g_strfreev (components);
}

static void
on_root_changed (DbusmenuClient *client,
				 DbusmenuMenuitem *newitem,
				 gpointer user_data)
{
  invoke_action_by_path (newitem, action);
}

static void
context_ready (UnityWebappsContext *context, gpointer user_data)
{
  DbusmenuClient *client;
  const gchar *context_name;
  
  context_name = unity_webapps_context_get_context_name (context);
  
  client = dbusmenu_client_new (context_name, "/com/canonical/Unity/Webapps/Context/ApplicationActions");
  
  g_signal_connect (client, "root-changed",
					G_CALLBACK (on_root_changed),
					NULL);
  
}


gint
main (gint argc, gchar **argv)
{
  GOptionContext *context;
  GError *error;

    g_type_init ();
  
  service = unity_webapps_service_new ();
  
  error = NULL;
  
  context = g_option_context_new ("- Activate Unity WebApps");
  
  // TODO: GETTEXT
  g_option_context_add_main_entries (context, option_entries, NULL);

  if (!g_option_context_parse (context, &argc, &argv, &error))
	{
	  g_printf("Failed to parse arguments: %s\n", error->message);
	  exit(1);
	}
  
  unity_webapps_context_new (service, name, domain, "", NULL, context_ready, NULL);
  
  g_main_loop_run (g_main_loop_new (NULL, FALSE));
  
  return 0;  
}