/* This module provides some helper APIs for creating a VCalendar object. Note on APIs: 1. The APIs does not attempt to verify if the arguments passed are correct. 2. Where the argument to an API is not applicable, pass the value 0. 3. See the test program at the bottom of this file as an example of usage. 4. This code calls APIs in vobject.c. */ /*************************************************************************** (C) Copyright 1996 Apple Computer, Inc., AT&T Corp., International Business Machines Corporation and Siemens Rolm Communications Inc. For purposes of this license notice, the term Licensors shall mean, collectively, Apple Computer, Inc., AT&T Corp., International Business Machines Corporation and Siemens Rolm Communications Inc. The term Licensor shall mean any of the Licensors. Subject to acceptance of the following conditions, permission is hereby granted by Licensors without the need for written agreement and without license or royalty fees, to use, copy, modify and distribute this software for any purpose. The above copyright notice and the following four paragraphs must be reproduced in all copies of this software and any software including this software. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS AND NO LICENSOR SHALL HAVE ANY OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS. IN NO EVENT SHALL ANY LICENSOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT OF THE USE OF THIS SOFTWARE EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. EACH LICENSOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY OF NONINFRINGEMENT OR THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. The software is provided with RESTRICTED RIGHTS. Use, duplication, or disclosure by the government are subject to restrictions set forth in DFARS 252.227-7013 or 48 CFR 52.227-19, as applicable. ***************************************************************************/ #include #include #include "vcaltmp.h" DLLEXPORT(VObject*) vcsCreateVCal( char *date_created, char *location, char *product_id, char *time_zone, char *version ) { VObject *vcal = newVObject(VCCalProp); #define Z(p,v) if (v) addPropValue(vcal,p,v); Z(VCDCreatedProp, date_created); Z(VCLocationProp, location) Z(VCProdIdProp, product_id) Z(VCTimeZoneProp, time_zone) Z(VCVersionProp, version) #undef Z return vcal; } DLLEXPORT(VObject*) vcsAddEvent( VObject *vcal, char *start_date_time, char *end_date_time, char *description, char *summary, char *categories, char *classification, char *status, char *transparency, char *uid, char *url ) { VObject *vevent = addProp(vcal,VCEventProp); #define Z(p,v) if (v) addPropValue(vevent,p,v); Z(VCDTstartProp,start_date_time); Z(VCDTendProp,end_date_time); if (description) { VObject *p = addPropValue(vevent,VCDescriptionProp,description); if (strchr(description,'\n')) addProp(p,VCQuotedPrintableProp); } Z(VCSummaryProp,summary); Z(VCCategoriesProp,categories); Z(VCClassProp,classification); Z(VCStatusProp,status); Z(VCTranspProp,transparency); Z(VCUniqueStringProp,uid); Z(VCURLProp,url); #undef Z return vevent; } DLLEXPORT(VObject*) vcsAddTodo( VObject *vcal, char *start_date_time, char *due_date_time, char *date_time_complete, char *description, char *summary, char *priority, char *classification, char *status, char *uid, char *url ) { VObject *vtodo = addProp(vcal,VCTodoProp); #define Z(p,v) if (v) addPropValue(vtodo,p,v); Z(VCDTstartProp,start_date_time); Z(VCDueProp,due_date_time); Z(VCCompletedProp,date_time_complete); if (description) { VObject *p = addPropValue(vtodo,VCDescriptionProp,description); if (strchr(description,'\n')) addProp(p,VCQuotedPrintableProp); } Z(VCSummaryProp,summary); Z(VCPriorityProp,priority); Z(VCClassProp,classification); Z(VCStatusProp,status); Z(VCUniqueStringProp,uid); Z(VCURLProp,url); #undef Z return vtodo; } DLLEXPORT(VObject*) vcsAddAAlarm( VObject *vevent, char *run_time, char *snooze_time, char *repeat_count, char *audio_content ) { VObject *aalarm= addProp(vevent,VCAAlarmProp); #define Z(p,v) if (v) addPropValue(aalarm,p,v); Z(VCRunTimeProp,run_time); Z(VCSnoozeTimeProp,snooze_time); Z(VCRepeatCountProp,repeat_count); Z(VCAudioContentProp,audio_content); #undef Z return aalarm; } DLLEXPORT(VObject*) vcsAddMAlarm( VObject *vevent, char *run_time, char *snooze_time, char *repeat_count, char *email_address, char *note ) { VObject *malarm= addProp(vevent,VCMAlarmProp); #define Z(p,v) if (v) addPropValue(malarm,p,v); Z(VCRunTimeProp,run_time); Z(VCSnoozeTimeProp,snooze_time); Z(VCRepeatCountProp,repeat_count); Z(VCEmailAddressProp,email_address); Z(VCNoteProp,note); #undef Z return malarm; } DLLEXPORT(VObject*) vcsAddDAlarm( VObject *vevent, char *run_time, char *snooze_time, char *repeat_count, char *display_string ) { VObject *dalarm= addProp(vevent,VCDAlarmProp); #define Z(p,v) if (v) addPropValue(dalarm,p,v); Z(VCRunTimeProp,run_time); Z(VCSnoozeTimeProp,snooze_time); Z(VCRepeatCountProp,repeat_count); Z(VCDisplayStringProp,display_string); #undef Z return dalarm; } DLLEXPORT(VObject*) vcsAddPAlarm( VObject *vevent, char *run_time, char *snooze_time, char *repeat_count, char *procedure_name ) { VObject *palarm= addProp(vevent,VCPAlarmProp); #define Z(p,v) if (v) addPropValue(palarm,p,v); Z(VCRunTimeProp,run_time); Z(VCSnoozeTimeProp,snooze_time); Z(VCRepeatCountProp,repeat_count); Z(VCProcedureNameProp,procedure_name); #undef Z return palarm; } #ifdef _TEST #if 0 This testcase would generate a file call "frankcal.vcf" with the following content: BEGIN:VCALENDAR DCREATED:19960523T100522 GEO:37.24,-17.87 PRODID:-//Frank Dawson/Hand Crafted In North Carolina//NONSGML Made By Hand//EN VERSION:0.3 BEGIN:VEVENT DTSTART:19960523T120000 DTEND:19960523T130000 DESCRIPTION;QUOTED-PRINTABLE:VERSIT PDI PR Teleconference/Interview =0A= With Tom Streeter and Frank Dawson - Discuss VERSIT PDI project and vCard and vCalendar=0A= activities with European Press representatives. SUMMARY:VERSIT PDI PR Teleconference/Interview CATEGORIES:PHONE CALL STATUS:CONFIRMED TRANSP:19960523T100522-4000F100582713-009251 UID:http://www.ibm.com/raleigh/fdawson/~c:\or2\orgfiles\versit.or2 DALARM:19960523T114500;5;3;Your Telecon Starts At Noon!!!; MALARM:19960522T120000;;;fdawson@raleigh.ibm.com;Remember 05/23 Noon Telecon!!!; PALARM:19960523T115500;;;c:\or2\organize.exe c:\or2\orgfiles\versit.or2; X-LDC-OR2-OLE:c:\temp\agenda.doc END:VEVENT BEGIN:VTODO DUE:19960614T0173000 DESCRIPTION:Review VCalendar helper API. END:VTODO END:VCALENDAR #endif void testVcalAPIs() { FILE *fp; VObject *vcal = vcsCreateVCal( "19960523T100522", "37.24,-17.87", "-//Frank Dawson/Hand Crafted In North Carolina//NONSGML Made By Hand//EN", 0, "0.3" ); VObject *vevent = vcsAddEvent( vcal, "19960523T120000", "19960523T130000", "VERSIT PDI PR Teleconference/Interview \nWith Tom Streeter and Frank Dawson - Discuss VERSIT PDI project and vCard and vCalendar\nactivities with European Press representatives.", "VERSIT PDI PR Teleconference/Interview", "PHONE CALL", 0, "CONFIRMED", "19960523T100522-4000F100582713-009251", "http://www.ibm.com/raleigh/fdawson/~c:\\or2\\orgfiles\\versit.or2", 0 ); vcsAddDAlarm(vevent, "19960523T114500", "5", "3", "Your Telecon Starts At Noon!!!"); vcsAddMAlarm(vevent, "19960522T120000", 0, 0, "fdawson@raleigh.ibm.com", "Remember 05/23 Noon Telecon!!!"); vcsAddPAlarm(vevent, "19960523T115500", 0 ,0, "c:\\or2\\organize.exe c:\\or2\\orgfiles\\versit.or2"); addPropValue(vevent, "X-LDC-OR2-OLE", "c:\\temp\\agenda.doc"); vcsAddTodo( vcal, 0, "19960614T0173000", 0, "Review VCalendar helper API.", 0, 0, 0, 0, 0, 0 ); /* now do something to the resulting VObject */ /* pretty print on stdout for fun */ printVObject(vcal); /* open the output text file */ #define OUTFILE "frankcal.vcf" fp = fopen(OUTFILE, "w"); if (fp) { /* write it in text form */ writeVObject(fp,vcal); fclose(fp); } else { printf("open output file '%s' failed\n", OUTFILE); } } void main() { testVcalAPIs(); } #endif /* end of source file vcaltmp.c */ ef='#n167'>167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-msg-composer-attachment-bar.c
 *
 * Copyright (C) 1999  Helix Code, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ettore Perazzoli
 */

#include <gnome.h>
#include <glade/glade.h>

#include "e-msg-composer-attachment.h"
#include "e-msg-composer-attachment-bar.h"
#include "camel/camel-data-wrapper.h"
#include "camel/camel-stream-fs.h"
#include "camel/camel-mime-part.h"


#define ICON_WIDTH 64
#define ICON_SEPARATORS " /-_"
#define ICON_SPACING 2
#define ICON_ROW_SPACING ICON_SPACING
#define ICON_COL_SPACING ICON_SPACING
#define ICON_BORDER 2
#define ICON_TEXT_SPACING 2


static GnomeIconListClass *parent_class = NULL;

struct _EMsgComposerAttachmentBarPrivate {
    GList *attachments;
    guint num_attachments;

    GtkWidget *context_menu;
    GtkWidget *icon_context_menu;
};


enum {
    CHANGED,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };


static void update (EMsgComposerAttachmentBar *bar);


static gchar *
size_to_string (gulong size)
{
    gchar *size_string;

    /* FIXME: The following should probably go into a separate module, as
           we might have to do the same thing in other places as well.  Also,
       I am not sure this will be OK for all the languages.  */

    if (size < 1e3L) {
        if (size == 1)
            size_string = g_strdup (_("1 byte"));
        else
            size_string = g_strdup_printf (_("%u bytes"),
                               (guint) size);
    } else {
        gdouble displayed_size;

        if (size < 1e6L) {
            displayed_size = (gdouble) size / 1.0e3;
            size_string = g_strdup_printf (_("%.1fK"),
                               displayed_size);
        } else if (size < 1e9L) {
            displayed_size = (gdouble) size / 1.0e6;
            size_string = g_strdup_printf (_("%.1fM"),
                               displayed_size);
        } else {
            displayed_size = (gdouble) size / 1.0e9;
            size_string = g_strdup_printf (_("%.1fG"),
                               displayed_size);
        }
    }

    return size_string;
}

/* Attachment handling functions.  */

static void
free_attachment_list (EMsgComposerAttachmentBar *bar)
{
    EMsgComposerAttachmentBarPrivate *priv;
    GList *p;

    priv = bar->priv;

    for (p = priv->attachments; p != NULL; p = p->next)
        gtk_object_unref (GTK_OBJECT (p->data));
}

static void
attachment_changed_cb (EMsgComposerAttachment *attachment,
               gpointer data)
{
    update (E_MSG_COMPOSER_ATTACHMENT_BAR (data));
}

static void
add_common (EMsgComposerAttachmentBar *bar,
        EMsgComposerAttachment *attachment)
{
    gtk_signal_connect (GTK_OBJECT (attachment), "changed",
                GTK_SIGNAL_FUNC (attachment_changed_cb),
                bar);

    bar->priv->attachments = g_list_append (bar->priv->attachments,
                        attachment);
    bar->priv->num_attachments++;

    update (bar);

    gtk_signal_emit (GTK_OBJECT (bar), signals[CHANGED]);
}

static void
add_from_mime_part (EMsgComposerAttachmentBar *bar,
            CamelMimePart *part)
{
    add_common (bar, e_msg_composer_attachment_new_from_mime_part (part));
}

static void
add_from_file (EMsgComposerAttachmentBar *bar,
           const gchar *file_name)
{
    add_common (bar, e_msg_composer_attachment_new (file_name));
}

static void
remove_attachment (EMsgComposerAttachmentBar *bar,
           EMsgComposerAttachment *attachment)
{
    bar->priv->attachments = g_list_remove (bar->priv->attachments,
                        attachment);
    bar->priv->num_attachments--;

    gtk_object_unref (GTK_OBJECT (attachment));

    gtk_signal_emit (GTK_OBJECT (bar), signals[CHANGED]);
}


/* Icon list contents handling.  */

static void
update (EMsgComposerAttachmentBar *bar)
{
    EMsgComposerAttachmentBarPrivate *priv;
    GnomeIconList *icon_list;
    GList *p;

    priv = bar->priv;
    icon_list = GNOME_ICON_LIST (bar);

    gnome_icon_list_freeze (icon_list);

    gnome_icon_list_clear (icon_list);

    /* FIXME could be faster, but we don't care.  */

    for (p = priv->attachments; p != NULL; p = p->next) {
        EMsgComposerAttachment *attachment;
        const gchar *icon_name, *desc;
        gchar *size_string, *label, *mime_type;
        GMimeContentField *content_type;

        attachment = p->data;
        content_type = camel_mime_part_get_content_type (attachment->body);
        mime_type = g_strdup_printf ("%s/%s", content_type->type,
                         content_type->subtype);
        icon_name = gnome_mime_get_value (mime_type, "icon-filename");
        g_free (mime_type);

        /* FIXME we need some better default icon.  */
        if (icon_name == NULL)
            icon_name = gnome_mime_get_value ("text/plain",
                              "icon-filename");

        desc = camel_mime_part_get_description (attachment->body);
        if (!desc)
            desc = camel_mime_part_get_filename (attachment->body);
        if (!desc)
            desc = "attachment";

        if (attachment->size) {
            size_string = size_to_string (attachment->size);
            label = g_strdup_printf ("%s (%s)", desc, size_string);
            g_free (size_string);
        } else
            label = g_strdup (desc);

        gnome_icon_list_append (icon_list, icon_name, label);
        g_free (label);
    }

    gnome_icon_list_thaw (icon_list);
}

static void
remove_selected (EMsgComposerAttachmentBar *bar)
{
    GnomeIconList *icon_list;
    EMsgComposerAttachment *attachment;
    GList *attachment_list;
    GList *p;
    gint num;

    icon_list = GNOME_ICON_LIST (bar);

    /* Weee!  I am especially proud of this piece of cheesy code: it is
           truly awful.  But unless one attaches a huge number of files, it
           will not be as greedy as intended.  FIXME of course.  */

    attachment_list = NULL;
    for (p = icon_list->selection; p != NULL; p = p->next) {
        num = GPOINTER_TO_INT (p->data);
        attachment = E_MSG_COMPOSER_ATTACHMENT
            (g_list_nth (bar->priv->attachments, num)->data);
        attachment_list = g_list_prepend (attachment_list, attachment);
    }

    for (p = attachment_list; p != NULL; p = p->next)
        remove_attachment (bar, E_MSG_COMPOSER_ATTACHMENT (p->data));

    g_list_free (attachment_list);

    update (bar);
}

static void
edit_selected (EMsgComposerAttachmentBar *bar)
{
    GnomeIconList *icon_list;
    EMsgComposerAttachment *attachment;
    gint num;

    icon_list = GNOME_ICON_LIST (bar);

    num = GPOINTER_TO_INT (icon_list->selection->data);
    attachment = g_list_nth (bar->priv->attachments, num)->data;

    e_msg_composer_attachment_edit (attachment, GTK_WIDGET (bar));
}


/* "Attach" dialog.  */

static void
attach_cb (GtkWidget *widget,
       gpointer data)
{
    EMsgComposerAttachmentBar *bar;
    GtkWidget *file_selection;
    const gchar *file_name;

    file_selection = gtk_widget_get_toplevel (widget);
    bar = E_MSG_COMPOSER_ATTACHMENT_BAR (data);

    file_name = gtk_file_selection_get_filename
                                  (GTK_FILE_SELECTION (file_selection));
    add_from_file (bar, file_name);

    gtk_widget_hide (file_selection);
}

static void
add_from_user (EMsgComposerAttachmentBar *bar)
{
    static GtkWidget *fs;

    if (!fs) {
        GtkWidget *cancel_button;
        GtkWidget *ok_button;

        fs = gtk_file_selection_new (_("Add attachment"));

        ok_button = GTK_FILE_SELECTION (fs)->ok_button;
        gtk_signal_connect (GTK_OBJECT (ok_button),
                    "clicked", GTK_SIGNAL_FUNC (attach_cb),
                    bar);

        cancel_button = GTK_FILE_SELECTION (fs)->cancel_button;
        gtk_signal_connect_object (GTK_OBJECT (cancel_button),
                       "clicked",
                       GTK_SIGNAL_FUNC (gtk_widget_hide),
                       GTK_OBJECT (fs));

        gtk_signal_connect (GTK_OBJECT (fs), "delete_event",
                    GTK_SIGNAL_FUNC (gtk_widget_hide), NULL);
    } else
        gtk_file_selection_set_filename (GTK_FILE_SELECTION (fs), "");

    gtk_window_set_position (GTK_WINDOW (fs), GTK_WIN_POS_MOUSE);

    gtk_widget_show (GTK_WIDGET (fs));
}


/* Callbacks.  */

static void
add_cb (GtkWidget *widget,
    gpointer data)
{
    g_return_if_fail (E_IS_MSG_COMPOSER_ATTACHMENT_BAR (data));

    add_from_user (E_MSG_COMPOSER_ATTACHMENT_BAR (data));
}

static void
properties_cb (GtkWidget *widget,
           gpointer data)
{
    EMsgComposerAttachmentBar *bar;

    g_return_if_fail (E_IS_MSG_COMPOSER_ATTACHMENT_BAR (data));

    bar = E_MSG_COMPOSER_ATTACHMENT_BAR (data);
    edit_selected (data);
}

static void
remove_cb (GtkWidget *widget,
       gpointer data)
{
    EMsgComposerAttachmentBar *bar;

    g_return_if_fail (E_IS_MSG_COMPOSER_ATTACHMENT_BAR (data));

    bar = E_MSG_COMPOSER_ATTACHMENT_BAR (data);
    remove_selected (bar);
}


/* Popup menu handling.  */

static GnomeUIInfo icon_context_menu_info[] = {
    GNOMEUIINFO_ITEM (N_("Remove"),
              N_("Remove selected items from the attachment list"),
              remove_cb, NULL),
    GNOMEUIINFO_MENU_PROPERTIES_ITEM (properties_cb, NULL),
    GNOMEUIINFO_END
};

static GtkWidget *
get_icon_context_menu (EMsgComposerAttachmentBar *bar)
{
    EMsgComposerAttachmentBarPrivate *priv;

    priv = bar->priv;
    if (priv->icon_context_menu == NULL)
        priv->icon_context_menu = gnome_popup_menu_new
                               (icon_context_menu_info);

    return priv->icon_context_menu;
}

static void
popup_icon_context_menu (EMsgComposerAttachmentBar *bar,
             gint num,
             GdkEventButton *event)
{
    GtkWidget *menu;

    menu = get_icon_context_menu (bar);
    gnome_popup_menu_do_popup (menu, NULL, NULL, event, bar);
}

static GnomeUIInfo context_menu_info[] = {
    GNOMEUIINFO_ITEM (N_("Add attachment..."),
              N_("Attach a file to the message"),
              add_cb, NULL),
    GNOMEUIINFO_END
};

static GtkWidget *
get_context_menu (EMsgComposerAttachmentBar *bar)
{
    EMsgComposerAttachmentBarPrivate *priv;

    priv = bar->priv;
    if (priv->context_menu == NULL)
        priv->context_menu = gnome_popup_menu_new (context_menu_info);

    return priv->context_menu;
}

static void
popup_context_menu (EMsgComposerAttachmentBar *bar,
            GdkEventButton *event)
{
    GtkWidget *menu;

    menu = get_context_menu (bar);
    gnome_popup_menu_do_popup (menu, NULL, NULL, event, bar);
}


/* GtkObject methods.  */

static void
destroy (GtkObject *object)
{
    EMsgComposerAttachmentBar *bar;

    bar = E_MSG_COMPOSER_ATTACHMENT_BAR (object);

    free_attachment_list (bar);

    if (GTK_OBJECT_CLASS (parent_class)->destroy != NULL)
        (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}


/* GtkWidget methods.  */

static gint
button_press_event (GtkWidget *widget,
            GdkEventButton *event)
{
    EMsgComposerAttachmentBar *bar;
    GnomeIconList *icon_list;
    gint icon_number;

    bar = E_MSG_COMPOSER_ATTACHMENT_BAR (widget);
    icon_list = GNOME_ICON_LIST (widget);

    if (event->button != 3)
        return GTK_WIDGET_CLASS (parent_class)->button_press_event
                                    (widget, event);

    icon_number = gnome_icon_list_get_icon_at (icon_list,
                           event->x, event->y);

    if (icon_number >= 0) {
        gnome_icon_list_select_icon (icon_list, icon_number);
        popup_icon_context_menu (bar, icon_number, event);
    } else {
        popup_context_menu (bar, event);
    }

    return TRUE;
}


/* Initialization.  */

static void
class_init (EMsgComposerAttachmentBarClass *class)
{
    GtkObjectClass *object_class;
    GtkWidgetClass *widget_class;
    GnomeIconListClass *icon_list_class;

    object_class = GTK_OBJECT_CLASS (class);
    widget_class = GTK_WIDGET_CLASS (class);
    icon_list_class = GNOME_ICON_LIST_CLASS (class);

    parent_class = gtk_type_class (gnome_icon_list_get_type ());

    object_class->destroy = destroy;

    widget_class->button_press_event = button_press_event;

    /* Setup signals.  */

    signals[CHANGED] =
        gtk_signal_new ("changed",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (EMsgComposerAttachmentBarClass,
                           changed),
                gtk_marshal_NONE__NONE,
                GTK_TYPE_NONE, 0);

    gtk_object_class_add_signals (object_class, signals, LAST_SIGNAL);
}

static void
init (EMsgComposerAttachmentBar *bar)
{
    EMsgComposerAttachmentBarPrivate *priv;
    guint icon_size;

    priv = g_new (EMsgComposerAttachmentBarPrivate, 1);

    priv->attachments = NULL;
    priv->context_menu = NULL;
    priv->icon_context_menu = NULL;

    priv->num_attachments = 0;

    bar->priv = priv;

    /* FIXME partly hardcoded.  We should compute height from the font, and
           allow at least 2 lines for every item.  */
    icon_size = ICON_WIDTH + ICON_SPACING + ICON_BORDER + ICON_TEXT_SPACING;
    icon_size += 24;

    gtk_widget_set_usize (GTK_WIDGET (bar), icon_size * 4, icon_size);
}


GtkType
e_msg_composer_attachment_bar_get_type (void)
{
    static GtkType type = 0;

    if (type == 0) {
        static const GtkTypeInfo info = {
            "EMsgComposerAttachmentBar",
            sizeof (EMsgComposerAttachmentBar),
            sizeof (EMsgComposerAttachmentBarClass),
            (GtkClassInitFunc) class_init,
            (GtkObjectInitFunc) init,
            /* reserved_1 */ NULL,
            /* reserved_2 */ NULL,
            (GtkClassInitFunc) NULL,
        };

        type = gtk_type_unique (gnome_icon_list_get_type (), &info);
    }

    return type;
}

GtkWidget *
e_msg_composer_attachment_bar_new (GtkAdjustment *adj)
{
    EMsgComposerAttachmentBar *new;
    GnomeIconList *icon_list;

    gtk_widget_push_visual (gdk_imlib_get_visual ());
    gtk_widget_push_colormap (gdk_imlib_get_colormap ());
    new = gtk_type_new (e_msg_composer_attachment_bar_get_type ());
    gtk_widget_pop_visual ();
    gtk_widget_pop_colormap ();

    icon_list = GNOME_ICON_LIST (new);

    gnome_icon_list_construct (icon_list, ICON_WIDTH, adj, 0);

    gnome_icon_list_set_separators (icon_list, ICON_SEPARATORS);
    gnome_icon_list_set_row_spacing (icon_list, ICON_ROW_SPACING);
    gnome_icon_list_set_col_spacing (icon_list, ICON_COL_SPACING);
    gnome_icon_list_set_icon_border (icon_list, ICON_BORDER);
    gnome_icon_list_set_text_spacing (icon_list, ICON_TEXT_SPACING);
    gnome_icon_list_set_selection_mode (icon_list, GTK_SELECTION_MULTIPLE);

    return GTK_WIDGET (new);
}


static void
attach_to_multipart (CamelMultipart *multipart,
             EMsgComposerAttachment *attachment)
{
    GMimeContentField *content_type;

    content_type = camel_mime_part_get_content_type (attachment->body);

    /* Kludge a bit on CTE. For now, we set QP for text and B64
     * for all else except message (which must be 7bit, 8bit, or
     * binary). FIXME.
     */
    if (!strcasecmp (content_type->type, "text")) {
        camel_mime_part_set_encoding (attachment->body,
                          CAMEL_MIME_PART_ENCODING_QUOTEDPRINTABLE);
    } else if (strcasecmp (content_type->type, "message") != 0) {
        camel_mime_part_set_encoding (attachment->body,
                          CAMEL_MIME_PART_ENCODING_BASE64);
    }

    camel_multipart_add_part (multipart, attachment->body);
}

void
e_msg_composer_attachment_bar_to_multipart (EMsgComposerAttachmentBar *bar,
                        CamelMultipart *multipart)
{
    EMsgComposerAttachmentBarPrivate *priv;
    GList *p;

    g_return_if_fail (bar != NULL);
    g_return_if_fail (E_IS_MSG_COMPOSER_ATTACHMENT_BAR (bar));
    g_return_if_fail (multipart != NULL);
    g_return_if_fail (CAMEL_IS_MULTIPART (multipart));

    priv = bar->priv;

    for (p = priv->attachments; p != NULL; p = p->next) {
        EMsgComposerAttachment *attachment;

        attachment = E_MSG_COMPOSER_ATTACHMENT (p->data);
        attach_to_multipart (multipart, attachment);
    }
}


guint
e_msg_composer_attachment_bar_get_num_attachments (EMsgComposerAttachmentBar *bar)
{
    g_return_val_if_fail (bar != NULL, 0);
    g_return_val_if_fail (E_IS_MSG_COMPOSER_ATTACHMENT_BAR (bar), 0);

    return bar->priv->num_attachments;
}


void
e_msg_composer_attachment_bar_attach (EMsgComposerAttachmentBar *bar,
                      const gchar *file_name)
{
    g_return_if_fail (E_IS_MSG_COMPOSER_ATTACHMENT_BAR (bar));

    if (file_name == NULL)
        add_from_user (bar);
    else
        add_from_file (bar, file_name);
}

void
e_msg_composer_attachment_bar_attach_mime_part (EMsgComposerAttachmentBar *bar,
                        CamelMimePart *part)
{
    g_return_if_fail (E_IS_MSG_COMPOSER_ATTACHMENT_BAR (bar));

    add_from_mime_part (bar, part);
}