3
* GNOME Do is the legal property of its developers. Please refer to the
4
* COPYRIGHT file distributed with this
7
* This program is free software: you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation, either version 3 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22
using System.Threading;
23
using System.Collections;
32
/// A class providing a generic login widget for plugins that will need
33
/// to log into an external service. Provides a clean UI and enforces
34
/// asynchronous validation so the plugin developer doesn't need to know
35
/// about delegates or any complex concepts. To see an example of this
36
/// class in use, see GMailContacts.
38
public abstract partial class AbstractLoginWidget : Gtk.Bin
41
private LinkButton new_acct_btn;
44
/// Builds the generic UI with the name passed in by service.
46
/// <param name="service">
47
/// A <see cref="System.String"/>
49
public AbstractLoginWidget (string service)
52
get_account_lbl.Markup = String.Format ("<i>Don't have {0}?</i>",service);
53
new_acct_btn = new LinkButton ("", String.Format ("Sign up for {0}",
55
new_acct_hbox.Add (new_acct_btn);
56
Box.BoxChild wInt = new_acct_hbox [new_acct_btn] as Box.BoxChild;
59
password_entry.Activated += OnPasswordEntryActivated;
60
new_acct_btn.Clicked += OnNewAcctBtnClicked;
62
string username, password;
63
username = password = "";
64
GetAccountData (out username, out password, GetType ());
66
username_entry.Text = username;
67
password_entry.Text = password;
73
/// Default contructor that initializes any service names with the
74
/// generic string "an account".
76
public AbstractLoginWidget () :
83
/// Provides access to the Gtk.Entry field for a username
85
protected Gtk.Entry UsernameEntry {
87
return username_entry;
92
/// Provides access to the Gtk.Label for the username
93
/// Access to this field is provided as a courtesy to developers
94
/// who would like to customize their UI. It is not generally needed.
96
protected Gtk.Label UsernameLabel {
103
/// Provides access to the Gtk.Entry field for the password
105
protected Gtk.Entry PasswordEntry {
107
return password_entry;
112
/// Provides access to the Gtk.Label for the password
113
/// Access to this field is provided as a courtesy to developers
114
/// who would like to customize their UI. It is not generally needed.
116
protected Gtk.Label PasswordLabel {
123
/// Provides access to the Gtk.Label for current validation status
125
protected Gtk.Label StatusLabel {
132
/// Provides access to the Gtk.Button to validate account settings
134
protected Gtk.Button ValidateButton {
141
/// Provides access to the Gtk.Label for signing up for the service
142
/// Access to this field is provided as a courtesy to developers
143
/// who would like to customize their UI. It is not generally needed.
145
protected Gtk.Label GetAccountLabel {
147
return get_account_lbl;
152
/// Provides access to the Gtk.Label field for the username.
153
/// Generally only the .Uri property needs set.
155
protected Gtk.LinkButton GetAccountButton {
162
/// Fires when button to validate account is clicked
164
/// <param name="sender">
165
/// A <see cref="System.Object"/>
168
/// A <see cref="EventArgs"/>
170
protected virtual void OnApplyBtnClicked (object sender, EventArgs e)
172
validate_lbl.Markup = "<i>Validating...</i>";
173
validate_btn.Sensitive = false;
174
string username = username_entry.Text;
175
string password = password_entry.Text;
177
Thread thread = new Thread ((ThreadStart) delegate {
178
bool valid = Validate (username, password);
179
Gtk.Application.Invoke (delegate {
181
validate_lbl.Markup = "<i>Account validation succeeded!</i>";
182
SaveAccountData (username_entry.Text, password_entry.Text,
185
validate_lbl.Markup = "<i>Account validation failed!</i>";
187
validate_btn.Sensitive = true;
190
thread.IsBackground = true; //don't hang on exit if fail
195
/// Opens new browser window with the uri from new_acct_btn.
196
/// if uri is unset, button does nothing.
198
/// <param name="sender">
199
/// A <see cref="System.Object"/>
202
/// A <see cref="EventArgs"/>
204
protected virtual void OnNewAcctBtnClicked (object sender, EventArgs e)
206
if (!String.IsNullOrEmpty (new_acct_btn.Uri))
207
Do.Addins.Util.Environment.Open (new_acct_btn.Uri);
211
/// Saves account data to permanant storage whether it be
212
/// GConf, gnome-keyring, or a flat file.
214
public static void SaveAccountData (string username, string password,
217
string keyName = type.FullName;
222
keyring = Ring.GetDefaultKeyring ();
223
ht = new Hashtable ();
224
ht["name"] = keyName;
225
ht["username"] = username;
227
Ring.CreateItem (keyring, ItemType.GenericSecret, keyName,
230
} catch (Exception e) {
231
Console.Error.WriteLine (e.Message);
236
/// Loads account data from gnome-keyring
238
public static void GetAccountData (out string username,
239
out string password, Type type)
241
string keyName = type.FullName;
242
username = password = "";
243
Hashtable ht = new Hashtable ();
244
ht ["name"] = keyName;
247
foreach (ItemData s in Ring.Find (ItemType.GenericSecret, ht)) {
248
if (s.Attributes.ContainsKey ("name") && s.Attributes.ContainsKey ("username")
249
&& (s.Attributes ["name"] as string).Equals (keyName)) {
250
username = s.Attributes ["username"] as string;
255
} catch (Exception) {
256
Console.Error.WriteLine ("No account info stored for {0}",
262
/// Makes validation call to service
264
/// <param name="username">
265
/// A <see cref="System.String"/>
267
/// <param name="password">
268
/// A <see cref="System.String"/>
271
/// A <see cref="System.Boolean"/>
273
protected abstract bool Validate (string username, string password);
275
protected virtual void OnPasswordEntryActivated (object sender, System.EventArgs e)
277
validate_btn.Click ();