2025-04-02 10:25:28 +00:00
using System ;
2020-07-21 22:18:36 +00:00
using System.IO ;
2022-10-30 19:23:04 +00:00
using System.Threading.Tasks ;
2023-04-25 12:04:08 +00:00
#pragma warning disable IDE0005
2020-08-11 22:37:56 +00:00
using System.Windows ;
2023-04-25 12:04:08 +00:00
#pragma warning restore IDE0005
2020-08-11 22:23:10 +00:00
using System.Windows.Media ;
2020-07-21 22:18:36 +00:00
namespace Flow.Launcher.Plugin.WebSearch
2016-06-20 23:14:32 +00:00
{
2020-07-22 10:35:56 +00:00
public class SearchSourceViewModel : BaseModel
2016-06-20 23:14:32 +00:00
{
public SearchSource SearchSource { get ; set ; }
2020-07-21 22:18:36 +00:00
2020-08-11 21:12:43 +00:00
public void UpdateIconAttributes ( SearchSource selectedSearchSource , string fullpathToSelectedImage )
2020-07-21 22:18:36 +00:00
{
2020-07-22 12:00:14 +00:00
var parentDirectorySelectedImg = Directory . GetParent ( fullpathToSelectedImage ) . ToString ( ) ;
2020-08-11 21:08:24 +00:00
selectedSearchSource . CustomIcon = parentDirectorySelectedImg ! = Main . DefaultImagesDirectory ;
2020-07-22 12:00:14 +00:00
2020-07-21 22:18:36 +00:00
var iconFileName = Path . GetFileName ( fullpathToSelectedImage ) ;
selectedSearchSource . Icon = iconFileName ;
}
2020-07-22 12:00:14 +00:00
public void CopyNewImageToUserDataDirectoryIfRequired (
SearchSource selectedSearchSource , string fullpathToSelectedImage , string fullPathToOriginalImage )
2020-07-21 22:18:36 +00:00
{
2020-08-11 21:08:24 +00:00
var destinationFileNameFullPath = Path . Combine ( Main . CustomImagesDirectory , Path . GetFileName ( fullpathToSelectedImage ) ) ;
2020-07-21 22:18:36 +00:00
2020-07-22 12:00:14 +00:00
var parentDirectorySelectedImg = Directory . GetParent ( fullpathToSelectedImage ) . ToString ( ) ;
2020-07-22 10:35:56 +00:00
2020-08-11 21:08:24 +00:00
if ( parentDirectorySelectedImg ! = Main . CustomImagesDirectory & & parentDirectorySelectedImg ! = Main . DefaultImagesDirectory )
2020-07-21 22:18:36 +00:00
{
2020-07-22 12:00:14 +00:00
try
{
File . Copy ( fullpathToSelectedImage , destinationFileNameFullPath ) ;
}
2022-08-09 00:35:38 +00:00
catch ( Exception )
2020-07-22 12:00:14 +00:00
{
2020-07-21 22:18:36 +00:00
#if DEBUG
2022-08-09 00:35:38 +00:00
throw ;
2020-07-21 22:18:36 +00:00
#else
2025-04-02 10:25:28 +00:00
Main . _context . API . ShowMsgBox ( string . Format ( "Copying the selected image file to {0} has failed, changes will now be reverted" , destinationFileNameFullPath ) ) ;
UpdateIconAttributes ( selectedSearchSource , fullPathToOriginalImage ) ;
2020-07-21 22:18:36 +00:00
#endif
2020-07-22 12:00:14 +00:00
}
2020-07-21 22:18:36 +00:00
}
}
2020-07-22 12:00:14 +00:00
internal void SetupCustomImagesDirectory ( )
2020-07-21 22:18:36 +00:00
{
2020-08-11 21:08:24 +00:00
if ( ! Directory . Exists ( Main . CustomImagesDirectory ) )
Directory . CreateDirectory ( Main . CustomImagesDirectory ) ;
2020-07-22 12:00:14 +00:00
}
2020-07-21 22:18:36 +00:00
2020-07-22 12:00:14 +00:00
internal bool ShouldProvideHint ( string fullPathToSelectedImage )
{
2020-08-11 21:08:24 +00:00
return Directory . GetParent ( fullPathToSelectedImage ) . ToString ( ) = = Main . DefaultImagesDirectory ;
2020-07-21 22:18:36 +00:00
}
2020-08-11 22:23:10 +00:00
2022-10-30 19:23:04 +00:00
internal async ValueTask < ImageSource > LoadPreviewIconAsync ( string pathToPreviewIconImage )
2020-08-11 22:23:10 +00:00
{
2025-04-02 10:25:28 +00:00
return await Main . _context . API . LoadImageAsync ( pathToPreviewIconImage ) ;
2020-08-11 22:23:10 +00:00
}
2016-06-20 23:14:32 +00:00
}
2022-03-03 23:00:07 +00:00
}