This repository has been archived by the owner on Feb 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
104 lines (81 loc) · 1.81 KB
/
test.c
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
#include "scrobble.h"
#include <string.h> /* for strdup */
#include <glib.h>
#include <glib-object.h>
static GMainLoop *main_loop;
static GKeyFile *keyfile;
static sr_session_t *lastfm;
static sr_session_t *librefm;
static gboolean
load_cred(sr_session_t *s,
const char *id)
{
gchar *username = NULL, *password = NULL;
gboolean ok;
username = g_key_file_get_string(keyfile, id, "username", NULL);
password = g_key_file_get_string(keyfile, id, "password", NULL);
ok = username && password;
if (!ok)
goto leave;
sr_session_set_cred(s, username, password);
leave:
g_free(username);
g_free(password);
return ok;
}
static void error_cb(int fatal,
const char *msg)
{
g_warning(msg);
if (fatal)
g_main_loop_quit(main_loop);
}
static gboolean timeout(void *data)
{
sr_session_submit(lastfm);
sr_session_submit(librefm);
return TRUE;
}
static sr_session_t *
get_session(const char *url,
const char *id)
{
sr_session_t *s;
s = sr_session_new(url, "tst", "1.0");
s->error_cb = error_cb;
if (!load_cred(s, id)) {
sr_session_free(s);
return NULL;
}
sr_session_load_list(s, "list");
sr_session_handshake(s);
return s;
}
int main(void)
{
gchar *file;
gboolean ok;
g_type_init();
if (!g_thread_supported())
g_thread_init(NULL);
keyfile = g_key_file_new();
file = g_build_filename(g_get_user_config_dir(),
"scrobbler", NULL);
ok = g_key_file_load_from_file(keyfile, file, G_KEY_FILE_NONE, NULL);
if (!ok)
goto leave;
lastfm = get_session(SR_LASTFM_URL, "lastfm");
if (!lastfm)
goto leave;
librefm = get_session(SR_LIBREFM_URL, "librefm");
if (!librefm)
goto leave;
g_timeout_add_seconds(30, timeout, NULL);
main_loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(main_loop);
leave:
sr_session_free(lastfm);
sr_session_free(librefm);
g_key_file_free(keyfile);
return 0;
}