~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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * unity-webapps-tool.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 <stdio.h>
#include <glib/gstdio.h>

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

static UnityWebappsService *service = NULL;

static void
print_help () 
{
  g_printf("Usage: unity-webapps-tool <COMMAND>\n");
  g_printf("Possible commands include:\n");
  g_printf("   help - display this message\n");
  g_printf("   list - list running web-application contexts");
  g_printf("   shutdown - shutdown the Unity Webapps System");
}

static UnityWebappsContext *
make_context_proxy (const gchar *name)
{
  return unity_webapps_context_new_for_context_name (service, name);
}

static void
list_contexts ()
{
  gchar **contexts;
  guint i, len;
  
  contexts = unity_webapps_service_list_contexts (service);
  if (contexts == NULL)
	{
	  return;
	}
  len = g_strv_length ((gchar **)contexts);

  g_printf("Running contexts:\n");
  for (i = 0; i < len; i++)
	{
	  UnityWebappsContext *context;
	  const gchar *context_name;
	  
	  context_name = contexts[i];
	  if (context_name[0] == '\0')
		continue;

	  context = make_context_proxy (context_name);
	  
	  
	  g_printf(" * %s (%s)\n", unity_webapps_context_get_name (context),
			   context_name);
	}

  g_strfreev (contexts);
}

static void
shutdown_all ()
{
  gchar **contexts;
  guint i, len;
  
  contexts = unity_webapps_service_list_contexts (service);
  if (contexts == NULL)
	{
	  return;
	}
  len = g_strv_length ((gchar **)contexts);

  printf("Shutting down contexts\n");
  for (i = 0; i < len; i++)
	{
	  UnityWebappsContext *context;
	  const gchar *context_name;
	  gboolean shutdown;
	  
	  context_name = contexts[i];
	  context = make_context_proxy (context_name);
	  
	  g_printf(" * Shutting down %s (%s)..", unity_webapps_context_get_name (context),
			   context_name);
	  
	  shutdown = unity_webapps_context_shutdown (context);
	  if (shutdown == FALSE)
		{
		  g_printf("Failed to shutdown\n");
		}
	  else
		{
		  g_printf("Done\n");
		}
	  
	  g_printf("Shutting down service...");
	  unity_webapps_service_shutdown (service);
	  g_printf("Done\n");

	}

  g_strfreev (contexts);
}

gint
main (gint argc, gchar **argv)
{
  g_type_init ();
  
  service = unity_webapps_service_new ();
  
  if (argc == 1)
	{
	  print_help();
	  exit(0);
	}
  if (g_strcmp0(argv[1], "help") == 0)
	{
	  print_help();
	  exit(0);
	}
  if (g_strcmp0(argv[1], "list") == 0)
	{
	  list_contexts();
	  exit(0);
	}
  if (g_strcmp0(argv[1], "shutdown") == 0)
	{
	  shutdown_all();
	  exit(0);
	}
  print_help();

  
  exit (0);
  
}