Skip to content

Commit

Permalink
add a more comprehensive dbus api
Browse files Browse the repository at this point in the history
  • Loading branch information
zagortenay333 committed Sep 9, 2017
1 parent e6a6ece commit 0d983ff
Show file tree
Hide file tree
Showing 27 changed files with 1,099 additions and 817 deletions.
31 changes: 8 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,29 +346,14 @@ date, time spent (hh:mm), type ('++' = project, '()' = task), task or project
> **HINT:**
> There is an option to pause the time tracker when the pomodoro stops!
The time tracker can also be controled via the command line using dbus:
```sh
# The id 'asdf' is created by using the 'tracker_id:string' todo.txt extension.
# When using the stop/start by id funcs, all tasks with the given id will
# stop/start tracking.
#
# There are 3 funcs:
# - stop_all_tracking
# - stop_tracking_by_id
# - start_tracking_by_id

gdbus call --session --dest org.gnome.Shell \
--object-path /timepp/zagortenay333/TimeTracker \
--method timepp.zagortenay333.TimeTracker.stop_all_tracking

gdbus call --session --dest org.gnome.Shell \
--object-path /timepp/zagortenay333/TimeTracker \
--method timepp.zagortenay333.TimeTracker.start_tracking_by_id 'asdf'

gdbus call --session --dest org.gnome.Shell \
--object-path /timepp/zagortenay333/TimeTracker \
--method timepp.zagortenay333.TimeTracker.stop_tracking_by_id 'asdf'
```
---

### DBus API

This extension comes with a dbus api. Check out the [dbus](dbus) for info on
what you can do.

There are also some example scripts that might come in handy. :smile:

---

Expand Down
32 changes: 32 additions & 0 deletions dbus/pomodoro_iface.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE node PUBLIC
'-//freedesktop//DTD D-BUS Object Introspection 1.0//EN'
'http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd'>

<node>
<interface name="timepp.zagortenay333.Pomodoro">

<method name="start_pomo">
<arg name="pomo" type="d"/> <!-- seconds -->
</method>

<method name="start_new_pomo"/>
<method name="stop"/>
<method name="take_break"/>
<method name="timer_toggle"/>
<method name="show_fullscreen"/>
<method name="clear_pomo_counter"/>

<method name="set_phase_durations">
<arg name="pomo" type="s"/> <!-- seconds -->
<arg name="short_break" type="s"/> <!-- seconds -->
<arg name="long_break" type="s"/> <!-- seconds -->
<arg name="break_rate" type="s"/> <!-- long break every n pomos -->
</method>

<!-- state is one of: 'POMO', 'STOPPED', 'LONG_BREAK', 'SHORT_BREAK' -->
<signal name="pomo_state_changed">
<arg name="state" type="s"/>
</signal>

</interface>
</node>
37 changes: 37 additions & 0 deletions dbus/scripts/examples
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
set -eu


# =====================================================================
# Templates for monitoring a signal and calling a method
# =====================================================================


pomodoro_monitor () {
local signal
local -a msg

gdbus monitor --session \
--dest org.gnome.Shell \
--object-path /timepp/zagortenay333/Pomodoro |
while read -r -a msg; do
signal=${msg[1]} signal=${signal##*.}

case "$signal" in
"POMO") echo "$signal";;
"STOPPED") echo "$signal";;
"LONG_BREAK") echo "$signal";;
"SHORT_BREAK") echo "$signal";;
esac
done
}

timer_start () {
local time="$1"

>/dev/null gdbus call \
--session \
--dest org.gnome.Shell \
--object-path /timepp/zagortenay333/Timer \
--method timepp.zagortenay333.Timer.start "$time"
}
164 changes: 164 additions & 0 deletions dbus/scripts/time_tracker_control
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#!/bin/bash
set -eu


# =====================================================================
# This script is used to control the time-tracker of the Time++ gnome
# shell extension:
# https://github.com/zagortenay333/timepp__gnome
#
# Once executed, the script will listen for when the workspace change
# and do something.
#
# The tracker is controled by supplying string arguments.
# An arg string is of the form:
# "["default" ]tracker_id n1[, n2, n3,...]"
#
# If the string starts with the "default" keyword then:
# - the given tracker_id is gonna be the default id
# - no workspace numbers need to provided
#
# The tracker_id is either:
# - the string "stop"
# - a tracker_id specified in Time++ using the the "tracker_id:string"
# todo.txt extension. (See README of Time++.)
#
# The n1, n2, ... numbers correspond to workspace numbers. They are
# 0-indexed.
#
# Examples:
#
# 1) tracker_control "stop 0" "default id1 2 3" "id2 1 4"
# Means:
# - If on workspace number 0 , stop the tracker.
# - If on workspace number 2 or 3, start tracking tasks with id "id1".
# - If on workspace number 1 or 4, start tracking tasks with id "id2".
# - If on any other workspace , start tracking tasks with id "id1".
#
# 2) tracker_control "id1 2 3"
# Means:
# - If on workspace number 2 or 3, start tracking tasks with id "id1".
# - If on any other workspace , stop tracking.
#
# 3) tracker_control "default id1"
# Means:
# - Always track tasks with id "id1".
#
# 4) tracker_control
# Means:
# - Stop tracking.
# =====================================================================


# =====================================================================
# @@@ Constants
# =====================================================================
declare -A id_workspace_map

current_workspace=0

current_id=0

default_id="stop"



# =====================================================================
# @@@ Funcs
# =====================================================================
stop_tracking () {
>/dev/null gdbus call \
--session \
--dest org.gnome.Shell \
--object-path /timepp/zagortenay333/TimeTracker \
--method timepp.zagortenay333.TimeTracker.stop_all_tracking
}

start_tracking () {
local id=$1

stop_tracking

>/dev/null gdbus call \
--session \
--dest org.gnome.Shell \
--object-path /timepp/zagortenay333/TimeTracker \
--method timepp.zagortenay333.TimeTracker.start_tracking_by_id "$id"
}

on_workspace_changed () {
local found_id=$default_id
local it
local workspace

for it in "${!id_workspace_map[@]}"; do
for workspace in ${id_workspace_map[$it]}; do
[[ $workspace == "$current_workspace" ]] && found_id=$it && break 1
done
done

[[ $found_id == "$current_id" ]] && return

if [[ $found_id == "stop" ]]; then
stop_tracking
else
start_tracking "$found_id"
fi

current_id=$found_id
}

check_workspace () {
local workspace

read -r -a workspace <<< "$(
gdbus call \
--session \
--dest org.gnome.Shell \
--object-path /org/gnome/Shell \
--method org.gnome.Shell.Eval \
"global.screen.get_active_workspace_index()"
)"

# gdbus will return a string of the form "('bool', 'n')".
# We need to extract n only.
workspace=${workspace[1]}
workspace=${workspace:1: -2}

if [[ $workspace != "$current_workspace" ]]; then
current_workspace=$workspace
on_workspace_changed
fi
}



# =====================================================================
# @@@ Parse args
# =====================================================================
[[ $# == 0 ]] && stop_tracking && exit

for it in "$@"; do
read -r -a it <<< "$it"

case ${it[0]} in
"default")
default_id=${it[1]}
id_workspace_map[${it[1]}]="${it[*]:2}"
;;
*)
id_workspace_map[${it[0]}]="${it[*]:1}"
esac
done



# =====================================================================
# @@@ Mainloop
# =====================================================================
trap "stop_tracking" INT TERM EXIT

while true; do
check_workspace
sleep 1
done
20 changes: 20 additions & 0 deletions dbus/stopwatch_iface.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE node PUBLIC
'-//freedesktop//DTD D-BUS Object Introspection 1.0//EN'
'http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd'>

<node>
<interface name="timepp.zagortenay333.Stopwatch">

<method name="start"/>
<method name="stop"/>
<method name="reset"/>
<method name="lap"/>
<method name="show_fullscreen"/>

<!-- time is returned in miliseconds -->
<method name="get_time">
<arg name="time" type="u" direction="out"/>
</method>

</interface>
</node>
23 changes: 23 additions & 0 deletions dbus/time_tracker_iface.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE node PUBLIC
'-//freedesktop//DTD D-BUS Object Introspection 1.0//EN'
'http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd'>

<node>
<interface name="timepp.zagortenay333.TimeTracker">

<method name="stop_all_tracking"/>

<!--
- The argument is the tracker id specified using the
- 'tracker_id:string' todo.txt extension.
-->
<method name="stop_tracking_by_id">
<arg name="tracker_id" type="s" direction="in"/>
</method>

<method name="start_tracking_by_id">
<arg name="tracker_id" type="s" direction="in"/>
</method>

</interface>
</node>
24 changes: 24 additions & 0 deletions dbus/timer_iface.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE node PUBLIC
'-//freedesktop//DTD D-BUS Object Introspection 1.0//EN'
'http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd'>

<node>
<interface name="timepp.zagortenay333.Timer">

<method name="start">
<arg name="time" type="u" direction="in"/> <!-- in seconds -->
</method>

<method name="stop"/>
<method name="toggle_timer"/>
<method name="reset"/>
<method name="show_fullscreen"/>

<method name="set_notif_msg">
<arg name="msg" type="s" direction="in"/>
</method>

<signal name="timer_expired"/>

</interface>
</node>
2 changes: 1 addition & 1 deletion lib/num_picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ var NumPicker = new Lang.Class({
else return s;
},

_set_counter: function (num) {
set_counter: function (num) {
this.counter_label.text = this._left_pad(num, 2);
this.counter = num;
},
Expand Down
12 changes: 3 additions & 9 deletions lib/panel_item.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,14 @@ var PanelItem = new Lang.Class({
//
// listen
//
this.actor.connect('button-press-event', (actor, event) => {
let btn = event.get_button();

switch (btn) {
this.actor.connect('button-press-event', (_, event) => {
switch (event.get_button()) {
case Clutter.BUTTON_PRIMARY:
this.emit('left-click');
return Clutter.EVENT_STOP;

case Clutter.BUTTON_MIDDLE:
this.emit('middle-click');
return Clutter.EVENT_STOP;

case Clutter.BUTTON_SECONDARY:
this.emit('right-click');
return Clutter.EVENT_STOP;
Expand All @@ -61,9 +57,7 @@ var PanelItem = new Lang.Class({
return Clutter.EVENT_PROPAGATE;
});

this.actor.connect('key-press-event', (actor, event) => {
let symbol = event.get_key_symbol();

this.actor.connect('key-press-event', (_, event) => {
if (event.get_key_symbol() === Clutter.KEY_Down) {
this.menu.actor.navigate_focus(global.stage.get_key_focus(),
Gtk.DirectionType.TAB_FORWARD,
Expand Down
Loading

0 comments on commit 0d983ff

Please sign in to comment.