context: 
  using System;
  using System.Collections.Generic;
  #if ENABLE_DBUS
  using DBus;
  using org.freedesktop.DBus;
  #endif
  
  using Tomboy.Notebooks;
  
  namespace Tomboy
  {
  	public delegate void RemoteDeletedHandler (string uri, string title);
  	public delegate void RemoteAddedHandler (string uri);
  	public delegate void RemoteSavedHandler (string uri);
  #if ENABLE_DBUS
  	[Interface ("org.gnome.Tomboy.RemoteControl")]
  #endif
  	public class RemoteControl : MarshalByRefObject, IRemoteControl
  	{
  		private NoteManager note_manager;
  
  		public RemoteControl (NoteManager mgr)
  		{
  			note_manager = mgr;
  			note_manager.NoteDeleted += OnNoteDeleted;
  			note_manager.NoteAdded += OnNoteAdded;
  			note_manager.NoteSaved += OnNoteSaved;
  		}
  
  		//Convert System.DateTime to unix timestamp
  		private static long UnixDateTime(DateTime d)
  		{
  			long epoch_ticks = new DateTime (1970,1,1).Ticks;
  			//Ticks is in 100s of nanoseconds, unix time is in seconds
  			return (d.ToUniversalTime ().Ticks - epoch_ticks) / 10000000;
  		}
  
  		public string Version ()
  		{
  			return Defines.VERSION;
  		}
  
  		public bool DisplayNote (string uri)
  		{
  			Note note;
  
  			note = note_manager.FindByUri (uri);
  			if (note == null)
  				return false;
  
  			note.Window.Present ();
  			return true;
  		}
  
  		public bool HideNote (string uri)
  		{
  			Note note;
  
  			note = note_manager.FindByUri (uri);
  			if (note == null)
  				return false;
  
  			note.Window.Hide ();
  			return true;
  		}
  
  		public bool DisplayNoteWithSearch (string uri, string search)
  		{
  			Note note;
  
  			note = note_manager.FindByUri (uri);
  			if (note == null)
  				return false;
  
  			note.Window.Present ();
  
  			// Pop open the find-bar
  			NoteFindBar find = note.Window.Find;
  			find.ShowAll ();
  			find.Visible = true;
  			find.SearchText = search;
  
  			return true;
  		}
  
  		public string FindNote (string linked_title)
  		{
  			Note note = note_manager.Find (linked_title);
  			return (note == null) ? String.Empty : note.Uri;
  		}
  
  		public string FindStartHereNote ()
  		{
  			Note note = note_manager.FindByUri (NoteManager.StartNoteUri);
  			return (note == null) ? String.Empty : note.Uri;
  		}
  
  		public string CreateNote ()
  		{
  			if (note_manager.ReadOnly)
  				return string.Empty;
  			try {
  				Note note = note_manager.Create ();
  				note.QueueSave (ChangeType.ContentChanged);
  				return note.Uri;
  			} catch {
  				return string.Empty;
  			}
  		}
  
  		public string CreateNamedNote (string linked_title)
  		{
  			if (note_manager.ReadOnly)
  				return string.Empty;
  			Note note;
  
  			note = note_manager.Find (linked_title);
  			if (note != null)
  				return string.Empty;
  
  			try {
  				note = note_manager.Create (linked_title);
  				note.QueueSave (ChangeType.ContentChanged);
  				return note.Uri;
  			} catch {
  				return string.Empty;
  			}
  		}
  
  		public string CreateNamedNoteWithUri (string linked_title, string uri)
  		{
  			if (note_manager.ReadOnly)
  				return string.Empty;
  			Note note;
  			string guid;
  			try {
  				guid = uri.Replace ("note://tomboy/", "");
  			} catch {
  				return string.Empty;
  			}
  
  			if (String.IsNullOrEmpty (guid))
  				return string.Empty;
  
  			note = note_manager.Find (linked_title);
  			if (note != null)
  				return string.Empty;
  
  			note = note_manager.FindByUri (uri);
  			if (note != null)
  				return string.Empty;
  
  			try {
  				note = note_manager.CreateWithGuid (linked_title, guid);
  				note.QueueSave (ChangeType.ContentChanged);
  				return note.Uri;
  			} catch {
  				return string.Empty;
  			}
  		}
  
  		public bool DeleteNote (string uri)
  		{
  			if (note_manager.ReadOnly)
  				return false;
  			Note note;
  
  			note = note_manager.FindByUri (uri);
  			if (note == null)
  				return false;
  
  			note_manager.Delete (note);
  			return true;
  		}
  
  		public void DisplaySearch ()
  		{
  			NoteRecentChanges.GetInstance (note_manager).Present ();
  		}
  
  		public void DisplaySearchWithText (string search_text)
  		{
  			NoteRecentChanges recent_changes =
  			        NoteRecentChanges.GetInstance (note_manager);
  			if (recent_changes == null)
  				return;
  
  			recent_changes.SearchText = search_text;
  			recent_changes.Present ();
  		}
  
  		public bool NoteExists (string uri)
  		{
  			Note note = note_manager.FindByUri (uri);
  			return note != null;
  		}
  
  		public string[] ListAllNotes ()
  		{
  			List<string> uris = new List<string> ();
  			foreach (Note note in note_manager.Notes) {
  				uris.Add (note.Uri);
  			}
  			return uris.ToArray ();
  		}
  
  		public string GetNoteContents (string uri)
  		{
  			Note note;
  			note = note_manager.FindByUri (uri);
  			if (note == null)
  				return string.Empty;
  			return note.TextContent;
  		}
  
  		public string GetNoteTitle (string uri)
  		{
  			Note note;
  			note = note_manager.FindByUri (uri);
  			if (note == null)
  				return string.Empty;
  			return note.Title;
  		}
  
  		public long GetNoteCreateDate (string uri)
  		{
  			Note note;
  			note = note_manager.FindByUri (uri);
  			if (note == null)
  				return -1;
  			return UnixDateTime (note.CreateDate);
  		}
  
  		public long GetNoteChangeDate (string uri)
  		{
  			Note note;
  			note = note_manager.FindByUri (uri);
  			if (note == null)
  				return -1;
  			return UnixDateTime (note.MetadataChangeDate);
  		}
  
  		public string GetNoteContentsXml (string uri)
  		{
  			Note note;
  
gt: 			note = note_manager.FindByUri (uri);