public void Save(string path) { var settings = new IniSettings(); foreach (var prop in properties.Values) { if (prop.PropertyType == typeof(Color)) { settings.Set(prop.Name, "#" + (((Color)prop.GetValue(this, null)).ToArgb() & 0xFFFFFF).ToString("X")); } else if (prop.PropertyType == typeof(Brush)) { var value = prop.GetValue(this, null); var solidBrush = value as SolidBrush; if (solidBrush != null) { settings.Set(prop.Name, "#" + ((solidBrush.Color).ToArgb() & 0xFFFFFF).ToString("X")); } } else if (prop.PropertyType == typeof(Pen)) { var value = prop.GetValue(this, null); var solidBrush = (value as Pen)?.Brush as SolidBrush; if (solidBrush != null) { settings.Set(prop.Name, "#" + ((solidBrush.Color).ToArgb() & 0xFFFFFF).ToString("X")); } } } settings.Save(path); }
public static TwitterApi Login(IniSettings setting, string section) { TwitterApi api = new TwitterApi { setting = setting, section = section }; string consumerKey = Convert.ToString(setting.GetValue(section, "ConsumerKey", string.Empty)); string consumerSecret = Convert.ToString(setting.GetValue(section, "ConsumerSecret", string.Empty)); string accessToken = Convert.ToString(setting.GetValue(section, "AccessToken", string.Empty)); string accessSecret = Convert.ToString(setting.GetValue(section, "AccessSecret", string.Empty)); if (string.IsNullOrWhiteSpace(consumerKey) || string.IsNullOrWhiteSpace(consumerSecret)) { setting.Save(); api.Status = UserStatus.NO_APIKEY; return(api); } if (string.IsNullOrWhiteSpace(accessToken) || string.IsNullOrWhiteSpace(accessSecret)) { api.OAuth = new TwitterOAuth(consumerKey, consumerSecret); TwitterOAuth.TokenPair tokens = api.OAuth.RequestToken(); if (tokens == null) { api.Status = UserStatus.NO_APIKEY; return(api); } api.OAuth.User.Token = tokens.Token; api.OAuth.User.Secret = tokens.Secret; api.Status = UserStatus.LOGIN_REQUESTED; return(api); } api.OAuth = new TwitterOAuth(consumerKey, consumerSecret, accessToken, accessSecret); api.MyUserInfo = api.getMyUserInfo(); api.Status = api.MyUserInfo == null ? UserStatus.INVALID_CREDITIONAL : UserStatus.LOGIN_SUCCESS; return(api); }
private void ProcessTwitterLogin(string SettingsSection = "Authenticate") { TwitterApi twitter = loginTemp ?? (loginTemp = TwitterApi.Login(settings, SettingsSection)); switch (twitter.Status) { case UserStatus.NO_APIKEY: RegisterApiKeyForm rakform = new RegisterApiKeyForm() { TargetSection = SettingsSection }; if (DialogResult.OK == rakform.ShowDialog(this)) { loginTemp = twitter = TwitterApi.Login(settings, SettingsSection); SetStatusLabel(twitter.Status.ToString()); ProcessTwitterLogin(SettingsSection); } break; case UserStatus.NO_CREDITIONAL: MessageBox.Show(this, @"Unable to access your account! Please try login again.", @"No Creditional", MessageBoxButtons.OK, MessageBoxIcon.Warning); ProcessTwitterLogin(SettingsSection); break; case UserStatus.LOGIN_REQUESTED: LoginProgressForm lpform = new LoginProgressForm(twitter); if (DialogResult.OK == lpform.ShowDialog(this)) { loginTemp = twitter = TwitterApi.Login(settings, SettingsSection); SetStatusLabel(twitter.Status.ToString()); ProcessTwitterLogin(SettingsSection); } break; case UserStatus.INVALID_CREDITIONAL: loginTemp = null; MessageBox.Show(this, @"Unable to access your account! Please try login from menu.", @"Invalid Creditional", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); break; case UserStatus.LOGIN_SUCCESS: try { loginTemp = null; string apikey = SettingsSection.Equals("Authenticate") ? "Default" : SettingsSection; CredentialManager.Instance.AddCredential(apikey, twitter); manageAPIKeysToolStripMenuItem.DropDownItems.Add(new ToolStripMenuItem(apikey) { Name = apikey }); removeAPIKeyToolStripMenuItem.DropDownItems.Add(new ToolStripMenuItem(apikey) { Name = apikey }); loginToolStripMenuItem.Enabled = false; loginToolStripMenuItem.Text = @"Logged in as " + twitter.MyUserInfo.screen_name; } catch (InvalidCredentialException e) { settings.DeleteSection(SettingsSection); settings.Save(); MessageBox.Show(this, e.Message, @"Invalid Creditional", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } break; } }