ShareX/UploadersLib/GUI/TwitterMsg.cs

200 lines
5.5 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2014-05-13 21:06:40 +12:00
Copyright (C) 2007-2014 ShareX Developers
2013-11-03 23:53:49 +13:00
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 2
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using UploadersLib.HelperClasses;
using UploadersLib.ImageUploaders;
2013-11-03 23:53:49 +13:00
using UploadersLib.Properties;
namespace UploadersLib
{
public partial class TwitterMsg : Form
{
public string Message
{
get
{
return txtTweet.Text;
}
set
{
txtTweet.Text = value;
}
}
2014-06-15 22:29:09 +12:00
private int length;
public int Length
{
get
{
return length;
}
set
{
length = value;
UpdateLength();
}
}
public bool IsValidMessage
{
get
{
return !string.IsNullOrEmpty(Message) && Message.Length <= Length;
}
}
2013-11-03 23:53:49 +13:00
public OAuthInfo AuthInfo { get; set; }
public TwitterClientSettings Config { get; set; }
2014-06-15 22:29:09 +12:00
public TwitterMsg(OAuthInfo oauth)
2013-11-03 23:53:49 +13:00
{
InitializeComponent();
AuthInfo = oauth;
Icon = Resources.Twitter;
2014-06-15 22:29:09 +12:00
Length = Twitter.MessageLimit;
2013-11-03 23:53:49 +13:00
Config = new TwitterClientSettings();
}
2014-06-15 22:29:09 +12:00
public TwitterMsg()
: this(null)
2013-11-03 23:53:49 +13:00
{
}
2014-06-15 22:29:09 +12:00
private void UpdateLength()
2013-11-03 23:53:49 +13:00
{
2014-06-15 22:29:09 +12:00
lblTweetLength.Text = (Length - Message.Length).ToString();
btnOK.Enabled = IsValidMessage;
2013-11-03 23:53:49 +13:00
}
private void btnOK_Click(object sender, EventArgs e)
{
2014-06-15 22:29:09 +12:00
if (IsValidMessage)
2013-11-03 23:53:49 +13:00
{
DialogResult = DialogResult.OK;
2014-06-15 22:29:09 +12:00
if (AuthInfo != null)
2013-11-03 23:53:49 +13:00
{
Hide();
try
{
2014-06-15 22:29:09 +12:00
TwitterStatusResponse status = new Twitter(AuthInfo).TweetMessage(Message);
2013-11-03 23:53:49 +13:00
if (status != null && !string.IsNullOrEmpty(status.in_reply_to_screen_name))
{
Config.AddUser(status.in_reply_to_screen_name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Tweet Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Close();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
2014-06-15 22:29:09 +12:00
private void TwitterMsg_Load(object sender, EventArgs e)
{
foreach (string user in Config.Addressees)
{
lbUsers.Items.Add(user);
}
}
2013-11-03 23:53:49 +13:00
private void txtTweet_TextChanged(object sender, EventArgs e)
{
2014-06-15 22:29:09 +12:00
UpdateLength();
2013-11-03 23:53:49 +13:00
}
private void lbUsers_SelectedIndexChanged(object sender, EventArgs e)
{
if (lbUsers.SelectedItem != null)
{
txtTweet.Text = txtTweet.Text.Insert(txtTweet.SelectionStart, "@" + lbUsers.SelectedItem + " ");
txtTweet.SelectionStart = txtTweet.Text.Length;
txtTweet.Focus();
}
}
private void lbUsers_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
for (int i = lbUsers.Items.Count - 1; i >= 0; i--)
{
lbUsers.SetSelected(i, true);
}
}
else if (e.KeyCode == Keys.Delete)
{
if (lbUsers.SelectedIndex != -1)
{
List<string> temp = lbUsers.SelectedItems.Cast<string>().ToList();
foreach (string hi in temp)
{
lbUsers.Items.Remove(hi);
}
if (lbUsers.Items.Count > 0)
{
lbUsers.SelectedIndex = 0;
}
}
}
}
}
public class TwitterClientSettings
{
public List<string> Addressees { get; set; }
public TwitterClientSettings()
{
Addressees = new List<string>();
}
public void AddUser(string user)
{
if (!string.IsNullOrEmpty(user) && !Addressees.Contains(user))
{
Addressees.Add(user);
}
}
}
}