guiutils.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. """
  2. @package startup.guiutils
  3. @brief General GUI-dependent utilities for GUI startup of GRASS GIS
  4. (C) 2018 by Vaclav Petras the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. @author Vaclav Petras <wenzeslaus gmail com>
  8. @author Linda Kladivova <l.kladivova@seznam.cz>
  9. This is for code which depend on something from GUI (wx or wxGUI).
  10. """
  11. import os
  12. import wx
  13. from grass.grassdb.checks import (
  14. is_mapset_locked,
  15. get_mapset_lock_info,
  16. is_mapset_name_valid,
  17. is_location_name_valid,
  18. get_mapset_name_invalid_reason,
  19. get_location_name_invalid_reason,
  20. get_reason_mapset_not_removable,
  21. get_reasons_mapsets_not_removable,
  22. get_reasons_location_not_removable,
  23. get_reasons_locations_not_removable,
  24. get_reasons_grassdb_not_removable,
  25. is_fallback_session
  26. )
  27. import grass.grassdb.config as cfg
  28. from grass.grassdb.create import create_mapset, get_default_mapset_name
  29. from grass.grassdb.manage import (
  30. delete_mapset,
  31. delete_location,
  32. delete_grassdb,
  33. rename_mapset,
  34. rename_location,
  35. )
  36. from grass.script.core import create_environment
  37. from grass.script.utils import try_remove
  38. from grass.script import gisenv
  39. from core.gcmd import GError, GMessage, RunCommand
  40. from gui_core.dialogs import TextEntryDialog
  41. from location_wizard.dialogs import RegionDef
  42. from gui_core.widgets import GenericValidator
  43. class MapsetDialog(TextEntryDialog):
  44. def __init__(self, parent=None, default=None, message=None, caption=None,
  45. database=None, location=None):
  46. self.database = database
  47. self.location = location
  48. validator = GenericValidator(self._isMapsetNameValid,
  49. self._showMapsetNameInvalidReason)
  50. TextEntryDialog.__init__(
  51. self, parent=parent,
  52. message=message,
  53. caption=caption,
  54. defaultValue=default,
  55. validator=validator,
  56. )
  57. def _showMapsetNameInvalidReason(self, ctrl):
  58. message = get_mapset_name_invalid_reason(self.database,
  59. self.location,
  60. ctrl.GetValue())
  61. GError(parent=self, message=message, caption=_("Invalid mapset name"))
  62. def _isMapsetNameValid(self, text):
  63. """Check whether user's input location is valid or not."""
  64. return is_mapset_name_valid(self.database, self.location, text)
  65. class LocationDialog(TextEntryDialog):
  66. def __init__(self, parent=None, default=None, message=None, caption=None,
  67. database=None):
  68. self.database = database
  69. validator = GenericValidator(self._isLocationNameValid,
  70. self._showLocationNameInvalidReason)
  71. TextEntryDialog.__init__(
  72. self, parent=parent,
  73. message=message,
  74. caption=caption,
  75. defaultValue=default,
  76. validator=validator,
  77. )
  78. def _showLocationNameInvalidReason(self, ctrl):
  79. message = get_location_name_invalid_reason(self.database,
  80. ctrl.GetValue())
  81. GError(parent=self, message=message, caption=_("Invalid location name"))
  82. def _isLocationNameValid(self, text):
  83. """Check whether user's input location is valid or not."""
  84. return is_location_name_valid(self.database, text)
  85. def create_mapset_interactively(guiparent, grassdb, location):
  86. """
  87. Create new mapset
  88. """
  89. dlg = MapsetDialog(
  90. parent=guiparent,
  91. default=get_default_mapset_name(),
  92. message=_("Name for the new mapset:"),
  93. caption=_("Create new mapset"),
  94. database=grassdb,
  95. location=location,
  96. )
  97. mapset = None
  98. if dlg.ShowModal() == wx.ID_OK:
  99. mapset = dlg.GetValue()
  100. try:
  101. create_mapset(grassdb, location, mapset)
  102. except OSError as err:
  103. mapset = None
  104. GError(
  105. parent=guiparent,
  106. message=_("Unable to create new mapset: {}").format(err),
  107. showTraceback=False,
  108. )
  109. dlg.Destroy()
  110. return mapset
  111. def create_location_interactively(guiparent, grassdb):
  112. """
  113. Create new location using Location Wizard.
  114. Returns tuple (database, location, mapset) where mapset is "PERMANENT"
  115. by default or another mapset a user created and may want to switch to.
  116. """
  117. from location_wizard.wizard import LocationWizard
  118. gWizard = LocationWizard(parent=guiparent,
  119. grassdatabase=grassdb)
  120. if gWizard.location is None:
  121. gWizard_output = (None, None, None)
  122. # Returns Nones after Cancel
  123. return gWizard_output
  124. if gWizard.georeffile:
  125. message = _(
  126. "Do you want to import {} "
  127. "to the newly created location?"
  128. ).format(gWizard.georeffile)
  129. dlg = wx.MessageDialog(parent=guiparent,
  130. message=message,
  131. caption=_("Import data?"),
  132. style=wx.YES_NO | wx.YES_DEFAULT |
  133. wx.ICON_QUESTION)
  134. dlg.CenterOnParent()
  135. if dlg.ShowModal() == wx.ID_YES:
  136. gisrc_file, env = create_environment(gWizard.grassdatabase,
  137. gWizard.location,
  138. 'PERMANENT')
  139. import_file(guiparent, gWizard.georeffile, env)
  140. try_remove(gisrc_file)
  141. dlg.Destroy()
  142. if gWizard.default_region:
  143. defineRegion = RegionDef(guiparent, location=gWizard.location)
  144. defineRegion.CenterOnParent()
  145. defineRegion.ShowModal()
  146. defineRegion.Destroy()
  147. if gWizard.user_mapset:
  148. mapset = create_mapset_interactively(guiparent,
  149. gWizard.grassdatabase,
  150. gWizard.location)
  151. # Returns database and location created by user
  152. # and a mapset user may want to switch to
  153. gWizard_output = (gWizard.grassdatabase, gWizard.location,
  154. mapset)
  155. else:
  156. # Returns PERMANENT mapset when user mapset not defined
  157. gWizard_output = (gWizard.grassdatabase, gWizard.location,
  158. "PERMANENT")
  159. return gWizard_output
  160. def rename_mapset_interactively(guiparent, grassdb, location, mapset):
  161. """Rename mapset with user interaction.
  162. Exceptions during renaming are handled in get_reason_mapset_not_removable
  163. function.
  164. Returns newmapset if there was a change or None if the mapset cannot be
  165. renamed (see reasons given by get_reason_mapset_not_removable
  166. function) or if another error was encountered.
  167. """
  168. newmapset = None
  169. # Check selected mapset
  170. message = get_reason_mapset_not_removable(grassdb, location, mapset,
  171. check_permanent=True)
  172. if message:
  173. dlg = wx.MessageDialog(
  174. parent=guiparent,
  175. message=_(
  176. "Cannot rename mapset <{mapset}> for the following reason:\n\n"
  177. "{reason}\n\n"
  178. "No mapset will be renamed."
  179. ).format(mapset=mapset, reason=message),
  180. caption=_("Unable to rename selected mapset"),
  181. style=wx.OK | wx.ICON_WARNING
  182. )
  183. dlg.ShowModal()
  184. dlg.Destroy()
  185. return newmapset
  186. # Display question dialog
  187. dlg = MapsetDialog(
  188. parent=guiparent,
  189. default=mapset,
  190. message=_("Current name: {}\n\nEnter new name:").format(mapset),
  191. caption=_("Rename selected mapset"),
  192. database=grassdb,
  193. location=location,
  194. )
  195. if dlg.ShowModal() == wx.ID_OK:
  196. newmapset = dlg.GetValue()
  197. try:
  198. rename_mapset(grassdb, location, mapset, newmapset)
  199. except OSError as err:
  200. newmapset = None
  201. wx.MessageBox(
  202. parent=guiparent,
  203. caption=_("Error"),
  204. message=_("Unable to rename mapset.\n\n{}").format(err),
  205. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  206. )
  207. dlg.Destroy()
  208. return newmapset
  209. def rename_location_interactively(guiparent, grassdb, location):
  210. """Rename location with user interaction.
  211. Exceptions during renaming are handled in get_reasons_location_not_removable
  212. function.
  213. Returns newlocation if there was a change or None if the location cannot be
  214. renamed (see reasons given by get_reasons_location_not_removable
  215. function) or if another error was encountered.
  216. """
  217. newlocation = None
  218. # Check selected location
  219. messages = get_reasons_location_not_removable(grassdb, location)
  220. if messages:
  221. dlg = wx.MessageDialog(
  222. parent=guiparent,
  223. message=_(
  224. "Cannot rename location <{location}> for the following reasons:\n\n"
  225. "{reasons}\n\n"
  226. "No location will be renamed."
  227. ).format(location=location, reasons="\n".join(messages)),
  228. caption=_("Unable to rename selected location"),
  229. style=wx.OK | wx.ICON_WARNING
  230. )
  231. dlg.ShowModal()
  232. dlg.Destroy()
  233. return newlocation
  234. # Display question dialog
  235. dlg = LocationDialog(
  236. parent=guiparent,
  237. default=location,
  238. message=_("Current name: {}\n\nEnter new name:").format(location),
  239. caption=_("Rename selected location"),
  240. database=grassdb,
  241. )
  242. if dlg.ShowModal() == wx.ID_OK:
  243. newlocation = dlg.GetValue()
  244. try:
  245. rename_location(grassdb, location, newlocation)
  246. except OSError as err:
  247. newlocation = None
  248. wx.MessageBox(
  249. parent=guiparent,
  250. caption=_("Error"),
  251. message=_("Unable to rename location.\n\n{}").format(err),
  252. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  253. )
  254. dlg.Destroy()
  255. return newlocation
  256. def download_location_interactively(guiparent, grassdb):
  257. """
  258. Download new location using Location Wizard.
  259. Returns tuple (database, location, mapset) where mapset is "PERMANENT"
  260. by default or in future it could be the mapset the user may want to
  261. switch to.
  262. """
  263. from startup.locdownload import LocationDownloadDialog
  264. result = (None, None, None)
  265. loc_download = LocationDownloadDialog(parent=guiparent,
  266. database=grassdb)
  267. loc_download.Centre()
  268. loc_download.ShowModal()
  269. if loc_download.GetLocation() is not None:
  270. # Returns database and location created by user
  271. # and a mapset user may want to switch to
  272. result = (grassdb, loc_download.GetLocation(), "PERMANENT")
  273. loc_download.Destroy()
  274. return result
  275. def delete_mapset_interactively(guiparent, grassdb, location, mapset):
  276. """Delete one mapset with user interaction.
  277. This is currently just a convenience wrapper for delete_mapsets_interactively().
  278. """
  279. mapsets = [(grassdb, location, mapset)]
  280. return delete_mapsets_interactively(guiparent, mapsets)
  281. def delete_mapsets_interactively(guiparent, mapsets):
  282. """Delete multiple mapsets with user interaction.
  283. Parameter *mapsets* is a list of tuples (database, location, mapset).
  284. Exceptions during deletation are handled in get_reasons_mapsets_not_removable
  285. function.
  286. Returns True if there was a change, i.e., all mapsets were successfully
  287. deleted or at least one mapset was deleted.
  288. Returns False if one or more mapsets cannot be deleted (see reasons given
  289. by get_reasons_mapsets_not_removable function) or if an error was
  290. encountered when deleting the first mapset in the list.
  291. """
  292. deletes = []
  293. modified = False
  294. # Check selected mapsets
  295. messages = get_reasons_mapsets_not_removable(mapsets, check_permanent=True)
  296. if messages:
  297. dlg = wx.MessageDialog(
  298. parent=guiparent,
  299. message=_(
  300. "Cannot delete one or more mapsets for the following reasons:\n\n"
  301. "{reasons}\n\n"
  302. "No mapsets will be deleted."
  303. ).format(reasons="\n".join(messages)),
  304. caption=_("Unable to delete selected mapsets"),
  305. style=wx.OK | wx.ICON_WARNING
  306. )
  307. dlg.ShowModal()
  308. dlg.Destroy()
  309. return modified
  310. # No error occurs, create list of mapsets for deleting
  311. for grassdb, location, mapset in mapsets:
  312. mapset_path = os.path.join(grassdb, location, mapset)
  313. deletes.append(mapset_path)
  314. # Display question dialog
  315. dlg = wx.MessageDialog(
  316. parent=guiparent,
  317. message=_(
  318. "Do you want to continue with deleting"
  319. " one or more of the following mapsets?\n\n"
  320. "{deletes}\n\n"
  321. "All maps included in these mapsets will be permanently deleted!"
  322. ).format(deletes="\n".join(deletes)),
  323. caption=_("Delete selected mapsets"),
  324. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  325. )
  326. if dlg.ShowModal() == wx.ID_YES:
  327. try:
  328. for grassdb, location, mapset in mapsets:
  329. delete_mapset(grassdb, location, mapset)
  330. modified = True
  331. dlg.Destroy()
  332. return modified
  333. except OSError as error:
  334. wx.MessageBox(
  335. parent=guiparent,
  336. caption=_("Error when deleting mapsets"),
  337. message=_(
  338. "The following error occured when deleting mapset <{path}>:"
  339. "\n\n{error}\n\n"
  340. "Deleting of mapsets was interrupted."
  341. ).format(
  342. path=os.path.join(grassdb, location, mapset),
  343. error=error,
  344. ),
  345. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  346. )
  347. dlg.Destroy()
  348. return modified
  349. def delete_location_interactively(guiparent, grassdb, location):
  350. """Delete one location with user interaction.
  351. This is currently just a convenience wrapper for delete_locations_interactively().
  352. """
  353. locations = [(grassdb, location)]
  354. return delete_locations_interactively(guiparent, locations)
  355. def delete_locations_interactively(guiparent, locations):
  356. """Delete multiple locations with user interaction.
  357. Parameter *locations* is a list of tuples (database, location).
  358. Exceptions during deletation are handled in get_reasons_locations_not_removable
  359. function.
  360. Returns True if there was a change, i.e., all locations were successfully
  361. deleted or at least one location was deleted.
  362. Returns False if one or more locations cannot be deleted (see reasons given
  363. by get_reasons_locations_not_removable function) or if an error was
  364. encountered when deleting the first location in the list.
  365. """
  366. deletes = []
  367. modified = False
  368. # Check selected locations
  369. messages = get_reasons_locations_not_removable(locations)
  370. if messages:
  371. dlg = wx.MessageDialog(
  372. parent=guiparent,
  373. message=_(
  374. "Cannot delete one or more locations for the following reasons:\n\n"
  375. "{reasons}\n\n"
  376. "No locations will be deleted."
  377. ).format(reasons="\n".join(messages)),
  378. caption=_("Unable to delete selected locations"),
  379. style=wx.OK | wx.ICON_WARNING
  380. )
  381. dlg.ShowModal()
  382. dlg.Destroy()
  383. return modified
  384. # No error occurs, create list of locations for deleting
  385. for grassdb, location in locations:
  386. location_path = os.path.join(grassdb, location)
  387. deletes.append(location_path)
  388. # Display question dialog
  389. dlg = wx.MessageDialog(
  390. parent=guiparent,
  391. message=_(
  392. "Do you want to continue with deleting"
  393. " one or more of the following locations?\n\n"
  394. "{deletes}\n\n"
  395. "All mapsets included in these locations will be permanently deleted!"
  396. ).format(deletes="\n".join(deletes)),
  397. caption=_("Delete selected locations"),
  398. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  399. )
  400. if dlg.ShowModal() == wx.ID_YES:
  401. try:
  402. for grassdb, location in locations:
  403. delete_location(grassdb, location)
  404. modified = True
  405. dlg.Destroy()
  406. return modified
  407. except OSError as error:
  408. wx.MessageBox(
  409. parent=guiparent,
  410. caption=_("Error when deleting locations"),
  411. message=_(
  412. "The following error occured when deleting location <{path}>:"
  413. "\n\n{error}\n\n"
  414. "Deleting of locations was interrupted."
  415. ).format(
  416. path=os.path.join(grassdb, location),
  417. error=error,
  418. ),
  419. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  420. )
  421. dlg.Destroy()
  422. return modified
  423. def delete_grassdb_interactively(guiparent, grassdb):
  424. """
  425. Delete grass database if could be deleted.
  426. If current grass database found, desired operation cannot be performed.
  427. Exceptions during deleting are handled in this function.
  428. Returns True if grass database is deleted from the disk. Returns None if
  429. cannot be deleted (see above the possible reasons).
  430. """
  431. deleted = False
  432. # Check selected grassdb
  433. messages = get_reasons_grassdb_not_removable(grassdb)
  434. if messages:
  435. dlg = wx.MessageDialog(
  436. parent=guiparent,
  437. message=_(
  438. "Cannot delete GRASS database from disk for the following reason:\n\n"
  439. "{reasons}\n\n"
  440. "GRASS database will not be deleted."
  441. ).format(reasons="\n".join(messages)),
  442. caption=_("Unable to delete selected GRASS database"),
  443. style=wx.OK | wx.ICON_WARNING
  444. )
  445. dlg.ShowModal()
  446. else:
  447. dlg = wx.MessageDialog(
  448. parent=guiparent,
  449. message=_(
  450. "Do you want to delete"
  451. " the following GRASS database from disk?\n\n"
  452. "{grassdb}\n\n"
  453. "The directory will be permanently deleted!"
  454. ).format(grassdb=grassdb),
  455. caption=_("Delete selected GRASS database"),
  456. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  457. )
  458. if dlg.ShowModal() == wx.ID_YES:
  459. try:
  460. delete_grassdb(grassdb)
  461. deleted = True
  462. dlg.Destroy()
  463. return deleted
  464. except OSError as error:
  465. wx.MessageBox(
  466. parent=guiparent,
  467. caption=_("Error when deleting GRASS database"),
  468. message=_(
  469. "The following error occured when deleting database <{path}>:"
  470. "\n\n{error}\n\n"
  471. "Deleting of GRASS database was interrupted."
  472. ).format(
  473. path=grassdb,
  474. error=error,
  475. ),
  476. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  477. )
  478. dlg.Destroy()
  479. return deleted
  480. def can_switch_mapset_interactive(guiparent, grassdb, location, mapset):
  481. """
  482. Checks if mapset is locked and offers to remove the lock file.
  483. Returns True if user wants to switch to the selected mapset in spite of
  484. removing lock. Returns False if a user wants to stay in the current
  485. mapset or if an error was encountered.
  486. """
  487. can_switch = True
  488. mapset_path = os.path.join(grassdb, location, mapset)
  489. if is_mapset_locked(mapset_path):
  490. info = get_mapset_lock_info(mapset_path)
  491. user = info['owner'] if info['owner'] else _('unknown')
  492. lockpath = info['lockpath']
  493. timestamp = info['timestamp']
  494. dlg = wx.MessageDialog(
  495. parent=guiparent,
  496. message=_("User {user} is already running GRASS in selected mapset "
  497. "<{mapset}>\n (file {lockpath} created {timestamp} "
  498. "found).\n\n"
  499. "Concurrent use not allowed.\n\n"
  500. "Do you want to stay in the current mapset or remove "
  501. ".gislock and switch to selected mapset?"
  502. ).format(user=user,
  503. mapset=mapset,
  504. lockpath=lockpath,
  505. timestamp=timestamp),
  506. caption=_("Mapset is in use"),
  507. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  508. )
  509. dlg.SetYesNoLabels("S&witch to selected mapset",
  510. "S&tay in current mapset")
  511. if dlg.ShowModal() == wx.ID_YES:
  512. # Remove lockfile
  513. try:
  514. os.remove(lockpath)
  515. except IOError as e:
  516. wx.MessageBox(
  517. parent=guiparent,
  518. caption=_("Error when removing lock file"),
  519. message=_("Unable to remove {lockpath}.\n\n Details: {error}."
  520. ).format(lockpath=lockpath,
  521. error=e),
  522. style=wx.OK | wx.ICON_ERROR | wx.CENTRE
  523. )
  524. can_switch = False
  525. else:
  526. can_switch = False
  527. dlg.Destroy()
  528. return can_switch
  529. def import_file(guiparent, filePath, env):
  530. """Tries to import file as vector or raster.
  531. If successful sets default region from imported map.
  532. """
  533. RunCommand('db.connect', flags='c', env=env)
  534. mapName = os.path.splitext(os.path.basename(filePath))[0]
  535. vectors = RunCommand('v.in.ogr', input=filePath, flags='l',
  536. read=True, env=env)
  537. wx.BeginBusyCursor()
  538. wx.GetApp().Yield()
  539. if vectors:
  540. # vector detected
  541. returncode, error = RunCommand(
  542. 'v.in.ogr', input=filePath, output=mapName,
  543. getErrorMsg=True, env=env)
  544. if returncode == 0:
  545. RunCommand('g.region', flags='s', vector=mapName, env=env)
  546. else:
  547. returncode, error = RunCommand(
  548. 'r.in.gdal', input=filePath, output=mapName,
  549. getErrorMsg=True, env=env)
  550. if returncode == 0:
  551. RunCommand('g.region', flags='s', raster=mapName, env=env)
  552. wx.EndBusyCursor()
  553. if returncode != 0:
  554. GError(
  555. parent=guiparent,
  556. message=_(
  557. "Import of <%(name)s> failed.\n"
  558. "Reason: %(msg)s") % ({
  559. 'name': filePath,
  560. 'msg': error}))
  561. else:
  562. GMessage(
  563. message=_(
  564. "Data file <%(name)s> imported successfully. "
  565. "The location's default region was set from "
  566. "this imported map.") % {
  567. 'name': filePath},
  568. parent=guiparent)
  569. def switch_mapset_interactively(guiparent, giface, dbase, location, mapset,
  570. show_confirmation=False):
  571. """Switch current mapset. Emits giface.currentMapsetChanged signal."""
  572. # Decide if a user is in a fallback session
  573. fallback_session = is_fallback_session()
  574. if dbase:
  575. if RunCommand('g.mapset', parent=guiparent,
  576. location=location,
  577. mapset=mapset,
  578. dbase=dbase) == 0:
  579. if show_confirmation:
  580. GMessage(parent=guiparent,
  581. message=_("Current GRASS database is <%(dbase)s>.\n"
  582. "Current location is <%(loc)s>.\n"
  583. "Current mapset is <%(mapset)s>."
  584. ) %
  585. {'dbase': dbase, 'loc': location, 'mapset': mapset})
  586. giface.currentMapsetChanged.emit(dbase=dbase,
  587. location=location,
  588. mapset=mapset)
  589. elif location:
  590. if RunCommand('g.mapset', parent=guiparent,
  591. location=location,
  592. mapset=mapset) == 0:
  593. if show_confirmation:
  594. GMessage(parent=guiparent,
  595. message=_("Current location is <%(loc)s>.\n"
  596. "Current mapset is <%(mapset)s>.") %
  597. {'loc': location, 'mapset': mapset})
  598. giface.currentMapsetChanged.emit(dbase=None,
  599. location=location,
  600. mapset=mapset)
  601. else:
  602. if RunCommand('g.mapset',
  603. parent=guiparent,
  604. mapset=mapset) == 0:
  605. if show_confirmation:
  606. GMessage(parent=guiparent,
  607. message=_("Current mapset is <%s>.") % mapset)
  608. giface.currentMapsetChanged.emit(dbase=None,
  609. location=None,
  610. mapset=mapset)
  611. if fallback_session:
  612. tmp_dbase = os.environ["TMPDIR"]
  613. tmp_loc = cfg.temporary_location
  614. if tmp_dbase != gisenv()["GISDBASE"]:
  615. # Delete temporary location
  616. delete_location(tmp_dbase, tmp_loc)
  617. # Remove useless temporary grassdb node
  618. giface.grassdbChanged.emit(
  619. location=location, grassdb=tmp_dbase, action="delete", element="grassdb"
  620. )