CASA/CASA/gui/ExportSecrets.cs

219 lines
6.4 KiB
C#

using System;
using System.IO;
using Novell.CASA.MiCasa.Communication;
using Novell.CASA.MiCasa.Common;
using Gtk;
using Glade;
namespace Novell.CASA.GUI
{
/// <summary>
/// Summary description for ExportSecrets.
/// </summary>
public class ExportSecrets
{
private Config m_config = null;
[Glade.Widget]
Gtk.Label labelExportDialogDesc1,
labelExportDialogDesc2,
labelExportDialogPrompt,
label86,
label88;
[Glade.Widget]
Gtk.Entry entryMasterPassword;
[Glade.Widget]
Gtk.Dialog dialogExport;
[Glade.Widget]
Gtk.CheckButton checkbuttonNoEncrypt;
[Glade.Widget]
Gtk.Button buttonNewFolder;
public ExportSecrets(Config config)
{
m_config = config;
}
public void Run()
{
// prompt for master password, and optional passphrase to encrypt
//Logger.DbgLog("GUI:CasaMain.Login() - IsMasterPasswordSet returned false");
#if W32
Glade.XML gxmlTemp = new Glade.XML ("../images/casa.glade", "dialogExport", null);
#endif
#if LINUX
Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogExport", null);
#endif
gxmlTemp.Autoconnect (this);
if (CommonGUI.UseMasterPassword())
{
labelExportDialogDesc1.Visible = true;
}
else
{
labelExportDialogDesc2.Visible = true;
labelExportDialogPrompt.Text = "Encryption string:";
}
dialogExport.TransientFor = (Gtk.Window)CasaMain.gxmlMain.GetWidget("windowMain");
dialogExport.Destroyed += new EventHandler(dialogExport_Destroyed);
dialogExport.Modal = true;
dialogExport.Show();
}
private void dialogExport_Destroyed(object sender, EventArgs e)
{
}
private void on_buttonHelpExportSecrets_clicked(object sender, EventArgs args)
{
Common.ShowHelpUrl("ExportingSecrets.htm");
}
private void on_buttonCloseExportSecrets_clicked(object sender, EventArgs args)
{
if (dialogExport != null)
{
dialogExport.Destroy();
}
}
private void on_buttonOkExportSecrets_clicked(object sender, EventArgs args)
{
bool bUseMasterPassword = CommonGUI.UseMasterPassword();
if ( ((bUseMasterPassword) && (0 == miCASA.SetMasterPassword(0, entryMasterPassword.Text) ))
|| (!bUseMasterPassword))
{
string sMasterPWD = entryMasterPassword.Text;
string sEncryptString = entryMasterPassword.Text;
if (checkbuttonNoEncrypt.Active)
sEncryptString = null;
if (entryMasterPassword.Text.Length < 1)
{
sEncryptString = null;
}
if (dialogExport != null)
{
dialogExport.Destroy();
}
string sHintDir = m_config.GetConfigSetting(CommonGUI.HINT_DIR, null);
string sHintFile = m_config.GetConfigSetting(CommonGUI.HINT_FILENAME, null);
// prompt the user for storage location
string sFileName = GetStorageFileName(sHintDir, sHintFile);
if (sFileName != null)
{
#if LINUX
// make sure user has 'Write' rights to this directory
string sDir = sFileName.Substring(0, sFileName.LastIndexOf("/"));
int rcode = Mono.Unix.Native.Syscall.access(sDir,
Mono.Unix.Native.AccessModes.R_OK |
Mono.Unix.Native.AccessModes.W_OK );
if (rcode != 0)
{
CommonGUI.DisplayMessage(Gtk.MessageType.Error, "You do not have rights to export your files here.\r\n"+sDir);
return;
}
#endif
//Store off this location for next export
int iLastSlash = sFileName.LastIndexOf("/");
if (Common.IS_WINDOWS)
iLastSlash = sFileName.LastIndexOf("\\");
if (iLastSlash > 0)
{
sHintFile = sFileName.Substring(iLastSlash + 1);
sHintDir = sFileName.Substring(0, sFileName.Length - sHintFile.Length);
}
// save for later use
m_config.SetConfigSetting(CommonGUI.HINT_DIR, sHintDir);
m_config.SetConfigSetting(CommonGUI.HINT_FILENAME, sHintFile);
m_config.WriteConfig();
// call our daemon to get the users secrets
ExportXMLSecrets exportSecrets = new ExportXMLSecrets(sMasterPWD, sEncryptString, null);
byte[] baSecrets = (byte[])Novell.CASA.MiCasa.Communication.MiCasaRequestReply.Send(MiCasaRequestReply.VERB_EXPORT_SECRETS, null, null, null, exportSecrets);
if (baSecrets != null)
{
try
{
FileStream fs = new FileStream(sFileName, FileMode.Create);
fs.Write(baSecrets, 0, baSecrets.Length);
fs.Flush();
fs.Close();
CommonGUI.DisplayMessage(MessageType.Info, "Secrets saved to: \r\n" + sFileName);
}
catch
{
CommonGUI.DisplayMessage(MessageType.Error, "Failed to save secrets");
}
}
else
{
CommonGUI.DisplayMessage(MessageType.Error, "No Secrets found");
}
}
}
else
{
// prompt user
MessageDialog md=new MessageDialog(dialogExport,Gtk.DialogFlags.Modal,
Gtk.MessageType.Warning,
Gtk.ButtonsType.Ok,
"Master Password incorrect");
md.Response +=new ResponseHandler(md_Response2);
md.SetPosition(Gtk.WindowPosition.CenterOnParent);
md.Modal = true;
md.Show();
}
}
private string GetStorageFileName(string sHintDir, string sHintFile)
{
FileChooser fc = new FileChooser(FileChooser.ACTION_SAVE);
String sFileName = fc.GetFile(sHintDir, sHintFile, "*.casa");
/*
#if W32
String sFileName = CommonGUI.FileChooser(FileChooserAction.Save, "Select filename and location for secrets", sHintDir, sHintFile);
#else
String sFileName = null;
CommonGUI.DisplayMessage(Gtk.MessageType.Info, "Not implemented");
#endif
*/
return sFileName;
}
private void md_Response2(object o, ResponseArgs args)
{
MessageDialog md = (MessageDialog)o;
if (md != null)
{
md.Destroy();
}
}
}
}