Skip to content

Commit 340fb22

Browse files
committed
dock config now written to (and can be restored from) xml file ~/.config/mate_dock_applet.conf
1 parent 9260070 commit 340fb22

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

src/dock_xml.in

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#!/usr/bin/env python3
2+
3+
"""Provide functionality allowing the dock applets configuration to be saved
4+
and loaded
5+
6+
Store the configuration in a specified XML file
7+
8+
The file will contain the following information:
9+
: a list of all pinned app's .desktop files
10+
: the indicator type (light or dark)
11+
: whether unpinned apps from all workspaces are to be displayed
12+
: whether an indicator for each open window is to be displayed
13+
"""
14+
15+
# Copyright (C) 1997-2003 Free Software Foundation, Inc.
16+
#
17+
# This program is free software; you can redistribute it and/or
18+
# modify it under the terms of the GNU General Public License as
19+
# published by the Free Software Foundation; either version 2 of the
20+
# License, or (at your option) any later version.
21+
#
22+
# This program is distributed in the hope that it will be useful, but
23+
# WITHOUT ANY WARRANTY; without even the implied warranty of
24+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25+
# General Public License for more details.
26+
#
27+
# You should have received a copy of the GNU General Public License
28+
# along with this program; if not, write to the Free Software
29+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
30+
# 02110-1301, USA.
31+
#
32+
# Author:
33+
# Robin Thompson
34+
35+
import xml.etree.ElementTree as ET
36+
37+
def write_xml (filename, desktop_files, light_ind, unpinned_from_all, multi_ind):
38+
""" Write the xml file using the specified information
39+
40+
The xml file will be in the following format:
41+
42+
<root>
43+
<pinned_apps>
44+
<desktop_file name="name1"/>
45+
<desktop_file name="name2"/>
46+
etc...
47+
</pinned_apps>
48+
<light_ind>int</>
49+
<unpinned_from_all>True or False</>
50+
<multi_ind>True or False</>
51+
</root>
52+
53+
Args:
54+
filename - the filename to use. If the file already exists it will be
55+
overwritten.
56+
desktop_files: a list containing the names of the applet's pinned app's
57+
.desktop files e.g. ['pluma.desktop']
58+
light_ind : boolean - Whether or not a light indicator is to be used
59+
unpinned_from_all : boolean - Whethr or not unpinned apps from all workspaces
60+
are to be shown
61+
multi_ind : Whether indicators for each of an app's open windows are to be
62+
shown
63+
64+
Returns:
65+
Boolean - True if the file was written successfully, False otherwise
66+
67+
"""
68+
69+
root = ET.Element("root")
70+
pa_el = ET.Element ("pinned_apps")
71+
72+
for df in desktop_files:
73+
df_el = ET.Element("desktop_file", name = df)
74+
pa_el.append(df_el)
75+
76+
ind_el = ET.Element("ind_type", light = "%d" %light_ind)
77+
ufa_el = ET.Element("unpinned_from_all", show_all = "%s" %unpinned_from_all)
78+
mi_el = ET.Element("multi_ind", show_multi = "%s" %multi_ind)
79+
80+
root.append (pa_el)
81+
root.append (ind_el)
82+
root.append (ufa_el)
83+
root.append (mi_el)
84+
85+
try:
86+
ET.ElementTree(root).write(filename, xml_declaration=True)
87+
except FileNotFoundError:
88+
return False # invalid file or path name
89+
90+
return True
91+
92+
def read_xml (filename):
93+
""" Reads an xml file created using the write_xml method
94+
95+
Args:
96+
filename - the filename to read.
97+
98+
Returns:
99+
boolean : True if the file was read successfully, False otherwise
100+
101+
A tuple containing the following:
102+
a list of the .desktop files in the file (i.e. the pinned apps)
103+
an interger - the light indicator setting
104+
a boolean - the unpinned from all setting
105+
a boolean - the multiple indicators setting
106+
107+
"""
108+
109+
try:
110+
root = ET.parse(filename)
111+
except FileNotFoundError:
112+
return [False]
113+
114+
df_list = []
115+
pinned_apps = root.find("pinned_apps")
116+
if pinned_apps is not None:
117+
for df in pinned_apps:
118+
df_list.append(df.get("name"))
119+
120+
ind_el = root.find("ind_type")
121+
if ind_el is not None:
122+
light_ind = int(ind_el.get("light"))
123+
else:
124+
return [False] # indicator type should always be there...
125+
126+
ufa_el = root.find("unpinned_from_all")
127+
if ufa_el is not None:
128+
show_all = ufa_el.get("show_all") == "True"
129+
else:
130+
return [False]
131+
132+
mi_el = root.find("multi_ind")
133+
if mi_el is not None:
134+
multi_ind = mi_el.get("show_multi") == "True"
135+
else:
136+
return [False]
137+
138+
return [True, df_list, light_ind, show_all, multi_ind]
139+
140+
def main():
141+
"""Main function.
142+
143+
Debugging code can go here
144+
"""
145+
146+
write_xml ("/home/robin/tmp/text.xml", ["thing.desktop","blah.desktop"], 99, False, True)
147+
results = read_xml ("/home/robin/tmp/text.xml")
148+
if results[0] == True:
149+
for df in results[1]:
150+
print ("Desktop file found: %s" %df)
151+
152+
print ("Use light ind = %d" %results[2])
153+
print ("Show unpinned on all = %s" %results[3])
154+
print ("Multi ind = %s" %results[4])
155+
else:
156+
print ("Error reading file....")
157+
158+
if __name__ == "__main__":
159+
main()

0 commit comments

Comments
 (0)