aboutsummaryrefslogblamecommitdiffstats
path: root/calendar/modules/e-cal-shell-view-private.c
blob: d738d230b379a52df177d5a664875df0e28b1c5f (plain) (tree)





















                                                                           




































































                                                                           
    

                                                                 

                                                          

                                 
 




                                                                
                                                                    






                                                                            





                                                                    


                                     
                                







                                                                    




                                                                  
                                                             


                                                 
                                                         






                                                                

                                    
                                         


                                          






                                                                     




                                                                 
                                
 






                                                                   
                                   




                                                                
                                                                  









                                                                
                                                                     







                                                                       
                                                               
























































































































                                                                               
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-cal-shell-view-private.c
 *
 * 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.
 */

#include "e-cal-shell-view-private.h"

#include "calendar/gui/calendar-view-factory.h"
#include "widgets/menus/gal-view-factory-etable.h"

static void
cal_shell_view_load_view_collection (EShellViewClass *shell_view_class)
{
    GalViewCollection *collection;
    GalViewFactory *factory;
    ETableSpecification *spec;
    const gchar *base_dir;
    gchar *filename;

    collection = shell_view_class->view_collection;

    base_dir = EVOLUTION_ETSPECDIR;
    spec = e_table_specification_new ();
    filename = g_build_filename (base_dir, ETSPEC_FILENAME, NULL);
    if (!e_table_specification_load_from_file (spec, filename))
        g_critical ("Unable to load ETable specification file "
                "for calendars");
    g_free (filename);

    factory = calendar_view_factory_new (GNOME_CAL_DAY_VIEW);
    gal_view_collection_add_factory (collection, factory);
    g_object_unref (factory);

    factory = calendar_view_factory_new (GNOME_CAL_WORK_WEEK_VIEW);
    gal_view_collection_add_factory (collection, factory);
    g_object_unref (factory);

    factory = calendar_view_factory_new (GNOME_CAL_WEEK_VIEW);
    gal_view_collection_add_factory (collection, factory);
    g_object_unref (factory);

    factory = calendar_view_factory_new (GNOME_CAL_MONTH_VIEW);
    gal_view_collection_add_factory (collection, factory);
    g_object_unref (factory);

    factory = gal_view_factory_etable_new (spec);
    gal_view_collection_add_factory (collection, factory);
    g_object_unref (factory);
    g_object_unref (spec);

    gal_view_collection_load (collection);
}

static void
cal_shell_view_notify_view_id_cb (ECalShellView *cal_shell_view)
{
    ECalShellContent *cal_shell_content;
    GalViewInstance *view_instance;
    const gchar *view_id;

    cal_shell_content = cal_shell_view->priv->cal_shell_content;
    view_instance =
        e_cal_shell_content_get_view_instance (cal_shell_content);
    view_id = e_shell_view_get_view_id (E_SHELL_VIEW (cal_shell_view));

    /* A NULL view ID implies we're in a custom view.  But you can
     * only get to a custom view via the "Define Views" dialog, which
     * would have already modified the view instance appropriately.
     * Furthermore, there's no way to refer to a custom view by ID
     * anyway, since custom views have no IDs. */
    if (view_id == NULL)
        return;

    gal_view_instance_set_current_view_id (view_instance, view_id);
}

void
e_cal_shell_view_private_init (ECalShellView *cal_shell_view,
                               EShellViewClass *shell_view_class)
{
    ECalShellViewPrivate *priv = cal_shell_view->priv;
    ESourceList *source_list;
    GObject *object;

    object = G_OBJECT (shell_view_class->type_module);
    source_list = g_object_get_data (object, "source-list");
    g_return_if_fail (E_IS_SOURCE_LIST (source_list));

    priv->source_list = g_object_ref (source_list);
    priv->calendar_actions = gtk_action_group_new ("calendars");

    if (!gal_view_collection_loaded (shell_view_class->view_collection))
        cal_shell_view_load_view_collection (shell_view_class);

    g_signal_connect (
        cal_shell_view, "notify::view-id",
        G_CALLBACK (cal_shell_view_notify_view_id_cb), NULL);
}

void
e_cal_shell_view_private_constructed (ECalShellView *cal_shell_view)
{
    ECalShellViewPrivate *priv = cal_shell_view->priv;
    EShellContent *shell_content;
    EShellSidebar *shell_sidebar;
    EShellView *shell_view;
    GnomeCalendar *calendar;

    shell_view = E_SHELL_VIEW (cal_shell_view);
    shell_content = e_shell_view_get_shell_content (shell_view);
    shell_sidebar = e_shell_view_get_shell_sidebar (shell_view);

    /* Cache these to avoid lots of awkward casting. */
    priv->cal_shell_content = g_object_ref (shell_content);
    priv->cal_shell_sidebar = g_object_ref (shell_sidebar);

    calendar = e_cal_shell_view_get_calendar (cal_shell_view);

    g_signal_connect_swapped (
        calendar, "dates-shown-changed",
        G_CALLBACK (e_cal_shell_view_update_sidebar),
        cal_shell_view);

    e_shell_view_update_actions (shell_view);
    e_cal_shell_view_update_sidebar (cal_shell_view);
}

void
e_cal_shell_view_private_dispose (ECalShellView *cal_shell_view)
{
    ECalShellViewPrivate *priv = cal_shell_view->priv;

    DISPOSE (priv->source_list);

    DISPOSE (priv->calendar_actions);

    DISPOSE (priv->cal_shell_content);
    DISPOSE (priv->cal_shell_sidebar);

    if (cal_shell_view->priv->activity != NULL) {
        /* XXX Activity is not cancellable. */
        e_activity_complete (cal_shell_view->priv->activity);
        g_object_unref (cal_shell_view->priv->activity);
        cal_shell_view->priv->activity = NULL;
    }
}

void
e_cal_shell_view_private_finalize (ECalShellView *cal_shell_view)
{
    /* XXX Nothing to do? */
}

void
e_cal_shell_view_set_status_message (ECalShellView *cal_shell_view,
                                     const gchar *status_message)
{
    EActivity *activity;
    EShellView *shell_view;
    EShellModule *shell_module;

    g_return_if_fail (E_IS_CAL_SHELL_VIEW (cal_shell_view));

    activity = cal_shell_view->priv->activity;
    shell_view = E_SHELL_VIEW (cal_shell_view);
    shell_module = e_shell_view_get_shell_module (shell_view);

    if (status_message == NULL || *status_message == '\0') {
        if (activity != NULL) {
            e_activity_complete (activity);
            g_object_unref (activity);
            activity = NULL;
        }

    } else if (activity == NULL) {
        activity = e_activity_new (status_message);
        e_shell_module_add_activity (shell_module, activity);

    } else
        e_activity_set_primary_text (activity, status_message);

    cal_shell_view->priv->activity = activity;
}

void
e_cal_shell_view_update_sidebar (ECalShellView *cal_shell_view)
{
    EShellView *shell_view;
    EShellSidebar *shell_sidebar;
    GnomeCalendar *calendar;
    GnomeCalendarViewType view;
    time_t start_time, end_time;
    struct tm start_tm, end_tm;
    struct icaltimetype start_tt, end_tt;
    icaltimezone *timezone;
    gchar buffer[512];
    gchar end_buffer[512];

    shell_view = E_SHELL_VIEW (cal_shell_view);
    shell_sidebar = e_shell_view_get_shell_sidebar (shell_view);

    calendar = e_cal_shell_view_get_calendar (cal_shell_view);

    gnome_calendar_get_visible_time_range (
        calendar, &start_time, &end_time);
    timezone = gnome_calendar_get_timezone (calendar);
    view = gnome_calendar_get_view (calendar);

    start_tt = icaltime_from_timet_with_zone (start_time, FALSE, timezone);
    start_tm.tm_year = start_tt.year - 1900;
    start_tm.tm_mon = start_tt.month - 1;
    start_tm.tm_mday = start_tt.day;
    start_tm.tm_hour = start_tt.hour;
    start_tm.tm_min = start_tt.minute;
    start_tm.tm_sec = start_tt.second;
    start_tm.tm_isdst = -1;
    start_tm.tm_wday = time_day_of_week (
        start_tt.day, start_tt.month - 1, start_tt.year);

    /* Subtract one from end_time so we don't get an extra day. */
    end_tt = icaltime_from_timet_with_zone (end_time - 1, FALSE, timezone);
    end_tm.tm_year = end_tt.year - 1900;
    end_tm.tm_mon = end_tt.month - 1;
    end_tm.tm_mday = end_tt.day;
    end_tm.tm_hour = end_tt.hour;
    end_tm.tm_min = end_tt.minute;
    end_tm.tm_sec = end_tt.second;
    end_tm.tm_isdst = -1;
    end_tm.tm_wday = time_day_of_week (
        end_tt.day, end_tt.month - 1, end_tt.year);

    switch (view) {
        case GNOME_CAL_DAY_VIEW:
        case GNOME_CAL_WORK_WEEK_VIEW:
        case GNOME_CAL_WEEK_VIEW:
            if (start_tm.tm_year == end_tm.tm_year &&
                start_tm.tm_mon == end_tm.tm_mon &&
                start_tm.tm_mday == end_tm.tm_mday) {
                e_utf8_strftime (
                    buffer, sizeof (buffer),
                    _("%A %d %b %Y"), &start_tm);
            } else if (start_tm.tm_year == end_tm.tm_year) {
                e_utf8_strftime (
                    buffer, sizeof (buffer),
                    _("%a %d %b"), &start_tm);
                e_utf8_strftime (
                    end_buffer, sizeof (end_buffer),
                    _("%a %d %b %Y"), &end_tm);
                strcat (buffer, " - ");
                strcat (buffer, end_buffer);
            } else {
                e_utf8_strftime (
                    buffer, sizeof (buffer),
                    _("%a %d %b %Y"), &start_tm);
                e_utf8_strftime (
                    end_buffer, sizeof (end_buffer),
                    _("%a %d %b %Y"), &end_tm);
                strcat (buffer, " - ");
                strcat (buffer, end_buffer);
            }
            break;

        case GNOME_CAL_MONTH_VIEW:
        case GNOME_CAL_LIST_VIEW:
            if (start_tm.tm_year == end_tm.tm_year) {
                if (start_tm.tm_mon == end_tm.tm_mon) {
                    e_utf8_strftime (
                        buffer,
                        sizeof (buffer),
                        "%d", &start_tm);
                    e_utf8_strftime (
                        end_buffer,
                        sizeof (end_buffer),
                        _("%d %b %Y"), &end_tm);
                    strcat (buffer, " - ");
                    strcat (buffer, end_buffer);
                } else {
                    e_utf8_strftime (
                        buffer,
                        sizeof (buffer),
                        _("%d %b"), &start_tm);
                    e_utf8_strftime (
                        end_buffer,
                        sizeof (end_buffer),
                        _("%d %b %Y"), &end_tm);
                    strcat (buffer, " - ");
                    strcat (buffer, end_buffer);
                }
            } else {
                e_utf8_strftime (
                    buffer, sizeof (buffer),
                    _("%d %b %Y"), &start_tm);
                e_utf8_strftime (
                    end_buffer, sizeof (end_buffer),
                    _("%d %b %Y"), &end_tm);
                strcat (buffer, " - ");
                strcat (buffer, end_buffer);
            }
            break;

        default:
            g_return_if_reached ();
    }

    e_shell_sidebar_set_secondary_text (shell_sidebar, buffer);
}