Beispiel #1
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());
        }
Beispiel #2
0
        private static DateTime?GetRemoteLastModified(string filename, YandexWebDavClient webClient)
        {
            return(Async.Invoke(() =>
            {
                if (!webClient.IsFileExist(filename))
                {
                    return null;
                }

                return (DateTime?)webClient.GetLastModified(filename);
            }));
        }
Beispiel #3
0
 private byte[] DownloadFile(string filename, YandexWebDavClient webClient)
 {
     byte[] databaseData;
     try
     {
         databaseData = webClient.GetFile(filename, progress => { SetStatusText($"Download ({Math.Floor(progress)}%)"); });
     }
     catch (WebException httpEx)
     {
         var response = (HttpWebResponse)httpEx.Response;
         if (response.StatusCode != HttpStatusCode.NotFound)
         {
             throw new SynchronizeException("Database not found on YandexDisc. Please save it first.");
         }
         throw;
     }
     return(databaseData);
 }
Beispiel #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();
        }
Beispiel #5
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");
            }
        }
Beispiel #6
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());
        }
Beispiel #7
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);
            }
        }