-
Notifications
You must be signed in to change notification settings - Fork 13
/
nowbutton.py
executable file
·118 lines (91 loc) · 3.25 KB
/
nowbutton.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# nowbutton.py
#
# A Zim plugin that jumps to today's journal entry, and appends the current *time* to the end of the file.
# This makes it nearly trivial to keep a log with tighter-than-one-day granularity.
#
# Skeleton and basic operation of this script was DERIVED from zim 'quicknote' and 'tasklist' plugins.
#
from time import strftime
import gtk
from datetime import datetime, timedelta
from datetime import date as dateclass
from zim.config import StringAllowEmpty
# TODO: kern out unneccesary imports
from zim.plugins import PluginClass, WindowExtension, extends
from zim.actions import action
import logging
logger = logging.getLogger('zim.plugins.nowbutton')
class NowButtonPlugin(PluginClass):
plugin_info = {
'name': _('Now Button'), # T: plugin name
'description': _('''\
This plugin provides an easy toolbar option to append the current time to today's journal entry and
focus that page. Note that it is easy to get back to where you were due to Zim\'s built-in back-tracking
buttons.
'''), # T: plugin description
'author': 'Robert Hailey',
'help': 'Plugins:NowButton',
}
plugin_preferences = (
('hours_past_midnight', 'int', _('Hours past Midnight'), 4, (0, 12)),
('timestamp_format', 'string', _('Timestamp format'), '%I:%M%p -', StringAllowEmpty),
)
@extends('MainWindow')
class MainWindowExtension(WindowExtension):
uimanager_xml = '''
<ui>
<menubar name='menubar'>
<menu action='tools_menu'>
<placeholder name='plugin_items'>
<menuitem action='now_button_clicked'/>
</placeholder>
</menu>
</menubar>
<toolbar name='toolbar'>
<placeholder name='tools'>
<toolitem action='now_button_clicked'/>
</placeholder>
</toolbar>
</ui>
'''
def __init__(self, plugin, window):
WindowExtension.__init__(self, plugin, window)
@action(
_('Log Entry'),
stock=gtk.STOCK_JUMP_TO,
readonly=True,
accelerator = '<Control><Shift>E'
) # T: menu item
def now_button_clicked(self):
calendar_config=self.plugin.config.get_config_dict('<profile>/preferences.conf')['CalendarPlugin'];
calendar_namespace=calendar_config['namespace'];
offset_time=datetime.today()-timedelta(hours=self.plugin.preferences['hours_past_midnight'])
name=calendar_namespace.child(offset_time.strftime('%Y:%m:%d')).name;
text = '\n%s ' % strftime(self.plugin.preferences['timestamp_format']).lower();
ui = self.window.ui
try:
#v0.65
path=ui.notebook.resolve_path(name);
except AttributeError:
#v0.66
path=ui.notebook.pages.lookup_from_user_input(name);
page=ui.notebook.get_page(path);
#ui.append_text_to_page(path, text)
if not page.exists():
parsetree = ui.notebook.get_template(page)
page.set_parsetree(parsetree)
page.parse('wiki', text, append=True) # FIXME format hard coded ??? (this FIXME was copied from gui.__init__)
ui.present(path)
ui.notebook.store_page(page);
# Move the cursor to the end of the line that was just appended...
textBuffer = self.window.pageview.view.get_buffer();
i = textBuffer.get_end_iter();
i.backward_visible_cursor_positions(1);
textBuffer.place_cursor(i);
# and finally... scroll the window all the way to the bottom.
self.window.pageview.scroll_cursor_on_screen();
def on_notebook_changed(self):
return None