Exemplo n.º 1
0
        internal static CloudStorage InitCouldStorage(Form1 form, CloudStorage cloudStorage)
        {
            if (IsCloudStorageOpen(cloudStorage))
            {
                return(cloudStorage);
            }

            ToolStripStatusLabel toolStripStatusLabel = form.toolStripStatusLabel;

            try
            {
                if (ConfigUtil.GetBoolParameter("ProxyEnabled"))
                {
                    if (String.IsNullOrEmpty(ConfigUtil.GetStringParameter("ProxyHost")))
                    {
                        WindowManager.ShowAlertBox(form, LanguageUtil.GetCurrentLanguageString("ProxyHostRequired", className));
                        return(null);
                    }

                    WebRequestManager.Instance.SetProxySettings(ConfigUtil.GetStringParameter("ProxyHost"), ConfigUtil.GetIntParameter("ProxyPort"), ProxyUtil.InitNetworkCredential());
                }

                DropBoxConfiguration standardConfiguration = DropBoxConfiguration.GetStandardConfiguration();

                try                                                                                                                                  //I try to authenticate myself with previous access token
                {
                    using (Stream tokenStream = new MemoryStream(Encoding.UTF8.GetBytes(PasswordUtil.GetStringParameter("LastDropboxAccessToken")))) //ConfigUtil.GetStringParameter("LastDropboxAccessToken")
                    {
                        if (tokenStream.Length == 0)
                        {
                            throw new UnauthorizedAccessException();
                        }

                        cloudStorage = new CloudStorage();
                        cloudStorage.Open(standardConfiguration, cloudStorage.DeserializeSecurityToken(tokenStream)); //cloudStorage.DeserializeSecurityToken(tokenStream, standardConfiguration)
                    }
                }
                catch (Exception) //No way, I should renew my access token
                {
                    standardConfiguration.AuthorizationCallBack = new Uri(ConstantUtil.dropboxAuthUrl);
                    DropBoxRequestToken requestToken = DropBoxStorageProviderTools.GetDropBoxRequestToken(standardConfiguration, ConstantUtil.dropboxConsumerKey, ConstantUtil.dropboxConsumerSecret);

                    if (requestToken == null)
                    {
                        throw new UnauthorizedAccessException();
                    }

                    if (WindowManager.ShowInternalBrowser(form, DropBoxStorageProviderTools.GetDropBoxAuthorizationUrl(standardConfiguration, requestToken)) == DialogResult.Cancel)
                    {
                        return(null);
                    }

                    //OtherManager.StartProcess(form, DropBoxStorageProviderTools.GetDropBoxAuthorizationUrl(standardConfiguration, requestToken));
                    //if (WindowManager.ShowQuestionBox(form, LanguageUtil.GetCurrentLanguageString("SuccessfullyAuth", className)) != DialogResult.Yes)
                    //{
                    //    return null;
                    //}

                    ICloudStorageAccessToken accessToken = DropBoxStorageProviderTools.ExchangeDropBoxRequestTokenIntoAccessToken(standardConfiguration, ConstantUtil.dropboxConsumerKey, ConstantUtil.dropboxConsumerSecret, requestToken);

                    if (accessToken == null)
                    {
                        throw new UnauthorizedAccessException();
                    }

                    cloudStorage = new CloudStorage();
                    cloudStorage.Open(standardConfiguration, accessToken);

                    if (ConfigUtil.GetBoolParameter("RememberDropboxAccess"))
                    {
                        using (Stream tokenStream = cloudStorage.SerializeSecurityToken(accessToken))
                        {
                            PasswordUtil.UpdateParameter("LastDropboxAccessToken", new StreamReader(tokenStream).ReadToEnd()); //ConfigUtil.UpdateParameter("LastDropboxAccessToken", new StreamReader(tokenStream).ReadToEnd());
                        }
                    }
                    else
                    {
                        PasswordUtil.UpdateParameter("LastDropboxAccessToken", String.Empty); //ConfigUtil.UpdateParameter("LastDropboxAccessToken", String.Empty);
                    }

                    toolStripStatusLabel.Text = LanguageUtil.GetCurrentLanguageString("DropboxLogIn", className);
                }

                return(cloudStorage);
            }
            catch (UnauthorizedAccessException)
            {
                WindowManager.ShowAlertBox(form, LanguageUtil.GetCurrentLanguageString("UnauthorizedAccess", className));
                return(null);
            }
            catch (UriFormatException)
            {
                WindowManager.ShowAlertBox(form, LanguageUtil.GetCurrentLanguageString("UriFormat", className));
                return(null);
            }
            finally
            {
                form.Cursor = Cursors.Default;
            }
        }