Beispiel #1
0
        public static bool? Synchronize(PwDatabase pwStorage, IUIOperations uiOps,
            bool bOpenFromUrl, Form fParent)
        {
            if(pwStorage == null) throw new ArgumentNullException("pwStorage");
            if(!pwStorage.IsOpen) return null;
            if(!AppPolicy.Try(AppPolicyId.Import)) return null;

            List<IOConnectionInfo> vConnections = new List<IOConnectionInfo>();
            if(bOpenFromUrl == false)
            {
                OpenFileDialog ofd = UIUtil.CreateOpenFileDialog(KPRes.Synchronize,
                    UIUtil.CreateFileTypeFilter(null, null, true), 1, null, true, true);

                if(ofd.ShowDialog() != DialogResult.OK) return null;

                foreach(string strSelFile in ofd.FileNames)
                    vConnections.Add(IOConnectionInfo.FromPath(strSelFile));
            }
            else // Open URL
            {
                IOConnectionForm iocf = new IOConnectionForm();
                iocf.InitEx(false, new IOConnectionInfo(), true, true);

                if(UIUtil.ShowDialogNotValue(iocf, DialogResult.OK)) return null;

                vConnections.Add(iocf.IOConnectionInfo);
                UIUtil.DestroyForm(iocf);
            }

            return Import(pwStorage, new KeePassKdb2x(), vConnections.ToArray(),
                true, uiOps, false, fParent);
        }
		// Public for plugins
		public void SaveDatabaseAs(PwDatabase pdToSave, IOConnectionInfo iocTo,
			bool bOnline, object sender, bool bCopy)
		{
			PwDatabase pd = (pdToSave ?? m_docMgr.ActiveDatabase);

			if(!pd.IsOpen) return;
			if(!AppPolicy.Try(AppPolicyId.SaveFile)) return;

			Guid eventGuid = Guid.NewGuid();
			if(this.FileSaving != null)
			{
				FileSavingEventArgs args = new FileSavingEventArgs(true, bCopy, pd, eventGuid);
				this.FileSaving(sender, args);
				if(args.Cancel) return;
			}

			DialogResult dr;
			IOConnectionInfo ioc = iocTo;

			if((ioc != null) && (ioc.Path.Length > 0))
			{
				dr = DialogResult.OK; // Caller (plugin) specified target file
			}
			else if(bOnline)
			{
				IOConnectionForm iocf = new IOConnectionForm();
				iocf.InitEx(true, pd.IOConnectionInfo.CloneDeep(), true, true);

				dr = iocf.ShowDialog();
				ioc = iocf.IOConnectionInfo;
				UIUtil.DestroyForm(iocf);
			}
			else
			{
				SaveFileDialogEx sfdDb = UIUtil.CreateSaveFileDialog(KPRes.SaveDatabase,
					UrlUtil.GetFileName(pd.IOConnectionInfo.Path),
					UIUtil.CreateFileTypeFilter(AppDefs.FileExtension.FileExt,
					KPRes.KdbxFiles, true), 1, AppDefs.FileExtension.FileExt,
					AppDefs.FileDialogContext.Database);

				GlobalWindowManager.AddDialog(sfdDb.FileDialog);
				dr = sfdDb.ShowDialog();
				GlobalWindowManager.RemoveDialog(sfdDb.FileDialog);

				if(dr == DialogResult.OK)
					ioc = IOConnectionInfo.FromPath(sfdDb.FileName);
			}

			if(dr == DialogResult.OK)
			{
				EcasPropertyDictionary dProps = new EcasPropertyDictionary();
				dProps.Set(EcasProperty.IOConnectionInfo, ioc);
				dProps.Set(EcasProperty.Database, pd);
				Program.TriggerSystem.RaiseEvent(EcasEventIDs.SavingDatabaseFile,
					dProps);

				UIBlockInteraction(true);

				ShowWarningsLogger swLogger = CreateShowWarningsLogger();
				swLogger.StartLogging(KPRes.SavingDatabase, true);
				ShutdownBlocker sdb = new ShutdownBlocker(this.Handle, KPRes.SavingDatabase);

				bool bSuccess = true;
				try
				{
					PreSavingEx(pd, ioc);
					pd.SaveAs(ioc, !bCopy, swLogger);
					PostSavingEx(!bCopy, pd, ioc, swLogger);
				}
				catch(Exception exSaveAs)
				{
					MessageService.ShowSaveWarning(ioc, exSaveAs, true);
					bSuccess = false;
				}

				sdb.Dispose();
				swLogger.EndLogging();

				// Immediately after the UIBlockInteraction call the form might
				// be closed and UpdateUIState might crash, if the order of the
				// two methods is swapped; so first update state, then unblock
				UpdateUIState(false);
				UIBlockInteraction(false); // Calls Application.DoEvents()

				if(this.FileSaved != null)
				{
					FileSavedEventArgs args = new FileSavedEventArgs(bSuccess, pd, eventGuid);
					this.FileSaved(sender, args);
				}
				if(bSuccess)
					Program.TriggerSystem.RaiseEvent(EcasEventIDs.SavedDatabaseFile,
						dProps);
			}
		}
		internal static IOConnectionInfo CompleteConnectionInfo(IOConnectionInfo ioc,
			bool bSave, bool bCanRememberCred, bool bTestConnection, bool bForceShow)
		{
			if(ioc == null) { Debug.Assert(false); return null; }
			if(!bForceShow && ((ioc.CredSaveMode == IOCredSaveMode.SaveCred) ||
				ioc.IsLocalFile() || ioc.IsComplete))
				return ioc.CloneDeep();

			IOConnectionForm dlg = new IOConnectionForm();
			dlg.InitEx(bSave, ioc.CloneDeep(), bCanRememberCred, bTestConnection);
			if(UIUtil.ShowDialogNotValue(dlg, DialogResult.OK)) return null;

			IOConnectionInfo iocResult = dlg.IOConnectionInfo;
			UIUtil.DestroyForm(dlg);
			return iocResult;
		}
        /// <summary>
        /// Open a database. This function opens the specified database and updates
        /// the user interface.
        /// </summary>
        public void OpenDatabase(IOConnectionInfo ioConnection, CompositeKey cmpKey,
            bool bOpenLocal)
        {
            // OnFileClose(null, null);
            // if(m_docMgr.ActiveDatabase.IsOpen) return;

            if(m_bFormLoading && Program.Config.Application.Start.MinimizedAndLocked &&
                (ioConnection != null) && (ioConnection.Path.Length > 0))
            {
                DocumentStateEx ds = m_docMgr.CreateNewDocument(true);
                ds.LockedIoc = ioConnection.CloneDeep();
                UpdateUI(true, ds, true, null, true, null, false);
                return;
            }

            IOConnectionInfo ioc;
            if(ioConnection == null)
            {
                if(bOpenLocal)
                {
                    OpenFileDialog ofdDb = UIUtil.CreateOpenFileDialog(KPRes.OpenDatabaseFile,
                        UIUtil.CreateFileTypeFilter("kdbx", KPRes.KdbxFiles, true), 1,
                        null, false, false);

                    GlobalWindowManager.AddDialog(ofdDb);
                    DialogResult dr = ofdDb.ShowDialog();
                    GlobalWindowManager.RemoveDialog(ofdDb);
                    if(dr != DialogResult.OK) return;

                    ioc = IOConnectionInfo.FromPath(ofdDb.FileName);
                }
                else
                {
                    IOConnectionForm iocf = new IOConnectionForm();
                    iocf.InitEx(false, new IOConnectionInfo(), true, true);
                    if(iocf.ShowDialog() != DialogResult.OK) return;

                    ioc = iocf.IOConnectionInfo;
                }
            }
            else // ioConnection != null
            {
                ioc = ioConnection.CloneDeep();

                if((ioc.CredSaveMode != IOCredSaveMode.SaveCred) &&
                    (!ioc.IsLocalFile()))
                {
                    IOConnectionForm iocf = new IOConnectionForm();
                    iocf.InitEx(false, ioc.CloneDeep(), true, true);
                    if(iocf.ShowDialog() != DialogResult.OK) return;

                    ioc = iocf.IOConnectionInfo;
                }
            }

            if((ioc == null) || !ioc.CanProbablyAccess())
            {
                MessageService.ShowWarning(ioc.GetDisplayName(), KPRes.FileNotFoundError);
                return;
            }

            if(OpenDatabaseRestoreIfOpened(ioc)) return;

            PwDatabase pwOpenedDb = null;
            if(cmpKey == null)
            {
                for(int iTry = 0; iTry < 3; ++iTry)
                {
                    KeyPromptForm kpf = new KeyPromptForm();
                    kpf.InitEx(ioc.GetDisplayName(), IsFileLocked(null));

                    DialogResult dr = kpf.ShowDialog();
                    if(dr == DialogResult.Cancel) break;
                    else if(kpf.HasClosedWithExit)
                    {
                        Debug.Assert(dr == DialogResult.Abort);
                        OnFileExit(null, null);
                        return;
                    }

                    pwOpenedDb = OpenDatabaseInternal(ioc, kpf.CompositeKey);
                    if(pwOpenedDb != null) break;
                }
            }
            else // cmpKey != null
            {
                pwOpenedDb = OpenDatabaseInternal(ioc, cmpKey);
            }

            if((pwOpenedDb == null) || !pwOpenedDb.IsOpen) return;

            string strName = pwOpenedDb.IOConnectionInfo.GetDisplayName();
            m_mruList.AddItem(strName, pwOpenedDb.IOConnectionInfo.CloneDeep());

            DocumentStateEx dsExisting = m_docMgr.FindDocument(pwOpenedDb);
            if(dsExisting != null) m_docMgr.ActiveDocument = dsExisting;

            bool bCorrectDbActive = (m_docMgr.ActiveDocument.Database == pwOpenedDb);
            Debug.Assert(bCorrectDbActive);

            AutoEnableVisualHiding();

            if(Program.Config.Application.Start.OpenLastFile)
                Program.Config.Application.LastUsedFile =
                    pwOpenedDb.IOConnectionInfo.CloneDeep();
            else
                Program.Config.Application.LastUsedFile = new IOConnectionInfo();

            if(bCorrectDbActive)
                m_docMgr.ActiveDocument.LockedIoc = new IOConnectionInfo(); // Clear

            if(this.FileOpened != null)
            {
                FileOpenedEventArgs ea = new FileOpenedEventArgs(pwOpenedDb);
                this.FileOpened(this, ea);
            }

            UpdateUI(true, null, true, null, true, null, false);
            UpdateColumnSortingIcons();

            if(bCorrectDbActive && pwOpenedDb.IsOpen &&
                Program.Config.Application.FileOpening.ShowSoonToExpireEntries)
            {
                ShowExpiredEntries(true, 7);

                // Avoid view being destroyed by the unlocking routine
                pwOpenedDb.LastSelectedGroup = PwUuid.Zero;
            }
            else if(bCorrectDbActive && pwOpenedDb.IsOpen &&
                Program.Config.Application.FileOpening.ShowExpiredEntries)
            {
                ShowExpiredEntries(true, 0);

                // Avoid view being destroyed by the unlocking routine
                pwOpenedDb.LastSelectedGroup = PwUuid.Zero;
            }

            ResetDefaultFocus(null);
        }
        private void SaveDatabaseAs(bool bOnline, object sender, bool bCopy)
        {
            if(!m_docMgr.ActiveDatabase.IsOpen) return;
            if(!AppPolicy.Try(AppPolicyId.SaveFile)) return;

            PwDatabase pd = m_docMgr.ActiveDatabase;

            Guid eventGuid = Guid.NewGuid();
            if(this.FileSaving != null)
            {
                FileSavingEventArgs args = new FileSavingEventArgs(true, bCopy, pd, eventGuid);
                this.FileSaving(sender, args);
                if(args.Cancel) return;
            }

            DialogResult dr;
            IOConnectionInfo ioc = new IOConnectionInfo();

            if(bOnline)
            {
                IOConnectionForm iocf = new IOConnectionForm();
                iocf.InitEx(true, pd.IOConnectionInfo.CloneDeep(), true, true);

                dr = iocf.ShowDialog();
                ioc = iocf.IOConnectionInfo;
            }
            else
            {
                SaveFileDialog sfdDb = UIUtil.CreateSaveFileDialog(KPRes.SaveDatabase,
                    UrlUtil.GetFileName(pd.IOConnectionInfo.Path),
                    UIUtil.CreateFileTypeFilter("kdbx", KPRes.KdbxFiles, true),
                    1, "kdbx", false);

                GlobalWindowManager.AddDialog(sfdDb);
                dr = sfdDb.ShowDialog();
                GlobalWindowManager.RemoveDialog(sfdDb);

                if(dr == DialogResult.OK)
                    ioc = IOConnectionInfo.FromPath(sfdDb.FileName);
            }

            if(dr == DialogResult.OK)
            {
                ShowWarningsLogger swLogger = CreateShowWarningsLogger();
                swLogger.StartLogging(KPRes.SavingDatabase, true);

                bool bSuccess = true;
                try
                {
                    pd.SaveAs(ioc, !bCopy, swLogger);

                    PostSavingEx(!bCopy, pd, ioc);
                }
                catch(Exception exSaveAs)
                {
                    MessageService.ShowSaveWarning(ioc, exSaveAs, true);
                    bSuccess = false;
                }

                swLogger.EndLogging();

                if(this.FileSaved != null)
                {
                    FileSavedEventArgs args = new FileSavedEventArgs(bSuccess, pd, eventGuid);
                    this.FileSaved(sender, args);
                }
            }

            UpdateUIState(false);
        }
		private void SaveDatabaseAs(bool bOnline, object sender, bool bCopy)
		{
			if(!m_docMgr.ActiveDatabase.IsOpen) return;
			if(!AppPolicy.Try(AppPolicyId.SaveFile)) return;

			PwDatabase pd = m_docMgr.ActiveDatabase;

			Guid eventGuid = Guid.NewGuid();
			if(this.FileSaving != null)
			{
				FileSavingEventArgs args = new FileSavingEventArgs(true, bCopy, pd, eventGuid);
				this.FileSaving(sender, args);
				if(args.Cancel) return;
			}

			DialogResult dr;
			IOConnectionInfo ioc = new IOConnectionInfo();

			if(bOnline)
			{
				IOConnectionForm iocf = new IOConnectionForm();
				iocf.InitEx(true, pd.IOConnectionInfo.CloneDeep(), true, true);

				dr = iocf.ShowDialog();
				ioc = iocf.IOConnectionInfo;
			}
			else
			{
				SaveFileDialog sfdDb = UIUtil.CreateSaveFileDialog(KPRes.SaveDatabase,
					UrlUtil.GetFileName(pd.IOConnectionInfo.Path),
					UIUtil.CreateFileTypeFilter(AppDefs.FileExtension.FileExt,
					KPRes.KdbxFiles, true), 1, AppDefs.FileExtension.FileExt, false, true);

				GlobalWindowManager.AddDialog(sfdDb);
				dr = sfdDb.ShowDialog();
				GlobalWindowManager.RemoveDialog(sfdDb);

				if(dr == DialogResult.OK)
					ioc = IOConnectionInfo.FromPath(sfdDb.FileName);
			}

			if(dr == DialogResult.OK)
			{
				Program.TriggerSystem.RaiseEvent(EcasEventIDs.SavingDatabaseFile,
					ioc.Path);

				UIBlockInteraction(true);

				ShowWarningsLogger swLogger = CreateShowWarningsLogger();
				swLogger.StartLogging(KPRes.SavingDatabase, true);

				bool bSuccess = true;
				try
				{
					PreSavingEx(pd);
					pd.SaveAs(ioc, !bCopy, swLogger);
					PostSavingEx(!bCopy, pd, ioc, swLogger);
				}
				catch(Exception exSaveAs)
				{
					MessageService.ShowSaveWarning(ioc, exSaveAs, true);
					bSuccess = false;
				}

				swLogger.EndLogging();

				// Immediately after the UIBlockInteraction call the form might
				// be closed and UpdateUIState might crash, if the order of the
				// two methods is swapped; so first update state, then unblock
				UpdateUIState(false);
				UIBlockInteraction(false); // Calls Application.DoEvents()

				if(this.FileSaved != null)
				{
					FileSavedEventArgs args = new FileSavedEventArgs(bSuccess, pd, eventGuid);
					this.FileSaved(sender, args);
				}
				if(bSuccess)
					Program.TriggerSystem.RaiseEvent(EcasEventIDs.SavedDatabaseFile,
						ioc.Path);
			}
		}