Esempio n. 1
0
        public bool initLyncSubscription()
        {
            try
            {
                serialPortInit();

                _contactSubscription = LyncClient.GetClient().ContactManager.CreateSubscription();
                lync = LyncClient.GetClient();
                //Wait for Lync to sign in
                while (lync.State != ClientState.SignedIn)
                {
                    setLED(ERROR);
                    Thread.Sleep(sleepInterval);
                    lync = LyncClient.GetClient();
                }



                _contact  = lync.ContactManager.GetContactByUri(lync.Uri);
                firstName = lync.Uri.Split(':')[1].Split('.')[0].ToUpper();

                if (string.IsNullOrEmpty(TaskTrayApplication.Properties.Settings.Default.reponsePrefix))
                {
                    TaskTrayApplication.Properties.Settings.Default.reponsePrefix = firstName;
                }

                _ContactInformationList.Add(ContactInformationType.Availability);
                _contactSubscription.AddContact(_contact);
                _contactSubscription.Subscribe(ContactSubscriptionRefreshRate.High, _ContactInformationList);
                //Register for event raised when selected user contact information
                //is re-published.
                _contact.ContactInformationChanged += ShowMessage;

                //register for new message

                lync.StateChanged += _LyncClient_StateChanged;
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("running"))
                {
                    //   MessageBox.Show("Error: Skype not running.  Try again after starting skype");
                    //   Environment.Exit(10);
                    return(false);
                }
                try
                {
                    if (!TaskTrayApplication.Properties.Settings.Default.disableLyncLED)
                    {
                        _serialPort.Open();
                        _serialPort.Write("4");
                        _serialPort.Close();
                    }
                }
                catch {
                    return(false);
                }
            }
            return(true);
        }
        /// <summary>
        /// Adds contacts found through a search to a ContactSubscription
        /// and raises the ContactAddedEvent.
        /// </summary>
        /// <param name="sContact">List[Contact]. The list of contacts found in a search.</param>
        public void SubscribeToSearchResults(List <Contact> sContactList)
        {
            try
            {
                if (searchResultSubscription == null)
                {
                    // Create subscription for the contact manager
                    // if the contact manager is not subscribed.
                    searchResultSubscription = contactMgr.CreateSubscription();
                }
                else
                {
                    // Remove all existing search results.
                    searchResultSubscription.Unsubscribe();
                    foreach (Contact c in searchResultSubscription.Contacts)
                    {
                        searchResultSubscription.RemoveContact(c);
                    }
                }

                // Add the Contact to a ContactSubscription.
                searchResultSubscription.AddContacts(sContactList);

                // Specify the Contact Information Types to be
                // returned in ContactInformationChanged events.
                ContactInformationType[] ContactInformationTypes = { ContactInformationType.Availability, ContactInformationType.ActivityId };

                // Activate the subscription.
                searchResultSubscription.Subscribe(ContactSubscriptionRefreshRate.High, ContactInformationTypes);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:    " + ex.Message);
            }
        }
Esempio n. 3
0
        public void SetContact(string ContactURIorDN)
        {
            FreeContact();

            if (_LyncClient != null)
            {
                string ContactURI = string.Empty;

                if (_RegExDistinguishedName.IsMatch(ContactURIorDN))
                {
                    ContactURI = _GetContactURIByDistinguishedName(ContactURIorDN);
                }
                else
                {
                    ContactURI = ContactURIorDN;
                }

                try
                {
                    _Contact = _LyncClient.ContactManager.GetContactByUri(ContactURI);
                    _Contact.SettingChanged            += _ContactSettingChanged;
                    _Contact.ContactInformationChanged += _ContactInformationChanged;
                    _Contact.UriChanged += _ContactUriChanged;
                    _ContactSubscription.AddContact(_Contact);
                    _ContactSubscription.Subscribe(ContactSubscriptionRefreshRate.High, _ContactInformationList);

                    _GetContactInfo();
                }
                catch (LyncClientException e)
                {
                    FreeContact();
                    _LyncClientInitialization();
                    ClientSignIn();
                    // Console.WriteLine(e);
                }
                catch (SystemException systemException)
                {
                    if (_IsLyncException(systemException))
                    {
                        // Log the exception thrown by the Lync Model API.
                        // Console.WriteLine("Error: " + systemException);
                    }
                    else
                    {
                        // Rethrow the SystemException which did not come from the Lync Model API.
                        throw;
                    }
                    FreeContact();
                }
            }
        }
Esempio n. 4
0
        public static void SearchForContacts(string searchKey)
        {
            List <ContactInformationType> ContactInformationList = new List <ContactInformationType>();

            //ContactInformationList.Add(ContactInformationType.Activity);
            ContactInformationList.Add(ContactInformationType.Availability);
            // ContactInformationList.Add(ContactInformationType.CapabilityString);

            ContactSubscription contactSubscription = LyncClient.GetClient().ContactManager.CreateSubscription();


            Console.WriteLine(
                "Searching for contacts on " +
                searchKey);

            name = searchKey;

            LyncClient.GetClient().ContactManager.BeginSearch(
                searchKey,
                (ar) =>
            {
                SearchResults searchResults = LyncClient.GetClient().ContactManager.EndSearch(ar);
                if (searchResults.Contacts.Count > 0)
                {
                    Console.WriteLine(
                        searchResults.Contacts.Count.ToString() +
                        " found");

                    foreach (Contact contact in searchResults.Contacts)
                    {
                        contactSubscription.AddContact(contact);
                        contactSubscription.Subscribe(ContactSubscriptionRefreshRate.High, ContactInformationList);
                        Console.WriteLine(
                            contact.GetContactInformation(ContactInformationType.DisplayName).ToString() + "   " + contact.GetContactInformation(ContactInformationType.Availability).ToString());
                        scode = (int)contact.GetContactInformation(ContactInformationType.Availability);
                        // next update, make this int and pass int code rather than string .ToString()
                    }
                }
            },
                null);
        }
Esempio n. 5
0
        public void GetContactInfo(string ContactURI)
        {
            FreeContact();

            if (_LyncClient != null)
            {
                try
                {
                    _Contact = _LyncClient.ContactManager.GetContactByUri(ContactURI);
                    _Contact.SettingChanged            += _Contact_SettingChanged;
                    _Contact.ContactInformationChanged += _Contact_ContactInformationChanged;
                    _Contact.UriChanged += _Contact_UriChanged;
                    _ContactSubscription.AddContact(_Contact);
                    _ContactSubscription.Subscribe(ContactSubscriptionRefreshRate.High, _ContactInformationList);

                    _ContactName         = (string)_Contact.GetContactInformation(ContactInformationType.DisplayName);
                    _ContactAvailability = (int)(ContactAvailability)_Contact.GetContactInformation(ContactInformationType.Availability);
                }
                catch (LyncClientException e)
                {
                    FreeContact();
                    LyncClientInitialization(true);
                    // Console.WriteLine(e);
                }
                catch (SystemException systemException)
                {
                    if (IsLyncException(systemException))
                    {
                        // Log the exception thrown by the Lync Model API.
                        // Console.WriteLine("Error: " + systemException);
                    }
                    else
                    {
                        // Rethrow the SystemException which did not come from the Lync Model API.
                        throw;
                    }
                    FreeContact();
                }
            }
        }
        private void BeginSearchCallback(IAsyncResult r)
        {
            object[]       asyncState = (object[])r.AsyncState;
            ContactManager cm         = (ContactManager)asyncState[0];

            try
            {
                SearchResults results = cm.EndSearch(r);
                if (results.AllResults.Count == 0)
                {
                    Console.WriteLine("No results.");
                }
                else if (results.AllResults.Count == 1)
                {
                    ContactSubscription srs     = cm.CreateSubscription();
                    Contact             contact = results.Contacts[0];
                    srs.AddContact(contact);
                    ContactInformationType[] contactInformationTypes = { ContactInformationType.Availability, ContactInformationType.ActivityId };
                    srs.Subscribe(ContactSubscriptionRefreshRate.High, contactInformationTypes);
                    _conversation = _client.ConversationManager.AddConversation();
                    _conversation.AddParticipant(contact);
                    Dictionary <InstantMessageContentType, String> messages = new Dictionary <InstantMessageContentType, String>();
                    messages.Add(InstantMessageContentType.PlainText, _message);
                    InstantMessageModality m = (InstantMessageModality)_conversation.Modalities[ModalityTypes.InstantMessage];
                    m.BeginSendMessage(messages, BeginSendMessageCallback, messages);
                }
                else
                {
                    Console.WriteLine("More than one result.");
                }
            }
            catch (SearchException se)
            {
                Console.WriteLine("Search failed: " + se.Reason.ToString());
            }
            _client.ContactManager.EndSearch(r);
        }