Public feed #527192

Untitled

public text v1 · immutable
Raw Edit
·published 2007-06-01 06:21 UTC
rendered paste body
//
// Searcher.cs : Implementation of the Xesam searching interface
//
// Copyright (C) 2007 Arun Raghavan <arunissatan@gmail.com>
//

//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Collections.Generic;
using NDesk.DBus;
using org.freedesktop.DBus;

namespace Beagle {
	namespace Xesam {
                public delegate void HitsAddedMethod (string searchId, int count);

		[Interface("org.freedesktop.xesam.search")]
		public interface ISearcher {
			string NewSession();
			void CloseSession(string s);
			object GetProperty(string s, string prop);
			string NewSearch(string s, string xmlSearch);
			event HitsAddedMethod HitsAdded;
		}

		public class Searcher : ISearcher {
			static private bool Debug = true;
			// Start worrying about threads?
			private int sessionCount = 0;
			private int searchCount = 0;
			private Dictionary<string, Session> sessions = new Dictionary<string, Session>();
			private Dictionary<string, Search> searches = new Dictionary<string, Search>();

			public string NewSession()
			{
				Session session = new Session();
				session.VendorId = "Beagle";
				// XXX: populate this from the Beagle daemon
				//session.VendorVersion = "0";
				session.VendorDisplay = "The Beagle sesktop search tool";
				// XXX: populate fieldnames, extensions
				sessions.Add(Convert.ToString(sessionCount), session);

				if (Debug) 
					Console.Error.WriteLine("NewSession() -- {0}", sessionCount);

				return Convert.ToString(sessionCount++);
			}

			public void CloseSession(string s)
			{
				// XXX: error handling?
				sessions.Remove(s);
				if (Debug) 
					Console.Error.WriteLine("CloseSession() -- {0}", s);
			}

			public object GetProperty(string s, string prop)
			{
                          ...
			}

			public string NewSearch(string s, string xmlQuery)
			{
				Session session = sessions[s];
				string searchId = Convert.ToString(searchCount++);
				Search search;

				search = session.CreateSearch(searchId, xmlQuery);
				//search.HitsAddedHandler += HitsAdded;
				search.HitsAddedHandler += MyHitsAdded;
				searches.Add(searchId, search);

				search.Start();

				if (Debug) 
					Console.Error.WriteLine("NewSearch() -- {0}, {1}, {2}", s, searchId, xmlQuery);

				return searchId;
			}

			private void MyHitsAdded(string s, int count)
			{
				Console.Error.WriteLine("HitsAdded raised: {0} {1}", s, count);
				if (HitsAdded != null)
					HitsAdded(s, count);
				else
					Console.Error.WriteLine("No event to call");
			}
		}
	}
}