private void cboCatName_TextChanged(object sender, EventArgs e)
{
if (cboCatName.Text.Length == 0 || textUpdateTarget != cboCatName.Text)
{
return;
}
// needed to avoid overzealous mouse pointer hiding on Windows
PlatformSpecific.FakeMouseMovement();
DateTime now = DateTime.Now;
latestDate = now;
// use same queries as HotCat (via GET), to take advantage of server-side caching
MorebitsDotNet.GetApi(Wiki.Commons,
"format=json&action=opensearch&namespace=14&limit=30&search=Category:" + Uri.EscapeDataString(cboCatName.Text),
now, delegate(string json, object date)
{
if (latestDate > ((DateTime)date))
{
return;
}
string[] results;
// we take advantage of the fact that [ is not a legal character in wiki page titles
// To be honest, this whole thing is crass, but I don't want to bring in a full JSON
// parser just for this
string resultsPart = json.Substring(json.IndexOf('[', 2) + 1);
// ...and taking advantage of the fact that ] is not legal either!
if (resultsPart.IndexOf(']') > 0)
{
// we have results, so trim off the trailing junk and parse out character escapes
resultsPart = resultsPart.Substring(1, resultsPart.IndexOf(']') - 2);
resultsPart.Replace("\\\\", "#");
int location;
string charValue;
while ((location = resultsPart.IndexOf("\\u")) != -1)
{
charValue = resultsPart.Substring(location + 2, 4);
resultsPart = resultsPart.Remove(location, 6).Insert(location,
Char.ConvertFromUtf32(int.Parse(charValue, NumberStyles.AllowHexSpecifier)));
}
resultsPart = resultsPart.Replace("\\\"", "\"").Replace("#", "\\");
results = resultsPart.Split(new string[] { "\",\"" }, StringSplitOptions.None);
}
else
{
// no results
results = new string[0];
}
//MessageBox.Show(json + "\n\n====================\n\n" + string.Join("\n", results));
ParentCoolCat.AddCategoryExistenceRange(results, true);
if (ParentCoolCat.DoesCategoryExist(cboCatName.Text) != true)
{
// does it exist? Go looking
MorebitsDotNet.GetApi(Wiki.Commons,
"format=json&action=query&titles=Category:" + Uri.EscapeDataString(cboCatName.Text),
cboCatName.Text, delegate(string innerJson, object catName)
{
// more crassness
bool exists = innerJson.IndexOf("\"-1\":") == -1;
SetCategoryExistenceIcon(exists);
ParentCoolCat.AddCategoryExistence((string)catName, exists);
FinishCategoryAutoComplete(results, exists ? (string)catName : null);
}
);
}
else
{
SetCategoryExistenceIcon(true);
FinishCategoryAutoComplete(results, null);
}
}
);
}