~seif/zeitgeist/tomboy

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
using System;
using Gtk;
using Tomboy;
using NDesk.DBus;
using Gdk;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using Mono.Posix;
using Pango;
using Gnome;
using GLib;

namespace Tomboy.UsedWith {
	
	[NDesk.DBus.Interface ("org.gnome.zeitgeist.Log")]
	interface ILogger {
		string[] FindRelatedUris(long[] timerange, ZGEvent[] events, ZGEvent[] results, int storage, int numEvents, int resultType);
		ZGEvent[] FindEvents(long[] timerange, ZGEvent[] events, int storage, int numEvents, int resultType);
	}
	public struct ZGEvent {
		public string[] metadata { get; set; }
		public string[][] subjects { get; set; }
		public byte[] payload { get; set; } 
		
		public ZGEvent(string[] metadata, string[][] subjects, byte[] payload)
		{
			this.metadata = metadata;
			this.subjects = subjects;
			this.payload = payload;
		}
	}                  

	public class UsedWithWindow : Gtk.Window
	{
		private ZGEvent[] events;
		private ListStore treeStore;
		private static Dictionary<string, string> INTERP_PARENT = new Dictionary<string, string>()
		{
			{"http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Document","http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Document"},
			{"http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Presentation","http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Document"},
			{"http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#TextDocument","http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Document"},
			{"http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#PaginatedTextDocument","http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Document"},
			{"http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#PlainTextDocument","http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Document"},
			{"http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#SourceCode","http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Document"},
			{"http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#HtmlDocument","http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Document"},
			
			{"http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Audio","http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Audio"},
			{"http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#MusicPiece","http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Audio"},
			
			{"http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Image","http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Image"},
			{"http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#VectorImage","http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Image"},
			{"http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#RasterImage","http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Image"},
			
			{"http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Video","¶http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Video"},
		};
		
		private static Dictionary<string, string> INTERP_ICON = new Dictionary<string, string>()
		{
		};
		
		string EscapeFileName (string filename)
		{
			foreach (char c in filename) {
				if ((int)c < 32 || (int)c > 127)
					return null;
			}
			return ConvertFileNameToVFS (filename);
		}
		
		static string ConvertFileNameToVFS (string fileName)
		{
			string result = fileName;
			result = result.Replace ("%", "%25");
			result = result.Replace ("#", "%23");
			result = result.Replace ("?", "%3F");
			return result;
		}
		
		
		public UsedWithWindow(string name, ZGEvent[] events) : base("Used with... "+ name)
		{
			this.events = events;
			TreeView tree = new TreeView ();
			treeStore = new ListStore (typeof (Gdk.Pixbuf), 	typeof (string),  typeof (string), typeof(ZGEvent));		 
			
			Gtk.CellRendererText cell = new Gtk.CellRendererText();	
			cell.Ellipsize = Pango.EllipsizeMode.End;			
			
			tree.AppendColumn ("Icon", new Gtk.CellRendererPixbuf(), "pixbuf", 0);  
			tree.AppendColumn ("File Name", cell, "text", 1);
			
			Gtk.Viewport viewport = new Gtk.Viewport();
			Gtk.ScrolledWindow s = new Gtk.ScrolledWindow();
			s.Add(viewport);
			viewport.Add(tree);
			
	 		tree.Model = treeStore;
			tree.HeadersVisible = false;
			this.SetSizeRequest(300, 200);
			loadContent();
			this.Add (s);
			this.ShowAll();
			
		}
		
		private string convertToReadable(string uri)
		{
			uri = uri.Replace("file:///","/");
			uri = uri.Replace("%20"," ");
			return uri;
		}
		
		private void loadContent()
		{	
			Gtk.IconTheme IconTheme = new Gtk.IconTheme ();
			Dictionary<string, List<ZGEvent>> intrpDict = new Dictionary<string, List<ZGEvent>>();
			foreach (ZGEvent e in events)
			{	
				/**
				string intrp = e.subjects[0][1];
				if (intrpDict.ContainsKey(e.subjects[0][1]) == false)
				{
					intrpDict.Add(intrp, new List<ZGEvent>());
					Console.WriteLine(intrp);
				}
				intrpDict[intrp].Add(e);
				**/
				string intrp = e.subjects[0][1];
				string uri = convertToReadable(e.subjects[0][0]);
				string name = e.subjects[0][5];
				Gdk.Pixbuf icon = FileIconLoader.GetPixbufForFile(uri, 24);
				treeStore.AppendValues(icon, name, e);		
			}
		}
	}
	
	public class UsedWithNoteAddin : NoteAddin
	{
		Gtk.MenuItem item;
		Note note;
		
		private static ILogger zeitgeist_proxy = 
			Bus.Session.GetObject<ILogger>("org.gnome.zeitgeist.Engine", 
			                               new ObjectPath("/org/gnome/zeitgeist/log/activity"));
		
		public override void Initialize ()
		{
			note = Note;
		}

		public override void Shutdown ()
		{
			if (item != null)
				item.Activated -= OnMenuItemActivated;
		}

		public override void OnNoteOpened ()
		{
			// Add the menu item when the window is created
			item = new Gtk.MenuItem (
				Catalog.GetString ("Used with..."));
			item.Activated += OnMenuItemActivated;
			item.AddAccelerator ("activate", Window.AccelGroup,
				(uint) Gdk.Key.u, Gdk.ModifierType.ControlMask,
				Gtk.AccelFlags.Visible);
			item.Show ();
			AddPluginMenuItem (item);
		}

		void OnMenuItemActivated (object sender, EventArgs args)
		{
			Console.WriteLine(note.Uri);
			getUsedWith(note.Uri);
		}
		
		private ZGEvent createEvent(string uri)
		{
			ZGEvent e = new ZGEvent();
			e.metadata = new string[5];
			e.metadata[0] = ""; //id (filled in by Zeitgeist)
			e.metadata[1] = "";
			e.metadata[2] = "";
			e.metadata[3] = "";
			e.metadata[4] = "";
			string[] subject = new string[7];
			subject[0] = uri; //uri
			subject[1] = ""; 
			subject[2] = "";  
			subject[3] = ""; //origin
			subject[4] = ""; //mimetype
			subject[5] = ""; //text
			subject[6] = ""; //storage id
			e.subjects = new string[][] { subject };
			e.payload = new byte[0];
			return e;
		}
		
		private string convertToReadable(string uri)
		{
			uri = uri.Replace("file:///","/");
			uri = uri.Replace("%20"," ");	
			return uri;
		}
		
		private void getUsedWith(string uri)
		{
			Console.WriteLine("*** "+uri);
			
			ZGEvent[] events = new ZGEvent[]{createEvent(uri)};
			long[] timestamps = new long[]{0,9999999999999};
			string[] results = zeitgeist_proxy.FindRelatedUris(timestamps, events, new ZGEvent[0], 2, 99, 1);
		
			List<ZGEvent> eventList = new List<ZGEvent>();
			for (int i=0; i<results.Length; i++)
			{	
				Console.WriteLine(results[i]);
				string x = convertToReadable(results[i]);
				if (System.IO.File.Exists (x)) eventList.Add(createEvent(results[i]));
				if (i==10) break;
			}
			Console.WriteLine(eventList.Count);
			events = eventList.ToArray();
			if (events.Length > 0)	events = zeitgeist_proxy.FindEvents(timestamps, events, 2, 99, 4);
			createWindow(note.Title, events);
		}
		
		private void createWindow(string name, ZGEvent[] events)
		{
			Console.WriteLine(name);
			UsedWithWindow window = new UsedWithWindow(name, events);
		}
		
		public static void Main(string[] args)
		{
		}
	}

	public class FileIconLoader
      {
            static Gdk.Pixbuf defaultIcon;
            static Gnome.ThumbnailFactory thumbnailFactory;
            static Hashtable iconHash;

            static FileIconLoader ()
            {
                  thumbnailFactory = new Gnome.ThumbnailFactory (Gnome.ThumbnailSize.Normal);
                  iconHash = new Hashtable ();
            }

            private FileIconLoader ()
            {
            }

            public static Gdk.Pixbuf DefaultIcon {
                  get {
                        if (defaultIcon == null)
                              defaultIcon = new Gdk.Pixbuf (typeof(FileIconLoader).Assembly, "gnome-fs-regular.png");
                        return defaultIcon;
                  }
            }

            // FIXME: is there a GTK replacement for Gnome.Icon.LookupSync?
            public static Gdk.Pixbuf GetPixbufForFile (string filename, int size)
            {
                  if (filename == "Documentation") {
                        return GetPixbufForType ("gnome-fs-regular", size);
                  } else if (Directory.Exists (filename)) {
                        return GetPixbufForType ("gnome-fs-directory", size);
                  } else if (System.IO.File.Exists (filename)) {
                        foreach (char c in filename) {
                              // FIXME: This is a temporary workaround. In some systems, files with
                              // accented characters make LookupSync crash. Still trying to find out why.
                              if ((int)c < 32 || (int)c > 127)
                                    return GetPixbufForType ("gnome-fs-regular", size);
                        }
                        filename = filename.Replace ("%", "%25");
                        filename = filename.Replace ("#", "%23");
                        filename = filename.Replace ("?", "%3F");
                        string icon = null;
                        try {
                              Gnome.IconLookupResultFlags result;
                              icon = Gnome.Icon.LookupSync (Gnome.IconTheme.Default, thumbnailFactory, filename, null, Gnome.IconLookupFlags.None, out result);
                        } catch {}
                        if (icon == null || icon.Length == 0)
                              return GetPixbufForType ("gnome-fs-regular", size);
                        else
                              return GetPixbufForType (icon, size);
                  }

                  return GetPixbufForType ("gnome-fs-regular", size);
            }

            public static Gdk.Pixbuf GetPixbufForType (string type, int size)
            {
                  // FIXME: is caching these really worth it?
                  // we have to cache them in both type and size
                  Gdk.Pixbuf bf = (Gdk.Pixbuf) iconHash [type+size];
                  if (bf == null) {
                        try {
                              bf = Gnome.IconTheme.Default.LoadIcon (type, size, (Gtk.IconLookupFlags) 0);
                        }
                        catch {
                              bf = DefaultIcon;
                              if (bf.Height > size)
                                    bf = bf.ScaleSimple (size, size, Gdk.InterpType.Bilinear);
                        }
                        iconHash [type+size] = bf;
                  }
                  return bf;
            }
      }
}