/// <summary>
        /// Searches for results
        /// </summary>
        /// <param name="query">Search query object</param>
        /// <returns>List of Wox <see cref="Result"/>s.</returns>
        internal static List <Result> ExecuteSearch(Query query, string iconTheme)
        {
            List <AvailableResult> availableFormats = new List <AvailableResult>();
            List <Result>          results          = new List <Result>();
            bool   isKeywordSearch    = !string.IsNullOrEmpty(query.ActionKeyword);
            bool   isEmptySearchInput = string.IsNullOrEmpty(query.Search);
            string searchTerm         = query.Search;

            // Conjunction search without keyword => return no results
            // (This improves the results on global queries.)
            if (!isKeywordSearch && _conjunctionList.Any(x => x.Equals(searchTerm, StringComparison.CurrentCultureIgnoreCase)))
            {
                return(results);
            }

            // Switch search type
            if (isEmptySearchInput || (!isKeywordSearch && TimeDateSettings.Instance.OnlyDateTimeNowGlobal))
            {
                // Return all results for system time/date on empty keyword search
                // or only time, date and now results for system time on global queries if the corresponding setting is enabled
                availableFormats.AddRange(AvailableResultsList.GetList(isKeywordSearch));
            }
            else if (Regex.IsMatch(searchTerm, @".+" + Regex.Escape(InputDelimiter) + @".+"))
            {
                // Search for specified format with specified time/date value
                var userInput = searchTerm.Split(InputDelimiter);
                if (TimeAndDateHelper.ParseStringAsDateTime(userInput[1], out DateTime timestamp))
                {
                    availableFormats.AddRange(AvailableResultsList.GetList(isKeywordSearch, null, null, timestamp));
                    searchTerm = userInput[0];
                }
            }
            else if (TimeAndDateHelper.ParseStringAsDateTime(searchTerm, out DateTime timestamp))
            {
                // Return all formats for specified time/date value
                availableFormats.AddRange(AvailableResultsList.GetList(isKeywordSearch, null, null, timestamp));
                searchTerm = string.Empty;
            }
            else
            {
                // Search for specified format with system time/date (All other cases)
                availableFormats.AddRange(AvailableResultsList.GetList(isKeywordSearch));
            }

            // Check searchTerm after getting results to select type of result list
            if (string.IsNullOrEmpty(searchTerm))
            {
                // Generate list with all results
                foreach (var f in availableFormats)
                {
                    results.Add(new Result
                    {
                        Title             = f.Value,
                        SubTitle          = $"{f.Label} - {Resources.Microsoft_plugin_timedate_SubTitleNote}",
                        ToolTipData       = ResultHelper.GetSearchTagToolTip(f, out Visibility v),
                        ToolTipVisibility = v,
                        IcoPath           = f.GetIconPath(iconTheme),
                        Action            = _ => ResultHelper.CopyToClipBoard(f.Value),
                        ContextData       = f,
                    });
Esempio n. 2
0
        /// <summary>
        /// Returns a list with all available date time formats
        /// </summary>
        /// <param name="isKeywordSearch">Is this a search with plugin activation keyword or not</param>
        /// <param name="timeLong">Required for UnitTest: Show time in long format</param>
        /// <param name="dateLong">Required for UnitTest: Show date in long format</param>
        /// <param name="timestamp">Required for UnitTest: Use custom <see cref="DateTime"/> object to calculate results</param>
        /// <returns>List of results</returns>
        internal static List <AvailableResult> GetList(bool isKeywordSearch, bool?timeLong = null, bool?dateLong = null, DateTime?timestamp = null)
        {
            List <AvailableResult> results = new List <AvailableResult>();
            bool     timeExtended          = timeLong ?? TimeDateSettings.Instance.TimeWithSeconds;
            bool     dateExtended          = dateLong ?? TimeDateSettings.Instance.DateWithWeekday;
            bool     isSystemDateTime      = timestamp == null;
            Calendar calendar       = CultureInfo.CurrentCulture.Calendar;
            DateTime dateTimeNow    = timestamp ?? DateTime.Now;
            DateTime dateTimeNowUtc = dateTimeNow.ToUniversalTime();

            results.AddRange(new[]
            {
                // This range is reserved for the following three results: Time, Date, Now
                // Don't add any new result in this range! For new results, please use the next range.
                new AvailableResult()
                {
                    Value = dateTimeNow.ToString(TimeAndDateHelper.GetStringFormat(FormatStringType.Time, timeExtended, dateExtended), CultureInfo.CurrentCulture),
                    Label = Resources.Microsoft_plugin_timedate_Time,
                    AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, string.Empty, "Microsoft_plugin_timedate_SearchTagTimeNow"),
                    IconType             = ResultIconType.Time,
                },
                new AvailableResult()
                {
                    Value = dateTimeNow.ToString(TimeAndDateHelper.GetStringFormat(FormatStringType.Date, timeExtended, dateExtended), CultureInfo.CurrentCulture),
                    Label = Resources.Microsoft_plugin_timedate_Date,
                    AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, string.Empty, "Microsoft_plugin_timedate_SearchTagDateNow"),
                    IconType             = ResultIconType.Date,
                },
                new AvailableResult()
                {
                    Value = dateTimeNow.ToString(TimeAndDateHelper.GetStringFormat(FormatStringType.DateTime, timeExtended, dateExtended), CultureInfo.CurrentCulture),
                    Label = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_DateAndTime", "Microsoft_plugin_timedate_Now"),
                    AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
                    IconType             = ResultIconType.DateTime,
                },
            });

            if (isKeywordSearch || !TimeDateSettings.Instance.OnlyDateTimeNowGlobal)
            {
                // We use long instead of int  for unix time stamp because int ist to small after 03:14:07 UTC 2038-01-19
                long   unixTimestamp = (long)dateTimeNowUtc.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
                int    weekOfYear    = calendar.GetWeekOfYear(dateTimeNow, DateTimeFormatInfo.CurrentInfo.CalendarWeekRule, DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek);
                string era           = DateTimeFormatInfo.CurrentInfo.GetEraName(calendar.GetEra(dateTimeNow));
                string eraShort      = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedEraName(calendar.GetEra(dateTimeNow));

                results.AddRange(new[]
                {
                    new AvailableResult()
                    {
                        Value = dateTimeNowUtc.ToString(TimeAndDateHelper.GetStringFormat(FormatStringType.Time, timeExtended, dateExtended), CultureInfo.CurrentCulture),
                        Label = Resources.Microsoft_plugin_timedate_TimeUtc,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, string.Empty, "Microsoft_plugin_timedate_SearchTagTimeNow"),
                        IconType             = ResultIconType.Time,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNowUtc.ToString(TimeAndDateHelper.GetStringFormat(FormatStringType.DateTime, timeExtended, dateExtended), CultureInfo.CurrentCulture),
                        Label = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_DateAndTimeUtc", "Microsoft_plugin_timedate_NowUtc"),
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
                        IconType             = ResultIconType.DateTime,
                    },
                    new AvailableResult()
                    {
                        Value = unixTimestamp.ToString(CultureInfo.CurrentCulture),
                        Label = Resources.Microsoft_plugin_timedate_Unix,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
                        IconType             = ResultIconType.DateTime,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNow.Hour.ToString(CultureInfo.CurrentCulture),
                        Label = Resources.Microsoft_plugin_timedate_Hour,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagTime"),
                        IconType             = ResultIconType.Time,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNow.Minute.ToString(CultureInfo.CurrentCulture),
                        Label = Resources.Microsoft_plugin_timedate_Minute,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagTime"),
                        IconType             = ResultIconType.Time,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNow.Second.ToString(CultureInfo.CurrentCulture),
                        Label = Resources.Microsoft_plugin_timedate_Second,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagTime"),
                        IconType             = ResultIconType.Time,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNow.Millisecond.ToString(CultureInfo.CurrentCulture),
                        Label = Resources.Microsoft_plugin_timedate_Millisecond,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagTime"),
                        IconType             = ResultIconType.Time,
                    },
                    new AvailableResult()
                    {
                        Value = DateTimeFormatInfo.CurrentInfo.GetDayName(dateTimeNow.DayOfWeek),
                        Label = Resources.Microsoft_plugin_timedate_Day,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"),
                        IconType             = ResultIconType.Date,
                    },
                    new AvailableResult()
                    {
                        Value = TimeAndDateHelper.GetNumberOfDayInWeek(dateTimeNow).ToString(CultureInfo.CurrentCulture),
                        Label = Resources.Microsoft_plugin_timedate_DayOfWeek,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"),
                        IconType             = ResultIconType.Date,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNow.Day.ToString(CultureInfo.CurrentCulture),
                        Label = Resources.Microsoft_plugin_timedate_DayOfMonth,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"),
                        IconType             = ResultIconType.Date,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNow.DayOfYear.ToString(CultureInfo.CurrentCulture),
                        Label = Resources.Microsoft_plugin_timedate_DayOfYear,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"),
                        IconType             = ResultIconType.Date,
                    },
                    new AvailableResult()
                    {
                        Value = TimeAndDateHelper.GetWeekOfMonth(dateTimeNow).ToString(CultureInfo.CurrentCulture),
                        Label = Resources.Microsoft_plugin_timedate_WeekOfMonth,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"),
                        IconType             = ResultIconType.Date,
                    },
                    new AvailableResult()
                    {
                        Value = weekOfYear.ToString(CultureInfo.CurrentCulture),
                        Label = Resources.Microsoft_plugin_timedate_WeekOfYear,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"),
                        IconType             = ResultIconType.Date,
                    },
                    new AvailableResult()
                    {
                        Value = DateTimeFormatInfo.CurrentInfo.GetMonthName(dateTimeNow.Month),
                        Label = Resources.Microsoft_plugin_timedate_Month,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"),
                        IconType             = ResultIconType.Date,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNow.Month.ToString(CultureInfo.CurrentCulture),
                        Label = Resources.Microsoft_plugin_timedate_MonthOfYear,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"),
                        IconType             = ResultIconType.Date,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNow.ToString("M", CultureInfo.CurrentCulture),
                        Label = Resources.Microsoft_plugin_timedate_DayMonth,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"),
                        IconType             = ResultIconType.Date,
                    },
                    new AvailableResult()
                    {
                        Value = calendar.GetYear(dateTimeNow).ToString(CultureInfo.CurrentCulture),
                        Label = Resources.Microsoft_plugin_timedate_Year,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"),
                        IconType             = ResultIconType.Date,
                    },
                    new AvailableResult()
                    {
                        Value = era,
                        Label = Resources.Microsoft_plugin_timedate_Era,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagEra"),
                        IconType             = ResultIconType.Date,
                    },
                    new AvailableResult()
                    {
                        Value = era != eraShort ? eraShort : string.Empty, // Setting value to empty string if 'era == eraShort'. This result will be filtered later.
                        Label = Resources.Microsoft_plugin_timedate_EraAbbreviation,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagEra"),
                        IconType             = ResultIconType.Date,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNow.ToString("Y", CultureInfo.CurrentCulture),
                        Label = Resources.Microsoft_plugin_timedate_MonthYear,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagDate"),
                        IconType             = ResultIconType.Date,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNow.Ticks.ToString(CultureInfo.CurrentCulture),
                        Label = Resources.Microsoft_plugin_timedate_WindowsFileTime,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
                        IconType             = ResultIconType.DateTime,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNowUtc.ToString("u"),
                        Label = Resources.Microsoft_plugin_timedate_UniversalTime,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
                        IconType             = ResultIconType.DateTime,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNow.ToString("s"),
                        Label = Resources.Microsoft_plugin_timedate_Iso8601,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
                        IconType             = ResultIconType.DateTime,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNowUtc.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture),
                        Label = Resources.Microsoft_plugin_timedate_Iso8601Utc,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
                        IconType             = ResultIconType.DateTime,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNow.ToString("yyyy-MM-ddTHH:mm:ssK", CultureInfo.InvariantCulture),
                        Label = Resources.Microsoft_plugin_timedate_Iso8601Zone,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
                        IconType             = ResultIconType.DateTime,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNowUtc.ToString("yyyy-MM-ddTHH:mm:ss'Z'", CultureInfo.InvariantCulture),
                        Label = Resources.Microsoft_plugin_timedate_Iso8601ZoneUtc,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
                        IconType             = ResultIconType.DateTime,
                    },
                    new AvailableResult()
                    {
                        Value = dateTimeNow.ToString("R"),
                        Label = Resources.Microsoft_plugin_timedate_Rfc1123,
                        AlternativeSearchTag = ResultHelper.SelectStringFromResources(isSystemDateTime, "Microsoft_plugin_timedate_SearchTagFormat"),
                        IconType             = ResultIconType.DateTime,
                    },
                });
            }

            // Return only results where value is not empty
            // This can happen, for example, when we can't read the 'era' or when 'era == era abbreviation' and we set value explicitly to an empty string.
            return(results.Where(x => !string.IsNullOrEmpty(x.Value)).ToList());
        }