Commit b298bd84 authored by Dylan McCall's avatar Dylan McCall

Changed to a more conventional coding style.

We're using the coding style seen with Vala examples and documentation, which is effectively the GTK+ coding style with less flair.
parent ef22b90d
......@@ -22,16 +22,16 @@ public const string HELPER_BREAK_OBJECT_BASE_PATH = "/org/gnome/BreakTimer/";
[DBus (name = "org.gnome.BreakTimer")]
public interface IBreakHelper : Object {
/** Returns the ID of the break that is currently focused and activated, if any. */
public abstract string? get_current_active_break() throws IOError;
public abstract string? get_current_active_break () throws IOError;
/** Returns a list of breaks that are currently known to the break helper. */
public abstract string[] get_break_ids() throws IOError;
public abstract string[] get_break_ids () throws IOError;
/** Returns a list of helpful status messages for each break, for debugging. */
public abstract string[] get_status_messages() throws IOError;
public abstract string[] get_status_messages () throws IOError;
/** Activate the specified break immediately, regardless of the usual activation conditions. */
public abstract void activate_break(string break_id) throws IOError;
public abstract void activate_break (string break_id) throws IOError;
// TODO: It might make sense to communicate when the active break changes,
// using a signal. The only reason we don't at the moment is it adds
......@@ -42,10 +42,10 @@ public interface IBreakHelper : Object {
[DBus (name = "org.gnome.BreakTimer.TimerBreak")]
public interface IBreakHelper_TimerBreak : Object {
/** Get the break's current status, such as time remaining, or time until the break starts */
public abstract TimerBreakStatus get_status() throws IOError;
public abstract TimerBreakStatus get_status () throws IOError;
/** Activate the break */
public abstract void activate() throws IOError;
public abstract void activate () throws IOError;
}
public struct BreakStatus {
......@@ -61,4 +61,4 @@ public struct TimerBreakStatus {
int starts_in;
int time_remaining;
int current_duration;
}
}
\ No newline at end of file
......@@ -16,11 +16,11 @@
*/
public interface ISessionStatus : Object {
public signal void locked();
public signal void unlocked();
public signal void locked ();
public signal void unlocked ();
public abstract bool is_locked();
public abstract void lock_screen();
public abstract void blank_screen();
public abstract void unblank_screen();
public abstract bool is_locked ();
public abstract void lock_screen ();
public abstract void blank_screen ();
public abstract void unblank_screen ();
}
\ No newline at end of file
......@@ -30,41 +30,41 @@ public class NaturalTime : Object {
this.seconds = seconds;
try {
this.re_single = new Regex(label_single.replace("%d", "(\\d+)"));
this.re_single = new Regex (label_single.replace ("%d", "(\\d+)"));
} catch (RegexError error) {
GLib.warning("Error compiling regex for TimeUnit: %s", error.message);
GLib.warning ("Error compiling regex for TimeUnit: %s", error.message);
}
try {
this.re_plural = new Regex(label_plural.replace("%d", "(\\d+)"));
this.re_plural = new Regex (label_plural.replace ("%d", "(\\d+)"));
} catch (RegexError error) {
GLib.warning("Error compiling regex for TimeUnit: %s", error.message);
GLib.warning ("Error compiling regex for TimeUnit: %s", error.message);
}
}
public string format_seconds (int seconds) {
int time = seconds / this.seconds;
return ngettext(this.label_single.printf(time),
this.label_plural.printf(time),
return ngettext (this.label_single.printf (time),
this.label_plural.printf (time),
time);
}
public MatchInfo? matches_for_input (string input) {
MatchInfo? match_info = null;
bool single_matched = this.re_single.match(input, RegexMatchFlags.ANCHORED, out match_info);
if (! single_matched) this.re_plural.match(input, RegexMatchFlags.ANCHORED, out match_info);
bool single_matched = this.re_single.match (input, RegexMatchFlags.ANCHORED, out match_info);
if (! single_matched) this.re_plural.match (input, RegexMatchFlags.ANCHORED, out match_info);
return match_info;
}
}
private TimeUnit[] units {get; private set;}
private TimeUnit[] units { get; private set; }
private NaturalTime () {
this.units = {
TimeUnit(_("%d second"), _("%d seconds"), 1),
TimeUnit(_("%d minute"), _("%d minutes"), 60),
TimeUnit(_("%d hour"), _("%d hours"), 3600)
TimeUnit (_ ("%d second"), _ ("%d seconds"), 1),
TimeUnit (_ ("%d minute"), _ ("%d minutes"), 60),
TimeUnit (_ ("%d hour"), _ ("%d hours"), 3600)
};
}
......@@ -72,7 +72,7 @@ public class NaturalTime : Object {
public static NaturalTime instance {
get {
if (_instance == null) {
_instance = new NaturalTime();
_instance = new NaturalTime ();
}
return _instance;
}
......@@ -86,10 +86,10 @@ public class NaturalTime : Object {
* @return a list of strings representing the same time in different units.
*/
public string[] get_completions_for_input (string input) {
int time = get_time_for_input(input);
int time = get_time_for_input (input);
if (time < 1) time = 1;
return get_completions_for_time(time);
return get_completions_for_time (time);
}
/**
......@@ -106,8 +106,8 @@ public class NaturalTime : Object {
for (int i = 0; i < units.length; i++) {
TimeUnit unit = units[i];
completions[i] = ngettext(unit.label_single.printf(time),
unit.label_plural.printf(time),
completions[i] = ngettext (unit.label_single.printf (time),
unit.label_plural.printf (time),
time);
}
......@@ -132,7 +132,7 @@ public class NaturalTime : Object {
if (seconds == 0) break;
}
}
return label_unit.format_seconds(seconds);
return label_unit.format_seconds (seconds);
}
/**
......@@ -151,10 +151,10 @@ public class NaturalTime : Object {
label_unit = unit;
}
}
return label_unit.format_seconds(seconds);
return label_unit.format_seconds (seconds);
}
private int soften_seconds_for_countdown(int seconds) {
private int soften_seconds_for_countdown (int seconds) {
int interval = 1;
if (seconds <= 10) {
interval = 1;
......@@ -163,7 +163,7 @@ public class NaturalTime : Object {
} else {
interval = 60;
}
int time_softened = ((seconds-1) / interval) + 1;
int time_softened = ( (seconds-1) / interval) + 1;
return time_softened * interval;
}
......@@ -176,8 +176,8 @@ public class NaturalTime : Object {
* @return a string representing the time remaining.
*/
public string get_countdown_for_seconds (int seconds) {
int seconds_softened = soften_seconds_for_countdown(seconds);
return get_simplest_label_for_seconds(seconds_softened);
int seconds_softened = soften_seconds_for_countdown (seconds);
return get_simplest_label_for_seconds (seconds_softened);
}
/**
......@@ -192,9 +192,9 @@ public class NaturalTime : Object {
* @return a string representing the time remaining.
*/
public string get_countdown_for_seconds_with_start (int seconds, int start) {
int seconds_softened = soften_seconds_for_countdown(seconds);
int seconds_softened = soften_seconds_for_countdown (seconds);
if (seconds_softened > start) seconds_softened = start;
return get_simplest_label_for_seconds(seconds_softened);
return get_simplest_label_for_seconds (seconds_softened);
}
private bool get_unit_for_input (string input, out TimeUnit? out_unit, out int out_time) {
......@@ -202,10 +202,10 @@ public class NaturalTime : Object {
out_time = -1;
foreach (TimeUnit unit in units) {
MatchInfo? match_info = unit.matches_for_input(input);
if (match_info != null && match_info.matches()) {
string time_str = match_info.fetch(1);
out_time = int.parse(time_str);
MatchInfo? match_info = unit.matches_for_input (input);
if (match_info != null && match_info.matches ()) {
string time_str = match_info.fetch (1);
out_time = int.parse (time_str);
out_unit = unit;
return true;
}
......@@ -218,16 +218,16 @@ public class NaturalTime : Object {
Regex time_re;
try {
// this assumes \\d+ will _only_ match the time
time_re = new Regex("(\\d+)");
time_re = new Regex ("(\\d+)");
} catch (RegexError error) {
GLib.warning("Error compiling regex: %s", error.message);
GLib.warning ("Error compiling regex: %s", error.message);
return -1;
}
MatchInfo match_info;
if (time_re.match(input, 0, out match_info)) {
string time_str = match_info.fetch(1);
int time = int.parse(time_str);
if (time_re.match (input, 0, out match_info)) {
string time_str = match_info.fetch (1);
int time = int.parse (time_str);
return time;
} else {
return -1;
......@@ -244,11 +244,10 @@ public class NaturalTime : Object {
public int get_seconds_for_input (string input) {
TimeUnit? unit;
int time;
if (get_unit_for_input(input, out unit, out time)) {
if (get_unit_for_input (input, out unit, out time)) {
return time * unit.seconds;
} else {
return -1;
}
}
}
}
\ No newline at end of file
......@@ -22,78 +22,78 @@ public class BreakManager : Object {
private BreakHelperServer break_helper_server;
private Settings settings;
public bool master_enabled {get; set;}
public string[] selected_break_ids {get; set;}
public bool master_enabled { get; set; }
public string[] selected_break_ids { get; set; }
public BreakManager(UIManager ui_manager) {
public BreakManager (UIManager ui_manager) {
this.ui_manager = ui_manager;
this.breaks = new Gee.HashMap<string, BreakType>();
this.settings = new Settings("org.gnome.break-timer");
this.breaks = new Gee.HashMap<string, BreakType> ();
this.settings = new Settings ("org.gnome.break-timer");
this.settings.bind("enabled", this, "master-enabled", SettingsBindFlags.DEFAULT);
this.settings.bind("selected-breaks", this, "selected-break-ids", SettingsBindFlags.DEFAULT);
this.notify["master-enabled"].connect(this.update_enabled_breaks);
this.notify["selected-break-ids"].connect(this.update_enabled_breaks);
this.settings.bind ("enabled", this, "master-enabled", SettingsBindFlags.DEFAULT);
this.settings.bind ("selected-breaks", this, "selected-break-ids", SettingsBindFlags.DEFAULT);
this.notify["master-enabled"].connect (this.update_enabled_breaks);
this.notify["selected-break-ids"].connect (this.update_enabled_breaks);
this.break_helper_server = new BreakHelperServer(this);
this.break_helper_server = new BreakHelperServer (this);
try {
DBusConnection connection = Bus.get_sync(BusType.SESSION, null);
connection.register_object(
DBusConnection connection = Bus.get_sync (BusType.SESSION, null);
connection.register_object (
HELPER_OBJECT_PATH,
this.break_helper_server
);
} catch (IOError error) {
GLib.error("Error registering helper on the session bus: %s", error.message);
GLib.error ("Error registering helper on the session bus: %s", error.message);
}
}
public Json.Object serialize() {
Json.Object json_root = new Json.Object();
foreach (BreakType break_type in this.all_breaks()) {
Json.Object break_json = break_type.break_controller.serialize();
json_root.set_object_member(break_type.id, break_json);
public Json.Object serialize () {
Json.Object json_root = new Json.Object ();
foreach (BreakType break_type in this.all_breaks ()) {
Json.Object break_json = break_type.break_controller.serialize ();
json_root.set_object_member (break_type.id, break_json);
}
return json_root;
}
public void deserialize(ref Json.Object json_root) {
foreach (BreakType break_type in this.all_breaks()) {
Json.Object break_json = json_root.get_object_member(break_type.id);
public void deserialize (ref Json.Object json_root) {
foreach (BreakType break_type in this.all_breaks ()) {
Json.Object break_json = json_root.get_object_member (break_type.id);
if (break_json != null) {
break_type.break_controller.deserialize(ref break_json);
break_type.break_controller.deserialize (ref break_json);
}
}
}
public void load_breaks(ActivityMonitor activity_monitor) {
this.add_break(new MicroBreakType(activity_monitor));
this.add_break(new RestBreakType(activity_monitor));
public void load_breaks (ActivityMonitor activity_monitor) {
this.add_break (new MicroBreakType (activity_monitor));
this.add_break (new RestBreakType (activity_monitor));
this.update_enabled_breaks();
this.update_enabled_breaks ();
}
public Gee.Set<string> all_break_ids() {
public Gee.Set<string> all_break_ids () {
return this.breaks.keys;
}
public Gee.Collection<BreakType> all_breaks() {
public Gee.Collection<BreakType> all_breaks () {
return this.breaks.values;
}
public BreakType? get_break_type_for_name(string name) {
return this.breaks.get(name);
public BreakType? get_break_type_for_name (string name) {
return this.breaks.get (name);
}
private void add_break(BreakType break_type) {
this.breaks.set(break_type.id, break_type);
break_type.initialize(this.ui_manager);
private void add_break (BreakType break_type) {
this.breaks.set (break_type.id, break_type);
break_type.initialize (this.ui_manager);
}
private void update_enabled_breaks() {
foreach (BreakType break_type in this.all_breaks()) {
private void update_enabled_breaks () {
foreach (BreakType break_type in this.all_breaks ()) {
bool is_enabled = this.master_enabled && break_type.id in this.selected_break_ids;
break_type.break_controller.set_enabled(is_enabled);
break_type.break_controller.set_enabled (is_enabled);
}
}
}
......@@ -102,44 +102,43 @@ public class BreakManager : Object {
private class BreakHelperServer : Object, IBreakHelper {
private weak BreakManager break_manager;
public BreakHelperServer(BreakManager break_manager) {
public BreakHelperServer (BreakManager break_manager) {
this.break_manager = break_manager;
}
public string? get_current_active_break() {
public string? get_current_active_break () {
/* Ask for focused break */
foreach (BreakType break_type in this.break_manager.all_breaks()) {
bool is_active = break_type.break_view.has_ui_focus() &&
break_type.break_controller.is_active();
foreach (BreakType break_type in this.break_manager.all_breaks ()) {
bool is_active = break_type.break_view.has_ui_focus () &&
break_type.break_controller.is_active ();
if (is_active) return break_type.id;
}
return null;
}
public bool is_active() {
public bool is_active () {
bool active = false;
foreach (BreakType break_type in this.break_manager.all_breaks()) {
active = active || break_type.break_controller.is_active();
foreach (BreakType break_type in this.break_manager.all_breaks ()) {
active = active || break_type.break_controller.is_active ();
}
return active;
}
public string[] get_break_ids() {
return this.break_manager.all_break_ids().to_array();
public string[] get_break_ids () {
return this.break_manager.all_break_ids ().to_array ();
}
public string[] get_status_messages() {
var messages = new Gee.ArrayList<string>();
foreach (BreakType break_type in break_manager.all_breaks()) {
string status_message = break_type.break_view.get_status_message();
messages.add("%s:\t%s".printf(break_type.id, status_message));
public string[] get_status_messages () {
var messages = new Gee.ArrayList<string> ();
foreach (BreakType break_type in break_manager.all_breaks ()) {
string status_message = break_type.break_view.get_status_message ();
messages.add ("%s:\t%s".printf (break_type.id, status_message));
}
return messages.to_array();
return messages.to_array ();
}
public void activate_break(string break_name) {
BreakType? break_type = this.break_manager.get_break_type_for_name(break_name);
if (break_type != null) break_type.break_controller.activate();
public void activate_break (string break_name) {
BreakType? break_type = this.break_manager.get_break_type_for_name (break_name);
if (break_type != null) break_type.break_controller.activate ();
}
}
}
\ No newline at end of file
......@@ -20,33 +20,32 @@ public class HelperApplication : Gtk.Application {
const string app_name = _("GNOME Break Timer");
const int DATA_VERSION = 0;
/* FIXME: font-size should have units, but we can only do that with GTK 3.8 and later */
static const string STYLE_DATA =
"""
@define-color bg_top rgba(218, 236, 237, 0.80);
@define-color bg_middle rgba(226, 237, 236, 0.87);
@define-color bg_bottom rgba(179, 209, 183, 0.89);
GtkWindow._screen-overlay {
background-color: @bg_inner;
background-image:-gtk-gradient (linear,
center top,
center bottom,
color-stop (0, @bg_top),
color-stop (0.08, @bg_middle),
color-stop (0.92, @bg_middle),
color-stop (1, @bg_bottom));
font-size: 18;
color: #999;
}
private static const string STYLE_DATA =
"""
@define-color bg_top rgba(218, 236, 237, 0.80);
@define-color bg_middle rgba(226, 237, 236, 0.87);
@define-color bg_bottom rgba(179, 209, 183, 0.89);
GtkWindow._screen-overlay {
background-color: @bg_inner;
background-image:-gtk-gradient (linear,
center top,
center bottom,
color-stop (0, @bg_top),
color-stop (0.08, @bg_middle),
color-stop (0.92, @bg_middle),
color-stop (1, @bg_bottom));
font-size: 18px;
color: #999;
}
GtkLabel._timer-label {
font-weight: bold;
font-size: 36;
color: #333;
text-shadow: 1px 1px 5px rgba(0, 0, 0, 0.5);
}
""";
GtkLabel._timer-label {
font-weight: bold;
font-size: 36px;
color: #333;
text-shadow: 1px 1px 5px rgba (0, 0, 0, 0.5);
}
""";
private BreakManager break_manager;
private ISessionStatus session_status;
......@@ -56,129 +55,129 @@ public class HelperApplication : Gtk.Application {
private string cache_path;
public HelperApplication() {
Object(
public HelperApplication () {
Object (
application_id: app_id,
register_session: true,
flags: ApplicationFlags.FLAGS_NONE
);
Environment.set_application_name(app_name);
Environment.set_application_name (app_name);
// Keep running for one minute after the last break is disabled
this.set_inactivity_timeout(60 * 1000);
this.set_inactivity_timeout (60 * 1000);
string user_cache_path = Environment.get_user_cache_dir();
this.cache_path = Path.build_filename(user_cache_path, "gnome-break-timer");
string user_cache_path = Environment.get_user_cache_dir ();
this.cache_path = Path.build_filename (user_cache_path, "gnome-break-timer");
}
public override void activate() {
base.activate();
public override void activate () {
base.activate ();
}
public override void startup() {
base.startup();
public override void startup () {
base.startup ();
Notify.init(app_name);
Notify.init (app_name);
/* set up custom gtk style for application */
Gdk.Screen screen = Gdk.Screen.get_default();
Gtk.CssProvider style_provider = new Gtk.CssProvider();
Gdk.Screen screen = Gdk.Screen.get_default ();
Gtk.CssProvider style_provider = new Gtk.CssProvider ();
try {
style_provider.load_from_data(STYLE_DATA, -1);
style_provider.load_from_data (STYLE_DATA, -1);
} catch (Error error) {
GLib.warning("Error loading style data: %s", error.message);
GLib.warning ("Error loading style data: %s", error.message);
}
Gtk.StyleContext.add_provider_for_screen(
Gtk.StyleContext.add_provider_for_screen (
screen,
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
);
this.session_status = new SessionStatus(this);
this.session_status = new SessionStatus (this);
try {
this.activity_monitor_backend = new X11ActivityMonitorBackend();
this.activity_monitor_backend = new X11ActivityMonitorBackend ();
} catch {
GLib.error("Failed to initialize activity monitor backend");
GLib.error ("Failed to initialize activity monitor backend");
}
this.activity_monitor = new ActivityMonitor(session_status, activity_monitor_backend);
this.activity_monitor = new ActivityMonitor (session_status, activity_monitor_backend);
this.ui_manager = new UIManager(this, session_status, false);
this.break_manager = new BreakManager(ui_manager);
this.break_manager.load_breaks(activity_monitor);
this.ui_manager = new UIManager (this, session_status, false);
this.break_manager = new BreakManager (ui_manager);
this.break_manager.load_breaks (activity_monitor);
this.restore_state();
this.restore_state ();
this.activity_monitor.start();
this.activity_monitor.start ();
var connection = this.get_dbus_connection();
var connection = this.get_dbus_connection ();
if (connection != null) {
Bus.own_name_on_connection(connection, HELPER_BUS_NAME, BusNameOwnerFlags.REPLACE, null, null);
Bus.own_name_on_connection (connection, HELPER_BUS_NAME, BusNameOwnerFlags.REPLACE, null, null);
}
}
public override void shutdown() {
base.shutdown();
public override void shutdown () {
base.shutdown ();
this.save_state();
this.save_state ();
}
private File get_state_file() {
File cache_dir = File.new_for_path(this.cache_path);
private File get_state_file () {
File cache_dir = File.new_for_path (this.cache_path);
try {
if (! cache_dir.query_exists()) cache_dir.make_directory_with_parents();
if (! cache_dir.query_exists ()) cache_dir.make_directory_with_parents ();
} catch (Error e) {
GLib.warning("Error creating cache directory: %s", e.message);
GLib.warning ("Error creating cache directory: %s", e.message);
}
string state_file_name = "last-state-%d".printf(DATA_VERSION);
return cache_dir.get_child(state_file_name);
string state_file_name = "last-state-%d".printf (DATA_VERSION);
return cache_dir.get_child (state_file_name);
}
private void save_state() {
File state_file = this.get_state_file();
private void save_state () {
File state_file = this.get_state_file ();
Json.Generator generator = new Json.Generator();
Json.Node root = new Json.Node(Json.NodeType.OBJECT);
Json.Object root_object = new Json.Object();
root.set_object(root_object);
generator.set_root(root);
Json.Generator generator = new Json.Generator ();
Json.Node root = new Json.Node (Json.NodeType.OBJECT);
Json.Object root_object = new Json.Object ();
root.set_object (root_object);
generator.set_root (root);
root_object.set_object_member("break_manager", this.break_manager.serialize());
root_object.set_object_member("activity_monitor_backend", this.activity_monitor_backend.serialize());
root_object.set_object_member("activity_monitor", this.activity_monitor.serialize());
root_object.set_object_member ("break_manager", this.break_manager.serialize ());
root_object.set_object_member ("activity_monitor_backend", this.activity_monitor_backend.serialize ());
root_object.set_object_member ("activity_monitor", this.activity_monitor.serialize ());
try {
OutputStream state_stream = state_file.replace(null, false, FileCreateFlags.NONE);
generator.to_stream(state_stream);
OutputStream state_stream = state_file.replace (null, false, FileCreateFlags.NONE);
generator.to_stream (state_stream);
} catch (Error e) {
GLib.warning("Error writing to breaks state file: %s", e.message);
GLib.warning ("Error writing to breaks state file: %s", e.message);
}
}
private void restore_state() {
File state_file = this.get_state_file();
if (state_file.query_exists()) {
InputStream state_stream = state_file.read();
Json.Parser parser = new Json.Parser();
parser.load_from_stream(state_stream);
private void restore_state () {
File state_file = this.get_state_file ();
if (state_file.query_exists ()) {
InputStream state_stream = state_file.read ();
Json.Parser parser = new Json.Parser ();
parser.load_from_stream (state_stream);
Json.Node? root = parser.get_root();
Json.Node? root = parser.get_root ();
if (root != null) {
Json.Object root_object = root.get_object();
Json.Object root_object = root.get_object ();
Json.Object break_manager_json = root_object.get_object_member("break_manager");
this.break_manager.deserialize(ref break_manager_json);
Json.Object break_manager_json = root_object.get_object_member ("break_manager");
this.break_manager.deserialize (ref break_manager_json);
Json.Object activity_monitor_backend_json = root_object.get_object_member("activity_monitor_backend");
this.activity_monitor_backend.deserialize(ref activity_monitor_backend_json);
Json.Object activity_monitor_backend_json = root_object.get_object_member ("activity_monitor_backend");
this.activity_monitor_backend.deserialize (ref activity_monitor_backend_json);
Json.Object activity_monitor_json = root_object.get_object_member("activity_monitor");
this.activity_monitor.deserialize(ref activity_monitor_json);
Json.Object activity_monitor_json = root_object.get_object_member ("activity_monitor");
this.activity_monitor.deserialize (ref activity_monitor_json);
this.activity_monitor.poll_activity();
this.activity_monitor.poll_activity ();
}
}
}
}
}
\ No newline at end of file
......@@ -18,10 +18,10 @@
/* FIXME: Do another overlay widget and kill the set_format junk :) */
public interface IScreenOverlayContent : Gtk.Widget {
public abstract void added_to_overlay();
public abstract void removed_from_overlay();
public abstract void before_fade_in();
public abstract void before_fade_out();
public abstract void added_to_overlay ();
public abstract void removed_from_overlay ();
public abstract void before_fade_in ();
public abstract void before_fade_out ();
}