File: recentchangesdialog.py

package info (click to toggle)
zim 0.65-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,816 kB
  • ctags: 6,779
  • sloc: python: 54,978; xml: 1,137; makefile: 45; sh: 35
file content (96 lines) | stat: -rw-r--r-- 2,740 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-

# Copyright 2013 Jaap Karssenberg <jaap.karssenberg@gmail.com>

import gtk
import gobject
import logging

import zim.datetimetz as datetime

from zim.notebook import Path
from zim.gui.widgets import Dialog, BrowserTreeView, ScrolledWindow


logger = logging.getLogger('zim.gui.dialogs')


class RecentChangesDialog(Dialog):

	def __init__(self, ui):
		Dialog.__init__(self, ui, _('Recent Changes'), # T: Dialog title
			buttons=gtk.BUTTONS_CLOSE,
			defaultwindowsize=(400, 300)
		)

		self.treeview = RecentChangesTreeView(ui)
		self.vbox.add(ScrolledWindow(self.treeview))

		self.update()
		self.ui.notebook.connect_after('stored-page', lambda *a: self.update())

	def update(self):
		model = self.treeview.get_model()
		model.clear()
		list = self.ui.notebook.index.list_recent_pages(offset=0, limit=50)
		for indexpath in list:
			model.append((indexpath.name, indexpath.contentkey)) # HACK !



class RecentChangesTreeView(BrowserTreeView):

	NAME_COL = 0
	MODIFIED_COL = 1

	def __init__(self, ui):
		model = gtk.ListStore(str, str)
			# NAME_COL, MODIFIED_COL
		BrowserTreeView.__init__(self, model)
		self.ui = ui

		cell_renderer = gtk.CellRendererText()

		column = gtk.TreeViewColumn(_('Page'), cell_renderer, text=self.NAME_COL) # T: Column header
		column.set_sort_column_id(self.NAME_COL)
		column.set_expand(True)
		self.append_column(column)

		today = datetime.date.today()
		yesterday = today - datetime.timedelta(days=1)
		def render_date(col, cell, model, i):
			utime = model.get_value(i, self.MODIFIED_COL)
			if utime:
				utime = float(utime)
				dt = datetime.datetime.fromtimestamp(utime)
				date = dt.date()
				if date == today:
					text = _('Today') + datetime.strftime(' %H:%M', dt)
					# T: label for modified time
				elif date == yesterday:
					text = _('Yesterday') + datetime.strftime(' %H:%M', dt)
					# T: label for modified time
				elif date.year == today.year:
					text = datetime.strftime('%a %d %b %H:%M', dt) # TODO allow config for format ?
				else:
					text = datetime.strftime('%a %d %b %Y %H:%M', dt) # TODO allow config for format ?
			else:
				text = ''

			cell.set_property('text', text)

		cell_renderer = gtk.CellRendererText()
		#cell_renderer.set_property('font', 'mono')
		column = gtk.TreeViewColumn(_('Last Modified'), cell_renderer, text=self.MODIFIED_COL) # T: Column header
		column.set_cell_data_func(cell_renderer, render_date)
		column.set_sort_column_id(self.MODIFIED_COL)
		self.append_column(column)


		self.connect('row-activated', self._do_open_page)

	def _do_open_page(self, view, path, col):
		page = Path( self.get_model()[path][self.NAME_COL].decode('utf-8') )
		self.ui.notebook.flush_page_cache(page)
		self.ui.open_page(page)