From 1c253ed41ff1f6f36173707d89070d36054db9f0 Mon Sep 17 00:00:00 2001
From: Alexei Sorokin <sor.alexei@meowr.ru>
Date: Mon, 22 Aug 2016 23:56:13 +0300
Subject: encode with utf-8 before writing XML

based on:
  https://github.com/GNOME/alacarte/commit/f7835d7
  https://github.com/GNOME/alacarte/commit/0d7f351
  https://github.com/GNOME/alacarte/commit/23f0318
---
 Mozo/MainWindow.py |  7 ++++---
 Mozo/MenuEditor.py | 47 +++++++++++++++++++++++++++++++++--------------
 2 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/Mozo/MainWindow.py b/Mozo/MainWindow.py
index c6fa109..279ffdf 100644
--- a/Mozo/MainWindow.py
+++ b/Mozo/MainWindow.py
@@ -22,9 +22,11 @@ gi.require_version('Gtk', '3.0')
 gi.require_version('Gdk', '3.0')
 from gi.repository import GLib, Gio
 from gi.repository import Gtk, Gdk, GdkPixbuf
-import cgi, os
+import cgi
+import os
 import gettext
 import subprocess
+import shutil
 import urllib
 try:
 	from Mozo import config
@@ -382,8 +384,7 @@ class MainWindow:
 			file_type = 'Menu'
 
 		if not os.path.isfile(file_path):
-			data = open(item.get_desktop_file_path()).read()
-			open(file_path, 'w').write(data)
+			shutil.copy(item.get_desktop_file_path(), file_path)
 			self.editor._MenuEditor__addUndo([(file_type, os.path.split(file_path)[1]),])
 		else:
 			self.editor._MenuEditor__addUndo([item,])
diff --git a/Mozo/MenuEditor.py b/Mozo/MenuEditor.py
index 0e9652f..512e7ef 100644
--- a/Mozo/MenuEditor.py
+++ b/Mozo/MenuEditor.py
@@ -16,6 +16,7 @@
 #   License along with this library; if not, write to the Free Software
 #   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
+import codecs
 import os
 import re
 import xml.dom.minidom
@@ -68,9 +69,9 @@ class MenuEditor:
 
 	def save(self, from_loading=False):
 		for menu in ('applications', 'settings'):
-			fd = open(getattr(self, menu).path, 'w')
-			fd.write(re.sub("\n[\s]*([^\n<]*)\n[\s]*</", "\\1</", getattr(self, menu).dom.toprettyxml().replace('<?xml version="1.0" ?>\n', '')))
-			fd.close()
+			xml = getattr(self, menu).dom.toprettyxml().replace('<?xml version="1.0" ?>\n', '')
+			with codecs.open(getattr(self, menu).path, 'w', 'utf-8') as f:
+				f.write(re.sub("\n[\s]*([^\n<]*)\n[\s]*</", "\\1</", xml))
 		if not from_loading:
 			self.__loadMenus()
 
@@ -123,13 +124,23 @@ class MenuEditor:
 		for file_path in files:
 			new_path = file_path.rsplit('.', 1)[0]
 			redo_path = util.getUniqueRedoFile(new_path)
-			data = open(new_path).read()
-			open(redo_path, 'w').write(data)
-			data = open(file_path).read()
-			open(new_path, 'w').write(data)
+
+			f_file_path = codecs.open(new_path, 'r', 'utf-8')
+			f_new_path = codecs.open(new_path, 'rw', 'utf-8')
+			f_redo_path = codecs.open(redo_path, 'rw', 'utf-8')
+
+			data = f_new_path.read()
+			f_redo_path.write(data)
+			data = f_file_path.read()
+			f_new_path.write(data)
+
+			f_file_path.close()
+			f_new_path.close()
+			f_redo_path.close()
+
 			os.unlink(file_path)
 			redo.append(redo_path)
-		#reload DOM to make changes stick
+		# reload DOM to make changes stick
 		for name in ('applications', 'settings'):
 			menu = getattr(self, name)
 			try:
@@ -147,12 +158,18 @@ class MenuEditor:
 		for file_path in files:
 			new_path = file_path.rsplit('.', 1)[0]
 			undo_path = util.getUniqueUndoFile(new_path)
-			data = open(new_path).read()
-			open(undo_path, 'w').write(data)
-			data = open(file_path).read()
-			open(new_path, 'w').write(data)
+			f_file_path = codecs.open(new_path, 'r', 'utf-8')
+			f_new_path = codecs.open(new_path, 'rw', 'utf-8')
+			f_undo_path = codecs.open(undo_path, 'rw', 'utf-8')
+			data = f_new_path.read()
+			f_undo_path.write(data)
+			data = f_file_path.read()
+			f_new_path.write(data)
 			os.unlink(file_path)
 			undo.append(undo_path)
+			f_file_path.close()
+			f_new_path.close()
+			f_undo_path.close()
 		#reload DOM to make changes stick
 		for name in ('applications', 'settings'):
 			menu = getattr(self, name)
@@ -428,9 +445,11 @@ class MenuEditor:
 					file_path = item.get_desktop_file_path()
 			else:
 				continue
-			data = open(file_path).read()
+			with codecs.open(file_path, 'r', 'utf-8') as f:
+				data = f.read()
 			undo_path = util.getUniqueUndoFile(file_path)
-			open(undo_path, 'w').write(data)
+			with codecs.open(undo_path, 'w', 'utf-8') as f:
+				f.write(data)
 			self.__undo[-1].append(undo_path)
 
 	def __getMenu(self, item):
-- 
cgit v1.2.1