1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-12 05:34:06 +02:00

Add WIP export/import selection dialog

This commit is contained in:
chylex 2016-12-28 21:16:53 +01:00
parent 85b90574b8
commit dd14ad470e
6 changed files with 282 additions and 37 deletions

View File

@ -0,0 +1,118 @@
namespace TweetDck.Core.Other.Settings.Dialogs {
partial class DialogSettingsExport {
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
this.btnCancel = new System.Windows.Forms.Button();
this.btnApply = new System.Windows.Forms.Button();
this.cbConfig = new System.Windows.Forms.CheckBox();
this.cbSession = new System.Windows.Forms.CheckBox();
this.cbPluginData = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// btnCancel
//
this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnCancel.AutoSize = true;
this.btnCancel.Location = new System.Drawing.Point(216, 107);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Padding = new System.Windows.Forms.Padding(3, 0, 3, 0);
this.btnCancel.Size = new System.Drawing.Size(56, 23);
this.btnCancel.TabIndex = 0;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// btnApply
//
this.btnApply.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnApply.AutoSize = true;
this.btnApply.Location = new System.Drawing.Point(157, 107);
this.btnApply.Name = "btnApply";
this.btnApply.Padding = new System.Windows.Forms.Padding(3, 0, 3, 0);
this.btnApply.Size = new System.Drawing.Size(53, 23);
this.btnApply.TabIndex = 1;
this.btnApply.UseVisualStyleBackColor = true;
this.btnApply.Click += new System.EventHandler(this.btnApply_Click);
//
// cbConfig
//
this.cbConfig.AutoSize = true;
this.cbConfig.Location = new System.Drawing.Point(13, 13);
this.cbConfig.Name = "cbConfig";
this.cbConfig.Size = new System.Drawing.Size(106, 17);
this.cbConfig.TabIndex = 2;
this.cbConfig.Text = "Program Settings";
this.cbConfig.UseVisualStyleBackColor = true;
this.cbConfig.CheckedChanged += new System.EventHandler(this.cbConfig_CheckedChanged);
//
// cbSession
//
this.cbSession.AutoSize = true;
this.cbSession.Location = new System.Drawing.Point(13, 37);
this.cbSession.Name = "cbSession";
this.cbSession.Size = new System.Drawing.Size(92, 17);
this.cbSession.TabIndex = 3;
this.cbSession.Text = "Login Session";
this.cbSession.UseVisualStyleBackColor = true;
this.cbSession.CheckedChanged += new System.EventHandler(this.cbSession_CheckedChanged);
//
// cbPluginData
//
this.cbPluginData.AutoSize = true;
this.cbPluginData.Location = new System.Drawing.Point(13, 61);
this.cbPluginData.Name = "cbPluginData";
this.cbPluginData.Size = new System.Drawing.Size(81, 17);
this.cbPluginData.TabIndex = 4;
this.cbPluginData.Text = "Plugin Data";
this.cbPluginData.UseVisualStyleBackColor = true;
this.cbPluginData.CheckedChanged += new System.EventHandler(this.cbPluginData_CheckedChanged);
//
// DialogSettingsExport
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 142);
this.Controls.Add(this.cbPluginData);
this.Controls.Add(this.cbSession);
this.Controls.Add(this.cbConfig);
this.Controls.Add(this.btnApply);
this.Controls.Add(this.btnCancel);
this.MinimumSize = new System.Drawing.Size(200, 170);
this.Name = "DialogSettingsExport";
this.ShowIcon = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnApply;
private System.Windows.Forms.CheckBox cbConfig;
private System.Windows.Forms.CheckBox cbSession;
private System.Windows.Forms.CheckBox cbPluginData;
}
}

View File

@ -0,0 +1,80 @@
using System;
using System.Windows.Forms;
using TweetDck.Core.Other.Settings.Export;
namespace TweetDck.Core.Other.Settings.Dialogs{
sealed partial class DialogSettingsExport : Form{
public static DialogSettingsExport Import(ExportFileFlags flags){
return new DialogSettingsExport(flags);
}
public static DialogSettingsExport Export(){
return new DialogSettingsExport(ExportFileFlags.None);
}
public ExportFileFlags Flags{
get{
return selectedFlags;
}
set{
selectedFlags = value;
btnApply.Enabled = selectedFlags != ExportFileFlags.None;
cbConfig.Checked = selectedFlags.HasFlag(ExportFileFlags.Config);
cbSession.Checked = selectedFlags.HasFlag(ExportFileFlags.Session);
cbPluginData.Checked = selectedFlags.HasFlag(ExportFileFlags.PluginData);
}
}
private readonly bool isExporting;
private ExportFileFlags selectedFlags = ExportFileFlags.None;
private DialogSettingsExport(ExportFileFlags importFlags){
InitializeComponent();
isExporting = importFlags == ExportFileFlags.None;
if (isExporting){
Text = "Export";
btnApply.Text = "Export";
Flags = ExportFileFlags.All;
}
else{
Text = "Import";
btnApply.Text = "Import";
Flags = importFlags;
cbConfig.Enabled = cbConfig.Checked;
cbSession.Enabled = cbSession.Checked;
cbPluginData.Enabled = cbPluginData.Checked;
}
}
private void SetFlag(ExportFileFlags flag, bool enable){
selectedFlags = enable ? selectedFlags | flag : selectedFlags & ~flag;
}
private void cbConfig_CheckedChanged(object sender, EventArgs e){
SetFlag(ExportFileFlags.Config, cbConfig.Checked);
}
private void cbSession_CheckedChanged(object sender, EventArgs e){
SetFlag(ExportFileFlags.Session, cbConfig.Checked);
}
private void cbPluginData_CheckedChanged(object sender, EventArgs e){
SetFlag(ExportFileFlags.PluginData, cbConfig.Checked);
}
private void btnApply_Click(object sender, EventArgs e){
DialogResult = DialogResult.OK;
Close();
}
private void btnCancel_Click(object sender, EventArgs e){
DialogResult = DialogResult.Cancel;
Close();
}
}
}

View File

@ -0,0 +1,12 @@
using System;
namespace TweetDck.Core.Other.Settings.Export{
[Flags]
enum ExportFileFlags{
None = 0,
Config = 1,
Session = 2,
PluginData = 4,
All = Config|Session|PluginData
}
}

View File

@ -22,22 +22,26 @@ public ExportManager(string file, PluginManager plugins){
this.plugins = plugins;
}
public bool Export(bool includeSession){
public bool Export(ExportFileFlags flags){
try{
using(CombinedFileStream stream = new CombinedFileStream(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None))){
stream.WriteFile("config", Program.ConfigFilePath);
if (flags.HasFlag(ExportFileFlags.Config)){
stream.WriteFile("config", Program.ConfigFilePath);
}
foreach(Plugin plugin in plugins.Plugins){
foreach(PathInfo path in EnumerateFilesRelative(plugin.GetPluginFolder(PluginFolder.Data))){
try{
stream.WriteFile(new string[]{ "plugin.data", plugin.Identifier, path.Relative }, path.Full);
}catch(ArgumentOutOfRangeException e){
MessageBox.Show("Could not include a plugin file in the export. "+e.Message, "Export Profile", MessageBoxButtons.OK, MessageBoxIcon.Warning);
if (flags.HasFlag(ExportFileFlags.PluginData)){
foreach(Plugin plugin in plugins.Plugins){
foreach(PathInfo path in EnumerateFilesRelative(plugin.GetPluginFolder(PluginFolder.Data))){
try{
stream.WriteFile(new string[]{ "plugin.data", plugin.Identifier, path.Relative }, path.Full);
}catch(ArgumentOutOfRangeException e){
MessageBox.Show("Could not include a plugin file in the export. "+e.Message, "Export Profile", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
if (includeSession){
if (flags.HasFlag(ExportFileFlags.Session)){
stream.WriteFile("cookies", CookiesPath);
}
@ -51,7 +55,7 @@ public bool Export(bool includeSession){
}
}
public bool Import(){
public bool Import(ExportFileFlags flags){
try{
HashSet<string> missingPlugins = new HashSet<string>();
@ -61,23 +65,28 @@ public bool Import(){
while((entry = stream.ReadFile()) != null){
switch(entry.KeyName){
case "config":
entry.WriteToFile(Program.ConfigFilePath);
Program.ReloadConfig();
if (flags.HasFlag(ExportFileFlags.Config)){
entry.WriteToFile(Program.ConfigFilePath);
Program.ReloadConfig();
}
break;
case "plugin.data":
string[] value = entry.KeyValue;
if (flags.HasFlag(ExportFileFlags.PluginData)){
string[] value = entry.KeyValue;
entry.WriteToFile(Path.Combine(Program.PluginDataPath, value[0], value[1]), true);
entry.WriteToFile(Path.Combine(Program.PluginDataPath, value[0], value[1]), true);
if (!plugins.IsPluginInstalled(value[0])){
missingPlugins.Add(value[0]);
if (!plugins.IsPluginInstalled(value[0])){
missingPlugins.Add(value[0]);
}
}
break;
case "cookies":
if (MessageBox.Show("Do you want to import the login session? This will restart "+Program.BrandName+".", "Importing "+Program.BrandName+" Profile", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes){
if (flags.HasFlag(ExportFileFlags.Session) && MessageBox.Show("Do you want to import the login session? This will restart "+Program.BrandName+".", "Importing "+Program.BrandName+" Profile", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes){
entry.WriteToFile(Path.Combine(Program.StoragePath, TempCookiesPath));
IsRestarting = true;
}

View File

@ -95,10 +95,16 @@ private void btnEditCSS_Click(object sender, EventArgs e){
}
private void btnExport_Click(object sender, EventArgs e){
DialogResult resultSaveCredentials = MessageBox.Show("Do you want to include your login session? This will not save your password into the file, but it will allow anyone with the file to login into TweetDeck as you.", "Export "+Program.BrandName+" Settings", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
if (resultSaveCredentials == DialogResult.Cancel)return;
ExportFileFlags flags;
using(DialogSettingsExport dialog = DialogSettingsExport.Export()){
if (dialog.ShowDialog() != DialogResult.OK){
return;
}
flags = dialog.Flags;
}
bool saveCredentials = resultSaveCredentials == DialogResult.Yes;
string file;
using(SaveFileDialog dialog = new SaveFileDialog{
@ -110,17 +116,19 @@ private void btnExport_Click(object sender, EventArgs e){
Title = "Export "+Program.BrandName+" Settings",
Filter = Program.BrandName+" Settings (*.tdsettings)|*.tdsettings"
}){
file = dialog.ShowDialog() == DialogResult.OK ? dialog.FileName : null;
if (dialog.ShowDialog() != DialogResult.OK){
return;
}
file = dialog.FileName;
}
if (file != null){
Program.UserConfig.Save();
Program.UserConfig.Save();
ExportManager manager = new ExportManager(file, plugins);
if (!manager.Export(saveCredentials)){
Program.Reporter.HandleException("Profile Export Error", "An exception happened while exporting "+Program.BrandName+" settings.", true, manager.LastException);
}
ExportManager manager = new ExportManager(file, plugins);
if (!manager.Export(flags)){
Program.Reporter.HandleException("Profile Export Error", "An exception happened while exporting "+Program.BrandName+" settings.", true, manager.LastException);
}
}
@ -133,21 +141,32 @@ private void btnImport_Click(object sender, EventArgs e){
Title = "Import "+Program.BrandName+" Settings",
Filter = Program.BrandName+" Settings (*.tdsettings)|*.tdsettings"
}){
file = dialog.ShowDialog() == DialogResult.OK ? dialog.FileName : null;
if (dialog.ShowDialog() != DialogResult.OK){
return;
}
file = dialog.FileName;
}
if (file != null){
ExportManager manager = new ExportManager(file, plugins);
ExportManager manager = new ExportManager(file, plugins);
ExportFileFlags flags;
if (manager.Import()){
if (!manager.IsRestarting){
((FormSettings)ParentForm).ReloadUI();
}
using(DialogSettingsExport dialog = DialogSettingsExport.Import(ExportFileFlags.All)){ // TODO detect
if (dialog.ShowDialog() != DialogResult.OK){
return;
}
else{
Program.Reporter.HandleException("Profile Import Error", "An exception happened while importing "+Program.BrandName+" settings.", true, manager.LastException);
flags = dialog.Flags;
}
if (manager.Import(flags)){
if (!manager.IsRestarting){
((FormSettings)ParentForm).ReloadUI();
}
}
else{
Program.Reporter.HandleException("Profile Import Error", "An exception happened while importing "+Program.BrandName+" settings.", true, manager.LastException);
}
}
private void btnReset_Click(object sender, EventArgs e){

View File

@ -142,7 +142,14 @@
<Compile Include="Core\Other\Settings\Dialogs\DialogSettingsCefArgs.Designer.cs">
<DependentUpon>DialogSettingsCefArgs.cs</DependentUpon>
</Compile>
<Compile Include="Core\Other\Settings\Dialogs\DialogSettingsExport.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Core\Other\Settings\Dialogs\DialogSettingsExport.Designer.cs">
<DependentUpon>DialogSettingsExport.cs</DependentUpon>
</Compile>
<Compile Include="Core\Other\Settings\Export\CombinedFileStream.cs" />
<Compile Include="Core\Other\Settings\Export\ExportFileFlags.cs" />
<Compile Include="Core\Other\Settings\Export\ExportManager.cs" />
<Compile Include="Core\Other\Settings\TabSettingsAdvanced.cs">
<SubType>UserControl</SubType>