/// <summary> /// Removes the <see cref="FrameworkElement"/> with the given <see cref="EventDefinition"/> from Tracking. /// </summary> /// <param name="element"></param> /// <param name="event"></param> private void RemoveEventHandler(FrameworkElement element, EventDefinition @event) { IEnumerable <KeyValuePair <FrameworkElementEvent, Delegate> > registerd = null; if (@event.AssignRecursively || [email protected](element)) { registerd = EventDelegates.Where(i => i.Key.Event.Equals(@event)).ToList(); } else { var key = new FrameworkElementEvent(element, @event, Element); registerd = EventDelegates.Where(i => i.Key.Equals(key)).ToList(); } if (registerd.Count() > 0) { foreach (var item in registerd) { var info = @event.GetEventInfo(item.Key.Element.GetType()); info.RemoveEventHandler(item.Key.Element, item.Value); EventDelegates.Remove(item.Key); Config.Instance.WriteLog(this, $"Removed {item.Key?.Element?.ToString()} with EventType {item.Key?.Event?.EventType.ToString()} to Tracked Events of {Element?.ToString()}.", Enums.LogType.Info); } } }
/// <summary> /// Send a message to the web server, requesting the current list of sights. /// </summary> /// <param name="responseDelegate">The delegate that should be /// called if the client receives a response.</param> /// <param name="failDelegate">The delegate that should be /// called if the client fails to receive a response.</param> public void GetSightsList( EventDelegates.HTTPResponseDelegate responseDelegate, EventDelegates.HTTPFailDelegate failDelegate) { this.webClient.SendRequest( false, serverURL + sightsListURL, new Dictionary<String, String>(), responseDelegate, failDelegate); }
/// <summary> /// Send a message to Amazon S3, requesting a picture. /// </summary> /// <param name="url">The picture URL.</param> /// <param name="responseDelegate">The delegate that should be /// called if the client receives a response.</param> /// <param name="failDelegate">The delegate that should be /// called if the client fails to receive a response.</param> public void DownloadPicture( String url, EventDelegates.HTTPResponseDelegate responseDelegate, EventDelegates.HTTPFailDelegate failDelegate) { // Remove the "https://" from the beginning of the string. if (url.StartsWith("https://")) url = url.Substring(9); this.webClient.SendRequest( false, url.Substring(9), new Dictionary<String, String>(), responseDelegate, failDelegate); }
/// <summary> /// Send a message to the web server, uploading a new picture. /// </summary> /// <param name="userID">The user's ID.</param> /// <param name="imageBytes">The picture to be uploaded as a byte array.</param> /// <param name="responseDelegate">The delegate that should be /// called if the client receives a response.</param> /// <param name="failDelegate">The delegate that should be /// called if the client fails to receive a response.</param> public void UploadPhoto( int userID, byte[] imageBytes, EventDelegates.HTTPResponseDelegate responseDelegate, EventDelegates.HTTPFailDelegate failDelegate) { // Build a dictionary which contains the user ID. // This dictionary will be used to create the POST variables. Dictionary<String, String> vars = new Dictionary<String, String>(); vars.Add("photo[user_id]", userID.ToString()); vars.Add("photo[latitude]", Convert.ToString(App.CurrentLatitude)); vars.Add("photo[longitude]", Convert.ToString(App.CurrentLongitude)); // Create the file data needed for the upload. String fileName = App.ImageFilename; String varName = "photo[file]"; // Upload the photo. this.webClient.UploadFile( serverURL + uploadURL, fileName, varName, imageBytes, vars, responseDelegate, failDelegate); }
/// <summary> /// Send a message to the web server, requesting to register a new user. /// </summary> /// <param name="firstName">The user's first name.</param> /// <param name="lastName">The user's last name.</param> /// <param name="emailAddress">The user's email address.</param> /// <param name="password">The user's password.</param> /// <param name="passwordConfirm">The user's password again.</param> /// <param name="responseDelegate">The delegate that should be /// called if the client receives a response.</param> /// <param name="failDelegate">The delegate that should be /// called if the client fails to receive a response.</param> public void Register( string firstName, string lastName, string emailAddress, string password, string passwordConfirm, EventDelegates.HTTPResponseDelegate responseDelegate, EventDelegates.HTTPFailDelegate failDelegate) { // Build a dictionary which contains the user's first name, // last name, email address, and password. // This dictionary will be used to create the POST variables. Dictionary<String, String> vars = new Dictionary<String, String>(); vars.Add("user[first_name]", firstName); vars.Add("user[last_name]", lastName); vars.Add("user[email]", emailAddress); vars.Add("user[password]", password); vars.Add("user[password_confirmation]", passwordConfirm); this.webClient.SendRequest( true, serverURL + registerURL, vars, responseDelegate, failDelegate); }
/// <summary> /// Send a message to the web server, requesting to login. /// </summary> /// <param name="emailAddress">The user's email address.</param> /// <param name="password">The user's password.</param> /// <param name="responseDelegate">The delegate that should be /// called if the client receives a response.</param> /// <param name="failDelegate">The delegate that should be /// called if the client fails to receive a response.</param> public void Login( String emailAddress, String password, EventDelegates.HTTPResponseDelegate responseDelegate, EventDelegates.HTTPFailDelegate failDelegate) { // Build a dictionary which contains the email address and password. // This dictionary will be used to create the POST variables. Dictionary<String, String> vars = new Dictionary<String, String>(); vars.Add("user_session[email]", emailAddress); vars.Add("user_session[password]", password); this.webClient.SendRequest( true, serverURL + loginURL, vars, responseDelegate, failDelegate); }
/// <summary> /// Adds the EventDelegate to the event handler for the given Event. If the EventType is not Assignable to the given FrameworkElement, /// it will Query through the Visual Tree and register this Event to the first FrameworkElement in each path where it fits. /// Method is not running Synchronous, it will add the event handlers in the Loaded Event, when the given Framework Element is not actually part of the Visual tree. /// </summary> /// <param name="element"></param> /// <param name="event"></param> /// <param name="trackingElement"></param> private void AddEventHandler(FrameworkElement element, EventDefinition @event, FrameworkElement trackingElement) { var key = new FrameworkElementEvent(element, @event, trackingElement); //Check if Event is already Added if (!EventDelegates.ContainsKey(key)) { Action <object, EventArgs> loadedAction = (sender, e) => { var children = element.GetChildren().Where(i => i is FrameworkElement).Cast <FrameworkElement>().ToList(); foreach (var child in children) { AddEventHandler(child, @event, trackingElement); } }; //Test if Event is assignable if (@event.IsAssignable(element)) { //Register Delegate var @delegate = Delegate.CreateDelegate( @event.Description.EventHandler, key, key.GetType() .GetMethod("EventDelegate", BindingFlags.NonPublic | BindingFlags.Instance)); RegisterDelegate(element, @delegate, @event); EventDelegates.Add(key, @delegate); Config.Instance.WriteLog(this, $"Adding {key?.Element?.ToString()} with EventType {key?.Event?.EventType.ToString()} to Tracked Events of {Element?.ToString()}.", Enums.LogType.Info); if (@event.AssignRecursively) { var parent = VisualTreeHelper.GetParent(element); var cCount = VisualTreeHelper.GetChildrenCount(element); if (parent == null && cCount == default(int)) { //Creating a delegate with Executes this after Loaded Event, because then is the Visual tree generated. element.Loaded += (sender, e) => loadedAction(sender, e); } else { //Element is attached to Visual Tree so get the Children directly loadedAction.Invoke(element, new RoutedEventArgs()); } } } else { //If not assignable: Query the visual tree for Elements where the Event can be Assigned to var parent = VisualTreeHelper.GetParent(element); var cCount = VisualTreeHelper.GetChildrenCount(element); if (parent == null && cCount == default(int)) { //Creating a delegate with Executes this after Loaded Event, because then is the Visual tree generated. element.Loaded += (sender, e) => loadedAction(sender, e); } else { //Element is attached to Visual Tree so get the Children directly loadedAction.Invoke(element, new RoutedEventArgs()); } } } }
/// <summary> /// Begins an asynchronous call to the specified URI. /// </summary> /// <param name="uri">The URI to which to send the request.</param> /// <param name="method">The HTTP method. Can be "GET" or "POST".</param> /// <param name="contentType">The HTTP content type.</param> /// <param name="body">The body of a POST request.</param> /// <param name="responseDelegate">The delegate that should be /// called if the client receives a response.</param> /// <param name="failDelegate">The delegate that should be /// called if the client fails to receive a response.</param> private void StartAsyncCall( String uri, String method, String contentType, byte[] body, EventDelegates.HTTPResponseDelegate responseDelegate, EventDelegates.HTTPFailDelegate failDelegate) { try { // Create and initialize an HTTP request to the desired URI. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(uri)); request.CookieContainer = App.Cookies; // Write some HTTP headers. request.Method = method; request.ContentType = contentType; // Create a request state. RequestState requestState = new RequestState(); requestState.Body = body; requestState.Request = request; requestState.ResponseDelegate = responseDelegate; requestState.FailDelegate = failDelegate; // Start the asynchronous request. if (method == "POST") request.BeginGetRequestStream(new AsyncCallback(WriteBody), requestState); else if (method == "GET") request.BeginGetResponse(new AsyncCallback(ReadResponse), requestState); } catch (Exception e) { failDelegate(e.Message); } }
/// <summary> /// Uploads a file using a POST request to the specified url. /// </summary> /// <param name="url">The URL to which to send the request.</param> /// <param name="fileName">The name of the file to upload.</param> /// <param name="varName">The variable name for the file to upload.</param> /// <param name="fileContents">The contents of the file.</param> /// <param name="vars">Any other POST variables.</param> /// <param name="responseDelegate">The delegate that should be /// called if the client receives a response.</param> /// <param name="failDelegate">The delegate that should be /// called if the client fails to receive a response.</param> public void UploadFile( string url, string fileName, string varName, byte[] fileContents, Dictionary<string, string> vars, EventDelegates.HTTPResponseDelegate responseDelegate, EventDelegates.HTTPFailDelegate failDelegate) { // Create the body of the multi-part POST request. byte[] body = ConstructMultiPartBody(fileName, varName, fileContents, vars); // Create the full correct URI. String uri = ConstructURI(url, true, String.Empty); // Start the asynchronous call. StartAsyncCall( uri, "POST", "multipart/form-data;boundary=" + boundary, body, responseDelegate, failDelegate); }
/// <summary> /// Sends an HTTP request to the specified URL. /// </summary> /// <param name="isPOST">True if this is a POST request, /// false if it's a GET request.</param> /// <param name="url">The URL to which to send the request.</param> /// <param name="vars">The POST or GET variables.</param> /// <param name="responseDelegate">The delegate that should be /// called if the client receives a response.</param> /// <param name="failDelegate">The delegate that should be /// called if the client fails to receive a response.</param> public void SendRequest( bool isPOST, String url, Dictionary<String, String> vars, EventDelegates.HTTPResponseDelegate responseDelegate, EventDelegates.HTTPFailDelegate failDelegate) { // Get the variable string. String varString = ConstructVarString(vars); // Create a binary byte array for the body. byte[] body = StrToByteArray(varString); // Create the full correct URI. String uri = ConstructURI(url, isPOST, varString); // Determine the method and content type. String method, contentType; if (isPOST) { method = "POST"; contentType = "application/x-www-form-urlencoded"; } else { method = "GET"; contentType = String.Empty; } // Start the asynchronous call. StartAsyncCall( uri, method, contentType, body, responseDelegate, failDelegate); }