Ejemplo n.º 1
0
        public override SyncInfo Initialize(DatabaseInfo info)
        {
            if (info == null)
                throw new ArgumentNullException("info");

            var details = info.Details;
            var parts = details.Url.Split('\n');

            _client = new WebDavClient(
                parts[1], parts[2]);

            _info = new SyncInfo
            {
                Path = parts[0],
                Modified = details.Modified,
                HasLocalChanges = details.HasLocalChanges,
            };

            info.OpenDatabaseFile(x =>
            {
                using (var buffer = new MemoryStream())
                {
                    BufferEx.CopyStream(x, buffer);
                    _info.Database = buffer.ToArray();
                }
            });

            return _info;
        }
Ejemplo n.º 2
0
        public static void Update(DatabaseInfo info,
            Func<DatabaseInfo, bool> queryUpdate,
            Action<SyncCompleteInfo> report)
        {
            if (info == null) throw new ArgumentNullException("info");
            if (queryUpdate == null) throw new ArgumentNullException("queryUpdate");
            if (report == null) throw new ArgumentNullException("report");

            var details = info.Details;
            var url = new Uri(details.Url);
            var client = CreateClient(url.UserInfo);

            var syncInfo = new SyncInfo
            {
                Path = url.LocalPath,
                Modified = details.Modified,
                HasLocalChanges = details.HasLocalChanges,
            };

            info.OpenDatabaseFile(x =>
            {
                using (var buffer = new MemoryStream())
                {
                    BufferEx.CopyStream(x, buffer);
                    syncInfo.Database = buffer.ToArray();
                }
            });

            Synchronize(client, syncInfo, x =>
            {
                if (queryUpdate(info))
                    report(x);
            });
        }
Ejemplo n.º 3
0
        public override SyncInfo Initialize(DatabaseInfo info)
        {
            if (info == null)
                throw new ArgumentNullException("info");

            var details = info.Details;
            var url = new Uri(details.Url);
            _client = CreateClient(url.UserInfo);

            _info = new SyncInfo
            {
                Path = url.LocalPath,
                Modified = details.Modified,
                HasLocalChanges = details.HasLocalChanges,
            };

            info.OpenDatabaseFile(x =>
            {
                using (var buffer = new MemoryStream())
                {
                    BufferEx.CopyStream(x, buffer);
                    _info.Database = buffer.ToArray();
                }
            });

            return _info;
        }
Ejemplo n.º 4
0
        public override SyncInfo Initialize(DatabaseInfo info)
        {
            if (info == null)
                throw new ArgumentNullException("info");

            var details = info.Details;

            string id;
            _client = OneDriveClient
                .ParsePath(details.Url, out id);

            _info = new SyncInfo
            {
                Path = id,
                Modified = details.Modified,
                HasLocalChanges = details.HasLocalChanges,
            };

            info.OpenDatabaseFile(x =>
            {
                using (var buffer = new MemoryStream())
                {
                    BufferEx.CopyStream(x, buffer);
                    _info.Database = buffer.ToArray();
                }
            });

            return _info;
        }
Ejemplo n.º 5
0
        private void DatabaseUpdated(DatabaseInfo info,
            bool success, string error)
        {
            var dispatcher = Dispatcher;
            var listItem = _items.First(
                x => x.Info == info);

            dispatcher.BeginInvoke(() =>
                listItem.IsUpdating = false);

            if (success)
            {
                dispatcher.BeginInvoke(() =>
                    UpdateItem(listItem, true));

                return;
            }

            var msg = string.Format(
                Properties.Resources.UpdateFailure,
                info.Details.Name, error);

            dispatcher.BeginInvoke(() =>
            {
                listItem.UpdatedIcon = null;

                MessageBox.Show(msg,
                    Properties.Resources.UpdateTitle,
                    MessageBoxButton.OK);
            });
        }
Ejemplo n.º 6
0
        public static void Update(DatabaseInfo info,
            Func<DatabaseInfo, bool> queryUpdate,
            ReportUpdateResult report)
        {
            if (info == null) throw new ArgumentNullException("info");
            if (queryUpdate == null) throw new ArgumentNullException("queryUpdate");
            if (report == null) throw new ArgumentNullException("report");

            var details = info.Details;
            var url = new Uri(details.Url);

            var client = CreateClient(url.UserInfo);
            client.DownloadAsync(url.LocalPath, x =>
            {
                if (!queryUpdate(info))
                    return;

                if (x == null)
                {
                    report(info, false,
                        DropBoxResources.DownloadError);

                    return;
                }

                info.SetDatabase(x, details);
                report(info, true, null);
            });
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Notifies that the database has been deleted.
        /// </summary>
        /// <param name="database">The database.</param>
        public static void Deleted(DatabaseInfo database)
        {
            var uri = GetUri(database);
            var tile = GetTile(uri);

            if (tile != null)
                tile.Delete();
        }
Ejemplo n.º 8
0
        private static void _wkOpen_DoWork(
            object sender, DoWorkEventArgs e)
        {
            var args = (OpenArgs)e.Argument;
            var database = new DatabaseInfo(args.Folder);

            e.Result = database.Open(args.Dispatcher,
                args.Password, args.SavePassword);
        }
Ejemplo n.º 9
0
        private void OnFileDownloaded(MetaListItemInfo item,
            string path, byte[] bytes)
        {
            var dispatcher = Dispatcher;

            try
            {
                using (var buffer = new MemoryStream(bytes))
                {
                    if (string.IsNullOrEmpty(_folder))
                    {
                        if (!DatabaseVerifier.Verify(dispatcher, buffer))
                            return;

                        var name = item.Title.RemoveKdbx();

                        var storage = new DatabaseInfo();
                        storage.SetDatabase(buffer, new DatabaseDetails
                        {
                            Url = path,
                            Name = name,
                            Modified = item.Modified,
                            Type = SourceTypes.Synchronizable,
                            Source = DatabaseUpdater.ONEDRIVE_UPDATER,
                        });
                        dispatcher.BeginInvoke(
                    this.BackToDBs);
                    }
                    else
                    {
                        var hash = KeyFile.GetKey(buffer);
                        if (hash == null)
                        {
                            dispatcher.BeginInvoke(() => MessageBox.Show(
                                Properties.Resources.InvalidKeyFile,
                                Properties.Resources.KeyFileTitle,
                                MessageBoxButton.OK));
                            return;
                        }

                        new DatabaseInfo(_folder)
                            .SetKeyFile(hash);

                        dispatcher.BeginInvoke(
                    this.BackToDBPassword);
                    }
                }


            }
            finally
            {
                dispatcher.BeginInvoke(() =>
                    progBusy.IsBusy = false);
            }
        }
Ejemplo n.º 10
0
        public static void CacheDb(DatabaseInfo info,
            string name, Database database)
        {
            _info = info;
            _standards.Clear();
            Database = database;

            _appSettings[KEY_DATABASE] = name;
            _appSettings.Save();
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Notifies that the database has been renamed.
        /// </summary>
        /// <param name="database">The database.</param>
        public static void Renamed(DatabaseInfo database)
        {
            var uri = GetUri(database);

            var tile = GetTile(uri);
            if (tile == null)
                return;

            var data = GetData(database);
            tile.Update(data);
        }
Ejemplo n.º 12
0
        protected override void OnNavigatedTo(
            bool cancelled, NavigationEventArgs e)
        {
            if (cancelled)
                return;

            _folder = NavigationContext.QueryString["db"];
            _hasKeyFile = new DatabaseInfo(_folder).HasKeyFile;

            UpdatePasswordStatus();
        }
Ejemplo n.º 13
0
        public static void Update(DatabaseInfo info,
            Func<DatabaseInfo, bool> queryUpdate,
            ReportUpdateResult report)
        {
            if (info == null) throw new ArgumentNullException("info");
            if (queryUpdate == null) throw new ArgumentNullException("queryUpdate");
            if (report == null) throw new ArgumentNullException("report");

            var details = info.Details;
            var urlInfo = WebUtils.Deserialize(details.Url);
            var credentials = WebUtils.CreateCredentials(
                urlInfo.User, urlInfo.Password, urlInfo.Domain);

            WebUtils.Download(urlInfo.Url, credentials, (req, getResponse) =>
            {
                if (!queryUpdate(info))
                    return;

                HttpWebResponse res;

                try
                {
                    res = getResponse();
                }
                catch (WebException ex)
                {
                    report(info, SyncResults.Failed, ex.Message);
                    return;
                }

                using (var buffer = new MemoryStream())
                {
                    using (var stream = res.GetResponseStream())
                    {
                        BufferEx.CopyStream(stream, buffer);
                        buffer.Position = 0;
                    }

                    var check = DatabaseVerifier
                        .VerifyUnattened(buffer);

                    if (check.Result == VerifyResultTypes.Error)
                    {
                        report(info, SyncResults.Failed,
                            check.Message);

                        return;
                    }

                    info.SetDatabase(buffer, details);
                    report(info, SyncResults.Downloaded, null);
                }
            });
        }
Ejemplo n.º 14
0
        private static ShellTileData GetData(DatabaseInfo database)
        {
            var background = new Uri(
                "Background.png",
                UriKind.Relative);

            return new StandardTileData
            {
                BackgroundImage = background,
                Title = database.Details.Name,
            };
        }
Ejemplo n.º 15
0
        internal DatabaseItem(DatabaseInfo info)
        {
            if (info == null)
                throw new ArgumentNullException("info");

            _info = info;
            _hasKeyFile = info.HasKeyFile;
            _hasPassword = info.HasPassword;

            if (info.Details == null)
                info.LoadDetails();
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Pins the specified database.
        /// </summary>
        /// <param name="database">The database.</param>
        /// <returns><c>true</c> if the database is pinned;
        /// otherwise, <c>false</c>.</returns>
        public static bool Pin(DatabaseInfo database)
        {
            var uri = GetUri(database);

            var tile = GetTile(uri);
            if (tile != null)
                return false;

            var data = GetData(database);
            ShellTile.Create(uri, data);

            return true;
        }
Ejemplo n.º 17
0
        public Synchronizer(DatabaseInfo db,
            IServiceAdapter adapter,
            Func<DatabaseInfo, bool> queryUpdate)
        {
            if (adapter == null) throw new ArgumentNullException("adapter");
            if (db == null) throw new ArgumentNullException("db");
            if (queryUpdate == null) throw new ArgumentNullException("queryUpdate");

            _db = db;
            _adapter = adapter;
            _queryUpdate = queryUpdate;
            _info = _adapter.Initialize(db);

            _adapter.Error += _adapter_Error;
        }
Ejemplo n.º 18
0
        protected override void OnNavigatedTo(
            bool cancelled, NavigationEventArgs e)
        {
            if (cancelled)
                return;

            _database = new DatabaseInfo(
                NavigationContext.QueryString["db"]);
            _database.LoadDetails();

            _originalName = _database.Details.Name;

            lblRename.Text = string.Format(
                lblRename.Text, _originalName);
        }
Ejemplo n.º 19
0
        public static void Update(DatabaseInfo info,
            Func<DatabaseInfo, bool> queryUpdate,
            ReportUpdateResult report)
        {
            switch (info.Details.Source)
            {
                case DROPBOX_UPDATER:
                    DropBoxUpdater.Update(info, queryUpdate,
                        x => Report(info, x, report));
                    break;

                case WEB_UPDATER:
                    WebUpdater.Update(info,
                        queryUpdate, report);
                    break;
            }
        }
Ejemplo n.º 20
0
        private static void Report(DatabaseInfo info,
            SyncCompleteInfo result, ReportUpdateResult report)
        {
            string msg = null;
            var details = info.Details;

            switch (result.Result)
            {
                case SyncResults.Downloaded:
                    using (var buffer = new MemoryStream(result.Database))
                        info.SetDatabase(buffer, details);
                    break;

                case SyncResults.Uploaded:
                    details.Modified = result.Modified;
                    info.SaveDetails();
                    break;

                case SyncResults.Conflict:
                    details.Url = result.Path;
                    details.Modified = result.Modified;
                    info.SaveDetails();

                    msg = string.Format(
                        Properties.Resources.Conflict,
                        new Uri(result.Path).LocalPath);
                    break;

                case SyncResults.Failed:
                    msg = Properties.Resources
                        .DownloadError;
                    break;
            }

            report(info, result.Result, msg);
        }
Ejemplo n.º 21
0
 private void Open(DatabaseInfo database,
     bool fromTile)
 {
     if (!fromTile)
     {
         if (!database.HasPassword)
         {
             this.NavigateTo<Password>(
                 "db={0}", database.Folder);
         }
         else
         {
             database.Open(Dispatcher);
             this.NavigateTo<GroupDetails>();
         }
     }
     else
     {
         if (!database.HasPassword)
         {
             this.NavigateTo<Password>(
                 "db={0}&fromTile=1", database.Folder);
         }
         else
         {
             database.Open(Dispatcher);
             this.NavigateTo<GroupDetails>("fromTile=1");
         }
     }
 }
Ejemplo n.º 22
0
        private void DatabaseUpdated(DatabaseInfo info,
            SyncResults result, string error)
        {
            var listItem = _items.FirstOrDefault(
                x => x.Info == info);
            if (listItem == null)
                return;

            var dispatcher = Dispatcher;
            dispatcher.BeginInvoke(() =>
                listItem.IsUpdating = false);

            switch (result)
            {
                case SyncResults.NoChange:
                case SyncResults.Downloaded:
                    dispatcher.BeginInvoke(() =>
                        UpdateItem(listItem, "updated"));
                    break;

                case SyncResults.Uploaded:

                    dispatcher.BeginInvoke(() =>
                        UpdateItem(listItem, "uploaded"));
                    break;

                case SyncResults.Conflict:
                    dispatcher.BeginInvoke(() =>
                    {
                        UpdateItem(listItem, "uploaded");

                        MessageBox.Show(error,
                            Properties.Resources.ConflictTitle,
                            MessageBoxButton.OK);
                    });
                    break;

                case SyncResults.Failed:
                    var msg = string.Format(
                        Properties.Resources.UpdateFailure,
                        info.Details.Name, error);

                    dispatcher.BeginInvoke(() =>
                    {
                        listItem.UpdatedIcon = null;

                        MessageBox.Show(msg,
                            Properties.Resources.UpdateTitle,
                            MessageBoxButton.OK);
                    });
                    break;
            }
        }
Ejemplo n.º 23
0
        private void OnFileDownloaded(byte[] file,
            string path, string title, string modified)
        {
            var dispatcher = Dispatcher;

            try
            {
                using (var buffer = new MemoryStream(file))
                {
                    if (string.IsNullOrEmpty(_folder))
                    {
                        if (!DatabaseVerifier.Verify(dispatcher, buffer))
                            return;

                        var storage = new DatabaseInfo();
                        storage.SetDatabase(buffer, new DatabaseDetails
                        {
                            Modified = modified,
                            Name = title.RemoveKdbx(),
                            Url = _client.GetUrl(path),
                            Type = SourceTypes.Synchronizable,
                            Source = DatabaseUpdater.WEBDAV_UPDATER,
                        });
                    }
                    else
                    {
                        var hash = KeyFile.GetKey(buffer);
                        if (hash == null)
                        {
                            dispatcher.BeginInvoke(() => MessageBox.Show(
                                Properties.Resources.InvalidKeyFile,
                                Properties.Resources.KeyFileTitle,
                                MessageBoxButton.OK));

                            return;
                        }

                        new DatabaseInfo(_folder)
                            .SetKeyFile(hash);
                    }
                }

                dispatcher.BeginInvoke(
                    this.BackToDBs);
            }
            finally
            {
                dispatcher.BeginInvoke(() =>
                    progBusy.IsBusy = false);
            }
        }
Ejemplo n.º 24
0
        private void OnFileDownloaded(byte[] file,
            string path, string title, string modified)
        {
            var dispatcher = Dispatcher;

            try
            {
                using (var buffer = new MemoryStream(file))
                {
                    if (string.IsNullOrEmpty(_folder))
                    {
                        if (!DatabaseVerifier.Verify(dispatcher, buffer))
                            return;

                        var storage = new DatabaseInfo();
                        storage.SetDatabase(buffer, new DatabaseDetails
                        {
                            Url = path,
                            Name = title,
                            Modified = modified,
                            Source = DatabaseUpdater.DROPBOX_UPDATER,
                        });
                    }
                    else
                    {
                        var hash = KeyFile.GetKey(buffer);
                        if (hash == null)
                        {
                            dispatcher.BeginInvoke(() => MessageBox.Show(
                                Properties.Resources.InvalidKeyFile,
                                Properties.Resources.KeyFileTitle,
                                MessageBoxButton.OK));

                            return;
                        }

                        new DatabaseInfo(_folder)
                            .SetKeyFile(hash);
                    }
                }

                dispatcher.BeginInvoke(
                    GoBack<MainPage>);
            }
            finally
            {
                dispatcher.BeginInvoke(() =>
                    progBusy.IsBusy = false);
            }
        }
Ejemplo n.º 25
0
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            _database = new DatabaseInfo(NavigationContext.QueryString["db"]);
            _database.LoadDetails();

            _originalName = _database.Details.Name;

            DateTime convertedDate;
            convertedDate = DateTime.Now;

            txtName.Text = _originalName;
            var urlstr = _database.Details.Url;

            var locCH = "";
            if (_database.Details.HasLocalChanges)
                locCH = "\n " + Strings.DBDetail_LocalCH;

            mnuDelKeyFile.IsEnabled = _database.HasKeyFile;
            mnuClearPW.IsEnabled =_database.HasPassword;

            switch (_database.Details.Source)
            {
                case "Demo":
                    lblSource.Text = _database.Details.Source + locCH;

                    txtURL.Visibility = Visibility.Collapsed;
                    txtURLUser.Visibility = Visibility.Collapsed;
                    txtURLPW.Visibility = Visibility.Collapsed;
                    txtDomain.Visibility = Visibility.Collapsed;
                    break;
                case "Web":
                    urlstr = _database.Details.Url;
                    lblSource.Text = _database.Details.Source + ", " + Strings.DBDetail_Updated + locCH;
                    string[] urlarr = urlstr.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    string[] domain = urlarr[0].Split(new string[] { "\":\"" }, StringSplitOptions.RemoveEmptyEntries);
                    string[] pwd = urlarr[1].Split(new string[] { "\":\"" }, StringSplitOptions.RemoveEmptyEntries);
                    string[] url = urlarr[2].Split(new string[] { "\":\"" }, StringSplitOptions.RemoveEmptyEntries);
                    string[] user = urlarr[3].Split(new string[] { "\":\"" }, StringSplitOptions.RemoveEmptyEntries);

                    txtURL.Text = url[1].Replace("\"", string.Empty);
                    _originalURL = txtURL.Text;

                    if (user.Length > 1)
                    {
                        txtURLUser.Text = user[1].Replace("\"", string.Empty).Replace("}", string.Empty);
                        _originalUser = txtURLUser.Text;
                    }
                    else
                    {
                        txtURLUser.Visibility = Visibility.Collapsed;
                    }

                    if (pwd.Length > 1)
                    {
                        txtURLPW.Password = pwd[1].Replace("\"", string.Empty);
                        _originalPW = txtURLPW.Password;
                    }
                    else
                    {
                        txtURLPW.Visibility = Visibility.Collapsed;
                    }

                    if (domain.Length > 1)
                    {
                        txtDomain.Text = domain[1].Replace("\"", string.Empty);
                        _originalDomain = txtDomain.Text;
                    }
                    else
                    {
                        txtDomain.Visibility = Visibility.Collapsed;
                    }
                    break;
                case "DropBox":
                    convertedDate = DateTime.Parse(_database.Details.Modified);
                    lblSource.Text = _database.Details.Source + ", " + convertedDate.ToLocalTime() + locCH;
                    txtURL.Text = _database.Details.Url;
                    _originalURL = _database.Details.Url;

                    txtURLUser.Visibility = Visibility.Collapsed;
                    txtURLPW.Visibility = Visibility.Collapsed;
                    txtDomain.Visibility = Visibility.Collapsed;
                    break;
                case "SkyDrive":
                    convertedDate = DateTime.Parse(_database.Details.Modified);
                    lblSource.Text = _database.Details.Source + ", " + convertedDate.ToLocalTime() + locCH;

                    txtURL.Visibility = Visibility.Collapsed;
                    txtURLUser.Visibility = Visibility.Collapsed;
                    txtURLPW.Visibility = Visibility.Collapsed;
                    txtDomain.Visibility = Visibility.Collapsed;
                    break;
                case "WebDAV":
                    convertedDate = DateTime.Parse(_database.Details.Modified);
                    //lblModified.Text = _database.Details.Modified;
                    lblSource.Text = _database.Details.Source + ", " + convertedDate.ToLocalTime() + locCH;
                    txtName.Text = _originalName;
                    string[] urlarr2 = urlstr.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
                    {
                        txtURL.Text = urlarr2[0];
                        _originalURL = txtURL.Text;

                        txtURLUser.Text = urlarr2[1];
                        _originalUser = txtURLUser.Text;

                        txtURLPW.Password = urlarr2[2];
                        _originalPW = txtURLPW.Password;
                    }
                    txtDomain.Visibility = Visibility.Collapsed;
                    break;
                default:
                    lblSource.Text = Strings.Download_LocalFile + locCH;

                    txtURL.Visibility = Visibility.Collapsed;
                    txtURLUser.Visibility = Visibility.Collapsed;
                    txtURLPW.Visibility = Visibility.Collapsed;
                    txtDomain.Visibility = Visibility.Collapsed;
                    return;
            }
        }
Ejemplo n.º 26
0
        private void SaveKeyFile(byte[] hash)
        {
            var info = new DatabaseInfo(_folder);

            info.SetKeyFile(hash);
            _page.Dispatcher.BeginInvoke(
                _page.BackToDBs);
        }
Ejemplo n.º 27
0
        private void SaveDatabase(
            WebRequest request, Stream stream)
        {
            stream.Position = 0;
            var info = new DatabaseInfo();

            var name = Path.GetFileName(
                request.RequestUri.ToString());
            var url = WebUtils.Serialize(request);

            info.SetDatabase(stream, new DatabaseDetails
            {
                Url = url,
                Name = name.RemoveKdbx(),
                Type = SourceTypes.Updatable,
                Source = DatabaseUpdater.WEB_UPDATER,
            });

            _page.Dispatcher.BeginInvoke(
                _page.BackToDBs);
        }
Ejemplo n.º 28
0
        public static void RestoreCache(Dispatcher dispatcher)
        {
            if (dispatcher == null)
                throw new ArgumentNullException("dispatcher");

            string name;
            if (!_appSettings.TryGetValue(KEY_DATABASE, out name) ||
                string.IsNullOrEmpty(name))
                return;

            var info = new DatabaseInfo(name);
            if (!info.HasPassword)
                return;

            info.Open(dispatcher);
        }
Ejemplo n.º 29
0
        private static void Upgrade(IsolatedStorageFile store)
        {
            var files = new List<string>(
                store.GetFileNames());

            if (!files.Contains("Database.kdbx"))
                return;

            var appSettings = IsolatedStorageSettings
                .ApplicationSettings;

            string url;
            if (!appSettings.TryGetValue("Url", out url))
                url = null;

            var info = new DatabaseInfo();

            using (var fs = store.OpenFile("Database.kdbx", FileMode.Open))
            {
                var source = string.IsNullOrEmpty(url)
                    ? "WinPass" : DatabaseUpdater.WEB_UPDATER;

                var details = new DatabaseDetails
                {
                    Url = url,
                    Source = source,
                    Type = SourceTypes.OneTime,
                    Name = "WinPass 1.x database",
                };

                info.SetDatabase(fs, details);
            }

            store.DeleteFile("Database.kdbx");
            store.DeleteFile("Protection.bin");
            store.DeleteFile("Decrypted.xml");
        }
Ejemplo n.º 30
0
 protected bool Equals(DatabaseInfo other)
 {
     return string.Equals(Folder, other.Folder);
 }