Skip to content

Commit

Permalink
If originalProjectPath is not found, as to use the current project
Browse files Browse the repository at this point in the history
  • Loading branch information
suricactus committed Jun 23, 2021
1 parent 279ebde commit ca4e4b4
Showing 1 changed file with 66 additions and 48 deletions.
114 changes: 66 additions & 48 deletions qfieldsync/gui/synchronize_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
***************************************************************************/
"""
import os
from pathlib import Path

from qgis.core import QgsProject
from qgis.PyQt.QtCore import pyqtSlot
from qgis.PyQt.QtWidgets import QDialog, QDialogButtonBox
from qgis.PyQt.QtWidgets import QDialog, QDialogButtonBox, QMessageBox
from qgis.PyQt.uic import loadUiType

from qfieldsync.core.project import ProjectConfiguration
Expand Down Expand Up @@ -66,11 +67,13 @@ def __init__(self, iface, offline_editing, parent=None):

def start_synchronization(self):
self.button_box.button(QDialogButtonBox.Save).setEnabled(False)
qfield_folder = self.qfieldDir.text()
self.preferences.set_value("importDirectoryProject", qfield_folder)
current_path = Path(QgsProject.instance().fileName())
qfield_path = Path(self.qfieldDir.text())
self.preferences.set_value("importDirectoryProject", qfield_path)

try:
current_import_file_checksum = import_file_checksum(qfield_folder)
imported_files_checksums = import_checksums_of_project(qfield_folder)
current_import_file_checksum = import_file_checksum(str(qfield_path))
imported_files_checksums = import_checksums_of_project(qfield_path)

if (
imported_files_checksums
Expand All @@ -81,59 +84,74 @@ def start_synchronization(self):
"Data from this file are already synchronized with the original project."
)
raise NoProjectFoundError(message)
qgs_file = get_project_in_folder(qfield_folder)
open_project(qgs_file)

open_project(get_project_in_folder(str(qfield_path)))

self.offline_editing.progressStopped.connect(self.update_done)
self.offline_editing.layerProgressUpdated.connect(self.update_total)
self.offline_editing.progressModeSet.connect(self.update_mode)
self.offline_editing.progressUpdated.connect(self.update_value)
self.offline_editing.synchronize()
if self.offline_editing_done:
original_project_path = ProjectConfiguration(
QgsProject.instance()
).original_project_path
if original_project_path:
# import the DCIM folder
copy_images(
os.path.join(qfield_folder, "DCIM"),
os.path.join(os.path.dirname(original_project_path), "DCIM"),
)
if open_project(original_project_path):
# save the data_file_checksum to the project and save it
imported_files_checksums.append(
import_file_checksum(qfield_folder)
)
ProjectConfiguration(
QgsProject.instance()
).imported_files_checksums = imported_files_checksums
QgsProject.instance().write()
self.iface.messageBar().pushInfo(
"QFieldSync",
self.tr(
"Opened original project {}".format(
original_project_path
)
),
)
else:
self.iface.messageBar().pushInfo(
"QFieldSync",
self.tr(
"The data has been synchronized successfully but the original project ({}) could not be opened".format(
original_project_path
)
),
)

project_config = ProjectConfiguration(QgsProject.instance())
original_path = Path(project_config.original_project_path or "")

if not original_path.exists():
answer = QMessageBox.warning(
self,
self.tr("Original project not found"),
self.tr(
'The original project path at "{}" is not found. Would you like to use the currently opened project path at "{}" instead?'
).format(
original_path,
current_path,
),
QMessageBox.Yes | QMessageBox.No,
)

if answer == QMessageBox.Ok:
project_config.original_project_path = str(current_path)
original_path = current_path
else:
self.iface.messageBar().pushInfo(
"QFieldSync", self.tr("No original project path found")
"QFieldSync",
self.tr('No original project path found at "{}"').format(
original_path
),
)

if not self.offline_editing_done:
raise NoProjectFoundError(
self.tr(
"The project you imported does not seem to be an offline project"
)
self.close()
)

if original_path.exists() and open_project(str(original_path)):
copy_images(
os.path.join(qfield_path, "DCIM"),
os.path.join(original_path.parent, "DCIM"),
)
# save the data_file_checksum to the project and save it
imported_files_checksums.append(import_file_checksum(str(qfield_path)))
ProjectConfiguration(
QgsProject.instance()
).imported_files_checksums = imported_files_checksums
QgsProject.instance().write()
self.iface.messageBar().pushInfo(
"QFieldSync",
self.tr("Opened original project {}".format(original_path)),
)
else:
message = self.tr(
"The project you imported does not seem to be an offline project"
self.iface.messageBar().pushInfo(
"QFieldSync",
self.tr(
"The data has been synchronized successfully but the original project ({}) could not be opened".format(
original_path
)
),
)
raise NoProjectFoundError(message)
self.close()
except NoProjectFoundError as e:
self.iface.messageBar().pushWarning("QFieldSync", str(e))

Expand Down

0 comments on commit ca4e4b4

Please sign in to comment.