~mate-ob/gm-notify/multi-account

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
#!/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, Gdk

from twisted.internet import gtk3reactor
gtk3reactor.install()
from twisted.internet import reactor

import gm_notify_keyring as keyring
from gm_notify_keyring import Credentials
from account_config import AccountConfig
import account_settings_provider 
 

_ = 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 run(self, args):
        self.register();
        if self.get_is_remote():
            self.activate()
        else:
            super(Window, self).run(args)

    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")
        
        if os.path.exists("gm-list.ui"):
            builder_file = "gm-list.ui"
        elif os.path.exists("/usr/local/share/gm-notify/gm-list.ui"):
            builder_file = "/usr/local/share/gm-notify/gm-list.ui"
        elif os.path.exists("/usr/share/gm-notify/gm-list.ui"):
            builder_file = "/usr/share/gm-notify/gm-list.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.window.connect("delete_event", self.terminate)
        self.window.connect("window-state-event", self.on_update_accounts)
        self.add_window(self.window)
        
        self.accounts = self.wTree.get_object("accounts")

        self.accounts_treeview = self.wTree.get_object("accounts_treeview")
        self.accounts_treeview.connect("row-activated", self.on_modify_account);
        
        self.wTree.get_object("notebook_main").set_current_page(0)
        
        
        self.add_button = self.wTree.get_object("add_button")
        self.add_button.connect("clicked", self.on_add_account)

        self.remove_button = self.wTree.get_object("remove_button")
        self.remove_button.connect("clicked", self.on_remove_account)
        
        self.wTree.get_object("button_ok").connect("clicked", self.terminate)
        # 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/applications/gm-notify.desktop"):
            self.gm_notify_autostart_file = "/usr/local/share/applications/gm-notify.desktop"
        elif os.path.exists("/usr/share/applications/gm-notify.desktop"):
            self.gm_notify_autostart_file = "/usr/share/applications/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))
        
    def update_accounts(self, accounts, keys):
        accounts.clear()
        if not self.keys.has_any_users():
            return
        for user in keys.get_all_users():
            accounts.append((user,))
    
    def on_update_accounts(self, widget, event):
        print("Event %s WindowState %s" % (str(event.type), str(event.new_window_state)))
        if (event.type == Gdk.EventType.WINDOW_STATE
            and event.new_window_state == Gdk.WindowState.FOCUSED):
            self.update_accounts(self.accounts, self.keys)
    
    def on_modify_account(self, widget, path, column):
        iter = self.accounts.get_iter(path)
        account = self.accounts[iter]
        username = account[0]
        credentials = self.keys.get_credentials(username)
        account_config = AccountConfig(self.keys, credentials)
        window = account_config.init_window(self.window)
    
    def on_add_account(self, widget):
        account_config = AccountConfig(self.keys, Credentials())
        window = account_config.init_window(self.window)
    
    def on_remove_account(self, widget):
        tree_selection = self.accounts_treeview.get_selection()
        accounts,iter = tree_selection.get_selected()
        if iter is None:
            return
        
        account = accounts[iter]
        username = account[0]
        settings_provider = account_settings_provider.create_settings_provider(username)
        settings_provider.remove_all_settings()
        self.keys.delete_credentials(username)
        
        accounts.remove(iter)
        
    def save(self, widget, data=None):
        '''saves the entered data and closes the app'''
        
        # 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, event = None):
        reactor.stop()
    
t = Window()
reactor.registerGApplication(t)
reactor.run()