@@ -31,39 +31,78 @@ internal static bool ProcessImport()
3131
3232 // get the shortcuts
3333 var searchOption = Variables . Settings . ShortcutSearchRecusively ? SearchOption . AllDirectories : SearchOption . TopDirectoryOnly ;
34- var shortcuts = Directory . EnumerateFiles ( Variables . Settings . ShortcutSourceFolder , "*.lnk" , searchOption ) ? . ToArray ( ) ;
34+ var ( lnkSuccesful , lnkNewFiles ) = ProcessLnks ( searchOption ) ;
35+ var ( urlSuccesful , urlNewFiles ) = ProcessUrls ( searchOption ) ;
3536
36- if ( ! shortcuts . Any ( ) )
37+ // new shortcuts?
38+ if ( lnkNewFiles || urlNewFiles )
3739 {
38- Log . Information ( "[SHORTCUTS] No shortcuts found, nothing to process" ) ;
39- return true ;
40+ // yep, store them
41+ var serializedShortcuts = JsonConvert . SerializeObject ( Variables . ProcessedShortcuts , Formatting . Indented ) . RemoveBackslashEscaping ( ) ;
42+ File . WriteAllText ( Variables . ProcessedShortcutsFile , serializedShortcuts ) ;
43+
44+ // restart hass.agent
45+ if ( Variables . Settings . RestartHASSAgentOnNewItems ) HASSAgentManager . Restart ( ) ;
4046 }
47+ else Log . Information ( "[SHORTCUTS] No shortcuts found, nothing to process" ) ;
48+
49+ // done
50+ return lnkSuccesful && urlSuccesful ;
51+ }
52+ catch ( Exception ex )
53+ {
54+ Log . Fatal ( ex , "[SHORTCUTS] Importing failed: {err}" , ex . Message ) ;
55+ return false ;
56+ }
57+ }
58+
59+ private static ( bool succesful , bool newFiles ) ProcessLnks ( SearchOption searchOption )
60+ {
61+ var newFiles = 0 ;
62+
63+ try
64+ {
65+ var shortcuts = Directory . EnumerateFiles ( Variables . Settings . ShortcutSourceFolder , "*.lnk" , searchOption ) ? . ToArray ( ) ;
66+ if ( ! shortcuts . Any ( ) ) return ( true , false ) ;
4167
4268 // iterate them
43- var newFiles = 0 ;
4469 foreach ( var shortcutFile in shortcuts )
4570 {
71+ var fileName = Path . GetFileName ( shortcutFile ) ;
72+
4673 var shortcut = Shortcut . ReadFromFile ( shortcutFile ) ;
4774 if ( shortcut == null )
4875 {
49- Log . Warning ( "[SHORTCUTS] Shortcut parsing failed for: {file}" , Path . GetFileName ( shortcutFile ) ) ;
76+ Log . Warning ( "[SHORTCUTS] Shortcut parsing failed for: {file}" , fileName ) ;
5077 continue ;
5178 }
52-
79+
5380 if ( Variables . ProcessedShortcuts . Any ( x => x . LinkFile == shortcutFile ) )
5481 {
5582 // already know it, skip
5683 continue ;
5784 }
5885
5986 var name = Path . GetFileNameWithoutExtension ( shortcutFile ) ;
60- var target = shortcut . LinkTargetIDList . Path ;
61- var args = shortcut . StringData . CommandLineArguments ;
62-
63- Log . Information ( "[SHORTCUTS] New shortcut found, importing into HASS.Agent:" ) ;
87+ var target = shortcut . LinkTargetIDList ? . Path ?? string . Empty ;
88+ var args = shortcut . StringData ? . CommandLineArguments ?? string . Empty ;
89+
90+ if ( string . IsNullOrWhiteSpace ( target ) )
91+ {
92+ Log . Warning ( "[SHORTCUTS] Shortcut contained empty or no target: {file}" , fileName ) ;
93+ continue ;
94+ }
95+
96+ Log . Information ( "[SHORTCUTS] New shortcut found, importing into HASS.Agent:\r \n " ) ;
97+ Log . Information ( "[SHORTCUTS] Type: {type}" , ".lnk" ) ;
6498 Log . Information ( "[SHORTCUTS] Name: {name}" , name ) ;
65- Log . Information ( "[SHORTCUTS] Target: {target}" , target ) ;
66- if ( ! string . IsNullOrWhiteSpace ( args ) ) Log . Information ( "[SHORTCUTS] Arguments: {args}" , args ) ;
99+ if ( string . IsNullOrWhiteSpace ( args ) ) Log . Information ( "[SHORTCUTS] Target: {target}\r \n " , target ) ;
100+ else
101+ {
102+ Log . Information ( "[SHORTCUTS] Target: {target}" , target ) ;
103+ Log . Information ( "[SHORTCUTS] Arguments: {args}\r \n " , args ) ;
104+
105+ }
67106
68107 var processedShortcut = new ProcessedShortcut
69108 {
@@ -74,7 +113,7 @@ internal static bool ProcessImport()
74113 CommandGuid = Guid . NewGuid ( ) . ToString ( ) ,
75114 SensorGuid = Guid . NewGuid ( ) . ToString ( )
76115 } ;
77-
116+
78117 var imported = HASSAgentManager . ProcessShortcut ( processedShortcut ) ;
79118 if ( ! imported )
80119 {
@@ -88,23 +127,85 @@ internal static bool ProcessImport()
88127 Log . Information ( "[SHORTCUTS] Succesfully imported\r \n " ) ;
89128 }
90129
91- // new shortcuts?
92- if ( newFiles > 0 )
130+ return ( true , newFiles > 0 ) ;
131+ }
132+ catch ( Exception ex )
133+ {
134+ return ( false , newFiles > 0 ) ;
135+ }
136+ }
137+
138+ private static ( bool succesful , bool newFiles ) ProcessUrls ( SearchOption searchOption )
139+ {
140+ var newFiles = 0 ;
141+
142+ try
143+ {
144+ var shortcuts = Directory . EnumerateFiles ( Variables . Settings . ShortcutSourceFolder , "*.url" , searchOption ) ? . ToArray ( ) ;
145+ if ( ! shortcuts . Any ( ) ) return ( true , false ) ;
146+
147+ // iterate them
148+ foreach ( var shortcutFile in shortcuts )
93149 {
94- // yep, store them
95- File . WriteAllText ( Variables . ProcessedShortcutsFile , JsonConvert . SerializeObject ( Variables . ProcessedShortcuts , Formatting . Indented ) ) ;
150+ var fileName = Path . GetFileName ( shortcutFile ) ;
96151
97- // restart hass.agent
98- if ( Variables . Settings . RestartHASSAgentOnNewItems ) HASSAgentManager . Restart ( ) ;
152+ var ( parsed , target ) = HelperFunctions . ParseUrl ( shortcutFile ) ;
153+ if ( ! parsed )
154+ {
155+ Log . Warning ( "[SHORTCUTS] Shortcut parsing failed for: {file}" , fileName ) ;
156+ continue ;
157+ }
158+
159+ if ( string . IsNullOrEmpty ( target ) )
160+ {
161+ Log . Warning ( "[SHORTCUTS] Shortcut contained empty or no URL: {file}" , fileName ) ;
162+ continue ;
163+ }
164+
165+ if ( Variables . ProcessedShortcuts . Any ( x => x . LinkFile == shortcutFile ) )
166+ {
167+ // already know it, skip
168+ continue ;
169+ }
170+
171+ var name = Path . GetFileNameWithoutExtension ( shortcutFile ) ;
172+
173+ Log . Information ( "[SHORTCUTS] New shortcut found, importing into HASS.Agent:\r \n " ) ;
174+ Log . Information ( "[SHORTCUTS] Type: {type}" , ".url" ) ;
175+ Log . Information ( "[SHORTCUTS] Name: {name}" , name ) ;
176+ Log . Information ( "[SHORTCUTS] Target: {target}\r \n " , target ) ;
177+
178+ // create the shortcut with the right command
179+ var processedShortcut = new ProcessedShortcut
180+ {
181+ LinkFile = shortcutFile ,
182+ Name = name ,
183+ Target = $ "start { target } ",
184+ CommandGuid = Guid . NewGuid ( ) . ToString ( ) ,
185+ SensorGuid = Guid . NewGuid ( ) . ToString ( )
186+ } ;
187+
188+ var imported = HASSAgentManager . ProcessShortcut ( processedShortcut ) ;
189+ if ( ! imported )
190+ {
191+ Log . Error ( "[SHORTCUTS] Import failed\r \n " ) ;
192+ continue ;
193+ }
194+
195+ // restore the target
196+ processedShortcut . Target = target ;
197+
198+ newFiles ++ ;
199+ Variables . ProcessedShortcuts . Add ( processedShortcut ) ;
200+
201+ Log . Information ( "[SHORTCUTS] Succesfully imported\r \n " ) ;
99202 }
100203
101- // done
102- return true ;
204+ return ( true , newFiles > 0 ) ;
103205 }
104206 catch ( Exception ex )
105207 {
106- Log . Fatal ( ex , "[SHORTCUTS] Importing failed: {err}" , ex . Message ) ;
107- return false ;
208+ return ( false , newFiles > 0 ) ;
108209 }
109210 }
110211 }
0 commit comments