~ubuntu-branches/ubuntu/oneiric/alarm-clock/oneiric

« back to all changes in this revision

Viewing changes to src/alarm_thread.c

  • Committer: Bazaar Package Importer
  • Author(s): Ryan Niebur
  • Date: 2009-09-06 12:14:17 UTC
  • mfrom: (1.1.11 upstream)
  • mto: (9.2.3 squeeze)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: james.westby@ubuntu.com-20090906121417-8er9o81ggu07jt3w
Tags: 1.2.3-2
* don't do 'make check', it doesn't work right with quilt patches
* add gtk-builder-proper-error-handling patch, used to debug LP 425163
* add fix-duplicate-id, which fixes loading on Ubuntu (LP: #425163)

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
}
71
71
 
72
72
gboolean
73
 
check_play_alarm(gchar *name, gchar *current_date)
74
 
{
75
 
        time_t rawtime;
76
 
        struct tm * timeinfo;
77
 
        time ( &rawtime );
78
 
        timeinfo = localtime ( &rawtime );
79
 
        
80
 
        gboolean ret = FALSE;
81
 
        gchar *date = g_key_file_get_string(loaded_alarms, name, "DateTime", NULL);
82
 
        
83
 
        if (g_strcmp0(date, current_date) == 0) ret = TRUE;
84
 
 
85
 
        g_free(date);
86
 
        return ret;
87
 
}
88
 
 
89
 
gboolean
90
73
is_date_excluded(gchar *alarm_name, gint day, gint month)
91
74
{
92
75
        gchar *buffer, **split, **date_split;
245
228
        }
246
229
        /* We have to compare which one is earlier */
247
230
 
 
231
        
248
232
        if (got_date) return g_time_val_to_iso8601(&smallest_date);
249
233
        else return g_time_val_to_iso8601(&smallest_weekday);
250
234
 
257
241
}
258
242
 
259
243
void
 
244
move_to_missed(gchar *name)
 
245
{
 
246
        gint key = 0;
 
247
        gchar *buffer, **keys;
 
248
        GKeyFile *temp_key = g_key_file_new();
 
249
        
 
250
        g_key_file_load_from_file(temp_key, config_missed, G_KEY_FILE_NONE, NULL);
 
251
        
 
252
        keys = g_key_file_get_keys(loaded_alarms, name, NULL, NULL);
 
253
        
 
254
        while (TRUE)
 
255
        {
 
256
                if (keys[key] == NULL) break;
 
257
                buffer = g_key_file_get_string(loaded_alarms, name, keys[key], NULL);
 
258
 
 
259
                g_key_file_set_string(temp_key, name, keys[key], buffer);
 
260
                
 
261
                key++;
 
262
                g_free(buffer);
 
263
        }
 
264
        
 
265
        buffer = g_key_file_to_data(temp_key, NULL, NULL);
 
266
        
 
267
        g_file_set_contents(config_missed, buffer, -1, NULL);
 
268
        
 
269
        g_key_file_free(temp_key);
 
270
        g_free(buffer);
 
271
        g_strfreev(keys);
 
272
        
 
273
}
 
274
 
 
275
void
 
276
manage_old_alarm(gchar *name)
 
277
{
 
278
        gchar *type = g_key_file_get_string(loaded_alarms, name, "AlarmType", NULL);
 
279
        gboolean snooze = g_key_file_get_boolean(loaded_alarms, name, "snoozed", NULL);
 
280
        gboolean single = TRUE;
 
281
 
 
282
        if (g_strcmp0(type, "Schedule") == 0) single = FALSE;
 
283
        if (snooze)  snooze = FALSE;
 
284
 
 
285
        g_free(type);
 
286
        
 
287
        if (need_check_first)
 
288
        {
 
289
                move_to_missed(name);
 
290
                if (single) 
 
291
                {
 
292
                        remove_alarm(name);
 
293
                        reload_alarms();
 
294
                        update_list_entries();
 
295
                }
 
296
        }
 
297
        else
 
298
        {
 
299
                if (single && !snooze && !running_snooze)
 
300
                {
 
301
                        g_usleep(G_USEC_PER_SEC);
 
302
                        remove_alarm(name);
 
303
                        reload_alarms();
 
304
                        update_list_entries();
 
305
                }
 
306
        }
 
307
}
 
308
 
 
309
 
 
310
void
260
311
update_alarm_thread(void)
261
312
{
262
313
        time_t rawtime;
265
316
        GTimeVal alarm_time, current_time;
266
317
        gchar *iso_date, *time_left, *alarm_type, **alarms;
267
318
        guint count;
268
 
        GString *date_string;
269
319
        GtkTreeIter iter;
270
320
        gchar *name;
271
321
        count = 0;
272
 
        date_string = g_string_new(NULL);
 
322
 
273
323
        time ( &rawtime );
274
324
        timeinfo = localtime ( &rawtime );
275
325
        gboolean snoozed;
276
326
 
277
 
        g_string_append(date_string, g_strdup_printf("%i-", timeinfo->tm_year + 1900));
278
 
        g_string_append(date_string, g_strdup_printf("%i-", timeinfo->tm_mon + 1));
279
 
        g_string_append(date_string, g_strdup_printf("%iT", timeinfo->tm_mday));
280
 
        g_string_append(date_string, g_strdup_printf("%i:", timeinfo->tm_hour));
281
 
        g_string_append(date_string, g_strdup_printf("%i:", timeinfo->tm_min));
282
 
        g_string_append(date_string, g_strdup_printf("%i", timeinfo->tm_sec));
283
 
                
 
327
 
284
328
        alarms = g_key_file_get_groups(loaded_alarms, NULL);
285
329
        
286
330
        while(TRUE)
325
369
                }
326
370
                // Tutaj formatujemy timeleft
327
371
 
328
 
                if (diff > 0)
 
372
                if (diff > 1)
329
373
                {
330
374
                        if (snoozed)
331
375
                        {
343
387
                        }
344
388
 
345
389
                }
346
 
                        
 
390
 
 
391
 
 
392
                if (diff == 1)
 
393
                {
 
394
                        gdk_threads_enter();
 
395
                        run_alarm(alarms[count]);
 
396
                        manage_old_alarm(alarms[count]);
 
397
                        update_list_entries();
 
398
                        gdk_threads_leave();
 
399
                }
 
400
 
 
401
                if (diff < 1)
 
402
                {
 
403
                        manage_old_alarm(alarms[count]);
 
404
                }
 
405
 
347
406
                count++;        
348
407
        }       
349
408
}
350
409
 
351
410
void
352
 
move_to_missed(gchar *name)
353
 
{
354
 
        gint key = 0;
355
 
        gchar *buffer, **keys;
356
 
        GKeyFile *temp_key = g_key_file_new();
357
 
        
358
 
        g_key_file_load_from_file(temp_key, config_missed, G_KEY_FILE_NONE, NULL);
359
 
        
360
 
        keys = g_key_file_get_keys(loaded_alarms, name, NULL, NULL);
361
 
        
362
 
        while (TRUE)
363
 
        {
364
 
                if (keys[key] == NULL) break;
365
 
                buffer = g_key_file_get_string(loaded_alarms, name, keys[key], NULL);
366
 
 
367
 
                g_key_file_set_string(temp_key, name, keys[key], buffer);
368
 
                
369
 
                key++;
370
 
                g_free(buffer);
371
 
        }
372
 
        
373
 
        buffer = g_key_file_to_data(temp_key, NULL, NULL);
374
 
        
375
 
        g_file_set_contents(config_missed, buffer, -1, NULL);
376
 
        
377
 
        g_key_file_free(temp_key);
378
 
        g_free(buffer);
379
 
        g_strfreev(keys);
380
 
        
381
 
}
382
 
 
383
 
void
384
 
manage_old_alarm(gchar *name)
385
 
{
386
 
        gchar *type = g_key_file_get_string(loaded_alarms, name, "AlarmType", NULL);
387
 
        gboolean snooze = g_key_file_get_boolean(loaded_alarms, name, "snoozed", NULL);
388
 
        gboolean single = TRUE;
389
 
 
390
 
        if (g_strcmp0(type, "Schedule") == 0) single = FALSE;
391
 
        if (snooze)  snooze = FALSE;
392
 
 
393
 
        g_free(type);
394
 
        
395
 
        if (need_check_first)
396
 
        {
397
 
                move_to_missed(name);
398
 
                if (single) 
399
 
                {
400
 
                        remove_alarm(name);
401
 
                        reload_alarms();
402
 
                        update_list_entries();
403
 
                }
404
 
        }
405
 
        else
406
 
        {
407
 
                if (single && !snooze && !running_snooze)
408
 
                {
409
 
                        remove_alarm(name);
410
 
                        reload_alarms();
411
 
                        update_list_entries();
412
 
                }
413
 
        }
414
 
}
415
 
 
416
 
 
417
 
void
418
411
process_alarm_thread(void)
419
412
{
420
413
        time_t rawtime;
423
416
        GTimeVal alarm_time, current_time;
424
417
        gchar *iso_date, **alarms;
425
418
 
 
419
        time ( &rawtime );
 
420
        timeinfo = localtime ( &rawtime );
 
421
        
426
422
        guint count;
427
 
        GString *date_string;
428
 
 
429
423
        count = 0;
430
 
        date_string = g_string_new(NULL);
431
 
        time ( &rawtime );
432
 
        timeinfo = localtime ( &rawtime );
433
 
        g_string_append(date_string, g_strdup_printf("%i-", timeinfo->tm_year + 1900));
434
 
        g_string_append(date_string, g_strdup_printf("%i-", timeinfo->tm_mon + 1));
435
 
        g_string_append(date_string, g_strdup_printf("%iT", timeinfo->tm_mday));
436
 
        g_string_append(date_string, g_strdup_printf("%i:", timeinfo->tm_hour));
437
 
        g_string_append(date_string, g_strdup_printf("%i:", timeinfo->tm_min));
438
 
        g_string_append(date_string, g_strdup_printf("%i", timeinfo->tm_sec));
 
424
 
 
425
        
439
426
 
440
427
        if (timeinfo->tm_hour == 0 && timeinfo->tm_min == 0 && timeinfo->tm_sec == 0)
441
428
        {
452
439
        {       
453
440
                if (alarms[count] == NULL) break;
454
441
                
455
 
                if (check_play_alarm(alarms[count], date_string->str))
456
 
                {
457
 
                        gdk_threads_enter();
458
 
                        run_alarm(alarms[count]);
459
 
                        gdk_threads_leave();
460
 
                }
461
 
                
462
442
                iso_date = g_key_file_get_string(loaded_alarms, alarms[count], "DateTime", NULL);
463
443
                g_time_val_from_iso8601(iso_date, &alarm_time);
464
444
                g_free(iso_date);
468
448
                g_free(iso_date);
469
449
                diff = alarm_time.tv_sec - current_time.tv_sec;
470
450
 
471
 
                if (diff < 0)
472
 
                {
473
 
                        manage_old_alarm(alarms[count]);
474
 
                }
475
 
 
476
451
                count++;        
477
452
        }
478
453
        update_alarm_thread();
479
 
        g_string_free(date_string, TRUE);
 
454
 
480
455
}
481
456
 
482
457
void