forked from linuxmint/cinnamon
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cinnamon-add-panel-launcher.py
executable file
·154 lines (126 loc) · 4.7 KB
/
cinnamon-add-panel-launcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#! /usr/bin/python -OOt
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import Gio
from os.path import expanduser
from time import sleep as wait
import os
import os.path
import inspect
import sys
import gettext
gettext.install("cinnamon", "/usr/share/cinnamon/locale")
Settings = Gio.Settings.new("org.cinnamon")
_ = gettext.gettext
class Namespace: pass
iface = Namespace()
oldDesktopName = ""
newDesktopName = ""
appName = ""
appPath = ""
iconPath = ""
editMode = len(sys.argv) > 1
if editMode:
oldDesktopName = sys.argv[1]
appName = sys.argv[2]
appPath = sys.argv[3]
iconPath = sys.argv[4]
def updatePreviewIcon(name):
global iface
if os.path.exists(name):
iface.preview_icon.set_from_file(name)
else:
iface.preview_icon.set_from_icon_name(name, 6)
def editOrAddLaunchers():
makeLauncher()
global Settings, desktopName
desktopFiles = Settings.get_strv('panel-launchers')
if not editMode:
desktopFiles.append(newDesktopName)
else:
i = desktopFiles.index(oldDesktopName)
if i >= 0:
del desktopFiles[i]
desktopFiles.insert(i, newDesktopName)
Settings.set_strv('panel-launchers', desktopFiles)
Gtk.main_quit(None)
def makeLauncher():
global appName, appPath, iconPath, custom_launchers_path, newDesktopName
description = _("Custom Launcher")
i = 1
dir = Gio.file_new_for_path(custom_launchers_path)
if not dir.query_exists(None):
dir.make_directory_with_parents(None)
file = Gio.file_parse_name(custom_launchers_path + '/cinnamon-custom-launcher-' + str(i) + '.desktop')
while file.query_exists(None):
i = i + 1
file = Gio.file_parse_name(custom_launchers_path + '/cinnamon-custom-launcher-' + str(i) + '.desktop')
file = open(custom_launchers_path+ '/cinnamon-custom-launcher-' + str(i) + '.desktop', "w")
desktopEntry = "[Desktop Entry]\nName=" + appName + "\nExec=" + appPath + "\nType=Application\n"
desktopEntry = desktopEntry + "Description=" + description + "\n"
if iconPath == "":
iconPath = "application-x-executable"
desktopEntry += "Icon=" + iconPath + "\n"
print desktopEntry
file.write(desktopEntry)
file.close()
newDesktopName = 'cinnamon-custom-launcher-' + str(i) + '.desktop'
class Handler:
def onDeleteWindow(self, *args):
Gtk.main_quit(*args)
def onAdd(self, button):
global appPath, appName
if appPath == "" or appName == "":
return
else:
editOrAddLaunchers()
def onIconPicked(self, *args):
global iconPath, iface
iconPath = iface.icon_picker.get_uri()[7:]
iface.icon_path.set_text(iconPath)
updatePreviewIcon(iconPath)
def onAppPicked(self, *args):
global appPath, iface
appPath = iface.app_picker.get_uri()[7:]
iface.file_path.set_text(appPath)
def onNameChanged(self, *args):
global appName, iface
appName = iface.app_name.get_text().strip()
def onAppChanged(self, *args):
global appPath, iface
appPath = iface.file_path.get_text().strip()
def onIconChanged(self, *args):
global iconPath, iface
iconPath = iface.icon_path.get_text().strip()
updatePreviewIcon(iconPath)
builder = Gtk.Builder()
userhome = expanduser("~")
custom_launchers_path = userhome + "/.cinnamon/panel-launchers"
applet_dir = os.path.dirname(inspect.getfile(inspect.currentframe()))
builder.add_from_file(applet_dir + "/cinnamon-add-panel-launcher.glade")
window = builder.get_object("add-panel-launcher-dialog")
builder.connect_signals(Handler())
iface.add_button = builder.get_object("add_button")
iface.cancel_button = builder.get_object("cancel_button")
iface.preview_icon = builder.get_object("icon")
iface.app_name = builder.get_object("app_name")
iface.file_path = builder.get_object("app_path")
iface.icon_path = builder.get_object("icon_path")
iface.app_picker = builder.get_object("app_picker")
iface.icon_picker = builder.get_object("icon_picker")
# set static translations (labels, etc..)
builder.get_object("name_label").set_markup(_("Name"))
builder.get_object("application_label").set_markup(_("Application"))
builder.get_object("icon_label").set_markup(_("Icon"))
builder.get_object("cancel_button").set_label(_("Cancel"))
builder.get_object("add-panel-launcher-dialog").set_title(_("Add panel launcher..."))
if editMode:
iface.app_name.set_text(appName)
iface.file_path.set_text(appPath)
iface.icon_path.set_text(iconPath)
iface.add_button.set_label(_("Update"))
updatePreviewIcon(iconPath)
else:
iface.add_button.set_label(_("Add"))
window.show_all()
Gtk.main()