public StorageAccount(string id, StorageAccount.StorageAccountType type, string userName, double usedSize)
 {
     this.Id = id;
     this.StorageName = type.ToString();
     this.UserName = userName;
     this.UsedSize = usedSize;
 }
 public static StorageAccount ConvertToStorageAccount(MSStorageAccount mssa)
 {
     StorageAccount sa = new StorageAccount();
     sa.Id = mssa.account_platform_id;
     sa.StorageName = mssa.account_platform_id_type;
     sa.UserName = mssa.account_name;
     sa.UsedSize = mssa.account_used_size;
     return sa;
 }
        public async Task<bool> SignIn()
        {
            this.tcs = new TaskCompletionSource<bool>();
            try
            {
                // If it haven't registerd live client, register
                LiveConnectClient liveClient = await this.GetLiveConnectClientAsync();
                this.LiveClient = liveClient;

                // Get id and name.
                LiveOperationResult operationResult = await this.LiveClient.GetAsync("me");
                string accountId = (string)operationResult.Result["id"];
                string accountUserName = (string)operationResult.Result["name"];

                // Register account
                await TaskHelper.WaitTask(App.AccountManager.GetPtcId());
                StorageAccount storageAccount = await App.AccountManager.GetStorageAccountAsync(accountId);
                if (storageAccount == null)
                {
                    storageAccount = new StorageAccount();
                    storageAccount.Id = accountId;
                    storageAccount.StorageName = this.GetStorageName();
                    storageAccount.UserName = accountUserName;
                    storageAccount.UsedSize = 0.0;
                    await App.AccountManager.CreateStorageAccountAsync(storageAccount);
                }
                this.CurrentAccount = storageAccount;

                // Save sign in setting.
                App.ApplicationSettings[ONE_DRIVE_SIGN_IN_KEY] = true;
                App.ApplicationSettings.Save();
                TaskHelper.AddTask(TaskHelper.STORAGE_EXPLORER_SYNC + this.GetStorageName(), StorageExplorer.Synchronize(this.GetStorageName()));
                tcs.SetResult(true);
            }
            catch
            {
                tcs.SetResult(false);
            }
            return tcs.Task.Result;
        }
        public Task<bool> SignIn()
        {
            // Get dropbox _client.
            this.tcs = new TaskCompletionSource<bool>();
            this._client = new DropNetClient(DROPBOX_CLIENT_KEY, DROPBOX_CLIENT_SECRET);

            // If dropbox user exists, get it.
            // Otherwise, get from user.
            UserLogin dropboxUser = null;
            if (App.ApplicationSettings.Contains(DROPBOX_USER_KEY))
                dropboxUser = (UserLogin)App.ApplicationSettings[DROPBOX_USER_KEY];

            if (dropboxUser != null)
            {
                this._client.UserLogin = dropboxUser;
                this._client.AccountInfoAsync((info) =>
                {
                    this.CurrentAccount = new StorageAccount(info.uid.ToString(), StorageAccount.StorageAccountType.DROPBOX, info.display_name, 0.0);
                    TaskHelper.AddTask(TaskHelper.STORAGE_EXPLORER_SYNC + this.GetStorageName(), StorageExplorer.Synchronize(this.GetStorageName()));
                    tcs.SetResult(true);                
                }, (fail) =>
                {
                    this.CurrentAccount = null;
                    tcs.SetResult(false);
                });
            }
            else
            {
                this._client.GetTokenAsync(async (userLogin) =>
                {
                    string authUri = this._client.BuildAuthorizeUrl(DROPBOX_AUTH_URI);
                    DropboxWebBrowserTask webBrowser = new DropboxWebBrowserTask(authUri);
                    await webBrowser.ShowAsync();

                    this._client.GetAccessTokenAsync(async (accessToken) =>
                    {
                        UserLogin user = new UserLogin();
                        user.Token = accessToken.Token;
                        user.Secret = accessToken.Secret;
                        this._client.UserLogin = user;

                        // Save dropbox user got and sign in setting.
                        try
                        {
                            this.CurrentAccount = await this.GetMyAccountAsync();

                            StorageAccount account = await App.AccountManager.GetStorageAccountAsync(this.CurrentAccount.Id);
                            if (account == null)
                                await App.AccountManager.CreateStorageAccountAsync(this.CurrentAccount);

                            App.ApplicationSettings[DROPBOX_SIGN_IN_KEY] = true;
                            App.ApplicationSettings[DROPBOX_USER_KEY] = user;
                            App.ApplicationSettings.Save();
                            TaskHelper.AddTask(TaskHelper.STORAGE_EXPLORER_SYNC + this.GetStorageName(), StorageExplorer.Synchronize(this.GetStorageName()));
                            tcs.SetResult(true);
                        }
                        catch
                        {
                            tcs.SetResult(false);
                        }
                    },
                    (error) =>
                    {
                        Debug.WriteLine(error.ToString());
                        tcs.SetResult(false);
                    });
                },
               (error) =>
               {
                   tcs.SetResult(false);
               });
            }
            return tcs.Task;
        }
 // Remove user and record
 public void SignOut()
 {
     App.ApplicationSettings.Remove(DROPBOX_USER_KEY);
     App.ApplicationSettings.Remove(DROPBOX_SIGN_IN_KEY);
     StorageExplorer.RemoveKey(this.GetStorageName());
     this._client = null;
     this.CurrentAccount = null;
 }
 public static MSStorageAccount ConvertToMSStorageAccount(StorageAccount sa)
 {
     MSStorageAccount mssa = new MSStorageAccount(sa.Id, sa.StorageName, sa.UserName, sa.UsedSize);
     return mssa;
 }
 public async Task<bool> CreateStorageAccountAsync(StorageAccount sa)
 {
     MSStorageAccount mssa = StorageAccount.ConvertToMSStorageAccount(sa);
     mssa.ptc_account_id = this.GetPtcId();
     await App.MobileService.GetTable<MSStorageAccount>().InsertAsync(mssa);
     return true;
 }
 // Remove user and record
 public void SignOut()
 {
     LiveAuthClient liveAuthClient = new LiveAuthClient(LIVE_CLIENT_ID);
     liveAuthClient.Logout();
     App.ApplicationSettings.Remove(ONE_DRIVE_SIGN_IN_KEY);
     StorageExplorer.RemoveKey(this.GetStorageName());
     this.LiveClient = null;
     this.CurrentAccount = null;
 }
        public async Task<bool> SignIn()
        {
            this.tcs = new TaskCompletionSource<bool>();
            try
            {
                this.Credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    new ClientSecrets
                    {
                        ClientId = GOOGLE_DRIVE_CLIENT_ID,
                        ClientSecret = GOOGLE_DRIVE_CLIENT_SECRET
                    },
                    new[] { DriveService.Scope.Drive },
                    this._GetUserSession(),
                    CancellationToken.None
                );
                
                this.Service = new DriveService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = this.Credential,
                    ApplicationName = "At Here",
                });

                AboutResource aboutResource = this.Service.About;
                About about = await aboutResource.Get().ExecuteAsync();
                this.User = about.User;

                string name = this.User.DisplayName;
                string id = about.PermissionId;

                // Register account
                StorageAccount account = await App.AccountManager.GetStorageAccountAsync(id);
                if (account == null)
                {
                    account = new StorageAccount(id, StorageAccount.StorageAccountType.GOOGLE_DRIVE, name, 0.0);
                    await App.AccountManager.CreateStorageAccountAsync(account);
                }
                this.CurrentAccount = account;

                // Save sign in setting.
                App.ApplicationSettings[GOOGLE_DRIVE_SIGN_IN_KEY] = true;
                App.ApplicationSettings.Save();
                TaskHelper.AddTask(TaskHelper.STORAGE_EXPLORER_SYNC + this.GetStorageName(), StorageExplorer.Synchronize(this.GetStorageName()));
                this.tcs.SetResult(true);
            }
            catch (Microsoft.Phone.Controls.WebBrowserNavigationException)
            {
                this.tcs.SetResult(false);
            }
            catch (Google.GoogleApiException)
            {
                this.tcs.SetResult(false);
            }
            catch (System.Threading.Tasks.TaskCanceledException)
            {
                this.tcs.SetResult(false);
            }
            catch (Google.Apis.Auth.OAuth2.Responses.TokenResponseException)
            {
                this.tcs.SetResult(false);
            }
            catch(Exception)
            {
                this.tcs.SetResult(false);
            }
            return this.tcs.Task.Result;
        }