changeset 1008:bbf53cd43fbb

Functions to update room bookmarks
author Mikael Berthe <mikael@lilotux.net>
date Sun, 12 Nov 2006 10:45:13 +0100
parents 35b83557ffe1
children c112423ac012
files mcabber/src/jab_iq.c mcabber/src/jab_priv.h mcabber/src/jabglue.c
diffstat 3 files changed, 88 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/src/jab_iq.c	Sat Nov 11 12:14:10 2006 +0100
+++ b/mcabber/src/jab_iq.c	Sun Nov 12 10:45:13 2006 +0100
@@ -34,6 +34,9 @@
 #include "hbuf.h"
 
 
+// Bookmarks for IQ:private storage
+xmlnode bookmarks;
+
 static GSList *iqs_list;
 
 // Enum for vCard attributes
@@ -608,6 +611,8 @@
     if (p && !strcmp(p, "conference"))
       storage_bookmarks_parse_conference(x);
   }
+  xmlnode_free(bookmarks);
+  bookmarks = xmlnode_dup(ansqry);
 }
 
 static void request_storage_bookmarks(void)
@@ -895,4 +900,20 @@
   }
 }
 
+//  send_storage_bookmarks()
+// Send the current bookmarks node to update the server.
+// Note: the sender should check we're online.
+void send_storage_bookmarks(void)
+{
+  eviqs *iqn;
+
+  if (!bookmarks) return;
+
+  iqn = iqs_new(JPACKET__SET, NS_PRIVATE, "storage", IQS_DEFAULT_TIMEOUT);
+  xmlnode_insert_node(xmlnode_get_tag(iqn->xmldata, "query"), bookmarks);
+
+  jab_send(jc, iqn->xmldata);
+  iqs_del(iqn->id); // XXX
+}
+
 /* vim: set expandtab cindent cinoptions=>2\:2(0:  For Vim users... */
--- a/mcabber/src/jab_priv.h	Sat Nov 11 12:14:10 2006 +0100
+++ b/mcabber/src/jab_priv.h	Sun Nov 12 10:45:13 2006 +0100
@@ -29,6 +29,7 @@
 #define IQS_CONTEXT_ERROR   2U
 
 extern enum enum_jstate jstate;
+extern xmlnode bookmarks;
 
 extern char *mcabber_version(void);
 
@@ -44,6 +45,7 @@
 void request_version(const char *fulljid);
 void request_time(const char *fulljid);
 void request_vcard(const char *barejid);
+void send_storage_bookmarks(void);
 
 #endif /* __JAB_PRIV_H__ */
 
--- a/mcabber/src/jabglue.c	Sat Nov 11 12:14:10 2006 +0100
+++ b/mcabber/src/jabglue.c	Sun Nov 12 10:45:13 2006 +0100
@@ -1154,6 +1154,63 @@
   jb_reset_keepalive();
 }
 
+//  jb_set_storage_bookmark(roomid, name, nick, passwd, autojoin)
+// Update the private storage bookmarks: add a conference room.
+// If name is nil, we remove the bookmark.
+void jb_set_storage_bookmark(const char *roomid, const char *name,
+                             const char *nick, const char *passwd, int autojoin)
+{
+  xmlnode x;
+
+  if (!roomid)
+    return;
+
+  // If we have no bookmarks, probably the server doesn't support them.
+  if (!bookmarks) {
+    scr_LogPrint(LPRINT_LOGNORM,
+                 "Sorry, your server doesn't seem to support private storage.");
+    return;
+  }
+
+  // Walk through the storage tags
+  x = xmlnode_get_firstchild(bookmarks);
+  for ( ; x; x = xmlnode_get_nextsibling(x)) {
+    const char *jid;
+    const char *p;
+    p = xmlnode_get_name(x);
+    // If the current node is a conference item, see if we have to replace it.
+    if (p && !strcmp(p, "conference")) {
+      jid = xmlnode_get_attrib(x, "jid");
+      if (!jid)
+        continue;
+      if (!strcmp(jid, roomid)) {
+        // We've found a bookmark for this room.  Let's hide it and we'll
+        // create a new one.
+        xmlnode_hide(x);
+        break;
+      }
+    }
+  }
+
+  // Let's create a node/bookmark for this roomid, if the name is not NULL.
+  if (name) {
+    x = xmlnode_insert_tag(bookmarks, "conference");
+    xmlnode_put_attrib(x, "jid", roomid);
+    xmlnode_put_attrib(x, "name", name);
+    xmlnode_put_attrib(x, "autojoin", autojoin ? "1" : "0");
+    if (nick)
+      xmlnode_insert_cdata(xmlnode_insert_tag(x, "nick"), nick, -1);
+    if (passwd)
+      xmlnode_insert_cdata(xmlnode_insert_tag(x, "password"), passwd, -1);
+  }
+
+  if (online)
+    send_storage_bookmarks();
+  else
+    scr_LogPrint(LPRINT_LOGNORM,
+                 "Warning: you're not connected to the server.");
+}
+
 static void gotmessage(char *type, const char *from, const char *body,
                        const char *enc, time_t timestamp)
 {
@@ -1317,11 +1374,15 @@
 
         online = FALSE;
         mystatus = offline;
-        if (mystatusmsg) {
-          g_free(mystatusmsg);
-          mystatusmsg = NULL;
-        }
+        // Free status message
+        g_free(mystatusmsg);
+        mystatusmsg = NULL;
+        // Free bookmarks
+        xmlnode_free(bookmarks);
+        bookmarks = NULL;
+        // Free roster
         roster_free();
+        // Update display
         update_roster = TRUE;
         scr_UpdateBuddyWindow();
         break;