forked from ihdavids/orgextended
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orgnotifications.py
179 lines (155 loc) · 5.29 KB
/
orgnotifications.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import sublime
import sublime_plugin
import threading, time, signal
from datetime import timedelta
from datetime import datetime
import os
import sys
import OrgExtended.orgagenda as agenda
import OrgExtended.orgdb as db
import OrgExtended.asettings as sets
from OrgExtended.orgparse.date import OrgDate
import logging
import subprocess, os
CHECK_PERIOD = 60*1
log = logging.getLogger(__name__)
class Notification(agenda.TodoView):
def Show(self, notifications, newItem):
# Windows notice.
n = newItem['node']
f = newItem['file']
heading = ""
if(n.scheduled):
heading = OrgDate.format_clock(n.scheduled.start,active=False)
else:
heading = "SOON!"
body = f.AgendaFilenameTag() + " " + n.heading
ShowBalloon(body, heading)
# Show the in sublime version, clear out other
# todos and just show our notices.
self.entries = []
if(notifications and type(notifications) is dict):
for key,item in notifications.items():
self.AddEntry(item['node'],item['file'])
window = sublime.active_window()
window.active_view().run_command('org_show_notifications')
return
notification = None
class OrgShowNotifications(sublime_plugin.TextCommand):
def run(self, edit):
notification.DoRenderView(edit)
def ShowBalloon(todo, time):
log.debug("SHOW BALLOON POPUP")
formatDict = {
"time": time,
"todo": todo
}
if(sublime.platform() == 'windows'):
commandLine = sets.Get("ExternalNotificationCommand",[r"C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe", "-ExecutionPolicy", "Unrestricted", ".\\balloontip.ps1", "\"{todo}\"", "\"{time}\""], formatDict)
elif(sublime.platform() == 'osx'):
commandLine = sets.Get("ExternalNotificationCommand",['osascript','-e',"display notification \"{time}\" with title \"{todo}\" subtitle \""+"Org Mode TODO"+"\""], formatDict)
else:
print("ERROR: platform not yet supported for notifications")
# Expand all potential macros.
for i in range(len(commandLine)):
commandLine[i] = commandLine[i].format(todo=todo,time=time)
try:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
except:
startupinfo = None
# cwd=working_dir, env=my_env,
cwd = os.path.join(sublime.packages_path(),"OrgExtended")
if(sublime.platform() == 'windows'):
popen = subprocess.Popen(commandLine, universal_newlines=True, cwd=cwd, startupinfo=startupinfo, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
popen.wait()
elif(sublime.platform() == 'osx'):
subprocess.check_call(commandLine,stderr=subprocess.STDOUT)
def IsWithinNotificationWindow(n, hours, minutes):
if(not n.scheduled):
return False
now = datetime.now()
hour = now.hour
mins = now.minute
if(n.scheduled.repeating):
next = n.scheduled.next_repeat_from_today
return ((next.hour-hours) == hour and (next.minute - mins) <= minutes)
next = agenda.EnsureDateTime(n.scheduled.start)
return ((next.hour-hours) == hour and (next.minute - mins) <= minutes)
def GetUID(item):
n = item['node']
f = item['file']
return f.filename + "@" + n.get_locator() + "@" + str(n.scheduled)
class NotificationSystem(threading.Thread):
def __init__(self, interval):
threading.Thread.__init__(self)
self.daemon = False
self.stopped = threading.Event()
self.interval = interval
self.today = None
self.notified = {}
self.todaysDate = datetime.now().day
self.checkcount = 1
def stop(self):
self.stopped.set()
self.join()
def run(self):
while not self.stopped.wait(self.interval.total_seconds()):
self.CheckNotifications()
def HaveNotifiedFor(self, item):
return GetUID(item) in self.notified
def DoNotify(self,item):
self.notified[GetUID(item)] = item
global notification
notification = Notification("Notifications")
notification.Show(self.notified, item)
def CheckNotifications(self):
log.debug("CHECKING...")
if(datetime.now().day > self.todaysDate or self.today == None):
self.todaysDate = datetime.now().day
self.notified = {}
self.BuildToday()
# Periodically rebuild the day.
if((self.checkcount % 4) == 0):
self.checkcount += 1
self.BuildToday()
hours = sets.Get("notifyHoursBefore", 0)
minutes = sets.Get("notifyMinsBefore", 15)
for item in self.today:
n = item['node']
if(IsWithinNotificationWindow(n, hours, minutes) and not self.HaveNotifiedFor(item)):
log.debug("DO NOTIFY CALLED")
self.DoNotify(item)
def AddEntry(self,n,f):
self.today.append({'node': n, 'file': f})
def BuildToday(self):
self.today = []
for file in db.Get().Files:
for n in file.org[1:]:
if(self.TodayCheck(n, file)):
self.AddEntry(n, file)
def TodayCheck(self, n, file):
try:
now = datetime.now()
return (agenda.IsTodo(n) and not agenda.IsDone(n) and not agenda.IsArchived(n) and agenda.IsToday(n, now))
except:
if(n):
log.error("NOTIFICATIONS FAILED TO PARSE: " + str(n.heading))
return False
notice = None
def Setup():
global notice
checkPeriod = sets.Get("noticePeriod",1)*60
if(checkPeriod < 60):
checkPeriod = 60
notice = NotificationSystem(interval=timedelta(seconds=checkPeriod))
notice.start()
log.debug("NOTIFICATION SYSTEM IS UP AND RUNNING: " + str(checkPeriod))
def Get():
return notice
class OrgRebuildNotificationsCommand(sublime_plugin.TextCommand):
def run(self, edit):
global notice
if(notice == None):
Setup()
notice.BuildToday()