File: test-unique.c

package info (click to toggle)
libunique 1.1.6-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,704 kB
  • sloc: sh: 10,371; ansic: 2,472; xml: 1,345; makefile: 238
file content (221 lines) | stat: -rw-r--r-- 5,887 bytes parent folder | download | duplicates (5)
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
/* Unique - Single Instance Application library
 *
 * Copyright (C) 2007  Emmanuele Bassi  <ebassi@o-hand.com>
 */

#include <stdlib.h>
#include <gtk/gtk.h>
#include <unique/unique.h>

enum {
  COMMAND_0,

  COMMAND_FOO,
  COMMAND_BAR
};

static GtkWidget *main_window = NULL;

static UniqueResponse
app_message_cb (UniqueApp         *app,
                gint               command,
                UniqueMessageData *message_data,
                guint              time_,
                gpointer           user_data)
{
  GtkWidget *dialog;
  gchar *message = NULL;
  gchar *title = NULL;
  GdkScreen *screen;
  const gchar *startup_id;

  screen = unique_message_data_get_screen (message_data);
  startup_id = unique_message_data_get_startup_id (message_data);

  g_print ("Message received from screen: %d, startup-id: %s, workspace: %d\n",
           gdk_screen_get_number (screen),
           startup_id,
           unique_message_data_get_workspace (message_data));

  /* raise the window */
  gtk_window_present_with_time (GTK_WINDOW (main_window), time_);

  switch (command)
    {
    case UNIQUE_NEW:
      title = g_strdup ("Received the NEW command");
      break;
    case UNIQUE_OPEN:
      {
        gchar **uris;
        gint n_uris, i;
        GString *buf;

        title = g_strdup ("Received the OPEN command");
        
        uris = unique_message_data_get_uris (message_data);
        n_uris = g_strv_length (uris);
        buf = g_string_new (NULL);
        for (i = 0; i < n_uris; i++)
          g_string_append_printf (buf, "uri: %s\n", uris[i]);

        message = g_string_free (buf, FALSE);
        g_strfreev (uris);
      }
      break;
    case UNIQUE_ACTIVATE:
      title = g_strdup ("Received the ACTIVATE command");
      break;
    case COMMAND_FOO:
      title = g_strdup ("Received the FOO command");
      message = unique_message_data_get_text (message_data);
      break;
    case COMMAND_BAR:
      title = g_strdup ("Received the BAR command");
      message = g_strdup ("Thid command doesn't do anything special");
      break;
    default:
      break;
    }

  dialog = gtk_message_dialog_new (GTK_WINDOW (main_window),
                                   GTK_DIALOG_DESTROY_WITH_PARENT,
                                   GTK_MESSAGE_INFO,
                                   GTK_BUTTONS_CLOSE,
                                   "%s",
                                   title);
  if (message)
    gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                              "%s",
                                              message);

  gtk_window_set_urgency_hint (GTK_WINDOW (dialog), TRUE);

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);

  g_free (message);
  g_free (title);

  return UNIQUE_RESPONSE_OK;
}

int
main (int argc, char *argv[])
{
  UniqueApp *app;
  gboolean new = FALSE;
  gboolean activate = FALSE;
  gboolean foo = FALSE;
  gchar **uris = NULL;
  GError *init_error = NULL;
  GOptionEntry entries[] = {
    { "new", 'n',
      0,
      G_OPTION_ARG_NONE, &new,
      "Send 'new' command", NULL,
    },
    { "open-uri", 'o',
      0,
      G_OPTION_ARG_STRING_ARRAY, &uris,
      "Send 'open' command", "URI",
    },
    {
      "activate", 'a',
      0,
      G_OPTION_ARG_NONE, &activate,
      "Send 'activate' command", NULL,
    },
    {
      "foo", 'f',
      0,
      G_OPTION_ARG_NONE, &foo,
      "Send 'foo' command", NULL,
    },
    { NULL },
  };

  gtk_init_with_args (&argc, &argv,
                      "Test GtkUnique",
                      entries,
                      NULL,
                      &init_error);
  if (init_error)
    {
      g_print ("*** Error: %s\n"
               "Usage: test-unique [COMMAND]\n",
               init_error->message);
      g_error_free (init_error);

      exit (1);
    }

  app = unique_app_new_with_commands ("org.gnome.TestUnique", NULL,
                                      "foo", COMMAND_FOO,
                                      "bar", COMMAND_BAR,
                                      NULL);
  if (unique_app_is_running (app))
    {
      UniqueMessageData *message = NULL;
      UniqueResponse response;
      gint command;


      if (new)
        command = UNIQUE_NEW;
      else if (uris && uris[0])
        {
          command = UNIQUE_OPEN;
      
          message = unique_message_data_new ();
          unique_message_data_set_uris (message, uris); 
        }
      else if (activate)
        command = UNIQUE_ACTIVATE;
      else if (foo)
        {
          command = COMMAND_FOO;
          
          message = unique_message_data_new ();
          unique_message_data_set (message, (const guchar *) "bar", 3);
        }
      else
        command = COMMAND_BAR;
      
      if (message)
        {
          response = unique_app_send_message (app, command, message);
          unique_message_data_free (message);
        }
      else
        response = unique_app_send_message (app, command, NULL);

      g_print ("Response code: %d\n", response);

      gdk_notify_startup_complete ();
      
      g_object_unref (app);

      return (response == UNIQUE_RESPONSE_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
    }
  else
    {
      main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      g_signal_connect (main_window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
      gtk_window_set_title (GTK_WINDOW (main_window), "Test GtkUnique");
      gtk_window_set_default_size (GTK_WINDOW (main_window), 400, 300);
      gtk_container_set_border_width (GTK_CONTAINER (main_window), 12);

      unique_app_watch_window (app, GTK_WINDOW (main_window));
      g_signal_connect (app, "message-received",
                        G_CALLBACK (app_message_cb), NULL);

      gtk_widget_show (main_window);
    }
  
  gtk_main ();

  g_object_unref (app);

  return EXIT_SUCCESS;
}