1+ using Flow . Launcher . Plugin . Everything . Everything ;
2+ using Flow . Launcher . Plugin . Everything . Everything . Exceptions ;
3+ using System ;
4+ using System . Collections . Generic ;
5+ using System . Runtime . InteropServices ;
6+ using System . Text ;
7+ using System . Threading ;
8+ using System . Threading . Tasks ;
9+
10+ namespace Flow . Launcher . Plugin . Explorer . Search . Everything
11+ {
12+
13+ public static class EverythingApi
14+ {
15+
16+ private const int BufferSize = 4096 ;
17+
18+ private static readonly object syncObject = new object ( ) ;
19+ // cached buffer to remove redundant allocations.
20+ private static readonly StringBuilder buffer = new StringBuilder ( BufferSize ) ;
21+
22+ public enum StateCode
23+ {
24+ OK ,
25+ MemoryError ,
26+ IPCError ,
27+ RegisterClassExError ,
28+ CreateWindowError ,
29+ CreateThreadError ,
30+ InvalidIndexError ,
31+ InvalidCallError
32+ }
33+
34+ /// <summary>
35+ /// Gets or sets a value indicating whether [match path].
36+ /// </summary>
37+ /// <value><c>true</c> if [match path]; otherwise, <c>false</c>.</value>
38+ public static bool MatchPath
39+ {
40+ get => EverythingApiDllImport . Everything_GetMatchPath ( ) ;
41+ set => EverythingApiDllImport . Everything_SetMatchPath ( value ) ;
42+ }
43+
44+ /// <summary>
45+ /// Gets or sets a value indicating whether [match case].
46+ /// </summary>
47+ /// <value><c>true</c> if [match case]; otherwise, <c>false</c>.</value>
48+ public static bool MatchCase
49+ {
50+ get => EverythingApiDllImport . Everything_GetMatchCase ( ) ;
51+ set => EverythingApiDllImport . Everything_SetMatchCase ( value ) ;
52+ }
53+
54+ /// <summary>
55+ /// Gets or sets a value indicating whether [match whole word].
56+ /// </summary>
57+ /// <value><c>true</c> if [match whole word]; otherwise, <c>false</c>.</value>
58+ public static bool MatchWholeWord
59+ {
60+ get => EverythingApiDllImport . Everything_GetMatchWholeWord ( ) ;
61+ set => EverythingApiDllImport . Everything_SetMatchWholeWord ( value ) ;
62+ }
63+
64+ /// <summary>
65+ /// Gets or sets a value indicating whether [enable regex].
66+ /// </summary>
67+ /// <value><c>true</c> if [enable regex]; otherwise, <c>false</c>.</value>
68+ public static bool EnableRegex
69+ {
70+ get => EverythingApiDllImport . Everything_GetRegex ( ) ;
71+ set => EverythingApiDllImport . Everything_SetRegex ( value ) ;
72+ }
73+
74+ /// <summary>
75+ /// Resets this instance.
76+ /// </summary>
77+ private static void Reset ( )
78+ {
79+ lock ( syncObject )
80+ {
81+ EverythingApiDllImport . Everything_Reset ( ) ;
82+ }
83+ }
84+
85+ /// <summary>
86+ /// Checks whether the sort option is Fast Sort.
87+ /// </summary>
88+ public static bool IsFastSortOption ( SortOption sortOption )
89+ {
90+ var fastSortOptionEnabled = EverythingApiDllImport . Everything_IsFastSort ( sortOption ) ;
91+
92+ // If the Everything service is not running, then this call will incorrectly report
93+ // the state as false. This checks for errors thrown by the api and up to the caller to handle.
94+ CheckAndThrowExceptionOnError ( ) ;
95+
96+ return fastSortOptionEnabled ;
97+ }
98+
99+ /// <summary>
100+ /// Searches the specified key word and reset the everything API afterwards
101+ /// </summary>
102+ /// <param name="keyword">The key word.</param>
103+ /// <param name="token">when cancelled the current search will stop and exit (and would not reset)</param>
104+ /// <param name="sortOption">Sort By</param>
105+ /// <param name="offset">The offset.</param>
106+ /// <param name="maxCount">The max count.</param>
107+ /// <returns></returns>
108+ public static IEnumerable < SearchResult > SearchAsync ( string keyword , CancellationToken token , SortOption sortOption = SortOption . NAME_ASCENDING , int offset = 0 , int maxCount = 100 )
109+ {
110+ if ( string . IsNullOrEmpty ( keyword ) )
111+ throw new ArgumentNullException ( nameof ( keyword ) ) ;
112+
113+ if ( offset < 0 )
114+ throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
115+
116+ if ( maxCount < 0 )
117+ throw new ArgumentOutOfRangeException ( nameof ( maxCount ) ) ;
118+
119+ lock ( syncObject )
120+ {
121+ if ( keyword . StartsWith ( "@" ) )
122+ {
123+ EverythingApiDllImport . Everything_SetRegex ( true ) ;
124+ keyword = keyword [ 1 ..] ;
125+ }
126+
127+ EverythingApiDllImport . Everything_SetSearchW ( keyword ) ;
128+ EverythingApiDllImport . Everything_SetOffset ( offset ) ;
129+ EverythingApiDllImport . Everything_SetMax ( maxCount ) ;
130+
131+ EverythingApiDllImport . Everything_SetSort ( sortOption ) ;
132+
133+ if ( token . IsCancellationRequested )
134+ {
135+ return null ;
136+ }
137+
138+
139+ if ( ! EverythingApiDllImport . Everything_QueryW ( true ) )
140+ {
141+ CheckAndThrowExceptionOnError ( ) ;
142+ return null ;
143+ }
144+
145+ var results = new List < SearchResult > ( ) ;
146+ for ( var idx = 0 ; idx < EverythingApiDllImport . Everything_GetNumResults ( ) ; ++ idx )
147+ {
148+ if ( token . IsCancellationRequested )
149+ {
150+ return null ;
151+ }
152+
153+ EverythingApiDllImport . Everything_GetResultFullPathNameW ( idx , buffer , BufferSize ) ;
154+
155+ var result = new SearchResult
156+ {
157+ FullPath = buffer . ToString ( ) ,
158+ Type = EverythingApiDllImport . Everything_IsFolderResult ( idx ) ? ResultType . Folder :
159+ EverythingApiDllImport . Everything_IsFileResult ( idx ) ? ResultType . File :
160+ ResultType . Volume
161+ } ;
162+
163+ results . Add ( result ) ;
164+ }
165+
166+ Reset ( ) ;
167+
168+ return results ;
169+ }
170+ }
171+
172+ private static void CheckAndThrowExceptionOnError ( )
173+ {
174+ switch ( EverythingApiDllImport . Everything_GetLastError ( ) )
175+ {
176+ case StateCode . CreateThreadError :
177+ throw new CreateThreadException ( ) ;
178+ case StateCode . CreateWindowError :
179+ throw new CreateWindowException ( ) ;
180+ case StateCode . InvalidCallError :
181+ throw new InvalidCallException ( ) ;
182+ case StateCode . InvalidIndexError :
183+ throw new InvalidIndexException ( ) ;
184+ case StateCode . IPCError :
185+ throw new IPCErrorException ( ) ;
186+ case StateCode . MemoryError :
187+ throw new MemoryErrorException ( ) ;
188+ case StateCode . RegisterClassExError :
189+ throw new RegisterClassExException ( ) ;
190+ }
191+ }
192+ }
193+ }
0 commit comments