00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System;
00013 using System.IO;
00014 using System.Net;
00015 using System.Text;
00016 using System.Windows.Forms;
00017 using Mcs.Epm.MicrosoftProject.mpFx.Client.Plugins.PluginPlublisher.Controls;
00018 using Mcs.Epm.MicrosoftProject.mpFx.Client.Plugins.PluginPlublisher.Forms;
00019 using Mcs.Epm.MicrosoftProject.mpFx.Client.Plugins.PluginPlublisher.Properties;
00020 using Mcs.Epm.MicrosoftProject.mpFx.Client.Shared;
00021
00022 namespace Mcs.Epm.MicrosoftProject.mpFx.Client.Plugins.PluginPlublisher
00023 {
00024
00025
00026
00027 public class PluginPublisher : IMpFxClientPlugin
00028 {
00029 #region Instance Data
00030
00031 private bool _Disposed;
00032 private bool _IsLoaded;
00033
00034 #endregion
00035
00036 #region Public Properties
00037
00038 public ProjectServer ProjectServer { get; private set; }
00039
00040 public bool IsMarkedForDeletion { get; set; }
00041
00042 public Guid Guid
00043 {
00044 get
00045 {
00046 return new Guid("{2EB58C93-D11E-47c1-BB16-4C865D527A10}");
00047 }
00048 }
00049
00050 public Version Version
00051 {
00052 get
00053 {
00054 return new Version("1.0.0.0");
00055 }
00056 }
00057
00058 public string Implementation {get;set;}
00059
00060 public string Name
00061 {
00062 get
00063 {
00064 return Resources.PluginName;
00065 }
00066 }
00067
00068 public string Description
00069 {
00070 get
00071 {
00072 return Resources.PluginDescription;
00073 }
00074 }
00075
00076 public string Author
00077 {
00078 get
00079 {
00080 return Resources.PluginAuthor;
00081 }
00082 }
00083
00084 public string Tag
00085 {
00086 get
00087 {
00088 return Resources.PluginTag;
00089 }
00090 }
00091
00092 public Uri Preview
00093 {
00094 get
00095 {
00096 return new Uri(Resources.PluginPreview);
00097 }
00098 }
00099
00100 public bool IsLoaded
00101 {
00102 get
00103 {
00104 return _IsLoaded;
00105 }
00106 }
00107
00108 public Form MainForm
00109 {
00110 get
00111 {
00112 return null;
00113 }
00114 }
00115
00116 public UserControl OptionsControl
00117 {
00118 get
00119 {
00120 return new OptionsControl();
00121 }
00122 }
00123
00124 public string Password { get; private set; }
00125
00126 public string UserName { get; private set; }
00127
00128 public string Location { get; private set; }
00129
00130 #endregion
00131
00132 #region Public Events
00133
00134 public event OnStatusChangedEventHandler OnStatusChanged;
00135
00136 #endregion
00137
00138 #region Public Methods
00139
00140 public void OnLoad(ProjectServer projectServer, Form parent, ToolStrip toolStrip)
00141 {
00142 Tools.ValidateOnLoadParams(projectServer, parent, toolStrip);
00143
00144 ProjectServer = projectServer;
00145
00146 _IsLoaded = true;
00147 }
00148
00149 public void OnUnload()
00150 {
00151 ProjectServer = null;
00152
00153 _IsLoaded = false;
00154 }
00155
00156 public bool Login(Form parent)
00157 {
00158 using (GalleryLoginForm galleryLoginForm = new GalleryLoginForm())
00159 {
00160 DialogResult result = DialogResult.OK;
00161 Exception exception = null;
00162
00163 while (true)
00164 {
00165 parent.Cursor = Cursors.WaitCursor;
00166
00167 if (galleryLoginForm.ShowDialog(parent) == DialogResult.OK)
00168 {
00169 try
00170 {
00171 if (AttemptLogin(galleryLoginForm.GalleryLocation, galleryLoginForm.UserName, galleryLoginForm.Password))
00172 {
00173 parent.Cursor = Cursors.Default;
00174 return true;
00175 }
00176
00177 galleryLoginForm.DisplayLoginFailure = true;
00178 }
00179 catch (WebException webException)
00180 {
00181 if (webException.Message.Contains("530"))
00182 {
00183 exception = new Exception(Resources.LoginFailure);
00184 }
00185 else if (webException.Message.Contains("550"))
00186 {
00187 exception = new Exception(Resources.InvalidGalleryLocation);
00188 }
00189 else
00190 {
00191 exception = webException;
00192 }
00193 }
00194 catch (Exception generalException)
00195 {
00196 exception = generalException;
00197 }
00198
00199 if (exception != null)
00200 {
00201 result = MessageBox.Show(parent,
00202 exception.Message,
00203 Resources.PluginName,
00204 MessageBoxButtons.OKCancel,
00205 MessageBoxIcon.Error);
00206
00207 }
00208
00209 if (result == DialogResult.Cancel)
00210 {
00211 break;
00212 }
00213 }
00214 else
00215 {
00216 break;
00217 }
00218 }
00219
00220 parent.Cursor = Cursors.Default;
00221 }
00222
00223 return false;
00224 }
00225
00226 public bool AddPlugin(Form parent, PluginHost pluginHost)
00227 {
00228 using (AddPluginForm addPluginForm = new AddPluginForm(pluginHost))
00229 {
00230 return addPluginForm.ShowDialog(parent) == DialogResult.OK;
00231 }
00232 }
00233
00234 #endregion
00235
00236 #region Private Methods
00237
00238 private bool AttemptLogin(string location, string name, string password)
00239 {
00240 string tempLocalFile = string.Empty;
00241
00242 try
00243 {
00244 string loginFile = location;
00245
00246 if (!loginFile.EndsWith("/"))
00247 {
00248 loginFile = loginFile + '/';
00249 }
00250
00251 loginFile = loginFile + "Login/" + name + DateTime.Now.Ticks;
00252 loginFile = loginFile.Replace(" ", "");
00253 loginFile = loginFile.Replace("@", "_");
00254 loginFile = loginFile.Replace(@"http://", @"ftp://");
00255 loginFile = loginFile.Replace(@"www", @"ftp");
00256
00257 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(loginFile);
00258 request.Method = WebRequestMethods.Ftp.UploadFile;
00259
00260
00261 request.Credentials = new NetworkCredential(name, password);
00262
00263
00264 tempLocalFile = Path.GetTempFileName();
00265
00266 File.WriteAllText(tempLocalFile, loginFile);
00267
00268 byte[] fileContents;
00269
00270 using (StreamReader sourceStream = new StreamReader(tempLocalFile))
00271 {
00272 fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
00273 sourceStream.Close();
00274 }
00275
00276 File.Delete(tempLocalFile);
00277
00278 request.ContentLength = fileContents.Length;
00279
00280 using (Stream requestStream = request.GetRequestStream())
00281 {
00282 requestStream.Write(fileContents, 0, fileContents.Length);
00283 requestStream.Close();
00284 }
00285
00286 using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
00287 {
00288 response.Close();
00289 }
00290
00291 Location = location;
00292 UserName = name;
00293 Password = password;
00294 }
00295 finally
00296 {
00297
00298 if (!string.IsNullOrEmpty(tempLocalFile) && File.Exists(tempLocalFile))
00299 {
00300 Tools.DeleteFile(tempLocalFile);
00301 }
00302 }
00303
00304 return true;
00305 }
00306
00307 #endregion
00308
00309 #region Internal Methods
00310
00311 internal void DoOnStatusChanged(OnStatusChangedArgs args)
00312 {
00313 if (OnStatusChanged != null)
00314 {
00315 OnStatusChanged(this, args);
00316 }
00317 }
00318
00319 #endregion
00320
00321 #region IDisposable members
00322
00323 ~PluginPublisher()
00324 {
00325 Dispose(false);
00326 }
00327
00328 public void Dispose()
00329 {
00330 Dispose(true);
00331 GC.SuppressFinalize(this);
00332 }
00333
00334 private void Dispose(bool disposing)
00335 {
00336 if (_Disposed)
00337 {
00338 if (disposing)
00339 {
00340 try
00341 {
00342 }
00343 catch (Exception)
00344 {}
00345 }
00346
00347 _Disposed = true;
00348 }
00349 }
00350
00351
00352 #endregion
00353 }
00354 }