aboutsummaryrefslogtreecommitdiffstats
path: root/src/ephy-main.c
blob: 0a0093974a30a209947ae5eac9ae40caef3bd4e0 (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
/*
 *  Copyright (C) 2000-2002 Marco Pesenti Gritti
 *
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "ephy-shell.h"
#include "ephy-automation.h"
#include "ephy-window.h"
#include "EphyAutomation.h"

#include <libbonoboui.h>
#include <libgnome/gnome-program.h>
#include <libgnomeui/gnome-ui-init.h>
#include <libgnomeui/gnome-window-icon.h>
#include <libgnomevfs/gnome-vfs-init.h>
#include <glade/glade-init.h>

#define EPHY_FACTORY_OAFIID "OAFIID:GNOME_Epiphany_Automation_Factory"

static gboolean
ephy_main_automation_init (void);
static gint
ephy_main_translate_url_arguments (poptContext context, gchar ***urls);
static gboolean
ephy_main_start (gpointer data);

GnomeProgram *program;
CORBA_Environment corba_env;            /* Global for downloader    */
static gboolean open_in_existing      = FALSE;  /* load in existing window?     */
static gboolean open_in_new_tab       = FALSE;  /* force open in a new tab?     */
static gboolean noraise               = FALSE;  /* no raise                     */
static gboolean open_in_new_window    = FALSE;  /* force open in a new window?  */
static gboolean open_fullscreen       = FALSE;  /* open ephy in full screen ? */
static gchar   *session_filename      = NULL;   /* the session filename         */
static gchar   *geometry_string       = NULL;   /* the geometry string          */
static gchar   *bookmark_url          = NULL;   /* the temp bookmark to add     */
static gboolean close_option          = FALSE;  /* --close                      */
static gboolean quit_option           = FALSE;  /* --quit                       */
static gboolean ephy_server_mode    = FALSE;
static gboolean open_as_nautilus_view = FALSE;

static BonoboObject *automation_object;
static gint n_urls;
static gchar **url;
static gboolean first_instance;

/* command line argument parsing structure */
static struct poptOption popt_options[] =
{
    { NULL, '\0', POPT_ARG_INCLUDE_TABLE, &bonobo_activation_popt_options, 0, NULL,
      NULL },
    { "new-tab", 'n', POPT_ARG_NONE, &open_in_new_tab, 0,
      N_("Open a new tab in an existing Ephy window"),
      NULL },
    { "new-window", 'w', POPT_ARG_NONE, &open_in_new_window, 0,
      N_("Open a new window in an existing Ephy process"),
      NULL },
    { "noraise", '\0', POPT_ARG_NONE, &noraise, 0,
      N_("Do not raise the window when opening a page in an existing Ephy process"),
      NULL },
    { "fullscreen", 'f', POPT_ARG_NONE, &open_fullscreen, 0,
      N_("Run Ephy in full screen mode"),
      NULL },
    { "existing", 'x', POPT_ARG_NONE, &open_in_existing, 0,
      N_("Attempt to load URL in existing Ephy window"),
      NULL },
    { "load-session", 'l', POPT_ARG_STRING, &session_filename, 0,
      N_("Load the given session file"),
      N_("FILE") },
    { "server", 's', POPT_ARG_NONE, &ephy_server_mode, 0,
      N_("Don't open any windows; instead act as a server "
         "for quick startup of new Ephy instances"),
      NULL },
    { "add-bookmark", 't', POPT_ARG_STRING, &bookmark_url,
      0, N_("Add a bookmark (don't open any window)"),
      N_("URL")},
    { "geometry", 'g', POPT_ARG_STRING, &geometry_string,
      0, N_("Create the initial window with the given geometry.\n"
        "see X(1) for the GEOMETRY format"),
      N_("GEOMETRY")},
    { "close", 'c', POPT_ARG_NONE, &close_option, 0,
      N_("Close all Ephy windows"),
      NULL },
    { "quit", 'q', POPT_ARG_NONE, &quit_option, 0,
      N_("Same as --close, but exits server mode too"),
      NULL },
    { "nautilus-view", 'v', POPT_ARG_NONE, &open_as_nautilus_view, 0,
      N_("Used internally by the nautilus view"),
      NULL },

    /* terminator, must be last */
    { NULL, 0, 0, NULL, 0, NULL, NULL }
};

int
main (int argc, char *argv[])
{
    poptContext context;
        GValue context_as_value = { 0 };
    GnomeProgram *program;

#ifdef ENABLE_NLS
    /* Initialize the i18n stuff */
    bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
    textdomain(GETTEXT_PACKAGE);
#endif

    program = gnome_program_init (PACKAGE, VERSION,
                                      LIBGNOMEUI_MODULE, argc, argv,
                                      GNOME_PARAM_POPT_TABLE, popt_options,
                                      GNOME_PARAM_HUMAN_READABLE_NAME, _("Ephy"),
                      GNOME_PARAM_APP_DATADIR, DATADIR,
                                      NULL);

        g_object_get_property (G_OBJECT (program),
                               GNOME_PARAM_POPT_CONTEXT,
                               g_value_init (&context_as_value, G_TYPE_POINTER));

        context = g_value_get_pointer (&context_as_value);

    /* load arguments that aren't regular options (urls to load) */
        n_urls = ephy_main_translate_url_arguments (context, &url);

    first_instance = ephy_main_automation_init ();

    if (first_instance)
    {
        gnome_vfs_init ();

        glade_gnome_init ();

        gnome_window_icon_set_default_from_file (PIXMAP_DIR "/ephy.png");

        ephy_shell_new ();

        g_idle_add ((GSourceFunc) ephy_main_start, NULL);

        bonobo_main ();

        gnome_vfs_shutdown ();
    }

    return 0;
}

static gboolean
ephy_main_start (gpointer data)
{
    GNOME_EphyAutomation gaserver;
    int i;

    CORBA_exception_init (&corba_env);

    gaserver = bonobo_activation_activate_from_id ("OAFIID:GNOME_Epiphany_Automation",
                               0, NULL, &corba_env);

    if (gaserver == NULL)
    {
        GtkWidget *dialog;
        dialog = gtk_message_dialog_new
            (NULL,
                         GTK_DIALOG_MODAL,
                         GTK_MESSAGE_ERROR,
                         GTK_BUTTONS_CLOSE,
                         "Ephy can't be used now. "
                         "Running the command \"bonobo-slay\" "
                         "from the console may fix the problem. If not, "
                         "you can try rebooting the computer or "
                         "installing Ephy again.\n\n"
                         "Bonobo couldn't locate the GNOME_Epiphany_Automation.server. ");
        gtk_dialog_run (GTK_DIALOG (dialog));

    }
    /* FIXME ephy --server doesnt work when not first istance */
    /* Server mode */
    else if (ephy_server_mode)
    {
        g_object_ref (G_OBJECT(ephy_shell));
    }
    /* load the session if requested */
    else if (session_filename)
    {
        GNOME_EphyAutomation_loadSession
            (gaserver, session_filename, &corba_env);
    }
    /* if found and we're given a bookmark to add... */
    else if (bookmark_url != NULL)
    {
        GNOME_EphyAutomation_addBookmark
            (gaserver, bookmark_url, &corba_env);
    }
    else if (close_option || quit_option)
    {
        GNOME_EphyAutomation_quit
            (gaserver, quit_option, &corba_env);
    }
    /* provided with urls? */
    else if (n_urls == 0 &&
         !open_as_nautilus_view)
    {
        /* no, open a default window */
        GNOME_EphyAutomation_loadurl
            (gaserver, "",
             geometry_string ?
             geometry_string : "",
             open_fullscreen,
             open_in_existing,
             open_in_new_window,
             open_in_new_tab,
             !noraise,
             &corba_env);
    }
    else
    {
        /* open all of the urls */
        for (i = 0; i < n_urls; i++)
        {
            GNOME_EphyAutomation_loadurl
                (gaserver, url[i],
                 geometry_string ?
                 geometry_string : "",
                 open_fullscreen,
                 open_in_existing,
                 open_in_new_window,
                 open_in_new_tab,
                 !noraise,
                 &corba_env);
        }
    }

    /* Unref so it will exit if no more used */
    if (first_instance)
    {
        g_object_unref (G_OBJECT(ephy_shell));
    }

    if (gaserver)
    {
        bonobo_object_release_unref (gaserver, &corba_env);
    }

    CORBA_exception_free (&corba_env);

    return FALSE;
}

static gboolean
ephy_main_automation_init (void)
{
    CORBA_Object factory;

    factory = bonobo_activation_activate_from_id
        (EPHY_FACTORY_OAFIID,
         Bonobo_ACTIVATION_FLAG_EXISTING_ONLY,
         NULL, NULL);

    if (!factory)
    {
        automation_object = ephy_automation_new ();
        return TRUE;
    }
    else
    {
        ephy_main_start (NULL);
        g_message (_("Ephy already running, using existing process"));
        return FALSE;
    }
}

/**
 * translate_url_arguments: gather URL arguments and expand them fully
 * with realpath if they're filenames
 */
static gint
ephy_main_translate_url_arguments (poptContext context, gchar ***urls)
{
        gchar buffer[PATH_MAX];
        gchar **args;
        gint i, n;

        /* any context remaining? */
        if (context == NULL)
        {
                *urls = NULL;
                return 0;
        }

        /* get the args and check */
        args = (gchar **) poptGetArgs (context);
        if (args == NULL)
        {
                poptFreeContext (context);
                *urls = NULL;
                return 0;
        }

        /* count args */
        for (n = 0; args[n] != NULL; n++)
                /* nothing */;

        /* allocate pointer array */
        *urls = g_new0 (gchar *, n + 1);

        /* translate each one */
        for (i = 0; i < n; i++)
        {
                /* try to expand as files */
                if (realpath (args[i], buffer) != NULL)
                {
                        (*urls)[i] = g_strconcat ("file://", buffer, NULL);
                }
                else
                {
                        (*urls)[i] = g_strdup (args[i]);
                }
        }
        poptFreeContext (context);
        (*urls)[i] = NULL;

        /* return the number of urls */
        return n;
}