2013-11-03 23:53:49 +13:00
|
|
|
/*
|
|
|
|
* Greenshot - a free and open source screenshot tool
|
2016-03-14 05:07:53 +13:00
|
|
|
* Copyright (C) 2007-2015 Thomas Braun, Jens Klingen, Robin Krom
|
2013-11-03 23:53:49 +13:00
|
|
|
*
|
|
|
|
* For more information see: http://getgreenshot.org/
|
|
|
|
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
|
|
|
|
*
|
|
|
|
* 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 1 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
using Greenshot.IniFile;
|
|
|
|
using Greenshot.Plugin;
|
|
|
|
using GreenshotPlugin.Core;
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
namespace GreenshotPlugin.Controls
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Custom dialog for saving images, wraps SaveFileDialog.
|
|
|
|
/// For some reason SFD is sealed :(
|
|
|
|
/// </summary>
|
2014-09-26 14:43:13 +12:00
|
|
|
internal class SaveImageFileDialog : IDisposable
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
|
|
|
|
protected SaveFileDialog SaveFileDialog;
|
|
|
|
private FilterOption[] _filterOptions;
|
|
|
|
private DirectoryInfo _eagerlyCreatedDirectory;
|
|
|
|
private readonly ICaptureDetails _captureDetails = null;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
if (SaveFileDialog != null)
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
SaveFileDialog.Dispose();
|
|
|
|
SaveFileDialog = null;
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public SaveImageFileDialog()
|
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
Init();
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
public SaveImageFileDialog(ICaptureDetails captureDetails)
|
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
_captureDetails = captureDetails;
|
|
|
|
Init();
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
2016-05-23 03:17:02 +12:00
|
|
|
private void Init()
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
SaveFileDialog = new SaveFileDialog();
|
|
|
|
ApplyFilterOptions();
|
2013-11-03 23:53:49 +13:00
|
|
|
string initialDirectory = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
initialDirectory = Path.GetDirectoryName(conf.OutputFileAsFullpath);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
LOG.WarnFormat("OutputFileAsFullpath was set to {0}, ignoring due to problem in path.", conf.OutputFileAsFullpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(initialDirectory) && Directory.Exists(initialDirectory))
|
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
SaveFileDialog.InitialDirectory = initialDirectory;
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
else if (Directory.Exists(conf.OutputFilePath))
|
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
SaveFileDialog.InitialDirectory = conf.OutputFilePath;
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
// The following property fixes a problem that the directory where we save is locked (bug #2899790)
|
2016-05-23 03:17:02 +12:00
|
|
|
SaveFileDialog.RestoreDirectory = true;
|
|
|
|
SaveFileDialog.OverwritePrompt = true;
|
|
|
|
SaveFileDialog.CheckPathExists = false;
|
|
|
|
SaveFileDialog.AddExtension = true;
|
2013-11-03 23:53:49 +13:00
|
|
|
ApplySuggestedValues();
|
|
|
|
}
|
|
|
|
|
2016-05-23 03:17:02 +12:00
|
|
|
private void ApplyFilterOptions()
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
PrepareFilterOptions();
|
2013-11-03 23:53:49 +13:00
|
|
|
string fdf = "";
|
|
|
|
int preselect = 0;
|
2016-03-14 05:07:53 +13:00
|
|
|
var outputFileFormatAsString = Enum.GetName(typeof(OutputFormat), conf.OutputFileFormat);
|
2016-05-23 03:17:02 +12:00
|
|
|
for (int i = 0; i < _filterOptions.Length; i++)
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
FilterOption fo = _filterOptions[i];
|
2013-11-03 23:53:49 +13:00
|
|
|
fdf += fo.Label + "|*." + fo.Extension + "|";
|
2016-03-14 05:07:53 +13:00
|
|
|
if (outputFileFormatAsString == fo.Extension)
|
|
|
|
preselect = i;
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
fdf = fdf.Substring(0, fdf.Length - 1);
|
2016-05-23 03:17:02 +12:00
|
|
|
SaveFileDialog.Filter = fdf;
|
|
|
|
SaveFileDialog.FilterIndex = preselect + 1;
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
2016-05-23 03:17:02 +12:00
|
|
|
private void PrepareFilterOptions()
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
|
|
|
OutputFormat[] supportedImageFormats = (OutputFormat[])Enum.GetValues(typeof(OutputFormat));
|
2016-05-23 03:17:02 +12:00
|
|
|
_filterOptions = new FilterOption[supportedImageFormats.Length];
|
|
|
|
for (int i = 0; i < _filterOptions.Length; i++)
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
|
|
|
string ifo = supportedImageFormats[i].ToString();
|
|
|
|
if (ifo.ToLower().Equals("jpeg")) ifo = "Jpg"; // we dont want no jpeg files, so let the dialog check for jpg
|
2016-05-23 03:17:02 +12:00
|
|
|
FilterOption fo = new FilterOption
|
|
|
|
{
|
|
|
|
Label = ifo.ToUpper(),
|
|
|
|
Extension = ifo.ToLower()
|
|
|
|
};
|
|
|
|
_filterOptions.SetValue(fo, i);
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// filename exactly as typed in the filename field
|
|
|
|
/// </summary>
|
|
|
|
public string FileName
|
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
get { return SaveFileDialog.FileName; }
|
|
|
|
set { SaveFileDialog.FileName = value; }
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// initial directory of the dialog
|
|
|
|
/// </summary>
|
|
|
|
public string InitialDirectory
|
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
get { return SaveFileDialog.InitialDirectory; }
|
|
|
|
set { SaveFileDialog.InitialDirectory = value; }
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// returns filename as typed in the filename field with extension.
|
|
|
|
/// if filename field value ends with selected extension, the value is just returned.
|
|
|
|
/// otherwise, the selected extension is appended to the filename.
|
|
|
|
/// </summary>
|
|
|
|
public string FileNameWithExtension
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
string fn = SaveFileDialog.FileName;
|
2013-11-03 23:53:49 +13:00
|
|
|
// if the filename contains a valid extension, which is the same like the selected filter item's extension, the filename is okay
|
|
|
|
if (fn.EndsWith(Extension, StringComparison.CurrentCultureIgnoreCase)) return fn;
|
|
|
|
// otherwise we just add the selected filter item's extension
|
|
|
|
else return fn + "." + Extension;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
FileName = Path.GetFileNameWithoutExtension(value);
|
|
|
|
Extension = Path.GetExtension(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// gets or sets selected extension
|
|
|
|
/// </summary>
|
|
|
|
public string Extension
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
return _filterOptions[SaveFileDialog.FilterIndex - 1].Extension;
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
for (int i = 0; i < _filterOptions.Length; i++)
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
if (value.Equals(_filterOptions[i].Extension, StringComparison.CurrentCultureIgnoreCase))
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
SaveFileDialog.FilterIndex = i + 1;
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public DialogResult ShowDialog()
|
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
DialogResult ret = SaveFileDialog.ShowDialog();
|
2013-11-03 23:53:49 +13:00
|
|
|
CleanUp();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// sets InitialDirectory and FileName property of a SaveFileDialog smartly, considering default pattern and last used path
|
|
|
|
/// </summary>
|
|
|
|
private void ApplySuggestedValues()
|
|
|
|
{
|
|
|
|
// build the full path and set dialog properties
|
2016-05-23 03:17:02 +12:00
|
|
|
FileName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(conf.OutputFileFilenamePattern, _captureDetails);
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
private class FilterOption
|
|
|
|
{
|
|
|
|
public string Label;
|
|
|
|
public string Extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CleanUp()
|
|
|
|
{
|
|
|
|
// fix for bug #3379053
|
|
|
|
try
|
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
if (_eagerlyCreatedDirectory != null && _eagerlyCreatedDirectory.GetFiles().Length == 0 && _eagerlyCreatedDirectory.GetDirectories().Length == 0)
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
2016-05-23 03:17:02 +12:00
|
|
|
_eagerlyCreatedDirectory.Delete();
|
|
|
|
_eagerlyCreatedDirectory = null;
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
LOG.WarnFormat("Couldn't cleanup directory due to: {0}", e.Message);
|
2016-05-23 03:17:02 +12:00
|
|
|
_eagerlyCreatedDirectory = null;
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|