Example #1
0
        void StartXauthLogin(string user, string password, NSAction callback)
        {
            LoadMoreElement status = loginDialog.Root [1][0] as LoadMoreElement;

            // Spin off a thread to start the OAuth authentication process,
            // let the GUI thread show the spinner.
            ThreadPool.QueueUserWorkItem(delegate {
                var oauth = new OAuthAuthorizer(TwitterAccount.OAuthConfig, user, password);

                if (oauth.AcquireAccessToken())
                {
                    BeginInvokeOnMainThread(delegate {
                        if (callback == null)
                        {
                            StartupAfterAuthorization(oauth);
                        }
                        else
                        {
                            SetDefaultAccount(oauth);
                            callback();
                        }
                    });
                    return;
                }

                BeginInvokeOnMainThread(delegate {
                    status.Animating = false;
                    loginAlert       = new UIAlertView(Locale.GetText("Error"),
                                                       Locale.GetText("Unable to login"),
                                                       null, null, Locale.GetText("Ok"));
                    loginAlert.Dismissed += delegate { loginAlert = null; };
                    loginAlert.Show();
                });
            });
        }
Example #2
0
        public void SetDefaultAccount(OAuthAuthorizer oauth)
        {
            var newAccount = TwitterAccount.Create(oauth);

            TwitterAccount.SetDefault(newAccount);
            Account = newAccount;
        }
Example #3
0
        //
        // TODO ITEMS:
        //   * Need to change this to use HttpWebRequest, since I need to erad
        //     the result back and create a tweet out of it, and insert in DB.
        //
        //   * Report error to the user?   Perhaps have a priority flag
        //     (posts show dialog, btu starring does not?
        //
        // Runs on a thread from the threadpool.
        void PostTask(QueuedTask2 [] tasks)
        {
            var client = GetClient();

            try {
                Util.PushNetworkActive();
                foreach (var task in tasks)
                {
                    Uri taskUri = new Uri(task.Url);
                    client.Headers [HttpRequestHeader.Authorization] = OAuthAuthorizer.AuthorizeRequest(OAuthConfig, OAuthToken, OAuthTokenSecret, "POST", taskUri, task.PostData);
                    try {
                        var res = client.UploadData(taskUri, task.Verb, Encoding.UTF8.GetBytes(task.PostData));
                        //var r = new StreamReader (new MemoryStream (res));
                        //Console.WriteLine (r.ReadToEnd ());
                    } catch (Exception e) {
                        // Can happen if we had already favorited this status
                        Console.WriteLine("Can happen if we already favorited: {0}", e);
                    }

                    lock (Database.Main)
                        Database.Main.Execute("DELETE FROM QueuedTask2 WHERE TaskId = ?", task.TaskId);
                }
            } catch (Exception e) {
                Console.WriteLine(e);
            } finally {
                Util.PopNetworkActive();
            }
        }
Example #4
0
 void StartupAfterAuthorization(OAuthAuthorizer oauth)
 {
     loginRoot.View.RemoveFromSuperview();
     CreatePhoneGui();
     SetDefaultAccount(oauth);
     loginDialog = null;
     loginRoot   = null;
 }
Example #5
0
            public AuthorizationViewController(OAuthAuthorizer oauth, string url, NSAction callback)
            {
                this.url       = url;
                this.container = oauth;
                this.callback  = callback;

                SetupWeb(url);
            }
Example #6
0
            //
            // the progress is reported like this:
            // 10% to open the connection
            // 90% for the image upload
            //
            public void Upload()
            {
                var boundary = "###" + Guid.NewGuid().ToString() + "###";

                //var url = new Uri ("http://api.twitpic.com/2/upload.json");
                var url = new Uri("http://yfrog.com/api/xauth_upload");
                var req = (HttpWebRequest)WebRequest.Create(url);

                req.Method      = "POST";
                req.ContentType = "multipart/form-data; boundary=" + boundary;
                OAuthAuthorizer.AuthorizeTwitPic(OAuthConfig, req, account.OAuthToken, account.OAuthTokenSecret);

                Stream upload = GenerateYFrogFrom(boundary, SourceStream, account.Username);

                req.ContentLength = upload.Length;
                SetProgress(0);
                try {
                    using (var rs = req.GetRequestStream()){
                        SetProgress(0.1f);
                        upload.Position = 0;
                        CopyToEnd(upload, rs, (sofar) => {
                            SetProgress(.1f + ((sofar / upload.Length) * 0.9f));
                        });
                        try {
                            rs.Close();
                        } catch {}
                    }
                } catch (Exception e) {
                    UploadCompletedCallback(null);
                }
                if (stop)
                {
                    return;
                }
                string urlToPic = null;

                try {
                    var response = (HttpWebResponse)req.GetResponse();
                    var stream   = response.GetResponseStream();
                    var doc      = XDocument.Load(stream);
                    if (doc.Element("rsp").Attribute("stat").Value == "ok")
                    {
                        urlToPic = doc.Element("rsp").Element("mediaurl").Value;
                    }
                    stream.Close();
                } catch (Exception e) {
                    Console.WriteLine(e);
                }
                if (stop)
                {
                    return;
                }

                invoker.BeginInvokeOnMainThread(delegate {
                    //ProgressHudView.Progress = 1;
                    UploadCompletedCallback(urlToPic);
                });
            }
        public static TwitterAccount Create(OAuthAuthorizer oauth)
        {
            var account = new TwitterAccount () {
                Username = oauth.AccessScreenname,
                AccountId = oauth.AccessId,
                OAuthToken = oauth.AccessToken,
                OAuthTokenSecret = oauth.AccessTokenSecret
            };
            lock (Database.Main)
                Database.Main.Insert (account);
            accounts [account.LocalAccountId] = account;

            return account;
        }
Example #8
0
        public void AddAccount(DialogViewController dvc, NSAction action)
        {
            var oauth = new OAuthAuthorizer (TwitterAccount.OAuthConfig);

            if (useXauth)
                NewAccountXAuth (dvc, action);
            else {
                if (oauth.AcquireRequestToken ()){
                    oauth.AuthorizeUser (dvc, delegate {
                        SetDefaultAccount (oauth);
                        action ();
                    });
                }
            }
        }
Example #9
0
        public static TwitterAccount Create(OAuthAuthorizer oauth)
        {
            var account = new TwitterAccount()
            {
                Username         = oauth.AccessScreenname,
                AccountId        = oauth.AccessId,
                OAuthToken       = oauth.AccessToken,
                OAuthTokenSecret = oauth.AccessTokenSecret
            };

            lock (Database.Main)
                Database.Main.Insert(account);
            accounts [account.LocalAccountId] = account;

            return(account);
        }
Example #10
0
        void StartLogin(DialogViewController dvc)
        {
            dvc.Root.RemoveAt(1);
            LoadMoreElement status;

            if (dvc.Root.Count == 1)
            {
                status = new LoadMoreElement(
                    Locale.GetText("Could not authenticate with twitter"),
                    Locale.GetText("Contacting twitter"), null, UIFont.BoldSystemFontOfSize(16), UIColor.Black)
                {
                    Animating = true
                };
                dvc.Root.Add(new Section()
                {
                    status
                });
            }
            else
            {
                status = (LoadMoreElement)dvc.Root [1][0];
            }

            // Spin off a thread to start the OAuth authentication process,
            // let the GUI thread show the spinner.
            ThreadPool.QueueUserWorkItem(delegate {
                var oauth = new OAuthAuthorizer(TwitterAccount.OAuthConfig);

                try {
                    if (oauth.AcquireRequestToken())
                    {
                        BeginInvokeOnMainThread(delegate {
                            oauth.AuthorizeUser(dvc, delegate {
                                StartupAfterAuthorization(oauth);
                            });
                        });
                        return;
                    }
                } catch (Exception e) {
                    Console.WriteLine(e);
                }

                BeginInvokeOnMainThread(delegate { status.Animating = false; });
            });
        }
Example #11
0
        public void AddAccount(DialogViewController dvc, NSAction action)
        {
            var oauth = new OAuthAuthorizer(TwitterAccount.OAuthConfig);

            if (useXauth)
            {
                NewAccountXAuth(dvc, action);
            }
            else
            {
                if (oauth.AcquireRequestToken())
                {
                    oauth.AuthorizeUser(dvc, delegate {
                        SetDefaultAccount(oauth);
                        action();
                    });
                }
            }
        }
Example #12
0
        void Launch(string url, bool callbackOnMainThread, Action <Stream> callback)
        {
            Util.PushNetworkActive();
            Uri uri = new Uri(url);

            // Wake up 3G if it has been more than 3 minutes
            lock (minuteLock){
                var nowTicks = DateTime.UtcNow.Ticks;
                if (nowTicks - lastLaunchTick > TimeSpan.TicksPerMinute * 3)
                {
                    MonoTouch.ObjCRuntime.Runtime.StartWWAN(uri);
                }
                lastLaunchTick = nowTicks;
            }

            var request = (HttpWebRequest)WebRequest.Create(uri);

            request.AutomaticDecompression = DecompressionMethods.GZip;
            request.Headers [HttpRequestHeader.Authorization] = OAuthAuthorizer.AuthorizeRequest(OAuthConfig, OAuthToken, OAuthTokenSecret, "GET", uri, null);

            request.BeginGetResponse(ar => {
                try {
                    lock (queue)
                        pending--;
                    Util.PopNetworkActive();
                    Stream stream = null;

                    try {
                        var response = (HttpWebResponse)request.EndGetResponse(ar);
                        stream       = response.GetResponseStream();

                        // Since the stream will deliver in chunks, make a copy before passing to the main UI
                        if (callbackOnMainThread)
                        {
                            var ms = new MemoryStream();
                            CopyStream(stream, ms);
                            ms.Position = 0;
                            stream.Close();
                            stream = ms;
                        }
                    } catch (WebException we) {
                        var response = we.Response as HttpWebResponse;
                        if (response != null)
                        {
                            switch (response.StatusCode)
                            {
                            case HttpStatusCode.Unauthorized:
                                // This is the case of sharing two keys
                                break;
                            }
                            stream = null;
                        }
                        Console.WriteLine(we);
                    } catch (Exception e) {
                        Console.WriteLine(e);
                        stream = null;
                    }

                    if (callbackOnMainThread)
                    {
                        invoker.BeginInvokeOnMainThread(delegate { InvokeCallback(callback, stream); });
                    }
                    else
                    {
                        InvokeCallback(callback, stream);
                    }
                } catch (Exception e) {
                    Console.WriteLine(e);
                }
                lock (queue){
                    if (queue.Count > 0)
                    {
                        var nextRequest = queue.Dequeue();
                        Launch(nextRequest.Url, nextRequest.CallbackOnMainThread, nextRequest.Callback);
                    }
                }
            }, null);
        }
Example #13
0
        void StartXauthLogin(string user, string password, NSAction callback)
        {
            LoadMoreElement status = loginDialog.Root [1][0] as LoadMoreElement;

            // Spin off a thread to start the OAuth authentication process,
            // let the GUI thread show the spinner.
            ThreadPool.QueueUserWorkItem (delegate {
                var oauth = new OAuthAuthorizer (TwitterAccount.OAuthConfig, user, password);

                if (oauth.AcquireAccessToken ()){
                    BeginInvokeOnMainThread (delegate {
                        if (callback == null)
                            StartupAfterAuthorization (oauth);
                        else {
                            SetDefaultAccount (oauth);
                            callback ();
                        }
                    });
                    return;
                }

                BeginInvokeOnMainThread (delegate {
                    status.Animating = false;
                    loginAlert = new UIAlertView (Locale.GetText ("Error"),
                                                 Locale.GetText ("Unable to login"),
                                                 null, null, Locale.GetText ("Ok"));
                    loginAlert.Dismissed += delegate { loginAlert = null; };
                    loginAlert.Show ();
                });
            });
        }
Example #14
0
 void StartupAfterAuthorization(OAuthAuthorizer oauth)
 {
     loginRoot.View.RemoveFromSuperview ();
     CreatePhoneGui ();
     SetDefaultAccount (oauth);
     loginDialog = null;
     loginRoot = null;
 }
Example #15
0
        void StartLogin(DialogViewController dvc)
        {
            dvc.Root.RemoveAt (1);
            LoadMoreElement status;

            if (dvc.Root.Count == 1){
                status = new LoadMoreElement (
                Locale.GetText ("Could not authenticate with twitter"),
                Locale.GetText ("Contacting twitter"), null, UIFont.BoldSystemFontOfSize (16), UIColor.Black) {
                    Animating = true
                };
                dvc.Root.Add (new Section () { status });
            } else
                status = (LoadMoreElement) dvc.Root [1][0];

            // Spin off a thread to start the OAuth authentication process,
            // let the GUI thread show the spinner.
            ThreadPool.QueueUserWorkItem (delegate {
                var oauth = new OAuthAuthorizer (TwitterAccount.OAuthConfig);

                try {
                    if (oauth.AcquireRequestToken ()){
                        BeginInvokeOnMainThread (delegate {
                            oauth.AuthorizeUser (dvc, delegate {
                                StartupAfterAuthorization (oauth);
                            });
                        });
                        return;
                    }
                } catch (Exception e){
                    Console.WriteLine (e);
                }

                BeginInvokeOnMainThread (delegate { status.Animating = false; });
            });
        }
Example #16
0
 public void SetDefaultAccount(OAuthAuthorizer oauth)
 {
     var newAccount = TwitterAccount.Create (oauth);
     TwitterAccount.SetDefault (newAccount);
     Account = newAccount;
 }
Example #17
0
            public AuthorizationViewController(OAuthAuthorizer oauth, string url, NSAction callback)
            {
                this.url = url;
                this.container = oauth;
                this.callback = callback;

                NavigationItem.Title = Locale.GetText ("Login to Twitter");
                NavigationItem.LeftBarButtonItem = new UIBarButtonItem (UIBarButtonSystemItem.Cancel, delegate {
                    DismissModalViewControllerAnimated (false);
                });
            }
            public AuthorizationViewController(OAuthAuthorizer oauth, string url, NSAction callback)
            {
                this.url = url;
                this.container = oauth;
                this.callback = callback;

                SetupWeb (url);
            }