summaryrefslogtreecommitdiff
path: root/src/main.c
blob: df9bce97f7ee92b65ba8d66ba2d53ebfcb783f4a (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
/*
 * transmission-remote-gtk - A GTK RPC client to Transmission
 * Copyright (C) 2011-2013  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>
#include <fontconfig/fontconfig.h>

#include "trg-gtk-app.h"
#if WIN32
#include "win32-mailslot.h"
#endif

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

/* Handle arguments and start the main window.
 *
 * either GtkApplication - replaces libunique, GTK3 only, and non-win32.
 * or win32 API mailslots.
 *
 * win32 could possibly run from GtkApplication now, mailslots were needed
 * for GTK2 (support removed).
 */

#if !WIN32

/* GtkApplication - the replacement for libunique.
 * This is implemented in trg-gtk-app.c
 */

static gint trg_gtkapp_init(TrgClient * client, int argc, char *argv[])
{
    TrgGtkApp *gtk_app = trg_gtk_app_new(client);

    gint exitCode = g_application_run(G_APPLICATION(gtk_app), argc, argv);

    g_object_unref(gtk_app);

    return exitCode;
}

#elif WIN32

static gint
trg_win32_init(TrgClient * client, int argc, char *argv[], gchar ** args)
{
    gchar *moddir =
        g_win32_get_package_installation_directory_of_module(NULL);
    gchar *localedir =
        g_build_path(G_DIR_SEPARATOR_S, moddir, "share", "locale",
                     NULL);

    bindtextdomain(GETTEXT_PACKAGE, localedir);
    g_free(moddir);
    g_free(localedir);

    if (!mailslot_send_message(args)) {
        TrgMainWindow *window =
            trg_main_window_new(client, should_be_minimised(argc, argv));
        trg_main_window_set_start_args(window, args);
        auto_connect_if_required(window);
        mailslot_start_background_listener(window);
        gtk_main();
    }

    return EXIT_SUCCESS;
}

#else

static gint
trg_simple_init(TrgClient * client, int argc, char *argv[], gchar ** args)
{
    TrgMainWindow *window =
        trg_main_window_new(client, should_be_minimised(argc, argv));
    trg_main_window_set_start_args(window, args);
    auto_connect_if_required(window);
    gtk_main();

    return EXIT_SUCCESS;
}

#endif

/* Win32 mailslots. I've implemented this in win32-mailslot.c */

#if !WIN32
static void trg_non_win32_init(void)
{
    bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
}
#endif

static void trg_cleanup(void)
{
    curl_global_cleanup();
}

#if WIN32

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;
}

#endif

int main(int argc, char *argv[])
{
#if WIN32
    gchar **args;
#endif
    gint exitCode = EXIT_SUCCESS;
    TrgClient *client;

    gtk_init(&argc, &argv);

#if WIN32
    args = convert_args(argc, argv);
#endif

    curl_global_init(CURL_GLOBAL_ALL);
    client = trg_client_new();

    g_set_application_name(PACKAGE_NAME);
    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
    textdomain(GETTEXT_PACKAGE);

#ifdef WIN32
    exitCode = trg_win32_init(client, argc, argv, args);
#else
    trg_non_win32_init();
    exitCode = trg_gtkapp_init(client, argc, argv);
#endif

    trg_cleanup();

    return exitCode;
}