/* diversity-jana.c - DiversityJana * * Copyright 2008 OpenMoko, Inc. * Authored by Chia-I Wu * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include #include #include "ebook-marshal.h" enum { TAG_ADDED, LAST_SIGNAL }; #define DIVERSITY_JANA_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), DIVERSITY_TYPE_JANA, DiversityJanaPrivate)) typedef struct _DiversityJanaPrivate DiversityJanaPrivate; struct _DiversityJanaPrivate { JanaStore *notes; gboolean opened; }; static guint jana_signals[LAST_SIGNAL] = { 0 }; G_DEFINE_TYPE(DiversityJana, diversity_jana, G_TYPE_OBJECT); static void diversity_jana_dispose(GObject *obj) { DiversityJanaPrivate *priv = DIVERSITY_JANA_GET_PRIVATE(obj); if (priv->notes) { g_object_unref(priv->notes); priv->notes = NULL; } priv->opened = FALSE; ((GObjectClass *) diversity_jana_parent_class)->dispose(obj); } static void diversity_jana_class_init(DiversityJanaClass *klass) { GObjectClass *o_class = (GObjectClass *) klass; o_class->dispose = diversity_jana_dispose; g_type_class_add_private(klass, sizeof(DiversityJanaPrivate)); jana_signals[TAG_ADDED] = g_signal_new("tag-added", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET(DiversityJanaClass, tag_added), NULL, NULL, ebook_marshal_VOID__STRING_DOUBLE_DOUBLE_STRING, G_TYPE_NONE, 4, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE); } #define L6N_MIN_LEN (3 + 6) static gboolean _jana_note_parse_l6n(JanaNote *note, gdouble *lon, gdouble *lat, gchar **desc) { gchar *body; gchar *p, *endp; body = jana_note_get_body(note); if (!body || strlen(body) < L6N_MIN_LEN) goto fail; p = body; if (strncmp(p, "l6n(", 4) != 0) goto fail; p += 4; errno = 0; *lon = g_ascii_strtod(p, &endp); if (!endp || errno || *lon < -180.0 || *lon >= 180.0) goto fail; p = endp; if (*p != ',') goto fail; p++; *lat = g_ascii_strtod(p, &endp); if (!endp || errno || *lat < -90.0 || *lat >= 90.0) goto fail; p = endp; if (strncmp(p, ")\n", 2) != 0) goto fail; p += 2; *desc = g_strdup(p); g_free(body); return TRUE; fail: g_free(body); return FALSE; } static void on_note_added(JanaStoreView *store_view, GList *components, DiversityJana *jana) { for (; components; components = components->next) { JanaNote *note; gdouble lon, lat; gchar *uid, *desc; if (!JANA_IS_NOTE(components->data)) continue; if (jana_utils_component_has_category(components->data, "Sent") || jana_utils_component_has_category(components->data, "Sending")) continue; note = JANA_NOTE(components->data); if (_jana_note_parse_l6n(note, &lon, &lat, &desc)) { uid = jana_component_get_uid((JanaComponent *) note); g_signal_emit(jana, jana_signals[TAG_ADDED], 0, uid, lon, lat, desc); g_free(uid); g_free(desc); } } } static void on_note_modified(JanaStoreView *store_view, GList *components, DiversityJana *jana) { on_note_added(store_view, components, jana); } static void diversity_jana_init(DiversityJana *jana) { DiversityJanaPrivate *priv = DIVERSITY_JANA_GET_PRIVATE(jana); priv->notes = jana_ecal_store_new(JANA_COMPONENT_NOTE); priv->opened = FALSE; } static void on_store_opened(JanaStore *store, DiversityJana *jana) { DiversityJanaPrivate *priv = DIVERSITY_JANA_GET_PRIVATE(jana); JanaStoreView *store_view; priv->opened = TRUE; store_view = jana_store_get_view(store); g_signal_connect(store_view, "added", G_CALLBACK(on_note_added), jana); g_signal_connect(store_view, "modified", G_CALLBACK(on_note_modified), jana); jana_store_view_start(store_view); } void diversity_jana_open_and_start(DiversityJana *jana) { DiversityJanaPrivate *priv = DIVERSITY_JANA_GET_PRIVATE(jana); jana_store_open(priv->notes); g_signal_connect(priv->notes, "opened", G_CALLBACK(on_store_opened), jana); }