~ubuntu-branches/ubuntu/quantal/shotwell/quantal

« back to all changes in this revision

Viewing changes to plugins/common/accounts.vala

  • Committer: Package Import Robot
  • Author(s): Ken VanDine, Alberto Mardegan
  • Date: 2012-09-24 11:10:48 UTC
  • Revision ID: package-import@ubuntu.com-20120924111048-dic3zkytvn0iaz36
Tags: 0.13.0-0ubuntu2
[ Alberto Mardegan ]
* debian/patches/06_uoa.patch (LP: #1046461)
  - Support multiple accounts per service
  - Attempt automatic login
  - Remove logout buttons

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Copyright 2009-2011 Yorba Foundation
2
2
 *
3
3
 * This software is licensed under the GNU Lesser General Public License
4
 
 * (version 2.1 or later).  See the COPYING file in this distribution. 
 
4
 * (version 2.1 or later).  See the COPYING file in this distribution.
5
5
 */
6
6
 
7
7
namespace Publishing.Accounts {
8
8
 
9
9
public class SharingAccount {
10
10
    private Ag.AccountService account_service = null;
11
 
    
 
11
 
12
12
    public SharingAccount(Ag.AccountService account_service) {
13
13
        this.account_service = account_service;
14
14
    }
30
30
}
31
31
 
32
32
public class SharingAccounts {
33
 
    private Ag.Manager manager = null;
34
 
    
35
 
    public SharingAccounts() {
36
 
        manager = new Ag.Manager.for_service_type("sharing");
37
 
    }
38
 
 
39
 
    public SharingAccount? get_account_for_provider(string provider) {
 
33
    private static Ag.Manager manager = null;
 
34
    private string provider_name;
 
35
    private Ag.AccountService[] all_accounts;
 
36
 
 
37
    public SharingAccounts(string provider_name) {
 
38
        if (manager == null) {
 
39
            manager = new Ag.Manager.for_service_type("sharing");
 
40
        }
 
41
        manager.enabled_event.connect(on_account_enabled);
 
42
 
 
43
        this.provider_name = provider_name;
 
44
        all_accounts = get_accounts();
 
45
    }
 
46
 
 
47
    private void on_account_enabled(uint account_id) {
 
48
        /* To keep the implementation simple, just rebuild the account
 
49
         * list from scratch */
 
50
        all_accounts = get_accounts();
 
51
    }
 
52
 
 
53
    private Ag.AccountService[] get_accounts() {
40
54
        GLib.List<Ag.AccountService> accounts =
41
55
            manager.get_enabled_account_services();
42
56
 
 
57
        Ag.AccountService[] list = {};
 
58
 
43
59
        foreach (Ag.AccountService account_service in accounts) {
44
60
            Ag.Account account = account_service.get_account();
45
 
            if (account.get_provider_name() == provider) {
 
61
            if (account.get_provider_name() == provider_name) {
 
62
                list += account_service;
 
63
            }
 
64
        }
 
65
        return list;
 
66
    }
 
67
 
 
68
    public bool has_enabled_accounts() {
 
69
        return all_accounts.length > 0;
 
70
    }
 
71
 
 
72
    public string[] list_account_names() {
 
73
        string[] names = {};
 
74
        foreach (Ag.AccountService account_service in all_accounts) {
 
75
            names += account_service.get_account().get_display_name();
 
76
        }
 
77
        return names;
 
78
    }
 
79
 
 
80
    public SharingAccount? find_account(string? account_name) {
 
81
        foreach (Ag.AccountService account_service in all_accounts)
 
82
            if (account_service.get_account().get_display_name() == account_name) {
46
83
                return new SharingAccount(account_service);
47
84
            }
48
 
        }
 
85
 
49
86
        return null;
50
87
    }
51
88
}
52
89
 
 
90
public abstract class UOAPublishingService : Object, Spit.Pluggable, Spit.Publishing.Service {
 
91
    private SharingAccounts account_manager;
 
92
 
 
93
    public UOAPublishingService(string provider_name) {
 
94
        account_manager = new SharingAccounts(provider_name);
 
95
    }
 
96
 
 
97
    public virtual int get_pluggable_interface(int min_host_interface, int max_host_interface) {
 
98
        return Spit.negotiate_interfaces(min_host_interface, max_host_interface,
 
99
            Spit.Publishing.CURRENT_INTERFACE);
 
100
    }
 
101
 
 
102
    public abstract unowned string get_id();
 
103
 
 
104
    public abstract unowned string get_pluggable_name();
 
105
 
 
106
    public abstract void get_info(ref Spit.PluggableInfo info);
 
107
 
 
108
    public virtual void activation(bool enabled) {
 
109
    }
 
110
 
 
111
    public abstract Spit.Publishing.Publisher create_publisher(string? account_name, Spit.Publishing.PluginHost host);
 
112
 
 
113
    public abstract Spit.Publishing.Publisher.MediaType get_supported_media();
 
114
 
 
115
    public SharingAccount? find_account(string? account_name) {
 
116
        return account_manager.find_account(account_name);
 
117
    }
 
118
 
 
119
    public bool is_enabled() {
 
120
        return account_manager.has_enabled_accounts();
 
121
    }
 
122
 
 
123
    public string[] list_account_names() {
 
124
        return account_manager.list_account_names();
 
125
    }
 
126
}
 
127
 
 
128
public class UOAPublisherAuthenticator : Object {
 
129
    private weak Spit.Publishing.PluginHost host = null;
 
130
    private Signon.AuthSession auth_session = null;
 
131
    private SharingAccount account = null;
 
132
    private bool firstLoginAttempt = true;
 
133
    private string welcome_message = null;
 
134
 
 
135
    public UOAPublisherAuthenticator(SharingAccount account,
 
136
                                     Spit.Publishing.PluginHost host,
 
137
                                     string welcome_message)
 
138
    {
 
139
        this.host = host;
 
140
        this.account = account;
 
141
        this.welcome_message = welcome_message;
 
142
    }
 
143
 
 
144
    public signal void authenticated(owned HashTable<string,Value?> session_data);
 
145
 
 
146
    public void authenticate() {
 
147
        debug("ACTION: authentication requested.");
 
148
 
 
149
        do_authentication();
 
150
    }
 
151
 
 
152
    public HashTable<string,Value?>? get_authentication_data() {
 
153
        if (account == null) return null;
 
154
        string mechanism = null;
 
155
        return account.get_session_parameters(out mechanism);
 
156
    }
 
157
 
 
158
    private void do_authentication() {
 
159
        debug("ACTION: authenticating.");
 
160
 
 
161
        HashTable<string,Value?> data = null;
 
162
        string mechanism = null;
 
163
 
 
164
        if (account != null) {
 
165
            try {
 
166
                auth_session = account.create_auth_session();
 
167
                data = account.get_session_parameters(out mechanism);
 
168
                debug("Got account data");
 
169
            } catch (GLib.Error e) {
 
170
                warning("EVENT: couldn't create session for account: %s",
 
171
                        e.message);
 
172
            }
 
173
        }
 
174
 
 
175
        if (data == null) {
 
176
            warning ("No account authentication data");
 
177
            host.post_error(new Spit.Publishing.PublishingError.SERVICE_ERROR(
 
178
                "Error while accessing the account"));
 
179
            return;
 
180
        }
 
181
 
 
182
        if (firstLoginAttempt) {
 
183
            firstLoginAttempt = false;
 
184
            data.insert("UiPolicy", (int)Signon.SessionDataUiPolicy.NO_USER_INTERACTION);
 
185
        } else {
 
186
            var windowId = host.get_dialog_xid();
 
187
            if (windowId != 0) {
 
188
                data.insert("WindowId", (uint)windowId);
 
189
            }
 
190
        }
 
191
 
 
192
        auth_session.process(data, mechanism, on_processed);
 
193
        host.set_service_locked(true);
 
194
    }
 
195
 
 
196
    private void on_processed(Signon.AuthSession self, owned HashTable<string,Value?>? session_data, GLib.Error error){
 
197
        host.set_service_locked(false);
 
198
        if (error != null) {
 
199
            debug("got error: %s", error.message);
 
200
            if (error is Signon.Error.USER_INTERACTION) {
 
201
                debug("User interaction!");
 
202
                do_show_service_welcome_pane();
 
203
            } else {
 
204
                host.post_error(new Spit.Publishing.PublishingError.SERVICE_ERROR("Authentication failed"));
 
205
            }
 
206
            return;
 
207
        }
 
208
 
 
209
        authenticated(session_data);
 
210
    }
 
211
 
 
212
    private void do_show_service_welcome_pane() {
 
213
        debug("ACTION: showing service welcome pane.");
 
214
 
 
215
        host.install_welcome_pane(welcome_message, on_service_welcome_login);
 
216
    }
 
217
 
 
218
    private void on_service_welcome_login() {
 
219
        debug("EVENT: user clicked 'Login' in welcome pane.");
 
220
 
 
221
        do_authentication();
 
222
    }
 
223
}
 
224
 
53
225
}
54
226