aboutsummaryrefslogblamecommitdiffstats
path: root/calendar/gui/e-memo-table.c
blob: 75b3eccd6e1db7376f34e755757b94477d659529 (plain) (tree)
1
2
3
4
5
6
7
8
9







                                                                           
                                                        











                                                                    
                                                                           













                                                                          
                       
                        
                           















                                          

                                  
                                  

                        





                                                      






                       








                       



                                        

                                                       



                                                          
                                                                                    

                                                                      
 

                                  
                              





                                                         
                                                                  
 



















                                                                          
 
           
                                                     
 
                                                             

 
           

                                            

























                                                                        
           






























                                                                          

                                                  
 








































                                                                       


           










                                                   
















                                                                     





































                                                                  





                                                              
 

                          
                                 


                             
                          
 

                                                                 


                                                                   


                                                                       
 

                                       
                                       




                                                        
                                                                              

                                                            



                                                                  
                                                                              



                                                                 
 

                                                                 
 


                                                                 
 
                     

                                                                    
 







                                                                                                    

                                                                    


                              








                                                                             

                            









                                                                       
 
                                                              
                 
                                                       

 
























                                                                     


                    


                              
  
                             

           
                                         
 
                                                                  
 


                                                

 






                                                                  



                                 
  
                                                                  
  











                                                                  


                                 
  
                                                               
  





                                                                               
                                       
 
                                                                  
 
                                                               
 
                                                           













                                                                               
  








                                                                              

                                                     






                                                          
                                               






















                                                                                         




                                                         
                                    
 
                                                      
 

                                                                    



                                                                               

                                                       




                                                                                          
                                                          





                             

               
                                                                
  





                                                            










                                                                    




                                
  












                                                                     
                                                     















                                                                                                        
 



















































                                                                                            
                          

 



















                                                                                              









                                                    
                                
                       
 




                                                           
                                                     

                                                                       



                                                                                       

                                                               


                                                                                              
 

                                                  
                          



                                    
                                                                       





                                
                                    

















                                                                    
 

                                                                           





























                                                                                                                       
                










                                                                                                   
                                                          

 




                                                              
                                                     
                                                              























                                                                                                 








                                                                        
                                
 
                                                        
 

                                                         
 


                                                                           




                                                                            
                                               
 
                      

                                                        
                                            
 

                                                    




                                                                          

                                                
 

                      
                                                        
                                            
 

                                                    

 



























                                                                      






                             
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 * Authors :
 *  Damon Chaplin <damon@ximian.com>
 *  Rodrigo Moya <rodrigo@ximian.com>
 *  Nathan Owens <pianocomp81@yahoo.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 * USA
 */

/*
 * EMemoTable - displays the ECalComponent objects in a table (an ETable).
 * Used for memos.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/stat.h>
#include <unistd.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <gdk/gdkkeysyms.h>
#include <widgets/misc/e-gui-utils.h>
#include <table/e-cell-checkbox.h>
#include <table/e-cell-toggle.h>
#include <table/e-cell-text.h>
#include <table/e-cell-combo.h>
#include <e-util/e-dialog-utils.h>
#include <widgets/misc/e-cell-date-edit.h>
#include <widgets/misc/e-cell-percent.h>

#include "calendar-config.h"
#include "dialogs/delete-comp.h"
#include "dialogs/delete-error.h"
#include "dialogs/memo-editor.h"
#include "e-cal-model-memos.h"
#include "e-memo-table.h"
#include "e-cell-date-edit-text.h"
#include "print.h"
#include <e-util/e-icon-factory.h>
#include <e-util/e-util-private.h>
#include "e-cal-popup.h"

#define E_MEMO_TABLE_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_MEMO_TABLE, EMemoTablePrivate))

struct _EMemoTablePrivate {
    gpointer shell_view;  /* weak pointer */
};

enum {
    PROP_0,
    PROP_SHELL_VIEW
};

enum {
    OPEN_COMPONENT,
    POPUP_EVENT,
    STATUS_MESSAGE,
    USER_CREATED,
    LAST_SIGNAL
};

enum {
    TARGET_TYPE_VCALENDAR
};

static GtkTargetEntry target_types[] = {
    { "text/calendar", 0, TARGET_TYPE_VCALENDAR },
    { "text/x-calendar", 0, TARGET_TYPE_VCALENDAR }
};

static guint n_target_types = G_N_ELEMENTS (target_types);

static struct tm e_memo_table_get_current_time (ECellDateEdit *ecde, gpointer data);

static ECalModelComponent *get_selected_comp (EMemoTable *memo_table);

static gpointer parent_class;
static guint signals[LAST_SIGNAL];
static GdkAtom clipboard_atom;

/* The icons to represent the task. */
#define E_MEMO_MODEL_NUM_ICONS  2
static const char* icon_names[E_MEMO_MODEL_NUM_ICONS] = {
    "stock_notes", "stock_insert-note"
};
static GdkPixbuf* icon_pixbufs[E_MEMO_MODEL_NUM_ICONS] = { NULL };

static void
memo_table_emit_open_component (EMemoTable *memo_table,
                                ECalModelComponent *comp_data)
{
    g_signal_emit (memo_table, signals[OPEN_COMPONENT], 0, comp_data);
}

static void
memo_table_emit_popup_event (EMemoTable *memo_table,
                             GdkEvent *event)
{
    g_signal_emit (memo_table, signals[POPUP_EVENT], 0, event);
}

static void
memo_table_emit_status_message (EMemoTable *memo_table,
                                const gchar *message)
{
    g_signal_emit (memo_table, signals[STATUS_MESSAGE], 0, message);
}

static void
memo_table_emit_user_created (EMemoTable *memo_table)
{
    g_signal_emit (memo_table, signals[USER_CREATED], 0);
}

static gint
memo_table_date_compare_cb (gconstpointer a,
                            gconstpointer b)
{
    ECellDateEditValue *dv1 = (ECellDateEditValue *) a;
    ECellDateEditValue *dv2 = (ECellDateEditValue *) b;
    struct icaltimetype tt;

    /* First check if either is NULL. NULL dates sort last. */
    if (!dv1 || !dv2) {
        if (dv1 == dv2)
            return 0;
        else if (dv1)
            return -1;
        else
            return 1;
    }

    /* Copy the 2nd value and convert it to the same timezone as the
       first. */
    tt = dv2->tt;

    icaltimezone_convert_time (&tt, dv2->zone, dv1->zone);

    /* Now we can compare them. */

    return icaltime_compare (dv1->tt, tt);
}

static void
memo_table_double_click_cb (EMemoTable *memo_table,
                            gint row,
                            gint col,
                            GdkEvent *event)
{
    ECalModelComponent *comp_data;

    comp_data = e_cal_model_get_component_at (memo_table->model, row);
    memo_table_emit_open_component (memo_table, comp_data);
}

static gboolean
memo_table_popup_menu_cb (EMemoTable *memo_table)
{
    memo_table_emit_popup_event (memo_table, NULL);

    return TRUE;
}

static gint
memo_table_right_click_cb (EMemoTable *memo_table,
                           gint row,
                           gint col,
                           GdkEvent *event)
{
    memo_table_emit_popup_event (memo_table, event);

    return TRUE;
}

static void
memo_table_set_shell_view (EMemoTable *memo_table,
                           EShellView *shell_view)
{
    g_return_if_fail (memo_table->priv->shell_view == NULL);

    memo_table->priv->shell_view = shell_view;

    g_object_add_weak_pointer (
        G_OBJECT (shell_view),
        &memo_table->priv->shell_view);
}

static void
memo_table_set_property (GObject *object,
                         guint property_id,
                         const GValue *value,
                         GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_SHELL_VIEW:
            memo_table_set_shell_view (
                E_MEMO_TABLE (object),
                g_value_get_object (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
memo_table_get_property (GObject *object,
                         guint property_id,
                         GValue *value,
                         GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_SHELL_VIEW:
            g_value_set_object (
                value, e_memo_table_get_shell_view (
                E_MEMO_TABLE (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
memo_table_dispose (GObject *object)
{
    EMemoTable *memo_table;

    memo_table = E_MEMO_TABLE (object);

    if (memo_table->model != NULL) {
        g_object_unref (memo_table->model);
        memo_table->model = NULL;
    }

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
memo_table_class_init (EMemoTableClass *class)
{
    GObjectClass *object_class;

    parent_class = g_type_class_peek_parent (class);
    g_type_class_add_private (class, sizeof (EMemoTablePrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = memo_table_set_property;
    object_class->get_property = memo_table_get_property;
    object_class->dispose = memo_table_dispose;

    signals[OPEN_COMPONENT] = g_signal_new (
        "open-component",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
        G_STRUCT_OFFSET (EMemoTableClass, open_component),
        NULL, NULL,
        g_cclosure_marshal_VOID__OBJECT,
        G_TYPE_NONE, 1,
        E_TYPE_CAL_MODEL_COMPONENT);

    signals[POPUP_EVENT] = g_signal_new (
        "popup-event",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
        G_STRUCT_OFFSET (EMemoTableClass, popup_event),
        NULL, NULL,
        g_cclosure_marshal_VOID__BOXED,
        G_TYPE_NONE, 1,
        GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);

    signals[STATUS_MESSAGE] = g_signal_new (
        "status-message",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
        G_STRUCT_OFFSET (EMemoTableClass, status_message),
        NULL, NULL,
        g_cclosure_marshal_VOID__STRING,
        G_TYPE_NONE, 1,
        G_TYPE_STRING);

    signals[USER_CREATED] = g_signal_new (
        "user-created",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
        G_STRUCT_OFFSET (EMemoTableClass, user_created),
        NULL, NULL,
        g_cclosure_marshal_VOID__VOID,
        G_TYPE_NONE, 0);

    clipboard_atom = gdk_atom_intern ("CLIPBOARD", FALSE);
}

static void
memo_table_init (EMemoTable *memo_table)
{
    GtkWidget *widget;
    ETable *table;
    ECell *cell, *popup_cell;
    ETableExtras *extras;
    gint i;
    AtkObject *a11y;
    gchar *etspecfile;

    memo_table->priv = E_MEMO_TABLE_GET_PRIVATE (memo_table);

    /* Create the model */

    memo_table->model = (ECalModel *) e_cal_model_memos_new ();
    g_signal_connect_swapped (
        memo_table->model, "row_appended",
        G_CALLBACK (memo_table_emit_user_created), memo_table);

    /* Create the header columns */

    extras = e_table_extras_new ();

    /*
     * Normal string fields.
     */
    cell = e_cell_text_new (NULL, GTK_JUSTIFY_LEFT);
    g_object_set (cell, "bg_color_column", E_CAL_MODEL_FIELD_COLOR, NULL);
    e_table_extras_add_cell (extras, "calstring", cell);

    /*
     * Date fields.
     */
    cell = e_cell_date_edit_text_new (NULL, GTK_JUSTIFY_LEFT);
    g_object_set (cell, "bg_color_column", E_CAL_MODEL_FIELD_COLOR, NULL);

    popup_cell = e_cell_date_edit_new ();
    e_cell_popup_set_child (E_CELL_POPUP (popup_cell), cell);
    g_object_unref (cell);

    e_table_extras_add_cell (extras, "dateedit", popup_cell);
    memo_table->dates_cell = E_CELL_DATE_EDIT (popup_cell);

    e_cell_date_edit_set_get_time_callback (
        E_CELL_DATE_EDIT (popup_cell),
        e_memo_table_get_current_time, memo_table, NULL);

    /* Sorting */
    e_table_extras_add_compare (
        extras, "date-compare", memo_table_date_compare_cb);

    /* Create pixmaps */

    if (!icon_pixbufs[0])
        for (i = 0; i < E_MEMO_MODEL_NUM_ICONS; i++) {
            icon_pixbufs[i] = e_icon_factory_get_icon (icon_names[i], E_ICON_SIZE_LIST);
        }

    cell = e_cell_toggle_new (0, E_MEMO_MODEL_NUM_ICONS, icon_pixbufs);
    e_table_extras_add_cell (extras, "icon", cell);
    e_table_extras_add_pixbuf (extras, "icon", icon_pixbufs[0]);

    /* Create the table */

    etspecfile = g_build_filename (
        EVOLUTION_ETSPECDIR, "e-memo-table.etspec", NULL);
    widget = e_table_scrolled_new_from_spec_file (
        E_TABLE_MODEL (memo_table->model), extras, etspecfile, NULL);
    gtk_table_attach (
        GTK_TABLE (memo_table), widget, 0, 1, 0, 1,
        GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
    memo_table->etable = widget;
    gtk_widget_show (widget);
    g_free (etspecfile);

    table = e_table_scrolled_get_table (E_TABLE_SCROLLED (widget));
    g_signal_connect_swapped (
        table, "double-click",
        G_CALLBACK (memo_table_double_click_cb), memo_table);
    g_signal_connect_swapped (
        table, "popup_menu",
        G_CALLBACK (memo_table_popup_menu_cb), memo_table);
    g_signal_connect_swapped (
        table, "right-click",
        G_CALLBACK (memo_table_right_click_cb), memo_table);

    a11y = gtk_widget_get_accessible (GTK_WIDGET (table));
    if (a11y)
        atk_object_set_name (a11y, _("Memos"));
}

GType
e_memo_table_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0)) {
        static const GTypeInfo type_info = {
            sizeof (EMemoTableClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) memo_table_class_init,
            (GClassFinalizeFunc) NULL,
            NULL,  /* class_data */
            sizeof (EMemoTable),
            0,     /* n_preallocs */
            (GInstanceInitFunc) memo_table_init,
            NULL   /* value_table */
        };

        type = g_type_register_static (
            GTK_TYPE_TABLE, "EMemoTable", &type_info, 0);
    }

    return type;
}

/**
 * e_memo_table_new:
 * @shell_view: an #EShellView
 *
 * Returns a new #EMemoTable.
 *
 * Returns: a new #EMemoTable
 **/
GtkWidget *
e_memo_table_new (EShellView *shell_view)
{
    g_return_val_if_fail (E_IS_SHELL_VIEW (shell_view), NULL);

    return g_object_new (
        E_TYPE_MEMO_TABLE,
        "shell-view", shell_view, NULL);
}

EShellView *
e_memo_table_get_shell_view (EMemoTable *memo_table)
{
    g_return_val_if_fail (E_IS_MEMO_TABLE (memo_table), NULL);

    return memo_table->priv->shell_view;
}

/**
 * e_memo_table_get_model:
 * @memo_table: A calendar table.
 *
 * Queries the calendar data model that a calendar table is using.
 *
 * Return value: A memo model.
 **/
ECalModel *
e_memo_table_get_model (EMemoTable *memo_table)
{
    g_return_val_if_fail (memo_table != NULL, NULL);
    g_return_val_if_fail (E_IS_MEMO_TABLE (memo_table), NULL);

    return memo_table->model;
}


/**
 * e_memo_table_get_table:
 * @memo_table: A calendar table.
 *
 * Queries the #ETable widget that the calendar table is using.
 *
 * Return value: The #ETable widget that the calendar table uses to display its
 * data.
 **/
ETable *
e_memo_table_get_table (EMemoTable *memo_table)
{
    ETableScrolled *table_scrolled;

    g_return_val_if_fail (E_IS_MEMO_TABLE (memo_table), NULL);

    table_scrolled = E_TABLE_SCROLLED (memo_table->etable);

    return e_table_scrolled_get_table (table_scrolled);
}

/* Used from e_table_selected_row_foreach(); puts the selected row number in an
 * int pointed to by the closure data.
 */
static void
get_selected_row_cb (int model_row, gpointer data)
{
    int *row;

    row = data;
    *row = model_row;
}

/*
 * Returns the component that is selected in the table; only works if there is
 * one and only one selected row.
 */
static ECalModelComponent *
get_selected_comp (EMemoTable *memo_table)
{
    ETable *etable;
    int row;

    etable = e_memo_table_get_table (memo_table);

    if (e_table_selected_count (etable) != 1)
        return NULL;

    row = -1;
    e_table_selected_row_foreach (etable,
                      get_selected_row_cb,
                      &row);
    g_return_val_if_fail (row != -1, NULL);

    return e_cal_model_get_component_at (memo_table->model, row);
}

struct get_selected_uids_closure {
    EMemoTable *memo_table;
    GSList *objects;
};

/* Used from e_table_selected_row_foreach(), builds a list of the selected UIDs */
static void
add_uid_cb (int model_row, gpointer data)
{
    struct get_selected_uids_closure *closure;
    ECalModelComponent *comp_data;

    closure = data;

    comp_data = e_cal_model_get_component_at (closure->memo_table->model, model_row);

    closure->objects = g_slist_prepend (closure->objects, comp_data);
}

/* Deletes all of the selected components in the table */
static void
delete_selected_components (EMemoTable *memo_table)
{
    GSList *objs, *l;
    const gchar *status_message;

    objs = e_memo_table_get_selected (memo_table);

    status_message = _("Deleting selected objects");
    memo_table_emit_status_message (memo_table, status_message);

    for (l = objs; l; l = l->next) {
        ECalModelComponent *comp_data = (ECalModelComponent *) l->data;
        GError *error = NULL;

        e_cal_remove_object (comp_data->client,
                     icalcomponent_get_uid (comp_data->icalcomp), &error);
        delete_error_dialog (error, E_CAL_COMPONENT_JOURNAL);
        g_clear_error (&error);
    }

    memo_table_emit_status_message (memo_table, NULL);

    g_slist_free (objs);
}

/**
 * e_memo_table_get_selected:
 * @memo_table:
 *
 * Get the currently selected ECalModelComponent's on the table.
 *
 * Return value: A GSList of the components, which should be
 * g_slist_free'd when finished with.
 **/
GSList *
e_memo_table_get_selected (EMemoTable *memo_table)
{
    struct get_selected_uids_closure closure;
    ETable *etable;

    closure.memo_table = memo_table;
    closure.objects = NULL;

    etable = e_memo_table_get_table (memo_table);

    e_table_selected_row_foreach (etable, add_uid_cb, &closure);

    return closure.objects;
}

/**
 * e_memo_table_delete_selected:
 * @memo_table: A memo table.
 *
 * Deletes the selected components in the table; asks the user first.
 **/
void
e_memo_table_delete_selected (EMemoTable *memo_table)
{
    ETable *etable;
    int n_selected;
    ECalModelComponent *comp_data;
    ECalComponent *comp = NULL;

    g_return_if_fail (memo_table != NULL);
    g_return_if_fail (E_IS_MEMO_TABLE (memo_table));

    etable = e_memo_table_get_table (memo_table);

    n_selected = e_table_selected_count (etable);
    if (n_selected <= 0)
        return;

    if (n_selected == 1)
        comp_data = get_selected_comp (memo_table);
    else
        comp_data = NULL;

    /* FIXME: this may be something other than a TODO component */

    if (comp_data) {
        comp = e_cal_component_new ();
        e_cal_component_set_icalcomponent (comp, icalcomponent_new_clone (comp_data->icalcomp));
    }

    if (delete_component_dialog (comp, FALSE, n_selected, E_CAL_COMPONENT_JOURNAL,
                     GTK_WIDGET (memo_table)))
        delete_selected_components (memo_table);

    /* free memory */
    if (comp)
        g_object_unref (comp);
}

/**
 * e_memo_table_cut_clipboard:
 * @memo_table: A calendar table.
 *
 * Cuts selected tasks in the given calendar table
 */
void
e_memo_table_cut_clipboard (EMemoTable *memo_table)
{
    g_return_if_fail (E_IS_MEMO_TABLE (memo_table));

    e_memo_table_copy_clipboard (memo_table);
    delete_selected_components (memo_table);
}

/* callback for e_table_selected_row_foreach */
static void
copy_row_cb (int model_row, gpointer data)
{
    EMemoTable *memo_table;
    ECalModelComponent *comp_data;
    gchar *comp_str;
    icalcomponent *child;

    memo_table = E_MEMO_TABLE (data);

    g_return_if_fail (memo_table->tmp_vcal != NULL);

    comp_data = e_cal_model_get_component_at (memo_table->model, model_row);
    if (!comp_data)
        return;

    /* add timezones to the VCALENDAR component */
    e_cal_util_add_timezones_from_component (memo_table->tmp_vcal, comp_data->icalcomp);

    /* add the new component to the VCALENDAR component */
    comp_str = icalcomponent_as_ical_string (comp_data->icalcomp);
    child = icalparser_parse_string (comp_str);
    if (child) {
        icalcomponent_add_component (memo_table->tmp_vcal,
                         icalcomponent_new_clone (child));
        icalcomponent_free (child);
    }
    g_free (comp_str);
}

static void
clipboard_get_calendar_cb (GtkClipboard *clipboard,
               GtkSelectionData *selection_data,
               guint info,
               gpointer data)
{
    gchar *comp_str = (gchar *) data;

    switch (info) {
    case TARGET_TYPE_VCALENDAR:
        gtk_selection_data_set (selection_data,
                    gdk_atom_intern (target_types[info].target, FALSE), 8,
                    (const guchar *) comp_str,
                    (gint) strlen (comp_str));
        break;
    default:
        break;
    }
}

/**
 * e_memo_table_copy_clipboard:
 * @memo_table: A calendar table.
 *
 * Copies selected tasks into the clipboard
 */
void
e_memo_table_copy_clipboard (EMemoTable *memo_table)
{
    ETable *etable;
    GtkClipboard *clipboard;
    char *comp_str;

    g_return_if_fail (E_IS_MEMO_TABLE (memo_table));

    /* create temporary VCALENDAR object */
    memo_table->tmp_vcal = e_cal_util_new_top_level ();

    etable = e_memo_table_get_table (memo_table);
    e_table_selected_row_foreach (etable, copy_row_cb, memo_table);
    comp_str = icalcomponent_as_ical_string (memo_table->tmp_vcal);
    clipboard = gtk_widget_get_clipboard (GTK_WIDGET (memo_table), clipboard_atom);
    if (!gtk_clipboard_set_with_data(clipboard, target_types, n_target_types,
                     clipboard_get_calendar_cb,
                     NULL, comp_str)) {
        /* do not free this pointer, it owns libical */
        /* g_free (comp_str); */
    } else {
        gtk_clipboard_set_can_store (clipboard, target_types + 1, n_target_types - 1);
    }

    /* free memory */
    icalcomponent_free (memo_table->tmp_vcal);
    g_free (comp_str);
    memo_table->tmp_vcal = NULL;
}

static void
clipboard_get_calendar_data (EMemoTable *memo_table, const gchar *text)
{
    icalcomponent *icalcomp;
    char *uid;
    ECalComponent *comp;
    ECal *client;
    icalcomponent_kind kind;
    const gchar *status_message;

    g_return_if_fail (E_IS_MEMO_TABLE (memo_table));

    if (!text || !*text)
        return;

    icalcomp = icalparser_parse_string (text);
    if (!icalcomp)
        return;

    /* check the type of the component */
    kind = icalcomponent_isa (icalcomp);
    if (kind != ICAL_VCALENDAR_COMPONENT &&
        kind != ICAL_VJOURNAL_COMPONENT) {
        return;
    }

    client = e_cal_model_get_default_client (memo_table->model);

    status_message = _("Updating objects");
    memo_table_emit_status_message (memo_table, _("Updating objects"));

    if (kind == ICAL_VCALENDAR_COMPONENT) {
        icalcomponent_kind child_kind;
        icalcomponent *subcomp;
        icalcomponent *vcal_comp;

        vcal_comp = icalcomp;
        subcomp = icalcomponent_get_first_component (
            vcal_comp, ICAL_ANY_COMPONENT);
        while (subcomp) {
            child_kind = icalcomponent_isa (subcomp);
            if (child_kind == ICAL_VJOURNAL_COMPONENT) {
                ECalComponent *tmp_comp;

                uid = e_cal_component_gen_uid ();
                tmp_comp = e_cal_component_new ();
                e_cal_component_set_icalcomponent (
                    tmp_comp, icalcomponent_new_clone (subcomp));
                e_cal_component_set_uid (tmp_comp, uid);
                free (uid);

                /* FIXME should we convert start/due/complete times? */
                /* FIXME Error handling */
                e_cal_create_object (client, e_cal_component_get_icalcomponent (tmp_comp), NULL, NULL);

                g_object_unref (tmp_comp);
            }
            subcomp = icalcomponent_get_next_component (
                vcal_comp, ICAL_ANY_COMPONENT);
        }
    } else {
        comp = e_cal_component_new ();
        e_cal_component_set_icalcomponent (comp, icalcomp);
        uid = e_cal_component_gen_uid ();
        e_cal_component_set_uid (comp, (const char *) uid);
        free (uid);

        e_cal_create_object (client, e_cal_component_get_icalcomponent (comp), NULL, NULL);

        g_object_unref (comp);
    }

    memo_table_emit_status_message (memo_table, NULL);
}

static void
clipboard_paste_received_cb (GtkClipboard *clipboard,
                 GtkSelectionData *selection_data,
                 gpointer data)
{
    EMemoTable *memo_table = E_MEMO_TABLE (data);
    ETable *e_table = e_memo_table_get_table (memo_table);
    GnomeCanvas *canvas = e_table->table_canvas;
    GnomeCanvasItem *item = GNOME_CANVAS (canvas)->focused_item;

    if (gtk_clipboard_wait_is_text_available (clipboard) &&
        GTK_WIDGET_HAS_FOCUS (canvas) &&
        E_IS_TABLE_ITEM (item) &&
        E_TABLE_ITEM (item)->editing_col >= 0 &&
        E_TABLE_ITEM (item)->editing_row >= 0) {
        ETableItem *eti = E_TABLE_ITEM (item);
        ECellView *cell_view = eti->cell_views[eti->editing_col];
        e_cell_text_paste_clipboard (cell_view, eti->editing_col, eti->editing_row);
    } else {
        GdkAtom type = selection_data->type;
        if (type == gdk_atom_intern (target_types[TARGET_TYPE_VCALENDAR].target, TRUE)) {
            gchar *result = NULL;
            result = g_strndup ((const gchar *) selection_data->data,
                        selection_data->length);
            clipboard_get_calendar_data (memo_table, result);
            g_free (result);
        }
    }
    g_object_unref (memo_table);
}

/**
 * e_memo_table_paste_clipboard:
 * @memo_table: A calendar table.
 *
 * Pastes tasks currently in the clipboard into the given calendar table
 */
void
e_memo_table_paste_clipboard (EMemoTable *memo_table)
{
    GtkClipboard *clipboard;

    g_return_if_fail (E_IS_MEMO_TABLE (memo_table));

    clipboard = gtk_widget_get_clipboard (
        GTK_WIDGET (memo_table), clipboard_atom);

    gtk_clipboard_request_contents (
        clipboard, gdk_atom_intern (target_types[0].target, FALSE),
        clipboard_paste_received_cb, g_object_ref (memo_table));
}

/* Loads the state of the table (headers shown etc.) from the given file. */
void
e_memo_table_load_state (EMemoTable *memo_table,
                         const gchar *filename)
{
    ETable *table;

    g_return_if_fail (E_IS_MEMO_TABLE (memo_table));
    g_return_if_fail (filename != NULL);

    table = e_memo_table_get_table (memo_table);
    e_table_load_state (table, filename);
}


/* Saves the state of the table (headers shown etc.) to the given file. */
void
e_memo_table_save_state (EMemoTable *memo_table,
                         const gchar *filename)
{
    ETable *table;

    g_return_if_fail (E_IS_MEMO_TABLE (memo_table));
    g_return_if_fail (filename != NULL);

    table = e_memo_table_get_table (memo_table);
    e_table_save_state (table, filename);
}

/* Returns the current time, for the ECellDateEdit items.
   FIXME: Should probably use the timezone of the item rather than the
   current timezone, though that may be difficult to get from here. */
static struct tm
e_memo_table_get_current_time (ECellDateEdit *ecde, gpointer data)
{
    icaltimezone *zone;
    struct tm tmp_tm = { 0 };
    struct icaltimetype tt;

    /* Get the current timezone. */
    zone = calendar_config_get_icaltimezone ();

    tt = icaltime_from_timet_with_zone (time (NULL), FALSE, zone);

    /* Now copy it to the struct tm and return it. */
    tmp_tm.tm_year  = tt.year - 1900;
    tmp_tm.tm_mon   = tt.month - 1;
    tmp_tm.tm_mday  = tt.day;
    tmp_tm.tm_hour  = tt.hour;
    tmp_tm.tm_min   = tt.minute;
    tmp_tm.tm_sec   = tt.second;
    tmp_tm.tm_isdst = -1;

    return tmp_tm;
}


#ifdef TRANSLATORS_ONLY

static char *test[] = {
    N_("Click to add a memo")
};

#endif