comparison mcabber/src/commands.c @ 1022:4c8d7b558e83

Annotations listing (/roster note in the status buffer)
author Mikael Berthe <mikael@lilotux.net>
date Sat, 18 Nov 2006 10:44:58 +0100
parents 97f862e44d5d
children 94d9a3cbb211
comparison
equal deleted inserted replaced
1021:97f862e44d5d 1022:4c8d7b558e83
465 buddylist_build(); 465 buddylist_build();
466 update_roster = TRUE; 466 update_roster = TRUE;
467 } 467 }
468 } 468 }
469 469
470 // display_and_free_note(note, winId)
471 // Display the note information in the winId buffer, and free note
472 // (winId is a bare jid or NULL for the status window, in which case we
473 // display the note jid too)
474 static void display_and_free_note(struct annotation *note, const char *winId)
475 {
476 gchar tbuf[128];
477 GString *sbuf;
478 guint msg_flag = HBB_PREFIX_INFO;
479 /* We use the flag prefix_info for the first line, and prefix_none
480 for the other lines, for better readability */
481
482 if (!note)
483 return;
484
485 sbuf = g_string_new("");
486
487 if (!winId) {
488 // We're writing to the status window, so let's show the jid too.
489 g_string_printf(sbuf, "Annotation on <%s>", note->jid);
490 scr_WriteIncomingMessage(winId, sbuf->str, 0, msg_flag);
491 msg_flag = HBB_PREFIX_NONE;
492 }
493
494 // If we have the creation date, display it
495 if (note->cdate) {
496 strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S",
497 localtime(&note->cdate));
498 g_string_printf(sbuf, "Note created %s", tbuf);
499 scr_WriteIncomingMessage(winId, sbuf->str, 0, msg_flag);
500 msg_flag = HBB_PREFIX_NONE;
501 }
502 // If we have the modification date, display it
503 // unless it's the same as the creation date
504 if (note->mdate && note->mdate != note->cdate) {
505 strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S",
506 localtime(&note->mdate));
507 g_string_printf(sbuf, "Note modified %s", tbuf);
508 scr_WriteIncomingMessage(winId, sbuf->str, 0, msg_flag);
509 msg_flag = HBB_PREFIX_NONE;
510 }
511 // Note text
512 g_string_printf(sbuf, "Note: %s", note->text);
513 scr_WriteIncomingMessage(winId, sbuf->str, 0, msg_flag);
514
515 g_string_free(sbuf, TRUE);
516 g_free(note->text);
517 g_free(note->jid);
518 g_free(note);
519 }
520
521 static void display_all_annotations()
522 {
523 GSList *notes;
524 notes = jb_get_all_storage_rosternotes();
525 // Call display_and_free_note() for each note,
526 // with winId = NULL (special window)
527 g_slist_foreach(notes, (GFunc)&display_and_free_note, NULL);
528 g_slist_free(notes);
529 }
530
470 static void roster_note(char *arg) 531 static void roster_note(char *arg)
471 { 532 {
472 const char *jid; 533 const char *jid;
473 gchar *msg, *notetxt;
474 guint type; 534 guint type;
475 535
476 if (!current_buddy) 536 if (!current_buddy)
477 return; 537 return;
478 538
479 jid = buddy_getjid(BUDDATA(current_buddy)); 539 jid = buddy_getjid(BUDDATA(current_buddy));
480 type = buddy_gettype(BUDDATA(current_buddy)); 540 type = buddy_gettype(BUDDATA(current_buddy));
541
542 if (!jid && type == ROSTER_TYPE_SPECIAL && !arg) {
543 // We're in the status window (the only special buffer currently)
544 // Let's display all server notes
545 display_all_annotations();
546 return;
547 }
481 548
482 if (!jid || (type != ROSTER_TYPE_USER && 549 if (!jid || (type != ROSTER_TYPE_USER &&
483 type != ROSTER_TYPE_ROOM && 550 type != ROSTER_TYPE_ROOM &&
484 type != ROSTER_TYPE_AGENT)) { 551 type != ROSTER_TYPE_AGENT)) {
485 scr_LogPrint(LPRINT_NORMAL, "This item can't have a note."); 552 scr_LogPrint(LPRINT_NORMAL, "This item can't have a note.");
486 return; 553 return;
487 } 554 }
488 555
489 if (arg && *arg) 556 if (arg && *arg) { // Set a note
557 gchar *msg, *notetxt;
490 msg = to_utf8(arg); 558 msg = to_utf8(arg);
491 else
492 msg = NULL;
493
494 if (msg) { // Set a note
495 if (!strcmp(msg, "-")) 559 if (!strcmp(msg, "-"))
496 notetxt = NULL; // delete note 560 notetxt = NULL; // delete note
497 else 561 else
498 notetxt = msg; 562 notetxt = msg;
499 jb_set_storage_rosternotes(jid, notetxt); 563 jb_set_storage_rosternotes(jid, notetxt);
500 g_free(msg); 564 g_free(msg);
501 } else { // Display a note 565 } else { // Display a note
502 struct annotation *note = jb_get_storage_rosternotes(jid); 566 struct annotation *note = jb_get_storage_rosternotes(jid);
503 if (note) { 567 if (note) {
504 char tbuf[128]; 568 display_and_free_note(note, jid);
505 guint msg_flag = HBB_PREFIX_INFO;
506 /* We use the flag prefix_info for the first line, and prefix_none
507 for the other lines, for better readability */
508
509 // If we have the creation date, display it
510 if (note->cdate) {
511 strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S",
512 localtime(&note->cdate));
513 msg = g_strdup_printf("Note created %s", tbuf);
514 scr_WriteIncomingMessage(jid, msg, 0, msg_flag);
515 g_free(msg);
516 msg_flag = HBB_PREFIX_NONE;
517 }
518 // If we have the modification date, display it
519 // unless it's the same as the creation date
520 if (note->mdate && note->mdate != note->cdate) {
521 strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S",
522 localtime(&note->mdate));
523 msg = g_strdup_printf("Note modified %s", tbuf);
524 scr_WriteIncomingMessage(jid, msg, 0, msg_flag);
525 g_free(msg);
526 msg_flag = HBB_PREFIX_NONE;
527 }
528 // Note text
529 msg = g_strdup_printf("Note: %s", note->text);
530 scr_WriteIncomingMessage(jid, msg, 0, msg_flag);
531 g_free(note->text);
532 g_free(note);
533 g_free(msg);
534 } else { 569 } else {
535 scr_WriteIncomingMessage(jid, "This item doesn't have a note.", 0, 570 scr_WriteIncomingMessage(jid, "This item doesn't have a note.", 0,
536 HBB_PREFIX_INFO); 571 HBB_PREFIX_INFO);
537 } 572 }
538 } 573 }
1298 type == ROSTER_TYPE_AGENT) { 1333 type == ROSTER_TYPE_AGENT) {
1299 struct annotation *note = jb_get_storage_rosternotes(jid); 1334 struct annotation *note = jb_get_storage_rosternotes(jid);
1300 if (note) { 1335 if (note) {
1301 // We do not display the note, we just tell the user. 1336 // We do not display the note, we just tell the user.
1302 g_free(note->text); 1337 g_free(note->text);
1338 g_free(note->jid);
1303 g_free(note); 1339 g_free(note);
1304 scr_WriteIncomingMessage(jid, "(This item has an annotation)", 0, 1340 scr_WriteIncomingMessage(jid, "(This item has an annotation)", 0,
1305 HBB_PREFIX_INFO); 1341 HBB_PREFIX_INFO);
1306 } 1342 }
1307 } 1343 }