changeset 174:002538feba6e multiaccounts

trying to change from some global preferences to preferences per conversation
author mikanbako <maoutwo@gmail.com>
date Sat, 02 Aug 2008 11:31:37 +0900
parents 45da59a32019
children c4a8afde91c0
files pidgin-twitter.c pidgin-twitter.h
diffstat 2 files changed, 152 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin-twitter.c	Thu Jul 31 19:30:24 2008 +0900
+++ b/pidgin-twitter.c	Sat Aug 02 11:31:37 2008 +0900
@@ -540,17 +540,24 @@
     /* fetch friends time line */
     char *request, *header;
     char *basic_auth, *basic_auth_encoded;
+    PurpleAccount *account = purple_conversation_get_account(
+                                    (PurpleConversation *) data);
+    const char *conv_name = purple_conversation_get_name(
+                                   (PurpleConversation *) data);
 
     twitter_debug("called\n");
 
     /* if disabled, just return */
-    if(!purple_prefs_get_bool(OPT_API_BASE_POST))
+    if(!service_account_get_bool(account, conv_name, OPT_API_BASE_POST, FALSE))
         return TRUE;
 
     const char *screen_name =
-        purple_prefs_get_string(OPT_SCREEN_NAME_TWITTER);
+        service_account_get_string(account, conv_name, OPT_SCREEN_NAME, EMPTY);
     const char *password =
-        purple_prefs_get_string(OPT_PASSWORD_TWITTER);
+        service_account_get_string(account,
+                                   conv_name,
+                                   OPT_API_BASE_PASSWORD,
+                                   EMPTY);
 
     if (!screen_name || !password || !screen_name[0] || !password[0]) {
         twitter_debug("screen_name or password is empty\n");
@@ -720,12 +727,18 @@
     char *request, *status, *header;
     const char *url_encoded = purple_url_encode(*buffer);
     char *basic_auth, *basic_auth_encoded;
-
     twitter_message_t *tm;
 
     const char *screen_name =
-        purple_prefs_get_string(OPT_SCREEN_NAME_TWITTER);
-    const char *password = purple_prefs_get_string(OPT_PASSWORD_TWITTER);
+        service_account_get_string(account,
+                                   CONV_NAME_TWITTER,
+                                   OPT_SCREEN_NAME,
+                                   EMPTY);
+    const char *password = 
+        service_account_get_string(account,
+                                   CONV_NAME_TWITTER,
+                                   OPT_API_BASE_PASSWORD,
+                                   EMPTY);
 
     twitter_debug("tm.account: %s\n",
                   purple_account_get_username(account));
@@ -788,7 +801,8 @@
                                            account);
     twitter_debug("called\n");
 
-    if(conv && !strcmp(EMPTY, service_account_get_string(conv,
+    if(conv && !strcmp(EMPTY, service_account_get_string(account,
+                                                         recipient,
                                                          OPT_SCREEN_NAME,
                                                          EMPTY)))
         notify_that_input_screen_name(conv);
@@ -824,7 +838,10 @@
     }
 
     /* update status with Twitter API instead of IM protocol */
-    if (purple_prefs_get_bool(OPT_API_BASE_POST)) {
+    if (service_account_get_bool(account,
+                                 recipient,
+                                 OPT_API_BASE_POST,
+                                 FALSE)) {
         if (buffer && *buffer) {
             post_status_with_api(account, buffer);
             (*buffer)[0] = '\0';
@@ -1003,17 +1020,11 @@
         gchar *m = NULL;
         const char *screen_name = NULL;
 
-        switch(service) {
-        case twitter_service:
-            screen_name = purple_prefs_get_string(OPT_SCREEN_NAME_TWITTER);
-            break;
-        case wassr_service:
-            screen_name = purple_prefs_get_string(OPT_SCREEN_NAME_WASSR);
-            break;
-        case identica_service:
-            screen_name = purple_prefs_get_string(OPT_SCREEN_NAME_IDENTICA);
-            break;
-        }
+        screen_name = service_account_get_string(
+                                      account,
+                                      purple_conversation_get_name(conv),
+                                      OPT_SCREEN_NAME,
+                                      EMPTY);
 
         if (screen_name) {
             m = g_strdup_printf("%s: %s", screen_name, *buffer);
@@ -1507,7 +1518,10 @@
     case twitter_service:
         get_status_with_api((gpointer)conv);
         source.id = g_timeout_add_seconds(
-            purple_prefs_get_int(OPT_API_BASE_GET_INTERVAL),
+            service_account_get_int(purple_conversation_get_account(conv),
+                                    purple_conversation_get_name(conv),
+                                    OPT_API_BASE_GET_INTERVAL,
+                                    TWITTER_DEFAULT_INTERVAL),
             get_status_with_api, (gpointer)conv);
         source.conv = conv;
         attach_to_conv(conv, NULL);
@@ -1526,6 +1540,8 @@
 deleting_conv_cb(PurpleConversation *conv)
 {
     PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
+    PurpleAccount *account = purple_conversation_get_account(conv);
+    const char *conv_name = purple_conversation_get_name(conv);
 
     twitter_debug("called\n");
 
@@ -1537,7 +1553,10 @@
     /* only attach to twitter conversation window */
     switch(service) {
     case twitter_service:
-        if(purple_prefs_get_bool(OPT_API_BASE_POST)) {
+        if(service_account_get_bool(account,
+                                    conv_name,
+                                    OPT_API_BASE_POST,
+                                    FALSE)) {
             g_source_remove_by_user_data((gpointer)conv);
             source.id = 0;
             source.conv = NULL;
@@ -1608,7 +1627,10 @@
     }
 
     /* if we use api, discard incoming IM message. XXX too wild? */
-    if(purple_prefs_get_bool(OPT_API_BASE_POST)) {
+    if(service_account_get_bool(account,
+                                purple_conversation_get_name(conv),
+                                OPT_API_BASE_POST,
+                                FALSE)) {
         g_free(*sender); *sender = NULL;
         g_free(*buffer); *buffer = NULL;
     }
@@ -2360,7 +2382,10 @@
 
     twitter_debug("called\n");
 
-    if(!purple_prefs_get_bool(OPT_API_BASE_POST))
+    if(gc && !service_account_get_bool(purple_connection_get_account(gc),
+                                       CONV_NAME_TWITTER,
+                                       OPT_API_BASE_POST,
+                                       FALSE))
         return;
 
     if (!list)
@@ -3036,6 +3061,8 @@
 build_screen_name_preference_form(PurpleConversation *conv,
                                   GtkWidget *notebook)
 {
+    PurpleAccount *account = purple_conversation_get_account(conv);
+    const char *conv_name = purple_conversation_get_name(conv);
     GtkWidget *tab_label, *child, *line, *container, *widget;
 
     child = gtk_vbox_new(FALSE, 0);
@@ -3055,9 +3082,10 @@
     /* entry */
     widget = gtk_entry_new();
     gtk_entry_set_text(GTK_ENTRY(widget),
-                       service_account_get_string(conv,
+                       service_account_get_string(account,
+                                                  conv_name,
                                                   OPT_SCREEN_NAME,
-                                                  ""));
+                                                  EMPTY));
     gtk_entry_set_activates_default(GTK_ENTRY(widget), TRUE);
     g_object_set_data(G_OBJECT(widget),
                       PLUGIN_ID "-account_pref-key",
@@ -3103,6 +3131,9 @@
 build_twitter_api_preference_form(PurpleConversation *conv,
                                   GtkWidget *notebook)
 {
+    PurpleAccount *account = purple_conversation_get_account(conv);
+    const char *conv_name = purple_conversation_get_name(conv);
+
     GtkWidget *tab_label, *child, *line, *container, *widget;
     GtkObject *adjust;
     int value;
@@ -3116,7 +3147,8 @@
     line = gtk_hbox_new(TRUE, 0);
     widget = gtk_check_button_new_with_label("Get/post statuses via API");
     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget),
-                                 service_account_get_bool(conv,
+                                 service_account_get_bool(account,
+                                                          conv_name,
                                                           OPT_API_BASE_POST,
                                                           FALSE));
     g_object_set_data(G_OBJECT(widget),
@@ -3152,9 +3184,10 @@
     if (gtk_entry_get_invisible_char(GTK_ENTRY(widget)) == '*')
         gtk_entry_set_invisible_char(GTK_ENTRY(widget), PIDGIN_INVISIBLE_CHAR);
     gtk_entry_set_text(GTK_ENTRY(widget),
-                       service_account_get_string(conv,
+                       service_account_get_string(account,
+                                                  conv_name,
                                                   OPT_API_BASE_PASSWORD,
-                                                  ""));
+                                                  EMPTY));
     g_object_set_data(G_OBJECT(widget),
                       PLUGIN_ID "-account_pref-key",
                       OPT_API_BASE_PASSWORD);
@@ -3182,12 +3215,15 @@
     gtk_box_pack_start_defaults(GTK_BOX(line), container);
 
     /* spin button */
-    value = service_account_get_int(conv,
+    value = service_account_get_int(account, conv_name,
                                     OPT_API_BASE_GET_INTERVAL,
                                     0);
     if(value == 0) {
         value = TWITTER_DEFAULT_INTERVAL;
-        purple_prefs_set_int(OPT_API_BASE_GET_INTERVAL, value);
+        service_account_set_int(account,
+                                conv_name,
+                                OPT_API_BASE_GET_INTERVAL,
+                                value);
     }
     adjust = gtk_adjustment_new(value, 40, 3600, 10, 100, 100);
     widget = gtk_spin_button_new(GTK_ADJUSTMENT(adjust), 1.0, 0);
@@ -3226,35 +3262,72 @@
 account_preference_text_changed_cb(GtkEditable *editable, gpointer conv_ptr)
 {
     PurpleConversation *conv = (PurpleConversation *) conv_ptr;
+    PurpleAccount *account = purple_conversation_get_account(conv);
+    const char *conv_name = purple_conversation_get_name(conv);
     const char *key = g_object_get_data(G_OBJECT(editable),
                                         PLUGIN_ID "-account_pref-key");
     gchar *value = gtk_editable_get_chars(editable, 0, -1);
 
-    service_account_set_string(conv, key, value);
+    service_account_set_string(account, conv_name, key, value);
     g_free(value);
 }
 
 static void
+account_preference_api_base_post_changed(void)
+{
+    signed_on_cb(NULL);
+    get_status_with_api((gpointer)(source.conv));
+}
+
+static void
 account_preference_bool_toggled_cb(GtkToggleButton *togglebutton,
                                    gpointer conv_ptr)
 {
     PurpleConversation *conv = (PurpleConversation *) conv_ptr;
+    PurpleAccount *account = purple_conversation_get_account(conv);
+    const char *conv_name = purple_conversation_get_name(conv);
     const char *key = g_object_get_data(G_OBJECT(togglebutton),
                                         PLUGIN_ID "-account_pref-key");
     gboolean value = gtk_toggle_button_get_active(togglebutton);
 
-    service_account_set_bool(conv, key, value);
+    service_account_set_bool(account, conv_name, key, value);
+
+    if(!strcmp(OPT_API_BASE_POST, key))
+        account_preference_api_base_post_changed();
+}
+
+static void
+account_preference_interval_changed(PurpleAccount *account,
+                                    const char *conv_name)
+{
+    /* remove idle func */
+    g_source_remove_by_user_data((gpointer)(source.conv));
+
+    /* add idle func */
+    if(purple_prefs_get_bool(OPT_API_BASE_POST)) {
+        source.id = g_timeout_add_seconds(
+            service_account_get_int(account,
+                                    conv_name,
+                                    OPT_API_BASE_GET_INTERVAL,
+                                    TWITTER_DEFAULT_INTERVAL),
+            get_status_with_api, (gpointer)(source.conv));
+    }
 }
 
 static void
 account_preference_spin_changed_cb(GtkSpinButton *spinbutton, gpointer conv_ptr)
 {
     PurpleConversation *conv = (PurpleConversation *) conv_ptr;
+    PurpleAccount *account = purple_conversation_get_account(conv);
+    const char *conv_name = purple_conversation_get_name(conv);
     const char *key = g_object_get_data(G_OBJECT(spinbutton),
                                          PLUGIN_ID "-account_pref-key");
     gint value = gtk_spin_button_get_value_as_int(spinbutton);
 
-    service_account_set_int(conv, key, value);
+    service_account_set_int(account, conv_name, key, value);
+
+    if(!strcmp(OPT_API_BASE_GET_INTERVAL, key))
+        account_preference_interval_changed(account, conv_name);
 }
 
 static void
@@ -3276,30 +3349,30 @@
 }
 
 static gchar*
-create_key_with_service_account(PurpleConversation *conv, const char *key)
+create_key_with_service_account(const char *conv_name, const char *key)
 {
-    return g_strdup_printf("%s:%s", key, conv->name);
+    return g_strdup_printf("%s:%s", key, conv_name);
 }
 
 static void
-service_account_set_string(PurpleConversation *conv,
-                                      const char *key,
-                                      const char *value)
+service_account_set_string(PurpleAccount *account,
+                           const char *conv_name,
+                           const char *key,
+                           const char *value)
 {
-    PurpleAccount *account = purple_conversation_get_account(conv);
-    gchar *key_with_account = create_key_with_service_account(conv, key);
+    gchar *key_with_account = create_key_with_service_account(conv_name, key);
 
     purple_account_set_string(account, key_with_account, value);
     g_free(key_with_account);
 }
 
 static const char*
-service_account_get_string(PurpleConversation *conv,
-                                      const char *key,
-                                      const char *default_value)
+service_account_get_string(PurpleAccount *account,
+                           const char *conv_name,
+                           const char *key,
+                           const char *default_value)
 {
-    PurpleAccount *account = purple_conversation_get_account(conv);
-    gchar *key_with_account = create_key_with_service_account(conv, key);
+    gchar *key_with_account = create_key_with_service_account(conv_name, key);
     const char *value = purple_account_get_string(account,
                                                   key_with_account,
                                                   default_value);
@@ -3310,27 +3383,27 @@
 }
 
 static void
-service_account_set_int(PurpleConversation *conv,
-                                   const char *key,
-                                   int value)
+service_account_set_int(PurpleAccount *account,
+                        const char *conv_name,
+                        const char *key,
+                        int value)
 {
-    PurpleAccount *account = purple_conversation_get_account(conv);
-    gchar *key_with_account = create_key_with_service_account(conv, key);
+    gchar *key_with_account = create_key_with_service_account(conv_name, key);
 
     purple_account_set_int(account, key_with_account, value);
     g_free(key_with_account);
 }
 
 static int
-service_account_get_int(PurpleConversation *conv,
-                                   const char *key,
-                                   int default_value)
+service_account_get_int(PurpleAccount *account,
+                        const char *conv_name,
+                        const char *key,
+                        int default_value)
 {
-    PurpleAccount *account = purple_conversation_get_account(conv);
-    gchar *key_with_account = create_key_with_service_account(conv, key);
+    gchar *key_with_account = create_key_with_service_account(conv_name, key);
     int value = purple_account_get_int(account,
-                                        key_with_account,
-                                        default_value);
+                                       key_with_account,
+                                       default_value);
 
     g_free(key_with_account);
 
@@ -3338,24 +3411,24 @@
 }
 
 static void
-service_account_set_bool(PurpleConversation *conv,
-                                    const char *key,
-                                    gboolean value)
+service_account_set_bool(PurpleAccount *account,
+                         const char *conv_name,
+                         const char *key,
+                         gboolean value)
 {
-    PurpleAccount *account = purple_conversation_get_account(conv);
-    gchar *key_with_account = create_key_with_service_account(conv, key);
+    gchar *key_with_account = create_key_with_service_account(conv_name, key);
 
     purple_account_set_bool(account, key_with_account, value);
     g_free(key_with_account);
 }
 
 static gboolean
-service_account_get_bool(PurpleConversation *conv,
-                                    const char *key,
-                                    gboolean default_value)
+service_account_get_bool(PurpleAccount *account,
+                         const char *conv_name,
+                         const char *key,
+                         gboolean default_value)
 {
-    PurpleAccount *account = purple_conversation_get_account(conv);
-    gchar *key_with_account = create_key_with_service_account(conv, key);
+    gchar *key_with_account = create_key_with_service_account(conv_name, key);
     gboolean value = purple_account_get_bool(account,
                                              key_with_account,
                                              default_value);
--- a/pidgin-twitter.h	Thu Jul 31 19:30:24 2008 +0900
+++ b/pidgin-twitter.h	Sat Aug 02 11:31:37 2008 +0900
@@ -122,6 +122,9 @@
 #define OPT_SCREEN_NAME         OPT_PIDGINTWITTER "/screen_name"
 #define OPT_API_BASE_PASSWORD      OPT_PIDGINTWITTER "/api_base_password"
 
+/* conversation's name */
+#define CONV_NAME_TWITTER "twitter@twitter.com"
+
 /* formats and templates */
 #define RECIPIENT_FORMAT_TWITTER "@<a href='http://twitter.com/%s'>%s</a>"
 #define SENDER_FORMAT_TWITTER   "%s<a href='http://twitter.com/%s'>%s</a>: "
@@ -225,15 +228,17 @@
 static void destroyed_pref_button_cb(GtkWidget *pref_button, gpointer dialog);
 static void closed_account_preference_window_cb(GtkDialog *dialog, gint response_id, gpointer pref_button);
 static void account_preference_text_changed_cb(GtkEditable *editable, gpointer conv_ptr);
+static void account_preference_api_base_post_changed(void);
 static void account_preference_bool_toggled_cb(GtkToggleButton *togglebutton, gpointer conv_ptr);
+static void account_preference_interval_changed(PurpleAccount *account, const char *conv_name);
 static void account_preference_spin_changed_cb(GtkSpinButton *spinbutton, gpointer conv_ptr);
-static gchar* create_key_with_service_account(PurpleConversation *conv, const char *key);
-static void service_account_set_string(PurpleConversation *conv, const char *key, const char *value);
-static const char* service_account_get_string(PurpleConversation *conv, const char *key, const char *default_value);
-static void service_account_set_int(PurpleConversation *conv, const char *key, gboolean value);
-static gboolean service_account_get_int(PurpleConversation *conv, const char *key, gboolean default_value);
-static void service_account_set_bool(PurpleConversation *conv, const char *key, gboolean value);
-static gboolean service_account_get_bool(PurpleConversation *conv, const char *key, gboolean default_value);
+static gchar* create_key_with_service_account(const char *conv_name, const char *key);
+static void service_account_set_string(PurpleAccount *account, const char *conv_name, const char *key, const char *value);
+static const char* service_account_get_string(PurpleAccount *account, const char *conv_name, const char *key, const char *default_value);
+static void service_account_set_int(PurpleAccount *account, const char *conv_name, const char *key, gboolean value);
+static gboolean service_account_get_int(PurpleAccount *account, const char *conv_name, const char *key, gboolean default_value);
+static void service_account_set_bool(PurpleAccount *account, const char *conv_name, const char *key, gboolean value);
+static gboolean service_account_get_bool(PurpleAccount *account, const char *conv_name, const char *key, gboolean default_value);
 
 static void parse_user(xmlNode *user, status_t *st);
 static void parse_status(xmlNode *status, status_t *st);