00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004
00005
00006
00007 namespace DCPlusPlus
00008 {
00012 public class SearchResults
00013 {
00014
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
00097
00098
00099
00100
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
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
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
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();
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
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();
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
00222
00223
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
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
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);
00296
00297
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
00468
00469
00470
00471
00472
00473
00474
00475
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
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
00510
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 }