/// <summary> /// Updates IsFrequentlyMailed flag in the contacts collection. /// If the contact is not found in the current collection, it is added. /// Helper function of GetContacts to update the contacts collection. /// </summary> /// <param name="contacts">List of Gmail contacts; should be all contacts.</param> /// <returns>A <see cref="GmailContactCollection"/> of contacts in address book.</returns> // Added by Eric Larson [[email protected]]; 5/2/2005 private void GetFrequentlyMailedContacts(GmailContactCollection contacts) { //frequently mailed Uri location = new Uri(GMAIL_HOST_URL + "/mail?&ik=" + System.Web.HttpUtility.UrlEncode(this._session.IdentificationKey) + "&view=cl&search=contacts&pnl=d&zx=" + MakeUniqueUrl()); this._rawDataPackResponse = MakeWebRequest(location, "GET", null, null, true); // sanitize the incoming _rawDataPackResponse this._rawDataPackResponse = this._rawDataPackResponse.Replace("\n", ""); if (this._rawDataPackResponse.Length > 128) { // find the beginning of the address block // Fix by Billy Roebuck [[email protected]]; 6/24/2005 // Changed the line below (and again at the end of this method) and some of the offset values //int addressBlockStart = this._rawDataPackResponse.IndexOf("D([\"a\""); int addressBlockStart = this._rawDataPackResponse.IndexOf("D([\"cl\","); // make multiple passes to parse all address arrays while (addressBlockStart > -1) { // find the address block beginning and end addressBlockStart = this._rawDataPackResponse.IndexOf("[", addressBlockStart + 8); int addressBlockEnd = this._rawDataPackResponse.IndexOf("]]);", addressBlockStart) + 2; // Need to enclose address block in quotes for ParseJSArray to parse it correctly. // Fixed by Eric Larson [[email protected]]; 5/2/2005 // get the address block string addressBlock = "[" + this._rawDataPackResponse.Substring(addressBlockStart, addressBlockEnd - addressBlockStart) + "]"; // parse the address block into an ArrayList ArrayList addresses = Utilities.ParseJSArray(addressBlock); // loop through ArrayList of contacts and insert into collection foreach (ArrayList contact in addresses) { // Using GmailContact Indeces enum for easy changing // Added by Eric Larson [[email protected]]; 5/10/2005 GmailContact currentContact = new GmailContact(); int i; // loop through current contacts to update conatct collection for (i = 0; i < contacts.Count; i++) { // Check the contact list to see if the frequently mailed contact is in the collection currentContact = (GmailContact)contacts[i]; if (currentContact.id == int.Parse((string)contact[(int)GmailContact.Indeces.id], System.Globalization.NumberStyles.HexNumber)) { currentContact.IsFrequentlyMailed = true; break; } } // If contact was not in the collection, add it if (i == contacts.Count) { GmailContact tmpContact = new GmailContact(); tmpContact.id = int.Parse((string)contact[(int)GmailContact.Indeces.id], System.Globalization.NumberStyles.HexNumber); tmpContact.Email = contact[(int)GmailContact.Indeces.Email].ToString(); tmpContact.Name = contact[(int)GmailContact.Indeces.DefaultName].ToString(); if (contact.Count > (int)GmailContact.Indeces.Notes) { tmpContact.Notes = contact[(int)GmailContact.Indeces.Notes].ToString(); } // to keep backwards compatibility with 0.6.1 and before tmpContact.EmailUnescaped = System.Web.HttpUtility.HtmlEncode(contact[(int)GmailContact.Indeces.Email].ToString()); tmpContact.IsFrequentlyMailed = true; contacts.Add(tmpContact); } } // Check to see if there is another block of contacts. //addressBlockStart = this._rawDataPackResponse.IndexOf("D([\"a\"", addressBlockStart); // Fix by Billy Roebuck [[email protected]]; 6/24/2005 addressBlockStart = this._rawDataPackResponse.IndexOf("D([\"cl\",", addressBlockStart); } ; } return; }
/// <summary> /// Retrieves all the frequently mailed contacts in the user's Gmail address book. /// </summary> /// <returns>A <see cref="GmailContactCollection"/> of frequently mailed contacts in address book.</returns> // Added by Eric Larson [[email protected]]; 5/2/2005 // Can be used in place of GetContacts if you don't care about all the contacts. public GmailContactCollection GetFrequentlyMailedContacts() { // instantiate output vars GmailContactCollection output = new GmailContactCollection(); Uri location = new Uri(GMAIL_HOST_URL + "/mail?&ik=" + System.Web.HttpUtility.UrlEncode(this._session.IdentificationKey) + "&view=cl&search=contacts&pnl=d&zx=" + MakeUniqueUrl()); this._rawDataPackResponse = MakeWebRequest(location, "GET", null, null, true); // sanitize the incoming _rawDataPackResponse this._rawDataPackResponse = this._rawDataPackResponse.Replace("\n", ""); if (this._rawDataPackResponse.Length > 128) { // find the beginning of the address block //int addressBlockStart = this._rawDataPackResponse.IndexOf("D([\"a\""); // Fix by Billy Roebuck [[email protected]]; 6/24/2005 int addressBlockStart = this._rawDataPackResponse.IndexOf("D([\"cl\","); // make multiple passes to parse all address arrays while (addressBlockStart > -1) { // find the address block beginning and end addressBlockStart = this._rawDataPackResponse.IndexOf("[", addressBlockStart + 8); int addressBlockEnd = this._rawDataPackResponse.IndexOf("]]);", addressBlockStart) + 2; // get the address block string addressBlock = this._rawDataPackResponse.Substring(addressBlockStart, addressBlockEnd - addressBlockStart); // parse the address block into an ArrayList ArrayList addresses = Utilities.ParseJSArray(addressBlock); // loop through ArrayList of contacts and insert into collection foreach (ArrayList contact in addresses) { GmailContact currentContact = new GmailContact(); currentContact.id = int.Parse((string)contact[0], System.Globalization.NumberStyles.HexNumber); GmailContact tmpContact = new GmailContact(); tmpContact.id = int.Parse((string)contact[0], System.Globalization.NumberStyles.HexNumber); tmpContact.Email = contact[3].ToString(); tmpContact.Name = contact[1].ToString(); if (contact.Count > 4) { tmpContact.Notes = contact[4].ToString(); } // to keep backwards compatibility with 0.6.1 and before tmpContact.EmailUnescaped = System.Web.HttpUtility.HtmlEncode(contact[3].ToString()); tmpContact.IsFrequentlyMailed = true; output.Add(tmpContact); } // Check to see if there is another block of contacts. //addressBlockStart = this._rawDataPackResponse.IndexOf("D([\"a\"", addressBlockStart); // Fix by Billy Roebuck [[email protected]]; 6/24/2005 addressBlockStart = this._rawDataPackResponse.IndexOf("D([\"cl\",", addressBlockStart); } ; } return output; }
/// <summary> /// Retrieves all the contacts in the user's Gmail address book. /// </summary> /// <returns>A <see cref="GmailContactCollection"/> of contacts in address book.</returns> public GmailContactCollection GetContacts() { // instantiate output vars GmailContactCollection output = new GmailContactCollection(); // Added the IK the URI to the fit the current protocol // Fix by Eric Larson [[email protected]]; 5/2/2005 Uri location = new Uri(GMAIL_HOST_URL + "/mail?&ik=" + System.Web.HttpUtility.UrlEncode(this._session.IdentificationKey) + "&view=cl&search=contacts&pnl=a&zx=" + MakeUniqueUrl()); this._rawDataPackResponse = MakeWebRequest(location, "GET", null, null, true); // sanitize the incoming _rawDataPackResponse this._rawDataPackResponse = this._rawDataPackResponse.Replace("\n", ""); if (this._rawDataPackResponse.Length > 128) { // Looping through the data pack response looking for multiple arrays // Gmail only puts 15 address in a single array, so search all 'a' (address) arrays. // Fix by Eric Larson [[email protected]]; 5/2/2005 //int addressBlockStart = this._rawDataPackResponse.IndexOf("D([\"a\""); // Fix by Billy Roebuck [[email protected]]; 6/24/2005 // Changed the line below (and again at the end of this method) and some of the offset values //int addressBlockStart = this._rawDataPackResponse.IndexOf("D([\"a\""); int addressBlockStart = this._rawDataPackResponse.IndexOf("D([\"cl\","); while (addressBlockStart > -1) { // find the address block addressBlockStart = this._rawDataPackResponse.IndexOf("[", addressBlockStart + 6); int addressBlockEnd = this._rawDataPackResponse.IndexOf("]]);", addressBlockStart) + 1; string addressBlock = "[" + this._rawDataPackResponse.Substring(addressBlockStart, addressBlockEnd - addressBlockStart) + "]"; //addressBlock = addressBlock.Replace("\\\\\\\"", "\""); // parse the address block into an ArrayList ArrayList addresses = Utilities.ParseJSArray(addressBlock); // loop through ArrayList of contacts and insert into collection foreach (ArrayList contact in addresses) { GmailContact tmpContact = new GmailContact(); // Using GmailContact Indeces enum for easy changing // Added by Eric Larson [[email protected]]; 5/10/2005 // Store the contact ID so that we can edit or delete it. // Added by Eric Larson [[email protected]]; 5/2/2005 tmpContact.id = int.Parse((string)contact[(int)GmailContact.Indeces.id], System.Globalization.NumberStyles.HexNumber); tmpContact.Email = contact[(int)GmailContact.Indeces.Email].ToString(); tmpContact.Name = contact[(int)GmailContact.Indeces.DefaultName].ToString(); if (contact.Count > (int)GmailContact.Indeces.Notes) { tmpContact.Notes = contact[(int)GmailContact.Indeces.Notes].ToString(); } // EmailUnescaped is no longer sent by Gmail, so fake it. // to keep backwards compatibility with 0.6.1 and before // Added by Eric Larson [[email protected]]; 5/2/2005 tmpContact.EmailUnescaped = System.Web.HttpUtility.HtmlEncode(contact[(int)GmailContact.Indeces.Email].ToString()); // Frequently mailed can no longer be be determined in a single request. // So initialize all contacts to false and then make second request later. // Added by Eric Larson [[email protected]]; 5/2/2005 tmpContact.IsFrequentlyMailed = false; output.Add(tmpContact); } // Check to see if there is another block of contacts. // Added by Eric Larson [[email protected]]; 5/2/2005 //addressBlockStart = this._rawDataPackResponse.IndexOf("D([\"a\"", addressBlockStart); // Fix by Billy Roebuck [[email protected]]; 6/24/2005 addressBlockStart = this._rawDataPackResponse.IndexOf("D([\"cl\",", addressBlockStart); } ; } // Pass the current list of contacts to a helper function to find the frequently mailed contacts. // Added by Eric Larson [[email protected]]; 5/2/2005 GetFrequentlyMailedContacts(output); return output; }