~c-franke/do/XesamSearch

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
// DoXesamDriver.cs 
// User: kraut (c.franke@muenchen-mail.de) at 22:23 21.02.2008

using System;
using System.Diagnostics;
using System.Collections.Generic;

using NDesk.DBus;
using org.freedesktop.DBus;

namespace Do.Addins.DoXesam
{		
	public class DoXesamDriver
	{
		public static bool BInterface=false; //if there are beagle field its true	
	   
	     // Xesam Search Object Interface
		[Interface ("org.freedesktop.xesam.Search")]
		public interface ISearcher
		{
			string NewSession ();
			void CloseSession (string session);
			string NewSearch (string ses, string query);
			void StartSearch (string search);
			void CloseSearch (string s);
			uint GetHitCount (string s);
			object[][] GetHitData (string s, uint[] ids, string[] fields);
			object[][] GetHits (string search, uint count);
			object GetProperty (string s, string prop);
			object SetProperty (string s, string prop, object val);
			string[] GetState ();
			
		}
	
		
		/**
		/* Makes a simple Search (userQuery).
		/* param: searchstr - string searchstring / searchkey.
		/* returns: result Dictionary<string,string> - Search results / Hits. Key is the field.
		 **/
		public static List<Dictionary<string,string>> GetSearchResults(string searchstr, uint count)
		{
			//### Field Properties
			// Ive added all Porperties that could be important for Beagle
			// Some of them might be senseless
			// I think i config file would be a good idea :)
			
			List<string> properties = new List<string>();
			//##Beagle Only
			//IMLog
			properties.Add("beagle:speakingto");//aim nr or irc channel
			properties.Add("beagle:speakingto_alias");//chat partner alis   
			properties.Add("beagle:protocol");  //chat protcol: icq, aim,.....
			properties.Add("beagle:client");
			properties.Add("beagle:speakingto_icon");
			//Email
			properties.Add("beagle:stock_mail-replied");
			properties.Add("beagle:stock_mail-open");
			properties.Add("beagle:stock_mail");
			properties.Add("beagle:from_name");
			properties.Add("beagle:to_name");
			properties.Add("beagle:isSent");
			properties.Add("beagle:isAnswered");
			//properties.Add("beagle:folder");  
			properties.Add("beagle:hasAttachments");
			properties.Add("beagle:attachment_title");
			properties.Add("beagle:parent:title");
			//properties.Add("emblem"); 
			
			//##Xesam no IMLog support
			properties.Add("xesam:title");
			properties.Add("xesam:author");
			properties.Add("xesam:creator");
			properties.Add("xesam:width");
			properties.Add("xesam:hight");
			properties.Add("xesam:pageCount");
			properties.Add("xesam:mimeType");
			properties.Add("xesam:url");
			properties.Add("xesam:fileExtension");
			properties.Add("xesam:from");
			properties.Add("xesam:to");
			properties.Add("xesam:subject");
			properties.Add("xesam:isRead");
			properties.Add("xesam:artist");
			properties.Add("xesam:ExactFilename");
			properties.Add("xesam:videoCodec");
			properties.Add("xesam:sourceModified");
			
			try{
				
				ISearcher beagle = GetSearchObject(); //establish Connection to Beagle/Xesan
				string session= beagle.NewSession();  //new search session
				beagle.SetProperty(session, "hit.fields",  properties.ToArray() );
				//Send Query
				string search =beagle.NewSearch(session,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
				"<request xmlns=\"http://freedesktop.org/standards/xesam/1.0/query\"><userQuery>"+searchstr+"</userQuery></request>");
				
				beagle.StartSearch(search); //Start Search
				object[][] result=beagle.GetHits(search,count); //get results
				List<Dictionary<string,string>> LResults=new List<Dictionary<string,string>>();
				int BFieldCount=0;
				foreach(object[] entry in result)
				{
					Dictionary<string,string> DHitFields = new Dictionary<string,string>();
					int i =0;
					foreach(object field in entry)
					{
						DHitFields.Add(properties[i], (string)field);
						
						if(properties[i].StartsWith("beagle")&(string)field !="")
							BFieldCount++;
						i++;
					}
					LResults.Add(DHitFields);
				}
				beagle.CloseSession(search); //close search session
				if(BFieldCount>0)
					DoXesamDriver.BInterface=true;
					
	 			return LResults;

			}catch (Exception e){
				Console.WriteLine("ctor->Exception {0}", e);
				return null;
			}
		}
		
		/**
		/* New Xesam/Beagle search object.
		/* returns: searchobj ISearcher - Xesam Search Object.
		 **/
		public static ISearcher GetSearchObject()
		{
			const string ObjectPath = "/org/freedesktop/xesam/searcher/main";
			const string ServiceBusName = "org.freedesktop.xesam.searcher";
			try {
				return Bus.Session.GetObject<ISearcher>(ServiceBusName, new ObjectPath (ObjectPath));
			} catch {
				return null;
			}
		}
		
		/**
		/* Gnome-Do BeagleSearch Plugin requires that XesamAdaptor is running.
		/* If Not following methods should start it.
		 * FIX ME: Doesent work, its hard to identifiy 
		 *          mono --dedub=BaegleXesam.exe
		 * Solution: using cosole tools like ps ux:
		 *            1. Process.Start(ps ux)
		 *            2.   Regex() XesamBeagle.exe
		 *            3. Split(" ")  second element has pid 
		 **/
		public static bool InstanceIsRunning
		{
			get {
				Process pidof;
				
				try {
					//FIX ME: IT doesnt work, no own prozess name
					// for beagle-xesam-adaptor. pid name = mono --debug /usr/local/lib/beagle-xesam/XesamAdaptor.exe
					
					//**strange
					// Use pidof command to look for pidgin process. Exit
					// status is 0 if at least one matching process is found.
					// If there's any error, just assume some Purple client
					// is running.
					pidof = Process.Start ("pidof", "XesamAdaptor.exe"); //beagle-xesam-adaptor
					pidof.WaitForExit (); 	
					return pidof.ExitCode == 0;
				} catch {
					return true;
				}
			}
		}

		public static void StartIfNeccessary ()
		{
			//FIX ME: bragle-xesa-adaptor is not starting.			
			if (!InstanceIsRunning) {
				Process.Start ("beagle-xesam-adaptor"); //Funktioniert nicht
				System.Threading.Thread.Sleep (4 * 1000);
			}
		}
		
		public static void Main(String[] args){
		}
		
	}
}