Skip to content

Commit 1c51eb5

Browse files
committed
feat: add show notifications settings
1 parent df88337 commit 1c51eb5

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

micindicator.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from gi.repository import Keybinder
1919
from threading import Timer
2020
from gi.repository import GdkPixbuf
21+
from settings import Settings
2122

2223

2324
APPINDICATOR_ID = 'micmuteindicator'
@@ -26,6 +27,7 @@
2627
class Indicator():
2728

2829
def __init__(self):
30+
self.settings = Settings()
2931
self.indicator = appindicator.Indicator.new(APPINDICATOR_ID, self.get_current_state_icon(), appindicator.IndicatorCategory.SYSTEM_SERVICES)
3032
self.indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
3133
self.indicator.set_menu(self.build_menu())
@@ -41,15 +43,15 @@ def __init__(self):
4143
def show_about_dialog(self, _):
4244
aboutdialog = Gtk.AboutDialog()
4345

44-
authors = ["Sidnei Bernardo Junior"]
46+
authors = ["Sidnei Bernardo Junior", "AXeL-dev"]
4547
documenters = ["Sidnei Bernardo Junior"]
4648

4749
aboutdialog.set_program_name("Microphone AppIndicator for Ubuntu")
4850
aboutdialog.set_comments("AppIndicator to mute and show microphone mute status.")
4951
aboutdialog.set_logo(GdkPixbuf.Pixbuf.new_from_file(self.get_resource("icon.svg")))
5052
aboutdialog.set_authors(authors)
5153
aboutdialog.set_documenters(documenters)
52-
aboutdialog.set_website("https://github.com/sidneibjunior/microphone-indicator")
54+
aboutdialog.set_website("https://github.com/LinuxForGeeks/microphone-indicator")
5355
aboutdialog.set_website_label("Source code at GitHub")
5456
aboutdialog.connect("response", self.close_about_dialog)
5557

@@ -85,6 +87,17 @@ def build_menu(self):
8587
self.item_toggle.connect('activate', self.toggle_mic)
8688
menu.append(self.item_toggle)
8789

90+
item_settings = Gtk.MenuItem('Settings')
91+
menu_settings = Gtk.Menu()
92+
item_settings.set_submenu(menu_settings)
93+
menu.append(item_settings)
94+
95+
self.item_show_notifications = Gtk.CheckMenuItem('Show notifications')
96+
if self.settings.show_notifications:
97+
self.item_show_notifications.set_active(True)
98+
self.item_show_notifications.connect('activate', self.toggle_show_notifications)
99+
menu_settings.append(self.item_show_notifications)
100+
88101
self.item_about = Gtk.MenuItem('About')
89102
self.item_about.connect('activate', self.show_about_dialog)
90103
menu.append(self.item_about)
@@ -96,6 +109,10 @@ def build_menu(self):
96109
menu.show_all()
97110
return menu
98111

112+
def toggle_show_notifications(self, widget):
113+
self.settings.show_notifications = not self.settings.show_notifications
114+
self.settings.save()
115+
99116
def update_mic_state(self):
100117
self.update_menu_toggle_label()
101118
self.indicator.set_icon(self.get_current_state_icon())
@@ -111,8 +128,10 @@ def toggle_mic(self, _):
111128
self.update_mic_state()
112129

113130
self.show_toggle_notification()
114-
131+
115132
def show_toggle_notification(self):
133+
if not self.settings.show_notifications:
134+
return
116135
self.notification = Notify.Notification.new("Notify")
117136
title = ""
118137
if self.get_current_mic_state() == "[off]":
@@ -126,7 +145,7 @@ def show_toggle_notification(self):
126145
# creates a timer to close the notification as the 'set_timeout' Notify method is ignored by the server.
127146
t = Timer(1.0, self.close_toggle_notification)
128147
t.start()
129-
148+
130149
def close_toggle_notification(self):
131150
self.notification.close()
132151

settings.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/python3
2+
3+
import configparser
4+
import os
5+
import datetime
6+
7+
class Settings:
8+
def __init__(self):
9+
print ("DEBUG: Initializing the Settings module @", (str(datetime.datetime.now())))
10+
11+
cparse = configparser.ConfigParser()
12+
cparse.read([os.path.expanduser('~/.micindicator')])
13+
14+
try:
15+
self._shownotifications = cparse.get('DEFAULT', 'show-notifications')
16+
17+
except configparser.NoOptionError:
18+
print ("DEBUG: No configration file using default settings")
19+
self._shownotifications = '1'
20+
self.save()
21+
22+
except ValueError:
23+
print ("DEBUG: Problem while reading setting file, using the default settings")
24+
os.system("rm ~/.micindicator")
25+
self._shownotifications = '1'
26+
self.save()
27+
28+
## Functions to get and set settings
29+
@property
30+
def show_notifications(self):
31+
#print ("DEBUG: getting show notifications settings @", (str(datetime.datetime.now())))
32+
if self._shownotifications == '1':
33+
return True
34+
else:
35+
return False
36+
37+
@show_notifications.setter
38+
def show_notifications(self, data):
39+
#print ("DEBUG: setting show notifications settings @", (str(datetime.datetime.now())))
40+
if data == True:
41+
self._shownotifications = '1'
42+
else:
43+
self._shownotifications = '0'
44+
45+
## Function to save the settings
46+
def save(self):
47+
print ("DEBUG: saving settings file @", (str(datetime.datetime.now())))
48+
config = open(os.path.expanduser('~/.micindicator'), 'w')
49+
Text='''# Mic. Indicator Settings File
50+
[DEFAULT]
51+
# Should notifications be enabled
52+
show-notifications = %s
53+
''' % (self._shownotifications)
54+
config.write(Text)
55+
config.close()

0 commit comments

Comments
 (0)