CloudDriveManager.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using PDF_Office.Helper;
  2. using PDF_Office.Model.CloudDrive;
  3. using PDF_Office.Properties;
  4. using PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace PDF_Office.ViewModels.HomePanel.CloudDrive
  12. {
  13. //各云盘的管理类
  14. //单实例化
  15. public class CloudDriveManager
  16. {
  17. #region 云盘类型
  18. public GoogleDriveManager GoogleDrive => GoogleDriveManager.GoogleDrive;
  19. public DropbBoxManager DropbBox => DropbBoxManager.DropbBox;
  20. #endregion
  21. public CloudType cloudType;
  22. private static CloudDriveManager instance;
  23. public static CloudDriveManager CloudManager => instance ?? (instance = new CloudDriveManager());
  24. private CloudDriveManager()
  25. {
  26. }
  27. #region 用户帐号
  28. /// <summary>
  29. /// 登录
  30. /// </summary>
  31. public async Task<bool> LoginUser(CloudType cloudType)
  32. {
  33. switch(cloudType)
  34. {
  35. case CloudType.GoogleDrive:
  36. return await GoogleDrive.LoginUserCount();
  37. case CloudType.DropBox:
  38. return await DropbBox.LoginUser();
  39. }
  40. return false;
  41. }
  42. public async Task<ObservableCollection<FilesBaseItem>> OpenFolder(FileOperation fileOperation)
  43. {
  44. ObservableCollection<FilesBaseItem> FilesList = null;
  45. switch (fileOperation.User.cloudType)
  46. {
  47. case CloudType.GoogleDrive:
  48. {
  49. var list = await GoogleDrive.GetFolder(fileOperation);
  50. FilesList = new ObservableCollection<FilesBaseItem>();
  51. foreach (var item in list)
  52. {
  53. FilesList.Add(item);
  54. }
  55. }
  56. break;
  57. case CloudType.DropBox:
  58. {
  59. var list = await DropbBox.GetFolder(fileOperation);
  60. FilesList = new ObservableCollection<FilesBaseItem>();
  61. foreach (var item in list)
  62. {
  63. FilesList.Add(item);
  64. }
  65. }
  66. break;
  67. }
  68. return FilesList;
  69. }
  70. public async Task<bool> OpenFile(FileOperation fileOperation)
  71. {
  72. string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  73. string tempPath = "";
  74. switch (fileOperation.User.cloudType)
  75. {
  76. case CloudType.GoogleDrive:
  77. tempPath = await GoogleDrive.DownloadGoogleFile(fileOperation, docPath);
  78. break;
  79. case CloudType.DropBox:
  80. tempPath = await DropbBox.DownloadFile(fileOperation, docPath);
  81. break;
  82. }
  83. if(string.IsNullOrEmpty(tempPath) == false)
  84. {
  85. string[] filePaths = { tempPath };
  86. await Task.Delay(3);
  87. LoadPdfViewer(filePaths);
  88. }
  89. return false;
  90. }
  91. public void LoadPdfViewer(string[] filePaths)
  92. {
  93. var content = App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel;
  94. if (filePaths.Count() == 1)
  95. {
  96. if (App.OpenedFileList.Contains(filePaths[0]))
  97. {
  98. App.mainWindowViewModel.SelectItem(filePaths[0]);
  99. }
  100. else
  101. {
  102. content.OpenFile(filePaths[0]);
  103. }
  104. ToolMethod.SetFileThumbImg(filePaths[0]);
  105. }
  106. else
  107. {
  108. var fileList = filePaths.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  109. if (fileList.Count <= 0)
  110. return;
  111. content.OpenFile(filePaths[0]);
  112. for (int i = 1; i < fileList.Count(); i++)
  113. {
  114. if (!App.OpenedFileList.Contains(fileList[i]))
  115. {
  116. App.mainWindowViewModel.AddTabItem(fileList[i]);
  117. }
  118. ToolMethod.SetFileThumbImg(fileList[i]);
  119. }
  120. }
  121. Settings.Default.Save();
  122. }
  123. public bool LoadedUsers()
  124. {
  125. bool isUsers = false;
  126. if (Cloud.CloudLists.Count > 0)
  127. isUsers = true;
  128. return isUsers;
  129. }
  130. /// <summary>
  131. /// 移除用户
  132. /// </summary>
  133. public async Task<bool> RemoveUser(UserBaseItem user)
  134. {
  135. bool result = false;
  136. switch (user.cloudType)
  137. {
  138. case CloudType.GoogleDrive:
  139. {
  140. var googleUser = user as GoogleDriveUserItem;
  141. if(googleUser != null)
  142. {
  143. result = await GoogleDrive.RemoveUser(googleUser);
  144. }
  145. }
  146. break;
  147. case CloudType.DropBox:
  148. {
  149. var dropBoxUser = user as DropbBoxUserItem;
  150. if (dropBoxUser != null)
  151. {
  152. result = await DropbBox.RemoveUser(dropBoxUser);
  153. }
  154. }
  155. break;
  156. }
  157. return result;
  158. }
  159. #endregion
  160. #region 文件
  161. public async Task<bool> UpLoadFile(FileOperation fileOperation)
  162. {
  163. bool result = false;
  164. switch (fileOperation.User.cloudType)
  165. {
  166. case CloudType.GoogleDrive:
  167. {
  168. var googleUser = fileOperation.User as GoogleDriveUserItem;
  169. if (googleUser != null)
  170. {
  171. result = await GoogleDrive.UpLoadFile(fileOperation);
  172. }
  173. }
  174. break;
  175. case CloudType.DropBox:
  176. {
  177. var dropBoxUser = fileOperation.User as DropbBoxUserItem;
  178. if (dropBoxUser != null)
  179. {
  180. result = await DropbBox.UpLoadFile(fileOperation);
  181. }
  182. }
  183. break;
  184. }
  185. return result;
  186. }
  187. #endregion
  188. }
  189. }