/// <summary> /// Query the freedb server to see if there is information on this cd /// </summary> /// <param name="querystring"></param> /// <param name="queryResult"></param> /// <param name="queryResultsColl"></param> /// <returns></returns> public List<QueryResult> Query(string querystring) { List<string> coll = null; StringBuilder builder = new StringBuilder(FreedbHelper.Commands.CMD_QUERY); builder.Append("+"); builder.Append(querystring); //make call try { coll = Call(builder.ToString()); } catch (Exception ex) { string msg = "Unable to perform cddb query."; Exception newex = new Exception(msg, ex); throw newex; } // check if results came back if (coll.Count == 0) { string msg = "No results returned from cddb query."; Exception ex = new Exception(msg, null); throw ex; } string code = GetCode(coll[0]); if (code == ResponseCodes.CODE_INVALID) { string msg = "Unable to process results returned for query: Data returned: " + coll[0]; Exception ex = new Exception(msg, null); throw ex; } switch (code) { case ResponseCodes.CODE_500: case ResponseCodes.CODE_202: //Database entry is corrupt case ResponseCodes.CODE_403: //no handshake case ResponseCodes.CODE_409: return null; // Multiple results were returned // Put them into a queryResultCollection object case ResponseCodes.CODE_211: case ResponseCodes.CODE_210: { List<QueryResult> queryResults = new List<QueryResult>(); //remove the 210 or 211 coll.RemoveAt(0); foreach (string line in coll) { QueryResult result = new QueryResult(line, true); queryResults.Add(result); } return queryResults; } // exact match case ResponseCodes.CODE_200: { List<QueryResult> queryResults = new List<QueryResult>(); queryResults.Add(new QueryResult(coll[0])); return queryResults; } //not found default: return null; } // end of switch }
///// <summary> ///// Retrieve all Freedb servers from the main server site ///// </summary> ///// <param name="sites">SiteCollection that is populated with the site information</param> ///// <returns>Response Code</returns> //public List<Site> GetSites() //{ // return GetSites(Site.PROTOCOLS.ALL); //} #endregion ///// <summary> ///// Get the Freedb sites ///// </summary> ///// <param name="protocol"></param> ///// <param name="sites">SiteCollection that is populated with the site information</param> ///// <returns>Response Code</returns> ///// //public List<Site> GetSites(string protocol ) //{ // if (protocol != Site.PROTOCOLS.CDDBP && protocol != Site.PROTOCOLS.HTTP) // protocol = Site.PROTOCOLS.ALL; // List<string> coll; // try // { // coll= Call(Commands.CMD_SITES,m_mainSite.GetUrl()); // } // catch (Exception ex) // { // Debug.WriteLine("Error retrieving Sites." + ex.Message); // throw new Exception("FreedbHelper.GetSites: Error retrieving Sites.", ex); // } // // check if results came back // if (coll.Count < 0) // { // string msg = "No results returned from sites request."; // throw new Exception(msg,null); // } // string code = GetCode(coll[0]); // if (code == ResponseCodes.CODE_INVALID) // { // string msg = "Unable to process results Sites Request. Returned Data: " + coll[0]; // throw new Exception(msg,null); // } // switch (code) // { // case ResponseCodes.CODE_500: // case ResponseCodes.CODE_401: // return null; // case ResponseCodes.CODE_210: // { // coll.RemoveAt(0); // List<Site> sites = new List<Site>(); // foreach (String line in coll) // { // Debug.WriteLine("line: " + line); // Site site = new Site(line); // if (protocol == Site.PROTOCOLS.ALL) // sites.Add(new Site(line)); // else if (site.Protocol == protocol) // sites.Add(new Site(line)); // } // return sites; // } // default: // return null; // } //} /// <summary> /// Read Entry from the database. /// </summary> /// <param name="qr">A QueryResult object that is created by performing a query</param> /// <param name="cdEntry">out parameter - CDEntry object</param> /// <returns></returns> //public CDEntry Read(QueryResult qr) //{ // Debug.Assert(qr != null); // List<string> coll = null; // StringBuilder builder = new StringBuilder(FreedbHelper.Commands.CMD_READ); // builder.Append("+"); // builder.Append(qr.Category); // builder.Append("+"); // builder.Append(qr.Discid); // //make call // try // { // coll = Call(builder.ToString()); // } // catch (Exception ex) // { // string msg = "Error performing cddb read."; // throw new Exception(msg,ex); // } // // check if results came back // if (coll.Count < 0) // { // string msg = "No results returned from cddb read."; // throw new Exception(msg,null); // } // string code = GetCode(coll[0]); // if (code == ResponseCodes.CODE_INVALID) // { // string msg = "Unable to process results for cddb read. Returned Data: " + coll[0]; // throw new Exception(msg,null); // } // switch (code) // { // case ResponseCodes.CODE_500: // query problem // case ResponseCodes.CODE_401: // entry not found // case ResponseCodes.CODE_402: // server error // case ResponseCodes.CODE_403: // Database entry is corrupt // case ResponseCodes.CODE_409: // No handshake // return null; // case ResponseCodes.CODE_210: // good // { // coll.RemoveAt(0); // remove the 210 // return new CDEntry(coll); // } // default: // return null; // } //} /// <summary> /// Read Entry from the database. /// </summary> /// <param name="qr">A QueryResult object that is created by performing a query</param> /// <param name="cdEntry">out parameter - CDEntry object</param> /// <returns></returns> internal AlbumDescriptor ConvertToAlbumDescriptor(QueryResult qr, IInternetServiceListener listener) { Debug.Assert(qr != null); List<string> coll = null; StringBuilder builder = new StringBuilder(FreedbHelper.Commands.CMD_READ); builder.Append("+"); builder.Append(qr.Category); builder.Append("+"); builder.Append(qr.Discid); //make call try { coll = Call(builder.ToString()); } catch (WebException ex) { if (listener != null) listener.OnWebExeption(ex); return null; } catch (Exception) { return null; } // check if results came back if (coll.Count == 0) { return null; } string code = GetCode(coll[0]); if (code == ResponseCodes.CODE_INVALID) { if (listener != null) listener.OnUnExpectedUnreadableResult("Unable to process results for cddb read."); return null; } switch (code) { case ResponseCodes.CODE_500: // query problem case ResponseCodes.CODE_401: // entry not found case ResponseCodes.CODE_402: // server error case ResponseCodes.CODE_403: // Database entry is corrupt case ResponseCodes.CODE_409: // No handshake return null; case ResponseCodes.CODE_210: // good { coll.RemoveAt(0); // remove the 210 return AlbumDescriptor.FromFreeDBInfo(coll); } default: if (listener != null) listener.OnStatusCodeKO(); return null; } }