~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
// DoXesamDriver.cs 
// User: kraut (c.franke@muenchen-mail.de) at 22:23 21.02.2008
//
//  GNOME Do is the legal property of its developers, whose names are too numerous
//  to list here.  Please refer to the COPYRIGHT file distributed with this
//  source distribution.
//
//  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 3 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, see <http://www.gnu.org/licenses/>.

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using NDesk.DBus;
using org.freedesktop.DBus;
//using GLib;
namespace Do.Addins.DoXesam
{		
//public delegate void DaemonStarted ();
	public class DoXesamDriver
	{
		public static bool BInterface=false; //if there are beagle field its true	
	   	//public static event DaemonStarted DaemonStarted;
	     // 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 ();
			
		}
	
		private static List<string> getProperties(){
			//### Field Properties
			// Ive added many Porperties that could be important for Beagle.
			// Some of them might be senseless
			// BUt some properties are missing like Calendar fields.
			// I think i config file would be a good idea :)
			//bool test = InstanceIsRunning;
			
			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");
			 
			
			//##Xesam no IMLog support
			properties.Add("xesam:title");
			properties.Add("xesam:author");
			properties.Add("xesam:creator");
			properties.Add("xesam:width");
			properties.Add("xesam:height");
			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");
		
            return properties;
		}
		/**
		/* 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)
		{
			
			List<Dictionary<string,string>> LResults=new List<Dictionary<string,string>>();
			try{
				List<string> properties = DoXesamDriver.getProperties();
				ISearcher beagle = GetSearchObject(); //establish Connection to Beagle/Xesan
				if(beagle!=null){				
				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
				
				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++;
					}
                   // DHitFields.Add("error", "");
					LResults.Add(DHitFields);
				}
				beagle.CloseSession(search); //close search session
                
               // Dictionary<string,string> DHitFields2 = new Dictionary<string,string>();
			//	DHitFields2.Add("error", "");
			//	LResults.Add(DHitFields2);

				if(BFieldCount>0)
					DoXesamDriver.BInterface=true;
				
				}else{
					Dictionary<string,string> DHitFields = new Dictionary<string,string>();
					DHitFields.Add("error", "Please start Desktop Search Engine first!");
					LResults.Add(DHitFields);
				}	
	 			return LResults;

			}catch (Exception e){
				Dictionary<string,string> DHitFields = new Dictionary<string,string>();
					DHitFields.Add("error", "Please start Desktop Search Engine first!");
					LResults.Add(DHitFields);
				Console.WriteLine("ctor->Exception {0}", e);
				return LResults;
			}
		}
		
		/**
		/* New Xesam/Beagle search object.
		/* returns: searchobj ISearcher - Xesam Search Object.
		 **/
		private 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;
			}
		}
		
		
		public static void Main(String[] args){
		}
		
	}
}