Advertisement
Guest User

Untitled

a guest
Jul 5th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.85 KB | None | 0 0
  1. // I considered having this be the base class, and having two classes inherit from it, e.g.,
  2. // FTPDirectory : SaveDirectory, and
  3. // LocalOrNetworkDirectory : SaveDirectory;
  4. //
  5. // I am still not sure if that would have been better
  6. internal class SaveDirectory
  7. {
  8. private SaveDirectory(string description, LocationType type, bool usedynamicfolders, string dynamic_rootfolder, string custom_daytimefolder, string custom_nightfolder,
  9. string ftp_username, string ftp_password, Int16 ftp_port, string ftp_hostname)
  10. {
  11. this.Description = description;
  12. this.Type = type;
  13. this.UseDynamicFolders = usedynamicfolders;
  14. this.Dynamic_RootFolder = dynamic_rootfolder;
  15. this.Custom_Daytimefolder = custom_daytimefolder;
  16. this.Custom_NightFolder = custom_nightfolder;
  17.  
  18. this.FTP_Username = ftp_username;
  19. this.FTP_Password = ftp_password;
  20. this.FTP_Port = ftp_port;
  21. this.FTP_Hostname = ftp_hostname;
  22.  
  23. this.Verify();
  24. }
  25.  
  26. // static factory methods. I don't like these. Usually I start to doubt what
  27. // I'm doing when I need to use named parameters like this
  28.  
  29. // I was thinking of using overloaded constructors but it
  30. // was very easy to accidentally forget one string and e.g. change this class from
  31. // a custom folder save location to a dynamic folder save location
  32. internal static SaveDirectory CreateUNC_DynamicFolderNames(string description, string rootfolder)
  33. {
  34. return new SaveDirectory(description: description, type: LocationType.UNC_Path,
  35. usedynamicfolders: true,
  36. dynamic_rootfolder: rootfolder,
  37.  
  38. custom_daytimefolder: null,
  39. custom_nightfolder: null,
  40.  
  41. ftp_username: null,
  42. ftp_password: null,
  43. ftp_port: cNoPort,
  44. ftp_hostname: null);
  45. }
  46.  
  47. internal static SaveDirectory CreateUNC_CustomFolderNames(string description, string daytimefolder, string nightfolder)
  48. {
  49. return new SaveDirectory(description: description, type: LocationType.UNC_Path,
  50. usedynamicfolders: false,
  51. dynamic_rootfolder: null,
  52.  
  53. custom_daytimefolder: daytimefolder,
  54. custom_nightfolder: nightfolder,
  55.  
  56. ftp_username: null,
  57. ftp_password: null,
  58. ftp_port: cNoPort,
  59. ftp_hostname: null);
  60.  
  61. }
  62.  
  63. internal static SaveDirectory CreateFTPFolder_DynamicFolderNames(string description, string rootfolder,
  64. string serverhostname, Int16 port, string username, string password)
  65. {
  66. return new SaveDirectory(description: description, type: LocationType.FTP,
  67. usedynamicfolders: true,
  68. dynamic_rootfolder: rootfolder,
  69.  
  70. custom_daytimefolder: null,
  71. custom_nightfolder: null,
  72.  
  73. ftp_username: username,
  74. ftp_password: password,
  75. ftp_port: port,
  76. ftp_hostname: serverhostname);
  77. }
  78.  
  79. internal static SaveDirectory CreateFTPFolder_CustomFolderNames(string description, string daytimefolder, string nightfolder,
  80. string serverhostname, Int16 port, string username, string password)
  81. {
  82. return new SaveDirectory(description: description, type: LocationType.FTP,
  83. usedynamicfolders: false,
  84. dynamic_rootfolder: null,
  85.  
  86. custom_daytimefolder: daytimefolder,
  87. custom_nightfolder: nightfolder,
  88.  
  89. ftp_username: username,
  90. ftp_password: password,
  91. ftp_port: port,
  92. ftp_hostname: serverhostname);
  93. }
  94.  
  95. private void Verify() // I put this in here because the constructor was long enough already
  96. {
  97. if (this.Type == LocationType.FTP)
  98. {
  99. if (
  100. (this.FTP_Username == null) ||
  101. (this.FTP_Password == null) ||
  102. (this.FTP_Port == cNoPort) ||
  103. (this.FTP_Hostname == null)
  104. )
  105. {
  106. throw new ArgumentException("Internal error: Failed to create new ftp image saving location, incomplete or missing FTP server information.");
  107. }
  108.  
  109. }
  110.  
  111. if (this.UseDynamicFolders)
  112. {
  113. if (this.Dynamic_RootFolder == null)
  114. {
  115. throw new ArgumentException("Internal error: Failed to create new image saving location, missing root folder for dynamic directories.");
  116. }
  117. }
  118. else
  119. {
  120. if (
  121. (this.Custom_Daytimefolder == null) ||
  122. (this.Custom_NightFolder == null)
  123. )
  124. {
  125. throw new ArgumentException("Internal error: Failed to create new image saving location, missing at least one custom save directory.");
  126. }
  127.  
  128. }
  129.  
  130.  
  131. }
  132.  
  133. internal enum LocationType
  134. {
  135. // no reason for this to be 1 other than 0 being the default value for ints
  136. UNC_Path = 1,
  137. FTP
  138. };
  139.  
  140. // for a GUI (listview)
  141. internal string DisplayString
  142. {
  143. get
  144. {
  145. if (this.Type == LocationType.FTP)
  146. {
  147. return this.Description + "-- (" + this.FTP_Hostname + ")";
  148. }
  149.  
  150. if (this.UseDynamicFolders)
  151. {
  152. return this.Description + "-- (" + this.Dynamic_RootFolder + ")";
  153. }
  154. else
  155. {
  156. return this.Description + "-- (" + this.Custom_Daytimefolder + ")";
  157.  
  158. }
  159. }
  160. }
  161.  
  162. internal static string CreateFTPURL(string hostname, Int16 port, string folder)
  163. {
  164. // I've run into a couple cases already where I forgot that I was naming my folders "folder/" instead of "folder" or "/folder" or "/folder/",
  165. // is there a better way?
  166. return "ftp://" + hostname + ":" + port.ToString() + "/" + folder;
  167. }
  168.  
  169. // The FTP or local / network drive file writer function gets passed the output of this function.
  170. // it outputs something like \myservermyfolder or ftp://myserver:21/myfolder/
  171. internal string GetURL(string dynamic_daytimefolder, string dynamic_nightfolder, bool isdaytime)
  172. {
  173.  
  174. if (this.Type == LocationType.FTP)
  175. {
  176. if (this.UseDynamicFolders)
  177. {
  178. if(isdaytime)
  179. {
  180. return SaveDirectory.CreateFTPURL(this.FTP_Hostname, this.FTP_Port, dynamic_daytimefolder);
  181. }
  182. else
  183. {
  184. return SaveDirectory.CreateFTPURL(this.FTP_Hostname, this.FTP_Port, dynamic_nightfolder);
  185. }
  186. }
  187. else
  188. {
  189. if(isdaytime)
  190. {
  191. return SaveDirectory.CreateFTPURL(this.FTP_Hostname, this.FTP_Port, this.Custom_Daytimefolder);
  192. }
  193. else
  194. {
  195. return SaveDirectory.CreateFTPURL(this.FTP_Hostname, this.FTP_Port, this.Custom_NightFolder);
  196. }
  197. }
  198.  
  199. }
  200. else
  201. {
  202. if (this.UseDynamicFolders)
  203. {
  204. if(isdaytime)
  205. {
  206. return this.Dynamic_RootFolder + dynamic_daytimefolder;
  207. }
  208. else
  209. {
  210. return this.Dynamic_RootFolder + dynamic_nightfolder;
  211. }
  212. }
  213. else
  214. {
  215. if(isdaytime)
  216. {
  217. return this.Custom_Daytimefolder;
  218. }
  219. else
  220. {
  221. return this.Custom_NightFolder;
  222. }
  223. }
  224. }
  225.  
  226.  
  227. }
  228.  
  229. // this is an immutable object,
  230. // is there any reason to not make these public/internal if they are readonly?
  231. internal readonly string Description;
  232. internal readonly LocationType Type;
  233. internal readonly bool UseDynamicFolders;
  234. internal readonly string Dynamic_RootFolder;
  235.  
  236. internal readonly string Custom_Daytimefolder;
  237. internal readonly string Custom_NightFolder;
  238.  
  239. internal readonly string FTP_Hostname;
  240.  
  241. internal const Int16 cNoPort = 0;
  242. internal const Int16 cDefaultFTPPort = 21;
  243.  
  244. internal readonly Int16 FTP_Port;
  245. internal readonly string FTP_Username;
  246. internal readonly string FTP_Password;
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement