/*
         *      private void DownloadProgress(DownloadOperation download)
         *      {
         *
         *          System.Diagnostics.Debug.WriteLine(String.Format(CultureInfo.CurrentCulture, "Progress: {0}, Status: {1}", download.Guid, download.Progress.Status));
         *
         *          double percent = 100;
         *          if (download.Progress.TotalBytesToReceive > 0)
         *          {
         *              percent = download.Progress.BytesReceived * 100 / download.Progress.TotalBytesToReceive;
         *          }
         *
         *          System.Diagnostics.Debug.WriteLine(String.Format(CultureInfo.CurrentCulture, " - Transfered bytes: {0} of {1}, {2}%", download.Progress.BytesReceived, download.Progress.TotalBytesToReceive, percent));
         *
         *          if (download.Progress.HasRestarted)
         *          {
         *              System.Diagnostics.Debug.WriteLine(" - Download restarted");
         *          }
         *
         *          if (download.Progress.HasResponseChanged)
         *          {
         *              // We've received new response headers from the server.
         *              System.Diagnostics.Debug.WriteLine(" - Response updated; Header count: " + download.GetResponseInformation().Headers.Count);
         *
         *              // If you want to stream the response data this is a good time to start.
         *              // download.GetResultStreamAt(0);
         *          }
         *      }
         */


        // login windows http://msdn.microsoft.com/en-us/library/hh968445.aspx


        private async Task <string> obtainSessionAsync()
        {
            if (await OneDriveService.Instance.LoginAsync())
            {
                LiveStatusMessage notif = notifMessage;
                if (notif != null)
                {
                    notif(this, CONNECTED, null);
                }


                return("OK");
            }
            else
            {
                LiveStatusMessage notif = notifMessage;
                if (notif != null)
                {
                    notif(this, NOT_CONNECTED, null);
                }
            }
            return("KO");
        }
        public async Task <string> downloadFileAsync(string filename, IStorageFile file)
        {
            try
            {
                string tmp = await connectAsync();

                if (tmp == "OK")
                {
                    var sourceItem = await skyDriveFolder.GetFileAsync(filename);

                    if (sourceItem != null)
                    {
                        using (var remoteStream = (await sourceItem.StorageFilePlatformService.OpenAsync()) as IRandomAccessStream)
                        {
                            byte[] buffer      = new byte[remoteStream.Size];
                            var    localBuffer = await remoteStream.ReadAsync(buffer.AsBuffer(), (uint)remoteStream.Size, InputStreamOptions.ReadAhead);

                            using (var localStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                            {
                                //var res = await RandomAccessStream.CopyAsync(remoteStream, localStream);
                                await localStream.WriteAsync(localBuffer);

                                if (await localStream.FlushAsync())
                                {
                                }
                                //if (res == 1)
                                {
                                }
                            }
                        }

                        if (downloadFinished != null)
                        {
                            downloadFinished(this, file);
                        }

                        return("OK");
                    }
                    else
                    {
                        LiveStatusMessage notif = notifMessage;
                        if (notif != null)
                        {
                            notif(this, FOLDER_NOT_FOUND, null);
                        }
                    }
                }
            }
            catch (TaskCanceledException)
            {
                LiveStatusMessage notif = notifMessage;
                if (notif != null)
                {
                    notif(this, TASK_CANCELLED, null);
                }
            }
            catch (Exception ex)
            {
                LiveStatusMessage notif = notifMessage;
                if (notif != null)
                {
                    notif(this, ERROR, ex);
                }
            }
            //finally
            //{
            //    resetTransfers();
            //}
            return("KO");
        }