comparison src/scrobbler/xmms_scrobbler.c @ 344:03c1ae10bc8d trunk

[svn] - Merge audacious-scrobbler III, new features include: + Gerpok support + Better queue management
author nenolod
date Fri, 08 Dec 2006 19:31:43 -0800
parents 0eb1e99b7748
children
comparison
equal deleted inserted replaced
343:5d3f4b156197 344:03c1ae10bc8d
1 #include "settings.h"
1 #include "config.h" 2 #include "config.h"
2 3
3 #include <glib.h> 4 #include <glib.h>
4 #include <glib/gi18n.h> 5 #include <glib/gi18n.h>
5 6
19 #include <ctype.h> 20 #include <ctype.h>
20 #include <wchar.h> 21 #include <wchar.h>
21 #include <sys/time.h> 22 #include <sys/time.h>
22 23
23 #include "scrobbler.h" 24 #include "scrobbler.h"
25 #include "gerpok.h"
24 #include "gtkstuff.h" 26 #include "gtkstuff.h"
25 #include "config.h" 27 #include "config.h"
26 #include "fmt.h" 28 #include "fmt.h"
27 #include "configure.h" 29 #include "configure.h"
28 30
35 37
36 static void init(void); 38 static void init(void);
37 static void cleanup(void); 39 static void cleanup(void);
38 static void *xs_thread(void *); 40 static void *xs_thread(void *);
39 static void *hs_thread(void *); 41 static void *hs_thread(void *);
40 static int going; 42 static int sc_going, ge_going;
41 static GtkWidget *cfgdlg; 43 static GtkWidget *cfgdlg;
42 44
43 static GThread *pt_scrobbler; 45 static GThread *pt_scrobbler;
44 static GMutex *m_scrobbler; 46 static GMutex *m_scrobbler;
45 static GThread *pt_handshake; 47 static GThread *pt_handshake;
57 }; 59 };
58 60
59 static void init(void) 61 static void init(void)
60 { 62 {
61 char *username = NULL, *password = NULL; 63 char *username = NULL, *password = NULL;
64 char *ge_username = NULL, *ge_password = NULL;
62 ConfigDb *cfgfile; 65 ConfigDb *cfgfile;
63 going = 1; 66 sc_going = 1;
67 ge_going = 1;
64 GError **moo = NULL; 68 GError **moo = NULL;
65 cfgdlg = create_cfgdlg(); 69 cfgdlg = create_cfgdlg();
66 70
67 prefswin_page_new(cfgdlg, "Last.FM", DATA_DIR "/images/audioscrobbler.png"); 71 prefswin_page_new(cfgdlg, "Scrobbler", DATA_DIR "/images/audioscrobbler.png");
68 72
69 if ((cfgfile = bmp_cfg_db_open()) != NULL) { 73 if ((cfgfile = bmp_cfg_db_open()) != NULL) {
70 bmp_cfg_db_get_string(cfgfile, "audioscrobbler", "username", 74 bmp_cfg_db_get_string(cfgfile, "audioscrobbler", "username",
71 &username); 75 &username);
72 bmp_cfg_db_get_string(cfgfile, "audioscrobbler", "password", 76 bmp_cfg_db_get_string(cfgfile, "audioscrobbler", "password",
73 &password); 77 &password);
78 bmp_cfg_db_get_string(cfgfile, "audioscrobbler", "ge_username",
79 &ge_username);
80 bmp_cfg_db_get_string(cfgfile, "audioscrobbler", "ge_password",
81 &ge_password);
74 bmp_cfg_db_close(cfgfile); 82 bmp_cfg_db_close(cfgfile);
75 } 83 }
76 if ((!username || !password) || (!*username || !*password)) { 84
77 pdebug("username/password not found - not starting", 85 if ((!username || !password) || (!*username || !*password))
86 {
87 pdebug("username/password not found - not starting last.fm support",
78 DEBUG); 88 DEBUG);
79 going = 0; 89 sc_going = 0;
80 return; 90 }
81 } 91 else
82 sc_init(username, password); 92 sc_init(username, password);
93
83 g_free(username); 94 g_free(username);
84 g_free(password); 95 g_free(password);
96
97 if ((!ge_username || !ge_password) || (!*ge_username || !*ge_password))
98 {
99 pdebug("username/password not found - not starting Gerpok support",
100 DEBUG);
101 ge_going = 0;
102 }
103 else
104 gerpok_sc_init(ge_username, ge_password);
105
106 g_free(ge_username);
107 g_free(ge_password);
108
85 m_scrobbler = g_mutex_new(); 109 m_scrobbler = g_mutex_new();
86 if ((pt_scrobbler = g_thread_create(xs_thread, m_scrobbler, TRUE, moo)) == NULL) { 110 if ((pt_scrobbler = g_thread_create(xs_thread, m_scrobbler, TRUE, moo)) == NULL)
111 {
87 pdebug(fmt_vastr("Error creating scrobbler thread: %s", moo), DEBUG); 112 pdebug(fmt_vastr("Error creating scrobbler thread: %s", moo), DEBUG);
88 going = 0; 113 sc_going = 0;
114 ge_going = 0;
89 return; 115 return;
90 } 116 }
91 if ((pt_handshake = g_thread_create(hs_thread, m_scrobbler, TRUE, NULL)) == NULL) { 117
92 pdebug("Error creating handshake thread", DEBUG); 118 if ((pt_handshake = g_thread_create(hs_thread, m_scrobbler, TRUE, NULL)) == NULL)
93 going = 0; 119 {
120 pdebug(fmt_vastr("Error creating handshake thread: %s", moo), DEBUG);
121 sc_going = 0;
122 ge_going = 0;
94 return; 123 return;
95 } 124 }
125
96 pdebug("plugin started", DEBUG); 126 pdebug("plugin started", DEBUG);
97 } 127 }
98 128
99 static void cleanup(void) 129 static void cleanup(void)
100 { 130 {
101 g_free (xmms_scrobbler.description); 131 g_free (xmms_scrobbler.description);
102 xmms_scrobbler.description = NULL; 132 xmms_scrobbler.description = NULL;
103 133
104 prefswin_page_destroy(cfgdlg); 134 prefswin_page_destroy(cfgdlg);
105 135
106 if (!going) 136 if (!sc_going && !ge_going)
107 return; 137 return;
108 pdebug("about to lock mutex", DEBUG); 138 pdebug("about to lock mutex", DEBUG);
109 g_mutex_lock(m_scrobbler); 139 g_mutex_lock(m_scrobbler);
110 pdebug("locked mutex", DEBUG); 140 pdebug("locked mutex", DEBUG);
111 going = 0; 141 sc_going = 0;
142 ge_going = 0;
112 g_mutex_unlock(m_scrobbler); 143 g_mutex_unlock(m_scrobbler);
113 pdebug("joining threads", DEBUG); 144 pdebug("joining threads", DEBUG);
114 g_thread_join(pt_scrobbler); 145 g_thread_join(pt_scrobbler);
115 146
116 g_thread_join(pt_handshake); 147 g_thread_join(pt_handshake);
117 148
118 sc_cleaner(); 149 sc_cleaner();
150 gerpok_sc_cleaner();
119 } 151 }
120 152
121 static char ishttp(const char *a) 153 static char ishttp(const char *a)
122 { 154 {
123 char *tmp, *bp; 155 char *tmp, *bp;
375 { 407 {
376 errorbox_show(sc_fetch_error()); 408 errorbox_show(sc_fetch_error());
377 sc_clear_error(); 409 sc_clear_error();
378 } 410 }
379 411
412 if(gerpok_sc_catch_error())
413 {
414 errorbox_show(gerpok_sc_fetch_error());
415 gerpok_sc_clear_error();
416 }
417
380 /* Check for ability to submit */ 418 /* Check for ability to submit */
381 dosubmit = get_song_status(); 419 dosubmit = get_song_status();
382 420
383 if(dosubmit.dosubmit) { 421 if(dosubmit.dosubmit) {
384 TitleInput *tuple; 422 TitleInput *tuple;
395 pdebug(fmt_vastr( 433 pdebug(fmt_vastr(
396 "submitting artist: %s, title: %s", 434 "submitting artist: %s, title: %s",
397 tuple->performer, tuple->track_name), DEBUG); 435 tuple->performer, tuple->track_name), DEBUG);
398 sc_addentry(m_scrobbler, tuple, 436 sc_addentry(m_scrobbler, tuple,
399 dosubmit.len/1000); 437 dosubmit.len/1000);
438 gerpok_sc_addentry(m_scrobbler, tuple,
439 dosubmit.len/1000);
400 } 440 }
401 else 441 else
402 pdebug("tuple does not contain an artist or a title, not submitting.", DEBUG); 442 pdebug("tuple does not contain an artist or a title, not submitting.", DEBUG);
403 } 443 }
404 g_mutex_lock(m_scrobbler); 444 g_mutex_lock(m_scrobbler);
405 run = going; 445 run = (sc_going != 0 || ge_going != 0);
406 g_mutex_unlock(m_scrobbler); 446 g_mutex_unlock(m_scrobbler);
407 g_usleep(100000); 447 g_usleep(100000);
408 } 448 }
409 pdebug("scrobbler thread: exiting", DEBUG); 449 pdebug("scrobbler thread: exiting", DEBUG);
410 g_thread_exit(NULL); 450 g_thread_exit(NULL);
420 { 460 {
421 if(sc_idle(m_scrobbler)) 461 if(sc_idle(m_scrobbler))
422 { 462 {
423 pdebug("Giving up due to fatal error", DEBUG); 463 pdebug("Giving up due to fatal error", DEBUG);
424 g_mutex_lock(m_scrobbler); 464 g_mutex_lock(m_scrobbler);
425 going = 0; 465 sc_going = 0;
426 g_mutex_lock(m_scrobbler); 466 g_mutex_lock(m_scrobbler);
427 } 467 }
468
469 if(gerpok_sc_idle(m_scrobbler))
470 {
471 pdebug("Giving up due to fatal error", DEBUG);
472 g_mutex_lock(m_scrobbler);
473 ge_going = 0;
474 g_mutex_lock(m_scrobbler);
475 }
476
428 g_mutex_lock(m_scrobbler); 477 g_mutex_lock(m_scrobbler);
429 run = going; 478 run = (sc_going != 0 || ge_going != 0);
430 g_mutex_unlock(m_scrobbler); 479 g_mutex_unlock(m_scrobbler);
431 g_usleep(1000000); 480 g_usleep(1000000);
432 } 481 }
433 pdebug("handshake thread: exiting", DEBUG); 482 pdebug("handshake thread: exiting", DEBUG);
434 g_thread_exit(NULL); 483 g_thread_exit(NULL);