/* * openmoko-dates -- OpenMoko Calendar Application * * Authored by Chris Lord * * Copyright (C) 2006-2007 OpenMoko Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser Public License as published by * the Free Software Foundation; version 2 of the license. * * 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 Public License for more details. * * Current Version: $Rev$ ($Date$) [$Author$] */ #include #include #include "dates-utils.h" static const gchar *preset_categories[] = { "Meetings", "Appointments", "Birthdays", "Lessons", "Leisure", NULL }; GtkToolItem * dates_utils_toolbutton_new (const gchar *icon_name) { GtkToolItem *button = gtk_tool_button_new_from_stock (icon_name); gtk_tool_item_set_expand (button, TRUE); return button; } GtkToolItem * dates_utils_toggle_toolbutton_new (const gchar *icon_name) { GtkToolItem *button = gtk_toggle_tool_button_new_from_stock (icon_name); gtk_tool_item_set_expand (button, TRUE); return button; } void dates_utils_refresh_query (DatesData *data) { GList *view_l; for (view_l = data->views; view_l; view_l = view_l->next) { JanaStoreView *view = (JanaStoreView *)view_l->data; jana_store_view_set_range (view, data->start, data->end); } } gchar * dates_utils_casefold_replace (gchar *string) { gchar *newstring; if (!string) return NULL; newstring = g_utf8_casefold (string, -1); g_free (string); return newstring; } void dates_utils_fill_category_bar (DatesData *data, MokoSearchBar *bar) { GList *categories; GtkComboBox *combo = moko_search_bar_get_combo_box (bar); for (categories = data->category_list; categories; categories = categories->next) gtk_combo_box_append_text (combo, (gchar *)categories->data); } static void dates_utils_update_category_bar (DatesData *data, MokoSearchBar *bar) { gint cat_n; GList *categories; gchar *old_text; GtkTreeIter iter; GtkComboBox *combo = moko_search_bar_get_combo_box (bar); GtkTreeModel *model = gtk_combo_box_get_model (combo); old_text = gtk_combo_box_get_active_text (combo); while (gtk_tree_model_get_iter_first (model, &iter)) gtk_combo_box_remove_text (combo, 0); gtk_combo_box_append_text (combo, "All"); gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0); for (categories = data->category_list, cat_n = 1; categories; categories = categories->next, cat_n++) { gchar *category = (gchar *)categories->data; gtk_combo_box_append_text (combo, category); if (old_text && (strcmp (old_text, category) == 0)) gtk_combo_box_set_active (combo, cat_n); } g_free (old_text); } void dates_utils_update_category_bars (DatesData *data) { dates_utils_update_category_bar (data, MOKO_SEARCH_BAR (data->list_searchbar)); dates_utils_update_category_bar (data, MOKO_SEARCH_BAR (data->week_searchbar)); dates_utils_update_category_bar (data, MOKO_SEARCH_BAR (data->month_searchbar)); } GList * dates_utils_insert_preset_categories (GList *category_list) { gint i; /* Insert preset categories into category list */ for (i = 0; preset_categories[i]; i++) { if (!g_list_find_custom (category_list, preset_categories[i], (GCompareFunc)strcmp)) category_list = g_list_insert_sorted ( category_list, (gpointer)preset_categories[i], (GCompareFunc)g_utf8_collate); } return category_list; } void dates_utils_create_new_event (DatesData *data, JanaTime *time) { if (data->selected_store) { g_object_unref (data->selected_store); data->selected_store = NULL; } if (data->selected_event) g_object_unref (data->selected_event); data->selected_event = jana_ecal_event_new (); jana_event_set_summary (data->selected_event, "New event"); jana_event_set_start (data->selected_event, time); jana_time_set_hours (time, jana_time_get_hours (time) + 1); jana_event_set_end (data->selected_event, time); } gboolean dates_utils_event_store_filter_cb (GtkTreeModel *model, GtkTreeIter *iter, DatesData *data) { gint category; gchar *summary, *location, *description, **categories; gboolean result = FALSE; if ((!data->search) && (!data->category)) return TRUE; gtk_tree_model_get (model, iter, JANA_GTK_EVENT_STORE_COL_SUMMARY, &summary, JANA_GTK_EVENT_STORE_COL_LOCATION, &location, JANA_GTK_EVENT_STORE_COL_DESCRIPTION, &description, JANA_GTK_EVENT_STORE_COL_CATEGORIES, &categories, -1); summary = dates_utils_casefold_replace (summary); location = dates_utils_casefold_replace (location); description = dates_utils_casefold_replace (description); if (data->search) { if (summary && strstr (summary, data->search)) result = TRUE; else if (location && strstr (location, data->search)) result = TRUE; else if (description && strstr (description, data->search)) result = TRUE; else if (categories) for (category = 0; categories[category]; category++) { categories[category] = dates_utils_casefold_replace ( categories[category]); if (strstr (categories[category], data->search)) { result = TRUE; break; } } } else if (categories) { for (category = 0; categories[category]; category++) { if (strcmp (categories[category], data->category) == 0){ result = TRUE; break; } } } g_free (summary); g_free (location); g_free (description); g_strfreev (categories); return result; } gboolean dates_utils_event_list_filter_cb (GtkTreeModel *model, GtkTreeIter *iter, DatesData *data) { GtkTreeIter sub_iter; GtkTreePath *path; GtkTreeRowReference *row; if ((!data->search) && (!data->category)) return TRUE; gtk_tree_model_get (model, iter, JANA_GTK_EVENT_LIST_COL_ROW, &row, -1); /* Ignore headers */ if (!row) return TRUE; model = gtk_tree_row_reference_get_model (row); path = gtk_tree_row_reference_get_path (row); gtk_tree_model_get_iter (model, &sub_iter, path); gtk_tree_path_free (path); return dates_utils_event_store_filter_cb (model, &sub_iter, data); } void dates_utils_event_list_selection_changed_cb (GtkTreeSelection *selection, DatesData *data) { GtkTreeRowReference *row; GtkTreeModel *model; GtkTreePath *path; GtkTreeIter iter; gchar *uid; /* Unselect old selection */ if (data->selected_store) { g_object_unref (data->selected_store); data->selected_store = NULL; } if (data->selected_event) { g_object_unref (data->selected_event); data->selected_event = NULL; } /* Get new selection */ if ((!selection) || (gtk_tree_selection_count_selected_rows (selection) != 1)) return; gtk_tree_selection_get_selected (selection, &model, &iter); gtk_tree_model_get (model, &iter, JANA_GTK_EVENT_LIST_COL_ROW, &row, -1); if ((!row) || (!gtk_tree_row_reference_valid (row))) return; /* Get Jana Event/Store for new selection */ model = gtk_tree_row_reference_get_model (row); path = gtk_tree_row_reference_get_path (row); gtk_tree_model_get_iter (model, &iter, path); gtk_tree_path_free (path); data->selected_store = jana_gtk_event_store_get_store ( JANA_GTK_EVENT_STORE (model)); gtk_tree_model_get (model, &iter, JANA_GTK_EVENT_STORE_COL_UID, &uid, -1); data->selected_event = JANA_EVENT (jana_store_get_component ( data->selected_store, uid)); g_free (uid); } JanaTime * dates_utils_event_list_get_time (JanaGtkEventList *list) { JanaTime *time = NULL; GtkTreeSelection *selection; GtkTreeModel *model; GtkTreeIter iter; selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (list)); if (selection && (gtk_tree_selection_count_selected_rows (selection) == 1)) { GtkTreeRowReference *row; /* Try to get the time from a header */ gtk_tree_selection_get_selected (selection, &model, &iter); gtk_tree_model_get (model, &iter, JANA_GTK_EVENT_LIST_COL_ROW, &row, JANA_GTK_EVENT_LIST_COL_TIME, &time, -1); /* If it wasn't a header, try to get it from the event */ if ((!time) && row) { GtkTreePath *path; model = gtk_tree_row_reference_get_model (row); path = gtk_tree_row_reference_get_path (row); gtk_tree_model_get_iter (model, &iter, path); gtk_tree_path_free (path); gtk_tree_model_get (model, &iter, JANA_GTK_EVENT_STORE_COL_START, &time, -1); } } return time; } gboolean dates_utils_title_idle_cb (DatesData *data) { gtk_window_set_title (GTK_WINDOW (data->window), "Calendar"); return FALSE; }