Scheduler.vala 2.35 KB
Newer Older
Dylan McCall's avatar
Dylan McCall committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * This file is part of Brain Break.
 * 
 * Brain Break is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Brain Break is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Brain Break.  If not, see <http://www.gnu.org/licenses/>.
 */

Dylan McCall's avatar
Dylan McCall committed
18 19 20 21
/**
 * Interface for a type of break. Each break type has a unique feedback
 * mechanism triggered by calling the begin method.
 */
22 23
public abstract class Scheduler : Object {
	public int interval {get; set;}
Dylan McCall's avatar
Dylan McCall committed
24
	/* TODO: duration should be private to child class */
25
	public int duration {get; set;}
Dylan McCall's avatar
Dylan McCall committed
26 27
	
	/** Called when a break starts to run */
28
	public signal void started();
Dylan McCall's avatar
Dylan McCall committed
29
	/** Called when a break is finished running */
30 31 32 33 34 35 36
	public signal void finished();
	
	public enum SchedulerState {
		WAITING,
		ACTIVE
	}
	private static SchedulerState state;
Dylan McCall's avatar
Dylan McCall committed
37 38 39
	
	protected Timer start_timer;
	
40
	public Scheduler(int interval, int duration) {
Dylan McCall's avatar
Dylan McCall committed
41 42 43 44 45 46
		this.interval = interval;
		this.duration = duration;
		
		start_timer = new Timer();
		
		/* FIXME: We need LCD of duration and interval so we catch idle>duration as well as start the rest on time */
47
		Timeout.add_seconds(duration, idle_timeout);
Dylan McCall's avatar
Dylan McCall committed
48 49 50 51 52
	}
	
	/**
	 * Periodically tests if it is time for a break
	 */
53 54
	protected bool idle_timeout() {
		int idle_time = (int)(Magic.get_idle_time() / 1000);
Dylan McCall's avatar
Dylan McCall committed
55 56
		
		/* Reset timer if the user takes a sufficiently long break */
57 58
		if (idle_time > duration) {
			stdout.printf("Resetting break timer for %s!\n", this.get_type().name());
Dylan McCall's avatar
Dylan McCall committed
59 60 61 62
			start_timer.start();
		}
		
		/* Start break if the user has been active for interval */
63 64
		if (start_timer.elapsed() >= interval && this.state == SchedulerState.WAITING) {
			stdout.printf("Activating break %s!\n", this.get_type().name());
Dylan McCall's avatar
Dylan McCall committed
65 66 67 68 69 70 71 72 73
			activate();
		}
		
		return true;
	}
	
	/**
	 * It is time for a break!
	 */
74 75 76
	public virtual void activate() {
		this.state = SchedulerState.ACTIVE;
		this.started();
Dylan McCall's avatar
Dylan McCall committed
77 78
	}
	
79 80 81
	public virtual void end() {
		this.state = SchedulerState.WAITING;
		this.finished();
Dylan McCall's avatar
Dylan McCall committed
82 83 84
	}
}