| ||
|
The most important part of the .NET tools for Pocket PC is the SDP (Smart Device Programmability) and .NET CF (Compact Framework) included in Visual Studio .NET 2003. But for the developer of Internet-enabled applications, the product IP*Works .NET Compact Framework Edition could be a valuable addition to the toolbox. NNTP and Online Community NNTP (Network News Transfer Protocol) is the protocol used by the newsgroups of Usenet, mostly referred to simply as "newsgroups". In the huge amount of newsgroups a large part of the community activity goes on. People ask questions and get answers from other people. There are a number of advantages with newsgroups over simple mailing, and one example is the ability to follow multilevel threads of communication. Another is the fact that newsgroups are not pushed to your inbox, instead you have to access newsgroups with a dedicated newsgroup reader (like Microsoft's Outlook Express on PCs). An interesting part of newsgroups are individuals who dedicate a large amount of their free time to help out and answer questions. Each year Microsoft selected a number of those individuals – those that have made valuable contributions to the Microsoft software-related community – to be MVP (Most Valuable Professionals). This article and sample code is dedicated to all the wonderful people, MVPs and others, that follow their heart in helping out – thank you all! Wouldn't it be nice if that community could be extended to anyplace? Let's look at a sample application for connected Pocket PCs that show how that can happen. Newsreader Anyplace Sample The Newsreader Anyplace sample application is built with SDP, .NET CF and the NNTP component in IP*Works .NET CF Edition. The purpose of the sample is to show how to create a simple reader of Usenet newsgroups. The application consists of one form: ![]() Newsreader Anyplace sample The first menu option, Demo (in the Menu menu), loads the fifty most recent posts in the Pocket PC newsgroup (microsoft.public.pocketpc) on Microsoft's news server (news.microsoft.com). When any of the topics in the thread is tapped (in the figure above, the "Handheld PC - Pocket PC" topic was tapped) the details (from, date, subject, text) are downloaded and presented in the bottom of the screen. The sample is optimized for an online connection with cost per bytes transferred, and the implementation assures that only the articles selected are downloaded for viewing. To minimize cost further, the downloaded articles could be cached locally. Code Walkthrough After setting the reference to the IP*Works component (nsoftware.IPWorksCF.dll) in the device project in Visual Studio .NET, the NNTP component is created with the following code: nsoftware.IPWorksCF.Nntp nntp = new nsoftware.IPWorksCF.Nntp();The necessary event handlers are created in the form's constructor:
nntp.OnHeader += new
nsoftware.IPWorksCF.Nntp.OnHeaderHandler(nntp_OnHeader);
nntp.OnTransfer += new
nsoftware.IPWorksCF.Nntp.OnTransferHandler(nntp_OnTransfer);
nntp.OnEndTransfer += new
nsoftware.IPWorksCF.Nntp.OnEndTransferHandler(nntp_OnEndTransfer);
Now, let's look at what happens when we tap the Demo menu option:
// Connect to news server
nntp.NewsServer = "news.microsoft.com";
nntp.Connect();
// Select newsgroup
nntp.CurrentGroup = "microsoft.public.pocketpc";
// Get headers
GettingHeaders = true;
tvwThread.BeginUpdate();
int lastArticle = nntp.LastArticle;
int firstArticle = lastArticle - 50;
for (int i = firstArticle; i <= lastArticle; i++)
{
MessageID = "";
Subject = "";
References = "";
nntp.CurrentArticle = i.ToString();
try
{
nntp.FetchArticleHeaders();
TreeNode nod = new TreeNode(Subject);
nod.Tag = MessageID;
ParentNode = null;
try
{
string ParentID = References.Substring(
References.LastIndexOf("<"));
FindNode(ParentID);
}
catch {}
if (ParentNode != null)
ParentNode.Nodes.Add(nod);
else
tvwThread.Nodes.Add(nod);
}
catch {}
}
tvwThread.EndUpdate();
GettingHeaders = false;
The code start off by connecting to the news server and selecting the newsgroup. The loop to get the
headers start by setting the current article number of the NNTP object (CurrentArticle attribute).
Each header is fetched using a call to the FetchArticleHeaders method that generates an OnHeader
event (see below) that set the MessageID, Subject and References variables. A node is added to the
TreeView controls with the Subject as the label and the MessageID as the node tag (Tag attribute).
Before the node is added (as a root node), the FindNode function (see below) is used with the
References variable (contains message ID of parent node) to search the TreeView for any existing
parent nodes. If a parent is found, the node is added as a sub-node to that parent creating a message
thread structure.
The code for setting the header values in the OnHeader event looks like this:
switch (e.Field)
{
case "Message-ID":
MessageID = e.Value;
break;
case "Subject":
Subject = e.Value;
break;
case "References":
References = e.Value;
break;
}
To recursively search a TreeView control the FindNode (and FindRecursive helper) function is used:
private void FindNode(string tag)
{
TreeNodeCollection nodes = tvwThread.Nodes;
foreach (TreeNode nod in nodes)
{
FindRecursive(nod, tag);
}
}
private void FindRecursive(TreeNode treeNode, string tag)
{
if (ParentNode == null)
{
if (treeNode.Tag.ToString() == tag)
ParentNode = treeNode;
else
foreach (TreeNode nod in treeNode.Nodes)
{
FindRecursive(nod, tag);
}
}
}
If the parent is found the ParentNode variable is set and the recursive calls end.
When one of the nodes in the TreeView is tapped, the AfterSelect event is fired that contains the following code: ArticleText = ""; nntp.CurrentArticle = e.Node.Tag.ToString(); nntp.FetchArticleHeaders(); nntp.FetchArticleBody();One again, the header information is fetched (the call to the FetchArticleHeaders method that generates OnHeader events), but this time the "From" and "Date" fields are retrieved and presented in the form. When the FetchArticleBody method is called, a number of OnTransfer events are generated with the following code:
ArticleText += e.Text;
if (e.EOL)
if (ArticleText.Length > 0)
ArticleText += "\r\n";
Each event supply a part of the message text, and the EOL attribute of the event arguments indicates
that this text is the end of a line. That's why a carriage return and a linefeed is added to the
article text.
The OnTransfer events are followed by a final OnEndTransfer event with the code to present the article text on the form: txtArticle.Text = ArticleText;If the user wants to interrupt the current activity (getting headers or article text), the Stop menu option calls the Interupt method of the NTTP object, and the Exit menu option calls the Disconnect method before closing the form. For a complete example, please download this article's sample source code. Conclusion The true power of mobile devices such as the Pocket PC will be unleashed as they become connected. The power of the Internet and its standards can then be exploited by new applications that take advantage of the built-in support for those standards. With tools like IP*Works the support is extended to include even more standards, and they open up the possibility for even more interesting Pocket PC applications. Any comments? |
||||||||||||||||||||||||||
| ©2001-2009 Christian Forsberg & Andreas Sjöström |
||