summaryrefslogtreecommitdiff
path: root/src/main.c
blob: f79d3ed370f24b52312c27023be56b3d68cd210d (plain)
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * transmission-remote-gtk - A GTK RPC client to Transmission
 * Copyright (C) 2011  Alan Fitton

 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>

#include <curl/curl.h>
#include <curl/easy.h>

#include <glib/gi18n.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <json-glib/json-glib.h>
#ifdef HAVE_LIBUNIQUE
#include <unique/unique.h>
#elif WIN32
#include <windows.h>
#endif

#include "trg-main-window.h"
#include "trg-client.h"
#include "util.h"

/* The file with the main() function. It converts arguments into a NULL terminated
 * array, with relative paths converted to absolute paths.
 *
 * Much of the code here is to optionally handle passing torrent files/URLs to
 * a first instance. UNIX can use libunique (should alternatively use
 * GApplication for GNOME3). Windows uses the mailslot feature in the
 * native Win32 API, so there is no dependency on dbus etc.
 */

#define TRG_LIBUNIQUE_DOMAIN "uk.org.eth0.trg"
#define TRG_MAILSLOT_NAME "\\\\.\\mailslot\\TransmissionRemoteGTK"  //Name given to the Mailslot
#define MAILSLOT_BUFFER_SIZE 1024*32

#ifdef HAVE_LIBUNIQUE

enum {
    COMMAND_0,
    COMMAND_ADD
};

static UniqueResponse
message_received_cb(UniqueApp * app G_GNUC_UNUSED,
        gint command,
        UniqueMessageData * message,
        guint time_, gpointer user_data)
{
    TrgMainWindow *win;
    UniqueResponse res;
    gchar **uris;

    win = TRG_MAIN_WINDOW(user_data);

    switch (command) {
        case UNIQUE_ACTIVATE:
        gtk_window_set_screen(GTK_WINDOW(user_data),
                unique_message_data_get_screen(message));
        gtk_window_present_with_time(GTK_WINDOW(user_data), time_);
        res = UNIQUE_RESPONSE_OK;
        break;
        case COMMAND_ADD:
        uris = unique_message_data_get_uris(message);
        res =
        trg_add_from_filename(win,
                uris) ? UNIQUE_RESPONSE_OK :
        UNIQUE_RESPONSE_FAIL;
        break;
        default:
        res = UNIQUE_RESPONSE_OK;
        break;
    }

    return res;
}

#elif WIN32

struct trg_mailslot_recv_args {
    TrgMainWindow *win;
    gchar **uris;
    gboolean present;
};

/* to be queued into the glib main loop with g_idle_add() */

static gboolean mailslot_recv_args(gpointer data) {
    struct trg_mailslot_recv_args *args = (struct trg_mailslot_recv_args*) data;

    if (args->present) {
        gtk_window_deiconify(GTK_WINDOW(args->win));
        gtk_window_present(GTK_WINDOW(args->win));
    }

    if (args->uris)
        trg_add_from_filename(args->win, args->uris);

    g_free(args);

    return FALSE;
}

static gpointer mailslot_recv_thread(gpointer data) {
    TrgMainWindow *win = TRG_MAIN_WINDOW(data);
    JsonParser *parser;
    char szBuffer[MAILSLOT_BUFFER_SIZE];
    HANDLE hMailslot;
    DWORD cbBytes;
    BOOL bResult;

    hMailslot = CreateMailslot(TRG_MAILSLOT_NAME, // mailslot name
            MAILSLOT_BUFFER_SIZE, // input buffer size
            MAILSLOT_WAIT_FOREVER, // no timeout
            NULL); // default security attribute

    if (INVALID_HANDLE_VALUE == hMailslot) {
        g_error(
                "\nError occurred while creating the mailslot: %d", GetLastError());
        return NULL; //Error
    }

    while (1) {
        bResult = ReadFile(hMailslot, // handle to mailslot
                szBuffer, // buffer to receive data
                sizeof(szBuffer), // size of buffer
                &cbBytes, // number of bytes read
                NULL); // not overlapped I/O

        if ((!bResult) || (0 == cbBytes)) {
            g_error("Mailslot error from client: %d", GetLastError());
            break;
        }

        parser = json_parser_new();

        if (json_parser_load_from_data(parser, szBuffer, cbBytes, NULL)) {
            JsonNode *node = json_parser_get_root(parser);
            JsonObject *obj = json_node_get_object(node);
            struct trg_mailslot_recv_args *args =
                    g_new0(struct trg_mailslot_recv_args, 1);

            args->present = json_object_has_member(obj, "present") && json_object_get_boolean_member(obj, "present");
            args->win = win;

            if (json_object_has_member(obj, "args")) {
                JsonArray *array = json_object_get_array_member(obj, "args");
                GList *arrayList = json_array_get_elements(array);

                if (arrayList) {
                    guint arrayLength = g_list_length(arrayList);
                    guint i = 0;
                    GList *li;

                    args->uris = g_new0(gchar*, arrayLength+1);

                    for (li = arrayList; li; li = g_list_next(li)) {
                        const gchar *liStr = json_node_get_string((JsonNode*) li->data);
                        args->uris[i++] = g_strdup(liStr);
                    }

                    g_list_free(arrayList);
                }
            }

            json_node_free(node);

            g_idle_add(mailslot_recv_args, args);
        }

        g_object_unref(parser);
    }

    CloseHandle(hMailslot);
    return NULL; //Success
}

static int mailslot_send_message(HANDLE h, gchar **args) {
    DWORD cbBytes;
    JsonNode *node = json_node_new(JSON_NODE_OBJECT);
    JsonObject *obj = json_object_new();
    JsonArray *array = json_array_new();
    JsonGenerator *generator;
    gchar *msg;
    int i;

    if (args) {
        for (i = 0; args[i]; i++)
            json_array_add_string_element(array, args[i]);

        json_object_set_array_member(obj, "args", array);

        g_strfreev(args);
    } else {
        json_object_set_boolean_member(obj, "present", TRUE);
    }

    json_node_take_object(node, obj);

    generator = json_generator_new();
    json_generator_set_root(generator, node);
    msg = json_generator_to_data(generator, NULL);

    json_node_free(node);
    g_object_unref(generator);

    WriteFile(h, // handle to mailslot
            msg, // buffer to write from
            strlen(msg) + 1, // number of bytes to write, include the NULL
            &cbBytes, // number of bytes written
            NULL);

    CloseHandle(h);
    g_free(msg);

    return 0;
}

#endif

static gboolean is_minimised_arg(gchar *arg)
{
    return !g_strcmp0(arg, "-m")
            || !g_strcmp0(arg, "--minimized")
            || !g_strcmp0(arg, "/m");
}

static gboolean should_be_minimised(int argc, char *argv[]) {
    int i;
    for (i = 1; i < argc; i++)
        if (is_minimised_arg(argv[i]))
            return TRUE;

    return FALSE;
}

static gchar **convert_args(int argc, char *argv[]) {
    gchar *cwd = g_get_current_dir();
    gchar **files = NULL;

    if (argc > 1) {
        GSList *list = NULL;
        int i;

        for (i = 1; i < argc; i++) {
            if (is_minimised_arg(argv[i])) {
                continue;
            } else if (!is_url(argv[i]) && !is_magnet(argv[i])
                    && g_file_test(argv[i], G_FILE_TEST_IS_REGULAR)
                    && !g_path_is_absolute(argv[i])) {
                list = g_slist_append(list,
                        g_build_path(G_DIR_SEPARATOR_S, cwd, argv[i], NULL));
            } else {
                list = g_slist_append(list, g_strdup(argv[i]));
            }
        }

        if (list) {
            GSList *li;
            files = g_new0(gchar*, g_slist_length(list)+1);
            i = 0;
            for (li = list; li; li = g_slist_next(li)) {
                files[i++] = li->data;
            }
            g_slist_free(list);
        }
    }

    g_free(cwd);

    return files;
}

int main(int argc, char *argv[]) {
    int returnValue = EXIT_SUCCESS;
    TrgMainWindow *window;
    TrgClient *client;
    gchar **args = convert_args(argc, argv);
    gboolean withUnique;
#ifdef HAVE_LIBUNIQUE
    UniqueApp *app = NULL;
#endif
#ifdef WIN32
    gchar *localedir, *moddir;
    HANDLE hMailSlot;
#endif
#ifdef TRG_MEMPROFILE
    GMemVTable gmvt = {malloc,realloc,free,calloc,malloc,realloc};
    g_mem_set_vtable(&gmvt);
    g_mem_set_vtable(glib_mem_profiler_table);
    g_mem_profile();
#endif

    g_type_init();
    g_thread_init(NULL);
    gtk_init(&argc, &argv);

    g_set_application_name(PACKAGE_NAME);
#ifdef WIN32
    moddir = g_win32_get_package_installation_directory_of_module(NULL);
    localedir = g_build_path(G_DIR_SEPARATOR_S, moddir, "share", "locale",
            NULL);
    g_free(moddir);
    bindtextdomain(GETTEXT_PACKAGE, localedir);
    g_free(localedir);
#else
    bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
#endif
    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
    textdomain(GETTEXT_PACKAGE);

    withUnique = (g_getenv("TRG_NOUNIQUE") == NULL);

#ifdef HAVE_LIBUNIQUE
    if (withUnique)
        app = unique_app_new_with_commands(TRG_LIBUNIQUE_DOMAIN, NULL,
            "add", COMMAND_ADD, NULL);

    if (withUnique && unique_app_is_running(app)) {
        UniqueCommand command;
        UniqueResponse response;
        UniqueMessageData *message;

        if (args) {
            command = COMMAND_ADD;
            message = unique_message_data_new();
            unique_message_data_set_uris(message, args);
            g_strfreev(args);
        } else {
            command = UNIQUE_ACTIVATE;
            message = NULL;
        }

        response = unique_app_send_message(app, command, message);
        unique_message_data_free(message);

        if (response != UNIQUE_RESPONSE_OK)
            returnValue = EXIT_FAILURE;
    } else {
#elif WIN32
    hMailSlot = CreateFile(TRG_MAILSLOT_NAME, // mailslot name
            GENERIC_WRITE, // mailslot write only
            FILE_SHARE_READ, // required for mailslots
            NULL, // default security attributes
            OPEN_EXISTING, // opens existing mailslot
            FILE_ATTRIBUTE_NORMAL, // normal attributes
            NULL); // no template file

    if (INVALID_HANDLE_VALUE != hMailSlot) {
        returnValue = mailslot_send_message(hMailSlot, args);
    } else {
#endif
        client = trg_client_new();

        curl_global_init(CURL_GLOBAL_ALL);

        window = trg_main_window_new(client, should_be_minimised(argc, argv));

#ifdef HAVE_LIBUNIQUE
        if (withUnique) {
            g_signal_connect(app, "message-received",
                    G_CALLBACK(message_received_cb), window);
        }
#elif WIN32
        g_thread_create(mailslot_recv_thread, window, FALSE, NULL);
#endif

        auto_connect_if_required(window, args);
        gtk_main();

        curl_global_cleanup();
#ifdef HAVE_LIBUNIQUE
    }

    if (withUnique)
        g_object_unref(app);
#elif WIN32
    }
#endif

    return returnValue;
}