Subversion Repositories vs2008

Rev

Blame | Last modification | View Log | RSS feed

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Windows.Forms;
using com.jamiehill;

namespace OneNoteTime {
    class Program {
        static void Main(string[] args) {
            JPHOneNoteUtilities onp = new JPHOneNoteUtilities();

            string message = "";
            int totalSecs = 0;

            //string sectionId = DateTime.Now.Year.ToString();
            //XmlNodeList snl = GetSectionPages(ref sectionId, onp);
            
            // Contains 2-digit month
            string monthId = String.Format("{0:0#}", DateTime.Now.AddMonths(-1).Month);

            XmlNodeList[] snl = GetSectionPages(ref monthId, onp);
            int i = 0;
            for (i = 0; i < snl.Length; i++) {
                FindPages(snl[i], ref message, ref totalSecs, onp);
            }

            monthId = String.Format("{0:0#}", DateTime.Now.Month);
            snl = GetSectionPages(ref monthId, onp);
            for (i = 0; i < snl.Length; i++) {
                FindPages(snl[i], ref message, ref totalSecs, onp);
            }

            string sectionId = "Current";
            snl = GetSectionPages(ref sectionId, onp);
            for (i = 0; i < snl.Length; i++) {
                FindPages(snl[i], ref message, ref totalSecs, onp);
            }

            message += "\nTotal: " + String.Format("{0:#.##}", totalSecs / 3600.00) + " hours\n";
            MessageBox.Show(message, "Time Tracking", MessageBoxButtons.OK, MessageBoxIcon.None);
        }

        static public void FindPages(XmlNodeList snl, ref string message, ref int totalSecs, JPHOneNoteUtilities onp) {
            int dow = (int)DateTime.Now.DayOfWeek;
            Hashtable days = new Hashtable();

            string mydate = "";
            string tmp = "";
            for (int i=0; i<= dow; i++) {
                DateTime dt = DateTime.Now.Subtract(System.TimeSpan.FromDays(dow - i));

                mydate = String.Format("{0:####}", dt.Year) + String.Format("{0:0#}", dt.Month) + String.Format("{0:0#}", dt.Day);

                tmp += mydate + "\n";
                days.Add((string)mydate, 1);
            }

            int tp = snl.Count;
            for (int i = 0; i < tp; i++) {
                string pageDate = snl[i].Attributes.GetNamedItem("name").Value;

                if (!Regex.IsMatch(pageDate, @"\d{8}")) continue;
                if (!days.ContainsKey((string)pageDate)) continue;

                string pageId = snl[i].Attributes.GetNamedItem("ID").Value;
                OneNoteXMLObject ponxo = onp.GetOneNoteXMLObjectById(onp.GetNodeSet("Page"), ref pageId);
                string page = onp.GetPageContent(ponxo.id);

                XmlDocument xdp = null;
                XmlNamespaceManager nsmgrp = null;
                onp.LoadXmlDoc(ref page, out xdp, out nsmgrp);

                XmlNodeList pnl = xdp.GetElementsByTagName("one:T");

                ParsePagePanels(ref pnl, ref pageDate, ref message, ref totalSecs);
            }
        }

        static public XmlNodeList[] GetSectionPages(ref string sectionId, JPHOneNoteUtilities onp) {
            OneNoteXMLObject[] sonxo = onp.GetOneNoteXMLObjectsByPage(onp.GetNodeSet("Section"), ref sectionId);
            
            int count = sonxo.Length;
            XmlNodeList[] snl = new XmlNodeList[count];
            for (int i = 0; i < count; i++) {
                string section = onp.GetPageContent(sonxo[i].id);

                XmlDocument xds = null;
                XmlNamespaceManager nsmgrs = null;
                onp.LoadXmlDoc(ref section, out xds, out nsmgrs);

                snl[i] = xds.GetElementsByTagName("one:Page");
            }
            
            return (snl);
        }

        static public void ParsePagePanels(ref XmlNodeList pnls, ref string pageDate, ref string message, ref int totalSecs) {
            int seconds = 0;
            int te = pnls.Count;
            int lfh = -1;
            string oe = null;
            for (int j = 0; j < te; j++) {
                string e = pnls[j].InnerXml;
                // 7:35-10:00
                if (Regex.IsMatch(e, @"\d{1,2}:\d{2}-\d{1,2}:\d{2}")) {
                    Regex re = new Regex(@"(?<h1>\d{1,2}):(?<m1>\d{2})-(?<h2>\d{1,2}):(?<m2>\d{2})");
                    MatchCollection m = re.Matches(e);
                    foreach (Match im in m) {
                        int sh = System.Convert.ToInt32(im.Groups["h1"].ToString(), 10);
                        int sm = System.Convert.ToInt32(im.Groups["m1"].ToString(), 10);
                        int fh = System.Convert.ToInt32(im.Groups["h2"].ToString(), 10);
                        int fm = System.Convert.ToInt32(im.Groups["m2"].ToString(), 10);

                        if (lfh != -1) {
                            if (lfh > sh) {
                                MessageBox.Show("Start Hour (" + sh + ") is less than previous start hour (" + lfh + ")\n\nEntries:\n" + oe + "\n" + e, "Time Tracking", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                Environment.Exit(1);
                            }
                        }
                        lfh = fh;

                        int ssecs = (sh * 3600) + (sm * 60);
                        int fsecs = (fh * 3600) + (fm * 60);
                        seconds += fsecs - ssecs;
                    }
                } else if (Regex.IsMatch(e, @"\d{1,2}:\d{2}-")) {
                    Regex re = new Regex(@"(?<h1>\d{1,2}):(?<m1>\d{2})-");
                    MatchCollection m = re.Matches(e);
                    DateTime ct = DateTime.Now;
                    foreach (Match im in m) {
                        int sh = System.Convert.ToInt32(im.Groups["h1"].ToString(), 10);
                        int sm = System.Convert.ToInt32(im.Groups["m1"].ToString(), 10);
                        int fh = ct.Hour;
                        int fm = ct.Minute;

                        if (lfh != -1) {
                            if (lfh > sh) {
                                MessageBox.Show("Start Hour (" + sh + ") is less than previous start hour (" + lfh + ")\n\nEntries:\n" + oe + "\n" + e, "Time Tracking", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                Environment.Exit(1);
                            }
                        }
                        lfh = fh;

                        int ssecs = (sh * 3600) + (sm * 60);
                        int fsecs = (fh * 3600) + (fm * 60);
                        seconds += fsecs - ssecs;
                    }
                }

                oe = e;
            }
            totalSecs += seconds;
            message += pageDate + ": " + String.Format("{0:#.##}", seconds / 3600.00)+" hours\n";
        }
    }
}