mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-09 05:34:05 +02:00
Refactor and move BrowserCache, VideoPlayer, and ExportManager
This commit is contained in:
parent
931761600f
commit
e114a93714
@ -5,11 +5,11 @@
|
||||
using TweetDuck.Core.Bridge;
|
||||
using TweetDuck.Core.Controls;
|
||||
using TweetDuck.Core.Handling;
|
||||
using TweetDuck.Core.Management;
|
||||
using TweetDuck.Core.Notification;
|
||||
using TweetDuck.Core.Notification.Screenshot;
|
||||
using TweetDuck.Core.Other;
|
||||
using TweetDuck.Core.Other.Analytics;
|
||||
using TweetDuck.Core.Other.Management;
|
||||
using TweetDuck.Core.Other.Settings;
|
||||
using TweetDuck.Core.Utils;
|
||||
using TweetDuck.Plugins;
|
||||
|
@ -8,6 +8,7 @@
|
||||
using TweetDuck.Core.Utils;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using TweetDuck.Core.Management;
|
||||
using TweetDuck.Core.Other;
|
||||
|
||||
namespace TweetDuck.Core.Handling{
|
||||
|
@ -1,10 +1,10 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TweetDuck.Core.Utils{
|
||||
namespace TweetDuck.Core.Management{
|
||||
static class BrowserCache{
|
||||
public static string CacheFolder => Path.Combine(Program.StoragePath, "Cache");
|
||||
|
@ -2,38 +2,49 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using TweetDuck.Core.Other;
|
||||
using TweetDuck.Data;
|
||||
using TweetDuck.Plugins;
|
||||
using TweetDuck.Plugins.Enums;
|
||||
|
||||
namespace TweetDuck.Core.Other.Settings.Export{
|
||||
sealed class ExportManager{
|
||||
namespace TweetDuck.Core.Management{
|
||||
sealed class ProfileManager{
|
||||
private static readonly string CookiesPath = Path.Combine(Program.StoragePath, "Cookies");
|
||||
private static readonly string TempCookiesPath = Path.Combine(Program.StoragePath, "CookiesTmp");
|
||||
|
||||
[Flags]
|
||||
public enum Items{
|
||||
None = 0,
|
||||
UserConfig = 1,
|
||||
SystemConfig = 2, // TODO implement later
|
||||
Session = 4,
|
||||
PluginData = 8,
|
||||
All = UserConfig|SystemConfig|Session|PluginData
|
||||
}
|
||||
|
||||
public bool IsRestarting { get; private set; }
|
||||
public Exception LastException { get; private set; }
|
||||
|
||||
private readonly string file;
|
||||
private readonly PluginManager plugins;
|
||||
|
||||
public ExportManager(string file, PluginManager plugins){
|
||||
public ProfileManager(string file, PluginManager plugins){
|
||||
this.file = file;
|
||||
this.plugins = plugins;
|
||||
}
|
||||
|
||||
public bool Export(ExportFileFlags flags){
|
||||
public bool Export(Items items){
|
||||
try{
|
||||
using(CombinedFileStream stream = new CombinedFileStream(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None))){
|
||||
if (flags.HasFlag(ExportFileFlags.UserConfig)){
|
||||
if (items.HasFlag(Items.UserConfig)){
|
||||
stream.WriteFile("config", Program.UserConfigFilePath);
|
||||
}
|
||||
|
||||
if (flags.HasFlag(ExportFileFlags.SystemConfig)){
|
||||
if (items.HasFlag(Items.SystemConfig)){
|
||||
stream.WriteFile("system", Program.SystemConfigFilePath);
|
||||
}
|
||||
|
||||
if (flags.HasFlag(ExportFileFlags.PluginData)){
|
||||
if (items.HasFlag(Items.PluginData)){
|
||||
stream.WriteFile("plugin.config", Program.PluginConfigFilePath);
|
||||
|
||||
foreach(Plugin plugin in plugins.Plugins){
|
||||
@ -47,7 +58,7 @@ public bool Export(ExportFileFlags flags){
|
||||
}
|
||||
}
|
||||
|
||||
if (flags.HasFlag(ExportFileFlags.Session)){
|
||||
if (items.HasFlag(Items.Session)){
|
||||
stream.WriteFile("cookies", CookiesPath);
|
||||
}
|
||||
|
||||
@ -61,8 +72,8 @@ public bool Export(ExportFileFlags flags){
|
||||
}
|
||||
}
|
||||
|
||||
public ExportFileFlags GetImportFlags(){
|
||||
ExportFileFlags flags = ExportFileFlags.None;
|
||||
public Items FindImportItems(){
|
||||
Items items = Items.None;
|
||||
|
||||
try{
|
||||
using(CombinedFileStream stream = new CombinedFileStream(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None))){
|
||||
@ -71,33 +82,33 @@ public ExportFileFlags GetImportFlags(){
|
||||
while((key = stream.SkipFile()) != null){
|
||||
switch(key){
|
||||
case "config":
|
||||
flags |= ExportFileFlags.UserConfig;
|
||||
items |= Items.UserConfig;
|
||||
break;
|
||||
|
||||
case "system":
|
||||
flags |= ExportFileFlags.SystemConfig;
|
||||
items |= Items.SystemConfig;
|
||||
break;
|
||||
|
||||
case "plugin.config":
|
||||
case "plugin.data":
|
||||
flags |= ExportFileFlags.PluginData;
|
||||
items |= Items.PluginData;
|
||||
break;
|
||||
|
||||
case "cookies":
|
||||
flags |= ExportFileFlags.Session;
|
||||
items |= Items.Session;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}catch(Exception e){
|
||||
LastException = e;
|
||||
flags = ExportFileFlags.None;
|
||||
items = Items.None;
|
||||
}
|
||||
|
||||
return flags;
|
||||
return items;
|
||||
}
|
||||
|
||||
public bool Import(ExportFileFlags flags){
|
||||
public bool Import(Items items){
|
||||
try{
|
||||
HashSet<string> missingPlugins = new HashSet<string>();
|
||||
|
||||
@ -107,14 +118,14 @@ public bool Import(ExportFileFlags flags){
|
||||
while((entry = stream.ReadFile()) != null){
|
||||
switch(entry.KeyName){
|
||||
case "config":
|
||||
if (flags.HasFlag(ExportFileFlags.UserConfig)){
|
||||
if (items.HasFlag(Items.UserConfig)){
|
||||
entry.WriteToFile(Program.UserConfigFilePath);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "system":
|
||||
if (flags.HasFlag(ExportFileFlags.SystemConfig)){
|
||||
if (items.HasFlag(Items.SystemConfig)){
|
||||
entry.WriteToFile(Program.SystemConfigFilePath);
|
||||
IsRestarting = true;
|
||||
}
|
||||
@ -122,14 +133,14 @@ public bool Import(ExportFileFlags flags){
|
||||
break;
|
||||
|
||||
case "plugin.config":
|
||||
if (flags.HasFlag(ExportFileFlags.PluginData)){
|
||||
if (items.HasFlag(Items.PluginData)){
|
||||
entry.WriteToFile(Program.PluginConfigFilePath);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "plugin.data":
|
||||
if (flags.HasFlag(ExportFileFlags.PluginData)){
|
||||
if (items.HasFlag(Items.PluginData)){
|
||||
string[] value = entry.KeyValue;
|
||||
|
||||
entry.WriteToFile(Path.Combine(Program.PluginDataPath, value[0], value[1]), true);
|
||||
@ -142,7 +153,7 @@ public bool Import(ExportFileFlags flags){
|
||||
break;
|
||||
|
||||
case "cookies":
|
||||
if (flags.HasFlag(ExportFileFlags.Session)){
|
||||
if (items.HasFlag(Items.Session)){
|
||||
entry.WriteToFile(Path.Combine(Program.StoragePath, TempCookiesPath));
|
||||
IsRestarting = true;
|
||||
}
|
@ -3,10 +3,11 @@
|
||||
using System.IO;
|
||||
using System.Windows.Forms;
|
||||
using TweetDuck.Core.Controls;
|
||||
using TweetDuck.Core.Other;
|
||||
using TweetDuck.Core.Utils;
|
||||
using TweetLib.Communication;
|
||||
|
||||
namespace TweetDuck.Core.Other.Management{
|
||||
namespace TweetDuck.Core.Management{
|
||||
sealed class VideoPlayer : IDisposable{
|
||||
public bool Running{
|
||||
get{
|
@ -1,5 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
using TweetDuck.Configuration;
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
using System.IO;
|
||||
using System.Windows.Forms;
|
||||
using TweetDuck.Configuration;
|
||||
using TweetDuck.Core.Other.Settings.Export;
|
||||
using TweetDuck.Core.Management;
|
||||
using TweetDuck.Plugins;
|
||||
|
||||
namespace TweetDuck.Core.Other.Settings.Dialogs{
|
||||
@ -11,24 +11,25 @@ private enum State{
|
||||
Deciding, Reset, Import, Export
|
||||
}
|
||||
|
||||
public ExportFileFlags Flags{
|
||||
get => selectedFlags;
|
||||
private ProfileManager.Items SelectedItems{
|
||||
get => _selectedItems;
|
||||
|
||||
set{
|
||||
// this will call events and SetFlag, which also updates the UI
|
||||
cbConfig.Checked = value.HasFlag(ExportFileFlags.UserConfig);
|
||||
cbSession.Checked = value.HasFlag(ExportFileFlags.Session);
|
||||
cbPluginData.Checked = value.HasFlag(ExportFileFlags.PluginData);
|
||||
cbConfig.Checked = value.HasFlag(ProfileManager.Items.UserConfig);
|
||||
cbSession.Checked = value.HasFlag(ProfileManager.Items.Session);
|
||||
cbPluginData.Checked = value.HasFlag(ProfileManager.Items.PluginData);
|
||||
}
|
||||
}
|
||||
|
||||
public bool ShouldReloadBrowser { get; private set; }
|
||||
|
||||
private readonly PluginManager plugins;
|
||||
private State currentState;
|
||||
|
||||
private ExportManager importManager;
|
||||
private ExportFileFlags selectedFlags = ExportFileFlags.None;
|
||||
private State currentState;
|
||||
private ProfileManager importManager;
|
||||
|
||||
private ProfileManager.Items _selectedItems = ProfileManager.Items.None;
|
||||
|
||||
public DialogSettingsManage(PluginManager plugins){
|
||||
InitializeComponent();
|
||||
@ -42,15 +43,15 @@ private void radioDecision_CheckedChanged(object sender, EventArgs e){
|
||||
}
|
||||
|
||||
private void cbConfig_CheckedChanged(object sender, EventArgs e){
|
||||
SetFlag(ExportFileFlags.UserConfig, cbConfig.Checked);
|
||||
SetFlag(ProfileManager.Items.UserConfig, cbConfig.Checked);
|
||||
}
|
||||
|
||||
private void cbSession_CheckedChanged(object sender, EventArgs e){
|
||||
SetFlag(ExportFileFlags.Session, cbSession.Checked);
|
||||
SetFlag(ProfileManager.Items.Session, cbSession.Checked);
|
||||
}
|
||||
|
||||
private void cbPluginData_CheckedChanged(object sender, EventArgs e){
|
||||
SetFlag(ExportFileFlags.PluginData, cbPluginData.Checked);
|
||||
SetFlag(ProfileManager.Items.PluginData, cbPluginData.Checked);
|
||||
}
|
||||
|
||||
private void btnContinue_Click(object sender, EventArgs e){
|
||||
@ -63,7 +64,7 @@ private void btnContinue_Click(object sender, EventArgs e){
|
||||
currentState = State.Reset;
|
||||
|
||||
Text = "Restore Defaults";
|
||||
Flags = ExportFileFlags.UserConfig;
|
||||
SelectedItems = ProfileManager.Items.UserConfig;
|
||||
}
|
||||
|
||||
// Import
|
||||
@ -81,11 +82,11 @@ private void btnContinue_Click(object sender, EventArgs e){
|
||||
file = dialog.FileName;
|
||||
}
|
||||
|
||||
importManager = new ExportManager(file, plugins);
|
||||
importManager = new ProfileManager(file, plugins);
|
||||
currentState = State.Import;
|
||||
|
||||
Text = "Import Profile";
|
||||
Flags = importManager.GetImportFlags();
|
||||
SelectedItems = importManager.FindImportItems();
|
||||
|
||||
cbConfig.Enabled = cbConfig.Checked;
|
||||
cbSession.Enabled = cbSession.Checked;
|
||||
@ -98,7 +99,7 @@ private void btnContinue_Click(object sender, EventArgs e){
|
||||
|
||||
Text = "Export Profile";
|
||||
btnContinue.Text = "Export Profile";
|
||||
Flags = ExportFileFlags.All & ~ExportFileFlags.Session;
|
||||
SelectedItems = ProfileManager.Items.All & ~ProfileManager.Items.Session;
|
||||
}
|
||||
|
||||
// Continue...
|
||||
@ -108,11 +109,11 @@ private void btnContinue_Click(object sender, EventArgs e){
|
||||
|
||||
case State.Reset:
|
||||
if (FormMessage.Warning("Reset TweetDuck Options", "This will reset the selected items. Are you sure you want to proceed?", FormMessage.Yes, FormMessage.No)){
|
||||
if (Flags.HasFlag(ExportFileFlags.UserConfig)){
|
||||
if (SelectedItems.HasFlag(ProfileManager.Items.UserConfig)){
|
||||
Program.UserConfig.Reset();
|
||||
}
|
||||
|
||||
if (Flags.HasFlag(ExportFileFlags.SystemConfig)){
|
||||
if (SelectedItems.HasFlag(ProfileManager.Items.SystemConfig)){
|
||||
try{
|
||||
File.Delete(Program.SystemConfigFilePath);
|
||||
}catch(Exception ex){
|
||||
@ -120,7 +121,7 @@ private void btnContinue_Click(object sender, EventArgs e){
|
||||
}
|
||||
}
|
||||
|
||||
if (Flags.HasFlag(ExportFileFlags.PluginData)){
|
||||
if (SelectedItems.HasFlag(ProfileManager.Items.PluginData)){
|
||||
try{
|
||||
File.Delete(Program.PluginConfigFilePath);
|
||||
Directory.Delete(Program.PluginDataPath, true);
|
||||
@ -129,10 +130,10 @@ private void btnContinue_Click(object sender, EventArgs e){
|
||||
}
|
||||
}
|
||||
|
||||
if (Flags.HasFlag(ExportFileFlags.Session)){
|
||||
if (SelectedItems.HasFlag(ProfileManager.Items.Session)){
|
||||
Program.Restart(Arguments.ArgDeleteCookies);
|
||||
}
|
||||
else if (Flags.HasFlag(ExportFileFlags.SystemConfig)){
|
||||
else if (SelectedItems.HasFlag(ProfileManager.Items.SystemConfig)){
|
||||
Program.Restart();
|
||||
}
|
||||
else{
|
||||
@ -146,14 +147,14 @@ private void btnContinue_Click(object sender, EventArgs e){
|
||||
break;
|
||||
|
||||
case State.Import:
|
||||
if (importManager.Import(Flags)){
|
||||
if (importManager.Import(SelectedItems)){
|
||||
Program.UserConfig.Reload();
|
||||
|
||||
if (importManager.IsRestarting){
|
||||
if (Flags.HasFlag(ExportFileFlags.Session)){
|
||||
if (SelectedItems.HasFlag(ProfileManager.Items.Session)){
|
||||
Program.Restart(Arguments.ArgImportCookies);
|
||||
}
|
||||
else if (Flags.HasFlag(ExportFileFlags.SystemConfig)){
|
||||
else if (SelectedItems.HasFlag(ProfileManager.Items.SystemConfig)){
|
||||
Program.Restart();
|
||||
}
|
||||
}
|
||||
@ -189,9 +190,9 @@ private void btnContinue_Click(object sender, EventArgs e){
|
||||
Program.UserConfig.Save();
|
||||
Program.SystemConfig.Save();
|
||||
|
||||
ExportManager manager = new ExportManager(file, plugins);
|
||||
ProfileManager manager = new ProfileManager(file, plugins);
|
||||
|
||||
if (!manager.Export(Flags)){
|
||||
if (!manager.Export(SelectedItems)){
|
||||
Program.Reporter.HandleException("Profile Export Error", "An exception happened while exporting TweetDuck profile.", true, manager.LastException);
|
||||
}
|
||||
|
||||
@ -206,15 +207,15 @@ private void btnCancel_Click(object sender, EventArgs e){
|
||||
Close();
|
||||
}
|
||||
|
||||
private void SetFlag(ExportFileFlags flag, bool enable){
|
||||
selectedFlags = enable ? selectedFlags | flag : selectedFlags & ~flag;
|
||||
btnContinue.Enabled = selectedFlags != ExportFileFlags.None;
|
||||
private void SetFlag(ProfileManager.Items flag, bool enable){
|
||||
_selectedItems = enable ? _selectedItems | flag : _selectedItems & ~flag;
|
||||
btnContinue.Enabled = _selectedItems != ProfileManager.Items.None;
|
||||
|
||||
if (currentState == State.Import){
|
||||
btnContinue.Text = selectedFlags.HasFlag(ExportFileFlags.Session) ? "Import && Restart" : "Import Profile";
|
||||
btnContinue.Text = _selectedItems.HasFlag(ProfileManager.Items.Session) ? "Import && Restart" : "Import Profile";
|
||||
}
|
||||
else if (currentState == State.Reset){
|
||||
btnContinue.Text = selectedFlags.HasFlag(ExportFileFlags.Session) ? "Restore && Restart" : "Restore Defaults";
|
||||
btnContinue.Text = _selectedItems.HasFlag(ProfileManager.Items.Session) ? "Restore && Restart" : "Restore Defaults";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace TweetDuck.Core.Other.Settings.Export{
|
||||
[Flags]
|
||||
enum ExportFileFlags{
|
||||
None = 0,
|
||||
UserConfig = 1,
|
||||
SystemConfig = 2, // TODO implement later
|
||||
Session = 4,
|
||||
PluginData = 8,
|
||||
All = UserConfig|SystemConfig|Session|PluginData
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@
|
||||
using System.Windows.Forms;
|
||||
using TweetDuck.Configuration;
|
||||
using TweetDuck.Core.Controls;
|
||||
using TweetDuck.Core.Management;
|
||||
using TweetDuck.Core.Other.Settings.Dialogs;
|
||||
using TweetDuck.Core.Utils;
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
||||
using TweetDuck.Core;
|
||||
using TweetDuck.Core.Handling.General;
|
||||
using TweetDuck.Core.Other;
|
||||
using TweetDuck.Core.Other.Settings.Export;
|
||||
using TweetDuck.Core.Management;
|
||||
using TweetDuck.Core.Utils;
|
||||
using TweetDuck.Data;
|
||||
using TweetDuck.Updates;
|
||||
@ -118,10 +118,10 @@ private static void Main(){
|
||||
SystemConfig = SystemConfig.Load(SystemConfigFilePath);
|
||||
|
||||
if (Arguments.HasFlag(Arguments.ArgImportCookies)){
|
||||
ExportManager.ImportCookies();
|
||||
ProfileManager.ImportCookies();
|
||||
}
|
||||
else if (Arguments.HasFlag(Arguments.ArgDeleteCookies)){
|
||||
ExportManager.DeleteCookies();
|
||||
ProfileManager.DeleteCookies();
|
||||
}
|
||||
|
||||
if (Arguments.HasFlag(Arguments.ArgUpdated)){
|
||||
|
Loading…
Reference in New Issue
Block a user