示例#1
0
        public MainWindowViewModel()
        {
            Mapper.CreateMap<Task, TaskViewModel>();
            NameValueCollection appSettings = ConfigurationManager.AppSettings;
            DownloadStation = new DownloadStation(new Uri(appSettings["host"]), appSettings["username"], appSettings["password"], CreateProxy(appSettings["proxy"]));

            if (DownloadStation.Login())
            {
                var listResult = DownloadStation.List(String.Join(",", new []{ "detail", "transfer", "file", "tracker" }));
                if (listResult.Success)
                {
                    var taskList = (from task in listResult.Data.Tasks orderby task.Additional.Detail.CreateTime select Mapper.Map<TaskViewModel>(task)).ToList();
                    _allTasks = new ObservableCollection<TaskViewModel>(taskList);
                    var statusList = (new List<TaskStatusViewModel>() {new TaskStatusViewModel("all")}).Concat(
                        taskList.Select(t => t.Status).Distinct().OrderBy(s => s).Select(s => new TaskStatusViewModel(s)));
                    _statuses = new ObservableCollection<TaskStatusViewModel>(statusList);
                    _tasksByStatus = new Dictionary<string, ObservableCollection<TaskViewModel>>();
                    foreach (var taskStatus in _statuses)
                    {
                        if (taskStatus.Title == "all")
                        {
                            _tasksByStatus.Add(taskStatus.Title, _allTasks);
                            continue;
                        }
                        var tasks = new ObservableCollection<TaskViewModel>(_allTasks.Where(t => t.Status == taskStatus.Title).OrderBy(t => t.Additional.Detail.CreateTime));
                        _tasksByStatus.Add(taskStatus.Title, tasks);
                    }

                }
                OnPropertyChanged("CurrentTasks");
                OnPropertyChanged("Statuses");
                
                DownloadStation.Logout();

            }
        }
示例#2
0
        static void Main(string[] args)
        {
            string invokedVerb = "";
            object invokedVerbInstance = null;
            var options = new Options();
            if (!Parser.Default.ParseArguments(args, options,
                                        (verb, subOptions) =>
                                        {
                                            invokedVerb = verb;
                                            invokedVerbInstance = subOptions;
                                        }))
            {
                Console.ReadLine();
                Environment.Exit(Parser.DefaultExitCodeFail);
            }

            NameValueCollection appSettings = ConfigurationManager.AppSettings;
            var ds = new DownloadStation(new Uri(appSettings["host"]), appSettings["username"], appSettings["password"], CreateProxy(appSettings["proxy"]));
            // todo: this is ugly, needs refactoring
            switch(invokedVerb)
            {
                case("list"):
                    var listOptions = (ListOptions)invokedVerbInstance;
                    if (ds.Login())
                    {
                        var listResult = ds.List(String.Join(",", listOptions.Details));
                        if (listResult.Success)
                        {
                            var taskList = from task in listResult.Data.Tasks select task;
                            if (listOptions.Status.Any())
                            {
                                var statusesToList = new List<string>(listOptions.Status);
                                taskList = from task in taskList where statusesToList.Contains(task.Status) select task;
                            }
                            foreach (var task in taskList)
                            {
                                Console.WriteLine(ObjectUtils.HumanReadable(task));
                                Console.WriteLine();
                            }
                        }
                        ds.Logout();
                    }
                    break;

                case ("task"):
                    var taskOptions = (TaskOptions)invokedVerbInstance;
                    if (ds.Login())
                    {
                        var taskResult = ds.GetTasks(taskOptions.Id, taskOptions.Details);
                        if (taskResult.Success)
                        {
                            foreach (var task in taskResult.Data.Tasks)
                            {
                                Console.WriteLine(ObjectUtils.HumanReadable(task));
                                Console.WriteLine();
                            }
                        }
                        ds.Logout();
                    }
                    break;
                case ("new"):
                    var newOptions = (NewOptions)invokedVerbInstance;
                    if (ds.Login())
                    {
                        TResult<Object> taskResult = null;
                        var fileName = Path.GetFileName(newOptions.Filename);
                        var msgResult = String.Empty;
                        if (!String.IsNullOrWhiteSpace(newOptions.Filename))
                        {
                            using (var taskFile = new FileStream(newOptions.Filename, FileMode.Open, FileAccess.Read, FileShare.Read))
                            {
                                taskResult = ds.CreateTask(fileName, taskFile);
                            }
                            msgResult = String.Format("{0} upload", fileName);
                        }
                        if (!String.IsNullOrWhiteSpace(newOptions.Uri))
                        {
                            taskResult = ds.CreateTask(newOptions.Uri);
                            msgResult = String.Format("Create task to download {0}", newOptions.Uri);
                        }
                        Console.WriteLine("{0}: {1}", msgResult, taskResult != null && taskResult.Success ? "Ok" : "Failed ");
                        ds.Logout();
                    }
                    break;
                case ("delete"):
                    var deleteOptions = (TaskDeleteOptions)invokedVerbInstance;
                    if (ds.Login())
                    {
                        if (deleteOptions.Id.Any())
                        {
                            var taskResult = ds.DeleteTasks(deleteOptions.Id, deleteOptions.Force);
                            if (taskResult.Success)
                            {
                                foreach (var taskError in taskResult.Data)
                                {
                                    Console.WriteLine(ObjectUtils.HumanReadable(taskError));
                                    Console.WriteLine();
                                }
                            }
                        }
                        ds.Logout();
                    }
                    break;
                case ("pause"):
                    var pauseOptions = (TaskPauseOptions)invokedVerbInstance;
                    if (ds.Login())
                    {
                        if (pauseOptions.Id.Any())
                        {
                            var taskResult = ds.PauseTasks(pauseOptions.Id);
                            if (taskResult.Success)
                            {
                                foreach (var taskError in taskResult.Data)
                                {
                                    Console.WriteLine(ObjectUtils.HumanReadable(taskError));
                                    Console.WriteLine();
                                }
                            }
                        }
                        ds.Logout();
                    }
                    break;
                case ("resume"):
                    var resumeOptions = (TaskResumeOptions)invokedVerbInstance;
                    if (ds.Login())
                    {
                        if (resumeOptions.Id.Any())
                        {
                            var taskResult = ds.ResumeTasks(resumeOptions.Id);
                            if (taskResult.Success)
                            {
                                foreach (var taskError in taskResult.Data)
                                {
                                    Console.WriteLine(ObjectUtils.HumanReadable(taskError));
                                    Console.WriteLine();
                                }
                            }
                        }
                        ds.Logout();
                    }
                    break;
                default:
                    break;
            }
            // Console.ReadLine();
        }