summaryrefslogtreecommitdiff
path: root/src/trg-client.h
blob: a0bee337cce7faf58f646a6ac2f634ae8293077c (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
/*
 * 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.
 */

/* trg-client.h */

#ifndef _TRG_CLIENT_H_
#define _TRG_CLIENT_H_

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

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

#include <json-glib/json-glib.h>
#include <glib-object.h>

#include "trg-prefs.h"
#include "session-get.h"

#define TRANSMISSION_MIN_SUPPORTED 2.0
#define X_TRANSMISSION_SESSION_ID_HEADER_PREFIX "X-Transmission-Session-Id: "

#define TORRENT_GET_MODE_FIRST 0
#define TORRENT_GET_MODE_ACTIVE 1
#define TORRENT_GET_MODE_INTERACTION 2
#define TORRENT_GET_MODE_UPDATE 3

#define TORRENT_GET_TAG_MODE_FULL -1
#define TORRENT_GET_TAG_MODE_UPDATE -2

#define TRG_NO_HOSTNAME_SET -2

#define HTTP_URI_PREFIX "http"
#define HTTPS_URI_PREFIX "https"
#define HTTP_OK 200
#define HTTP_CONFLICT 409

#define FAIL_JSON_DECODE -2
#define FAIL_RESPONSE_UNSUCCESSFUL -3
#define DISPATCH_POOL_SIZE 3

typedef struct {
    int status;
    int size;
    char *raw;
    JsonObject *obj;
    gpointer cb_data;
} trg_response;

typedef struct {
    gint connid;
    JsonNode *node;
    gchar *str;
    GSourceFunc callback;
    gpointer cb_data;
} trg_request;

typedef struct _TrgClientPrivate TrgClientPrivate;

G_BEGIN_DECLS
#define TRG_TYPE_CLIENT trg_client_get_type()
#define TRG_CLIENT(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST ((obj), TRG_TYPE_CLIENT, TrgClient))
#define TRG_CLIENT_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST ((klass), TRG_TYPE_CLIENT, TrgClientClass))
#define TRG_IS_CLIENT(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TRG_TYPE_CLIENT))
#define TRG_IS_CLIENT_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_TYPE ((klass), TRG_TYPE_CLIENT))
#define TRG_CLIENT_GET_CLASS(obj) \
  (G_TYPE_INSTANCE_GET_CLASS ((obj), TRG_TYPE_CLIENT, TrgClientClass))
    typedef struct {
    GObject parent;
    TrgClientPrivate *priv;
} TrgClient;

typedef struct {
    GObjectClass parent_class;
    void (*session_updated) (TrgClient * tc, JsonObject * session,
                             gpointer data);

} TrgClientClass;

/* Thread local storage (TLS).
 * CURL clients can't be used concurrently.
 * So create one instance for each thread in the thread pool.
 */
typedef struct {
    /* Use a serial to figure out when there's been a configuration change
     * by comparing with priv->serial.
     * We lock updating (and checking for updates) with priv->configMutex
     */
    int serial;
    CURL *curl;
} trg_tls;

/* stuff that used to be in http.h */
void trg_response_free(trg_response * response);
int trg_http_perform(TrgClient * client, gchar * reqstr,
                     trg_response * reqrsp);
/* end http.h*/

/* stuff that used to be in dispatch.c */
trg_response *dispatch(TrgClient * client, JsonNode * req);
trg_response *dispatch_str(TrgClient * client, gchar * req);
gboolean dispatch_async(TrgClient * client, JsonNode * req,
                        GSourceFunc callback, gpointer data);
/* end dispatch.c*/

GType trg_client_get_type(void);

TrgClient *trg_client_new(void);
TrgPrefs *trg_client_get_prefs(TrgClient * tc);
int trg_client_populate_with_settings(TrgClient * tc);
void trg_client_set_session(TrgClient * tc, JsonObject * session);
gdouble trg_client_get_version(TrgClient * tc);
const gchar *trg_client_get_version_string(TrgClient * tc);
gint64 trg_client_get_rpc_version(TrgClient * tc);
gchar *trg_client_get_password(TrgClient * tc);
gchar *trg_client_get_username(TrgClient * tc);
gchar *trg_client_get_url(TrgClient * tc);
gchar *trg_client_get_session_id(TrgClient * tc);
void trg_client_set_session_id(TrgClient * tc, gchar * session_id);
#ifndef CURL_NO_SSL
gboolean trg_client_get_ssl(TrgClient * tc);
gboolean trg_client_get_ssl_validate(TrgClient * tc);
#endif
gchar *trg_client_get_proxy(TrgClient * tc);
gint64 trg_client_get_serial(TrgClient * tc);
void trg_client_thread_pool_push(TrgClient * tc, gpointer data,
                                 GError ** err);
void trg_client_set_torrent_table(TrgClient * tc, GHashTable * table);
GHashTable *trg_client_get_torrent_table(TrgClient * tc);
JsonObject *trg_client_get_session(TrgClient * tc);
void trg_client_status_change(TrgClient * tc, gboolean connected);
gboolean trg_client_is_connected(TrgClient * tc);
void trg_client_configunlock(TrgClient * tc);
void trg_client_configlock(TrgClient * tc);
guint trg_client_inc_failcount(TrgClient * tc);
guint trg_client_get_failcount(TrgClient * tc);
void trg_client_reset_failcount(TrgClient * tc);
void trg_client_inc_serial(TrgClient * tc);
void trg_client_inc_connid(TrgClient * tc);
gboolean trg_client_update_session(TrgClient * tc, GSourceFunc callback,
                                   gpointer data);
gboolean trg_client_get_seed_ratio_limited(TrgClient * tc);
gdouble trg_client_get_seed_ratio_limit(TrgClient * tc);

G_END_DECLS
#endif                          /* _TRG_CLIENT_H_ */