aboutsummaryrefslogtreecommitdiffstats
path: root/src/ephy-application.c
blob: 8ba4d89f3d1e6a6be139f4a724c2633865d529d3 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 *  Copyright © 2011 Igalia S.L.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"

#include "ephy-application.h"
#include "ephy-file-helpers.h"
#include "ephy-shell.h"
#include "ephy-session.h"
#include "ephy-debug.h"
#include "ephy-profile-utils.h"

#include <string.h>

#define EPHY_APPLICATION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_APPLICATION, EphyApplicationPrivate))

struct _EphyApplicationPrivate {
  EphyApplicationStartupContext *startup_context;
};

G_DEFINE_TYPE (EphyApplication, ephy_application, GTK_TYPE_APPLICATION);

static void ephy_application_finalize (GObject *object);

/**
 * ephy_application_startup_context_new:
 * @bookmarks_filename: A bookmarks file to import.
 * @session_filename: A session to restore.
 * @bookmark_url: A URL to be added to the bookmarks.
 * @arguments: A %NULL-terminated array of URLs and file URIs to be opened.
 * @user_time: The user time when the EphyApplication startup was invoked.
 *
 * Creates a new startup context. All string parameters, including
 * @arguments, are copied.
 *
 * Returns: a newly allocated #EphyApplicationStartupContext
 **/
EphyApplicationStartupContext *
ephy_application_startup_context_new (EphyStartupFlags startup_flags,
                                      char *bookmarks_filename,
                                      char *session_filename,
                                      char *bookmark_url,
                                      char **arguments,
                                      guint32 user_time)
{
  EphyApplicationStartupContext *ctx = g_slice_new0 (EphyApplicationStartupContext);

  ctx->startup_flags = startup_flags;

  ctx->bookmarks_filename = g_strdup (bookmarks_filename);
  ctx->session_filename = g_strdup (session_filename);
  ctx->bookmark_url = g_strdup (bookmark_url);

  ctx->arguments = g_strdupv (arguments);

  ctx->user_time = user_time;

  return ctx;
}

static void
ephy_application_free_startup_context (EphyApplication *application)
{
  EphyApplicationStartupContext *ctx = application->priv->startup_context;

  g_assert (ctx != NULL);

  g_free (ctx->bookmarks_filename);
  g_free (ctx->session_filename);
  g_free (ctx->bookmark_url);

  g_strfreev (ctx->arguments);

  g_slice_free (EphyApplicationStartupContext, ctx);

  application->priv->startup_context = NULL;
}

static void
queue_commands (EphyApplication *application)
{
  EphyApplicationStartupContext *ctx;
  EphyShell *shell;
  EphySession *session;

  shell = ephy_shell_get_default ();
  g_assert (shell != NULL);
  session = EPHY_SESSION (ephy_shell_get_session (shell));
  g_assert (session != NULL);

  ctx = application->priv->startup_context;

  /* We only get here when starting a new instance, so we first need
     to autoresume! */
  ephy_session_queue_command (EPHY_SESSION (ephy_shell_get_session (shell)),
                              EPHY_SESSION_CMD_RESUME_SESSION,
                              NULL, NULL, ctx->user_time, TRUE);

  if (ctx->startup_flags & EPHY_STARTUP_BOOKMARKS_EDITOR)
    ephy_session_queue_command (session,
                                EPHY_SESSION_CMD_OPEN_BOOKMARKS_EDITOR,
                                NULL, NULL, ctx->user_time, FALSE);

  else if (ctx->session_filename != NULL) {
    ephy_session_queue_command (session,
                                EPHY_SESSION_CMD_LOAD_SESSION,
                                ctx->session_filename, NULL,
                                ctx->user_time, FALSE);
  } else if (ctx->arguments != NULL) {
    /* Don't queue any window openings if no extra arguments given, */
    /* since session autoresume will open one for us. */
    GString *options;

    options = g_string_sized_new (64);

    if (ctx->startup_flags & EPHY_STARTUP_NEW_WINDOW) {
      g_string_append (options, "new-window,");
    }
    if (ctx->startup_flags & EPHY_STARTUP_NEW_TAB) {
      g_string_append (options, "new-tab,external,");
    }

    ephy_session_queue_command (session,
                                EPHY_SESSION_CMD_OPEN_URIS,
                                options->str,
                                ctx->arguments,
                                ctx->user_time, FALSE);
  }
}

static void
ephy_application_startup (GApplication* application)
{
  /* We're not remoting; start our services */
  /* Migrate profile if we are not running a private instance */
  if (ephy_has_private_profile () == FALSE &&
      ephy_profile_utils_get_migration_version () < EPHY_PROFILE_MIGRATION_VERSION) {
    GError *error = NULL;
    char *argv[1] = { "ephy-profile-migrator" };
    char *envp[1] = { "EPHY_LOG_MODULES=ephy-profile" };

    g_spawn_sync (NULL, argv, envp, G_SPAWN_SEARCH_PATH,
                  NULL, NULL, NULL, NULL,
                  NULL, &error);

    if (error) {
      LOG ("Failed to run migrator: %s", error->message);
      g_error_free (error);
    }
  }
}

static void
ephy_application_activate (GApplication *application)
{
  /*
   * We get here on each new instance (remote or not). Queue the
   * commands.
   */
  queue_commands (EPHY_APPLICATION (application));
}

/*
 * We use this enumeration to conveniently fill and read from the
 * dictionary variant that is sent from the remote to the primary
 * instance.
 */
typedef enum {
  CTX_STARTUP_FLAGS,
  CTX_BOOKMARKS_FILENAME,
  CTX_SESSION_FILENAME,
  CTX_BOOKMARK_URL,
  CTX_ARGUMENTS,
  CTX_USER_TIME
}CtxEnum;

static void
ephy_application_add_platform_data (GApplication *application,
                                    GVariantBuilder *builder)
{
  EphyApplication *app;
  EphyApplicationStartupContext *ctx;
  GVariantBuilder *ctx_builder;

  app = EPHY_APPLICATION (application);

  G_APPLICATION_CLASS (ephy_application_parent_class)->add_platform_data (application,
                                                                          builder);

  if (app->priv->startup_context) {
    /*
     * We create an array variant that contains only the elements in
     * ctx that are non-NULL.
     */
    ctx_builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
    ctx = app->priv->startup_context;

    if (ctx->startup_flags) {
      g_variant_builder_add (ctx_builder, "{iv}",
                             CTX_STARTUP_FLAGS,
                             g_variant_new_byte (ctx->startup_flags));
    }
    if (ctx->bookmarks_filename) {
      g_variant_builder_add (ctx_builder, "{iv}",
                             CTX_BOOKMARKS_FILENAME,
                             g_variant_new_string (ctx->bookmarks_filename));
    }
    if (ctx->session_filename) {
      g_variant_builder_add (ctx_builder, "{iv}",
                             CTX_SESSION_FILENAME,
                             g_variant_new_string (ctx->session_filename));
    }
    if (ctx->bookmark_url) {
      g_variant_builder_add (ctx_builder, "{iv}",
                             CTX_BOOKMARK_URL,
                             g_variant_new_string (ctx->bookmark_url));
    }
    if (ctx->arguments) {
      g_variant_builder_add (ctx_builder, "{iv}",
                             CTX_ARGUMENTS,
                             g_variant_new_strv ((const gchar * const *)ctx->arguments, -1));
    }

    g_variant_builder_add (ctx_builder, "{iv}",
                           CTX_USER_TIME,
                           g_variant_new_uint32 (ctx->user_time));

    g_variant_builder_add (builder, "{sv}",
                           "ephy-application-startup-context",
                           g_variant_builder_end (ctx_builder));

    g_variant_builder_unref (ctx_builder);
  }
}

static void
ephy_application_before_emit (GApplication *application,
                              GVariant *platform_data)
{
  GVariantIter iter, ctx_iter;
  const char *key;
  CtxEnum ctx_key;
  GVariant *value, *ctx_value;
  EphyApplicationStartupContext *ctx = NULL;

  EphyApplication *app = EPHY_APPLICATION (application);

  g_variant_iter_init (&iter, platform_data);
  while (g_variant_iter_loop (&iter, "{&sv}", &key, &value)) {
    if (strcmp (key, "ephy-application-startup-context") == 0) {
      ctx = g_slice_new0 (EphyApplicationStartupContext);

      /*
       * Iterate over the startup context variant and fill the members
       * that were wired. Everything else is just NULL.
       */
      g_variant_iter_init (&ctx_iter, value);
      while (g_variant_iter_loop (&ctx_iter, "{iv}", &ctx_key, &ctx_value)) {
        switch (ctx_key) {
        case CTX_STARTUP_FLAGS:
          ctx->startup_flags = g_variant_get_byte (ctx_value);
          break;
        case CTX_BOOKMARKS_FILENAME:
          ctx->bookmarks_filename = g_variant_dup_string (ctx_value, NULL);
          break;
        case CTX_SESSION_FILENAME:
          ctx->session_filename = g_variant_dup_string (ctx_value, NULL);
          break;
        case CTX_BOOKMARK_URL:
          ctx->bookmark_url = g_variant_dup_string (ctx_value, NULL);
          break;
        case CTX_ARGUMENTS:
          ctx->arguments = g_variant_dup_strv (ctx_value, NULL);
          break;
        case CTX_USER_TIME:
          ctx->user_time = g_variant_get_uint32 (ctx_value);
          break;
        default:
          g_assert_not_reached ();
          break;
        }
      }
    }
  }

  if (app->priv->startup_context)
    ephy_application_free_startup_context (app);
  app->priv->startup_context = ctx;

  G_APPLICATION_CLASS (ephy_application_parent_class)->before_emit (application,
                                                                    platform_data);
}

static void
ephy_application_class_init (EphyApplicationClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS(class);
  GApplicationClass *application_class = G_APPLICATION_CLASS (class);

  object_class->finalize = ephy_application_finalize;

  application_class->startup = ephy_application_startup;
  application_class->activate = ephy_application_activate;
  application_class->before_emit = ephy_application_before_emit;
  application_class->add_platform_data = ephy_application_add_platform_data;

  g_type_class_add_private (class, sizeof (EphyApplicationPrivate));
}

static void
ephy_application_init (EphyApplication *application)
{
  application->priv = EPHY_APPLICATION_GET_PRIVATE(application);
  application->priv->startup_context = NULL;
}

static void
ephy_application_finalize (GObject *object)
{
  ephy_application_free_startup_context (EPHY_APPLICATION (object));

  G_OBJECT_CLASS (ephy_application_parent_class)->finalize (object);
}

EphyApplication *
ephy_application_new (void)
{
  return g_object_new (EPHY_TYPE_APPLICATION,
                       "application-id", "org.gnome.Epiphany",
                       "flags", G_APPLICATION_FLAGS_NONE,
                       NULL);
}

/**
 * ephy_application_set_startup_context:
 * @application: A #EphyApplication
 * @ctx: (transfer full): a #EphyApplicationStartupContext
 *
 * Sets the startup context to be used during activation of a new instance.
 * See ephy_application_set_startup_new().
 **/
void
ephy_application_set_startup_context (EphyApplication *application,
                                      EphyApplicationStartupContext *ctx)
{
  g_return_if_fail (EPHY_IS_APPLICATION (application));

  if (application->priv->startup_context)
    ephy_application_free_startup_context (application);

  application->priv->startup_context = ctx;
}