~gotwig/gazette/global-service-states

« back to all changes in this revision

Viewing changes to plugins/Time/Time.vala

  • Committer: Santiago Ocamica
  • Date: 2013-05-06 00:39:31 UTC
  • Revision ID: santi6982@gmail.com-20130506003931-hh78n70xp8kgqyeq
Stylized time service a bit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
public class Time : Gazette.Plugin
2
2
{
3
3
        ShadowedLabel label;
4
 
    Settings settings;
5
4
    construct {
6
5
        id = "time";
7
6
        name = _("time");
8
7
    }
9
8
        public Time ()
10
9
        {
11
 
        settings = new Settings("org.pantheon.gazette.files");
 
10
            debug("Creating time");
 
11
        Timeout.add(60000, update);
 
12
        }
 
13
        public override void create () {
12
14
                label = new ShadowedLabel ("");
13
15
                service.add_content(label);
14
 
        Timeout.add(60000, update);
15
16
        }
16
 
        
17
17
        public override bool update () 
18
18
        {
 
19
            debug("Updating time");
19
20
                var date = new GLib.DateTime.now_local ();
20
 
                
21
 
                /*Date display, see http://unstable.valadoc.org/#!api=glib-2.0/GLib.DateTime.format for more details. 
22
 
                  %v is added here to provide the English date suffixes th, nd and so on*/
23
 
                var day_format = _("%A, %B %e%v");
24
 
                /*Time display, see http://unstable.valadoc.org/#!api=glib-2.0/GLib.DateTime.format for more details*/
25
 
                var time_format = _("%l:%M");
26
 
                /*AM/PM display, see http://unstable.valadoc.org/#!api=glib-2.0/GLib.DateTime.format for more details*/
27
 
                var meridiem_format = _(" %p");
28
 
                
29
 
                //there is no %v, but we need one, so we add one
30
 
                var num = date.get_day_of_month ();
31
 
                day_format = day_format.replace ("%v", get_english_number_suffix (num));
32
 
                
33
 
                service.clear_contents ();
34
 
                service.add_content(new ShadowedLabel(date.format (
35
 
                        "<span face='Open Sans Light' font='24'>"+
 
21
                var day_format = _("%A, %B %e");
 
22
                // TODO provide a localizable date suffix
 
23
                string tfmt = (new Settings("org.gnome.desktop.interface")). get_string ("clock-format");
 
24
                var time_format = _("<span weight='bold'>" + ((tfmt=="12h")?"%l":"%H") + ":</span><span weight='ultralight'>%M</span>");
 
25
                label.label = date.format (
 
26
                    "\n<span face='Raleway' font='80'>"+
 
27
                        time_format+
 
28
                        "</span>\n<span face='Open Sans Light' font='24'>"+
36
29
                        day_format+
37
 
                        "</span>\n<span face='Raleway' weight='100' font='72'>  "+
38
 
                        time_format+
39
 
                        "</span><span face='Raleway' weight='100' font='50'>"+
40
 
                        meridiem_format+
41
 
                        "</span>")));
 
30
                        "</span>");
42
31
                return true;
43
32
        }
44
 
        
45
 
        /**
46
 
         * Utility to get the English number suffix
47
 
         * @param number The number to find the the suffix for
48
 
         * @return The according English suffix
49
 
         **/
50
 
        string get_english_number_suffix (int number) {
51
 
                number %= 100;
52
 
                
53
 
                if (number > 20)
54
 
                        number %= 10;
55
 
                
56
 
                switch (number) {
57
 
                    case 1:
58
 
                            return "st";
59
 
                    case 2:
60
 
                            return "nd";
61
 
                    case 3:
62
 
                            return "rd";
63
 
                    case 11:
64
 
                    case 12:
65
 
                    case 13:
66
 
            default:
67
 
                return "th";
68
 
                }
69
 
        }
70
33
}
71
34
public Time plugin_init () {
72
35
    // types are registered automatically