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

19 by Alexander Hungenberg
New Config interface with all new options. New IMAP Lib included but not yet finished
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
79 by Alexander Hungenberg
added bulgarian translation and ready for release 0.10.3
4
# gm-notify-config v0.10.3
30 by Alexander Hungenberg
Increased version number and fixed README and setup.py
5
# GMail Notifier Configuration Utility
6
#
54 by Alexander Hungenberg
making final release modifications
7
# Copyright (c) 2009-2010, Alexander Hungenberg <alexander.hungenberg@gmail.com>
30 by Alexander Hungenberg
Increased version number and fixed README and setup.py
8
# 
9
# This program is free software: you can redistribute it and/or modify
10
# it under the terms of the GNU General Public License as published by
11
# the Free Software Foundation, either version 3 of the License, or
12
# (at your option) any later version.
13
#
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
# GNU General Public License for more details.
18
#
19
# You should have received a copy of the GNU General Public License
20
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
#
87 by Jason Conti
Port gm-config.glade to GtkBuilder (renamed to gm-config.ui)
22
from __future__ import print_function
23
19 by Alexander Hungenberg
New Config interface with all new options. New IMAP Lib included but not yet finished
24
import sys
21.1.3 by Ken VanDine
* Renamed gm-config.py back to gm-notify-config.py, it is referenced in quite a few places
25
import os
19 by Alexander Hungenberg
New Config interface with all new options. New IMAP Lib included but not yet finished
26
import gettext
27
import subprocess
61.1.1 by Fredrik Nilson
fix bug #373275
28
import shutil
27 by Alexander Hungenberg
Now everything is using the new IMAP Backend... Labels are working
29
104 by Mateusz Balbus
Initial implementation of multi-account support
30
from gi.repository import Gio, Gtk, Gdk
88 by Jason Conti
* gm-notify-config:
31
32
from twisted.internet import gtk3reactor
33
gtk3reactor.install()
19 by Alexander Hungenberg
New Config interface with all new options. New IMAP Lib included but not yet finished
34
from twisted.internet import reactor
22 by Alexander Hungenberg
some style corrections
35
71 by Alexander Hungenberg
fix bug #575531
36
import gm_notify_keyring as keyring
104 by Mateusz Balbus
Initial implementation of multi-account support
37
from gm_notify_keyring import Credentials
38
from account_config import AccountConfig
106 by Mateusz Balbus
[#5] Added support for multiaccount settings
39
import account_settings_provider 
104 by Mateusz Balbus
Initial implementation of multi-account support
40
 
19 by Alexander Hungenberg
New Config interface with all new options. New IMAP Lib included but not yet finished
41
42
_ = gettext.translation('gm-notify', fallback=True).ugettext
43
52 by Alexander Hungenberg
completly cleaned up gm-notify-config - fixed bug #463409
44
class PathNotFound(Exception): pass
45
46
def get_executable_path(name):
47
    path = "%s/%s" % (os.getcwd(), name)
48
    if os.path.exists(path) and os.access(path, os.X_OK): return path
49
    path = "/usr/local/bin/" + name
50
    if os.path.exists(path) and os.access(path, os.X_OK): return path
51
    path = "/usr/bin/" + name
52
    if os.path.exists(path) and os.access(path, os.X_OK): return path
53
    raise PathNotFound("%s not found" % name)
54
88 by Jason Conti
* gm-notify-config:
55
class Window(Gtk.Application):
19 by Alexander Hungenberg
New Config interface with all new options. New IMAP Lib included but not yet finished
56
    def __init__(self):
88 by Jason Conti
* gm-notify-config:
57
        super(Window, self).__init__(application_id="net.launchpad.gm-notify-config",
58
            flags=Gio.ApplicationFlags.FLAGS_NONE)
59
        self.window = None
60
        self.connect("activate", self.on_activate)
61
104 by Mateusz Balbus
Initial implementation of multi-account support
62
    def run(self, args):
63
        self.register();
64
        if self.get_is_remote():
65
            self.activate()
66
        else:
67
            super(Window, self).run(args)
68
88 by Jason Conti
* gm-notify-config:
69
    def on_activate(self, app):
70
        '''Setup the application'''
71
        if self.window is not None:
72
            return
73
40 by Alexander Hungenberg
ok - first commit with new backend... You wanna do some alpha testing?
74
        #####
75
        # GUI initialization
76
        #####
19 by Alexander Hungenberg
New Config interface with all new options. New IMAP Lib included but not yet finished
77
        self.keys = keyring.Keyring("GMail", "mail.google.com", "http")
78
        
104 by Mateusz Balbus
Initial implementation of multi-account support
79
        if os.path.exists("gm-list.ui"):
80
            builder_file = "gm-list.ui"
81
        elif os.path.exists("/usr/local/share/gm-notify/gm-list.ui"):
82
            builder_file = "/usr/local/share/gm-notify/gm-list.ui"
83
        elif os.path.exists("/usr/share/gm-notify/gm-list.ui"):
84
            builder_file = "/usr/share/gm-notify/gm-list.ui"
85
21.1.3 by Ken VanDine
* Renamed gm-config.py back to gm-notify-config.py, it is referenced in quite a few places
86
88 by Jason Conti
* gm-notify-config:
87
        self.wTree = Gtk.Builder.new()
88
        self.wTree.add_from_file(builder_file)
89
        self.wTree.set_translation_domain("gm-notify")
90
        self.window = self.wTree.get_object("gmnotify_config_main")
19 by Alexander Hungenberg
New Config interface with all new options. New IMAP Lib included but not yet finished
91
        self.window.show_all()
104 by Mateusz Balbus
Initial implementation of multi-account support
92
        self.window.connect("delete_event", self.terminate)
93
        self.window.connect("window-state-event", self.on_update_accounts)
88 by Jason Conti
* gm-notify-config:
94
        self.add_window(self.window)
104 by Mateusz Balbus
Initial implementation of multi-account support
95
        
96
        self.accounts = self.wTree.get_object("accounts")
88 by Jason Conti
* gm-notify-config:
97
104 by Mateusz Balbus
Initial implementation of multi-account support
98
        self.accounts_treeview = self.wTree.get_object("accounts_treeview")
105 by Mateusz Balbus
[#5] Added possibility to edit existing user
99
        self.accounts_treeview.connect("row-activated", self.on_modify_account);
104 by Mateusz Balbus
Initial implementation of multi-account support
100
        
88 by Jason Conti
* gm-notify-config:
101
        self.wTree.get_object("notebook_main").set_current_page(0)
19 by Alexander Hungenberg
New Config interface with all new options. New IMAP Lib included but not yet finished
102
        
104 by Mateusz Balbus
Initial implementation of multi-account support
103
        
104
        self.add_button = self.wTree.get_object("add_button")
105
        self.add_button.connect("clicked", self.on_add_account)
61.1.1 by Fredrik Nilson
fix bug #373275
106
104 by Mateusz Balbus
Initial implementation of multi-account support
107
        self.remove_button = self.wTree.get_object("remove_button")
108
        self.remove_button.connect("clicked", self.on_remove_account)
109
        
110
        self.wTree.get_object("button_ok").connect("clicked", self.terminate)
61.1.1 by Fredrik Nilson
fix bug #373275
111
        # Autorun
63 by Alexander Hungenberg
merge to fix bug #373275
112
        if os.path.exists("data/gm-notify.desktop"):
113
            self.gm_notify_autostart_file = "data/gm-notify.desktop"
94 by Jason Conti
Install gm-notify.desktop to /usr/share/applications, otherwise messaging-menu can't find it
114
        elif os.path.exists("/usr/local/share/applications/gm-notify.desktop"):
115
            self.gm_notify_autostart_file = "/usr/local/share/applications/gm-notify.desktop"
116
        elif os.path.exists("/usr/share/applications/gm-notify.desktop"):
117
            self.gm_notify_autostart_file = "/usr/share/applications/gm-notify.desktop"
61.1.1 by Fredrik Nilson
fix bug #373275
118
        self.autostart_file = os.path.expanduser("~/.config/autostart/gm-notify.desktop")
88 by Jason Conti
* gm-notify-config:
119
        self.wTree.get_object("checkbutton_autostart").set_active(os.path.exists(self.autostart_file))
19 by Alexander Hungenberg
New Config interface with all new options. New IMAP Lib included but not yet finished
120
        
107 by Mateusz Balbus
[#5] Implementation for multiaccount in gtalk and notifications
121
    def update_accounts(self, accounts, keys):
104 by Mateusz Balbus
Initial implementation of multi-account support
122
        accounts.clear()
107 by Mateusz Balbus
[#5] Implementation for multiaccount in gtalk and notifications
123
        if not self.keys.has_any_users():
124
            return
125
        for user in keys.get_all_users():
105 by Mateusz Balbus
[#5] Added possibility to edit existing user
126
            accounts.append((user,))
104 by Mateusz Balbus
Initial implementation of multi-account support
127
    
128
    def on_update_accounts(self, widget, event):
129
        print("Event %s WindowState %s" % (str(event.type), str(event.new_window_state)))
130
        if (event.type == Gdk.EventType.WINDOW_STATE
131
            and event.new_window_state == Gdk.WindowState.FOCUSED):
107 by Mateusz Balbus
[#5] Implementation for multiaccount in gtalk and notifications
132
            self.update_accounts(self.accounts, self.keys)
105 by Mateusz Balbus
[#5] Added possibility to edit existing user
133
    
134
    def on_modify_account(self, widget, path, column):
135
        iter = self.accounts.get_iter(path)
136
        account = self.accounts[iter]
137
        username = account[0]
138
        credentials = self.keys.get_credentials(username)
106 by Mateusz Balbus
[#5] Added support for multiaccount settings
139
        account_config = AccountConfig(self.keys, credentials)
105 by Mateusz Balbus
[#5] Added possibility to edit existing user
140
        window = account_config.init_window(self.window)
104 by Mateusz Balbus
Initial implementation of multi-account support
141
    
142
    def on_add_account(self, widget):
106 by Mateusz Balbus
[#5] Added support for multiaccount settings
143
        account_config = AccountConfig(self.keys, Credentials())
104 by Mateusz Balbus
Initial implementation of multi-account support
144
        window = account_config.init_window(self.window)
145
    
146
    def on_remove_account(self, widget):
147
        tree_selection = self.accounts_treeview.get_selection()
148
        accounts,iter = tree_selection.get_selected()
149
        if iter is None:
150
            return
151
        
152
        account = accounts[iter]
106 by Mateusz Balbus
[#5] Added support for multiaccount settings
153
        username = account[0]
154
        settings_provider = account_settings_provider.create_settings_provider(username)
155
        settings_provider.remove_all_settings()
156
        self.keys.delete_credentials(username)
157
        
104 by Mateusz Balbus
Initial implementation of multi-account support
158
        accounts.remove(iter)
159
        
19 by Alexander Hungenberg
New Config interface with all new options. New IMAP Lib included but not yet finished
160
    def save(self, widget, data=None):
161
        '''saves the entered data and closes the app'''
104 by Mateusz Balbus
Initial implementation of multi-account support
162
        
61.1.1 by Fredrik Nilson
fix bug #373275
163
        # Autorun
88 by Jason Conti
* gm-notify-config:
164
        if self.wTree.get_object("checkbutton_autostart").get_active():
61.1.1 by Fredrik Nilson
fix bug #373275
165
            if not os.path.exists(self.autostart_file):
166
                try:
167
                    os.makedirs(os.path.expanduser("~/.config/autostart"))
168
                except OSError:
169
                    pass
170
171
                try:
172
                    shutil.copyfile(self.gm_notify_autostart_file, self.autostart_file)
173
                    os.chmod(self.autostart_file, 0700)
174
                except IOError:
88 by Jason Conti
* gm-notify-config:
175
                    print("Warning: cannot write to path", self.autostart_file)
61.1.1 by Fredrik Nilson
fix bug #373275
176
        else:
177
            if os.path.exists(self.autostart_file):
178
                try:
179
                    os.unlink(self.autostart_file)
180
                except:
88 by Jason Conti
* gm-notify-config:
181
                    print("Warning: cannot delete", self.autostart_file)
61.1.1 by Fredrik Nilson
fix bug #373275
182
24 by Alexander Hungenberg
merged Tom's branch to fix Bug #373270 and config interface is working again
183
        # Start gm-notify itself
74.1.1 by Andrew Starr-Bochicchio
Finish removing script extensions.
184
        subprocess.Popen(get_executable_path("gm-notify"))
19 by Alexander Hungenberg
New Config interface with all new options. New IMAP Lib included but not yet finished
185
    
104 by Mateusz Balbus
Initial implementation of multi-account support
186
    def terminate(self, widget, event = None):
19 by Alexander Hungenberg
New Config interface with all new options. New IMAP Lib included but not yet finished
187
        reactor.stop()
52 by Alexander Hungenberg
completly cleaned up gm-notify-config - fixed bug #463409
188
    
19 by Alexander Hungenberg
New Config interface with all new options. New IMAP Lib included but not yet finished
189
t = Window()
88 by Jason Conti
* gm-notify-config:
190
reactor.registerGApplication(t)
19 by Alexander Hungenberg
New Config interface with all new options. New IMAP Lib included but not yet finished
191
reactor.run()