Advertisement
parabola949

Set Lync status to busy while programming

Sep 1st, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.19 KB | None | 0 0
  1. using Microsoft.Lync.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading;
  8. // ReSharper disable once FunctionNeverReturns
  9. namespace Monitor_Applications
  10. {
  11.     class Program
  12.     {
  13.         //DO NOT MODIFY THESE
  14.         private const string Busy = "6500";
  15.         private const string Free = "3500";
  16.         private const string DnD = "9500";
  17.  
  18.         //Choose which unavailable status you want to use:
  19.         private const string Unavailable = DnD;
  20.         //What message do you want when you are unavailable?
  21.         private const string StatusMessage = "Programming...";
  22.  
  23.         //What window title are we monitoring for? (this is a contains, not equals)
  24.         private const string WindowTitle = "Microsoft Visual Studio";
  25.  
  26.         //How long must you be using VS to trigger unavailable?
  27.         private const int MinutesInVsTrigger = 5;
  28.         //How long must you be doing other things to trigger available?
  29.         private const int MinutesOutVsTrigger = 5;
  30.  
  31.         private static bool _isBusy = false;
  32.         static LyncClient _lyncClient;
  33.  
  34.         [DllImport("user32.dll")]
  35.  
  36.         static extern int GetForegroundWindow();
  37.         [DllImport("user32.dll")]
  38.         static extern int GetWindowText(int hWnd, StringBuilder text, int count);
  39.  
  40.  
  41.         static void Main()
  42.         {
  43.             var swVSStart = new Stopwatch();
  44.             var swVSEnd = new Stopwatch();
  45.             while (true)
  46.             {
  47.                 if (GetActiveWindow().Contains(WindowTitle))
  48.                 {
  49.                     if (swVSEnd.IsRunning)
  50.                         swVSEnd.Stop();
  51.                     swVSEnd.Reset();
  52.                     //we are in visual studio.  is our VS timer running?
  53.                     if (!swVSStart.IsRunning)
  54.                         swVSStart.Start();
  55.                     //have we been in VS long enough to trigger status change?
  56.                     if (swVSStart.Elapsed.Minutes >= MinutesInVsTrigger && !_isBusy)
  57.                     {
  58.                         _isBusy = true;
  59.                         Console.WriteLine("SETTING STATUS TO BUSY");
  60.                         SetStatus(Unavailable);
  61.                     }
  62.  
  63.  
  64.                 }
  65.                 else
  66.                 {
  67.                     if (swVSStart.IsRunning)
  68.                         swVSStart.Stop();
  69.                     if (!swVSEnd.IsRunning)
  70.                         swVSEnd.Start();
  71.  
  72.                     if (swVSEnd.Elapsed.Minutes >= MinutesOutVsTrigger && _isBusy)
  73.                     {
  74.                         swVSStart.Reset();
  75.                         _isBusy = false;
  76.                         Console.WriteLine("SETTING STATUS TO AVAILABLE");
  77.                         SetStatus(Free);
  78.                     }
  79.                 }
  80.                 Console.WriteLine("Time in VS: {0}\nTime out of VS: {1}", swVSStart.Elapsed.ToString(), swVSEnd.Elapsed.ToString());
  81.                 Thread.Sleep(5000);
  82.             }
  83.         }
  84.  
  85.         private static void SetStatus(string status)
  86.         {
  87.             _lyncClient = LyncClient.GetClient();
  88.             //is our status already set?
  89.             var curStatus =
  90.                 _lyncClient.Self.Contact.GetContactInformation(ContactInformationType.Availability).ToString();
  91.             Console.WriteLine("Current status code: {0}", curStatus);
  92.             if (curStatus == status)
  93.                 return;
  94.            
  95.             //available 3500
  96.             //busy 6500
  97.             //dnd 9500
  98.  
  99.             //Each element of this array must contain a valid enumeration. If null array elements
  100.             //are passed, an ArgumentException is raised.
  101.             var publishData = new Dictionary<PublishableContactInformationType, object>();
  102.             switch (status)
  103.             {
  104.                 case DnD:
  105.                     publishData.Add(PublishableContactInformationType.PersonalNote, StatusMessage);
  106.                     publishData.Add(PublishableContactInformationType.Availability, ContactAvailability.DoNotDisturb);
  107.                     break;
  108.                 case Free:
  109.                     publishData.Add(PublishableContactInformationType.PersonalNote, "");
  110.                     publishData.Add(PublishableContactInformationType.Availability, ContactAvailability.Free);
  111.                     break;
  112.                 case Busy:
  113.                     publishData.Add(PublishableContactInformationType.PersonalNote, StatusMessage);
  114.                     publishData.Add(PublishableContactInformationType.Availability, ContactAvailability.Busy);
  115.                     break;
  116.             }
  117.            
  118.  
  119.             //Helper method is found in the next example.
  120.             SendPublishRequest(publishData);
  121.         }
  122.  
  123.         /// <summary>
  124.         /// Sends a publication request and handles any exceptions raised.
  125.         /// </summary>
  126.         /// <param name="publishData">Dictionary. Information to be published.</param>
  127.         private static void SendPublishRequest(Dictionary<PublishableContactInformationType, object> publishData)
  128.         {
  129.  
  130.             try
  131.             {
  132.                 _lyncClient.Self.BeginPublishContactInformation(
  133.                     publishData,
  134.                     ar => _lyncClient.Self.EndPublishContactInformation(ar),
  135.                     null);
  136.             }
  137.             catch (COMException ce)
  138.             {
  139.                 Console.WriteLine("publish COM exception: " + ce.ErrorCode);
  140.             }
  141.             catch (ArgumentException ae)
  142.             {
  143.                 Console.WriteLine("publish Argument exception: " + ae.Message);
  144.             }
  145.         }
  146.  
  147.         private static string GetActiveWindow()
  148.         {
  149.             const int nChars = 256;
  150.             var buff = new StringBuilder(nChars);
  151.             var result = "";
  152.             var handle = GetForegroundWindow();
  153.  
  154.             if (GetWindowText(handle, buff, nChars) <= 0) return result;
  155.             Console.Clear();
  156.             Console.WriteLine("Window Label Caption: {0}", buff);
  157.             result = buff.ToString();
  158.             Console.WriteLine("Window Handle ID: {0}", handle);
  159.  
  160.             return result;
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement