forked from artynusov/MacTimeLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainController.py
198 lines (153 loc) · 7.25 KB
/
MainController.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#
# MainController.py
# Main application controller
#
# Copyright 2009 Artem Yunusov. All rights reserved.
#
import os
import objc
from Foundation import *
from AppKit import *
from Tasks import Tasks
from Projects import Projects
from SlackingAutocompletes import SlackingAutocompletes
import FormatterHelpers as fh
from Settings import Settings
from Notification import Notification
class MainController(NSObject):
outputArea = objc.IBOutlet("outputArea")
lblTimeLeft = objc.IBOutlet("lblTimeLeft")
lblTimeSpentCurr = objc.IBOutlet("lblTimeSpentCurr")
cbxInput = objc.IBOutlet("cbxInput")
lblSlackingTime = objc.IBOutlet("lblSlackingTime")
lblTimeSpent = objc.IBOutlet("lblTimeSpent")
lblWorkTill = objc.IBOutlet("lblWorkTill")
pbtnProject = objc.IBOutlet("pbtnProject")
pbtnProject = objc.IBOutlet("pbtnProject")
workTillBox = objc.IBOutlet("workTillBox")
mainWindow = objc.IBOutlet("mainWindow")
prefWindow = objc.IBOutlet("prefWindow")
reportWindow = objc.IBOutlet("reportWindow")
reportController = objc.IBOutlet("reportController")
applicationRef = objc.IBOutlet("applicationRef")
btnDone = objc.IBOutlet("btnDone")
tasks = None
def awakeFromNib(self):
def onGrowlClick():
self.applicationRef.unhide()
self.cbxInput.becomeFirstResponder()
self.notification = Notification(onGrowlClick)
self.initControls()
self.initWindowStates()
self.readCounters()
self._timer = NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(Settings.get("timerInterval"),
self, self.timerFunction, None, True)
def initControls(self):
"""Init basic controls"""
self.outputArea.setString_("")
self.tasks = Tasks()
if Settings.get("showWorkTill"):
self.workTillBox.setHidden_(False)
else:
self.workTillBox.setHidden_(True)
self.pbtnProject.removeAllItems()
self.pbtnProject.addItemsWithTitles_(Projects.get())
self.pbtnProject.selectItemWithTitle_(Settings.get("selectedProject"))
self.projectChange_(None)
self.initDoneButton()
self.fillTasks()
self.scrollToEnd()
def initDoneButton(self):
if self.tasks.dayStarted():
self.btnDone.setTitle_("Done")
else:
self.btnDone.setTitle_("Start")
def initWindowStates(self):
"""Init windows sizes and positions"""
self.prefWindow.setFrameAutosaveName_("prefWindow")
self.mainWindow.setFrameAutosaveName_("mainWindow")
self.reportWindow.setFrameAutosaveName_("reportWindow")
def timerFunction(self):
"""Timer callback function"""
self.tasks.setCurrentTask(self.cbxInput.stringValue())
self.readCounters()
self.notification.idleNotify(self.tasks.timings.currentSeconds)
if self.tasks.timings.isNextDay():
self.initControls()
def readCounters(self):
"""Read counters"""
self.lblTimeSpent.setStringValue_(fh.secToTimeStr(self.tasks.timings.spentSeconds))
self.lblSlackingTime.setStringValue_(fh.secToTimeStr(self.tasks.timings.slackingSeconds))
self.lblTimeLeft.setStringValue_(fh.secToTimeStr(self.tasks.timings.leftSeconds))
self.lblTimeSpentCurr.setStringValue_(fh.secToTimeStr(self.tasks.timings.currentSeconds))
self.lblWorkTill.setStringValue_(fh.timeStructToTimeStr(self.tasks.timings.workTillTime))
def appendTask(self, taskString, color):
def appendText(text, color=None):
"""Append text to task text area"""
endRange = NSRange()
endRange.location = self.outputArea.textStorage().length()
endRange.length = 0
self.outputArea.replaceCharactersInRange_withString_(endRange, text)
if color:
colorRange = NSRange()
colorRange.location = self.outputArea.textStorage().length() - len(text)
colorRange.length = len(text)
self.outputArea.setTextColor_range_(color, colorRange)
appendText(taskString, color)
if self.reportWindow.isVisible():
self.reportController.generateChart()
def fillTasks(self):
"""Fill text area with tasks"""
for task in self.tasks.taskList:
self.appendTask(*fh.formatTaskString(*task))
def scrollToEnd(self):
"""Scroll tasks textarea to the end"""
self.outputArea.scrollRangeToVisible_(NSMakeRange(self.outputArea.string().length(), 0))
def showStartHelpMessage(self):
"""Show alert with help message"""
alert = NSAlert.alloc().init()
alert.addButtonWithTitle_('OK')
alert.setMessageText_("Congrats, you started your working day!")
alert.setInformativeText_("Now start doing your working activity (e.g reading mail). "
"When you finish with your activity go back to MacTimeLog and type it in. "
"If your activity is slacking, use 2 asterisks (**) at the end of the activity's name.")
alert.setShowsSuppressionButton_(True)
alert.runModal()
if alert.suppressionButton().state() == NSOnState:
Settings.set("showHelpMessageOnStart", False)
Settings.sync()
@objc.IBAction
def btnDonePress_(self, sender):
"""On done button press"""
if self.tasks.dayStarted():
if self.cbxInput.stringValue().strip():
taskName = self.cbxInput.stringValue()
self.appendTask(*fh.formatTaskString(*self.tasks.add(taskName, self.pbtnProject.titleOfSelectedItem())))
self.readCounters()
self.cbxInput.setStringValue_("")
self.scrollToEnd()
if Tasks.taskType(taskName) == "work":
Projects.addAutocomplete(self.pbtnProject.titleOfSelectedItem(), taskName)
else:
SlackingAutocompletes.add(taskName)
self.cbxInput.addItemWithObjectValue_(taskName)
else:
if Settings.get("showHelpMessageOnStart"):
self.showStartHelpMessage()
taskName = Settings.get("startPlaceholder")
self.appendTask(*fh.formatTaskString(*self.tasks.add(taskName)))
self.initDoneButton()
@objc.IBAction
def projectChange_(self, sender):
"""Project changed event"""
if self.pbtnProject.titleOfSelectedItem():
self.cbxInput.removeAllItems()
self.cbxInput.addItemsWithObjectValues_(Projects.getAutocomleteList(
self.pbtnProject.titleOfSelectedItem(), SlackingAutocompletes.get()))
if sender:
Settings.set("selectedProject", unicode(self.pbtnProject.titleOfSelectedItem()))
Settings.sync()
@objc.IBAction
def openLog_(self, sender):
""" Open log in text editor"""
os.system(Settings.get("logEditCommand") % Settings.get("logPath"))