e-cal-shell-view-memopad.c 13.2 KB
Newer Older
Matthew Barnes's avatar
Matthew Barnes committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * e-cal-shell-view-memopad.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * 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/>
Matthew Barnes's avatar
Matthew Barnes committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

/* Much of this file is based on e-memo-shell-view-actions.c. */

static void
action_calendar_memopad_forward_cb (GtkAction *action,
                                    ECalShellView *cal_shell_view)
{
	ECalShellContent *cal_shell_content;
	EMemoTable *memo_table;
	ECalModelComponent *comp_data;
	ECalComponent *comp;
	icalcomponent *clone;
	GSList *list;

	cal_shell_content = cal_shell_view->priv->cal_shell_content;
	memo_table = e_cal_shell_content_get_memo_table (cal_shell_content);

	list = e_memo_table_get_selected (memo_table);
	g_return_if_fail (list != NULL);
	comp_data = list->data;
	g_slist_free (list);

	/* XXX We only forward the first selected memo. */
	comp = e_cal_component_new ();
	clone = icalcomponent_new_clone (comp_data->icalcomp);
	e_cal_component_set_icalcomponent (comp, clone);
49 50
	itip_send_comp (
		E_CAL_COMPONENT_METHOD_PUBLISH, comp,
51
		comp_data->client, NULL, NULL, NULL, TRUE, FALSE);
Matthew Barnes's avatar
Matthew Barnes committed
52 53 54 55 56 57 58
	g_object_unref (comp);
}

static void
action_calendar_memopad_new_cb (GtkAction *action,
                                ECalShellView *cal_shell_view)
{
59 60 61
	EShell *shell;
	EShellView *shell_view;
	EShellWindow *shell_window;
Matthew Barnes's avatar
Matthew Barnes committed
62 63 64 65 66 67 68 69
	ECalShellContent *cal_shell_content;
	EMemoTable *memo_table;
	ECalModelComponent *comp_data;
	ECal *client;
	ECalComponent *comp;
	CompEditor *editor;
	GSList *list;

70 71 72 73
	shell_view = E_SHELL_VIEW (cal_shell_view);
	shell_window = e_shell_view_get_shell_window (shell_view);
	shell = e_shell_window_get_shell (shell_window);

Matthew Barnes's avatar
Matthew Barnes committed
74 75 76 77 78 79 80 81 82 83
	cal_shell_content = cal_shell_view->priv->cal_shell_content;
	memo_table = e_cal_shell_content_get_memo_table (cal_shell_content);

	list = e_memo_table_get_selected (memo_table);
	g_return_if_fail (list != NULL);
	comp_data = list->data;
	g_slist_free (list);

	client = comp_data->client;
	comp = cal_comp_memo_new_with_defaults (client);
84 85
	cal_comp_update_time_by_active_window (comp, shell);
	editor = memo_editor_new (client, shell, COMP_EDITOR_NEW_ITEM);
Matthew Barnes's avatar
Matthew Barnes committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	comp_editor_edit_comp (editor, comp);

	gtk_window_present (GTK_WINDOW (editor));

	g_object_unref (comp);
	g_object_unref (client);
}

static void
action_calendar_memopad_open_cb (GtkAction *action,
                                 ECalShellView *cal_shell_view)
{
	ECalShellContent *cal_shell_content;
	EMemoTable *memo_table;
	ECalModelComponent *comp_data;
	GSList *list;

	cal_shell_content = cal_shell_view->priv->cal_shell_content;
	memo_table = e_cal_shell_content_get_memo_table (cal_shell_content);

	list = e_memo_table_get_selected (memo_table);
	g_return_if_fail (list != NULL);
	comp_data = list->data;
	g_slist_free (list);

	/* XXX We only open the first selected memo. */
	e_cal_shell_view_memopad_open_memo (cal_shell_view, comp_data);
}

static void
action_calendar_memopad_open_url_cb (GtkAction *action,
                                     ECalShellView *cal_shell_view)
{
119 120
	EShellView *shell_view;
	EShellWindow *shell_window;
Matthew Barnes's avatar
Matthew Barnes committed
121 122 123 124 125 126
	ECalShellContent *cal_shell_content;
	EMemoTable *memo_table;
	ECalModelComponent *comp_data;
	icalproperty *prop;
	const gchar *uri;
	GSList *list;
127 128 129

	shell_view = E_SHELL_VIEW (cal_shell_view);
	shell_window = e_shell_view_get_shell_window (shell_view);
Matthew Barnes's avatar
Matthew Barnes committed
130 131 132 133 134 135 136 137 138 139 140 141

	cal_shell_content = cal_shell_view->priv->cal_shell_content;
	memo_table = e_cal_shell_content_get_memo_table (cal_shell_content);

	list = e_memo_table_get_selected (memo_table);
	g_return_if_fail (list != NULL);
	comp_data = list->data;
	g_slist_free (list);

	/* XXX We only open the URI of the first selected memo. */
	prop = icalcomponent_get_first_property (
		comp_data->icalcomp, ICAL_URL_PROPERTY);
142
	g_return_if_fail (prop != NULL);
Matthew Barnes's avatar
Matthew Barnes committed
143 144

	uri = icalproperty_get_url (prop);
145
	e_show_uri (GTK_WINDOW (shell_window), uri);
Matthew Barnes's avatar
Matthew Barnes committed
146 147 148 149 150 151 152 153 154 155
}

static void
action_calendar_memopad_print_cb (GtkAction *action,
                                  ECalShellView *cal_shell_view)
{
	ECalShellContent *cal_shell_content;
	EMemoTable *memo_table;
	ECalModelComponent *comp_data;
	ECalComponent *comp;
156
	ECalModel *model;
Matthew Barnes's avatar
Matthew Barnes committed
157 158 159 160 161
	icalcomponent *clone;
	GSList *list;

	cal_shell_content = cal_shell_view->priv->cal_shell_content;
	memo_table = e_cal_shell_content_get_memo_table (cal_shell_content);
162
	model = e_memo_table_get_model (memo_table);
Matthew Barnes's avatar
Matthew Barnes committed
163 164 165 166 167 168 169 170 171 172

	list = e_memo_table_get_selected (memo_table);
	g_return_if_fail (list != NULL);
	comp_data = list->data;
	g_slist_free (list);

	/* XXX We only print the first selected memo. */
	comp = e_cal_component_new ();
	clone = icalcomponent_new_clone (comp_data->icalcomp);
	e_cal_component_set_icalcomponent (comp, clone);
173 174 175 176 177 178 179

	print_comp (
		comp, comp_data->client,
		e_cal_model_get_timezone (model),
		e_cal_model_get_use_24_hour_format (model),
		GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG);

Matthew Barnes's avatar
Matthew Barnes committed
180 181 182 183 184 185 186
	g_object_unref (comp);
}

static void
action_calendar_memopad_save_as_cb (GtkAction *action,
                                    ECalShellView *cal_shell_view)
{
187
	EShell *shell;
188 189
	EShellView *shell_view;
	EShellWindow *shell_window;
190
	EShellBackend *shell_backend;
Matthew Barnes's avatar
Matthew Barnes committed
191 192 193
	ECalShellContent *cal_shell_content;
	EMemoTable *memo_table;
	ECalModelComponent *comp_data;
194
	EActivity *activity;
Matthew Barnes's avatar
Matthew Barnes committed
195
	GSList *list;
196
	GFile *file;
Matthew Barnes's avatar
Matthew Barnes committed
197 198
	gchar *string;

199 200
	shell_view = E_SHELL_VIEW (cal_shell_view);
	shell_window = e_shell_view_get_shell_window (shell_view);
201 202
	shell_backend = e_shell_view_get_shell_backend (shell_view);
	shell = e_shell_window_get_shell (shell_window);
203

Matthew Barnes's avatar
Matthew Barnes committed
204 205 206 207 208 209 210 211
	cal_shell_content = cal_shell_view->priv->cal_shell_content;
	memo_table = e_cal_shell_content_get_memo_table (cal_shell_content);

	list = e_memo_table_get_selected (memo_table);
	g_return_if_fail (list != NULL);
	comp_data = list->data;
	g_slist_free (list);

212 213
	/* Translators: Default filename part saving a memo to a file when
	 * no summary is filed, the '.ics' extension is concatenated to it. */
214
	string = icalcomp_suggest_filename (comp_data->icalcomp, _("memo"));
215
	file = e_shell_run_save_dialog (
216 217
		shell, _("Save as iCalendar"), string,
		"*.ics:text/calendar", NULL, NULL);
218
	g_free (string);
219
	if (file == NULL)
Matthew Barnes's avatar
Matthew Barnes committed
220 221 222 223 224 225 226
		return;

	/* XXX We only save the first selected memo. */
	string = e_cal_get_component_as_string (
		comp_data->client, comp_data->icalcomp);
	if (string == NULL) {
		g_warning ("Could not convert memo to a string.");
227
		g_object_unref (file);
Matthew Barnes's avatar
Matthew Barnes committed
228 229 230
		return;
	}

231 232 233 234 235
	/* XXX No callback means errors are discarded. */
	activity = e_file_replace_contents_async (
		file, string, strlen (string), NULL, FALSE,
		G_FILE_CREATE_NONE, (GAsyncReadyCallback) NULL, NULL);
	e_shell_backend_add_activity (shell_backend, activity);
Matthew Barnes's avatar
Matthew Barnes committed
236

237 238 239 240 241 242 243
	/* Free the string when the activity is finalized. */
	g_object_set_data_full (
		G_OBJECT (activity),
		"file-content", string,
		(GDestroyNotify) g_free);

	g_object_unref (file);
Matthew Barnes's avatar
Matthew Barnes committed
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
}

static GtkActionEntry calendar_memopad_entries[] = {

	{ "calendar-memopad-forward",
	  "mail-forward",
	  N_("_Forward as iCalendar..."),
	  NULL,
	  NULL,  /* XXX Add a tooltip! */
	  G_CALLBACK (action_calendar_memopad_forward_cb) },

	{ "calendar-memopad-new",
	  "stock_insert-note",
	  N_("New _Memo"),
	  NULL,
	  N_("Create a new memo"),
	  G_CALLBACK (action_calendar_memopad_new_cb) },

	{ "calendar-memopad-open",
	  GTK_STOCK_OPEN,
	  N_("_Open Memo"),
	  NULL,
	  N_("View the selected memo"),
	  G_CALLBACK (action_calendar_memopad_open_cb) },

	{ "calendar-memopad-open-url",
	  "applications-internet",
	  N_("Open _Web Page"),
	  NULL,
	  NULL,  /* XXX Add a tooltip! */
	  G_CALLBACK (action_calendar_memopad_open_url_cb) },
};

277 278 279 280 281 282 283 284 285 286
static GtkActionEntry lockdown_printing_entries[] = {

	{ "calendar-memopad-print",
	  GTK_STOCK_PRINT,
	  NULL,
	  NULL,
	  N_("Print the selected memo"),
	  G_CALLBACK (action_calendar_memopad_print_cb) }
};

287 288 289 290 291 292 293 294 295 296
static GtkActionEntry lockdown_save_to_disk_entries[] = {

	{ "calendar-memopad-save-as",
	  GTK_STOCK_SAVE_AS,
	  N_("Save as iCalendar..."),
	  NULL,
	  NULL,  /* XXX Add a tooltip! */
	  G_CALLBACK (action_calendar_memopad_save_as_cb) }
};

Matthew Barnes's avatar
Matthew Barnes committed
297 298 299 300 301 302 303 304 305 306
void
e_cal_shell_view_memopad_actions_init (ECalShellView *cal_shell_view)
{
	EShellView *shell_view;
	EShellWindow *shell_window;
	GtkActionGroup *action_group;

	shell_view = E_SHELL_VIEW (cal_shell_view);
	shell_window = e_shell_view_get_shell_window (shell_view);

307
	/* Calendar Actions */
308
	action_group = ACTION_GROUP (CALENDAR);
Matthew Barnes's avatar
Matthew Barnes committed
309 310 311
	gtk_action_group_add_actions (
		action_group, calendar_memopad_entries,
		G_N_ELEMENTS (calendar_memopad_entries), cal_shell_view);
312 313 314 315 316 317

	/* Lockdown Printing Actions */
	action_group = ACTION_GROUP (LOCKDOWN_PRINTING);
	gtk_action_group_add_actions (
		action_group, lockdown_printing_entries,
		G_N_ELEMENTS (lockdown_printing_entries), cal_shell_view);
318 319 320 321 322 323

	/* Lockdown Save-to-Disk Actions */
	action_group = ACTION_GROUP (LOCKDOWN_SAVE_TO_DISK);
	gtk_action_group_add_actions (
		action_group, lockdown_save_to_disk_entries,
		G_N_ELEMENTS (lockdown_save_to_disk_entries), cal_shell_view);
Matthew Barnes's avatar
Matthew Barnes committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
}

void
e_cal_shell_view_memopad_actions_update (ECalShellView *cal_shell_view)
{
	ECalShellContent *cal_shell_content;
	EShellWindow *shell_window;
	EShellView *shell_view;
	EMemoTable *memo_table;
	GtkAction *action;
	GSList *list, *iter;
	gboolean editable = TRUE;
	gboolean has_url = FALSE;
	gboolean sensitive;
	gint n_selected;

	shell_view = E_SHELL_VIEW (cal_shell_view);
	shell_window = e_shell_view_get_shell_window (shell_view);

	cal_shell_content = cal_shell_view->priv->cal_shell_content;
	memo_table = e_cal_shell_content_get_memo_table (cal_shell_content);

Matthew Barnes's avatar
Matthew Barnes committed
346
	n_selected = e_table_selected_count (E_TABLE (memo_table));
Matthew Barnes's avatar
Matthew Barnes committed
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

	list = e_memo_table_get_selected (memo_table);
	for (iter = list; iter != NULL; iter = iter->next) {
		ECalModelComponent *comp_data = iter->data;
		icalproperty *prop;
		gboolean read_only;

		e_cal_is_read_only (comp_data->client, &read_only, NULL);
		editable &= !read_only;

		prop = icalcomponent_get_first_property (
			comp_data->icalcomp, ICAL_URL_PROPERTY);
		has_url |= (prop != NULL);
	}
	g_slist_free (list);

	action = ACTION (CALENDAR_MEMOPAD_FORWARD);
	sensitive = (n_selected == 1);
	gtk_action_set_sensitive (action, sensitive);

	action = ACTION (CALENDAR_MEMOPAD_OPEN);
	sensitive = (n_selected == 1);
	gtk_action_set_sensitive (action, sensitive);

	action = ACTION (CALENDAR_MEMOPAD_OPEN_URL);
	sensitive = (n_selected == 1) && has_url;
	gtk_action_set_sensitive (action, sensitive);

	action = ACTION (CALENDAR_MEMOPAD_PRINT);
	sensitive = (n_selected == 1);
	gtk_action_set_sensitive (action, sensitive);

	action = ACTION (CALENDAR_MEMOPAD_SAVE_AS);
	sensitive = (n_selected == 1);
	gtk_action_set_sensitive (action, sensitive);
}

void
e_cal_shell_view_memopad_open_memo (ECalShellView *cal_shell_view,
                                    ECalModelComponent *comp_data)
{
388 389 390
	EShell *shell;
	EShellView *shell_view;
	EShellWindow *shell_window;
Matthew Barnes's avatar
Matthew Barnes committed
391 392 393 394 395 396 397 398 399
	CompEditor *editor;
	CompEditorFlags flags = 0;
	ECalComponent *comp;
	icalcomponent *clone;
	const gchar *uid;

	g_return_if_fail (E_IS_CAL_SHELL_VIEW (cal_shell_view));
	g_return_if_fail (E_IS_CAL_MODEL_COMPONENT (comp_data));

400 401 402 403
	shell_view = E_SHELL_VIEW (cal_shell_view);
	shell_window = e_shell_view_get_shell_window (shell_view);
	shell = e_shell_window_get_shell (shell_window);

Matthew Barnes's avatar
Matthew Barnes committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
	uid = icalcomponent_get_uid (comp_data->icalcomp);
	editor = comp_editor_find_instance (uid);

	if (editor != NULL)
		goto exit;

	comp = e_cal_component_new ();
	clone = icalcomponent_new_clone (comp_data->icalcomp);
	e_cal_component_set_icalcomponent (comp, clone);

	if (e_cal_component_has_organizer (comp))
		flags |= COMP_EDITOR_IS_SHARED;

	if (itip_organizer_is_user (comp, comp_data->client))
		flags |= COMP_EDITOR_USER_ORG;

420
	editor = memo_editor_new (comp_data->client, shell, flags);
Matthew Barnes's avatar
Matthew Barnes committed
421 422 423 424 425 426 427
	comp_editor_edit_comp (editor, comp);

	g_object_unref (comp);

exit:
	gtk_window_present (GTK_WINDOW (editor));
}
428 429 430 431 432 433 434 435

void
e_cal_shell_view_memopad_set_status_message (ECalShellView *cal_shell_view,
                                             const gchar *status_message,
                                             gdouble percent)
{
	EActivity *activity;
	EShellView *shell_view;
436
	EShellBackend *shell_backend;
437 438 439 440

	g_return_if_fail (E_IS_CAL_SHELL_VIEW (cal_shell_view));

	shell_view = E_SHELL_VIEW (cal_shell_view);
441
	shell_backend = e_shell_view_get_shell_backend (shell_view);
442 443 444 445 446

	activity = cal_shell_view->priv->memopad_activity;

	if (status_message == NULL || *status_message == '\0') {
		if (activity != NULL) {
Matthew Barnes's avatar
Matthew Barnes committed
447
			e_activity_set_state (activity, E_ACTIVITY_COMPLETED);
448 449 450 451 452
			g_object_unref (activity);
			activity = NULL;
		}

	} else if (activity == NULL) {
453
		activity = e_activity_new ();
454
		e_activity_set_percent (activity, percent);
Matthew Barnes's avatar
Matthew Barnes committed
455
		e_activity_set_text (activity, status_message);
456
		e_shell_backend_add_activity (shell_backend, activity);
457 458 459

	} else {
		e_activity_set_percent (activity, percent);
Matthew Barnes's avatar
Matthew Barnes committed
460
		e_activity_set_text (activity, status_message);
461 462 463 464
	}

	cal_shell_view->priv->memopad_activity = activity;
}