Exemple #1
0
        public bool CheckForUpdates()
        {
            OnSyncStateChanged?.Invoke(gSyncState.eChecking);
            if (gSyncSettingFileId.Equals("none"))
            {
                return(false);
            }

            if (!IsSyncAvaible())
            {
                OnSyncStateChanged?.Invoke(gSyncState.eFailed);
                return(false);
            }

            if (driveService == null)
            {
                driveService = new DriveService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName       = applicationName,
                });
            }

            try
            {
                using (var stream = new System.IO.FileStream(jsonDownloadFileLocalPath, System.IO.FileMode.Create))
                {
                    driveService.Files.Get(gSyncSettingFileId).DownloadWithStatus(stream);
                }
                bool result;
                using (StreamReader sr = new StreamReader(jsonDownloadFileLocalPath))
                {
                    string        json = sr.ReadToEnd();
                    GSyncSettings loadedGSyncSettings = JsonConvert.DeserializeObject <GSyncSettings>(json);
                    if (loadedGSyncSettings.dataVersion > gSyncSettings.dataVersion)
                    {
                        result = true;
                    }
                    else
                    {
                        result = false;
                    }
                }
                File.Delete(jsonDownloadFileLocalPath);
                return(result);
            }
            catch (System.Net.Http.HttpRequestException e)
            {
                OnSyncStateChanged?.Invoke(gSyncState.eLostConnection);
                return(false);
            }
            catch (Exception e)
            {
                OnSyncStateChanged?.Invoke(gSyncState.eFailed);
                OnError?.Invoke(e.Message);
                return(false);
            }
        }
Exemple #2
0
        void CreateTable(string tableName)
        {
            if (spreadSheetsService == null)
            {
                spreadSheetsService = new SheetsService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName       = applicationName,
                });
            }
            try
            {
                // TODO: Assign values to desired properties of `requestBody`:
                Google.Apis.Sheets.v4.Data.Spreadsheet requestBody = new Google.Apis.Sheets.v4.Data.Spreadsheet();
                requestBody.Properties       = new SpreadsheetProperties();
                requestBody.Properties.Title = tableName;
                List <Sheet> sheets    = new List <Sheet>();
                Sheet        mainSheet = new Sheet();
                mainSheet.Properties       = new SheetProperties();
                mainSheet.Properties.Title = "Notesieve";
                sheets.Add(mainSheet);
                requestBody.Sheets = sheets;

                SpreadsheetsResource.CreateRequest request = spreadSheetsService.Spreadsheets.Create(requestBody);
                // To execute asynchronously in an async method, replace `request.Execute()` as shown:
                Google.Apis.Sheets.v4.Data.Spreadsheet response = request.Execute();
                // Data.Spreadsheet response = await request.ExecuteAsync();

                // TODO: Change code below to process the `response` object:
                // Console.WriteLine();


                if (driveService == null)
                {
                    driveService = new DriveService(new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential,
                        ApplicationName       = applicationName,
                    });
                }

                var fileMetadata = new Google.Apis.Drive.v3.Data.File()
                {
                    Name    = jsonFileName,
                    Parents = new List <string>()
                    {
                        "appDataFolder"
                    }
                };

                using (StreamWriter sw = new StreamWriter(jsonFileLocalPath, false, System.Text.Encoding.Default))
                {
                    if (gSyncSettings == null)
                    {
                        gSyncSettings = new GSyncSettings();
                    }
                    gSyncSettings.spreadshetrID = response.SpreadsheetId;
                    gSyncSettings.dataVersion   = 0;

                    sw.WriteLine(JsonConvert.SerializeObject(gSyncSettings));
                }

                FilesResource.CreateMediaUpload newRequest;
                using (var stream = new System.IO.FileStream(jsonFileLocalPath, System.IO.FileMode.Open))
                {
                    newRequest        = driveService.Files.Create(fileMetadata, stream, "application/json");
                    newRequest.Fields = "id";
                    newRequest.Upload();

                    gSyncSettingFileId = newRequest.ResponseBody.Id;
                }


                File.Delete(jsonFileLocalPath);
            }
            catch (System.Net.Http.HttpRequestException e)
            {
                OnSyncStateChanged?.Invoke(gSyncState.eLostConnection);
            }
        }
Exemple #3
0
        private List <Note> GetAllNotes()
        {
            List <Note> answer = null;

            if (!IsSyncAvaible())
            {
                OnSyncStateChanged?.Invoke(gSyncState.eFailed);
                return(answer);
            }

            if (driveService == null)
            {
                driveService = new DriveService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName       = applicationName,
                });
            }
            try
            {
                var request = driveService.Files.List();
                request.Spaces = "appDataFolder";
                request.Fields = "nextPageToken, files(id, name)";
                request.Q      = "name = '" + jsonFileName + "'";
                var result = request.Execute();

                bool found = false;
                foreach (var file in result.Files)
                {
                    if (file.Name.Equals(jsonFileName))
                    {
                        found = true;
                        gSyncSettingFileId = file.Id;

                        using (var stream = new System.IO.FileStream(jsonDownloadFileLocalPath, System.IO.FileMode.Create))
                        {
                            driveService.Files.Get(gSyncSettingFileId).DownloadWithStatus(stream);
                        }
                        using (StreamReader sr = new StreamReader(jsonDownloadFileLocalPath))
                        {
                            string json = sr.ReadToEnd();
                            gSyncSettings = JsonConvert.DeserializeObject <GSyncSettings>(json);
                            answer        = ReadNotesFromTable();
                        }
                        File.Delete(jsonDownloadFileLocalPath);

                        break;
                    }
                }
                if (!found)
                {
                    CreateTable(tableName);
                }
                return(answer);
            }
            catch (System.Net.Http.HttpRequestException e)
            {
                OnSyncStateChanged?.Invoke(gSyncState.eLostConnection);
                return(null);
            }
        }