~ubuntu-branches/ubuntu/wily/alarm-clock-applet/wily

« back to all changes in this revision

Viewing changes to src/util.c

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2009-05-30 23:24:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090530232427-88on1j2ily4ajxdz
Tags: upstream-0.2.6
ImportĀ upstreamĀ versionĀ 0.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * util.c -- Misc utilities
 
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
#include <string.h>
 
25
#include <time.h>
 
26
#include <glib.h>
 
27
#include <glib-object.h>
 
28
#include "util.h"
 
29
 
 
30
/**
 
31
 * Calculates the alarm timestamp given hour, min and secs.
 
32
 */
 
33
time_t
 
34
get_alarm_timestamp (guint hour, guint minute, guint second)
 
35
{
 
36
        time_t now;
 
37
        struct tm *tm;
 
38
        
 
39
        time (&now);
 
40
        tm = localtime (&now);
 
41
        
 
42
        // Check if the alarm is for tomorrow
 
43
        if (hour < tm->tm_hour ||
 
44
                (hour == tm->tm_hour && minute < tm->tm_min) ||
 
45
                (hour == tm->tm_hour && minute == tm->tm_min && second < tm->tm_sec)) {
 
46
                
 
47
                g_debug("Alarm is for tomorrow.");
 
48
                tm->tm_mday++;
 
49
        }
 
50
        
 
51
        tm->tm_hour = hour;
 
52
        tm->tm_min = minute;
 
53
        tm->tm_sec = second;
 
54
        
 
55
        // DEBUG:
 
56
        char tmp[512];
 
57
        strftime (tmp, sizeof (tmp), "%c", tm);
 
58
        g_debug ("Alarm will trigger at %s", tmp);
 
59
        
 
60
        return mktime (tm);
 
61
}
 
62
 
 
63
/**
 
64
 * Construct a Uppercased name of filename without the extension.
 
65
 */
 
66
gchar *
 
67
to_basename (const gchar *filename)
 
68
{
 
69
        gint i, len;
 
70
        gchar *result;
 
71
        
 
72
        len = strlen (filename);
 
73
        // Remove extension
 
74
        for (i = len-1; i > 0; i--) {
 
75
                if (filename[i] == '.') {
 
76
                        break;
 
77
                }
 
78
        }
 
79
        
 
80
        if (i == 0)
 
81
                // Extension not found
 
82
                i = len;
 
83
        
 
84
        result = g_strndup (filename, i);
 
85
        
 
86
        // Make first character Uppercase
 
87
        result[0] = g_utf8_strup (result, 1)[0];
 
88
        
 
89
        return result;
 
90
}
 
91
 
 
92
/*
 
93
 * Run Command
 
94
 */
 
95
gboolean
 
96
command_run (const gchar *command) {
 
97
        GError *err = NULL;
 
98
        
 
99
        if (!g_spawn_command_line_async (command, &err)) {
 
100
                g_critical ("Could not launch `%s': %s", command, err->message);
 
101
                g_error_free (err);
 
102
                
 
103
                return FALSE;
 
104
        }
 
105
        
 
106
        return TRUE;
 
107
}
 
108
 
 
109
gboolean
 
110
is_executable_valid (gchar *executable)
 
111
{
 
112
    gchar *path;
 
113
 
 
114
    path = g_find_program_in_path (executable);
 
115
 
 
116
    if (path) {
 
117
                g_free (path);
 
118
                return TRUE;
 
119
    }
 
120
 
 
121
    return FALSE;
 
122
}
 
123