From d3eb8aa893f032f442359ddfbcee2076e75e8502 Mon Sep 17 00:00:00 2001 From: RudahXimenes <57870396+RudahXimenes@users.noreply.github.com> Date: Mon, 6 Jan 2020 15:51:11 -0300 Subject: [PATCH 1/9] Update VGriveClient.vala Experimental changes I made in check_deleted_files function, sync_files and others --- src/VGriveClient.vala | 53 +++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/VGriveClient.vala b/src/VGriveClient.vala index 92fd92a..5531be4 100644 --- a/src/VGriveClient.vala +++ b/src/VGriveClient.vala @@ -89,7 +89,7 @@ namespace App { public string trash_path; public bool syncing = false; public bool change_detected = false; - public int changes_check_period = 10; + public int changes_check_period = 1; private Gee.HashMap? library; Thread thread; @@ -191,21 +191,22 @@ namespace App { // Check if we have changes in files and sync them if (this.is_syncing ()) { this.log_level=0; - this.check_deleted_files (); this.check_remote_files (this.main_path); this.check_local_files (this.main_path); + this.check_deleted_files (); this.log_level=1; - if (this.is_syncing ()) { - this.log_message (_("Everything is up to date!")); - // trigger per revisar canvis quan canvia algo local - this.watch_local_changes (); - // trigger per revisar canvis quan canvia algo remot - this.watch_remote_changes (); - while (this.is_syncing ()) { - Thread.usleep (this.changes_check_period*1000000); - this.process_changes (); - } + + this.log_message (_("Everything is up to date!")); + // trigger per revisar canvis quan canvia algo local + this.watch_local_changes (); + // trigger per revisar canvis quan canvia algo remot + this.watch_remote_changes (); + + while (this.is_syncing ()) { + Thread.usleep (this.changes_check_period*1000000); + this.process_changes (); } + return 1; }else { return -1; @@ -216,7 +217,6 @@ namespace App { if (this.is_syncing ()) { if (this.change_detected) { this.log_message(_("Change detected. Updating files...")); - this.change_detected = false; this.check_deleted_files (); this.check_remote_files (this.main_path); this.check_local_files (this.main_path); @@ -228,17 +228,19 @@ namespace App { return false; } } - private void check_deleted_files () { + + private void check_deleted_files () { // Mira els fitxers que hi ha a la llibreria // Si no existeixen en local o en remot, el treu de la llibreria i l'elimina de on encara hi sigui if (!this.is_syncing ()) return; + var it = this.library.map_iterator (); bool exist_local, exist_remote, must_delete = false; - DriveFile remote_file; + DriveFile[] remote_files; string remote_id, filename, aux, lpath; Array to_delete = new Array (); + for (var has_next = it.next (); has_next; has_next = it.next ()) { - if (!this.is_syncing ()) return; // Check local exists lpath = it.get_value(); aux = it.get_key(); @@ -246,8 +248,21 @@ namespace App { exist_local = this.local_file_exists(lpath); // Check remote exists - remote_id = this.get_file_id(lpath); - exist_remote = remote_id != null && remote_id != ""; + /* + EXPERIMENTAL REMOTE CHECKING + Note: could be incremented checking if the remote file is trased or not. If trashed, exist_remote should be false, otherwise should be true + */ + remote_files = this.list_files(-1, lpath, -1); + + foreach(DriveFile f in remote_files){ + if(f.name == filename){ + exist_remote = true; + } + } + //END OF EXPERIMENTAL + + //remote_id = this.get_file_id(lpath); + //exist_remote = remote_id != null && remote_id != ""; // Si fa falta, l'eliminem de on sigui (fa falta si en un dels dos llocs s'ha de eliminar must_delete = !exist_local || !exist_remote; @@ -380,7 +395,7 @@ namespace App { try { new Thread.try ("Watch remote thread", () => { while (this.is_syncing ()) { - Thread.usleep (this.changes_check_period*1000000); + //Thread.usleep (this.changes_check_period*1000000); this.change_detected = this.check_remote_changes (this.page_token); } new MainLoop ().run (); From 735fba5e5b4dafc763dbf2eed1978564874f0427 Mon Sep 17 00:00:00 2001 From: RudahXimenes <57870396+RudahXimenes@users.noreply.github.com> Date: Tue, 7 Jan 2020 10:31:56 -0300 Subject: [PATCH 2/9] Update VGriveClient.vala --- src/VGriveClient.vala | 96 ++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 46 deletions(-) diff --git a/src/VGriveClient.vala b/src/VGriveClient.vala index 5531be4..f402e8f 100644 --- a/src/VGriveClient.vala +++ b/src/VGriveClient.vala @@ -89,7 +89,7 @@ namespace App { public string trash_path; public bool syncing = false; public bool change_detected = false; - public int changes_check_period = 1; + public int changes_check_period = 10; private Gee.HashMap? library; Thread thread; @@ -191,20 +191,22 @@ namespace App { // Check if we have changes in files and sync them if (this.is_syncing ()) { this.log_level=0; + this.check_deleted_files (); this.check_remote_files (this.main_path); this.check_local_files (this.main_path); - this.check_deleted_files (); this.log_level=1; - this.log_message (_("Everything is up to date!")); - // trigger per revisar canvis quan canvia algo local - this.watch_local_changes (); - // trigger per revisar canvis quan canvia algo remot - this.watch_remote_changes (); + if(this.in_syncing ()){ + this.log_message (_("Everything is up to date!")); + // trigger per revisar canvis quan canvia algo local + this.watch_local_changes (); + // trigger per revisar canvis quan canvia algo remot + this.watch_remote_changes (); - while (this.is_syncing ()) { - Thread.usleep (this.changes_check_period*1000000); - this.process_changes (); + while (this.is_syncing ()) { + Thread.usleep (this.changes_check_period*1000000); + this.process_changes (); + } } return 1; @@ -229,7 +231,7 @@ namespace App { } } - private void check_deleted_files () { + private void check_deleted_files () { // Mira els fitxers que hi ha a la llibreria // Si no existeixen en local o en remot, el treu de la llibreria i l'elimina de on encara hi sigui if (!this.is_syncing ()) return; @@ -241,45 +243,47 @@ namespace App { Array to_delete = new Array (); for (var has_next = it.next (); has_next; has_next = it.next ()) { - // Check local exists - lpath = it.get_value(); - aux = it.get_key(); - filename = lpath.split("/")[lpath.split("/").length-1]; - exist_local = this.local_file_exists(lpath); - - // Check remote exists - /* - EXPERIMENTAL REMOTE CHECKING - Note: could be incremented checking if the remote file is trased or not. If trashed, exist_remote should be false, otherwise should be true - */ - remote_files = this.list_files(-1, lpath, -1); + if(this.in_syncing ()){ + // Check local exists + lpath = it.get_value(); + aux = it.get_key(); + filename = lpath.split("/")[lpath.split("/").length-1]; + exist_local = this.local_file_exists(lpath); + + // Check remote exists + /* + EXPERIMENTAL REMOTE CHECKING + Note: could be incremented checking if the remote file is trased or not. If trashed, exist_remote should be false, otherwise should be true + */ + remote_files = this.list_files(-1, lpath, -1); - foreach(DriveFile f in remote_files){ - if(f.name == filename){ - exist_remote = true; + foreach(DriveFile f in remote_files){ + if(f.name == filename){ + exist_remote = true; + } } - } - //END OF EXPERIMENTAL + //END OF EXPERIMENTAL - //remote_id = this.get_file_id(lpath); - //exist_remote = remote_id != null && remote_id != ""; - - // Si fa falta, l'eliminem de on sigui (fa falta si en un dels dos llocs s'ha de eliminar - must_delete = !exist_local || !exist_remote; - if (must_delete) { - to_delete.append_val(aux); - if (exist_local) { - this.log_message (_("DELETE LOCAL FILE: %s").printf (filename)); - this.move_local_file_to_trash(lpath); - } - if (exist_remote) { - if (!this.is_google_doc (remote_id)) { - this.log_message (_("DELETE REMOTE FILE: %s").printf (filename)); - this.delete_file(remote_id); + //remote_id = this.get_file_id(lpath); + //exist_remote = remote_id != null && remote_id != ""; + + // Si fa falta, l'eliminem de on sigui (fa falta si en un dels dos llocs s'ha de eliminar + must_delete = !exist_local || !exist_remote; + if (must_delete) { + to_delete.append_val(aux); + if (exist_local) { + this.log_message (_("DELETE LOCAL FILE: %s").printf (filename)); + this.move_local_file_to_trash(lpath); + } + if (exist_remote) { + if (!this.is_google_doc (remote_id)) { + this.log_message (_("DELETE REMOTE FILE: %s").printf (filename)); + this.delete_file(remote_id); + } } + }else { + this.log_message (_("INFO: %s not deleted/moved").printf (filename), 0); } - }else { - this.log_message (_("INFO: %s not deleted/moved").printf (filename), 0); } } for (int i = 0; i < to_delete.length ; i++) { @@ -395,7 +399,7 @@ namespace App { try { new Thread.try ("Watch remote thread", () => { while (this.is_syncing ()) { - //Thread.usleep (this.changes_check_period*1000000); + Thread.usleep (this.changes_check_period*1000000); this.change_detected = this.check_remote_changes (this.page_token); } new MainLoop ().run (); From b496e6a1cb4c643e9ccf2966fbe4ace18b883cb0 Mon Sep 17 00:00:00 2001 From: RudahXimenes <57870396+RudahXimenes@users.noreply.github.com> Date: Tue, 7 Jan 2020 15:47:32 -0300 Subject: [PATCH 3/9] Update VGriveClient.vala --- src/VGriveClient.vala | 170 ++++++++++++++++++++++++++++++------------ 1 file changed, 123 insertions(+), 47 deletions(-) diff --git a/src/VGriveClient.vala b/src/VGriveClient.vala index f402e8f..17ede97 100644 --- a/src/VGriveClient.vala +++ b/src/VGriveClient.vala @@ -191,7 +191,8 @@ namespace App { // Check if we have changes in files and sync them if (this.is_syncing ()) { this.log_level=0; - this.check_deleted_files (); + //this.check_deleted_files (); + // Made some changes that does not require check_deleted_files(). Check out this.check_remote_files (this.main_path); this.check_local_files (this.main_path); this.log_level=1; @@ -219,7 +220,7 @@ namespace App { if (this.is_syncing ()) { if (this.change_detected) { this.log_message(_("Change detected. Updating files...")); - this.check_deleted_files (); + //this.check_deleted_files (); this.check_remote_files (this.main_path); this.check_local_files (this.main_path); this.change_detected = false; @@ -231,6 +232,7 @@ namespace App { } } + /* check_deleted_files is DEPRECATED */ private void check_deleted_files () { // Mira els fitxers que hi ha a la llibreria // Si no existeixen en local o en remot, el treu de la llibreria i l'elimina de on encara hi sigui @@ -251,6 +253,7 @@ namespace App { exist_local = this.local_file_exists(lpath); // Check remote exists + /* EXPERIMENTAL REMOTE CHECKING Note: could be incremented checking if the remote file is trased or not. If trashed, exist_remote should be false, otherwise should be true @@ -291,56 +294,127 @@ namespace App { } this.save_library (); } +//---------------------------------- + + private void delete_files (string type, string file_id, string current_path = ""){ + /* + This function should be used to do the changes already checked in check function. + To use it you must enter the type of your action, the file id in the remote server and the current path of the file. + Current_path default is root. + + TYPES: + REMOTE = Do a delete action remotelly + LOCAL = Do a delete action locally + + IMPORTANT: + if type is LOCAL, you should enter the FILENAME in var file_id. For example: this.delete_files("LOCAL", filename, lpath); + if type is REMOTE, you should enter the FILE_ID in var file_id. For example: this.delete_files("REMOTE", remote_file.id, lpath); + */ + + var it = this.library.map_iterator (); + string to_delete; + + if(!this.is_syncing ()) return; + + if(type == "LOCAL"){ + // Deleting file in local + this.log_message (_("DELETE LOCAL FILE: %s").printf (file_id)); + this.move_local_file_to_trash(current_path+"/"+file_id); + + // Updating library + while(it.get_value() != current_path+"/"+file_id){ + it.next(); + } + + to_delete.append_val(it.get_key()); + this.library.unset(to_delete.index(0)); + this.save_library (); + } else if (type == "REMOTE"){ + // Deleting file in remote + this.log_message (_("DELETE REMOTE FILE: %s").printf (file_id)); + this.delete_file(file_id); + + // Updating library + while(it.get_value() != file_id){ + it.next(); + } + + to_delete.append_val(it.get_key()); + this.library.unset(to_delete.index(0)); + this.save_library (); + } + } private void check_remote_files (string current_path, string root_id="") { - // Mira els fitxers que hi ha a la en remot - // Si no existeixen en local el crea i l'afageix a la llibreria - // Si existeix en local i la hora de modificació local NO és posterior al a la del remot, es baixa el remot i mou el local al .trash - if (!this.is_syncing ()) return; - DriveFile[] res = this.list_files(-1, root_id, -1); + /* + This function should check files to sync in remote server. + If it detects missing files in local, should check for delete or download action. + If the files were deleted call the function to delete, if weren't, download. + */ + + if (!this.is_syncing ()) return; + + DriveFile[] res = this.list_files(-1, root_id, -1); + foreach (DriveFile f in res) { if (!this.is_syncing ()) return; - if (!this.library.has_key(f.id)) this.library.set(f.id, current_path+"/"+f.name); - // Check if it's a directory or a file - if (f.mimeType == "application/vnd.google-apps.folder") { - // It's a directory. Create it if doesn't exist - // Get it's id to get its files and download them if necessary - if (!this.local_file_exists(current_path+"/"+f.name)) this.create_local_file(f, current_path); - this.check_remote_files(current_path+"/"+f.name, f.id); - } else if (this.is_google_mime_type (f.mimeType)) { - // It's a google document. We don't want to download them - this.log_message(_("INFO: %s ignored").printf(f.name), 0); - } else { - // It's a file. Download it if it doesn't exist - if (!this.local_file_exists(current_path+"/"+f.name)) { - this.download_new_remote_file(f, current_path); - }else { - // Detect if the remote version is newer than the local one. - // If it's the case, move the local versio to .trash and download remote - DriveFile extra_info_file = this.get_file_info_extra(f.id, "modifiedTime"); - if (this.compare_files_write_time(extra_info_file.modifiedTime, current_path+"/"+f.name) == -1) { - this.download_new_version_remote_file(f, current_path); - }else this.log_message(_("INFO: %s not changed").printf(f.name), 0); + + // If file is not listed in the library, should be a new file, so sync it. Otherwise should be a deleted file, so delete it. + if(!this.library.has_key(f.id)){ + // Set the file in the library with this notation: file.id;file.local.path/filename + this.library.set(f.id, current_path+"/"+f.name); + + if (f.mimeType == "application/vnd.google-apps.folder") { + // Check if this file is a folder, then create if doesn't exist + if (!this.local_file_exists(current_path+"/"+f.name)) this.create_local_file(f, current_path); + } else if(this.is_google_mime_type (f.mimeType)){ + // It's a google document. We don't want to download them + this.log_message(_("INFO: %s ignored").printf(f.name), 0); + } else { + // It's a file. Download it if it doesn't exist + if (!this.local_file_exists(current_path+"/"+f.name)) { + this.download_new_remote_file(f, current_path); + } } + } else if (this.local_file_exists(current_path+"/"+f.name)) { + // Detect if the remote version is newer than the local one. + // If it's the case, move the local versio to .trash and download remote + DriveFile extra_info_file = this.get_file_info_extra(f.id, "modifiedTime"); + if (this.compare_files_write_time(extra_info_file.modifiedTime, current_path+"/"+f.name) == -1) { + this.download_new_version_remote_file(f, current_path); + } else this.log_message(_("INFO: %s not changed").printf(f.name), 0); + } else { + delete_files("REMOTE", f.id, current_path); } } this.save_library (); } private void check_local_files (string current_path, string root_id="") { - // Mira els fitxers que hi ha en local - // Si no existeixen en remot el crea i l'afageix a la llibreria - // Si existeix en remot i la hora de modificació remot NO és posterior al a la del local, es puja el local i mou el remot al .trash + /* + This function should check files to sync in local. + If it detects missing files in remote server, should check for delete or upload action. + If the files were deleted call the function to delete, if weren't, upload. + */ + if (!this.is_syncing ()) return; try { - var directory = File.new_for_path (current_path); - var enumerator = directory.enumerate_children (FileAttribute.STANDARD_NAME, 0); - FileInfo info; - DriveFile remote_file; - while ((info = enumerator.next_file ()) != null) { - if (!this.is_syncing ()) return; - if (this.is_regular_file(info.get_name())) { - remote_file = this.get_file_info(info.get_name(), root_id, -1); + var directory = File.new_for_path (current_path); + var enumerator = directory.enumerate_children (FileAttribute.STANDARD_NAME, 0); + FileInfo info; + DriveFile remote_file; + + while ((info = enumerator.next_file ()) != null) { + if (!this.is_syncing ()) return; + + if (this.is_regular_file(info.get_name())) { + remote_file = this.get_file_info(info.get_name(), root_id, -1); + + // If file is not listed in the library, should be a new file, so sync it. Otherwise should be a deleted file, so delete it. + if(!this.library.has_key(remote_file.id)){ + // Set the file in the library with this notation: file.id;file.local.path/filename + this.library.set(remote_file.id, current_path+"/"+info.get_name()); + if (info.get_file_type () == FileType.DIRECTORY) { if (remote_file.id == null) { // Create DIR @@ -355,18 +429,20 @@ namespace App { if (remote_file.id == null) { // Create File remote_file = this.upload_new_local_file(current_path+"/"+info.get_name(), root_id); - }else { - // Detect if the local version is newer than the remote one. - // If it's the case, upload local one - DriveFile extra_info_file = this.get_file_info_extra(remote_file.id, "modifiedTime"); - if (this.compare_files_write_time(extra_info_file.modifiedTime, current_path+"/"+remote_file.name) == 1) { - this.upload_local_file_update(current_path+"/"+info.get_name(), remote_file.id); - } else this.log_message(_("INFO: %s not changed").printf(remote_file.name), 0); } } - if (!this.library.has_key(remote_file.id)) this.library.set(remote_file.id, current_path+"/"+info.get_name()); + } else if (remote_file.id != null) { + // Detect if the local version is newer than the remote one. + // If it's the case, upload local one + DriveFile extra_info_file = this.get_file_info_extra(remote_file.id, "modifiedTime"); + if (this.compare_files_write_time(extra_info_file.modifiedTime, current_path+"/"+remote_file.name) == 1) { + this.upload_local_file_update(current_path+"/"+info.get_name(), remote_file.id); + } else this.log_message(_("INFO: %s not changed").printf(remote_file.name), 0); + } else { + delete_files("LOCAL", info.get_name(), current_path); } } + } } catch (Error e) { stderr.printf ("Error: %s\n", e.message); From 5a895903f4bf72300529b1c52cd4e4b482b74c46 Mon Sep 17 00:00:00 2001 From: RudahXimenes <57870396+RudahXimenes@users.noreply.github.com> Date: Tue, 7 Jan 2020 16:07:43 -0300 Subject: [PATCH 4/9] Update VGriveClient.vala --- src/VGriveClient.vala | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/VGriveClient.vala b/src/VGriveClient.vala index 17ede97..34bb13d 100644 --- a/src/VGriveClient.vala +++ b/src/VGriveClient.vala @@ -377,6 +377,7 @@ namespace App { } } } else if (this.local_file_exists(current_path+"/"+f.name)) { + this.library.set(f.id, current_path+"/"+f.name); // Detect if the remote version is newer than the local one. // If it's the case, move the local versio to .trash and download remote DriveFile extra_info_file = this.get_file_info_extra(f.id, "modifiedTime"); @@ -432,6 +433,7 @@ namespace App { } } } else if (remote_file.id != null) { + this.library.set(remote_file.id, current_path+"/"+info.get_name()); // Detect if the local version is newer than the remote one. // If it's the case, upload local one DriveFile extra_info_file = this.get_file_info_extra(remote_file.id, "modifiedTime"); From 6b866641d58adf8f9ad3fc46cd509d623c0d1996 Mon Sep 17 00:00:00 2001 From: RudahXimenes <57870396+RudahXimenes@users.noreply.github.com> Date: Thu, 9 Jan 2020 11:05:32 -0300 Subject: [PATCH 5/9] Update VGriveClient.vala --- src/VGriveClient.vala | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/VGriveClient.vala b/src/VGriveClient.vala index 34bb13d..17ede97 100644 --- a/src/VGriveClient.vala +++ b/src/VGriveClient.vala @@ -377,7 +377,6 @@ namespace App { } } } else if (this.local_file_exists(current_path+"/"+f.name)) { - this.library.set(f.id, current_path+"/"+f.name); // Detect if the remote version is newer than the local one. // If it's the case, move the local versio to .trash and download remote DriveFile extra_info_file = this.get_file_info_extra(f.id, "modifiedTime"); @@ -433,7 +432,6 @@ namespace App { } } } else if (remote_file.id != null) { - this.library.set(remote_file.id, current_path+"/"+info.get_name()); // Detect if the local version is newer than the remote one. // If it's the case, upload local one DriveFile extra_info_file = this.get_file_info_extra(remote_file.id, "modifiedTime"); From e2712e7eba73b5e75429e68ac4b477cd7a83d491 Mon Sep 17 00:00:00 2001 From: RudahXimenes <57870396+RudahXimenes@users.noreply.github.com> Date: Thu, 9 Jan 2020 11:28:26 -0300 Subject: [PATCH 6/9] Update VGriveClient.vala --- src/VGriveClient.vala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/VGriveClient.vala b/src/VGriveClient.vala index 17ede97..328d9c8 100644 --- a/src/VGriveClient.vala +++ b/src/VGriveClient.vala @@ -197,7 +197,7 @@ namespace App { this.check_local_files (this.main_path); this.log_level=1; - if(this.in_syncing ()){ + if(this.is_syncing ()){ this.log_message (_("Everything is up to date!")); // trigger per revisar canvis quan canvia algo local this.watch_local_changes (); @@ -245,7 +245,7 @@ namespace App { Array to_delete = new Array (); for (var has_next = it.next (); has_next; has_next = it.next ()) { - if(this.in_syncing ()){ + if(this.is_syncing ()){ // Check local exists lpath = it.get_value(); aux = it.get_key(); @@ -312,7 +312,7 @@ namespace App { */ var it = this.library.map_iterator (); - string to_delete; + Array to_delete = new Array (); if(!this.is_syncing ()) return; From c22b943b83275ce07560913b1817f7ad063bfba6 Mon Sep 17 00:00:00 2001 From: RudahXimenes <57870396+RudahXimenes@users.noreply.github.com> Date: Thu, 9 Jan 2020 11:40:17 -0300 Subject: [PATCH 7/9] Update VGriveClient.vala --- src/VGriveClient.vala | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/src/VGriveClient.vala b/src/VGriveClient.vala index 328d9c8..7c5fa7e 100644 --- a/src/VGriveClient.vala +++ b/src/VGriveClient.vala @@ -253,22 +253,8 @@ namespace App { exist_local = this.local_file_exists(lpath); // Check remote exists - - /* - EXPERIMENTAL REMOTE CHECKING - Note: could be incremented checking if the remote file is trased or not. If trashed, exist_remote should be false, otherwise should be true - */ - remote_files = this.list_files(-1, lpath, -1); - - foreach(DriveFile f in remote_files){ - if(f.name == filename){ - exist_remote = true; - } - } - //END OF EXPERIMENTAL - - //remote_id = this.get_file_id(lpath); - //exist_remote = remote_id != null && remote_id != ""; + remote_id = this.get_file_id(lpath); + exist_remote = remote_id != null && remote_id != ""; // Si fa falta, l'eliminem de on sigui (fa falta si en un dels dos llocs s'ha de eliminar must_delete = !exist_local || !exist_remote; From f20f455316cf0af07e9e280871cd1d841df47e1b Mon Sep 17 00:00:00 2001 From: RudahXimenes <57870396+RudahXimenes@users.noreply.github.com> Date: Thu, 9 Jan 2020 11:56:39 -0300 Subject: [PATCH 8/9] Update VGriveClient.vala --- src/VGriveClient.vala | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/VGriveClient.vala b/src/VGriveClient.vala index 7c5fa7e..5145c2b 100644 --- a/src/VGriveClient.vala +++ b/src/VGriveClient.vala @@ -252,7 +252,7 @@ namespace App { filename = lpath.split("/")[lpath.split("/").length-1]; exist_local = this.local_file_exists(lpath); - // Check remote exists + // Check remote existszz remote_id = this.get_file_id(lpath); exist_remote = remote_id != null && remote_id != ""; @@ -299,6 +299,7 @@ namespace App { var it = this.library.map_iterator (); Array to_delete = new Array (); + string aux; if(!this.is_syncing ()) return; @@ -312,8 +313,9 @@ namespace App { it.next(); } - to_delete.append_val(it.get_key()); - this.library.unset(to_delete.index(0)); + aux = it.get_key(); + //to_delete.append_val(aux); + this.library.unset(aux); this.save_library (); } else if (type == "REMOTE"){ // Deleting file in remote @@ -325,8 +327,9 @@ namespace App { it.next(); } - to_delete.append_val(it.get_key()); - this.library.unset(to_delete.index(0)); + aux = it.get_key(); + //to_delete.append_val(aux); + this.library.unset(aux); this.save_library (); } } From 157c58fbfbe188243c9cff0c9306b9e1b8f4bc69 Mon Sep 17 00:00:00 2001 From: RudahXimenes <57870396+RudahXimenes@users.noreply.github.com> Date: Fri, 10 Jan 2020 21:40:03 -0300 Subject: [PATCH 9/9] Code updated and tested Sync methods working consistently. I reviewed the code and put it to the test. Works fine. * Download new remote files * Upload new local files * Delete files properly * Update library consistently --- src/VGriveClient.vala | 104 ++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 39 deletions(-) diff --git a/src/VGriveClient.vala b/src/VGriveClient.vala index 5145c2b..c406fd4 100644 --- a/src/VGriveClient.vala +++ b/src/VGriveClient.vala @@ -221,8 +221,8 @@ namespace App { if (this.change_detected) { this.log_message(_("Change detected. Updating files...")); //this.check_deleted_files (); - this.check_remote_files (this.main_path); this.check_local_files (this.main_path); + this.check_remote_files (this.main_path); this.change_detected = false; this.log_message (_("Everything is up to date!")); } @@ -252,7 +252,7 @@ namespace App { filename = lpath.split("/")[lpath.split("/").length-1]; exist_local = this.local_file_exists(lpath); - // Check remote existszz + // Check remote exists remote_id = this.get_file_id(lpath); exist_remote = remote_id != null && remote_id != ""; @@ -298,39 +298,42 @@ namespace App { */ var it = this.library.map_iterator (); - Array to_delete = new Array (); - string aux; - + string aux, filename = ""; + string to_delete = ""; + if(!this.is_syncing ()) return; if(type == "LOCAL"){ + // Updating library + for (var has_next = it.next (); has_next; has_next = it.next ()) { + aux = it.get_value(); + if(aux == current_path+"/"+file_id){ + to_delete = it.get_key(); + } + } + + this.library.unset(to_delete); + this.save_library (); + // Deleting file in local this.log_message (_("DELETE LOCAL FILE: %s").printf (file_id)); this.move_local_file_to_trash(current_path+"/"+file_id); - + } else if (type == "REMOTE"){ // Updating library - while(it.get_value() != current_path+"/"+file_id){ - it.next(); + for (var has_next = it.next (); has_next; has_next = it.next ()) { + aux = it.get_key(); + if(aux == file_id){ + to_delete = aux; + filename = it.get_value(); + } } - - aux = it.get_key(); - //to_delete.append_val(aux); - this.library.unset(aux); + + this.library.unset(to_delete); this.save_library (); - } else if (type == "REMOTE"){ + // Deleting file in remote - this.log_message (_("DELETE REMOTE FILE: %s").printf (file_id)); + this.log_message (_("DELETE REMOTE FILE: %s").printf (filename)); this.delete_file(file_id); - - // Updating library - while(it.get_value() != file_id){ - it.next(); - } - - aux = it.get_key(); - //to_delete.append_val(aux); - this.library.unset(aux); - this.save_library (); } } @@ -349,13 +352,19 @@ namespace App { if (!this.is_syncing ()) return; // If file is not listed in the library, should be a new file, so sync it. Otherwise should be a deleted file, so delete it. - if(!this.library.has_key(f.id)){ - // Set the file in the library with this notation: file.id;file.local.path/filename - this.library.set(f.id, current_path+"/"+f.name); - + if(!this.library.has_key(f.id)){ if (f.mimeType == "application/vnd.google-apps.folder") { // Check if this file is a folder, then create if doesn't exist - if (!this.local_file_exists(current_path+"/"+f.name)) this.create_local_file(f, current_path); + if (!this.local_file_exists(current_path+"/"+f.name)) { + this.log_message(_("NEW REMOTE DIRECTORY: %s downloading...").printf(f.name), 0); + this.create_local_file(f, current_path); + this.log_message(_("NEW REMOTE DIRECTORY: %s downloaded ✓").printf(f.name), 0); + + // Set the folder in the library with this notation: folder.id;folder.local.path/filename + this.library.set(f.id, current_path+"/"+f.name); + } + // Check files inside the folder + this.check_remote_files(current_path+"/"+f.name, f.id); } else if(this.is_google_mime_type (f.mimeType)){ // It's a google document. We don't want to download them this.log_message(_("INFO: %s ignored").printf(f.name), 0); @@ -363,14 +372,19 @@ namespace App { // It's a file. Download it if it doesn't exist if (!this.local_file_exists(current_path+"/"+f.name)) { this.download_new_remote_file(f, current_path); + + // Set the file in the library with this notation: file.id;file.local.path/filename + this.library.set(f.id, current_path+"/"+f.name); } } - } else if (this.local_file_exists(current_path+"/"+f.name)) { + } else if (this.library.has_key(f.id) && this.local_file_exists(current_path+"/"+f.name)) { // Detect if the remote version is newer than the local one. // If it's the case, move the local versio to .trash and download remote DriveFile extra_info_file = this.get_file_info_extra(f.id, "modifiedTime"); if (this.compare_files_write_time(extra_info_file.modifiedTime, current_path+"/"+f.name) == -1) { + this.log_message(_("FILE MODIFIED REMOTELY: %s updating...").printf(f.name), 0); this.download_new_version_remote_file(f, current_path); + this.log_message(_("FILE MODIFIED REMOTELY: %s updated ✓").printf(f.name), 0); } else this.log_message(_("INFO: %s not changed").printf(f.name), 0); } else { delete_files("REMOTE", f.id, current_path); @@ -389,30 +403,43 @@ namespace App { if (!this.is_syncing ()) return; try { var directory = File.new_for_path (current_path); + bool has_in_lib = false; var enumerator = directory.enumerate_children (FileAttribute.STANDARD_NAME, 0); FileInfo info; - DriveFile remote_file; + DriveFile remote_file, new_remote_file; while ((info = enumerator.next_file ()) != null) { if (!this.is_syncing ()) return; + var it = this.library.map_iterator (); if (this.is_regular_file(info.get_name())) { remote_file = this.get_file_info(info.get_name(), root_id, -1); - + + if(remote_file.id == null){ + for (var has_next = it.next (); has_next; has_next = it.next ()) { + if(it.get_value() == current_path+"/"+info.get_name()){ + has_in_lib = true; + break; + } + } + } + // If file is not listed in the library, should be a new file, so sync it. Otherwise should be a deleted file, so delete it. - if(!this.library.has_key(remote_file.id)){ - // Set the file in the library with this notation: file.id;file.local.path/filename - this.library.set(remote_file.id, current_path+"/"+info.get_name()); - + if(!has_in_lib){ if (info.get_file_type () == FileType.DIRECTORY) { if (remote_file.id == null) { // Create DIR this.log_message(_("NEW LOCAL DIRECTORY: %s uploading...").printf(info.get_name())); remote_file = this.upload_new_local_dir(current_path+"/"+info.get_name(), root_id); this.log_message(_("NEW LOCAL DIRECTORY: %s uploaded ✓").printf(remote_file.name)); - }else { + } else { this.log_message(_("INFO: %s not changed").printf(remote_file.name), 0); } + + // Set the file in the library with this notation: file.id;file.local.path/filename + new_remote_file = this.get_file_info(info.get_name(), root_id, -1); + this.library.set(new_remote_file.id, current_path+"/"+info.get_name()); + this.check_local_files(current_path+"/"+info.get_name(), remote_file.id); } else { if (remote_file.id == null) { @@ -420,7 +447,7 @@ namespace App { remote_file = this.upload_new_local_file(current_path+"/"+info.get_name(), root_id); } } - } else if (remote_file.id != null) { + } else if (has_in_lib && remote_file.id != null) { // Detect if the local version is newer than the remote one. // If it's the case, upload local one DriveFile extra_info_file = this.get_file_info_extra(remote_file.id, "modifiedTime"); @@ -1442,4 +1469,3 @@ namespace App { } } -