Eigene Dateien/Visual Studio 2005/Projects/DCPlusPlus/DCPlusPlus/SearchResults.cs

Go to the documentation of this file.
00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 //using System.Windows.Forms;
00005 //using System.
00006 
00007 namespace DCPlusPlus
00008 {
00012     public class SearchResults
00013     {
00014         //TODO deprecate this ;-)
00021         public delegate void ResultsChangedEventHandler(SearchResults s_results, int num_results);
00026         public event ResultsChangedEventHandler ResultsChanged;
00031         public delegate void ResultsClearedEventHandler(SearchResults s_results);
00036         public event ResultsClearedEventHandler ResultsCleared;
00042         public delegate void ResultAddedEventHandler(SearchResults s_results, SearchResult result);
00047         public event ResultAddedEventHandler ResultAdded;
00051         private string[] search_terms;
00052 
00053         protected string search_term = "";
00057         public string SearchTerm
00058         {
00059             get
00060             {
00061                 return (search_term);
00062             }
00063             set
00064             {
00065                 search_term = value;
00066                 char[] sep = new char[1];
00067                 sep[0] = ' ';
00068                 search_terms = search_term.Split(sep, StringSplitOptions.RemoveEmptyEntries);
00069             }
00070         }
00071 
00072         protected bool discard_old_results = false;
00078         public bool DiscardOldResults 
00079         {
00080             get
00081             {
00082                 return (discard_old_results);
00083             }
00084             set
00085             {
00086                 discard_old_results = value;
00087             }
00088         }
00094         public class SearchResult
00095         {
00096             //public delegate bool OnHubResolveHandler(object sender, string hub_address);
00097             //public event OnHubResolveHandler OnHubResolve;
00098 
00099 
00100             //TODO change this to a queue.queueentry.source
00101             protected string filename = "";
00105             public string Filename
00106             {
00107                 get
00108                 {
00109                     return (filename);
00110                 }
00111             }
00112             protected string user_name = "";
00116             public string UserName
00117             {
00118                 get
00119                 {
00120                     return (user_name);
00121                 }
00122             }
00126             private void SplitResultLine()
00127             {
00128                 /*
00129                  * $SR <source_nick> <result> <free_slots>/<total_slots><0x05><hub_name> (<hub_ip:listening_port>)[<0x05><target_nick>]|
00130                  * Description
00131                  * This command is used to return a file or directory that matches a $Search. All terms in the original query must be present in <result>, and all types and size restrictions in the $Search must be met. 
00132 
00133                  * <result> is one of the following: 
00134                  * <file_name><0x05><file_size> for file results 
00135                  * <directory> for directory results 
00136                  * The <0x05> characters used above for deliminators are the 5th character in the ASCII character set. 
00137                  * Sent by a client when a match to a $Search is found. 
00138                  * If the $Search was a passive one, the $SR is returned via the hub connection (TCP). In this case, <0x05><target_nick> must be included on the end of the $SR. The hub must strip the deliminator and <target_nick> before sending the $SR to <target_nick>. If the search was active, it is sent to the IP address and port specified in the $Search via UDP. 
00139                  * The port for the hub only needs to specified if its listening port is not the default (411). 
00140                  * On UNIX the path delimiter / must be converted to \ for compatibility. 
00141                  * DC++ will send a maximum of 5 search results to passive users and 10 to active users. 
00142                  * For files containing TTH, the <hub_name> parameter is replaced with TTH:<base32_encoded_tth_hash> (ref: TTH_Hash) 
00143                  * Example
00144                  * Passive Result
00145                  * $SR User1 mypathmotd.txt<0x05>437 3/4<0x05>Testhub (10.10.10.10:411)<0x05>User2|
00146                  * Active Result
00147                  * $SR User1 mypathmotd.txt<0x05>437 3/4<0x05>Testhub (10.10.10.10:411)|
00148                  */
00149 
00150                 if (string.IsNullOrEmpty(result_line))
00151                 {
00152                     Console.WriteLine("Error empty result line.");
00153                     return;
00154                 }
00155 
00156                 try
00157                 {
00158 
00159                     char[] sep = new char[1];
00160                     sep[0] = (char)0x05;
00161                     string[] blocks = result_line.Split(sep, StringSplitOptions.RemoveEmptyEntries);
00162                     if (blocks.Length == 3)
00163                     {
00164                         //Console.WriteLine("block 1: " + blocks[0] + ",block 2: " + blocks[1] + ",block 3: " + blocks[2]);
00165                         string[] first_block_tokens = blocks[0].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
00166                         string[] second_block_tokens = blocks[1].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
00167                         string[] third_block_tokens = blocks[2].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
00168                         string[] slots = second_block_tokens[second_block_tokens.Length - 1].Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
00169                         user_name = first_block_tokens[0];
00170                         if (third_block_tokens[0].StartsWith("TTH:"))
00171                         {
00172                             tth = third_block_tokens[0].Substring(4);
00173                         }
00174                         else
00175                         {
00176                             hub_name = "";
00177                             for (int i = 0; i < third_block_tokens.Length - 2; i++)
00178                                 hub_name += third_block_tokens[i] + " ";
00179                             hub_name.TrimEnd();//kill the last whitespace added - Maybe this will not give a correct hubname... for instance hub names using double whitespaces
00180                         }
00181 
00182                         if (third_block_tokens[third_block_tokens.Length - 1].StartsWith("(") && third_block_tokens[third_block_tokens.Length - 1].EndsWith(")"))
00183                         {
00184                             hub_address = third_block_tokens[third_block_tokens.Length - 1].Substring(1, third_block_tokens[third_block_tokens.Length - 1].Length - 2);
00185                         }
00186 
00187                         filename = blocks[0].Substring(user_name.Length + 1);
00188                         file_extension = System.IO.Path.GetExtension(filename);
00189  
00190                         filesize = int.Parse(second_block_tokens[0]);
00191                         free_slots = int.Parse(slots[0]);
00192                         total_slots = int.Parse(slots[1]);
00193                         is_directory = false;
00194                     }
00195                     else if (blocks.Length == 2)
00196                     {
00197                         //Console.WriteLine("block 1: " + blocks[0] + ",block 2: " + blocks[1]);
00198                         string[] first_block_tokens = blocks[0].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
00199                         string[] second_block_tokens = blocks[1].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
00200                         string[] slots = first_block_tokens[first_block_tokens.Length - 1].Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
00201                         user_name = first_block_tokens[0];
00202                         if (second_block_tokens[0].StartsWith("TTH:"))
00203                         {
00204                             tth = second_block_tokens[0].Substring(4);
00205                         }
00206                         else
00207                         {
00208                             hub_name = "";
00209                             for (int i = 0; i < second_block_tokens.Length - 2; i++)
00210                                 hub_name += second_block_tokens[i] + " ";
00211                             hub_name.TrimEnd();//kill the last whitespace added
00212                         }
00213                         if (second_block_tokens[second_block_tokens.Length - 1].StartsWith("(") && second_block_tokens[second_block_tokens.Length - 1].EndsWith(")"))
00214                         {
00215                             hub_address = second_block_tokens[second_block_tokens.Length - 1].Substring(1, second_block_tokens[second_block_tokens.Length - 1].Length - 2);
00216                         }
00217 
00218                         directory = blocks[0].Substring(user_name.Length + 1, blocks[0].Length - (user_name.Length + 1) - 1 - first_block_tokens[first_block_tokens.Length - 1].Length);
00219                         
00220                         /*
00221                         directory = "";
00222                         for (int i = 1; i < first_block_tokens.Length - 2; i++)
00223                             directory += first_block_tokens[i];
00224                         */
00225 
00226                         free_slots = int.Parse(slots[0]);
00227                         total_slots = int.Parse(slots[1]);
00228                         is_directory = true;
00229                     }
00230 
00231 
00232                     /*
00233                     Console.WriteLine("username: '" + user_name + "'");
00234                     if (is_directory)
00235                     {
00236                         Console.WriteLine("directory: '" + directory + "'");
00237                     }
00238                     else
00239                     {
00240                         Console.WriteLine("filename: '" + filename + "'");
00241                         Console.WriteLine("file_extension: '" + file_extension + "'");
00242                         Console.WriteLine("filesize: '" + filesize + "'");
00243                     }
00244                     Console.WriteLine("free slots: '" + free_slots + "'");
00245                     Console.WriteLine("total slots: '" + total_slots + "'");
00246                     if(has_tth)
00247                         Console.WriteLine("tth: '" + tth + "'");
00248                     else Console.WriteLine("hub_name: '" + hub_name + "'");
00249 
00250                     Console.WriteLine("hub_address: '" + hub_address + "'");
00251                     */
00252 
00253                 }
00254                 catch (Exception ex)
00255                 {
00256                     Console.WriteLine("Error parsing result: " + ex.Message);
00257                 }
00258 
00259 
00260 
00261 
00262             }
00263             protected string result_line;
00267             public string ResultLine
00268             {
00269                 set
00270                 {
00271                     result_line = value;
00272                     SplitResultLine();
00273 
00274                 }
00275             }
00276             protected bool is_hub_resolved = false;
00280             public bool IsHubResolved
00281             {
00282                 get
00283                 {
00284                     return (is_hub_resolved);
00285                 }
00286             }
00287             protected string hub_address="";
00291             public string HubAddress
00292             {
00293                 get
00294                 {
00295                     return(hub_address);// = value;
00296                     //if (OnHubResolve != null)
00297                         //is_hub_resolved = OnHubResolve(this, hub_address);
00298                 }
00299             }
00303             public bool IsFile
00304             {
00305                 get
00306                 {
00307                     return (!is_directory);
00308                 }
00309             }
00310             protected bool is_directory = false;
00314             public bool IsDirectory
00315             {
00316                 get
00317                 {
00318                     return (is_directory);
00319                 }
00320             }
00321             protected string directory = "";
00325             public string Directory
00326             {
00327                 get
00328                 {
00329                     return (directory);
00330                 }
00331             }
00332             protected string file_extension = "";
00336             public string FileExtension
00337             {
00338                 get
00339                 {
00340                     return (file_extension);
00341                 }
00342             }
00343             protected int filesize = 0;
00347             public int Filesize
00348             {
00349                 get
00350                 {
00351                     return (filesize);
00352                 }
00353             }
00357             public bool HasTTH
00358             {
00359                 get
00360                 {
00361                     return (!string.IsNullOrEmpty(tth));
00362                 }
00363             }
00364             protected string tth = "";
00368             public string TTH
00369             {
00370                 get
00371                 {
00372                     return (tth);
00373                 }
00374             }
00375             protected string hub_name="";
00379             public string HubName
00380             {
00381                 get
00382                 {
00383                     return (hub_name);
00384                 }
00385             }
00386             protected int free_slots = 0;
00390             public int FreeSlots
00391             {
00392                 get
00393                 {
00394                     return (free_slots);
00395                 }
00396             }
00397             protected int total_slots = 0;
00401             public int TotalSlots
00402             {
00403                 get
00404                 {
00405                     return (total_slots);
00406                 }
00407             }
00413             public bool HasHub
00414             {
00415                 get
00416                 {
00417                     return (hub!=null);
00418                 }
00419             }
00420             protected Hub hub=null;
00424             public Hub Hub
00425             {
00426                 get
00427                 {
00428                     return (hub);
00429                 }
00430                 set
00431                 {
00432                     if (value != null)
00433                         is_hub_resolved = true;
00434                     hub = value;
00435                 }
00436             }
00440             public SearchResult()
00441             {
00442 
00443             }
00449             public SearchResult(string result_parameter_line)
00450             {
00451                 result_line = result_parameter_line;
00452                 SplitResultLine();
00453             }
00454         }
00458         public SearchResults()
00459         {
00460             
00461         }
00466         protected Object results_lock = new Object();
00467         /*public Object ResultsLock
00468         {
00469             get
00470             {
00471                 return (results_lock);
00472             }
00473             set
00474             {
00475                 results_lock = value;
00476             }
00477         }*/
00478         protected List<SearchResult> results = new List<SearchResult>();
00483         public List<SearchResult> Results
00484         {
00485             get
00486             {
00487                 return (results);
00488             }
00489         }
00494         public void AddResult(SearchResult result)
00495         {
00496 
00497             //discard old results if desired
00498             if (discard_old_results)
00499             {
00500                 if (string.IsNullOrEmpty(search_term)) return;
00501                 string path = "";
00502                 if (result.IsFile)
00503                     path = result.Filename;
00504                 else if (result.IsDirectory)
00505                     path = result.Directory;
00506 
00507                 if (!string.IsNullOrEmpty(path))
00508                 {
00509                     //check if search terms whitespace seperated match the path+filename in the result
00510                     //if not discard result (return)
00511                     bool all_terms_included = true;
00512                     foreach (string term in search_terms)
00513                     {
00514                         if (!path.Contains(term))
00515                             all_terms_included = false;
00516                     }
00517                     if (!all_terms_included)
00518                         return;
00519 
00520                 }
00521             }
00522             lock (results_lock)
00523             {
00524                 results.Add(result);
00525             }
00526             try
00527             {
00528                 if (ResultAdded != null)
00529                     ResultAdded(this, result);
00530 
00531                 if (ResultsChanged != null)
00532                     ResultsChanged(this, results.Count);
00533             }
00534             catch (Exception ex)
00535             {
00536                 Console.WriteLine("Exception in event handler: " + ex.Message);
00537             }
00538 
00539         }
00544         public void RemoveResult(SearchResult result)
00545         {
00546             lock (results_lock)
00547             {
00548                 results.Remove(result);
00549             }
00550             try
00551             {
00552                 if (ResultsChanged != null)
00553                     ResultsChanged(this, 0);
00554             }
00555             catch (Exception ex)
00556             {
00557                 Console.WriteLine("Exception in event handler: " + ex.Message);
00558             }
00559 
00560         }
00564         public void ClearResults()
00565         {
00566             search_term = "";
00567             lock (results_lock)
00568             {
00569                 results.Clear();
00570             }
00571             try
00572             {
00573                 if (ResultsCleared != null)
00574                     ResultsCleared(this);
00575 
00576                 if (ResultsChanged != null)
00577                     ResultsChanged(this, 0);
00578             }
00579             catch (Exception ex)
00580             {
00581                 Console.WriteLine("Exception in event handler: " + ex.Message);
00582             }
00583 
00584         }
00585     }
00586 }

Generated on Wed Mar 7 19:09:21 2007 for DCPlusPlus by  doxygen 1.5.1-p1