ShareX/ShareX/UploadInfoManager.cs

345 lines
12 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-12-31 22:41:32 +13:00
Copyright © 2007-2015 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 Microsoft.VisualBasic.FileIO;
2014-12-11 12:19:28 +13:00
using ShareX.HelpersLib;
using ShareX.Properties;
2014-12-11 12:19:28 +13:00
using ShareX.UploadersLib;
2013-11-03 23:53:49 +13:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace ShareX
{
public class UploadInfoManager
{
public UploadInfoStatus[] SelectedItems { get; private set; }
public UploadInfoStatus SelectedItem
{
get
{
if (IsItemSelected)
{
return SelectedItems[0];
}
return null;
}
}
public bool IsItemSelected
{
get
{
return SelectedItems != null && SelectedItems.Length > 0;
}
}
private ListView lv;
private UploadInfoParser parser;
public UploadInfoManager(ListView listView)
{
lv = listView;
parser = new UploadInfoParser();
}
private void CopyTexts(IEnumerable<string> texts)
{
if (texts != null && texts.Count() > 0)
{
string urls = string.Join("\r\n", texts.ToArray());
if (!string.IsNullOrEmpty(urls))
{
ClipboardHelpers.CopyText(urls);
}
}
}
public void RefreshSelectedItems()
{
if (lv != null && lv.SelectedItems != null && lv.SelectedItems.Count > 0)
{
SelectedItems = lv.SelectedItems.Cast<ListViewItem>().Select(x => x.Tag as UploadTask).Where(x => x != null && x.Info != null).
Select(x => new UploadInfoStatus(x.Info)).ToArray();
}
else
{
SelectedItems = null;
}
}
#region Open
public void OpenURL()
{
if (IsItemSelected && SelectedItem.IsURLExist) URLHelpers.OpenURL(SelectedItem.Info.Result.URL);
2013-11-03 23:53:49 +13:00
}
public void OpenShortenedURL()
{
if (IsItemSelected && SelectedItem.IsShortenedURLExist) URLHelpers.OpenURL(SelectedItem.Info.Result.ShortenedURL);
2013-11-03 23:53:49 +13:00
}
public void OpenThumbnailURL()
{
if (IsItemSelected && SelectedItem.IsThumbnailURLExist) URLHelpers.OpenURL(SelectedItem.Info.Result.ThumbnailURL);
2013-11-03 23:53:49 +13:00
}
public void OpenDeletionURL()
{
if (IsItemSelected && SelectedItem.IsDeletionURLExist) URLHelpers.OpenURL(SelectedItem.Info.Result.DeletionURL);
2013-11-03 23:53:49 +13:00
}
public void OpenFile()
{
if (IsItemSelected && SelectedItem.IsFileExist) URLHelpers.OpenURL(SelectedItem.Info.FilePath);
2013-11-03 23:53:49 +13:00
}
public void OpenThumbnailFile()
{
if (IsItemSelected && SelectedItem.IsThumbnailFileExist) URLHelpers.OpenURL(SelectedItem.Info.ThumbnailFilePath);
}
2013-11-03 23:53:49 +13:00
public void OpenFolder()
{
if (IsItemSelected && SelectedItem.IsFileExist) Helpers.OpenFolderWithFile(SelectedItem.Info.FilePath);
}
public void TryOpen()
{
if (IsItemSelected)
{
if (SelectedItem.IsShortenedURLExist)
{
URLHelpers.OpenURL(SelectedItem.Info.Result.ShortenedURL);
2013-11-03 23:53:49 +13:00
}
else if (SelectedItem.IsURLExist)
{
URLHelpers.OpenURL(SelectedItem.Info.Result.URL);
2013-11-03 23:53:49 +13:00
}
else if (SelectedItem.IsFilePathValid)
2013-11-03 23:53:49 +13:00
{
URLHelpers.OpenURL(SelectedItem.Info.FilePath);
2013-11-03 23:53:49 +13:00
}
}
}
#endregion Open
#region Copy
public void CopyURL()
{
if (IsItemSelected) CopyTexts(SelectedItems.Where(x => x.IsURLExist).Select(x => x.Info.Result.URL));
}
public void CopyShortenedURL()
{
if (IsItemSelected) CopyTexts(SelectedItems.Where(x => x.IsShortenedURLExist).Select(x => x.Info.Result.ShortenedURL));
}
public void CopyThumbnailURL()
{
if (IsItemSelected) CopyTexts(SelectedItems.Where(x => x.IsThumbnailURLExist).Select(x => x.Info.Result.ThumbnailURL));
}
public void CopyDeletionURL()
{
if (IsItemSelected) CopyTexts(SelectedItems.Where(x => x.IsDeletionURLExist).Select(x => x.Info.Result.DeletionURL));
}
public void CopyFile()
{
if (IsItemSelected && SelectedItem.IsFileExist) ClipboardHelpers.CopyFile(SelectedItem.Info.FilePath);
}
public void CopyImage()
{
if (IsItemSelected && SelectedItem.IsImageFile) ClipboardHelpers.CopyImageFromFile(SelectedItem.Info.FilePath);
}
public void CopyText()
{
if (IsItemSelected && SelectedItem.IsTextFile) ClipboardHelpers.CopyTextFromFile(SelectedItem.Info.FilePath);
}
public void CopyThumbnailFile()
{
if (IsItemSelected && SelectedItem.IsThumbnailFileExist) ClipboardHelpers.CopyFile(SelectedItem.Info.ThumbnailFilePath);
}
public void CopyThumbnailImage()
{
if (IsItemSelected && SelectedItem.IsThumbnailFileExist) ClipboardHelpers.CopyImageFromFile(SelectedItem.Info.ThumbnailFilePath);
}
2013-11-03 23:53:49 +13:00
public void CopyHTMLLink()
{
if (IsItemSelected) CopyTexts(SelectedItems.Where(x => x.IsURLExist).Select(x => parser.Parse(x.Info, UploadInfoParser.HTMLLink)));
}
public void CopyHTMLImage()
{
if (IsItemSelected) CopyTexts(SelectedItems.Where(x => x.IsImageURL).Select(x => parser.Parse(x.Info, UploadInfoParser.HTMLImage)));
}
public void CopyHTMLLinkedImage()
{
if (IsItemSelected) CopyTexts(SelectedItems.Where(x => x.IsImageURL && x.IsThumbnailURLExist).Select(x => parser.Parse(x.Info, UploadInfoParser.HTMLLinkedImage)));
2013-11-03 23:53:49 +13:00
}
public void CopyForumLink()
{
if (IsItemSelected) CopyTexts(SelectedItems.Where(x => x.IsURLExist).Select(x => parser.Parse(x.Info, UploadInfoParser.ForumLink)));
}
public void CopyForumImage()
{
if (IsItemSelected) CopyTexts(SelectedItems.Where(x => x.IsImageURL).Select(x => parser.Parse(x.Info, UploadInfoParser.ForumImage)));
}
public void CopyForumLinkedImage()
{
if (IsItemSelected) CopyTexts(SelectedItems.Where(x => x.IsImageURL && x.IsThumbnailURLExist).Select(x => parser.Parse(x.Info, UploadInfoParser.ForumLinkedImage)));
2013-11-03 23:53:49 +13:00
}
public void CopyFilePath()
{
if (IsItemSelected) CopyTexts(SelectedItems.Where(x => x.IsFilePathValid).Select(x => x.Info.FilePath));
}
public void CopyFileName()
{
if (IsItemSelected) CopyTexts(SelectedItems.Where(x => x.IsFilePathValid).Select(x => Path.GetFileNameWithoutExtension(x.Info.FilePath)));
}
public void CopyFileNameWithExtension()
{
if (IsItemSelected) CopyTexts(SelectedItems.Where(x => x.IsFilePathValid).Select(x => Path.GetFileName(x.Info.FilePath)));
}
public void CopyFolder()
{
if (IsItemSelected) CopyTexts(SelectedItems.Where(x => x.IsFilePathValid).Select(x => Path.GetDirectoryName(x.Info.FilePath)));
}
public void CopyCustomFormat(string format)
{
if (!string.IsNullOrEmpty(format) && IsItemSelected) CopyTexts(SelectedItems.Where(x => x.IsURLExist).Select(x => parser.Parse(x.Info, format)));
2013-11-03 23:53:49 +13:00
}
public void TryCopy()
{
if (IsItemSelected)
{
if (SelectedItem.IsShortenedURLExist)
{
CopyTexts(SelectedItems.Where(x => x.IsShortenedURLExist).Select(x => x.Info.Result.ShortenedURL));
}
else if (SelectedItem.IsURLExist)
{
CopyTexts(SelectedItems.Where(x => x.IsURLExist).Select(x => x.Info.Result.URL));
}
else if (SelectedItem.IsFilePathValid)
{
CopyTexts(SelectedItems.Where(x => x.IsFilePathValid).Select(x => x.Info.FilePath));
}
}
}
2013-11-03 23:53:49 +13:00
#endregion Copy
#region Other
public void ShowImagePreview()
{
if (IsItemSelected && SelectedItem.IsImageFile) ImageViewer.ShowImage(SelectedItem.Info.FilePath);
}
public void ShowErrors()
{
if (IsItemSelected && SelectedItem.Info.Result != null && SelectedItem.Info.Result.IsError)
{
string errors = SelectedItem.Info.Result.ErrorsToString();
if (!string.IsNullOrEmpty(errors))
{
using (ErrorForm form = new ErrorForm(Resources.UploadInfoManager_ShowErrors_Upload_errors, errors, Program.LogsFilePath, Links.URL_ISSUES))
2013-11-03 23:53:49 +13:00
{
form.ShowDialog();
}
}
}
}
2014-07-28 10:08:37 +12:00
public void Upload()
2014-07-27 23:02:08 +12:00
{
2014-07-28 10:08:37 +12:00
if (IsItemSelected && SelectedItem.IsFileExist) UploadManager.UploadFile(SelectedItem.Info.FilePath);
2014-07-27 23:02:08 +12:00
}
2014-07-28 10:08:37 +12:00
public void EditImage()
{
2014-07-28 10:08:37 +12:00
if (IsItemSelected && SelectedItem.IsImageFile) TaskHelpers.OpenImageEditor(SelectedItem.Info.FilePath);
}
public void DeleteFile()
{
if (IsItemSelected && SelectedItem.IsFileExist) FileSystem.DeleteFile(SelectedItem.Info.FilePath, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
}
public void ShortenURL(UrlShortenerType urlShortener)
{
if (IsItemSelected && SelectedItem.IsURLExist) UploadManager.ShortenURL(SelectedItem.Info.Result.ToString(), urlShortener);
}
2014-07-14 08:59:17 +12:00
public void ShareURL(URLSharingServices urlSharingService)
{
2014-07-14 08:59:17 +12:00
if (IsItemSelected && SelectedItem.IsURLExist) UploadManager.ShareURL(SelectedItem.Info.Result.ToString(), urlSharingService);
}
public void ShowQRCode()
{
if (IsItemSelected && SelectedItem.IsURLExist) new QRCodeForm(SelectedItem.Info.Result.URL).Show();
}
2013-11-03 23:53:49 +13:00
public void ShowResponse()
{
if (IsItemSelected && SelectedItem.Info.Result != null && !string.IsNullOrEmpty(SelectedItem.Info.Result.Response))
{
using (ResponseForm form = new ResponseForm(SelectedItem.Info.Result.Response))
{
form.Icon = ShareXResources.Icon;
2013-11-03 23:53:49 +13:00
form.ShowDialog();
}
}
}
#endregion Other
}
}