Use gsettings for recent and favorite apps

during migration,
- rename the old favorites file
- delete the old recent file
This commit is contained in:
Michael Webster 2020-03-30 19:00:31 -04:00
parent d694deb65e
commit 6f69d0e499
5 changed files with 85 additions and 64 deletions

View File

@ -245,6 +245,9 @@ class pluginclass(object):
try: try:
# GSettings stuff # GSettings stuff
self.settings = Gio.Settings("com.linuxmint.mintmenu.plugins.applications") self.settings = Gio.Settings("com.linuxmint.mintmenu.plugins.applications")
self.migrate_favorites()
self.GetGSettingsEntries() self.GetGSettingsEntries()
self.settings.connect("changed::icon-size", self.changeIconSize) self.settings.connect("changed::icon-size", self.changeIconSize)
self.settings.connect("changed::favicon-size", self.changeFavIconSize) self.settings.connect("changed::favicon-size", self.changeFavIconSize)
@ -264,6 +267,7 @@ class pluginclass(object):
self.settings.connect("changed::enable-internet-search", self.GetGSettingsEntries) self.settings.connect("changed::enable-internet-search", self.GetGSettingsEntries)
self.settings.connect("changed::search-command", self.GetGSettingsEntries) self.settings.connect("changed::search-command", self.GetGSettingsEntries)
self.settings.connect("changed::default-tab", self.GetGSettingsEntries) self.settings.connect("changed::default-tab", self.GetGSettingsEntries)
self.settings.connect("changed::favorite-apps-list", self.favoriteAppsChanged)
except Exception as e: except Exception as e:
print(e) print(e)
@ -519,16 +523,6 @@ class pluginclass(object):
def RebuildPlugin(self): def RebuildPlugin(self):
self.content_holder.set_size_request(self.width, self.height) self.content_holder.set_size_request(self.width, self.height)
def checkMintMenuFolder(self):
if os.path.exists(os.path.join(os.path.expanduser("~"), ".linuxmint", "mintMenu", "applications")):
return True
try:
os.makedirs(os.path.join(os.path.expanduser("~"), ".linuxmint", "mintMenu", "applications"))
return True
except:
pass
return False
def onShowMenu(self): def onShowMenu(self):
if self.favorites: if self.favorites:
if self.defaultTab == -1: if self.defaultTab == -1:
@ -1245,26 +1239,44 @@ class pluginclass(object):
return None return None
def migrate_favorites(self):
if self.settings.get_strv("favorite-apps-list") != []:
return
default_path = os.path.join("/usr/linuxmint/mintMenu/applications.list")
path = os.path.join(home, ".linuxmint/mintMenu/applications.list")
if os.path.isdir(path):
# dir created by a bug in mint 19.2 beta
os.system("rm -rf %s" % path)
return
if not os.path.exists(path):
path = default_path
with open(path) as f:
self.settings.set_strv("favorite-apps-list", f.readlines())
try:
os.replace(path, path + ".deprecated_uses_dconf_now")
except:
# This will fail if it was the default path, ignore it.
pass
def favoriteAppsChanged(self, setting, key):
self.buildFavorites()
def buildFavorites(self): def buildFavorites(self):
try: try:
path = os.path.join(home, ".linuxmint/mintMenu/applications.list") faves = self.settings.get_strv("favorite-apps-list")
if os.path.isdir(path):
# dir created by a bug in mint 19.2 beta
os.system("rm -rf %s" % path)
if not os.path.exists(path):
os.system("mkdir -p ~/.linuxmint/mintMenu")
os.system("cp /usr/lib/linuxmint/mintMenu/applications.list " + path)
applicationsList = open(path).readlines()
self.favorites = []
for child in self.favoritesBox: for child in self.favoritesBox:
child.destroy() child.destroy()
position = 0 position = 0
self.favorites = []
for app in applicationsList: for app in faves:
try: try:
app = app.strip() app = app.strip()
@ -1296,7 +1308,6 @@ class pluginclass(object):
print("Can't add favorite: %s" % app) print("Can't add favorite: %s" % app)
print (e) print (e)
self.favoritesSave()
except Exception as e: except Exception as e:
print(e) print(e)
@ -1382,20 +1393,15 @@ class pluginclass(object):
self.favoritesRemove(fav.position) self.favoritesRemove(fav.position)
def favoritesSave(self): def favoritesSave(self):
try: new_faves = []
self.checkMintMenuFolder()
with open(os.path.join(home, ".linuxmint/mintMenu/applications.list") , "w") as appListFile: for favorite in self.favorites:
for favorite in self.favorites: if favorite.type == "location":
if favorite.type == "location": new_faves.append("location:" + favorite.desktopFile)
appListFile.write("location:" + favorite.desktopFile + "\n") else:
else: new_faves.append(favorite.type)
appListFile.write(favorite.type + "\n")
except Exception as e: self.settings.set_strv("favorite-apps-list", new_faves)
msgDlg = Gtk.MessageDialog(None, Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK,
_("Couldn't save favorites. Check if you have write access to ~/.linuxmint/mintMenu") +
"\n(" + e.__str__() + ")")
msgDlg.run()
msgDlg.destroy()
def isLocationInFavorites(self, location): def isLocationInFavorites(self, location):
for fav in self.favorites: for fav in self.favorites:

View File

@ -388,13 +388,6 @@ class ApplicationLauncher(easyButton):
if os.path.exists(self.startupFilePath): if os.path.exists(self.startupFilePath):
os.remove(self.startupFilePath) os.remove(self.startupFilePath)
def addToFavourites(self):
favouritesDir = os.path.join(os.path.expanduser("~"), ".linuxmint/mintMenu/applications")
if not os.path.exists(favouritesDir):
os.makedirs(favouritesDir)
shutil.copyfile(self.desktopFile, self.favouritesFilePath)
def removeFromFavourites(self): def removeFromFavourites(self):
if os.path.exists(self.favouritesFilePath): if os.path.exists(self.favouritesFilePath):
os.remove(self.favouritesFilePath) os.remove(self.favouritesFilePath)

View File

@ -12,6 +12,8 @@ from gi.repository import Gtk, Gio
import plugins.recentHelper as RecentHelper import plugins.recentHelper as RecentHelper
from plugins.execute import Execute from plugins.execute import Execute
home = os.path.expanduser("~")
# i18n # i18n
gettext.install("mintmenu", "/usr/share/linuxmint/locale") gettext.install("mintmenu", "/usr/share/linuxmint/locale")
locale.bindtextdomain("mintmenu", "/usr/share/linuxmint/locale") locale.bindtextdomain("mintmenu", "/usr/share/linuxmint/locale")
@ -56,6 +58,9 @@ class pluginclass:
self.icon = 'mate-folder.png' self.icon = 'mate-folder.png'
self.settings = Gio.Settings("com.linuxmint.mintmenu.plugins.recent") self.settings = Gio.Settings("com.linuxmint.mintmenu.plugins.recent")
RecentHelper.settings = self.settings
self.migrate_recent_apps()
self.settings.connect('changed', self.RegenPlugin) self.settings.connect('changed', self.RegenPlugin)
self.appSettings = Gio.Settings("com.linuxmint.mintmenu.plugins.applications") self.appSettings = Gio.Settings("com.linuxmint.mintmenu.plugins.applications")
@ -87,6 +92,20 @@ class pluginclass:
def RegenPlugin(self, *args, **kargs): def RegenPlugin(self, *args, **kargs):
self.GetGSettingsEntries() self.GetGSettingsEntries()
def migrate_recent_apps(self):
if self.settings.get_strv("recent-apps-list") != []:
return
path = os.path.join(home, ".linuxmint/mintMenu/recentApplications.list")
if os.path.exists(path):
with open(path) as f:
self.settings.set_strv("recent-apps-list", f.readlines())
try:
os.unlink(path)
except:
pass
def GetGSettingsEntries(self): def GetGSettingsEntries(self):
self.recenth = self.settings.get_int("height") self.recenth = self.settings.get_int("height")
self.recentw = self.settings.get_int("width") self.recentw = self.settings.get_int("width")

View File

@ -10,6 +10,8 @@ from plugins.easybuttons import ApplicationLauncher
home = os.path.expanduser("~") home = os.path.expanduser("~")
recentApps = [] recentApps = []
settings = None # set by recent plugin
mintMenuWin = None mintMenuWin = None
recentAppBox = None recentAppBox = None
numentries = 10 numentries = 10
@ -25,21 +27,15 @@ def recentAppsAdd(recentAppsButton):
counter = counter + 1 counter = counter + 1
def recentAppsSave(): def recentAppsSave():
try: new_recent_apps = []
path = os.path.join(home, ".linuxmint/mintMenu/recentApplications.list")
with open(path, "w") as recentAppListFile:
for recentApp in recentApps:
if not hasattr(recentApp, "type") or recentApp.type == "location":
recentAppListFile.write("location:" + recentApp.desktopFile + "\n")
else:
recentAppListFile.write(recentApp.type + "\n")
except Exception as e: for recentApp in recentApps:
print(e) if not hasattr(recentApp, "type") or recentApp.type == "location":
msgDlg = Gtk.MessageDialog(None, Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, new_recent_apps.append("location:" + recentApp.desktopFile)
_("Couldn't save recent apps. Check if you have write access to ~/.linuxmint/mintMenu")+"\n(" + e.__str__() + ")") else:
msgDlg.run() new_recent_apps.append(recentApp.type)
msgDlg.destroy()
settings.set_strv("recent-apps-list", new_recent_apps)
def recentAppBuildLauncher(location): def recentAppBuildLauncher(location):
try: try:
@ -82,13 +78,9 @@ def recentAppBuildLauncher(location):
def buildRecentApps(): def buildRecentApps():
del recentApps[:] del recentApps[:]
try: try:
path = os.path.join(home, ".linuxmint/mintMenu/recentApplications.list") recent_apps = settings.get_strv("recent-apps-list")
if not os.path.exists(path):
recentApplicationsList = []
else:
recentApplicationsList = open(path).readlines()
for app in recentApplicationsList : for app in recent_apps:
app = app.strip() app = app.strip()
if app[0:9] == "location:": if app[0:9] == "location:":

View File

@ -291,6 +291,12 @@
<summary></summary> <summary></summary>
<description></description> <description></description>
</key> </key>
<key type="as" name="favorite-apps-list">
<default>[]</default>
<summary>List of absolute desktop file paths</summary>
</key>
</schema> </schema>
<schema id="com.linuxmint.mintmenu.plugins.system_management" path="/com/linuxmint/mintmenu/plugins/system_management/"> <schema id="com.linuxmint.mintmenu.plugins.system_management" path="/com/linuxmint/mintmenu/plugins/system_management/">
@ -412,6 +418,11 @@
<description></description> <description></description>
</key> </key>
<key type="as" name="recent-apps-list">
<default>[]</default>
<summary>List of absolute desktop file paths</summary>
</key>
</schema> </schema>
</schemalist> </schemalist>