private void BookDownloaderOnDownloadCompleted(object sender, DownloadItemDataModel downloadItemDataModel)
        {
            _eventAggregator.Publish(new BookDownloaded(downloadItemDataModel));


            var catalogBookItemModel = new CatalogBookItemModel();
            catalogBookItemModel.Id = downloadItemDataModel.CatalogItemId;
            catalogBookItemModel.Title = downloadItemDataModel.Name;
            catalogBookItemModel.Author = downloadItemDataModel.Author;
            catalogBookItemModel.Description = downloadItemDataModel.Description;
            if (!string.IsNullOrWhiteSpace(downloadItemDataModel.AcquisitionUrl))
            {
                catalogBookItemModel.AcquisitionLink = new BookAcquisitionLinkModel
                                                           {
                                                               Url = downloadItemDataModel.AcquisitionUrl,
                                                               Type = downloadItemDataModel.AcquisitionType,
                                                               Prices = BookAcquisitionLinkModel.ParsePrices(downloadItemDataModel.AcquisitionPrices)
                                                           };
            }
            if (downloadItemDataModel.IsTrial)
            {
                catalogBookItemModel.TrialLink = new BookDownloadLinkModel
                                                     {
                                                         Url = downloadItemDataModel.Path,
                                                         Type = downloadItemDataModel.Type
                                                     };
            }

            _notificationsService.ShowToast(UIStrings.BookDownloader_Success, downloadItemDataModel.Name, _navigationService.UriFor<ReadPageViewModel>()
                                                                      .WithParam(vm => vm.BookId, downloadItemDataModel.BookID.ToString())
                                                                      .WithParam(vm => vm.CatalogId, downloadItemDataModel.DataSourceID)
                                                                      .WithParam(vm => vm.CatalogBookItemKey, TransientStorage.Put(catalogBookItemModel))
                                                                      .WithParam(vm => vm.TokenOffset, 0).BuildUri());

        }
        public void Enqueue(DownloadItemDataModel dataModel)
        {
            lock (SyncObject)
            {
                if (GetDataModelIndex(dataModel) >= 0)
                {
                    return;
                }

                _items.Add(dataModel);
                Count = _items.Count;
            }
        }
 public int GetDataModelIndex(DownloadItemDataModel data)
 {
     for (var i = 0; i < _items.Count; i++)
     {
         var model = _items[i];
         if (((model.DataSourceID == data.DataSourceID) && string.Equals(model.Path, data.Path, StringComparison.CurrentCultureIgnoreCase)) &&
             string.Equals(model.Name, data.Name, StringComparison.CurrentCultureIgnoreCase))
         {
             return i;
         }
     }
     return -1;
 }
Example #4
0
 public int GetDataModelIndex(DownloadItemDataModel data)
 {
     for (var i = 0; i < _items.Count; i++)
     {
         var model = _items[i];
         if (((model.DataSourceID == data.DataSourceID) && string.Equals(model.Path, data.Path, StringComparison.CurrentCultureIgnoreCase)) &&
             string.Equals(model.Name, data.Name, StringComparison.CurrentCultureIgnoreCase))
         {
             return(i);
         }
     }
     return(-1);
 }
Example #5
0
        public void Enqueue(DownloadItemDataModel dataModel)
        {
            lock (SyncObject)
            {
                if (GetDataModelIndex(dataModel) >= 0)
                {
                    return;
                }

                _items.Add(dataModel);
                Count = _items.Count;
            }
        }
Example #6
0
        public void Remove(DownloadItemDataModel dataModel)
        {
            lock (SyncObject)
            {
                int index = GetDataModelIndex(dataModel);
                if (index < 0)
                {
                    return;
                }

                _items.RemoveAt(index);
                Count = _items.Count;
            }
        }
        public void Remove(DownloadItemDataModel dataModel)
        {
            lock (SyncObject)
            {
                int index = GetDataModelIndex(dataModel);
                if (index < 0)
                {
                    return;
                }

                _items.RemoveAt(index);
                Count = _items.Count;
            }
        }
 public void Remove(DownloadItemDataModel item)
 {
     _bookDownloadsRepository.Remove(item.DownloadID);
 }
 private void OnDownloadCompleted(DownloadItemDataModel book)
 {
     var handler = DownloadCompleted;
     if (handler != null)
     {
         handler(this, book);
     }
 }
Example #10
0
 private void OnDownloadError(DownloadItemDataModel book)
 {
     var handler = DownloadError;
     if (handler != null)
     {
         handler(this, book);
     }
 }
Example #11
0
 private static string CreateBookPath(DownloadItemDataModel item)
 {
     return Path.Combine(item.BookID.ToString(), ModelConstants.BOOK_FILE_DATA_PATH);
 }
Example #12
0
 private static string CreateImagesPath(DownloadItemDataModel item)
 {
     return Path.Combine(item.BookID.ToString(), ModelConstants.BOOK_IMAGES_FILE_NAME);
 }
Example #13
0
 public BookDownloaded(DownloadItemDataModel book)
 {
     Book = book;
 }
Example #14
0
        private bool ProcessBook(DownloadItemDataModel item)
        {
            if (item.Canceled)
            {
                return false;
            }
            
            if (item.Status == DownloadStatus.Error)
            {
                return false;
            }

            if (item.Status == DownloadStatus.Pending)
            {
                SetItemStatus(item, DownloadStatus.Downloading);
                return ProcessBook(item);
            }

            if (item.Status == DownloadStatus.Downloading)
            {
                DownloadBook(item);
                return ProcessBook(item);
            }

            if (item.Status == DownloadStatus.Parsing)
            {
                ParseBook(item);
                return ProcessBook(item);
            }

            return item.Status == DownloadStatus.Completed;
        }
Example #15
0
        private void DeleteUnnecessaryInfo(DownloadItemDataModel item, BookSummary bookSummary)
        {
            _bookService.Remove(item.BookID.ToString());
            if (string.IsNullOrEmpty(bookSummary.UniqueID))
            {
                return;
            }

            var bookModel = _bookService.GetBookByUniqueId(bookSummary.UniqueID);
            if (bookModel == null)
            {
                return;
            }

            try
            {
                item.BookID = Guid.Parse(bookModel.BookID);
            }
            catch (Exception)
            {
            }
        }
Example #16
0
        private static void SetItemStatus(DownloadItemDataModel item, DownloadStatus status)
        {
            var wait = new AutoResetEvent(false);

            Execute.OnUIThread(() =>
                {
                    item.Status = status;
                    wait.Set();
                });
            wait.WaitOne();
        }
Example #17
0
        private void SaveBook(DownloadItemDataModel item, BookSummary bookSummary, IBookSummaryParser previewGenerator, IsolatedStorageFile storeForApplication)
        {
            using (var imageStorageFileStream = new IsolatedStorageFileStream(CreateImagesPath(item), FileMode.Create, storeForApplication))
            {
                previewGenerator.SaveImages(imageStorageFileStream);
            }

            previewGenerator.SaveCover(item.BookID.ToString());

            var book = CreateBook(item, bookSummary);

            try
            {
                _bookService.Add(book);
                TokensTool.SaveTokens(book, previewGenerator);
                book.Hidden = book.Trial;
                _bookService.Save(book);
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
            catch (Exception)
            {
                _bookService.Remove(book.BookID);
                throw;
            }
        }
Example #18
0
        private void BookDownloaderOnDownloadError(object sender, DownloadItemDataModel downloadItemDataModel)
        {
            _eventAggregator.Publish(new BookDownloaded(downloadItemDataModel));

            _notificationsService.ShowToast(
                UIStrings.BookDownloader_Error,
                downloadItemDataModel.Name, 
                _navigationService.UriFor<DownloadListPageViewModel>().BuildUri());
        }
Example #19
0
        private void ParseBook(DownloadItemDataModel item)
        {
            try
            {
                if (item.BookID == Guid.Empty)
                {
                    throw new Exception(BOOK_HAS_NO_ID_MESSAGE);
                }

                using (var storeForApplication = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (var bookStorageFileStream = new IsolatedStorageFileStream(CreateBookPath(item), FileMode.Open, storeForApplication))
                    {
                        var previewGenerator = BookFactory.GetPreviewGenerator(item.Type, item.Name, bookStorageFileStream);
                        var bookSummary = previewGenerator.GetBookPreview();

                        var trialBook = _bookService.GetTrials().SingleOrDefault(t => t.CatalogItemId == item.CatalogItemId);
                        if (trialBook != null)
                        {
                            _bookService.Remove(trialBook.BookID);
                        }

                        if (string.IsNullOrEmpty(bookSummary.UniqueID) || _bookService.CheckUniqueId(bookSummary.UniqueID))
                        {
                            SaveBook(item, bookSummary, previewGenerator, storeForApplication);
                        }
                        else
                        {
                            DeleteUnnecessaryInfo(item, bookSummary);
                        }

                        SetItemStatus(item, DownloadStatus.Completed);
                    }
                }
            }
            catch (SharpZipBaseException)
            {
                SetItemStatus(item, DownloadStatus.Error);
            }
            catch (Exception)
            {
                SetItemStatus(item, DownloadStatus.Error);
            }
        }
Example #20
0
        private static void HandleDownloadException(DownloadItemDataModel item, Exception exception)
        {
            if (exception is ThreadAbortException)
            {
                throw exception;
            }

            SetItemStatus(item, DownloadStatus.Error);
        }
Example #21
0
        private void DownloadBook(DownloadItemDataModel item)
        {
            try
            {
                using (var isostore = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    for (;;)
                    {
                        var fileLoader = _fileLoadingFactory.GetFileLoader(item.DataSourceID, item.IsTrial);
                        if (fileLoader == null)
                        {
                            throw new Exception(DATA_SOURCE_NOT_FOUND_MESSAGE);
                        }
                        try
                        {
                            var file = fileLoader.LoadFile(item.Path, item.IsZip);

                            SaveToFile(PrepareFilePath(item, isostore), file);
                            SetItemStatus(item, DownloadStatus.Parsing);
                            break;
                        }
                        catch (RestartException)
                        {
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                HandleDownloadException(item, exception);
            }
        }
 public void Restart(DownloadItemDataModel item)
 {
     item.Status = DownloadStatus.Pending;
 }
Example #23
0
 private static void RemoveItem(DownloadItemDataModel item, IDownloadsContainer downloads)
 {
     var wait = new AutoResetEvent(false);
     Execute.OnUIThread((() =>
                                 {
                                     downloads.Remove(item);
                                     wait.Set();
                                 }));
     wait.WaitOne();
 }
Example #24
0
        public bool DownloadBook(CatalogBookItemModel catalogBookItemModel, int catalogId, bool fullBook = true)
        {
            var link = fullBook
                           ? GetDownloadLink(catalogBookItemModel.Links.ToArray())
                           : GetDownloadLink(catalogBookItemModel.TrialLink);
            
            if (link == null)
                return false;

            if (!_bookDownloader.IsStarted)
            {
                _bookDownloader.Start();
            }

            var downloadModel = new BookDownloadModel
            {
                Path = link.Url,
                Type = GetBookType(link.Type),

                IsZip = CheckIsZip(link.Type),
                Name = catalogBookItemModel.Title,
                Author = catalogBookItemModel.Author,
                Description = catalogBookItemModel.Description,
                AcquisitionUrl = catalogBookItemModel.AcquisitionLink != null ? catalogBookItemModel.AcquisitionLink.Url : null,
                AcquisitionType = catalogBookItemModel.AcquisitionLink != null ? catalogBookItemModel.AcquisitionLink.Type : null,
                AcquisitionPrices = catalogBookItemModel.AcquisitionLink != null ? catalogBookItemModel.AcquisitionLink.ToString() : null,
                DataSourceID = catalogId,
                IsTrial = !fullBook,
                CatalogItemId = catalogBookItemModel.Id
            };

            int oldQueueCount = _container.Count;
            _bookDownloadsRepository.Add(downloadModel);

            var downloadViewModel = new DownloadItemDataModel(downloadModel);
            _container.Enqueue(downloadViewModel);

            if (_container.Count == oldQueueCount)
            {
                int index = _container.GetDataModelIndex(downloadViewModel);
                if (index > -1)
                {
                    var viewModel = _container.Items[index];
                    if (viewModel.Status == DownloadStatus.Error)
                    {
                        viewModel.Status = DownloadStatus.Pending;
                        _bookDownloadsRepository.Remove(downloadModel.DownloadID);
                    }
                }
            }
            return true;
        }
Example #25
0
        private static string PrepareFilePath(DownloadItemDataModel item, IsolatedStorageFile storage)
        {
            if (item.BookID == Guid.Empty || !storage.DirectoryExists(item.BookID.ToString()))
            {
                item.BookID = CreateFolder(storage);
            }

            var bookPath = CreateBookPath(item);
            if (storage.FileExists(bookPath))
            {
                storage.DeleteFile(bookPath);
            }

            return bookPath;
        }
Example #26
0
        private static BookModel CreateBook(DownloadItemDataModel item, BookSummary bookSummary)
        {
            var book = new BookModel
                {
                    BookID = item.BookID.ToString(),
                    Title = bookSummary.Title.SafeSubstring(1024),
                    Author = bookSummary.AuthorName.SafeSubstring(1024),
                    Type = item.Type,
                    Hidden = true,
                    Trial = item.IsTrial,
                    Deleted = false,
                    CreatedDate = DateTime.Now.ToFileTimeUtc(),
                    UniqueID = bookSummary.UniqueID.SafeSubstring(1024),
                    Description = bookSummary.Description,
                    Language = bookSummary.Language,
                    Url = item.Path,
                    CatalogItemId = item.CatalogItemId
                };

            return book;
        }
Example #27
0
        private void HandleBookDownloading(DownloadItemDataModel downloadItemDataModel, IDownloadsContainer instance)
        {
            if (ProcessBook(downloadItemDataModel))
            {
                RemoveItem(downloadItemDataModel, instance);
                _bookDownloadsRepository.Remove(downloadItemDataModel.DownloadID);

                OnDownloadCompleted(downloadItemDataModel);
            }
            else if (downloadItemDataModel.Status == DownloadStatus.Error)
            {
                OnDownloadError(downloadItemDataModel);
            }
        }