Esempio n. 1
0
        public static bool Export(PwExportInfo pwExportInfo, FileFormatProvider
                                  fileFormat, IOConnectionInfo iocOutput, IStatusLogger slLogger)
        {
            if (pwExportInfo == null)
            {
                throw new ArgumentNullException("pwExportInfo");
            }
            if (pwExportInfo.DataGroup == null)
            {
                throw new ArgumentException();
            }
            if (fileFormat == null)
            {
                throw new ArgumentNullException("fileFormat");
            }
            if (fileFormat.RequiresFile && (iocOutput == null))
            {
                throw new ArgumentNullException("iocOutput");
            }

            if (!AppPolicy.Try(AppPolicyId.Export))
            {
                return(false);
            }
            if (!fileFormat.SupportsExport)
            {
                return(false);
            }
            if (!fileFormat.TryBeginExport())
            {
                return(false);
            }

            bool bExistedAlready = true;             // No deletion by default
            bool bResult         = false;

            try
            {
                bExistedAlready = (fileFormat.RequiresFile ? IOConnection.FileExists(
                                       iocOutput) : false);
                Stream s = (fileFormat.RequiresFile ? IOConnection.OpenWrite(
                                iocOutput) : null);

                try { bResult = fileFormat.Export(pwExportInfo, s, slLogger); }
                finally { if (s != null)
                          {
                              s.Close();
                          }
                }
            }
            catch (Exception ex) { MessageService.ShowWarning(ex); }

            if (fileFormat.RequiresFile && !bResult && !bExistedAlready)
            {
                try { IOConnection.DeleteFile(iocOutput); }
                catch (Exception) { }
            }

            return(bResult);
        }
Esempio n. 2
0
        private static void PerformExport(PwExportInfo pwExportInfo,
                                          FileFormatProvider fileFormat, string strOutputFile, IStatusLogger slLogger)
        {
            if (!fileFormat.SupportsExport)
            {
                return;
            }
            if (fileFormat.TryBeginExport() == false)
            {
                return;
            }

            bool bExistedAlready = File.Exists(strOutputFile);

            FileStream fsOut = new FileStream(strOutputFile, FileMode.Create,
                                              FileAccess.Write, FileShare.None);

            bool bResult = fileFormat.Export(pwExportInfo, fsOut, slLogger);

            fsOut.Close();
            fsOut.Dispose();

            if ((bResult == false) && (bExistedAlready == false))
            {
                try { File.Delete(strOutputFile); }
                catch (Exception) { }
            }
        }
Esempio n. 3
0
        public static bool Export(PwExportInfo pwExportInfo, FileFormatProvider
                                  fileFormat, IOConnectionInfo iocOutput, IStatusLogger slLogger)
        {
            if (pwExportInfo == null)
            {
                throw new ArgumentNullException("pwExportInfo");
            }
            if (pwExportInfo.DataGroup == null)
            {
                throw new ArgumentException();
            }
            if (fileFormat == null)
            {
                throw new ArgumentNullException("fileFormat");
            }
            if (fileFormat.RequiresFile && (iocOutput == null))
            {
                throw new ArgumentNullException("iocOutput");
            }

            if (!AppPolicy.Try(AppPolicyId.Export))
            {
                return(false);
            }
            if (!fileFormat.SupportsExport)
            {
                return(false);
            }
            if (!fileFormat.TryBeginExport())
            {
                return(false);
            }

            // bool bExistedAlready = File.Exists(strOutputFile);
            bool bExistedAlready = (fileFormat.RequiresFile ? IOConnection.FileExists(
                                        iocOutput) : false);

            // FileStream fsOut = new FileStream(strOutputFile, FileMode.Create,
            //	FileAccess.Write, FileShare.None);
            Stream sOut = (fileFormat.RequiresFile ? IOConnection.OpenWrite(
                               iocOutput) : null);

            bool bResult = false;

            try { bResult = fileFormat.Export(pwExportInfo, sOut, slLogger); }
            catch (Exception ex) { MessageService.ShowWarning(ex); }

            if (sOut != null)
            {
                sOut.Close();
            }

            if (fileFormat.RequiresFile && (bResult == false) && (bExistedAlready == false))
            {
                try { IOConnection.DeleteFile(iocOutput); }
                catch (Exception) { }
            }

            return(bResult);
        }
Esempio n. 4
0
        private static void PerformExport(PwExportInfo pwExportInfo,
            FileFormatProvider fileFormat, string strOutputFile, IStatusLogger slLogger)
        {
            if(!fileFormat.SupportsExport) return;
            if(fileFormat.TryBeginExport() == false) return;

            bool bExistedAlready = File.Exists(strOutputFile);

            FileStream fsOut = new FileStream(strOutputFile, FileMode.Create,
                FileAccess.Write, FileShare.None);

            bool bResult = fileFormat.Export(pwExportInfo, fsOut, slLogger);

            fsOut.Close();
            fsOut.Dispose();

            if((bResult == false) && (bExistedAlready == false))
            {
                try { File.Delete(strOutputFile); }
                catch(Exception) { }
            }
        }
Esempio n. 5
0
        public static bool Export(PwExportInfo pwExportInfo, FileFormatProvider
            fileFormat, IOConnectionInfo iocOutput, IStatusLogger slLogger)
        {
            if(pwExportInfo == null) throw new ArgumentNullException("pwExportInfo");
            if(pwExportInfo.DataGroup == null) throw new ArgumentException();
            if(fileFormat == null) throw new ArgumentNullException("fileFormat");
            if(fileFormat.RequiresFile && (iocOutput == null))
                throw new ArgumentNullException("iocOutput");

            if(!AppPolicy.Try(AppPolicyId.Export)) return false;
            if(!fileFormat.SupportsExport) return false;
            if(!fileFormat.TryBeginExport()) return false;

            // bool bExistedAlready = File.Exists(strOutputFile);
            bool bExistedAlready = (fileFormat.RequiresFile ? IOConnection.FileExists(
                iocOutput) : false);

            // FileStream fsOut = new FileStream(strOutputFile, FileMode.Create,
            //	FileAccess.Write, FileShare.None);
            Stream sOut = (fileFormat.RequiresFile ? IOConnection.OpenWrite(
                iocOutput) : null);

            bool bResult = false;
            try { bResult = fileFormat.Export(pwExportInfo, sOut, slLogger); }
            catch(Exception ex) { MessageService.ShowWarning(ex); }

            if(sOut != null) sOut.Close();

            if(fileFormat.RequiresFile && (bResult == false) && (bExistedAlready == false))
            {
                try { IOConnection.DeleteFile(iocOutput); }
                catch(Exception) { }
            }

            return bResult;
        }
Esempio n. 6
0
        public static bool Export(PwExportInfo pwExportInfo, FileFormatProvider fileFormat,
                                  IOConnectionInfo iocOutput, IStatusLogger slLogger)
        {
            if (pwExportInfo == null)
            {
                throw new ArgumentNullException("pwExportInfo");
            }
            if (pwExportInfo.DataGroup == null)
            {
                throw new ArgumentException();
            }
            if (fileFormat == null)
            {
                throw new ArgumentNullException("fileFormat");
            }

            bool bFileReq = fileFormat.RequiresFile;

            if (bFileReq && (iocOutput == null))
            {
                throw new ArgumentNullException("iocOutput");
            }
            if (bFileReq && (iocOutput.Path.Length == 0))
            {
                throw new ArgumentException();
            }

            PwDatabase pd = pwExportInfo.ContextDatabase;

            Debug.Assert(pd != null);

            if (!AppPolicy.Try(AppPolicyId.Export))
            {
                return(false);
            }
            if (!AppPolicy.Current.ExportNoKey && (pd != null))
            {
                if (!KeyUtil.ReAskKey(pd, true))
                {
                    return(false);
                }
            }

            if (!fileFormat.SupportsExport)
            {
                return(false);
            }
            if (!fileFormat.TryBeginExport())
            {
                return(false);
            }

            CompositeKey ckOrgMasterKey = null;
            DateTime     dtOrgMasterKey = PwDefs.DtDefaultNow;

            PwGroup pgOrgData     = pwExportInfo.DataGroup;
            PwGroup pgOrgRoot     = ((pd != null) ? pd.RootGroup : null);
            bool    bParentGroups = (pwExportInfo.ExportParentGroups && (pd != null) &&
                                     (pgOrgData != pgOrgRoot));

            bool bExistedAlready = true;             // No deletion by default
            bool bResult         = false;

            try
            {
                if (pwExportInfo.ExportMasterKeySpec && fileFormat.RequiresKey &&
                    (pd != null))
                {
                    KeyCreationForm kcf = new KeyCreationForm();
                    kcf.InitEx((iocOutput ?? new IOConnectionInfo()), true);

                    if (UIUtil.ShowDialogNotValue(kcf, DialogResult.OK))
                    {
                        return(false);
                    }

                    ckOrgMasterKey = pd.MasterKey;
                    dtOrgMasterKey = pd.MasterKeyChanged;

                    pd.MasterKey        = kcf.CompositeKey;
                    pd.MasterKeyChanged = DateTime.UtcNow;

                    UIUtil.DestroyForm(kcf);
                }

                if (bParentGroups)
                {
                    PwGroup pgNew = WithParentGroups(pgOrgData, pd);
                    pwExportInfo.DataGroup = pgNew;
                    pd.RootGroup           = pgNew;
                }

                if (bFileReq)
                {
                    bExistedAlready = IOConnection.FileExists(iocOutput);
                }

                Stream s = (bFileReq ? IOConnection.OpenWrite(iocOutput) : null);
                try { bResult = fileFormat.Export(pwExportInfo, s, slLogger); }
                finally { if (s != null)
                          {
                              s.Close();
                          }
                }

                if (bFileReq && bResult)
                {
                    if (pwExportInfo.ExportPostOpen)
                    {
                        NativeLib.StartProcess(iocOutput.Path);
                    }
                    if (pwExportInfo.ExportPostShow)
                    {
                        WinUtil.ShowFileInFileManager(iocOutput.Path, true);
                    }
                }
            }
            catch (Exception ex) { MessageService.ShowWarning(ex); }
            finally
            {
                if (ckOrgMasterKey != null)
                {
                    pd.MasterKey        = ckOrgMasterKey;
                    pd.MasterKeyChanged = dtOrgMasterKey;
                }

                if (bParentGroups)
                {
                    pwExportInfo.DataGroup = pgOrgData;
                    pd.RootGroup           = pgOrgRoot;
                }
            }

            if (bFileReq && !bResult && !bExistedAlready)
            {
                try { IOConnection.DeleteFile(iocOutput); }
                catch (Exception) { }
            }

            return(bResult);
        }