using System.ComponentModel;
using System.Diagnostics;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using MediaDevices;
using System.Linq;
namespace ReSync;
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window, INotifyPropertyChanged
{
#region Variablen
private string _phoneFolderPath;
private string _backupFolderPath;
private string _encodedOutputFolderPath;
private string _phoneConnected;
private string _newFiles;
private string _backupsPending;
private string _encodingsPending;
private string _lastSync;
private string _dbEntries;
private List _devicesList = new();
private List _devicesNameList = new();
private string _selectedDevice;
private MediaDevice _selectedDeviceObject;
public List DevicesNameList
{
get => _devicesNameList;
set
{
if (_devicesNameList != value)
{
_devicesNameList = value;
OnPropertyChanged(nameof(DevicesNameList));
}
}
}
public MediaDevice SelectedDeviceObject
{
get => _selectedDeviceObject;
set
{
if (_selectedDeviceObject != value)
{
_selectedDeviceObject = value;
OnPropertyChanged(nameof(SelectedDeviceObject));
}
}
}
public string SelectedDevice
{
get => _selectedDevice;
set
{
if (_selectedDevice != value)
{
_selectedDevice = value;
OnPropertyChanged(nameof(SelectedDevice));
}
}
}
public List DevicesList
{
get => _devicesList;
set
{
if (_devicesList != value)
{
_devicesList = value;
OnPropertyChanged(nameof(DevicesList));
}
}
}
public string PhoneFolderPath
{
get => _phoneFolderPath;
set
{
if (_phoneFolderPath != value)
{
_phoneFolderPath = value;
OnPropertyChanged(nameof(PhoneFolderPath));
}
}
}
public string BackupFolderPath
{
get => _backupFolderPath;
set
{
if (_backupFolderPath != value)
{
_backupFolderPath = value;
OnPropertyChanged(nameof(BackupFolderPath));
}
}
}
public string EncodedOutputFolderPath
{
get => _encodedOutputFolderPath;
set
{
if (_encodedOutputFolderPath != value)
{
_encodedOutputFolderPath = value;
OnPropertyChanged(nameof(EncodedOutputFolderPath));
}
}
}
public string PhoneConnected
{
get => _phoneConnected;
set
{
if (_phoneConnected != value)
{
_phoneConnected = value;
OnPropertyChanged(nameof(PhoneConnected));
}
}
}
public string NewFiles
{
get => _newFiles;
set
{
if (_newFiles != value)
{
_newFiles = value;
OnPropertyChanged(nameof(NewFiles));
}
}
}
public string BackupsPending
{
get => _backupsPending;
set
{
if (_backupsPending != value)
{
_backupsPending = value;
OnPropertyChanged(nameof(BackupsPending));
}
}
}
public string EncodingsPending
{
get => _encodingsPending;
set
{
if (_encodingsPending != value)
{
_encodingsPending = value;
OnPropertyChanged(nameof(EncodingsPending));
}
}
}
public string LastSync
{
get => _lastSync;
set
{
if (_lastSync != value)
{
_lastSync = value;
OnPropertyChanged(nameof(LastSync));
}
}
}
public string DbEntries
{
get => _dbEntries;
set
{
if (_dbEntries != value)
{
_dbEntries = value;
OnPropertyChanged(nameof(DbEntries));
}
}
}
#endregion
public void repeatingTasks()
{
Dispatcher.InvokeAsync(async () =>
{
while (true)
{
DevicesList = GetDevices();
DevicesNameList = DevicesList.Select(d => d.FriendlyName).ToList();
if (SelectedDevice != null)
{
PhoneConnected = "Connected";
foreach (var device in DevicesList)
{
if (device.FriendlyName.Equals(SelectedDevice))
{
SelectedDeviceObject = device;
}
}
}
else
{
PhoneConnected = "Disconnected";
}
await Task.Delay(1000);
}
});
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
PhoneFolderPath = "storage/emulated/0/";
BackupFolderPath = "D:/MediaBackup/";
EncodedOutputFolderPath = "D:/EncodedForPhone/";
PhoneConnected = "Disconnected";
NewFiles = "None";
BackupsPending = "None";
EncodingsPending = "None";
LastSync = "Never";
DbEntries = "None";
DevicesList = GetDevices();
SelectedDevice = DevicesList.FirstOrDefault().FriendlyName;
repeatingTasks();
}
private List GetDevices()
{
var devices = MediaDevice.GetDevices();
foreach(var device in devices.Select(d => d.FriendlyName).ToList())
{
Debug.WriteLine(device);
}
return devices.ToList();
}
private void Scan_Phone_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Scan Phone button clicked");
Dispatcher.InvokeAsync(async () =>
{
if (SelectedDeviceObject != null)
{
await Task.Run(() => FileManager.ScanMTPDevice(SelectedDeviceObject));
}
});
}
private void Sync_Files_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Sync Files button clicked");
}
private void Settings_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Settings button clicked");
}
private void Database_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Database button clicked");
}
private void Duplicates_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Duplicates button clicked");
}
private void Logs_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Logs button clicked");
}
private void Refresh_Phone_Click(object sender, RoutedEventArgs e)
{
DevicesList = GetDevices();
}
private void Change_Backup_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Change Backup button clicked");
var dialog = new OpenFolderDialog();
if (dialog.ShowDialog() == true)
{
BackupFolderPath = dialog.FolderName;
}
}
private void Change_Encoded_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Change Encoded button clicked");
var dialog = new OpenFolderDialog();
if (dialog.ShowDialog() == true)
{
EncodedOutputFolderPath = dialog.FolderName;
}
}
private void Rescan_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Rescan button clicked");
}
private void Start_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Start Sync button clicked");
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Pause/Stop button clicked");
FileManager.CancelOperation();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}