File: remote-unique.c

package info (click to toggle)
gwyddion 2.47-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,180 kB
  • ctags: 37,276
  • sloc: ansic: 317,531; sh: 5,122; xml: 3,583; python: 3,067; makefile: 2,694; cpp: 2,550; pascal: 418; perl: 154; ruby: 130; sed: 17
file content (180 lines) | stat: -rw-r--r-- 4,888 bytes parent folder | download
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
/*
 *  @(#) $Id: remote-unique.c 19043 2016-10-08 18:58:43Z yeti-dn $
 *  Copyright (C) 2009 David Necas (Yeti).
 *  E-mail: yeti@gwyddion.net.
 *
 *  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 of the License, 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 "gwyddion.h"
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwyenum.h>
#include <libgwymodule/gwymodule-file.h>
#include <app/file.h>
#if (REMOTE_BACKEND == REMOTE_UNIQUE)
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <unique/unique.h>

struct _GwyRemote {
    UniqueApp *uniqueapp;
};

static UniqueApp *uniqueapp = NULL;

static UniqueResponse
message_received(UniqueApp *uniqueapp_,
                 gint command,
                 UniqueMessageData *message_data,
                 G_GNUC_UNUSED guint time_,
                 G_GNUC_UNUSED gpointer user_data)
{
    gchar *filename, **uris;
    guint i, nok = 0;

    g_assert(uniqueapp_ == uniqueapp);
    if (command != UNIQUE_OPEN)
        return UNIQUE_RESPONSE_PASSTHROUGH;

    uris = unique_message_data_get_uris(message_data);
    for (i = 0; uris[i]; i++) {
        filename = g_filename_from_uri(uris[i], NULL, NULL);
        if (filename) {
            if (gwy_file_detect(filename, FALSE, GWY_FILE_OPERATION_LOAD)) {
                if (gwy_app_file_load(NULL, filename, NULL))
                    nok++;
            }
            g_free(filename);
        }
    }
    g_strfreev(uris);

    return nok ? UNIQUE_RESPONSE_OK : UNIQUE_RESPONSE_FAIL;
}

void
gwy_remote_setup(GtkWidget *toolbox)
{
    if (!uniqueapp)
        uniqueapp = unique_app_new("net.gwyddion.Gwyddion", NULL);

    /* Only connect signals if run from GUI and we are the first instance.
     * The user can run more instances but these cannot be remotely
     * controlled. */
    if (toolbox) {
        if (unique_app_is_running(uniqueapp)) {
            gwy_remote_finalize(NULL);
            return;
        }
        g_signal_connect(uniqueapp, "message-received",
                         G_CALLBACK(message_received), NULL);
    }
}

void
gwy_remote_finalize(G_GNUC_UNUSED GtkWidget *toolbox)
{
    GWY_OBJECT_UNREF(uniqueapp);
}

GwyRemote*
gwy_remote_get(void)
{
    GwyRemote *remote;

    gwy_remote_setup(NULL);
    if (unique_app_is_running(uniqueapp)) {
        remote = g_new0(GwyRemote, 1);
        remote->uniqueapp = uniqueapp;
        return remote;
    }
    else
        return NULL;
}

void
gwy_remote_free(GwyRemote *remote)
{
    gwy_remote_finalize(NULL);
    g_free(remote);
}

gboolean
gwy_remote_open_files(GwyRemote *remote,
                      int argc,
                      char **argv)
{
    UniqueMessageData *message;
    UniqueResponse response = UNIQUE_RESPONSE_INVALID;
    GPtrArray *file_list;
    gchar *cwd;
    gint i;

    if (!remote)
        return FALSE;

    g_return_val_if_fail(remote->uniqueapp == uniqueapp, FALSE);

    /* Build the file list. */
    cwd = g_get_current_dir();
    file_list = g_ptr_array_new();
    for (i = 0; i < argc; i++) {
        gchar *s, *t;

        if (g_path_is_absolute(argv[i]))
            s = g_filename_to_uri(argv[i], NULL, NULL);
        else {
            t = g_build_filename(cwd, argv[i], NULL);
            s = g_filename_to_uri(t, NULL, NULL);
            g_free(t);
        }
        g_ptr_array_add(file_list, s);
    }
    g_ptr_array_add(file_list, NULL);

    message = unique_message_data_new();
    if (unique_message_data_set_uris(message, (gchar**)file_list->pdata)) {
        response = unique_app_send_message(uniqueapp, UNIQUE_OPEN, message);
    }
    else {
        g_warning("unique_message_data_set_uris() failed.");
    }

    unique_message_data_free(message);
    g_ptr_array_foreach(file_list, (GFunc)g_free, NULL);
    g_ptr_array_free(file_list, TRUE);
    gdk_notify_startup_complete();

    return response == UNIQUE_RESPONSE_OK;
}

void
gwy_remote_print(GwyRemote *remote)
{
    if (remote) {
        gchar *s;

        g_return_if_fail(remote->uniqueapp == uniqueapp);
        g_object_get(uniqueapp, "startup-id", &s, NULL);
        g_printerr("%s\n", s);
        g_free(s);
    }
}
#endif

/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */