~ubuntu-branches/ubuntu/precise/evolution/precise

« back to all changes in this revision

Viewing changes to calendar/gui/dialogs/goto-dialog.c

  • Committer: Bazaar Package Importer
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2011-06-24 00:27:53 UTC
  • mfrom: (1.1.80 upstream)
  • Revision ID: james.westby@ubuntu.com-20110624002753-2vh1vjavlya7r103
Tags: 3.1.2-0ubuntu1
* New upstream release 3.1.2.
* debian/control: adjust Build-Depends to match new upstream requirements for
  the new release.
* debian/control: update evolution and evolution-dev Depends to require the
  right versions of e-d-s (>= 3.1, << 3.2), and drop libunique-3.0-dev.
* debian/control,
  debian/evolution.install: remove groupwise-features from the plugin list,
  it's now split out into a separate module.
* debian/patches/03_lpi.patch: refreshed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Go to date dialog for Evolution
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) version 3.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 
16
 *
 
17
 *
 
18
 * Authors:
 
19
 *              Federico Mena <federico@ximian.com>
 
20
 *      JP Rosevear <jpr@ximian.com>
 
21
 *
 
22
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 
23
 * Copyright (C) 1998 Red Hat, Inc.
 
24
 *
 
25
 */
 
26
 
 
27
#ifdef HAVE_CONFIG_H
 
28
#include <config.h>
 
29
#endif
 
30
 
 
31
#include <gtk/gtk.h>
 
32
 
 
33
#include "goto-dialog.h"
 
34
 
 
35
#include "e-util/e-util.h"
 
36
#include "e-util/e-util-private.h"
 
37
#include "calendar/gui/calendar-config.h"
 
38
#include "calendar/gui/tag-calendar.h"
 
39
 
 
40
typedef struct
 
41
{
 
42
        GtkBuilder *builder;
 
43
        GtkWidget *dialog;
 
44
 
 
45
        GtkWidget *month_combobox;
 
46
        GtkWidget *year;
 
47
        ECalendar *ecal;
 
48
        GtkWidget *vbox;
 
49
 
 
50
        GnomeCalendar *gcal;
 
51
        gint year_val;
 
52
        gint month_val;
 
53
        gint day_val;
 
54
 
 
55
} GoToDialog;
 
56
 
 
57
static GoToDialog *dlg = NULL;
 
58
 
 
59
/* Callback used when the year adjustment is changed */
 
60
static void
 
61
year_changed (GtkAdjustment *adj, gpointer data)
 
62
{
 
63
        GtkSpinButton *spin_button;
 
64
        GoToDialog *dlg = data;
 
65
 
 
66
        spin_button = GTK_SPIN_BUTTON (dlg->year);
 
67
        dlg->year_val = gtk_spin_button_get_value_as_int (spin_button);
 
68
 
 
69
        e_calendar_item_set_first_month (
 
70
                dlg->ecal->calitem, dlg->year_val, dlg->month_val);
 
71
}
 
72
 
 
73
/* Callback used when a month button is toggled */
 
74
static void
 
75
month_changed (GtkToggleButton *toggle, gpointer data)
 
76
{
 
77
        GtkComboBox *combo_box;
 
78
        GoToDialog *dlg = data;
 
79
 
 
80
        combo_box = GTK_COMBO_BOX (dlg->month_combobox);
 
81
        dlg->month_val = gtk_combo_box_get_active (combo_box);
 
82
 
 
83
        e_calendar_item_set_first_month (
 
84
                dlg->ecal->calitem, dlg->year_val, dlg->month_val);
 
85
}
 
86
 
 
87
static void
 
88
ecal_date_range_changed (ECalendarItem *calitem, gpointer user_data)
 
89
{
 
90
        GoToDialog *dlg = user_data;
 
91
        ECalModel *model;
 
92
        ECal *client;
 
93
 
 
94
        model = gnome_calendar_get_model (dlg->gcal);
 
95
        client = e_cal_model_get_default_client (model);
 
96
        if (client)
 
97
                tag_calendar_by_client (dlg->ecal, client);
 
98
}
 
99
 
 
100
/* Event handler for day groups in the month item.  A button press makes
 
101
 * the calendar jump to the selected day and destroys the Go-to dialog box. */
 
102
static void
 
103
ecal_event (ECalendarItem *calitem, gpointer user_data)
 
104
{
 
105
        GoToDialog *dlg = user_data;
 
106
        GDate start_date, end_date;
 
107
        ECalModel *model;
 
108
        struct icaltimetype tt = icaltime_null_time ();
 
109
        icaltimezone *timezone;
 
110
        time_t et;
 
111
 
 
112
        model = gnome_calendar_get_model (dlg->gcal);
 
113
        e_calendar_item_get_selection (calitem, &start_date, &end_date);
 
114
        timezone = e_cal_model_get_timezone (model);
 
115
 
 
116
        tt.year = g_date_get_year (&start_date);
 
117
        tt.month = g_date_get_month (&start_date);
 
118
        tt.day = g_date_get_day (&start_date);
 
119
 
 
120
        et = icaltime_as_timet_with_zone (tt, timezone);
 
121
 
 
122
        gnome_calendar_goto (dlg->gcal, et);
 
123
 
 
124
        gtk_dialog_response (GTK_DIALOG (dlg->dialog), GTK_RESPONSE_NONE);
 
125
}
 
126
 
 
127
/* Returns the current time, for the ECalendarItem. */
 
128
static struct tm
 
129
get_current_time (ECalendarItem *calitem, gpointer data)
 
130
{
 
131
        icaltimezone *zone;
 
132
        struct tm tmp_tm = { 0 };
 
133
        struct icaltimetype tt;
 
134
 
 
135
        /* Get the current timezone. */
 
136
        zone = calendar_config_get_icaltimezone ();
 
137
 
 
138
        tt = icaltime_from_timet_with_zone (time (NULL), FALSE, zone);
 
139
 
 
140
        /* Now copy it to the struct tm and return it. */
 
141
        tmp_tm.tm_year  = tt.year - 1900;
 
142
        tmp_tm.tm_mon   = tt.month - 1;
 
143
        tmp_tm.tm_mday  = tt.day;
 
144
        tmp_tm.tm_hour  = tt.hour;
 
145
        tmp_tm.tm_min   = tt.minute;
 
146
        tmp_tm.tm_sec   = tt.second;
 
147
        tmp_tm.tm_isdst = -1;
 
148
 
 
149
        return tmp_tm;
 
150
}
 
151
 
 
152
/* Creates the ecalendar */
 
153
static void
 
154
create_ecal (GoToDialog *dlg)
 
155
{
 
156
        ECalendarItem *calitem;
 
157
 
 
158
        dlg->ecal = E_CALENDAR (e_calendar_new ());
 
159
        calitem = dlg->ecal->calitem;
 
160
 
 
161
        gnome_canvas_item_set (GNOME_CANVAS_ITEM (calitem),
 
162
                        "move_selection_when_moving", FALSE,
 
163
                        NULL);
 
164
        e_calendar_item_set_display_popup (calitem, FALSE);
 
165
        gtk_widget_show (GTK_WIDGET (dlg->ecal));
 
166
        gtk_box_pack_start (GTK_BOX (dlg->vbox), GTK_WIDGET (dlg->ecal), TRUE, TRUE, 0);
 
167
 
 
168
        e_calendar_item_set_first_month (calitem, dlg->year_val, dlg->month_val);
 
169
        e_calendar_item_set_get_time_callback (calitem,
 
170
                                               get_current_time,
 
171
                                               dlg, NULL);
 
172
 
 
173
        ecal_date_range_changed (calitem, dlg);
 
174
}
 
175
 
 
176
static void
 
177
goto_today (GoToDialog *dlg)
 
178
{
 
179
        gnome_calendar_goto_today (dlg->gcal);
 
180
}
 
181
 
 
182
/* Gets the widgets from the XML file and returns if they are all available. */
 
183
static gboolean
 
184
get_widgets (GoToDialog *dlg)
 
185
{
 
186
        #define GW(name) e_builder_get_widget (dlg->builder, name)
 
187
 
 
188
        dlg->dialog = GW ("goto-dialog");
 
189
 
 
190
        dlg->month_combobox = GW ("month-combobox");
 
191
        dlg->year = GW ("year");
 
192
        dlg->vbox = GW ("vbox");
 
193
 
 
194
        #undef GW
 
195
 
 
196
        return (dlg->dialog
 
197
                && dlg->month_combobox
 
198
                && dlg->year
 
199
                && dlg->vbox);
 
200
}
 
201
 
 
202
static void
 
203
goto_dialog_init_widgets (GoToDialog *dlg)
 
204
{
 
205
        GtkAdjustment *adj;
 
206
 
 
207
        g_signal_connect (
 
208
                dlg->month_combobox, "changed",
 
209
                G_CALLBACK (month_changed), dlg);
 
210
 
 
211
        adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (dlg->year));
 
212
        g_signal_connect (
 
213
                adj, "value_changed",
 
214
                G_CALLBACK (year_changed), dlg);
 
215
 
 
216
        g_signal_connect (
 
217
                dlg->ecal->calitem, "date_range_changed",
 
218
                G_CALLBACK (ecal_date_range_changed), dlg);
 
219
        g_signal_connect (
 
220
                dlg->ecal->calitem, "selection_changed",
 
221
                G_CALLBACK (ecal_event), dlg);
 
222
}
 
223
 
 
224
/* Creates a "goto date" dialog and runs it */
 
225
void
 
226
goto_dialog (GtkWindow *parent, GnomeCalendar *gcal)
 
227
{
 
228
        ECalModel *model;
 
229
        time_t start_time;
 
230
        struct icaltimetype tt;
 
231
        icaltimezone *timezone;
 
232
        gint week_start_day;
 
233
        gint b;
 
234
 
 
235
        if (dlg) {
 
236
                return;
 
237
        }
 
238
 
 
239
        dlg = g_new0 (GoToDialog, 1);
 
240
 
 
241
        /* Load the content widgets */
 
242
        dlg->builder = gtk_builder_new ();
 
243
        e_load_ui_builder_definition (dlg->builder, "goto-dialog.ui");
 
244
 
 
245
        if (!get_widgets (dlg)) {
 
246
                g_message ("goto_dialog(): Could not find all widgets in the XML file!");
 
247
                g_free (dlg);
 
248
                return;
 
249
        }
 
250
        dlg->gcal = gcal;
 
251
 
 
252
        model = gnome_calendar_get_model (gcal);
 
253
        timezone = e_cal_model_get_timezone (model);
 
254
        e_cal_model_get_time_range (model, &start_time, NULL);
 
255
        tt = icaltime_from_timet_with_zone (start_time, FALSE, timezone);
 
256
        dlg->year_val = tt.year;
 
257
        dlg->month_val = tt.month - 1;
 
258
        dlg->day_val = tt.day;
 
259
 
 
260
        gtk_combo_box_set_active (GTK_COMBO_BOX (dlg->month_combobox), dlg->month_val);
 
261
        gtk_spin_button_set_value (GTK_SPIN_BUTTON (dlg->year), dlg->year_val);
 
262
 
 
263
        create_ecal (dlg);
 
264
 
 
265
        goto_dialog_init_widgets (dlg);
 
266
 
 
267
        gtk_window_set_transient_for (GTK_WINDOW (dlg->dialog), parent);
 
268
 
 
269
        /* set initial selection to current day */
 
270
 
 
271
        dlg->ecal->calitem->selection_set = TRUE;
 
272
        dlg->ecal->calitem->selection_start_month_offset = 0;
 
273
        dlg->ecal->calitem->selection_start_day = tt.day;
 
274
        dlg->ecal->calitem->selection_end_month_offset = 0;
 
275
        dlg->ecal->calitem->selection_end_day = tt.day;
 
276
 
 
277
        /* Set week_start_day. Convert it to 0 (Mon) to 6 (Sun), which is what we use. */
 
278
        week_start_day = e_cal_model_get_week_start_day (model);
 
279
        dlg->ecal->calitem->week_start_day = (week_start_day + 6) % 7;
 
280
 
 
281
        gnome_canvas_item_grab_focus (GNOME_CANVAS_ITEM (dlg->ecal->calitem));
 
282
 
 
283
        b = gtk_dialog_run (GTK_DIALOG (dlg->dialog));
 
284
        gtk_widget_destroy (dlg->dialog);
 
285
 
 
286
        if (b == 0)
 
287
                goto_today (dlg);
 
288
 
 
289
        g_object_unref (dlg->builder);
 
290
        g_free (dlg);
 
291
        dlg = NULL;
 
292
}