Cross-Platform Network Applications with Mono

Curious about .NET? Try out this useful sample app that exercises the GUI and XML-RPC features of Mono.

with GTKWidgetType being replaced by the actual object type concerned, and widgetname being the name of the widget as defined in the Glade file. After Autoconnect() returns, these widgets can be used as if they had been created in the program itself.

Retrieving Old Entries

When the program loads, the first action it performs is to query the Weblog and download the recent posts, displaying them in the TreeView widget. The method getRecentPosts() in the MonoBlog class handles this; it is called in the main constructor if preferences exist, as the method needs to know about the Weblog it is contacting. The MetaWeblog API provides a function call, metaweblog.getRecentPosts, that returns a specified number of old posts or as many as it can find if fewer posts exist than we desire.

The communication with the Weblog is straightforward:

XmlRpcRequest client = new XmlRpcRequest();
client.MethodName = "metaWeblog.getRecentPosts";
client.Params.Add(BlogID);
client.Params.Add(ServerUser);
client.Params.Add(ServerPass);
client.Params.Add(NumberOfPosts);
XmlRpcResponse response = client.Send(ServerURL);

All that is required is to create a new XmlRpcRequest object, set the required API method name, fill in the necessary arguments and send it to the Weblog. The Weblog then returns a response, in this case an array of posts, which is stored in the Value field of the XmlRpcResponse object. Next, we need to update the GTKTreeView control.

In GTK 2.0 and above, this control uses a model-view-controller approach. Here, then, we create a new model object and pass it to the control:

System.Type[] ListTypes = new System.Type[3];
ListTypes[0] = typeof(string);
ListTypes[1] = typeof(string);
ListTypes[2] = typeof(string);
ListStore store = new ListStore(ListTypes);
treeview1.Model = store;

This model object creates a table with three columns. The ListStore object needs to be passed an array of Type objects; each item in the array corresponds to the type of column. A Weblog post contains three items—a title, the content of the post and a unique identifier—all of which are strings, so here all the columns have a String type. The rest of the method cycles through the array, populating the model:

TreeIter iter = new TreeIter ();
foreach (Hashtable post in results) {
  String title = (String) post ["title"];
  String postid = (String) post ["postid"];
  String description = (String) post ["description"];

  store.Append (out iter);
  store.SetValue (iter, 0, new GLib.Value(title));
  store.SetValue (iter, 1, new GLib.Value(postid));
  store.SetValue (iter, 2,
  new GLib.Value(description));
}

This, by itself, isn't enough to display the titles in the tree. For that, we include some code in the constructor, after the call to getRecentPosts():

TreeViewColumn TitleCol = new TreeViewColumn();
CellRenderer TitleRenderer = new CellRendererText();
TitleCol.AddAttribute (TitleRenderer, "text", 0);
treeview1.AppendColumn (TitleCol);

This adds a new column view to the tree. The AddAtrribute() method is hooked to the title column of the model (the first) with the 0 argument. As all the user requires is to see the title of an entry in the TreeView control; no other column views are needed. The information, though, is stored in the model to make the program more efficient.

Editing Old Posts

When a user clicks on an entry, the desired result is for the program to display the old entry in the text entry portions on the right-hand side of the window. The MetaWeblog API has a method called metaweblog.getPost that pulls posts from the Weblog. As they already have been downloaded in the getRecentPosts() method, the program can get the data from the model instead of communicating with the Weblog again. The row_activated signal is bound to the method SelectOldPost using Glade, so whenever an item is double-clicked, this code runs:

public void SelectOldPost(System.Object obj, EventArgs e) {
  TreeSelection currentSelection = treeview1.Selection;
  TreeIter iter;
  TreeModel model = treeview1.Model;
  currentSelection.GetSelected (out model, out iter);
  String selected = (string) model.GetValue (iter, 1);
  String oldTitle = (string) model.GetValue(iter,0);
  String oldEntry = (string) model.GetValue(iter,2);

  TextBuffer buffer = textview1.Buffer;
  entry1.Text = oldTitle;
  buffer.SetText(oldEntry);

  OldPostID = selected;
  EditingOldPost = true;
}

This method obtains the current selected item in the GTKTreeView control and uses an iterator to index into the model and find the required values. It then fills in the text fields with this information and updates two instance variables that are needed when the user clicks on the Post button. If the program is updating an older entry rather than creating a new entry, it needs to make a different MetaWeblog API call, which needs the unique identifier of the post. The variables OldPostID and EditingOldPost are updated to reflect this.

______________________

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState