~jconti/gm-notify/messaging-menu

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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# gm-notify-config v0.10.3
# GMail Notifier Configuration Utility
#
# Copyright (c) 2009-2010, Alexander Hungenberg <alexander.hungenberg@gmail.com>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import print_function

import sys
import os
import gettext
import subprocess
import shutil

from gi.repository import Gio, Gtk

#import pygtk
#pygtk.require("2.0")
#import gtk, gtk.glade
#import gconf
from twisted.internet import gtk3reactor
gtk3reactor.install()
from twisted.internet import reactor
from twisted.words.protocols.jabber import jid

import gm_notify_keyring as keyring
from gtalk import MailChecker

_ = gettext.translation('gm-notify', fallback=True).ugettext

class PathNotFound(Exception): pass

def get_executable_path(name):
    path = "%s/%s" % (os.getcwd(), name)
    if os.path.exists(path) and os.access(path, os.X_OK): return path
    path = "/usr/local/bin/" + name
    if os.path.exists(path) and os.access(path, os.X_OK): return path
    path = "/usr/bin/" + name
    if os.path.exists(path) and os.access(path, os.X_OK): return path
    raise PathNotFound("%s not found" % name)

class Window(Gtk.Application):
    def __init__(self):
        super(Window, self).__init__(application_id="net.launchpad.gm-notify-config",
            flags=Gio.ApplicationFlags.FLAGS_NONE)
        self.window = None
        self.connect("activate", self.on_activate)

    def on_activate(self, app):
        '''Setup the application'''
        if self.window is not None:
            return

        #####
        # GUI initialization
        #####
        self.keys = keyring.Keyring("GMail", "mail.google.com", "http")
        self.client = Gio.Settings("net.launchpad.gm-notify")
        
        if os.path.exists("gm-config.ui"):
            builder_file = "gm-config.ui"
        elif os.path.exists("/usr/local/share/gm-notify/gm-config.ui"):
            builder_file = "/usr/local/share/gm-notify/gm-config.ui"
        elif os.path.exists("/usr/share/gm-notify/gm-config.ui"):
            builder_file = "/usr/share/gm-notify/gm-config.ui"

        self.wTree = Gtk.Builder.new()
        self.wTree.add_from_file(builder_file)
        self.wTree.set_translation_domain("gm-notify")
        self.window = self.wTree.get_object("gmnotify_config_main")
        self.window.show_all()
        self.add_window(self.window)

        self.wTree.get_object("notebook_main").set_current_page(0)
        
        #####
        # Init with stored values
        #####
        
        # Credentials
        if self.keys.has_credentials():
            self.creds = self.keys.get_credentials()
        else:
            self.creds = ("", "")
        self.wTree.get_object("input_user").set_text(self.creds[0])
        self.wTree.get_object("input_password").set_text(self.creds[1])
        
        self.api = MailChecker("", "")
        self.api.cb_auth_successful = self.credentials_valid
        self.api.cb_auth_failed = self.credentials_invalid
        
        self.check_credentials(None, None)
        
        # Sound
        self.wTree.get_object("checkbutton_sound").set_active(self.client.get_boolean("play-sound"))
        if self.client.get_string("soundfile"):
            self.wTree.get_object("fcbutton_sound").set_filename(self.client.get_string("soundfile"))
        self.on_checkbutton_sound_toggled(self.wTree.get_object("checkbutton_sound"))
        
        # ClickAction
        if self.client.get_boolean("openclient"):
            self.wTree.get_object("radiobutton_openclient").set_active(True)
        else:
            self.wTree.get_object("radiobutton_openweb").set_active(True)
        
        # Mailboxes
        mailboxes = self.client.get_strv("mailboxes")
        self.wTree.get_object("checkbutton_inbox").set_active(self.client.get_boolean("ignore-inbox"))
        self.wTree.get_object("entry_labels").set_text(", ".join(mailboxes))

        # Autorun
        if os.path.exists("data/gm-notify.desktop"):
            self.gm_notify_autostart_file = "data/gm-notify.desktop"
        elif os.path.exists("/usr/local/share/gm-notify/gm-notify.desktop"):
            self.gm_notify_autostart_file = "/usr/local/share/gm-notify/gm-notify.desktop"
        elif os.path.exists("/usr/share/gm-notify/gm-notify.desktop"):
            self.gm_notify_autostart_file = "/usr/share/gm-notify/gm-notify.desktop"
        self.autostart_file = os.path.expanduser("~/.config/autostart/gm-notify.desktop")
        self.wTree.get_object("checkbutton_autostart").set_active(os.path.exists(self.autostart_file))
        
        # Connect signals
        self.wTree.get_object("button_close").connect("clicked", self.terminate)
        self.wTree.get_object("button_apply").connect("clicked", self.save)
        self.wTree.get_object("input_password").connect("focus-out-event", self.check_credentials)
        self.wTree.get_object("checkbutton_sound").connect("toggled", self.on_checkbutton_sound_toggled)
    
    def save(self, widget, data=None):
        '''saves the entered data and closes the app'''
        # Credentials
        self.keys.delete_credentials()
        self.keys.set_credentials(self.wTree.get_object("input_user").get_text(), 
                                  self.wTree.get_object("input_password").get_text())
        
        # Mailboxes
        mailboxes = []
        for label in self.wTree.get_object("entry_labels").get_text().split(","):
            mailboxes.append(label.strip())
        self.client.set_strv("mailboxes", mailboxes)
        self.client.set_boolean("ignore-inbox", self.wTree.get_object("checkbutton_inbox").get_active())
        
        # ClickAction
        self.client.set_boolean("openclient", self.wTree.get_object("radiobutton_openclient").get_active())

        # Soundfile
        if self.wTree.get_object("checkbutton_sound").get_active() and self.wTree.get_object("fcbutton_sound").get_filename():
            self.client.set_boolean("play-sound", True)
            self.client.set_string("soundfile", str(self.wTree.get_object("fcbutton_sound").get_filename()))
        else:
            self.client.set_boolean("play-sound", False)

        # Autorun
        if self.wTree.get_object("checkbutton_autostart").get_active():
            if not os.path.exists(self.autostart_file):
                try:
                    os.makedirs(os.path.expanduser("~/.config/autostart"))
                except OSError:
                    pass

                try:
                    shutil.copyfile(self.gm_notify_autostart_file, self.autostart_file)
                    os.chmod(self.autostart_file, 0700)
                except IOError:
                    print("Warning: cannot write to path", self.autostart_file)
        else:
            if os.path.exists(self.autostart_file):
                try:
                    os.unlink(self.autostart_file)
                except:
                    print("Warning: cannot delete", self.autostart_file)

        # Start gm-notify itself
        subprocess.Popen(get_executable_path("gm-notify"))
    
    def terminate(self, widget):
        reactor.stop()
    
    def on_checkbutton_sound_toggled(self, widget):
        self.wTree.get_object("fcbutton_sound").set_sensitive(self.wTree.get_object("checkbutton_sound").get_active())
    
    def check_credentials(self, widget, event, data=None):
        '''check if the given credentials are valid'''
        
        input_user = self.wTree.get_object("input_user")
        input_password = self.wTree.get_object("input_password")
        image_credentials = self.wTree.get_object("image_credentials")
        label_credentials = self.wTree.get_object("label_credentials")
        button_apply = self.wTree.get_object("button_apply")
        button_apply.set_sensitive(False)
        
        # Change status text and disable input fields
        if input_user.get_text() and input_password.get_text():
            image_credentials.set_from_file("/usr/share/gm-notify/checking.gif")
            label_credentials.set_text(_("checking..."))
            input_user.set_sensitive(False)
            input_password.set_sensitive(False)
            
            self.api.jid = jid.JID(input_user.get_text())
            self.api.password = input_password.get_text()
            self.api.connect()
        return False
    
    def credentials_valid(self):
        input_user = self.wTree.get_object("input_user")
        input_password = self.wTree.get_object("input_password")
        image_credentials = self.wTree.get_object("image_credentials")
        label_credentials = self.wTree.get_object("label_credentials")
        button_apply = self.wTree.get_object("button_apply")
        
        image_credentials.set_from_icon_name("gtk-yes", Gtk.IconSize.MENU)
        label_credentials.set_text(_("Valid credentials"))
        button_apply.set_sensitive(True)
        input_user.set_sensitive(True)
        input_password.set_sensitive(True)
        
        self.api.die()
    
    def credentials_invalid(self):
        input_user = self.wTree.get_object("input_user")
        input_password = self.wTree.get_object("input_password")
        image_credentials = self.wTree.get_object("image_credentials")
        label_credentials = self.wTree.get_object("label_credentials")
        
        image_credentials.set_from_icon_name("gtk-stop", Gtk.IconSize.MENU)
        label_credentials.set_text(_("Invalid credentials"))
        input_user.set_sensitive(True)
        input_password.set_sensitive(True)
        
        self.api.die()

t = Window()
reactor.registerGApplication(t)
reactor.run()