DesktopSDK 0.7 - PenData Library (C#) Documentation
ContentsIndexHome
List Packages Example
Examples
using System;
using System.IO;
using System.Threading;
using Livescribe.DesktopSDK.PenComm;
using Livescribe.DesktopSDK.PenComm.Interop;
using Livescribe.DesktopSDK.PenData;
using Livescribe.DesktopSDK.AFP;

namespace ListPackages
{
    class ListPackages
    {
        private static Smartpens smartpens;
        private const string PENCOMM_LOG = "ListPackages.log";
        private const string DATA_FILE = "data.zip";
        private static bool foundPen = false;
        private static bool complete = false;

        static void Main(string[] args)
        {
            System.Console.WriteLine("===============================================================");
            System.Console.WriteLine("List packages (penlet or paper product).");
            System.Console.WriteLine("Display AFD information and penlet data");
            System.Console.WriteLine("===============================================================");

            InitializePenCommLib();

            Thread.Sleep(1000);
            if (!foundPen)
            {
                System.Console.Write("Waiting for pen.");
            }

            // Windows programs should avoid ending while a callback is busy.  We also like to tell 
            // the user that we're still waiting, if we haven't found a smartpen yet.
            while (!complete)
            {
                Thread.Sleep(1000);
                if (!foundPen)
                {
                    System.Console.Write(".");
                }
            }
        }

        // Initialize PenComm library
        private static void InitializePenCommLib()
        {
            smartpens = new Smartpens(true, PENCOMM_LOG);

            // The Smartpens object keeps track of attach event handlers in SmartpenAttachNormalEvent.
            // Create a new callback object for our PenAttachEvent method and register it by adding it 
            // to the list of attach event handlers.
            smartpens.SmartpenAttachNormalEvent += new Smartpens.SmartpenChangeCallback(PenAttachEvent);

            // Search for any already docked pens
            smartpens.Find();
        }

        // This is called when a pen is attached
        private static void PenAttachEvent(Smartpen pen)
        {
            foundPen = true;
            try
            {
                // Refresh packages list
                pen.Packages.Update();

                // Show package data
                foreach (PackageItem packageItem in pen.Packages.Items)
                {
                    System.Console.WriteLine("\n==============================");
                    System.Console.WriteLine("\nPackage: " + packageItem.PackageName + " (" + packageItem.FullPath + ")");

                    // Retrieve the data of the package
                    pen.DataGet(packageItem.PackageName, 0, DATA_FILE);

                    // Anything that comes from the smartpen can be opened as a container.
                    try
                    {
                        Container container = new Container(DATA_FILE);

                        // If this container is a document, we can access the Document member.
                        if (container.IsDocument())
                        {
                            PrintReadAFDInfo(container.Document);
                        }

                        // If this container is a penlet, we can access the PenletData member.
                        if (container.IsPenletData())
                        {
                            PrintPenletData(container.PenletData);
                        }

                        container.Close();
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine("Exception: " + ex.Message, "Error");
                    }

                    File.Delete(DATA_FILE);
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Exception: " + ex.Message, "Error");
            }
            finally
            {
                complete = true;
            }
        }

        // Print AFD information
        private static void PrintReadAFDInfo(Document document)
        {
            string afdVersion;
            if (document.DocumentInfo.GetAFDVersion(out afdVersion))
            {
                Console.Out.WriteLine("AFD Version: " + afdVersion);
            }

            string guid;
            if (document.DocumentInfo.GetGUID(out guid))
            {
                Console.Out.WriteLine("GUID: " + guid);
            }

            string author;
            if (document.DocumentInfo.GetAuthor(out author))
            {
                Console.Out.WriteLine("Author: " + author);
            }

            Console.Out.WriteLine("Number of pages: " + document.GetNumberOfPages());
        }

        // Print the penlet data files
        private static void PrintPenletData(PenletData penletData)
        {
            foreach (PenItem penItem in penletData)
            {
                foreach (AppItem appItem in penItem)
                {
                    foreach (InstanceItem instance in appItem.InstanceItems)
                    {
                        foreach(LSFile file in instance)
                        {
                            PrintFile(file, "");
                        }
                    }
                }
            }
        }

        // Print the director/file structure.
        private static void PrintFile(LSFile file, string spacing)
        {
            if (file.Exists())
            {
                Console.Write(spacing);
                if (file.IsDirectory())
                {
                    Console.Out.WriteLine("[Directory] " + file.GetFileName());
                    foreach (LSFile f in file)
                    {
                        PrintFile(f, spacing + "  ");
                    }
                }
                else
                {
                    Console.Out.WriteLine("[File     ] " + file.GetFileName());
                }
            }
        }
    }
}
Created with a commercial version of Doc-O-Matic. In order to make this message disappear you need to register this software. If you have problems registering this software please contact us at support@toolsfactory.com.
Copyright © 2010 Livescribe, Inc. All rights reserved.