2015-01-26 17:46:55 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2015-11-01 00:02:56 +08:00
|
|
|
|
using System.Linq;
|
2013-12-19 23:51:20 +08:00
|
|
|
|
|
2014-01-29 18:33:24 +08:00
|
|
|
|
namespace Wox.Plugin
|
2013-12-19 23:51:20 +08:00
|
|
|
|
{
|
|
|
|
|
public class Query
|
|
|
|
|
{
|
2015-01-26 17:46:55 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Raw query, this includes action keyword if it has
|
|
|
|
|
/// We didn't recommend use this property directly. You should always use Search property.
|
|
|
|
|
/// </summary>
|
2015-11-01 00:02:56 +08:00
|
|
|
|
public string RawQuery { get; }
|
2015-01-26 17:46:55 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Search part of a query.
|
2015-02-06 18:13:22 +08:00
|
|
|
|
/// This will not include action keyword if exclusive plugin gets it, otherwise it should be same as RawQuery.
|
2015-11-01 00:02:56 +08:00
|
|
|
|
/// Since we allow user to switch a exclusive plugin to generic plugin,
|
|
|
|
|
/// so this property will always give you the "real" query part of the query
|
2015-01-26 17:46:55 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public string Search { get; internal set; }
|
|
|
|
|
|
2015-11-01 00:02:56 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The raw query splited into a string array.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string[] Terms { get; }
|
2015-01-26 17:46:55 +08:00
|
|
|
|
|
2015-11-01 00:02:56 +08:00
|
|
|
|
public const string Seperater = " ";
|
|
|
|
|
|
2015-11-02 08:09:42 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// * is used for System Plugin
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const string WildcardSign = "*";
|
|
|
|
|
|
2015-11-01 00:02:56 +08:00
|
|
|
|
internal string ActionKeyword { get; set; }
|
2015-01-26 17:46:55 +08:00
|
|
|
|
|
2015-02-04 23:16:41 +08:00
|
|
|
|
internal bool IsIntantQuery { get; set; }
|
|
|
|
|
|
2015-01-26 17:46:55 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Return first search split by space if it has
|
|
|
|
|
/// </summary>
|
2015-11-01 00:02:56 +08:00
|
|
|
|
public string FirstSearch => SplitSearch(0);
|
2015-01-26 17:46:55 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// strings from second search (including) to last search
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string SecondToEndSearch
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2015-11-02 03:47:20 +08:00
|
|
|
|
var index = String.IsNullOrEmpty(ActionKeyword) ? 1 : 2;
|
|
|
|
|
return String.Join(Seperater, Terms.Skip(index).ToArray());
|
2015-01-26 17:46:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Return second search split by space if it has
|
|
|
|
|
/// </summary>
|
2015-11-01 00:02:56 +08:00
|
|
|
|
public string SecondSearch => SplitSearch(1);
|
2015-01-26 17:46:55 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Return third search split by space if it has
|
|
|
|
|
/// </summary>
|
2015-11-01 00:02:56 +08:00
|
|
|
|
public string ThirdSearch => SplitSearch(2);
|
2015-01-26 17:46:55 +08:00
|
|
|
|
|
|
|
|
|
private string SplitSearch(int index)
|
|
|
|
|
{
|
2015-11-01 00:02:56 +08:00
|
|
|
|
try
|
2015-01-26 17:46:55 +08:00
|
|
|
|
{
|
2015-11-02 03:47:20 +08:00
|
|
|
|
return String.IsNullOrEmpty(ActionKeyword) ? Terms[index] : Terms[index + 1];
|
2015-11-01 00:02:56 +08:00
|
|
|
|
}
|
|
|
|
|
catch (IndexOutOfRangeException)
|
|
|
|
|
{
|
2015-11-02 03:47:20 +08:00
|
|
|
|
return String.Empty;
|
2015-01-26 17:46:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-01 00:02:56 +08:00
|
|
|
|
public override string ToString() => RawQuery;
|
2015-01-26 17:46:55 +08:00
|
|
|
|
|
|
|
|
|
[Obsolete("Use Search instead, A plugin developer shouldn't care about action name, as it may changed by users. " +
|
|
|
|
|
"this property will be removed in v1.3.0")]
|
2013-12-19 23:51:20 +08:00
|
|
|
|
public string ActionName { get; private set; }
|
2015-01-26 17:46:55 +08:00
|
|
|
|
|
|
|
|
|
[Obsolete("Use Search instead, this property will be removed in v1.3.0")]
|
2013-12-19 23:51:20 +08:00
|
|
|
|
public List<string> ActionParameters { get; private set; }
|
|
|
|
|
|
|
|
|
|
public Query(string rawQuery)
|
|
|
|
|
{
|
2015-11-01 00:02:56 +08:00
|
|
|
|
// replace multiple white spaces with one white space
|
|
|
|
|
Terms = rawQuery.Split(new[] { Seperater }, StringSplitOptions.RemoveEmptyEntries);
|
2015-11-02 03:47:20 +08:00
|
|
|
|
RawQuery = String.Join(Seperater, Terms.ToArray());
|
2015-11-01 00:02:56 +08:00
|
|
|
|
|
2013-12-20 19:38:10 +08:00
|
|
|
|
ActionParameters = new List<string>();
|
2013-12-19 23:51:20 +08:00
|
|
|
|
ParseQuery();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ParseQuery()
|
|
|
|
|
{
|
2015-11-02 03:47:20 +08:00
|
|
|
|
if (String.IsNullOrEmpty(RawQuery)) return;
|
2013-12-20 19:38:10 +08:00
|
|
|
|
|
|
|
|
|
string[] strings = RawQuery.Split(' ');
|
2014-02-28 23:21:01 +08:00
|
|
|
|
//todo:not exactly correct. query that didn't containing a space should be a valid query
|
2013-12-22 00:44:56 +08:00
|
|
|
|
if (strings.Length == 1) return; //we consider a valid query must contain a space
|
|
|
|
|
|
2013-12-20 19:38:10 +08:00
|
|
|
|
ActionName = strings[0];
|
2013-12-22 00:44:56 +08:00
|
|
|
|
for (int i = 1; i < strings.Length; i++)
|
2013-12-20 19:38:10 +08:00
|
|
|
|
{
|
2015-11-02 03:47:20 +08:00
|
|
|
|
if (!String.IsNullOrEmpty(strings[i]))
|
2013-12-20 19:38:10 +08:00
|
|
|
|
{
|
|
|
|
|
ActionParameters.Add(strings[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-19 23:51:20 +08:00
|
|
|
|
}
|
2014-02-28 23:21:01 +08:00
|
|
|
|
|
2015-01-26 17:46:55 +08:00
|
|
|
|
[Obsolete("Use Search instead, this method will be removed in v1.3.0")]
|
2014-02-28 23:21:01 +08:00
|
|
|
|
public string GetAllRemainingParameter()
|
|
|
|
|
{
|
2014-03-17 01:53:51 +08:00
|
|
|
|
|
2015-11-02 03:47:20 +08:00
|
|
|
|
string[] strings = RawQuery.Split(new char[] { ' ' }, 2, StringSplitOptions.None);
|
2014-02-28 23:21:01 +08:00
|
|
|
|
if (strings.Length > 1)
|
|
|
|
|
{
|
2014-03-17 01:53:51 +08:00
|
|
|
|
return strings[1];
|
2014-02-28 23:21:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-02 03:47:20 +08:00
|
|
|
|
return String.Empty;
|
2014-02-28 23:21:01 +08:00
|
|
|
|
}
|
2015-11-02 03:47:20 +08:00
|
|
|
|
|
2015-11-02 08:09:42 +08:00
|
|
|
|
|
2013-12-19 23:51:20 +08:00
|
|
|
|
}
|
|
|
|
|
}
|