private void ProcessWriteFileEx(Exception ex, AppInstallItem item) { item.SetLastEx(ex); this.UpdateProgress(0, item); if (ex is PathTooLongException) { this.UpdateState(InstallState.InstallFail_PathTooLongException, item); } else if (ex is IOException) { this.UpdateState(InstallState.InstallFail_FileInUse, item); } else if (ex is UnauthorizedAccessException) { this.UpdateState(InstallState.InstallFail_UnauthorizedAccess, item); } else if (ex is SecurityException) { this.UpdateState(InstallState.InstallFail_RequirePermission, item); } else { this.UpdateState(InstallState.InstallFail, item); } }
private void UpdateButtonState(AppInstallItem item) { this.cancelPenddingAppButton.IsEnabled = true; if (item.State == InstallState.DownloadFail || (item.State >= InstallState.InstallFail && item.State <= InstallState.InstallFail_PathTooLongException)) { this.retryButton.IsEnabled = true; } else { this.retryButton.IsEnabled = false; } if (item.State != InstallState.Downloading && item.State != InstallState.Installing) { this.cancelPenddingAppButton.IsEnabled = false; } if (item.State != InstallState.Installing && item.State != InstallState.Downloading) { this.deleteButton.IsEnabled = true; } else { this.deleteButton.IsEnabled = false; } }
internal void Cancel(string appId) { AppInstallItem item = this.GetAppInstallItemById(appId); if (item != null) { if (item.State == InstallState.Downloading) { if (item.WebClient != null) { item.WebClient.CancelAsync(); } this.UpdateState(InstallState.UserCancelling, item); } else if (item.State == InstallState.Installing) { } else if (item.State == InstallState.InstallFail || item.State == InstallState.DownloadFail) { item.WebClient.Clear(); lock (this.collectionLocker) { // this.installingAppCollection.Remove(item); } } } }
private void InitHttpHelper(HttpHelper helper, AppInstallItem appInstallItem) { helper.RetryBroken = true; helper.RetryTimes = 10; helper.AddHeader("SoonLearningPlatformKey", "{1FB07182-8794-47F1-90DB-7B3698AE1E61}"); helper.DownloadProgressChanged += ((sender, e) => { if (this.dispatcher != null) { this.UpdateProgress(e.ProgressPercentage, appInstallItem); } }); helper.DownloadFileCompleted += ((sender, e) => { if (e.Cancelled) { this.UpdateState(InstallState.UserCancelled, appInstallItem); this.InvokeInstallCompletedEvent(appInstallItem, e.Error); } else if (e.Error != null) { appInstallItem.SetLastEx(e.Error); this.UpdateState(InstallState.DownloadFail, appInstallItem); // this.installingAppCollection.Remove(appInstallItem); this.InvokeInstallCompletedEvent(appInstallItem, e.Error); } else { this.Install(appInstallItem); } this.runningItem = null; this.Next(); }); }
private void CompletedUpdate(TextBlock statusText, AppInstallItem app, System.Object status) { Dispatcher.Invoke((Action)(() => { statusText.Text = "Download is good."; LaunchTargetApp.IsEnabled = true; })); }
internal void Remove(AppInstallItem item) { lock (this.collectionLocker) { this.installingAppCollection.Remove(item); this.installingAppCollection.Save(DataMgr.Instance.AppInstallConfigFile); } }
private void InvokeInstallCompletedEvent(AppInstallItem item, Exception ex) { EventHandler <AppInstallCompletedEventArgs> temp = this.AppInstallCompletedEvent; if (temp != null) { temp(this, new AppInstallCompletedEventArgs(item, ex)); } }
private void StatusChangedUpdate(TextBlock statusText, AppInstallItem app, System.Object status) { Dispatcher.Invoke((Action)(() => { var statusResult = app.GetCurrentStatus(); statusText.Text = "We are installing " + statusResult.BytesDownloaded + " of " + statusResult.DownloadSizeInBytes; })); }
private void deleteButton_Click(object sender, RoutedEventArgs e) { AppInstallItem item = this.pendingListBox.SelectedItem as AppInstallItem; if (item == null) { return; } AppInstallMgr.Instance.Remove(item); }
private void cancelPenddingAppButton_Click(object sender, RoutedEventArgs e) { AppInstallItem item = this.pendingListBox.SelectedItem as AppInstallItem; if (item == null) { return; } AppInstallMgr.Instance.Cancel(item.AppItem.Id); }
private void UpdateProgress(int percent, AppInstallItem item) { this.dispatcher.BeginInvoke(new ThreadStart(() => { if (item.Percent != percent) { item.Percent = percent; } }), DispatcherPriority.Normal, null); }
private void pendingListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { AppInstallItem item = this.pendingListBox.SelectedItem as AppInstallItem; if (item == null) { this.cancelPenddingAppButton.IsEnabled = false; this.retryButton.IsEnabled = false; this.deleteButton.IsEnabled = false; } else { item.PropertyChanged -= this.AppInstallItemPropertyChangedHandler; item.PropertyChanged += this.AppInstallItemPropertyChangedHandler; this.UpdateButtonState(item); } }
internal void Start(GadgetItemOnline item) { lock (this.collectionLocker) { if (Enumerable.Count <AppInstallItem>(this.installingAppCollection, (c) => (c.AppItem.Id == item.Id)) > 0) { return; } } string tempPackFile = this.GetTempFile(item); HttpHelper helper = new HttpHelper(); AppInstallItem appInstallItem = new AppInstallItem(item, helper, tempPackFile); lock (this.collectionLocker) { this.installingAppCollection.Add(appInstallItem); } this.InitHttpHelper(helper, appInstallItem); if (this.runningItem != null) { return; } this.UpdateState(InstallState.Downloading, appInstallItem); this.runningItem = appInstallItem; try { this.runningItem.WebClient.DownloadFileAsync(this.runningItem.AppItem.PackageUrl, this.runningItem.LocakPackFile); } catch (Exception ex) { Debug.Assert(false, ex.Message); this.UpdateState(InstallState.DownloadFail, this.runningItem); this.runningItem = null; this.Next(); } }
private void autoStartFirst() { var query = from item in this.installingAppCollection where item.State != InstallState.Done select item; foreach (var item in query) { HttpHelper helper = new HttpHelper(); this.InitHttpHelper(helper, item); item.SetHttpHelper(helper); this.UpdateState(InstallState.Downloading, item); this.runningItem = item; string tempPackFile = this.GetTempFile(item.AppItem); helper.DownloadFileAsync(item.AppItem.PackageUrl, tempPackFile); break; } }
private void Next() { this.runningItem = null; lock (this.collectionLocker) { if (this.installingAppCollection.Count > 0) { var query = from item in this.installingAppCollection where item.State == InstallState.NotStart select item; foreach (AppInstallItem item in query) { this.runningItem = item; break; } } } if (this.runningItem != null) { if (this.runningItem.WebClient == null) { HttpHelper helper = new HttpHelper(); this.InitHttpHelper(helper, this.runningItem); this.runningItem.SetHttpHelper(helper); } this.UpdateState(InstallState.Downloading, this.runningItem); try { this.runningItem.WebClient.DownloadFileAsync(this.runningItem.AppItem.PackageUrl, this.runningItem.LocakPackFile); } catch (Exception ex) { Debug.Assert(false, ex.Message); this.UpdateState(InstallState.DownloadFail, this.runningItem); this.runningItem = null; this.Next(); } } }
internal void Retry(string appId) { AppInstallItem item = this.GetAppInstallItemById(appId); if (item != null) { this.UpdateState(InstallState.NotStart, item); if (item.WebClient == null) { HttpHelper helper = new HttpHelper(); this.InitHttpHelper(helper, item); item.SetHttpHelper(helper); } this.runningItem = item; this.UpdateState(InstallState.Downloading, item); item.WebClient.DownloadFileAsync(item.AppItem.PackageUrl, this.GetTempFile(item.AppItem)); } }
private void UpdateState(InstallState state, AppInstallItem item) { this.dispatcher.BeginInvoke(new ThreadStart(() => { item.State = state; if (state == InstallState.Done) { item.WebClient.Clear(); lock (this.collectionLocker) { this.Remove(item); // this.installingAppCollection.Add(item); } } lock (this.collectionLocker) { this.installingAppCollection.Save(DataMgr.Instance.AppInstallConfigFile); } }), DispatcherPriority.Send, null); }
private void pendingListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { AppInstallItem item = this.pendingListBox.SelectedItem as AppInstallItem; if (item == null) { this.cancelPenddingAppButton.IsEnabled = false; this.retryButton.IsEnabled = false; } else { this.cancelPenddingAppButton.IsEnabled = true; if (item.State == InstallState.DownloadFail || item.State == InstallState.InstallFail) { this.retryButton.IsEnabled = true; } else { this.retryButton.IsEnabled = false; } } }
private void Install(AppInstallItem item) { FileStream fs = null; try { this.UpdateState(InstallState.Installing, item); this.UpdateProgress(0, item); fs = File.OpenRead(item.LocakPackFile); // Check File Header string fileHeader = this.ReadString(fs); this.VerifyFileHeader(fileHeader); // Check AppTitle and AppId string appId = this.ReadString(fs); string appTitle = this.ReadString(fs); if (appTitle != item.AppItem.Title || appId != item.AppItem.Id) { // throw new NotSupportedException(); } string appFolder = this.GetAppFolder(fileHeader, appId); // Description string description = this.ReadString(fs); // Logos string thumbnail = this.ExtractLogo(fs, appId); // Entry File; string appEntryFile = this.ReadString(fs); string fullName = this.ReadString(fs); int type = BitConverter.ToInt32(this.ReadBytes(fs), 0); int subType = BitConverter.ToInt32(this.ReadBytes(fs), 0); AppItem appItem = this.CreateAppItem(fileHeader, type, subType); appItem.Id = appId; appItem.Title = appTitle; appItem.Description = description; appItem.Thumbnail = thumbnail; appItem.AppEntryFile = appEntryFile; appItem.FullName = fullName; appItem.Type = type; appItem.SubType = subType; appItem.CreateDate = DateTime.Parse(this.ReadString(fs)); appItem.CreatorName = this.ReadString(fs); appItem.CreatorLogo = this.ReadString(fs); appItem.CreatorWebSite = this.ReadString(fs); appItem.AddedTime = DateTime.Now.ToUniversalTime(); // Get File Count string countString = this.ReadString(fs); int count = Convert.ToInt32(countString); for (int i = 0; i < count; i++) { // Get FileName and Data string fileName = this.ReadString(fs); byte[] fileData = this.ReadBytes(fs); if (string.IsNullOrEmpty(fileName)) { continue; } appItem.RelatedFiles.Add(fileName); // Create File Path string filePath = appFolder + Path.GetDirectoryName(fileName); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } // Create File Name filePath = Path.Combine(filePath, Path.GetFileName(fileName)); if (File.Exists(filePath)) { } try { if (fileData.Length > 0) { File.WriteAllBytes(filePath, fileData); } } catch { } if (item.State == InstallState.UserCancelling) { this.UpdateState(InstallState.UserCancelled, item); break; } this.UpdateProgress((int)((i + 1 * 100) / count), item); } if (appItem is MemorizeAppItem) { MemorizeAppItem memorizeAppItem = appItem as MemorizeAppItem; try { memorizeAppItem.MemorizeEntry = MemorizeEntry.Load(System.IO.Path.Combine(appFolder, System.IO.Path.GetFileName(appItem.AppEntryFile))); if (memorizeAppItem.MemorizeEntry.SubType == 0) { memorizeAppItem.SubType = 10299; } else { memorizeAppItem.SubType = memorizeAppItem.MemorizeEntry.SubType; } memorizeAppItem.Type = 102; } catch (Exception e) { this.UpdateState(InstallState.InstallFail, item); this.InvokeInstallCompletedEvent(item, e); return; } } else if (appItem is AssessmentAppItem) { AssessmentAppItem assessmentAppItem = appItem as AssessmentAppItem; try { string folder = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); if (UIHelper.Is64BitOS()) { SevenZipExtractor.SetLibraryPath(System.IO.Path.Combine(folder, "ThirdParty\\7z\\7z64.dll")); } else { SevenZipExtractor.SetLibraryPath(System.IO.Path.Combine(folder, "ThirdParty\\7z\\7z.dll")); } string archiveFile = Path.Combine(folder, @"data\assessment\" + assessmentAppItem.Id + @"\" + assessmentAppItem.Id + ".zip"); assessmentAppItem.AppEntryFile = Path.Combine(folder, @"data\assessment\" + assessmentAppItem.Id + @"\" + assessmentAppItem.Id + ".sla"); using (SevenZipExtractor extractor = new SevenZipExtractor(archiveFile, "$L&%$D123@3$5^7*")) { string targetFolder = Path.Combine(folder, @"data\assessment\" + assessmentAppItem.Id); for (int i = 0; i < extractor.FilesCount; i++) { string fileName = extractor.ArchiveFileNames[i]; if (!extractor.ArchiveFileNames[i].StartsWith("\\")) { fileName = "\\" + extractor.ArchiveFileNames[i]; } string targetFile = targetFolder + fileName; FileStream targetFs = File.Open(targetFile, FileMode.OpenOrCreate, FileAccess.ReadWrite); extractor.ExtractFile(i, targetFs); targetFs.Close(); } } File.Delete(archiveFile); } catch (Exception ex) { Debug.Assert(false, ex.Message); this.UpdateState(InstallState.InstallFail, item); this.InvokeInstallCompletedEvent(item, ex); return; } } if (DataMgr.Instance.getAppItemById(appItem.Id) == null) { DataMgr.Instance.insertAppItem(0, appItem); } this.UpdateProgress(100, item); this.UpdateState(InstallState.Done, item); this.InvokeInstallCompletedEvent(item, null); // this.Remove(item); DataMgr.Instance.AppService.IncreaseDownloadCount(appItem.Id, LoginInfo.GetMD5Hash("#$32*d_&")); } catch (Exception ex) { ProcessWriteFileEx(ex, item); this.InvokeInstallCompletedEvent(item, ex); } finally { if (fs != null) { fs.Close(); } try { File.Delete(item.LocakPackFile); } catch { } } }
public AppInstallItemEvents(AppInstallItem This) { this.This = This; }