1 /* 2 * alarm.h -- Core alarm functionality 3 * 4 * Copyright (C) 2007-2008 Johannes H. Jensen <joh@pseudoberries.com> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 * 20 * Authors: 21 * Johannes H. Jensen <joh@pseudoberries.com> 22 */ 23 24 #ifndef ALARM_H_ 25 #define ALARM_H_ 26 27 #include <glib.h> 28 #include <glib-object.h> 29 #include <glib/gi18n.h> 30 #include <gconf/gconf-client.h> 31 32 #include "player.h" 33 34 G_BEGIN_DECLS 35 36 /* 37 * Utility macros 38 */ 39 40 #define TYPE_ALARM (alarm_get_type()) 41 42 #define ALARM(object) \ 43 (G_TYPE_CHECK_INSTANCE_CAST((object), TYPE_ALARM, Alarm)) 44 45 #define ALARM_CLASS(klass) \ 46 (G_TYPE_CHECK_CLASS_CAST((klass), TYPE_ALARM, AlarmClass)) 47 48 #define IS_ALARM(object) \ 49 (G_TYPE_CHECK_INSTANCE_TYPE((object), TYPE_ALARM)) 50 51 #define IS_ALARM_CLASS(klass) \ 52 (G_TYPE_CHECK_CLASS_TYPE((klass), TYPE_ALARM)) 53 54 #define ALARM_GET_CLASS(object) \ 55 (G_TYPE_INSTANCE_GET_CLASS((object), TYPE_ALARM, AlarmClass)) 56 57 /* 58 * Structure definitions 59 */ 60 61 typedef enum { 62 ALARM_TYPE_INVALID = 0, 63 ALARM_TYPE_CLOCK, /* Alarm at specific time */ 64 ALARM_TYPE_TIMER /* Alarm in X mins */ 65 } AlarmType; 66 67 typedef enum { 68 ALARM_REPEAT_NONE = 0, 69 ALARM_REPEAT_SUN = 1 << 0, 70 ALARM_REPEAT_MON = 1 << 1, 71 ALARM_REPEAT_TUE = 1 << 2, 72 ALARM_REPEAT_WED = 1 << 3, 73 ALARM_REPEAT_THU = 1 << 4, 74 ALARM_REPEAT_FRI = 1 << 5, 75 ALARM_REPEAT_SAT = 1 << 6, 76 } AlarmRepeat; 77 78 #define ALARM_REPEAT_WEEKDAYS (ALARM_REPEAT_MON | ALARM_REPEAT_TUE | ALARM_REPEAT_WED | ALARM_REPEAT_THU | ALARM_REPEAT_FRI) 79 #define ALARM_REPEAT_WEEKENDS (ALARM_REPEAT_SAT | ALARM_REPEAT_SUN) 80 #define ALARM_REPEAT_ALL (ALARM_REPEAT_WEEKDAYS | ALARM_REPEAT_WEEKENDS) 81 82 typedef enum { 83 ALARM_NOTIFY_INVALID = 0, 84 ALARM_NOTIFY_SOUND, /* Notification by sound */ 85 ALARM_NOTIFY_COMMAND, /* Notification by command */ 86 } AlarmNotifyType; 87 88 typedef struct _Alarm Alarm; 89 typedef struct _AlarmClass AlarmClass; 90 91 struct _Alarm { 92 GObject parent; 93 94 gchar *gconf_dir; /* GConf directory */ 95 gint id; /* Alarm ID */ 96 97 gboolean triggered; // Whether the alarm has been triggered 98 99 /* GConf mapped values */ 100 AlarmType type; 101 time_t time; /* Time for alarm */ 102 time_t timestamp; /* UNIX timestamp (local time) for running alarms */ 103 gboolean active; 104 gchar *message; 105 AlarmRepeat repeat; 106 107 AlarmNotifyType notify_type; 108 gchar *sound_file; 109 gboolean sound_loop; 110 gchar *command; 111 }; 112 113 struct _AlarmClass { 114 GObjectClass parent; 115 116 /* Signals */ 117 void (*alarm)(Alarm *alarm); // Alarm triggered! 118 void (*cleared)(Alarm *alarm); // Alarm cleared 119 void (*error)(Alarm *alarm, GError *err); // An error occured 120 void (*player_changed)(Alarm *alarm, MediaPlayerState state); /* Media player state changed */ 121 }; 122 123 /* 124 * Error codes 125 */ 126 #define ALARM_ERROR alarm_error_quark () 127 128 typedef enum { 129 ALARM_ERROR_NONE, 130 ALARM_ERROR_PLAY, /* Error playing sound */ 131 ALARM_ERROR_COMMAND /* Error launching command */ 132 } AlarmErrorCode; 133 134 135 /* 136 * Failsafe defaults for the GConf-mapped properties for 137 * use when the schema isn't found or doesn't provide 138 * sensible defaults. 139 */ 140 #define ALARM_DEFAULT_TYPE ALARM_TYPE_CLOCK 141 #define ALARM_DEFAULT_TIME 0 142 #define ALARM_DEFAULT_TIMESTAMP 0 143 #define ALARM_DEFAULT_ACTIVE FALSE 144 #define ALARM_DEFAULT_MESSAGE "Alarm!" 145 #define ALARM_DEFAULT_REPEAT ALARM_REPEAT_NONE 146 #define ALARM_DEFAULT_NOTIFY_TYPE ALARM_NOTIFY_SOUND 147 #define ALARM_DEFAULT_SOUND_FILE "" // Should default to first in stock sound list 148 #define ALARM_DEFAULT_SOUND_LOOP TRUE 149 #define ALARM_DEFAULT_COMMAND "" // Should default to first in app list 150 151 /* 152 * GConf settings 153 */ 154 #define ALARM_GCONF_DIR_PREFIX "alarm" 155 #define ALARM_GCONF_SCHEMA_DIR "/schemas/apps/alarm-clock/alarm" 156 157 /* 158 * Player backoff timeout. 159 * We will stop the player automatically after 20 minutes. 160 */ 161 #define ALARM_SOUND_TIMEOUT (60 * 20) 162 163 /* 164 * Function prototypes. 165 */ 166 167 /* used by ALARM_TYPE */ 168 GType 169 alarm_get_type (void); 170 171 Alarm * 172 alarm_new (const gchar *gconf_dir, gint id); 173 174 guint 175 alarm_gen_id_dir (const gchar *gconf_dir); 176 177 guint 178 alarm_gen_id (Alarm *alarm); 179 180 gchar * 181 alarm_gconf_get_dir (Alarm *alarm); 182 183 gint 184 alarm_gconf_dir_get_id (const gchar *dir); 185 186 gchar * 187 alarm_gconf_get_full_key (Alarm *alarm, const gchar *key); 188 189 190 const gchar * 191 alarm_type_to_string (AlarmType type); 192 193 AlarmType 194 alarm_type_from_string (const gchar *type); 195 196 197 const gchar * 198 alarm_notify_type_to_string (AlarmNotifyType type); 199 200 AlarmNotifyType 201 alarm_notify_type_from_string (const gchar *type); 202 203 GList * 204 alarm_get_list (const gchar *gconf_dir); 205 206 void 207 alarm_signal_connect_list (GList *instances, 208 const gchar *detailed_signal, 209 GCallback c_handler, 210 gpointer data); 211 212 void 213 alarm_trigger (Alarm *alarm); 214 215 void 216 alarm_set_enabled (Alarm *alarm, gboolean enabled); 217 218 void 219 alarm_enable (Alarm *alarm); 220 221 void 222 alarm_clear (Alarm *alarm); 223 224 void 225 alarm_disable (Alarm *alarm); 226 227 void 228 alarm_delete (Alarm *alarm); 229 230 void 231 alarm_snooze (Alarm *alarm, guint seconds); 232 233 gboolean 234 alarm_is_playing (Alarm *alarm); 235 236 void 237 alarm_set_time (Alarm *alarm, guint hour, guint minute, guint second); 238 239 void 240 alarm_update_timestamp (Alarm *alarm); 241 242 void 243 alarm_update_timestamp_full (Alarm *alarm, gboolean include_today); 244 245 GQuark 246 alarm_error_quark (void); 247 248 void 249 alarm_error_trigger (Alarm *alarm, AlarmErrorCode code, const gchar *msg); 250 251 struct tm * 252 alarm_get_time (Alarm *alarm); 253 254 struct tm * 255 alarm_get_remain (Alarm *alarm); 256 257 time_t 258 alarm_get_remain_seconds (Alarm *alarm); 259 260 const gchar *alarm_repeat_to_string (AlarmRepeat repeat); 261 AlarmRepeat alarm_repeat_from_string (const gchar *str); 262 AlarmRepeat alarm_repeat_from_list (GSList *list); 263 GSList *alarm_repeat_to_list (AlarmRepeat repeat); 264 gint alarm_wday_distance (gint wday1, gint wday2); 265 gboolean alarm_should_repeat (Alarm *alarm); 266 gchar *alarm_repeat_to_pretty (AlarmRepeat repeat); 267 268 269 G_END_DECLS 270 271 #endif /*ALARM_H_*/ 272