/** * This file is part of maemo-examples package * * This maemo code example is licensed under a MIT-style license, * that can be found in the file called "COPYING" in the same * directory as this file. * Copyright (c) 2007-2008 Nokia Corporation. All rights reserved. */ #include #include #include #include #include #include #include "example_common.h" /* Define structure app-wide data*/ typedef struct { HildonProgram *program; HildonWindow *window; DBusGConnection *bus; } AppData; /* Define new GObject structure * HelloAlarm */ typedef struct { GObject parent; /* Define a pointer to store * appdata */ AppData *appdata; } HelloAlarm; /* Define HelloAlarm's class */ typedef struct { GObjectClass parent; /* HelloAlarm's say_hello-method. * This will be registered as a DBUS-method */ void (*say_hello)(HelloAlarm *self); } HelloAlarmClass; /* Callback to be called when application recieves * alarm message from the alarmd */ void hello_alarm_say_hello(HelloAlarm *self) { GtkWidget *dialog; dialog = gtk_message_dialog_new( GTK_WINDOW(self->appdata->window), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Hello Alarm!"); gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(dialog); } /* This file is generated from example_alarm_dbus.xml-file * using dbus-binding-tool and is needed to get the object's * DBUS export info (dbus_glib_hello_alarm_object_info). * See the Makefile for how the tool is used */ #include "example_alarm_dbus.h" /* Initialize class data for HelloAlarm */ static void hello_alarm_class_init(HelloAlarmClass *klass) { /* Define implementation location of the * say_hello-method */ klass->say_hello = hello_alarm_say_hello; } /* Get GType of the HelloAlarm object */ GType hello_alarm_get_type() { static GType type = 0; /* If the type is already registered, * return it, otherwise register the * GTypeInfoStructure */ if(!type) { static const GTypeInfo info = { sizeof(HelloAlarmClass), NULL, /* base_init */ NULL, /* base_finalize */ (GClassInitFunc)hello_alarm_class_init, /* class_init */ NULL, /* class_finalize */ NULL, /* class_data */ sizeof(HelloAlarm), 0, /* n_preallocs */ NULL, /* instance_init */ }; type = g_type_register_static(G_TYPE_OBJECT, "HelloAlarmType", &info, 0); } return type; } /* Tell alarmd to send a alarm */ static void set_alarm(GtkWidget *button, AppData *appdata) { alarm_event_t *event; time_t current_time; struct tm *alarm_time; alarm_action_t *action; /* Create new alarm event */ event = alarm_event_create(); /* Set name of the application this alarm belongs to.*/ alarm_event_set_alarm_appid(event, "hello_alarm"); /* Get current time and add two seconds to it, * meaning that alarm should be sent at * this time */ time(¤t_time); alarm_time = localtime(¤t_time); alarm_time->tm_min += 1; /* Set the alarm time */ event->alarm_tm = *alarm_time; /* NOTE: this way of setting alarm time triggers the alarm at the start of the next full minute from alarm_tm. Second precision can be achieved by using alarm_time: event->alarm_time = time(NULL) + 2; alarm_tm field should not be set simultaneously with alarm_time. Also, alarm_time method does not take timezone changes into account. */ /* Create action_t, set interface, service name, path and method * name of the HelloAlarm D-BUS interface */ action = alarm_action_create(); action = alarm_event_add_actions(event, 1); alarm_action_set_dbus_interface(action, "org.maemo.alarm_example"); alarm_action_set_dbus_service(action, "org.maemo.alarm_example"); alarm_action_set_dbus_path(action, "/org/maemo/alarm_example"); alarm_action_set_dbus_name(action, "hello_alarm"); /* The application presents its own dialog, * so the system provided isn't needed */ action->flags = ALARM_ACTION_WHEN_TRIGGERED; action->flags |= ALARM_ACTION_TYPE_DBUS; printf("Sane: %i\n", alarm_event_is_sane(event)); /* Add the alarm to alarmd and * verify that it was correctly created */ if(!alarmd_event_add(event)) { hildon_banner_show_information(button, "gtk-dialog-error", "Couldn't set alarm"); g_warning("Couldn't set alarm."); return; } hildon_banner_show_information(button, NULL, "Alarm set"); } /* Register GObject's D-BUS interface and * request a name for the service */ static gboolean register_service(DBusGConnection *bus, GObject *obj, const DBusGObjectInfo *info, gchar *name, gchar *path, GError **error) { DBusGProxy *system; gboolean result; /* Install GObject's info so glib knows * what kind of interface to present on * D-BUS */ dbus_g_object_type_install_info(G_TYPE_FROM_INSTANCE(obj), info); /* Register the object to D-BUS using * given object path */ dbus_g_connection_register_g_object(bus, path, obj); /* Get proxy D-BUS-service */ system = dbus_g_proxy_new_for_name(bus, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS); /* Request a name for the service from D-BUS */ result = org_freedesktop_DBus_request_name(system, name, 0, NULL, error); /* Check that the name request went OK */ if(!result) { g_object_unref(system); return FALSE; } g_object_unref(system); return TRUE; } int main(int argc, char **argv) { AppData appdata; GError *error = NULL; GtkWidget *button; HelloAlarm *server; /* Initialize the program */ example_gui_initialize(&appdata.program, &appdata.window, &argc, &argv, "alarm-example"); /* Connect to D-BUS session bus and verify that * it succeeded */ appdata.bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error); if(!appdata.bus) { g_error("Couldn't get DBUS connection: %s\n", error->message); g_error_free(error); return 1; } /* Create and initialize a HelloAlarm object and * register it to D-BUS */ server = g_object_new(hello_alarm_get_type(), NULL); server->appdata = &appdata; register_service(appdata.bus, G_OBJECT(server), &dbus_glib_hello_alarm_object_info, "org.maemo.alarm_example", "/org/maemo/alarm_example", &error); /* Check that the registration was successfull */ if(error) { g_error("Couldn't register service: %s", error->message); g_error_free(error); return 1; } /* Create a button and connect its clicked-event * to set_alarm-function */ button = gtk_button_new_with_label("Set alarm"); gtk_container_add(GTK_CONTAINER(appdata.window), button); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(set_alarm), &appdata); /* Run the mainloop */ example_gui_run(appdata.program, appdata.window); /* Disconnect from the dbus */ dbus_g_connection_unref(appdata.bus); return 0; }