-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathunity-opera.py
More file actions
355 lines (315 loc) · 10.4 KB
/
Copy pathunity-opera.py
File metadata and controls
355 lines (315 loc) · 10.4 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
########################################################
# Unity Opera
#
# Author: Kyle Baker (kyleabaker.com)
# Description: Provides several features for Unity users
# who also use Opera or Opera Next that are
# not available by default.
# Version: 2011-05-07
# Help: python unity-opera.py --help
########################################################
from gi.repository import Unity, Gio, GObject, Dbusmenu
from cStringIO import StringIO
import sys, os, commands, subprocess, re
loop = GObject.MainLoop()
# Global variables
home = os.getenv("HOME")
current_tabs = 0
current_speeddial = ""
tab_count_changed = False
is_first_check = True
bool_quicklist = False
bool_speeddial = False
bool_count_tabs = False
bool_urgency = False
bool_progress_bar = False
# Set version of Opera here from command args or assume opera (opera, opera-next)
# format: python unity-opera.py (opera or opera-next) (-qcup)
if len(sys.argv) > 2:
opera = sys.argv[1]
if "q" in sys.argv[2]:
bool_quicklist = True
if "s" in sys.argv[2]:
bool_speeddial = True
if "c" in sys.argv[2]:
bool_count_tabs = True
if "u" in sys.argv[2]:
bool_urgency = True
if "p" in sys.argv[2]:
bool_progress_bar = True
elif len(sys.argv) > 1:
if "--help" in sys.argv[1]:
print "Usage: python unity-opera.py [opera channel] [options]\n"
print "Opera channels:\n"
print " opera"
print " opera-next\n"
print "Options:\n"
print " q Enable basic quicklist"
print " s Enable Speed Dial entries in quicklist"
print " c Enable tab count"
print " u Enable urgency notification"
print " p Enable progress bar for downloads\n"
print "Notes:\n"
print " * Opera channels and Options are both optional"
print " * Options requires use of Opera channels arg"
print " * Progress bar for downloads is not functional at"
print " this time and may not be possible.\n"
print "Example usage:\n"
print " Use all available features"
print " * python unity-opera.py\n"
print " Count tabs and use urgency notification"
print " * python unity-opera.py opera -cu\n"
print " Use Opera Next and only Quicklist with Speed Dial entries"
print " * python unity-opera.py opera-next -qs"
exit()
opera = sys.argv[1]
bool_quicklist = True
bool_speeddial = True
bool_count_tabs = True
bool_urgency = True
bool_progress_bar = True
else:
opera = "opera"
bool_quicklist = True
bool_speeddial = True
bool_count_tabs = True
bool_urgency = True
bool_progress_bar = True
# Pretend to be opera
launcher = Unity.LauncherEntry.get_for_desktop_id (opera + "-browser.desktop")
########################################################
# is_opera_running()
#
# Description: Returns boolean True if Opera is running
########################################################
def is_opera_running():
output = commands.getoutput("ps -A | grep '" + opera + "' | awk '{print $4}'").split('\n')
i = 0
while i < len(output):
if output[i] == opera:
return True
i = i + 1
pass # do something
return False
########################################################
# menu_open_new_tab(a, b)
# menu_open_new_private_tab(a, b)
# menu_open_new_window(a, b)
# menu_open_speeddial_item(a, b, url)
# update_quicklist()
#
# Description: List of functions for quicklist menu
########################################################
def menu_open_new_tab(a, b):
os.popen3(opera + " -newtab")
def menu_open_new_private_tab(a, b):
os.popen3(opera + " -newprivatetab")
def menu_open_new_window(a, b):
os.popen3(opera + " -newwindow")
def menu_open_mail(a, b):
os.popen3(opera + " -mail")
#TODO: Fix this command so it actually opens M2
def menu_open_speeddial_item(a, b, url):
os.popen3(opera + " " + url)
def update_quicklist():
# Set quicklist menu items from speeddial
global current_speeddial, bool_quicklist
title = ""
url = ""
# Make sure the speed dial file exists before attempting to read it
if not os.path.isfile(home + "/." + opera + "/speeddial.ini"):
print "Error: Unable to open " + home + "/." + opera + "/speeddial.ini"
exit()
else:
if bool_speeddial:
try:
file = open(home + "/." + opera + "/speeddial.ini")
temp = file.read()
if temp == current_speeddial:
return True
else:
current_speeddial = temp
print "Updating Quicklist with Speed Dial entries:"
file.close()
except IOError:
pass
#TODO: Clear items previously added to menu so we can add new updated items
# Set default quicklist items
ql = Dbusmenu.Menuitem.new()
item1 = Dbusmenu.Menuitem.new()
item1.property_set (Dbusmenu.MENUITEM_PROP_LABEL, "New Tab")
item1.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE, True)
item1.connect ("item-activated", menu_open_new_tab)
item2 = Dbusmenu.Menuitem.new()
item2.property_set (Dbusmenu.MENUITEM_PROP_LABEL, "New Private Tab")
item2.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE, True)
item2.connect ("item-activated", menu_open_new_private_tab)
item3 = Dbusmenu.Menuitem.new()
item3.property_set (Dbusmenu.MENUITEM_PROP_LABEL, "New Window")
item3.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE, True)
item3.connect ("item-activated", menu_open_new_window)
ql.child_append (item1)
ql.child_append (item2)
ql.child_append (item3)
# Add Mail to menu if an account exists
if os.path.isfile(home + "/." + opera + "/mail/accounts.ini"):
file = open(home + "/." + opera + "/mail/accounts.ini")
if "Count=" in file.read():
item4 = Dbusmenu.Menuitem.new()
item4.property_set (Dbusmenu.MENUITEM_PROP_LABEL, "Mail")
item4.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE, True)
item4.connect ("item-activated", menu_open_mail)
ql.child_append (item4)
if bool_speeddial:
# Reread speeddial.ini since it was flush on the diff
file = open(home + "/." + opera + "/speeddial.ini")
# Set Speed Dial menu items for quicklist
while 1:
line = file.readline()
if not line:
break
if "Custom Title=" in line:
pass
elif "Title=" in line:
title = str(line[line.find("=")+1:len(line)]).rstrip('\n')
elif "Url=" in line:
url = str(line[line.find("=")+1:len(line)]).rstrip('\n')
item5 = Dbusmenu.Menuitem.new ()
item5.property_set (Dbusmenu.MENUITEM_PROP_LABEL, title)
item5.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE, True)
item5.connect ("item-activated", menu_open_speeddial_item, url)
ql.child_append (item5)
print " " + title + " " + url
pass # do something
file.close
# Activate quicklist
launcher.set_property("quicklist", ql)
# If current_speeddial is empty, then its disabled. Change it to stop processing
if current_speeddial == "":
bool_quicklist = False
########################################################
# update_tabs()
#
# Description: Get number of open tabs across all windows
########################################################
def update_tabs():
global current_tabs, tab_count_changed
tabs = 0
windows = 0
# Make sure the session file exists before attempting to read it
if not os.path.isfile(home + "/." + opera + "/sessions/autosave.win"):
print "Error: Unable to open " + home + "/." + opera + "/sessions/autosave.win"
exit()
try:
file = open(home + "/." + opera + "/sessions/autosave.win")
except IOError:
pass
while 1:
line = file.readline()
if not line:
break
if "window count" in line:
tabs = int(line[line.find("=")+1:len(line)])
elif "type=0" in line:
windows = windows + 1
pass # do something
file.close
tabs = tabs - windows
# Set number of open tabs across all windows
if tabs == current_tabs:
tab_count_changed = False
return True
elif tabs > 0:
if bool_count_tabs:
launcher.set_property("count", tabs)
launcher.set_property("count_visible", True)
if tabs > current_tabs:
tab_count_changed = True
current_tabs = tabs
print "Updating tab count"
else:
launcher.set_property("count", 0)
launcher.set_property("count_visible", False)
current_tabs = 0
tab_count_changed = False
return True
########################################################
# update_progress()
#
# Description: Get number of open tabs across all windows
########################################################
def update_progress():
return True
# # Set progress to 42% done
# launcher.set_property("progress", 0.42)
# launcher.set_property("progress_visible", True)
########################################################
# update_progress()
#
# Description: Get number of open tabs across all windows
########################################################
def update_urgency():
if not is_opera_focused():
if tab_count_changed:
launcher.set_property("urgent", True)
return
else:
launcher.set_property("urgent", False)
########################################################
# get_active_window_title()
#
# Description: Get the currently focused window. Used by
# update_urgency() to check for Opera.
########################################################
def is_opera_focused():
global is_first_check
if is_first_check:
is_first_check = False
return True
root_check = ''
root = subprocess.Popen(['xprop', '-root'], stdout=subprocess.PIPE)
if root.stdout != root_check:
root_check = root.stdout
for i in root.stdout:
if '_NET_ACTIVE_WINDOW(WINDOW):' in i:
id_ = i.split()[4]
id_w = subprocess.Popen(['xprop', '-id', id_], stdout=subprocess.PIPE)
id_w.wait()
buff = []
for j in id_w.stdout:
buff.append(j)
for line in buff:
match = re.match("WM_NAME\((?P<type>.+)\) = (?P<name>.+)", line)
if match != None:
type = match.group("type")
if type == "STRING" or type == "COMPOUND_TEXT":
if " - Opera" in match.group("name"):
return True
return False
########################################################
# get_updates()
#
# Description: Check for updates to apply
########################################################
def get_updates():
global current_tabs, is_first_check
if bool_quicklist:
update_quicklist()
if not is_opera_running():
launcher.set_property("count_visible", False)
launcher.set_property("urgent", False)
#initialize some settings so it works properly next time Opera's opened
current_tabs = 0
is_first_check = True
return True
else:
if bool_count_tabs or bool_urgency:
update_tabs()
#if bool_progress_bar:
#update_progress()
if bool_urgency:
update_urgency()
return True
# Call tab updates
GObject.timeout_add_seconds(1, get_updates)
loop.run()