Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Copy WallpaperFusion Images

Description
Archives your WallpaperFusion images.
Language
C#.net
Minimum Version
Created By
Zeusthrax800254
Contributors
-
Date Created
Feb 9, 2015
Date Last Modified
Feb 9, 2015

Scripted Function (Macro) Code

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

public static class DisplayFusionFunction
{
	public static void Run(IntPtr windowHandle)
	{
		string sourcePath = GetSettingValue("CopyWallpaperSourcePath", Path.Combine("DisplayFusion", "Web Images"), false, false);
		string destinationPath = GetSettingValue("CopyWallpaperDestinationPath", string.Empty, true, false);

		foreach(string fileName in CopyWallpaper(sourcePath,destinationPath))
			Process.Start(fileName);
	}


	public static IEnumerable<string> CopyWallpaper(string sourcePath, string destinationPath)
	{
		foreach (string file in Directory.GetFiles(sourcePath, "WallpaperFusion Image - *.jpg"))
		{
			foreach (Match match in Regex.Matches(file, @"WallpaperFusion Image - (?<WallpaperName>.*) (?<TimeStamp>\(.*\))(?<Extension>\.jpg)"))
			{
				if (match.Success && (match.Groups.Count > 2))
				{
					string sourceFile = Path.Combine(sourcePath, file);
					string tempName = Path.Combine(destinationPath,
					string.Format("{0}{1}", Guid.NewGuid(), match.Groups["Extension"].Value));
					File.Copy(sourceFile, tempName);

					KeyValuePair<DialogResult, string> result = GetDestinationFileName(destinationPath, match);

					if (result.Key == DialogResult.OK)
					{
						File.Copy(tempName, result.Value, true);
						File.Delete(tempName);
						yield return result.Value;
					}
				}
			}
		}
	}

	private static KeyValuePair<DialogResult, string> GetDestinationFileName(string destinationPath, Match match)
	{
		string destinationFileName = Path.Combine(destinationPath,
		string.Format("{0}{1}", match.Groups["WallpaperName"].Value, match.Groups["Extension"].Value));

		if (File.Exists(destinationFileName))
		{
			destinationFileName = Path.Combine(destinationPath,
			string.Format("{0} {1}{2}", match.Groups["WallpaperName"].Value,
			match.Groups["TimeStamp"].Value, match.Groups["Extension"].Value));
		}

		if (File.Exists(destinationFileName))
		{
			using (SaveFileDialog dialog = new SaveFileDialog())
			{
				dialog.AddExtension = true;
				dialog.InitialDirectory = destinationPath;
				dialog.Filter = "jpg (*.jpg)|*.jpg";
				dialog.FileName = Path.GetFileName(destinationFileName);
				if (dialog.ShowDialog() != DialogResult.OK)
					return new KeyValuePair<DialogResult, string>(DialogResult.Abort, destinationFileName);

				destinationPath = dialog.FileName;
			}
		}

		return new KeyValuePair<DialogResult, string>(DialogResult.OK, destinationFileName);
	}

	private static string GetSettingValue(string key, string defaultPath, bool forceSelection, bool allowNew)
	{
		string value = BFS.ScriptSettings.ReadValue(key);
		if (string.IsNullOrEmpty(value))
		{
			value = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), defaultPath);
			if (forceSelection || (!Directory.Exists(value)))
				value=GetFolderPathFromUser(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), allowNew);
		
			BFS.ScriptSettings.WriteValue(key, value);
		}
		
		return value;
	}

	private static string GetFolderPathFromUser(string sourcePath, bool allowNew)
	{
		using (FolderBrowserDialog dialog = new FolderBrowserDialog())
		{
			dialog.Description = "Select Directory for Source";
			dialog.SelectedPath = sourcePath;
			dialog.ShowNewFolderButton = allowNew;

			if (dialog.ShowDialog() == DialogResult.OK)
				sourcePath = dialog.SelectedPath;

			return sourcePath;
		}
	}
}