Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Lync.Model;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- // ReSharper disable once FunctionNeverReturns
- namespace Monitor_Applications
- {
- class Program
- {
- //DO NOT MODIFY THESE
- private const string Busy = "6500";
- private const string Free = "3500";
- private const string DnD = "9500";
- //Choose which unavailable status you want to use:
- private const string Unavailable = DnD;
- //What message do you want when you are unavailable?
- private const string StatusMessage = "Programming...";
- //What window title are we monitoring for? (this is a contains, not equals)
- private const string WindowTitle = "Microsoft Visual Studio";
- //How long must you be using VS to trigger unavailable?
- private const int MinutesInVsTrigger = 5;
- //How long must you be doing other things to trigger available?
- private const int MinutesOutVsTrigger = 5;
- private static bool _isBusy = false;
- static LyncClient _lyncClient;
- [DllImport("user32.dll")]
- static extern int GetForegroundWindow();
- [DllImport("user32.dll")]
- static extern int GetWindowText(int hWnd, StringBuilder text, int count);
- static void Main()
- {
- var swVSStart = new Stopwatch();
- var swVSEnd = new Stopwatch();
- while (true)
- {
- if (GetActiveWindow().Contains(WindowTitle))
- {
- if (swVSEnd.IsRunning)
- swVSEnd.Stop();
- swVSEnd.Reset();
- //we are in visual studio. is our VS timer running?
- if (!swVSStart.IsRunning)
- swVSStart.Start();
- //have we been in VS long enough to trigger status change?
- if (swVSStart.Elapsed.Minutes >= MinutesInVsTrigger && !_isBusy)
- {
- _isBusy = true;
- Console.WriteLine("SETTING STATUS TO BUSY");
- SetStatus(Unavailable);
- }
- }
- else
- {
- if (swVSStart.IsRunning)
- swVSStart.Stop();
- if (!swVSEnd.IsRunning)
- swVSEnd.Start();
- if (swVSEnd.Elapsed.Minutes >= MinutesOutVsTrigger && _isBusy)
- {
- swVSStart.Reset();
- _isBusy = false;
- Console.WriteLine("SETTING STATUS TO AVAILABLE");
- SetStatus(Free);
- }
- }
- Console.WriteLine("Time in VS: {0}\nTime out of VS: {1}", swVSStart.Elapsed.ToString(), swVSEnd.Elapsed.ToString());
- Thread.Sleep(5000);
- }
- }
- private static void SetStatus(string status)
- {
- _lyncClient = LyncClient.GetClient();
- //is our status already set?
- var curStatus =
- _lyncClient.Self.Contact.GetContactInformation(ContactInformationType.Availability).ToString();
- Console.WriteLine("Current status code: {0}", curStatus);
- if (curStatus == status)
- return;
- //available 3500
- //busy 6500
- //dnd 9500
- //Each element of this array must contain a valid enumeration. If null array elements
- //are passed, an ArgumentException is raised.
- var publishData = new Dictionary<PublishableContactInformationType, object>();
- switch (status)
- {
- case DnD:
- publishData.Add(PublishableContactInformationType.PersonalNote, StatusMessage);
- publishData.Add(PublishableContactInformationType.Availability, ContactAvailability.DoNotDisturb);
- break;
- case Free:
- publishData.Add(PublishableContactInformationType.PersonalNote, "");
- publishData.Add(PublishableContactInformationType.Availability, ContactAvailability.Free);
- break;
- case Busy:
- publishData.Add(PublishableContactInformationType.PersonalNote, StatusMessage);
- publishData.Add(PublishableContactInformationType.Availability, ContactAvailability.Busy);
- break;
- }
- //Helper method is found in the next example.
- SendPublishRequest(publishData);
- }
- /// <summary>
- /// Sends a publication request and handles any exceptions raised.
- /// </summary>
- /// <param name="publishData">Dictionary. Information to be published.</param>
- private static void SendPublishRequest(Dictionary<PublishableContactInformationType, object> publishData)
- {
- try
- {
- _lyncClient.Self.BeginPublishContactInformation(
- publishData,
- ar => _lyncClient.Self.EndPublishContactInformation(ar),
- null);
- }
- catch (COMException ce)
- {
- Console.WriteLine("publish COM exception: " + ce.ErrorCode);
- }
- catch (ArgumentException ae)
- {
- Console.WriteLine("publish Argument exception: " + ae.Message);
- }
- }
- private static string GetActiveWindow()
- {
- const int nChars = 256;
- var buff = new StringBuilder(nChars);
- var result = "";
- var handle = GetForegroundWindow();
- if (GetWindowText(handle, buff, nChars) <= 0) return result;
- Console.Clear();
- Console.WriteLine("Window Label Caption: {0}", buff);
- result = buff.ToString();
- Console.WriteLine("Window Handle ID: {0}", handle);
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement