示例#1
0
        /// <summary>
        /// Event handler to implement auto sync on save
        /// </summary>
        private void OnFileSaved(object sender, FileSavedEventArgs e)
        {
            if (e.Success == false)
            {
                return;
            }

            var conf = new YandexDiscSyncConf(_host.Database);

            if (AutoSyncMode.Save != (conf.AutoSyncMode & AutoSyncMode.Save))
            {
                return;
            }

            if (Keys.Shift == (Control.ModifierKeys & Keys.Shift))
            {
                SetStatusText("Shift Key pressed. Auto Sync ignored.");
                return;
            }
            if (!IsSyncConfigured(conf))
            {
                SetStatusText("YandexDiscSync not configured.");
                return;
            }
            SyncWithYandexDisc(SyncCommand.SyncRemote);
        }
示例#2
0
        private static DateTime GetLastModified(PwDatabase pwDatabase)
        {
            List <PwUuid> ignore    = new List <PwUuid>();
            PwEntry       confEntry = YandexDiscSyncConf.GetConfPwEntry(pwDatabase);

            ignore.Add(confEntry?.Uuid);
            ignore.AddRange(pwDatabase.DeletedObjects.Select(m => m.Uuid));

            DateTime lastModified = DateTime.MinValue;

            return(GetLastModified(pwDatabase.RootGroup, lastModified, ignore.ToArray()).ToUniversalTime());
        }
示例#3
0
        private bool IsSyncConfigured(YandexDiscSyncConf conf)
        {
            var required = new[] {
                conf.Username,
                conf.Password.ReadString()
            };

            if (required.Any(string.IsNullOrEmpty))
            {
                return(false);
            }

            return(true);
        }
示例#4
0
        private void SyncUpload(PwDatabase pwDatabase, YandexWebDavClient webClient, string databaseUuid,
                                YandexDiscSyncConf storageConf)
        {
            var lastModified = GetLastModified(pwDatabase);

            var location = _host.Database.IOConnectionInfo.Path;

            SetStatusText("Saving to Yandex Disc...");
            Async.Invoke(() =>
            {
                webClient.PutFile(
                    databaseUuid,
                    File.ReadAllBytes(location),
                    lastModified,
                    progress => { SetStatusText($"Saving to YandexDisc ({Math.Floor(progress)}%)"); });
            });
            SetStatusText("Successfull save to YandexDisc");

            storageConf.SyncRemoteLastModified = lastModified.ToString("u");
            storageConf.Save();
        }
示例#5
0
        /// <summary>
        /// Event handler for configuration menu entry
        /// </summary>
        private void OnConfigure(object sender, EventArgs e)
        {
            if (!_host.Database.IsOpen)
            {
                ShowMessageBox("You first need to open a database.");
                return;
            }

            if (_host.Database.IsOpen)
            {
                YandexDiscSyncConf conf = new YandexDiscSyncConf(_host.Database);

                VaultConnectionConfigForm form1 = new VaultConnectionConfigForm();
                form1.InitEx(conf);
                if (DialogResult.OK == UIUtil.ShowDialogAndDestroy(form1))
                {
                    conf.Save();
                    _host.MainWindow.UpdateUI(false, null, true, null, true, null, bSetModified: true);
                }
            }
        }
示例#6
0
 public YandexWebDavClient(YandexDiscSyncConf syncConf)
 {
     Username = syncConf.Username;
     Password = syncConf.Password;
     Location = syncConf.Location;
 }
示例#7
0
        private void SyncRemote(PwDatabase pwDatabase, YandexWebDavClient webClient, string filename, YandexDiscSyncConf vaultConf)
        {
            SetStatusText("Check changes...");
            var localLastModified = GetLastModified(pwDatabase);
            var lastModified      = GetRemoteLastModified(filename, webClient);

            if (lastModified != null && lastModified == localLastModified)
            {
                SetStatusText("No changes.");
                return;
            }

            if (lastModified != null)
            {
                SetStatusText("Downloading...");
                var dbData = Async.Invoke(() => DownloadFile(filename, webClient));
                if (dbData != null)
                {
                    SetStatusText("Sync...");
                    SyncDatabase(pwDatabase, dbData, true);
                    _host.MainWindow.Enabled = false;
                    localLastModified        = GetLastModified(pwDatabase);
                }
            }

            SetStatusText("Saving to YandexDisc...");
            Async.Invoke(() =>
            {
                webClient.PutFile(
                    filename,
                    File.ReadAllBytes(pwDatabase.IOConnectionInfo.Path),
                    localLastModified,
                    progress => { SetStatusText($"Saving to YandexDisc ({Math.Floor(progress)}%)"); });
            });
            SetStatusText("Successfull Sync Local <=> Remote");

            vaultConf.SyncRemoteLastModified = localLastModified.ToString("u");
            vaultConf.Save();
            pwDatabase.Save(new NullStatusLogger());
        }
示例#8
0
        private void SyncLocal(PwDatabase pwDatabase, YandexWebDavClient webClient, string filename, YandexDiscSyncConf vaultConf)
        {
            SetStatusText("Check changes...");
            var value = GetRemoteLastModified(filename, webClient);

            if (value == null)
            {
                SetStatusText("Database not found on Yandex Disc. Nothing to sync");
                return;
            }
            var lastModified = value.Value;

            if (vaultConf.SyncRemoteLastModified == lastModified.ToString("u"))
            {
                SetStatusText("No changes");
                return;
            }

            SetStatusText("Downloading...");
            var dbData = Async.Invoke(() => DownloadFile(filename, webClient));

            SetStatusText("Sync...");
            SyncDatabase(pwDatabase, dbData, false);
            SetStatusText("Successfull Sync Local <= Remote");

            vaultConf.SyncRemoteLastModified = lastModified.ToString("u");
            vaultConf.Save();
            pwDatabase.Save(new NullStatusLogger());
        }
示例#9
0
        private void SyncDownload(PwDatabase pwDatabase, YandexWebDavClient webClient, string filename, YandexDiscSyncConf storageConf)
        {
            var lastModified = GetRemoteLastModified(filename, webClient);

            if (lastModified != null)
            {
                SetStatusText("Downloading...");
                var dbData = Async.Invoke(() => DownloadFile(filename, webClient));

                ReplaceDatabase(pwDatabase, dbData);
                SetStatusText("Download Done.");
                storageConf.ChangeDatabase(_host.Database);
                storageConf.SyncRemoteLastModified = lastModified.Value.ToString("u");
                storageConf.Save();
                pwDatabase.Save(new NullStatusLogger());
            }
            else
            {
                SetStatusText("Database not found. Nothing to sync");
            }
        }
示例#10
0
        private void SyncWithYandexDisc(SyncCommand syncCommand)
        {
            PwDatabase pwDatabase = _host.Database;

            if (!pwDatabase.IsOpen)
            {
                ShowMessageBox("You first need to open a database.");
                return;
            }
            if (!pwDatabase.IOConnectionInfo.IsLocalFile())
            {
                ShowMessageBox("Only databases stored locally or on a network share are supported.\n" +
                               "Save your database locally or on a network share and try again.");
                return;
            }
            if (pwDatabase.Modified)
            {
                ShowMessageBox("Database has not saved changes. Save it first.");
                return;
            }

            YandexDiscSyncConf syncConf = new YandexDiscSyncConf(pwDatabase);

            if (!IsSyncConfigured(syncConf))
            {
                SetStatusText("Sync not Configured.");
                return;
            }

            _host.MainWindow.FileSaved  -= OnFileSaved;  // disable to not trigger when saving ourselves
            _host.MainWindow.FileOpened -= OnFileOpened; // disable to not trigger when opening ourselves
            _host.MainWindow.Enabled     = false;

            _lastStatus = "";

            try
            {
                var filename = Path.GetFileNameWithoutExtension(pwDatabase.IOConnectionInfo.Path);
                YandexWebDavClient webClient = new YandexWebDavClient(syncConf);

                if (syncCommand == SyncCommand.Download)
                {
                    try
                    {
                        SyncDownload(pwDatabase, webClient, filename, syncConf);
                    }
                    catch (Exception ex)
                    {
                        ShowMessageBox(ex.Message);
                        SetStatusText("SyncDownload Failed");
                    }
                }
                if (syncCommand == SyncCommand.Upload)
                {
                    try
                    {
                        SyncUpload(pwDatabase, webClient, filename, syncConf);
                    }
                    catch (Exception ex)
                    {
                        ShowMessageBox(ex.Message);
                        SetStatusText("SyncUpload Failed");
                    }
                }

                if (syncCommand == SyncCommand.SyncLocal)
                {
                    try
                    {
                        SyncLocal(pwDatabase, webClient, filename, syncConf);
                    }
                    catch (Exception ex)
                    {
                        ShowMessageBox(ex.Message);
                        SetStatusText("SyncLocal Failed");
                    }
                }

                if (syncCommand == SyncCommand.SyncRemote)
                {
                    try
                    {
                        SyncRemote(pwDatabase, webClient, filename, syncConf);
                    }
                    catch (Exception ex)
                    {
                        ShowMessageBox(ex.Message);
                        SetStatusText("SyncRemote Failed");
                    }
                }
            }
            catch (Exception ex)
            {
                ShowMessageBox(ex.Message);
                SetStatusText("Unknown Err: " + ex.Message);
            }

            _host.MainWindow.UpdateUI(false, null, true, null, true, null, false);
            _host.MainWindow.Enabled     = true;
            _host.MainWindow.FileSaved  += OnFileSaved;
            _host.MainWindow.FileOpened += OnFileOpened;
            if (_lastStatus != "")
            {
                SetStatusText(_lastStatus);
            }
        }