-//-----------------------------------------------------------------------
-// <copyright file="Helpers.cs" company="Takayoshi Matsuyama">
-// Copyright (c) Takayoshi Matsuyama. All rights reserved.
-// </copyright>
-//-----------------------------------------------------------------------
-
-// This file is part of Simple Backup.
-//
-// Simple Backup 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 3 of the License, or
-// (at your option) any later version.
-//
-// Simple Backup 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
-
-using System;
-using System.Diagnostics;
-using System.Globalization;
-using System.IO;
-using System.Windows;
-using System.Windows.Threading;
-using SimpleBackup.Resources;
-
-namespace SimpleBackup.Core.Utilities
-{
- /// <summary>
- /// Defines helper methods.
- /// </summary>
- internal static class Helpers
- {
- #region WPF
-
- public static void UpdateWpfGui()
- {
- var frame = new DispatcherFrame();
- var callback = new DispatcherOperationCallback(obj =>
- {
- ((DispatcherFrame)obj).Continue = false;
- return null;
- });
- Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, callback, frame);
- Dispatcher.PushFrame(frame);
- }
-
- #endregion
-
- #region Files and directories
-
- public static bool TryExtractValidFolderPath(string sourcePath, out string extractedPath)
- {
- extractedPath = string.Empty;
-
- if (string.IsNullOrEmpty(sourcePath))
- {
- return false;
- }
-
- string testPath = sourcePath;
- while (true)
- {
- if (Directory.Exists(testPath))
- {
- extractedPath = testPath;
- return true;
- }
-
- testPath = Path.GetDirectoryName(testPath);
- if (string.IsNullOrEmpty(testPath))
- {
- break;
- }
- }
-
- return false;
- }
-
- /// <summary>
- /// Creates the display path text.
- /// </summary>
- /// <param name="fullPath">The full path.</param>
- /// <returns>The display path text.</returns>
- public static string CreateDisplayPathText(string fullPath)
- {
- if (string.IsNullOrWhiteSpace(fullPath))
- {
- return string.Empty;
- }
-
- string fileName = Path.GetFileName(fullPath);
- string directoryName = Path.GetDirectoryName(fullPath);
-
- return string.Format(CultureInfo.CurrentCulture, "{0} ({1})", fileName, directoryName);
- }
-
- public static void OpenFolder(string path)
- {
- if (string.IsNullOrWhiteSpace(path))
- {
- MessageBox.Show(
- string.Format(Resources.Resources.FileAccess_InvalidPathFormat, path));
- return;
- }
-
- if (Directory.Exists(path))
- {
- Process.Start("explorer.exe", string.Format("/n, /e, {0}", path));
- }
- else
- {
- MessageBox.Show(
- string.Format(Resources.Resources.FileAccess_FolderPathNotExistFormat, path));
- }
- }
-
- /// <summary>
- /// Opens the parent folder and select the specified file or folder.
- /// </summary>
- /// <param name="path">The path.</param>
- public static void OpenParentFolderAndSelect(string path)
- {
- if (path.IsExistingFileOrDirectoryPath() == false)
- {
- MessageBox.Show(
- string.Format(Resources.Resources.FileAccess_InvalidPathFormat, path));
- return;
- }
-
- Process.Start("explorer.exe", string.Format("/n, /e, /select,{0}", path));
- }
-
- /// <summary>
- /// Opens the file.
- /// </summary>
- /// <param name="fileName">Name of the file.</param>
- public static void OpenFile(string fileName)
- {
- if (fileName.IsExistingFileOrDirectoryPath() == false)
- {
- MessageBox.Show(
- string.Format(Resources.Resources.FileAccess_InvalidPathFormat, fileName));
- return;
- }
-
- var processStartInfo = new ProcessStartInfo
- {
- FileName = fileName,
- UseShellExecute = true
- };
-
- try
- {
- Process.Start(processStartInfo);
- }
- catch
- {
- MessageBox.Show(
- string.Format(Resources.Resources.FileAccess_FailedToOpenFormat, fileName));
- }
- }
-
- #endregion
- }
-}