~gnome-zeitgeist/+junk/new-tag-editor

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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# -.- encoding: utf-8 -.-

import datetime
import time
import os
import gobject
import gtk
import gnome
import gnomedesktop
import gettext
import urllib

from zeitgeist.gui.zeitgeist_util import icon_factory, thumbnailer, launcher, favicons
# Some imports are in-place to avoid a circular dependency


class Data(gobject.GObject):
	
	__gsignals__ = {
		"relate" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
	}
	
	def __init__(self,
				 uri		= None,
				 name		= None,
				 comment	= "",
				 timestamp	= 0,
				 mimetype	= "N/A",
				 icon		= None,
				 tags		= "",
				 count		= 1,
				 use        = "",
				 type		= "N/A",
				 bookmark	= False,
				 app        = "",
				 content    = "",
				 origin	    = ""
				):
		
		gobject.GObject.__init__(self)
		
		# Remove characters that might be interpreted by pango as formatting
		if name:
			name = name.replace("<","")
			name = name.replace(">","")
		
		self.exists = True
		self.uri = uri
		if uri:
			self.uri = uri.replace("%20"," ")
			self.exists = exists(self.uri)
			
		self.name = name
		self.count = count
		self.comment = comment
		self.mimetype = mimetype
		self.use = use
		self.diff = ""
		self.bookmark = bookmark
		self.timestamp = timestamp
		self.app = app
		self.type = type
		self.content = content
		self.icon = icon
		self.tags = tags
		self.thumbnailer = None
		self.original_source = origin
		self.textview = None
		self.icon = self.icon_from_app()
	
	def icon_from_app(self):
		icon = None
		
		if self.type.strip() == "Tomboy Notes":
			return "tomboy"
		elif self.type.strip() == "Videos":
			return "gnome-mime-video"
		elif self.type.strip() == "Audio":
			return "gnome-mime-audio"
		elif self.type.strip() == "Images":
			return "image"
		elif self.type.strip() == "Documents":
			return "stock_new-presentation"
		elif self.type.strip() == "Development":
			return "applications-development"
		elif self.type.strip() == "Firefox History":
			return "gnome-globe"
		else:
			print self.type
		
		'''
		if APP_ICON_CACHE.has_key(self.app):
			return APP_ICON_CACHE[self.app]
		try:
			line = [line for line in open(self.app) if line.startswith("Icon=")]
			if line: 
				icon = line[0].split("=")[1].strip()
				return icon
		except:
			pass
		'''
	
	def get_origin(self):
		return self.original_source

	def get_timestamp(self):
		return self.timestamp
	
	def get_datestring(self):
		return datetime.datetime.fromtimestamp(self.timestamp).strftime("%a %d %b %Y")
	
	def get_use(self):
		return self.use
	
	def get_app(self):
		return self.app
	
	def get_icon(self, icon_size):
		if self.uri:
			if self.exists and self.uri.startswith("file"):
				thumb = thumbnailer.get_icon(self.get_uri(), self.get_mimetype(), icon_size, self.timestamp)
				return thumb
			
			"""
			if self.uri.startswith("http") and not self.comment.strip()=="":
				if self.comment[0] == ".":
					uri = "http://" + self.comment[1:] +"/"
				else: 
					uri = "http://" + self.comment +"/"
				icon =  favicons.get_icon(uri)
				if icon:
					return icon
			"""
			
			# TODO: Fix icon_factory to be able to load favicons
			if self.icon and not self.icon.split("://")[0] in ("http", "https"):
				icon = icon_factory.load_icon(self.icon, icon_size)
				if not self.exists and icon:
					icon = icon_factory.transparentize(icon, 75)
				return icon
			
			
				
			
	
	def open(self):
		if self.get_mimetype() == "x-tomboy/note":
			uri_to_open = "note://tomboy/%s" % os.path.splitext(os.path.split(self.get_uri())[1])[0]
		else:
			uri_to_open = self.get_uri()
		if uri_to_open:
			self.timestamp = time.time()
			launcher.launch_uri(uri_to_open, self.get_mimetype())
	
	def get_icon_string(self):
		return self.icon
	
	def get_icon_static_done(self, icon_size):
		if self.icon:
			return icon_factory.load_icon(self.icon, icon_size)
	
	def get_mimetype(self):
		return self.mimetype
	
	def get_type(self):
		return self.type

	def get_uri(self):
		return self.uri

	def get_name(self):
		return self.name
	
	def get_time(self):
		# TODO: Should it be possible to switch to 12-hour clock ("%l:%M %p")?
		return datetime.datetime.fromtimestamp(self.timestamp).strftime("%H:%M").strip()
	
	def get_comment(self):
		return self.comment
	
	def get_count(self):
		return self.count
	
	def get_use(self):
		return self.use
	
	def get_bookmark(self):
		return self.bookmark
	
	def get_content(self):
		return self.content
	
	def get_tags(self):
		return [tag.strip() for tag in self.tags.split(",") if tag.strip()]
	
	def open_from_timestamp(self):
		"""
		path = difffactory.restore_file(self)
		launcher.launch_uri(path, self.get_mimetype())
		"""
		pass
	
	def populate_popup(self, menu):
		open = gtk.ImageMenuItem (gtk.STOCK_OPEN)
		open.connect("activate", lambda *discard: self.open())
		open.show()
		menu.append(open)
		
		"""
		if self.type=="Documents" or self.type=="Other":
			timemachine = gtk.MenuItem("Open from timestamp")
			timemachine.connect("activate", lambda w: self.open_from_timestamp())
			timemachine.show()
			menu.append(timemachine)
			del timemachine
		"""
		if self.bookmark:
			bookmark = gtk.MenuItem(_("Unbookmark"))
		else:
			bookmark = gtk.MenuItem(_("Bookmark"))
		bookmark.connect("activate", lambda w: self.add_bookmark())
		bookmark.show()
		menu.append(bookmark)
		
		relate = gtk.MenuItem(_("Show related activities"))
		relate.connect("activate", lambda w: self.relate())
		relate.show()
		menu.append(relate)
		
		tag = gtk.MenuItem(_("Edit tags..."))
		tag.connect("activate", lambda w: self.tag_item())
		tag.show()
		menu.append(tag)
		
		# Add a separator (we could use SeparatorMenuItem but that's
		# just an alias for calling MenuItem without arguments).
		tag = gtk.MenuItem()
		tag.show()
		menu.append(tag)
		
		tag = gtk.MenuItem(_("Delete item from Zeitgeist"))
		tag.connect("activate", lambda w: self.delete_item())
		tag.show()
		menu.append(tag)
	
	def relate(self, *discard):
		self.emit("relate")
	
	def add_bookmark(self, *discard):
		self.bookmark = not self.bookmark
		from zeitgeist_engine_wrapper import engine
		engine.update_items((self,))
		from zeitgeist.gui.zeitgeist_bookmarker import bookmarker
		bookmarker.reload_bookmarks()
	
	def set_bookmark(self, bookmark):
		self.bookmark = bookmark
		from zeitgeist_engine_wrapper import engine
		engine.update_items((self,))
		from zeitgeist.gui.zeitgeist_bookmarker import bookmarker
		bookmarker.reload_bookmarks()
	
	def tag_item(self):
			tag_window=TagEditor(self.get_name(),self.tags,self)
			tag_window.window.show_all()
	def delete_item(self):
		from zeitgeist_engine_wrapper import engine
		uri = self.uri.replace(" ","%20")
		engine.delete_items((uri,))
	
	def set_tags(self, tags):
		self.tags = tags
		from zeitgeist_engine_wrapper import engine
		engine.update_items((self,))
	

	
	
	
	def has_search(self, search):
		if search.strip():
			return True
		if self.comment.lower().find(search) > -1:
			return True
		if self.name.lower().find(search) > -1:
			return True
		for tag in self.get_tags():
			if tag == search:
				return True
		return False

sig_plain_data = "(isssssssbssss)"

def objectify_data(item_list):
	return Data(
			timestamp = item_list[0],
			uri = item_list[1],
            name = item_list[2],
			type = item_list[3], # source
			content = item_list[4],
			mimetype = item_list[5],
            tags = item_list[6],
			comment = item_list[7],
			bookmark = item_list[8],
			use = item_list[9] or "",
            icon =  item_list[10],
			app = item_list[11] or "",
			origin = item_list[12],
            )

def plainify_data(obj):
	""" Takes a Data object or a dictionary and converts it into a
		tuple suitable for transmission through D-Bus. """
	
	return (
		int(obj.get_timestamp()),
		obj.get_uri(),
		obj.get_name(),
		obj.get_type(), # source
		obj.get_content(), # content
		obj.get_mimetype(), 
		",".join(obj.get_tags()),
		obj.get_comment(),
		obj.get_bookmark(),
		obj.get_use(),
		obj.get_icon_string() or "",
		obj.get_app(),
		obj.get_origin()
		)

def exists(uri):
	return not uri.startswith("file://") or os.path.exists(
		urllib.unquote(str(uri[7:])))
	
APP_ICON_CACHE = {}
class TagEditor():
		def __init__(self,item_name,tags,data):
			self.data=data
			builder=gtk.Builder()
			self.plain_tags=""
			builder.add_from_file("tageditor.xml") 
			self.tags=tags.split(",")
			
			builder.connect_signals(self)
			self.window = builder.get_object("window")
			self.window.connect("destroy",self.window.hide)
			self.window.set_title(item_name)
			self.label=builder.get_object("label")
			self.label.set_label(_("Tags for ")+item_name)
			self.vbox_tags=builder.get_object("vbox_tags")
			self.entry=builder.get_object("entry")
			
			for i in range(0,len(self.tags)):
				self.populate(self.tags[i],True)
			   
		def populate(self,tag_name,firstrun=False):
			
			tag= Tag(tag_name)
			self.vbox_tags.pack_end(tag)
			tag.show()
			
			tag.set_active(True)
			if firstrun==False:
		 		self.tags.append(unicode(tag_name))
		  		self.prepare_tags()
				self.data.set_tags(self.plain_tags)
			tag.connect("toggled",self.toggle)
		def toggle(self,widget):
				if widget.toggled==True:
					self.tags.remove(unicode(widget.tag_name))
					widget.toggled=False
				else:
					self.tags.append(unicode(widget.tag_name))
					widget.toggled=True
				self.prepare_tags()
				self.data.set_tags(self.plain_tags)
		def prepare_tags(self):
				self.plain_tags=""
				for i in self.tags:
					
					self.plain_tags=self.plain_tags+","+i
		def on_button_add_clicked(self,widget):
			if self.entry.get_text() != "":
				self.populate(self.entry.get_text())
		def on_button_exit_clicked(self,widget):
				self.window.hide()
		
				
		def on_window_key_press_event(self, widget, event):
				if event.keyval ==65293:
					self.on_button_add_clicked(self)
				
				if event.keyval==65535:
					self.on_button_remove_clicked(self)
class Tag(gtk.ToggleButton):
		def __init__(self,tag_name):
				gtk.ToggleButton.__init__(self)
				self.set_label(tag_name)
				self.toggled=True
				self.tag_name=tag_name