public static SyncJobRequest CreateRequest(List<string> itemIds, string name = null)
        {
            var request = new SyncJobRequest
            {
                ItemIds = itemIds,
                UserId = AuthenticationService.Current.LoggedInUserId,
                Name = !string.IsNullOrEmpty(name) ? name : Guid.NewGuid().ToString(),
            };

            return request;
        }
        public async Task<SyncJobCreationResult> CreateJob(SyncJobRequest request)
        {
            var items = GetItemsForSync(request.ItemIds).ToList();

            if (items.Count == 1)
            {
                request.Name = GetDefaultName(items[0]);
            }

            if (string.IsNullOrWhiteSpace(request.Name))
            {
                throw new ArgumentException("Please supply a name for the sync job.");
            }

            var target = GetSyncTargets(request.UserId)
                .First(i => string.Equals(request.TargetId, i.Id));

            var jobId = Guid.NewGuid().ToString("N");

            var job = new SyncJob
            {
                Id = jobId,
                Name = request.Name,
                TargetId = target.Id,
                UserId = request.UserId,
                UnwatchedOnly = request.UnwatchedOnly,
                Limit = request.Limit,
                LimitType = request.LimitType,
                RequestedItemIds = request.ItemIds,
                DateCreated = DateTime.UtcNow,
                DateLastModified = DateTime.UtcNow,
                ItemCount = 1
            };

            await _repo.Create(job).ConfigureAwait(false);

            return new SyncJobCreationResult
            {
                Job = GetJob(jobId)
            };
        }
        public async Task<SyncOption> RequestSyncOption(SyncJobRequest request)
        {
            var tcs = new TaskCompletionSource<SyncOption>();

            var optionsControl = new SyncOptionsControl();
            var prompt = new MessagePrompt
            {
                Body = optionsControl
            };

            optionsControl.MessagePrompt = prompt;
            optionsControl.SetOptions(request);

            prompt.Completed += (sender, args) =>
            {
                if (args.PopUpResult == PopUpResult.Ok)
                {
                    var item = optionsControl.GetSelectedOption();
                    tcs.SetResult(item);
                }
                else
                {
                    tcs.SetCanceled();
                }
            };

            prompt.Show();

            try
            {
                var qualityResult = await tcs.Task;

                return qualityResult;
            }
            catch (TaskCanceledException)
            {
                return null;
            }
        }
Exemple #4
0
        public async Task<SyncJobCreationResult> CreateJob(SyncJobRequest request)
        {
            var processor = GetSyncJobProcessor();

            var user = _userManager.GetUserById(request.UserId);

            var items = (await processor
                .GetItemsForSync(request.Category, request.ParentId, request.ItemIds, user, request.UnwatchedOnly).ConfigureAwait(false))
                .ToList();

            if (items.Any(i => !SupportsSync(i)))
            {
                throw new ArgumentException("Item does not support sync.");
            }

            if (string.IsNullOrWhiteSpace(request.Name))
            {
                if (request.ItemIds.Count == 1)
                {
                    request.Name = GetDefaultName(_libraryManager.GetItemById(request.ItemIds[0]));
                }
            }

            if (string.IsNullOrWhiteSpace(request.Name))
            {
                request.Name = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString();
            }

            var target = GetSyncTargets(request.UserId)
                .FirstOrDefault(i => string.Equals(request.TargetId, i.Id));

            if (target == null)
            {
                throw new ArgumentException("Sync target not found.");
            }

            var jobId = Guid.NewGuid().ToString("N");

            if (string.IsNullOrWhiteSpace(request.Quality))
            {
                request.Quality = GetQualityOptions(request.TargetId)
                    .Where(i => i.IsDefault)
                    .Select(i => i.Id)
                    .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
            }

            var job = new SyncJob
            {
                Id = jobId,
                Name = request.Name,
                TargetId = target.Id,
                UserId = request.UserId,
                UnwatchedOnly = request.UnwatchedOnly,
                ItemLimit = request.ItemLimit,
                RequestedItemIds = request.ItemIds ?? new List<string> { },
                DateCreated = DateTime.UtcNow,
                DateLastModified = DateTime.UtcNow,
                SyncNewContent = request.SyncNewContent,
                ItemCount = items.Count,
                Category = request.Category,
                ParentId = request.ParentId,
                Quality = request.Quality,
                Profile = request.Profile,
                Bitrate = request.Bitrate
            };

            if (!request.Category.HasValue && request.ItemIds != null)
            {
                var requestedItems = request.ItemIds
                    .Select(_libraryManager.GetItemById)
                    .Where(i => i != null);

                // It's just a static list
                if (!requestedItems.Any(i => i.IsFolder || i is IItemByName))
                {
                    job.SyncNewContent = false;
                }
            }

            await _repo.Create(job).ConfigureAwait(false);

            await processor.EnsureJobItems(job).ConfigureAwait(false);

            // If it already has a converting status then is must have been aborted during conversion
            var jobItemsResult = GetJobItems(new SyncJobItemQuery
            {
                Statuses = new[] { SyncJobItemStatus.Queued, SyncJobItemStatus.Converting },
                JobId = jobId,
                AddMetadata = false
            });

            await processor.SyncJobItems(jobItemsResult.Items, false, new Progress<double>(), CancellationToken.None)
                    .ConfigureAwait(false);

            jobItemsResult = GetJobItems(new SyncJobItemQuery
            {
                Statuses = new[] { SyncJobItemStatus.Queued, SyncJobItemStatus.Converting },
                JobId = jobId,
                AddMetadata = false
            });

            var returnResult = new SyncJobCreationResult
            {
                Job = GetJob(jobId),
                JobItems = jobItemsResult.Items.ToList()
            };

            if (SyncJobCreated != null)
            {
                EventHelper.FireEventIfNotNull(SyncJobCreated, this, new GenericEventArgs<SyncJobCreationResult>
                {
                    Argument = returnResult

                }, _logger);
            }

            if (returnResult.JobItems.Any(i => i.Status == SyncJobItemStatus.Queued || i.Status == SyncJobItemStatus.Converting))
            {
                _taskManager.QueueScheduledTask<SyncConvertScheduledTask>();
            }

            return returnResult;
        }
        public async Task AddJobAsync(SyncJobRequest request)
        {
            var apiClient = _connectionManager.GetApiClient(_serverInfo.ServerInfo.Id);
            var qualityOptions = await _messagePrompt.RequestSyncOption(request);

            if (qualityOptions != null)
            {
                _logger.Info("Quality requested for {0} is {1}", request.Name, qualityOptions.Quality.Name);
                request.Quality = qualityOptions.Quality.Name;
                request.ItemLimit = qualityOptions.ItemLimit;
                request.SyncNewContent = qualityOptions.AutoSyncNewItems;
                request.UnwatchedOnly = qualityOptions.UnwatchedItems;
                request.Profile = qualityOptions.Profile != null ? qualityOptions.Profile.Id : string.Empty;

                _logger.Info("Create sync job");
                var job = await apiClient.CreateSyncJob(request);
                if (job != null)
                {
                    _messagePrompt.ShowMessage(AppResources.MessageSyncJobCreated);
                    _logger.Info("Job created, start sync request");
                    await Sync().ConfigureAwait(false);
                }
            }
            else
            {
                _logger.Info("No quality given by the user, most likely dismissed (back button)");
            }
        }
        public async Task SetOptions(SyncJobRequest request)
        {
            IsLoading = true;
            _request = request;
            var apiClient = _connectionManager.GetApiClient(App.ServerInfo.Id);

            try
            {
                _logger.Info("Getting sync options");
                _options = await apiClient.GetSyncOptions(request);
                if (_options != null && _options.Targets.Any())
                {
                    var thisDevice = _options.Targets.FirstOrDefault(x => x.Id == apiClient.DeviceId);
                    if (thisDevice == null)
                    {
                        TargetDevices.ItemsSource = _options.Targets;
                        TargetDevices.SelectedItem = _options.Targets.FirstOrDefault();
                    }
                    else
                    {
                        thisDevice.Name = string.Format(AppResources.LabelThisDevice, thisDevice.Name);
                        _options.Targets.Remove(thisDevice);
                        _options.Targets.Insert(0, thisDevice);

                        TargetDevices.ItemsSource = _options.Targets;
                        TargetDevices.SelectedItem = thisDevice;
                        //GetOptionsForDevice(thisDevice.Id).ConfigureAwait(false);
                    }
                }
                else
                {
                    Close();
                }
            }
            catch (HttpException ex)
            {
                Close();
                Utils.HandleHttpException("SetOptions()", ex, SimpleIoc.Default.GetInstance<INavigationService>(), _logger);
            }

            IsLoading = false;
        }
Exemple #7
0
        public async Task<SyncJobCreationResult> CreateJob(SyncJobRequest request)
        {
            var processor = new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager);

            var user = _userManager.GetUserById(request.UserId);

            var items = processor
                .GetItemsForSync(request.ItemIds, user, request.UnwatchedOnly)
                .ToList();

            if (items.Any(i => !SupportsSync(i)))
            {
                throw new ArgumentException("Item does not support sync.");
            }

            if (string.IsNullOrWhiteSpace(request.Name) && request.ItemIds.Count == 1)
            {
                request.Name = GetDefaultName(_libraryManager.GetItemById(request.ItemIds[0]));
            }

            if (string.IsNullOrWhiteSpace(request.Name))
            {
                throw new ArgumentException("Please supply a name for the sync job.");
            }

            var target = GetSyncTargets(request.UserId)
                .First(i => string.Equals(request.TargetId, i.Id));

            var jobId = Guid.NewGuid().ToString("N");

            var job = new SyncJob
            {
                Id = jobId,
                Name = request.Name,
                TargetId = target.Id,
                UserId = request.UserId,
                UnwatchedOnly = request.UnwatchedOnly,
                ItemLimit = request.ItemLimit,
                RequestedItemIds = request.ItemIds,
                DateCreated = DateTime.UtcNow,
                DateLastModified = DateTime.UtcNow,
                SyncNewContent = request.SyncNewContent,
                ItemCount = items.Count,
                Quality = request.Quality,
                Category = request.Category,
                ParentId = request.ParentId
            };

            // It's just a static list
            if (!items.Any(i => i.IsFolder || i is IItemByName))
            {
                job.SyncNewContent = false;
            }

            await _repo.Create(job).ConfigureAwait(false);

            await processor.EnsureJobItems(job).ConfigureAwait(false);

            return new SyncJobCreationResult
            {
                Job = GetJob(jobId)
            };
        }