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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
# Copyright (C) 2007, One Laptop Per Child
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from gettext import gettext as _
import logging
import time
from datetime import datetime, timedelta
import gobject
import gtk
from sugar.graphics import units
from sugar.graphics import color
from sugar.graphics.xocolor import XoColor
from sugar.graphics.toolbox import Toolbox
from sugar.graphics.toolcombobox import ToolComboBox
from sugar.graphics.toolbutton import ToolButton
from sugar.graphics.combobox import ComboBox
from sugar import activity
from sugar.objects import objecttype
from sugar.datastore import datastore
class JournalToolbox(Toolbox):
def __init__(self):
Toolbox.__init__(self)
self.search_toolbar = SearchToolbar()
self.add_toolbar(_('Search'), self.search_toolbar)
self.search_toolbar.show()
self.entry_toolbar = EntryToolbar()
self.add_toolbar(_('Entry'), self.entry_toolbar)
self.entry_toolbar.show()
#self.create_toolbar = CreateToolbar()
#self.add_toolbar(_('Create'), self.create_toolbar)
#self.create_toolbar.show()
class SearchToolbar(gtk.Toolbar):
__gtype_name__ = 'SearchToolbar'
__gsignals__ = {
'query-changed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([object]))
}
_ACTION_ANYTIME = 0
_ACTION_TODAY = 1
_ACTION_SINCE_YESTERDAY = 2
_ACTION_THIS_WEEK = 3
_ACTION_THIS_MONTH = 4
_ACTION_THIS_YEAR = 5
_ACTION_ANYTHING = 0
_ACTION_EVERYBODY = 0
_ACTION_MY_FRIENDS = 1
_ACTION_MY_CLASS = 2
def __init__(self):
gtk.Toolbar.__init__(self)
self._volume_id = None
self._search_entry = gtk.Entry()
self._search_entry.connect('activate', self._search_entry_activated_cb)
self._add_widget(self._search_entry, expand=True)
self._what_search_combo = ComboBox()
self._what_combo_changed_sid = \
self._what_search_combo.connect('changed', self._combo_changed_cb)
tool_item = ToolComboBox(self._what_search_combo)
self.insert(tool_item, -1)
tool_item.show()
self._when_search_combo = self._get_when_search_combo()
tool_item = ToolComboBox(self._when_search_combo)
self.insert(tool_item, -1)
tool_item.show()
# TODO: enable it when the DS supports saving the buddies.
#self._with_search_combo = self._get_with_search_combo()
#tool_item = ToolComboBox(self._with_search_combo)
#self.insert(tool_item, -1)
#tool_item.show()
self._query = self._build_query()
self.refresh_filters()
def _get_when_search_combo(self):
when_search = ComboBox()
when_search.append_item(self._ACTION_ANYTIME, _('Anytime'))
when_search.append_separator()
when_search.append_item(self._ACTION_TODAY, _('Today'))
when_search.append_item(self._ACTION_SINCE_YESTERDAY,
_('Since yesterday'))
when_search.append_item(self._ACTION_THIS_WEEK, _('This week'))
when_search.append_item(self._ACTION_THIS_MONTH, _('This month'))
when_search.append_item(self._ACTION_THIS_YEAR, _('This year'))
when_search.set_active(0)
when_search.connect('changed', self._combo_changed_cb)
return when_search
def _get_with_search_combo(self):
with_search = ComboBox()
with_search.append_item(self._ACTION_EVERYBODY, _('Anyone'))
with_search.append_separator()
with_search.append_item(self._ACTION_MY_FRIENDS,
_('My friends'), 'theme:stock-open')
with_search.append_item(self._ACTION_MY_CLASS,
_('My class'), 'theme:stock-open')
with_search.append_separator()
# TODO: Ask the model for buddies.
with_search.append_item(3, 'Dan', 'theme:stock-buddy') #, XoColor('#6afbf4,#fc5f68'))
with_search.set_active(0)
with_search.connect('changed', self._combo_changed_cb)
return with_search
def _add_widget(self, widget, expand=False):
tool_item = gtk.ToolItem()
tool_item.set_expand(expand)
tool_item.add(widget)
widget.show()
self.insert(tool_item, -1)
tool_item.show()
def _build_query(self):
query = {}
if self._volume_id:
query['mountpoints'] = [self._volume_id]
if self._what_search_combo.props.value:
value = self._what_search_combo.props.value
type = objecttype.get_registry().get_type(value)
if type:
mime_types = type.mime_types
query['mime_type'] = mime_types
else:
query['activity'] = self._what_search_combo.props.value
if self._when_search_combo.props.value:
date_from, date_to = self._get_date_range()
query['mtime'] = {'start': date_from, 'end': date_to}
if self._search_entry.props.text:
query['query'] = self._search_entry.props.text
return query
def _get_date_range(self):
today = datetime.today().replace(hour=0, minute=0, second=0)
if self._when_search_combo.props.value == self._ACTION_TODAY:
date_range = (today,
today.replace(hour=23, minute=59, second=59))
elif self._when_search_combo.props.value == self._ACTION_SINCE_YESTERDAY:
date_range = (today - timedelta(1),
today.replace(hour=23, minute=59, second=59))
elif self._when_search_combo.props.value == self._ACTION_THIS_WEEK:
date_range = (today - timedelta(today.weekday()),
today.replace(hour=23, minute=59, second=59) + \
timedelta(6 - today.weekday()))
elif self._when_search_combo.props.value == self._ACTION_THIS_MONTH:
date_range = (today - timedelta(today.day - 1),
today.replace(hour=23, minute=59, second=59) + \
timedelta(self._days_in_month(today) - today.day))
elif self._when_search_combo.props.value == self._ACTION_THIS_YEAR:
date_range = (today.replace(month=1, day=1),
today.replace(month=12, day=31, hour=23, minute=59,
second=59))
return (date_range[0].isoformat(),
date_range[1].isoformat())
def _days_in_month(self, date):
year, month = date.year, date.month
if month == 12:
year += 1
month = 1
else:
month += 1
date = date.replace(year=year, month=month, day=1)
date -= timedelta(1)
return date.day
def _combo_changed_cb(self, combo):
new_query = self._build_query()
if self._query != new_query:
self._query = new_query
self.emit('query-changed', self._query)
def _search_entry_activated_cb(self, search_entry):
new_query = self._build_query()
if self._query != new_query:
self._query = new_query
self.emit('query-changed', self._query)
def set_volume_id(self, volume_id):
self._volume_id = volume_id
new_query = self._build_query()
if self._query != new_query:
self._query = new_query
self.emit('query-changed', self._query)
def refresh_filters(self):
current_value = self._what_search_combo.props.value
current_value_index = 0
self._what_search_combo.remove_all()
self._what_search_combo.append_item(self._ACTION_ANYTHING, _('Anything'))
appended_separator = False
for service_name in datastore.get_unique_values('activity'):
activity_info = activity.get_registry().get_activity(service_name)
if not activity_info is None:
if not appended_separator:
self._what_search_combo.append_separator()
appended_separator = True
self._what_search_combo.append_item(service_name, activity_info.name,
activity_info.icon)
if service_name == current_value:
current_value_index = len(self._what_search_combo.get_model()) - 1
types = []
for mime_type in datastore.get_unique_values('mime_type'):
type = objecttype.get_registry().get_type_for_mime(mime_type)
if type and type.type_id not in [t.type_id for t in types]:
types.append(type)
appended_separator = False
for type in types:
if not appended_separator:
self._what_search_combo.append_separator()
appended_separator = True
self._what_search_combo.append_item(type.type_id, type.name, type.icon)
if type.type_id == current_value:
current_value_index = len(self._what_search_combo.get_model()) - 1
self._what_search_combo.handler_block(self._what_combo_changed_sid)
try:
self._what_search_combo.set_active(current_value_index)
finally:
self._what_search_combo.handler_unblock(self._what_combo_changed_sid)
class EntryToolbar(gtk.Toolbar):
__gtype_name__ = 'EntryToolbar'
__gsignals__ = {
'entry-erased': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([]))
}
def __init__(self):
gtk.Toolbar.__init__(self)
self._jobject = None
self._resume = ToolButton('resume')
self._resume.set_tooltip(_('Resume'))
self._resume.props.sensitive = False
self._resume.connect('clicked', self._resume_clicked_cb)
self.insert(self._resume, -1)
self._resume.show()
self._copy = ToolButton('edit-copy')
self._copy.set_tooltip(_('Copy to clipboard'))
self._copy.props.sensitive = False
self._copy.connect('clicked', self._copy_clicked_cb)
self.insert(self._copy, -1)
self._copy.show()
separator = gtk.SeparatorToolItem()
separator.set_expand(True)
separator.props.draw = False
self.insert(separator, -1)
separator.show()
self._erase = ToolButton('erase')
self._erase.set_tooltip(_('Erase'))
self._erase.props.sensitive = False
self._erase.connect('clicked', self._erase_clicked_cb)
self.insert(self._erase, -1)
self._erase.show()
def _resume_clicked_cb(self, button):
self._jobject.resume()
def _copy_clicked_cb(self, button):
clipboard = gtk.Clipboard()
clipboard.set_with_data([('text/uri-list', 0, 0)],
self._clipboard_get_func_cb,
self._clipboard_clear_func_cb)
def _clipboard_get_func_cb(self, clipboard, selection_data, info, data):
selection_data.set('text/uri-list', 8, self._jobject.file_path)
def _clipboard_clear_func_cb(self, clipboard, data):
pass
def _erase_clicked_cb(self, button):
datastore.delete(self._jobject.object_id)
self.emit('entry-erased')
def set_jobject(self, jobject):
if jobject == self._jobject:
return
self._jobject = jobject
if self._jobject:
activities = self._jobject.get_activities()
self._copy.props.sensitive = True
self._erase.props.sensitive = True
if activities:
self._resume.props.sensitive = True
else:
self._resume.props.sensitive = False
else:
self._erase.props.sensitive = False
self._copy.props.sensitive = False
self._resume.props.sensitive = False
|