Exemplo n.º 1
0
        public void ImportBooksDrop(string[] paths)
        {
            if (paths.Length == 1 && Directory.Exists(paths[0]))
            {
                ImportDirectory(paths[0]);
                return;
            }
            else if (paths.Length == 1)
            {
                Task.Run(() =>
                {
                    if (!Formats.Resources.AcceptedFileTypes.Contains(Path.GetExtension(paths[0])))
                    {
                        var errDlg = new Dialogs.Error("Incompatible Format", $"Importing {Path.GetExtension(paths[0])} format books has not yet been implemented.");
                        _          = MaterialDesignThemes.Wpf.DialogHost.Show(errDlg);
                        return;
                    }
                    try
                    {
                        ImportBook(paths[0]);
                    }
                    catch (Exception e)
                    {
                        App.Current.Dispatcher.Invoke(() =>
                        {
                            var dlg = new Dialogs.Error("Import failed", e.Message);
                            _       = MaterialDesignThemes.Wpf.DialogHost.Show(dlg);
                        });
                        return;
                    }
                    finally
                    {
                        CloseBottomDrawer();
                    }
                    SnackBarQueue.Enqueue($"{Path.GetFileName(paths[0])} imported");
                });
            }
            else
            {
                List <Exception> errs = new List <Exception>();

                var prgDlg = new Dialogs.Progress("Importing Books", false);
                OpenBottomDrawer(prgDlg.Content);
                int step = 100 / paths.Length;

                Task.Run(() =>
                {
                    foreach (string path in paths)
                    {
                        prgDlg.Current  = $"Importing {Path.GetFileName(path)}";
                        prgDlg.Percent += step;
                        if (!Formats.Resources.AcceptedFileTypes.Contains(Path.GetExtension(path)))
                        {
                            Exception e    = new Exception($"Invalid filetype ${Path.GetExtension(path)}");
                            e.Data["item"] = path;
                            errs.Add(e);
                            continue;
                        }
                        try
                        {
                            ImportBook(path);
                        }
                        catch (Exception e)
                        {
                            e.Data["item"] = path;
                            errs.Add(e);
                            continue;
                        }
                    }

                    if (errs.Count > 0)
                    {
                        prgDlg.Finish("Import finished with errors:");
                        prgDlg.ShowError(new AggregateException(errs.ToArray()));
                    }
                    else
                    {
                        prgDlg.Close();
                        SnackBarQueue.Enqueue($"{paths.Length} new books imported successfully");
                    }
                });
            }
        }
Exemplo n.º 2
0
        public async void _EditMetadata()
        {
            if (SelectedTableRow == null)
            {
                return;
            }

            HashSet <string> authors    = new HashSet <string>();
            HashSet <string> series     = new HashSet <string>();
            HashSet <string> publishers = new HashSet <string>();

            foreach (Database.BookEntry i in CombinedLibrary)
            {
                authors.Add(i.Author);
                series.Add(i.Series);
                publishers.Add(i.Publisher);
            }

            authors.Remove("");
            series.Remove("");
            publishers.Remove("");

            var dlg = new Dialogs.MetadataEditor(new Database.BookEntry(SelectedTableRow), authors, series, publishers);
            await MaterialDesignThemes.Wpf.DialogHost.Show(dlg);

            if (dlg.DialogResult == false)
            {
                return;
            }

            List <Exception> errs = new List <Exception>();

            string title = null;

            if (CombinedLibrary.FirstOrDefault(x => x.IsLocal && x.Id == SelectedTableRow.Id) is Database.BookEntry bookEntry)
            {
                title = bookEntry.Title;
                try
                {
                    App.LocalLibrary.UpdateBookMetadata(dlg.ModBook);
                }
                catch (Exception e)
                {
                    errs.Add(e);
                }
            }

            if (SelectedDevice != null)
            {
                if (CombinedLibrary.FirstOrDefault(x => x.IsRemote && x.Id == SelectedTableRow.Id) is Database.BookEntry deviceBook)
                {
                    if (title == null)
                    {
                        title = deviceBook.Title;
                    }
                    try
                    {
                        SelectedDevice.UpdateBookMetadata(dlg.ModBook);
                    }
                    catch (Exception e)
                    {
                        errs.Add(e);
                    }
                }
            }


            if (errs.Count != 0)
            {
                string msg    = $"Metadata could not be updated. {string.Join("; ", errs.Select(x => x.Message).ToList())}";
                var    errDlg = new Dialogs.Error("Error updating metadata", msg);
                _ = MaterialDesignThemes.Wpf.DialogHost.Show(errDlg);
            }
            else if (title != null)
            {
                SnackBarQueue.Enqueue($"{title} updated");
            }
        }
Exemplo n.º 3
0
        public async void _RemoveBook()
        {
            if (SelectedTableRow == null)
            {
                return;
            }
            Database.BookEntry book = SelectedTableRow;

            bool onDevice = SelectedDevice == null ? false : CombinedLibrary.Any(x => x.IsRemote && x.Id == book.Id);
            bool onPC     = CombinedLibrary.Any(x => x.IsLocal && x.Id == book.Id);

            var dlg = new Dialogs.DeleteConfirm(book.Title, onDevice, onPC);
            await MaterialDesignThemes.Wpf.DialogHost.Show(dlg);

            if (dlg.DialogResult == false)
            {
                return;
            }
            if (dlg.DeleteFrom == 0 || dlg.DeleteFrom == 1) // device
            {
                Database.BookEntry remoteBook = SelectedDevice.Database.BOOKS.FirstOrDefault(x => x.Id == book.Id);
                try
                {
                    SelectedDevice.DeleteBook(book.Id);
                }
                catch (Exception e)
                {
                    var errDlg = new Dialogs.Error($"Unable to delete book from {SelectedDevice.Name}", e.Message);
                    await MaterialDesignThemes.Wpf.DialogHost.Show(errDlg);

                    return;
                }
            }
            if (dlg.DeleteFrom == 0 || dlg.DeleteFrom == 2) // pc
            {
                Database.BookEntry localBook = CombinedLibrary.FirstOrDefault(x => x.IsLocal && x.Id == book.Id);
                try
                {
                    if (localBook != null)
                    {
                        try
                        {
                            App.LocalLibrary.DeleteBook(localBook);
                        }
                        catch (FileNotFoundException) { }
                        catch (DirectoryNotFoundException) { }

                        Utils.Files.CleanBackward(Path.GetDirectoryName(localBook.FilePath), App.LibraryDirectory);
                    }
                    App.LocalLibrary.Database.RemoveBook(book);
                }
                catch (Exception e)
                {
                    var errDlg = new Dialogs.Error("Unable to delete book from library", e.Message);
                    await MaterialDesignThemes.Wpf.DialogHost.Show(errDlg);

                    return;
                }
            }

            string msg = dlg.DeleteFrom == 0 ? "PC & Kindle" : (dlg.DeleteFrom == 2 ? "PC" : "Kindle");

            SnackBarQueue.Enqueue($"{book.Title} deleted from {msg}.");
        }
Exemplo n.º 4
0
        public Unit _SendBook(IList bookList)
        {
            if (SelectedDevice == null)
            {
                var errDlg = new Dialogs.Error("No Kindle Selected", "Connect to Kindle Before Transferring Books");
                MaterialDesignThemes.Wpf.DialogHost.Show(errDlg);
                return(Unit.Default);
            }

            Task.Run(() =>
            {
                if (bookList.Count == 1)
                {
                    try
                    {
                        BookBase book = (BookBase)bookList[0];
                        book.FilePath = App.LocalLibrary.AbsoluteFilePath(book);
                        SelectedDevice.ImportBook(book);
                        SnackBarQueue.Enqueue($"{book.Title} transferred to {SelectedDevice.Name}");
                    }
                    catch (Exception e)
                    {
                        var dlg = new Dialogs.Error("Error transferring book", e.Message);
                    }
                }
                else
                {
                    List <Exception> errs = new List <Exception>();

                    var prgDlg = new Dialogs.Progress("Syncing Library", false);
                    OpenBottomDrawer(prgDlg.Content);

                    int step = 100 / bookList.Count;

                    foreach (Database.BookEntry book in bookList)
                    {
                        try
                        {
                            book.FilePath = App.LocalLibrary.AbsoluteFilePath(book);
                            SelectedDevice.ImportBook(book);
                            prgDlg.Current = $"Transferred {book.Title}";
                        }
                        catch (Exception e)
                        {
                            e.Data["item"] = book.Title;
                            errs.Add(e);
                        }
                        finally
                        {
                            prgDlg.Percent += step;
                        }
                    }

                    if (errs.Count > 0)
                    {
                        prgDlg.Finish("Book transfer finished with errors:");
                        prgDlg.ShowError(new AggregateException(errs.ToArray()));
                    }
                    else
                    {
                        prgDlg.Close();
                        SnackBarQueue.Enqueue("Book transfer finished");
                    }
                }
            });
            return(Unit.Default);
        }
Exemplo n.º 5
0
        public async void _SyncDeviceLibrary()
        {
            if (SelectedDevice == null)
            {
                var errDlg = new Dialogs.Error("No Kindle Selected", "Connect to Kindle Before Transferring Books");
                await MaterialDesignThemes.Wpf.DialogHost.Show(errDlg);

                return;
            }

            List <Database.BookEntry> toTransfer = new List <Database.BookEntry>();

            foreach (Database.BookEntry book in App.LocalLibrary.Database.BOOKS)
            {
                if (!SelectedDevice.Database.BOOKS.Any(x => x.Id == book.Id))
                {
                    toTransfer.Add(book);
                }
            }

            var dlg = new Dialogs.SyncConfirm(toTransfer, SelectedDevice.Name);
            await MaterialDesignThemes.Wpf.DialogHost.Show(dlg);

            if (dlg.DialogResult == false)
            {
                return;
            }

            var a = dlg.UserSelectedBooks;

            foreach (var b in dlg.UserSelectedBooks)
            {
                if (!b.Checked)
                {
                    Database.BookEntry t = toTransfer.FirstOrDefault(x => x.Id == b.Id);
                    if (t != null)
                    {
                        toTransfer.Remove(t);
                    }
                }
            }

            var prgDlg = new Dialogs.Progress("Syncing Kindle Library", false);

            OpenBottomDrawer(prgDlg.Content);

            _ = Task.Run(() =>
            {
                List <Exception> errs = new List <Exception>();
                int step = 100 / toTransfer.Count;
                for (int i = 0; i < toTransfer.Count; i++)
                {
                    Database.BookEntry book = toTransfer[i];
                    try
                    {
                        prgDlg.Current  = $"Copying {book.Title}";
                        prgDlg.Percent += step;
                        book.FilePath   = App.LocalLibrary.AbsoluteFilePath(book);
                        SelectedDevice.ImportBook(book);
                    }
                    catch (Exception e)
                    {
                        e.Data.Add("item", book.Title);
                        errs.Add(e);
                    }
                }

                if (errs.Count > 0)
                {
                    prgDlg.Finish("Library sync finished with errors:");
                    prgDlg.ShowError(new AggregateException(errs.ToArray()));
                }
                else
                {
                    prgDlg.Close();
                    SnackBarQueue.Enqueue($"{SelectedDevice.Name} library synced");
                }
            });
        }
Exemplo n.º 6
0
        public Unit _ReceiveBook(IList bookList)
        {
            if (bookList.Count == 0)
            {
                return(Unit.Default);
            }

            Database.BookEntry[] dbRows = new Database.BookEntry[bookList.Count];
            bookList.CopyTo(dbRows, 0);

            Task.Run(() =>
            {
                if (bookList.Count == 1)
                {
                    Database.BookEntry book = (Database.BookEntry)bookList[0];
                    try
                    {
                        book.FilePath = SelectedDevice.AbsoluteFilePath(book);
                        ImportBook(book);
                    }
                    catch (Exception e)
                    {
                        App.Current.Dispatcher.Invoke(() =>
                        {
                            var dlg = new Dialogs.Error("Error transferring book", e.Message);
                            MaterialDesignThemes.Wpf.DialogHost.Show(dlg);
                        });
                        return;
                    }
                    SnackBarQueue.Enqueue($"{book.Title} copied to library.");
                }
                else
                {
                    List <Exception> errs = new List <Exception>();

                    var prgDlg = new Dialogs.Progress("Syncing Library", false);
                    OpenBottomDrawer(prgDlg.Content);

                    int step = 100 / bookList.Count;

                    foreach (Database.BookEntry b in bookList)
                    {
                        Database.BookEntry book = new Database.BookEntry(b);
                        try
                        {
                            book.FilePath = SelectedDevice.AbsoluteFilePath(book);
                            ImportBook(book);
                        }
                        catch (Exception e)
                        {
                            e.Data["item"] = book.Title;
                            errs.Add(e);
                        }
                    }

                    if (errs.Count > 0)
                    {
                        prgDlg.Finish("Book transfer finished with errors:");
                        prgDlg.ShowError(new AggregateException(errs.ToArray()));
                    }
                    else
                    {
                        prgDlg.Close();
                        SnackBarQueue.Enqueue("Book transfer finished");
                    }
                }
            });

            return(Unit.Default);
        }