Exemplo n.º 1
0
        /// <summary>
        /// Set the current status text.
        /// </summary>
        /// <param name="strNewText">Status text.</param>
        /// <param name="lsType">Type of the message.</param>
        /// <returns>Returns <c>true</c> if the caller should continue
        /// the current work.</returns>
        public bool SetText(string strNewText, LogStatusType lsType)
        {
            Debug.Assert(m_bStartedLogging && !m_bEndedLogging);

            if((m_slForm == null) && ((lsType == LogStatusType.Warning) ||
                (lsType == LogStatusType.Error)))
            {
                m_slForm = new StatusLoggerForm();
                m_slForm.InitEx(false);

                m_slForm.Show();
                m_slForm.BringToFront();

                bool bLoggingStarted = false;
                foreach(KeyValuePair<LogStatusType, string> kvp in m_vCachedMessages)
                {
                    if(!bLoggingStarted)
                    {
                        m_slForm.StartLogging(kvp.Value, true);
                        bLoggingStarted = true;
                    }
                    else m_slForm.SetText(kvp.Value, kvp.Key);
                }
                Debug.Assert(bLoggingStarted);

                m_vCachedMessages.Clear();
            }

            bool b = true;
            if(m_sbDefault != null) b &= m_sbDefault.SetText(strNewText, lsType);
            if(m_slForm != null) b &= m_slForm.SetText(strNewText, lsType);

            if(m_slForm == null)
                m_vCachedMessages.Add(new KeyValuePair<LogStatusType, string>(
                    lsType, strNewText));

            return b;
        }
Exemplo n.º 2
0
        private static bool PerformImport(PwDatabase pwDatabase, FileFormatProvider fmtImp,
            IOConnectionInfo[] vConnections, bool bSynchronize, IUIOperations uiOps,
            bool bForceSave)
        {
            if(fmtImp.TryBeginImport() == false) return false;

            bool bUseTempDb = (fmtImp.SupportsUuids || fmtImp.RequiresKey);
            bool bAllSuccess = true;

            // if(bSynchronize) { Debug.Assert(vFiles.Length == 1); }

            StatusLoggerForm slf = new StatusLoggerForm();
            slf.InitEx(false);
            slf.Show();

            if(bSynchronize) slf.StartLogging(KPRes.Synchronize, false);
            else slf.StartLogging(KPRes.ImportingStatusMsg, false);

            if(vConnections.Length == 0)
            {
                try { fmtImp.Import(pwDatabase, null, slf); }
                catch(Exception exSingular)
                {
                    if((exSingular.Message != null) && (exSingular.Message.Length > 0))
                    {
                        slf.SetText(exSingular.Message, LogStatusType.Warning);
                        MessageService.ShowWarning(exSingular);
                    }
                }

                slf.EndLogging(); slf.Close();
                return true;
            }

            foreach(IOConnectionInfo iocIn in vConnections)
            {
                Stream s = null;

                try { s = IOConnection.OpenRead(iocIn); }
                catch(Exception exFile)
                {
                    MessageService.ShowWarning(iocIn.GetDisplayName(), exFile);
                    bAllSuccess = false;
                    continue;
                }

                if(s == null) { Debug.Assert(false); bAllSuccess = false; continue; }

                PwDatabase pwImp;
                if(bUseTempDb)
                {
                    pwImp = new PwDatabase();
                    pwImp.New(new IOConnectionInfo(), pwDatabase.MasterKey);
                    pwImp.MemoryProtection = pwDatabase.MemoryProtection.CloneDeep();
                }
                else pwImp = pwDatabase;

                if(fmtImp.RequiresKey && !bSynchronize)
                {
                    KeyPromptForm kpf = new KeyPromptForm();
                    kpf.InitEx(iocIn.GetDisplayName(), false);

                    if(kpf.ShowDialog() != DialogResult.OK) { s.Close(); continue; }

                    pwImp.MasterKey = kpf.CompositeKey;
                }
                else if(bSynchronize) pwImp.MasterKey = pwDatabase.MasterKey;

                slf.SetText((bSynchronize ? KPRes.Synchronizing : KPRes.ImportingStatusMsg) +
                    " " + iocIn.GetDisplayName(), LogStatusType.Info);

                try { fmtImp.Import(pwImp, s, slf); }
                catch(Exception excpFmt)
                {
                    string strMsgEx = excpFmt.Message;
                    if(bSynchronize)
                    {
                        strMsgEx += MessageService.NewParagraph +
                            KPRes.SynchronizingHint;
                    }

                    MessageService.ShowWarning(strMsgEx);

                    s.Close();
                    bAllSuccess = false;
                    continue;
                }

                s.Close();

                if(bUseTempDb)
                {
                    PwMergeMethod mm;
                    if(!fmtImp.SupportsUuids) mm = PwMergeMethod.CreateNewUuids;
                    else if(bSynchronize) mm = PwMergeMethod.Synchronize;
                    else
                    {
                        ImportMethodForm imf = new ImportMethodForm();
                        if(imf.ShowDialog() != DialogResult.OK)
                            continue;
                        mm = imf.MergeMethod;
                    }

                    slf.SetText(KPRes.MergingData, LogStatusType.Info);

                    try { pwDatabase.MergeIn(pwImp, mm); }
                    catch(Exception exMerge)
                    {
                        MessageService.ShowWarning(iocIn.GetDisplayName(),
                            KPRes.ImportFailed, exMerge);

                        bAllSuccess = false;
                        continue;
                    }
                }
            }

            slf.EndLogging(); slf.Close();

            if(bSynchronize && bAllSuccess)
            {
                Debug.Assert(uiOps != null);
                if(uiOps == null) throw new ArgumentNullException("uiOps");

                if(uiOps.UIFileSave(bForceSave))
                {
                    foreach(IOConnectionInfo ioc in vConnections)
                    {
                        try
                        {
                            if(pwDatabase.IOConnectionInfo.IsLocalFile())
                            {
                                if((pwDatabase.IOConnectionInfo.Path != ioc.Path) &&
                                    ioc.IsLocalFile())
                                {
                                    File.Copy(pwDatabase.IOConnectionInfo.Path,
                                        ioc.Path, true);
                                }
                            }
                            else pwDatabase.SaveAs(ioc, false, null);
                        }
                        catch(Exception exSync)
                        {
                            MessageService.ShowWarning(KPRes.SyncFailed,
                                pwDatabase.IOConnectionInfo.GetDisplayName() +
                                MessageService.NewLine + ioc.GetDisplayName(), exSync);

                            bAllSuccess = false;
                            continue;
                        }
                    }
                }
                else
                {
                    MessageService.ShowWarning(KPRes.SyncFailed,
                        pwDatabase.IOConnectionInfo.GetDisplayName());

                    bAllSuccess = false;
                }
            }
            else if(bSynchronize) // Synchronized but not successfully imported
            {
                MessageService.ShowWarning(KPRes.SyncFailed,
                    pwDatabase.IOConnectionInfo.GetDisplayName());

                bAllSuccess = false;
            }

            return bAllSuccess;
        }