2020-05-17 09:19:55 +00:00
using HeliosPlus.GameLibraries ;
using HeliosPlus.Resources ;
using HeliosPlus.Shared ;
using Manina.Windows.Forms ;
2020-05-16 11:16:46 +00:00
using System ;
2020-05-16 05:07:52 +00:00
using System.Collections.Generic ;
using System.ComponentModel ;
using System.Data ;
using System.Drawing ;
2020-05-17 09:19:55 +00:00
using System.Drawing.IconLib ;
using System.Globalization ;
using System.IO ;
2020-05-16 05:07:52 +00:00
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.Windows.Forms ;
namespace HeliosPlus.UIForms
{
public partial class ShortcutLibraryForm : Form
{
2020-05-16 11:16:46 +00:00
2020-07-15 08:11:38 +00:00
private ShortcutAdaptor _shortcutAdaptor = new ShortcutAdaptor ( ) ;
2020-07-23 23:06:33 +00:00
//private ImageListViewItem _selectedShortcutILVItem = null;
2020-06-07 08:48:45 +00:00
private ShortcutItem _selectedShortcut = null ;
2020-05-16 11:16:46 +00:00
2020-05-16 05:07:52 +00:00
public ShortcutLibraryForm ( )
{
InitializeComponent ( ) ;
2020-07-15 08:11:38 +00:00
//_shortcutAdaptor = new ShortcutAdaptor();
//_shortcutRepository = new ShortcutRepository();
//_profileRepository = new ProfileRepository();
2020-07-24 04:51:48 +00:00
ilv_saved_shortcuts . MultiSelect = false ;
ilv_saved_shortcuts . ThumbnailSize = new Size ( 100 , 100 ) ;
ilv_saved_shortcuts . AllowDrag = false ;
ilv_saved_shortcuts . AllowDrop = false ;
2020-05-16 05:07:52 +00:00
}
private void btn_back_Click ( object sender , EventArgs e )
{
this . Close ( ) ;
}
2020-05-16 11:16:46 +00:00
private void ShortcutLibraryForm_Load ( object sender , EventArgs e )
{
2020-05-17 09:19:55 +00:00
// Refresh the Shortcut Library UI
RefreshShortcutLibraryUI ( ) ;
2020-05-16 11:16:46 +00:00
}
2020-05-17 09:19:55 +00:00
private void RefreshShortcutLibraryUI ( )
2020-05-16 11:16:46 +00:00
{
2020-07-24 04:51:48 +00:00
if ( ShortcutRepository . ShortcutCount = = 0 )
return ;
2020-05-16 11:16:46 +00:00
2020-07-24 04:51:48 +00:00
// Temporarily stop updating the saved_profiles listview
ilv_saved_shortcuts . SuspendLayout ( ) ;
2020-05-16 11:16:46 +00:00
2020-07-24 04:51:48 +00:00
ImageListViewItem newItem = null ;
ilv_saved_shortcuts . Items . Clear ( ) ;
2020-10-20 08:22:07 +00:00
foreach ( ShortcutItem loadedShortcut in ShortcutRepository . AllShortcuts . OrderBy ( s = > s . Name ) )
2020-07-24 04:51:48 +00:00
{
//loadedProfile.SaveProfileImageToCache();
//newItem = new ImageListViewItem(loadedProfile.SavedProfileCacheFilename, loadedProfile.Name);
//newItem = new ImageListViewItem(loadedProfile, loadedProfile.Name);
newItem = new ImageListViewItem ( loadedShortcut , loadedShortcut . Name ) ;
2020-06-07 08:48:45 +00:00
2020-07-24 04:51:48 +00:00
// Select it if its the selectedProfile
if ( _selectedShortcut is ShortcutItem & & _selectedShortcut . Equals ( loadedShortcut ) )
newItem . Selected = true ;
2020-05-16 11:16:46 +00:00
2020-07-24 04:51:48 +00:00
//ilv_saved_profiles.Items.Add(newItem);
ilv_saved_shortcuts . Items . Add ( newItem , _shortcutAdaptor ) ;
2020-05-16 11:16:46 +00:00
}
2020-06-07 08:48:45 +00:00
2020-05-19 09:41:26 +00:00
2020-07-24 04:51:48 +00:00
// Restart updating the saved_profiles listview
ilv_saved_shortcuts . ResumeLayout ( ) ;
2020-05-19 09:41:26 +00:00
}
2020-07-24 04:51:48 +00:00
2020-06-07 08:48:45 +00:00
private ShortcutItem GetShortcutFromName ( string shortcutName )
2020-05-19 09:41:26 +00:00
{
2020-06-01 10:25:52 +00:00
return ( from item in ShortcutRepository . AllShortcuts where item . Name = = shortcutName select item ) . First ( ) ;
2020-05-16 11:16:46 +00:00
}
2020-05-17 09:19:55 +00:00
private void btn_save_Click ( object sender , EventArgs e )
{
DialogResult = DialogResult . None ;
// Only do something if there is a shortcut selected
if ( _selectedShortcut ! = null )
{
try
{
// Set the Shortcut save folder to the Desktop as that's where people will want it most likely
dialog_save . InitialDirectory = Environment . GetFolderPath ( Environment . SpecialFolder . Desktop ) ;
// Try to set up some sensible suggestions for the Shortcut name
if ( _selectedShortcut . Permanence = = ShortcutPermanence . Permanent )
{
dialog_save . FileName = _selectedShortcut . Name ;
}
else
{
if ( _selectedShortcut . Category = = ShortcutCategory . Application )
{
dialog_save . FileName = String . Concat ( Path . GetFileNameWithoutExtension ( _selectedShortcut . ExecutableNameAndPath ) , @" (" , _selectedShortcut . Name . ToLower ( CultureInfo . InvariantCulture ) , @")" ) ;
}
else
{
2020-07-21 07:50:41 +00:00
dialog_save . FileName = _selectedShortcut . Name ;
2020-05-17 09:19:55 +00:00
}
}
// Show the Save Shortcut window
if ( dialog_save . ShowDialog ( this ) = = DialogResult . OK )
{
if ( _selectedShortcut . CreateShortcut ( dialog_save . FileName ) )
{
MessageBox . Show (
2020-07-21 07:50:41 +00:00
String . Format ( Language . Shortcut_placed_successfully , dialog_save . FileName ) ,
2020-05-17 09:19:55 +00:00
Language . Shortcut ,
MessageBoxButtons . OK ,
MessageBoxIcon . Information ) ;
}
else
{
MessageBox . Show (
Language . Failed_to_create_the_shortcut_Unexpected_exception_occurred ,
Language . Shortcut ,
MessageBoxButtons . OK ,
MessageBoxIcon . Exclamation ) ;
}
dialog_save . FileName = string . Empty ;
DialogResult = DialogResult . OK ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . Message , Language . Shortcut , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
}
}
}
2020-05-19 09:41:26 +00:00
private void ilv_saved_shortcuts_ItemClick ( object sender , ItemClickEventArgs e )
{
_selectedShortcut = GetShortcutFromName ( e . Item . Text ) ;
}
private void ilv_saved_shortcuts_ItemDoubleClick ( object sender , ItemClickEventArgs e )
{
_selectedShortcut = GetShortcutFromName ( e . Item . Text ) ;
if ( _selectedShortcut = = null )
return ;
2020-10-20 08:22:07 +00:00
ShortcutItem oldShortcut = _selectedShortcut ;
2020-05-19 09:41:26 +00:00
var shortcutForm = new ShortcutForm ( _selectedShortcut ) ;
shortcutForm . ShowDialog ( this ) ;
if ( shortcutForm . DialogResult = = DialogResult . OK )
{
2020-10-20 08:22:07 +00:00
ShortcutRepository . RemoveShortcut ( oldShortcut ) ;
_selectedShortcut = shortcutForm . Shortcut ;
ShortcutRepository . AddShortcut ( _selectedShortcut ) ;
2020-05-19 09:41:26 +00:00
RefreshShortcutLibraryUI ( ) ;
2020-10-20 08:22:07 +00:00
// As this is an edit, we need to manually force saving the shortcut library
//ShortcutRepository.SaveShortcuts();
2020-05-19 09:41:26 +00:00
}
}
2020-07-24 04:51:48 +00:00
private void btn_new_Click ( object sender , EventArgs e )
{
var shortcutForm = new ShortcutForm ( ) ;
shortcutForm . ShowDialog ( this ) ;
if ( shortcutForm . DialogResult = = DialogResult . OK )
{
ShortcutRepository . AddShortcut ( shortcutForm . Shortcut ) ;
_selectedShortcut = shortcutForm . Shortcut ;
RefreshShortcutLibraryUI ( ) ;
}
}
2020-05-19 09:41:26 +00:00
private void btn_edit_Click ( object sender , EventArgs e )
{
2020-10-20 08:22:07 +00:00
int currentIlvIndex = ilv_saved_shortcuts . SelectedItems [ 0 ] . Index ;
string shortcutName = ilv_saved_shortcuts . Items [ currentIlvIndex ] . Text ;
_selectedShortcut = GetShortcutFromName ( shortcutName ) ;
2020-05-19 09:41:26 +00:00
if ( _selectedShortcut = = null )
return ;
2020-10-20 08:22:07 +00:00
ShortcutItem oldShortcut = _selectedShortcut ;
2020-05-19 09:41:26 +00:00
var shortcutForm = new ShortcutForm ( _selectedShortcut ) ;
shortcutForm . ShowDialog ( this ) ;
if ( shortcutForm . DialogResult = = DialogResult . OK )
{
2020-10-20 08:22:07 +00:00
ShortcutRepository . RemoveShortcut ( oldShortcut ) ;
_selectedShortcut = shortcutForm . Shortcut ;
ShortcutRepository . AddShortcut ( _selectedShortcut ) ;
2020-05-19 09:41:26 +00:00
RefreshShortcutLibraryUI ( ) ;
2020-10-20 08:22:07 +00:00
// As this is an edit, we need to manually force saving the shortcut library
//ShortcutRepository.SaveShortcuts();
2020-05-19 09:41:26 +00:00
}
}
2020-06-01 10:25:52 +00:00
private void btn_delete_Click ( object sender , EventArgs e )
{
if ( _selectedShortcut = = null )
return ;
if ( MessageBox . Show ( $"Are you sure you want to delete the '{_selectedShortcut.Name}' Shortcut?" , $"Delete '{_selectedShortcut.Name}' Shortcut?" , MessageBoxButtons . YesNo , MessageBoxIcon . Error ) = = DialogResult . No )
return ;
2020-06-07 08:48:45 +00:00
// remove the profile from the imagelistview
int currentIlvIndex = ilv_saved_shortcuts . SelectedItems [ 0 ] . Index ;
ilv_saved_shortcuts . Items . RemoveAt ( currentIlvIndex ) ;
// Remove the shortcut
2020-06-01 10:25:52 +00:00
ShortcutRepository . RemoveShortcut ( _selectedShortcut ) ;
_selectedShortcut = null ;
2020-10-20 08:22:07 +00:00
RefreshShortcutLibraryUI ( ) ;
2020-06-01 10:25:52 +00:00
}
2020-07-23 06:31:00 +00:00
private void btn_run_Click ( object sender , EventArgs e )
{
if ( _selectedShortcut = = null )
return ;
2020-11-21 01:48:32 +00:00
// Figure out the string we're going to use as the MaskedForm message
string message = "" ;
if ( _selectedShortcut . Category . Equals ( ShortcutCategory . Application ) )
message = $"Starting the {_selectedShortcut.ExecutableNameAndPath} application and waiting until you close it." ;
else if ( _selectedShortcut . Category . Equals ( ShortcutCategory . Game ) )
message = $"Starting the {_selectedShortcut.GameName} game and waiting until you close it." ;
// Create a MaskForm that will cover the ShortcutLibrary Window to lock
// the controls and inform the user that the game is running....
MaskedForm maskedForm = MaskedForm . Show ( this , message ) ;
// Get the MainForm so we can access the NotifyIcon on it.
MainForm mainForm = ( MainForm ) this . Owner ;
2020-11-19 09:40:08 +00:00
2020-07-23 06:31:00 +00:00
// Run the shortcut
2020-11-21 01:48:32 +00:00
ShortcutRepository . RunShortcut ( _selectedShortcut , mainForm . notifyIcon ) ;
maskedForm . Close ( ) ;
2020-07-23 06:31:00 +00:00
}
2020-05-16 05:07:52 +00:00
}
}