Skip to content

Commit

Permalink
Fixes issue #557 and adds better error checking to application update…
Browse files Browse the repository at this point in the history
… checker

Fixes Issue #556 check for null object when remote directory is not found due to configuration path error
  • Loading branch information
jimradford committed Oct 15, 2015
1 parent 74c0966 commit c303ca5
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 35 deletions.
63 changes: 44 additions & 19 deletions SuperPutty/HttpRequest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
using System;
/*
* Copyright (c) 2009 - 2015 Jim Radford http://www.jimradford.com
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Net;
using System.Threading;
using System.Reflection;
using System.Text;
using System.IO;

Expand Down Expand Up @@ -41,34 +61,39 @@ public void MakeRequest(string url, Action<bool, string> callback)
RequestState state = new RequestState();
state.Request = request;
state.Callback = callback;
((HttpWebRequest)request).UserAgent = "SuperPutty/1.4.0.5";
((HttpWebRequest)request).UserAgent = "SuperPuTTY/" + Assembly.GetExecutingAssembly().GetName().Version;

IAsyncResult result = (IAsyncResult)request.BeginGetResponse(
new System.AsyncCallback(RespCallback), state);
IAsyncResult result = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(RespCallback), state);
}

private static void RespCallback(IAsyncResult ar)
{
// Get the RequestState object from the async result.
RequestState rs = (RequestState)ar.AsyncState;
try {
// Get the WebRequest from RequestState.
WebRequest req = rs.Request;

// Get the WebRequest from RequestState.
WebRequest req = rs.Request;
// Call EndGetResponse, which produces the WebResponse object
// that came from the request issued above.
WebResponse resp = req.EndGetResponse(ar);

// Call EndGetResponse, which produces the WebResponse object
// that came from the request issued above.
WebResponse resp = req.EndGetResponse(ar);
// Start reading data from the response stream.
Stream ResponseStream = resp.GetResponseStream();

// Start reading data from the response stream.
Stream ResponseStream = resp.GetResponseStream();
// Store the response stream in RequestState to read
// the stream asynchronously.
rs.ResponseStream = ResponseStream;

// Store the response stream in RequestState to read
// the stream asynchronously.
rs.ResponseStream = ResponseStream;

// Pass rs.BufferRead to BeginRead. Read data into rs.BufferRead
IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0,
BufferSize, new System.AsyncCallback(ReadCallBack), rs);
// Pass rs.BufferRead to BeginRead. Read data into rs.BufferRead
IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0,
BufferSize, new System.AsyncCallback(ReadCallBack), rs);
}
catch (WebException ex)
{
// Fire the callback so we can inform the user what went wrong
rs.Callback(false, ex.Message);
}
}

private static void ReadCallBack(IAsyncResult asyncResult)
Expand Down
69 changes: 56 additions & 13 deletions SuperPutty/Scp/BrowserPresenter.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
using System;
/*
* Copyright (c) 2009 - 2015 Jim Radford http://www.jimradford.com
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using log4net;
using System.ComponentModel;
using SuperPutty.Data;

namespace SuperPutty.Scp
{
#region LocalBrowserPresenter
/// <summary>
/// Start in last directory...preference
/// </summary>
/// <summary>Class that contains data and methods for displaying local or remote directories</summary>
public class BrowserPresenter : IBrowserPresenter
{
private static readonly ILog Log = LogManager.GetLogger(typeof(BrowserPresenter));

/// <summary>Raised when login and password information is required to authenticate against a ssh server serving files via scp</summary>
public event EventHandler<AuthEventArgs> AuthRequest;

/// <summary>Construct a new instance of the BrowserPresenter class with parameters</summary>
/// <param name="name">A string that identifies the name of the associated <seealso cref="ViewModel"/> e.g. "Local" or "Remote"</param>
/// <param name="model">The BrowserModel object used to get the data from a file system</param>
/// <param name="session">The session information containing host, ip, and other data specific to the BrowserModel</param>
/// <param name="fileTransferPresenter">Methods and tools for working with file transfers</param>
public BrowserPresenter(string name, IBrowserModel model, SessionData session, IFileTransferPresenter fileTransferPresenter)
{
this.Model = model;
Expand All @@ -37,7 +61,10 @@ public BrowserPresenter(string name, IBrowserModel model, SessionData session, I
};
}

void FileTransfers_ListChanged(object sender, ListChangedEventArgs e)
/// <summary>Updates the <seealso cref="BrowserViewModel"/> when a change to the file transfers list is detected</summary>
/// <param name="sender">The <seealso cref="IBindingList"/> of <seealso cref="FileTransferViewItem"/> items</param>
/// <param name="e">The <seealso cref="ListChangedEventArgs"/> items containing the type of change detected and the index of the item</param>
private void FileTransfers_ListChanged(object sender, ListChangedEventArgs e)
{
if (e.ListChangedType == ListChangedType.ItemChanged)
{
Expand All @@ -55,12 +82,12 @@ void FileTransfers_ListChanged(object sender, ListChangedEventArgs e)

#region Async Work

void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.ViewModel.Status = (string) e.UserState;
}

void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
BrowserFileInfo targetPath = (BrowserFileInfo)e.Argument;
this.BackgroundWorker.ReportProgress(5, "Requesting files for " + targetPath.Path);
Expand All @@ -71,7 +98,7 @@ void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
e.Result = result;
}

void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
Expand Down Expand Up @@ -129,37 +156,53 @@ void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventA

#endregion

/// <summary>Asynchronously loads the contents of a directory</summary>
/// <param name="dir">The BrowserFileInfo object containing the path to the directory to load</param>
public void LoadDirectory(BrowserFileInfo dir)
{
if (this.BackgroundWorker.IsBusy)
{
this.ViewModel.Status = "Busying loading directory";
this.ViewModel.Status = "Busy loading directory";
}
else
{
this.ViewModel.BrowserState = BrowserState.Working;
Log.InfoFormat("LoadDirectory, path={0}", dir);
this.BackgroundWorker.RunWorkerAsync(dir);
if (dir != null)
{
Log.InfoFormat("LoadDirectory, path={0}", dir);
this.BackgroundWorker.RunWorkerAsync(dir);
}
else
{
Log.Error("LoadDirectory Failed: target was null");
}
}
}

/// <summary>Refresh the current directory</summary>
public void Refresh()
{
// refresh current directory
Log.DebugFormat("Refresh");
Log.DebugFormat("Refreshing current directory: '{0}'", this.CurrentPath);
this.LoadDirectory(this.CurrentPath);
}

/// <summary>Verify a file can be transfered</summary>
/// <param name="source">The Source file</param>
/// <param name="target">The Destination file</param>
/// <returns>true if the file can be transfered</returns>
public bool CanTransferFile(BrowserFileInfo source, BrowserFileInfo target)
{
return this.FileTransferPresenter.CanTransferFile(source, target);
}

/// <summary>Transfer a file between two locations</summary>
/// <param name="fileTransferReqeust">The request data containing files to transfer</param>
public void TransferFiles(FileTransferRequest fileTransferReqeust)
{
this.FileTransferPresenter.TransferFiles(fileTransferReqeust);
}


protected void OnAuthRequest(AuthEventArgs evt)
{
if (this.AuthRequest != null)
Expand Down
10 changes: 7 additions & 3 deletions SuperPutty/frmSuperPutty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,9 @@ private void menuStrip1_MenuDeactivate(object sender, EventArgs e)
menuStrip1.Visible = SuperPuTTY.Settings.ShowMenuBar;
}

/// <summary>Check for a newer version of the SuperPuTTY Application</summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
{
Log.Info("Checking for application update");
Expand All @@ -1570,7 +1573,7 @@ private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
if (success)
{
DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(GitRelease));
MemoryStream ms = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(content));
MemoryStream ms = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(content));
GitRelease latest = (GitRelease)js.ReadObject(ms);
ms.Close();

Expand All @@ -1583,7 +1586,7 @@ private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly) == System.Windows.Forms.DialogResult.Yes)
MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
{
Process.Start(latest.release_url);
}
Expand All @@ -1598,7 +1601,8 @@ private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
}
else
{
Log.Warn("An Error occurred trying to check for program updates");
MessageBox.Show("There was an error while checking for updates. Please try again later.", "Error during update check", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Log.Warn("An Error occurred trying to check for program updates: " + content);
}
});
}
Expand Down

0 comments on commit c303ca5

Please sign in to comment.