using Gtk; using System; using System.Collections; using System.Text.RegularExpressions; using Tomboy; using Mono.Posix; using Nwc.XmlRpc; public class BlogPlugin : NotePlugin { Gtk.Widget toolbar_item; static GConf.Client gconfClient = new GConf.Client (); protected override void Initialize () { // Do nothing. } protected override void Shutdown () { if (toolbar_item != null) { Window.Toolbar.Remove (toolbar_item); toolbar_item = null; } } protected override void OnNoteOpened () { toolbar_item = Window.Toolbar.AppendItem (Catalog.GetString ("Blog"), Catalog.GetString ("Post this note to a Blog"), null, new Gtk.Image (Gtk.Stock.Home, Gtk.IconSize.LargeToolbar), new Gtk.SignalFunc (BlogButtonClicked)); } void BlogButtonClicked () { string key = "/apps/gnome-blog/"; try { bool init_val = (bool) gconfClient.Get (key + "initialized"); Console.WriteLine ("The value is: '{0}'", init_val); if (init_val == true) { string xmlrpc_url = (string) gconfClient.Get (key + "xmlrpc_url"); string url_ending = (string) gconfClient.Get (key + "url_ending"); string blog_username = (string) gconfClient.Get (key + "blog_username"); string blog_password = (string) gconfClient.Get (key + "blog_password"); string blog_protocol = (string) gconfClient.Get (key + "blog_protocol"); string blog_id = (string) gconfClient.Get (key + "blog_id"); if (blog_protocol == "MetaWeblog") { PostToBlog(xmlrpc_url, url_ending, blog_username, blog_password, blog_id); } } } catch (GConf.NoSuchKeyException) { Console.WriteLine ("The value is not set."); // gconfClient.Set (key, "Value"); } } string RemoveTextWrappers(string textcontent) { // Remove the header (and maybe footer whitespace?) text string result = textcontent; try { result = Regex.Replace(result,@"^.*\n",""); } catch (Exception problem) { Console.WriteLine("Error occurred trying to substitute text: {0}", problem.ToString()); } return result; } void PostToBlog (string xmlrpc_url, string url_ending, string blog_username, string blog_password, string blog_id) { // post the note to the blog try { // attempt to post the note to the blog Console.WriteLine ("Posting Note '{0}' to Blog, content is {1}", Note.Title, RemoveTextWrappers(Note.TextContent)); // Console.WriteLine ("XML Content is {0}:\n",Note.XmlContent); Hashtable entryStruct = new Hashtable(); entryStruct.Add("title", Note.Title); entryStruct.Add("description", RemoveTextWrappers(Note.TextContent)); XmlRpcRequest client = new XmlRpcRequest(); client.MethodName = "metaWeblog.newPost"; client.Params.Add(blog_id); client.Params.Add(blog_username); client.Params.Add(blog_password); client.Params.Add(entryStruct); client.Params.Add(true); XmlRpcResponse response = client.Send( xmlrpc_url + url_ending ); Console.WriteLine("Server response.IsFault: {0}", response.IsFault); Console.WriteLine("Server response.FaultString: {0}", response.FaultString); if( response.IsFault ) { MessageDialog md = new MessageDialog(Note.Window, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Close,response.FaultString); md.Run(); md.Destroy(); } } catch(Exception problem) { MessageDialog md = new MessageDialog(Note.Window, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Close,problem.ToString()); md.Run(); md.Destroy(); } } }