<repo_name>4lk4tr43/CordovaManager<file_sep>/Cordova Manager/Controls/AppDetails.xaml.cs
﻿using System;
using System.Xml;

namespace Cordova_Manager.Controls
{
    /// <summary>
    ///     Displays and updates Cordova project properties.
    /// </summary>
    public partial class AppDetails
    {
        public AppDetails()
        {
            App.AppDetails = this;

            InitializeComponent();
        }

        public class Data
        {
            private string _filePath;

            private string _author;
            private string _authorEmail;
            private string _authorWebPage;
            private string _description;
            private string _id;
            private string _name;
            private string _version;

            public string Author
            {
                get { return _author; }
                set { _author = _author == value ? value : SaveToXml("author", null, value); }
            }

            public string AuthorEmail
            {
                get { return _authorEmail; }
                set { _authorEmail = _authorEmail == value ? value : SaveToXml("author", "email", value); }
            }

            public string AuthorWebPage
            {
                get { return _authorWebPage; }
                set { _authorWebPage = _authorWebPage == value ? value : SaveToXml("author", "href", value); }
            }

            public string Description
            {
                get { return _description; }
                set { _description = _description == value ? value : SaveToXml("description", null, value); }
            }

            public string Id
            {
                get { return _id; }
                set { _id = _id == value ? value : SaveToXml("widget", "id", value); }
            }

            public string Name
            {
                get { return _name; }
                set { _name = _name == value ? value : SaveToXml("name", null, value); }
            }

            public string Version
            {
                get { return _version; }
                set { _version = _version == value ? value : SaveToXml("widget", "version", value); }
            }

            public static Data LoadFromFile(string path)
            {
                var document = new XmlDocument();
                document.Load(path);

                var widget = document["widget"];
                var data = new Data
                {
                    _filePath = path,
 // ReSharper disable PossibleNullReferenceException
                    Name = widget["name"].InnerText,
                    Id = widget.Attributes["id"].Value,
                    Version = widget.Attributes["version"].Value,
                    Description = widget["description"].InnerText,
                    Author = widget["author"].InnerText,
                    AuthorEmail = widget["author"].Attributes["email"].Value,
                    AuthorWebPage = widget["author"].Attributes["href"].Value
// ReSharper restore PossibleNullReferenceException
                };

                return data;
            }

            private string SaveToXml(string tag, string attribute, string value)
            {
                var document = new XmlDocument();
                document.Load(_filePath);
                var widget = document["widget"];

                if (tag == "widget")
                {
                    if (string.IsNullOrEmpty(attribute)) throw new Exception();
// ReSharper disable PossibleNullReferenceException
                    widget.Attributes[attribute].Value = value;
                }
                else
                {
                    if (string.IsNullOrEmpty(attribute))
                    {
                        widget[tag].InnerText = value;
                    }
                    else
                    {
                        widget[tag].Attributes[attribute].Value = value;
// ReSharper restore PossibleNullReferenceException
                    }
                }

                document.Save(_filePath);
                return value;
            }
        }
    }
}<file_sep>/Cordova Manager/Controls/RenameApp.xaml.cs
﻿using System.IO;
using System.Windows;

namespace Cordova_Manager.Controls
{
    /// <summary>
    /// Interaction logic for RenameApp.xaml
    /// </summary>
    public partial class RenameApp : Window
    {
        public RenameApp()
        {
            InitializeComponent();
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            var source = ((DirectoryInfo) App.AppBrowser.ListBoxApps.SelectedItem).FullName;
            var destination = Properties.Settings.Default.RootFolderPath + "\\" + TextBoxName.Text;
            if (Directory.Exists(destination))
            {
                MessageBoxResult result = MessageBox.Show(this, "Directory already exists, do you want to delete it?",
                    "Directory collision", MessageBoxButton.YesNo);

                if (result == MessageBoxResult.Yes)
                {
                    Directory.Delete(destination, true);
                    App.TextBoxConsole.Text += "\n" + "Deleted directory \"" + destination + "\"";
                }
                else
                {
                    Close();
                    return;
                }
            }

            Directory.Move(source, destination);
            App.TextBoxConsole.Text += "\n" + "Renamed directory \"" + source + "\"" + " to \"" + destination + "\"";
            App.AppBrowser.RefreshAppsList();
            Close();
        }

        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
<file_sep>/Cordova Manager/Controls/AppBrowser.xaml.cs
﻿using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using Cordova_Manager.Helpers;
using Cordova_Manager.Properties;
using Helpers.IO;
using ListBox = System.Windows.Controls.ListBox;
using MessageBox = System.Windows.MessageBox;

namespace Cordova_Manager.Controls
{
    /// <summary>
    ///     Interaction logic for AppBrowser.xaml
    /// </summary>
    public partial class AppBrowser
    {
        public AppBrowser()
        {
            App.AppBrowser = this;

            InitializeComponent();

            RefreshAppsList();
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            RefreshAppsList();
        }

        public void RefreshAppsList()
        {
            if (string.IsNullOrEmpty(Settings.Default.RootFolderPath)) return;

            var directory = new DirectoryInfo(Settings.Default.RootFolderPath);

            if (string.IsNullOrWhiteSpace(TextBoxSearch.Text))
            {
                ListBoxApps.ItemsSource = directory.GetDirectories()
                    .Where(subdirectory =>
                        Cordova.IsCordovaDirectory(subdirectory.FullName));
            }
            else
            {
                ListBoxApps.ItemsSource = directory.GetDirectories()
                    .Where(subdirectory =>
                        Cordova.IsCordovaDirectory(subdirectory.FullName) &&
                        subdirectory.Name.ToLower().Contains(TextBoxSearch.Text.ToLower()));
            }
        }

        private void ButtonCreateNewProject_Click(object sender, RoutedEventArgs e)
        {
            var window = new CreateApp { Owner = Window.GetWindow(this) };
            window.ShowDialog();
        }

        private void ButtonDeleteProject_Click(object sender, RoutedEventArgs e)
        {
            string path = ((DirectoryInfo) ListBoxApps.SelectedItem).FullName;
            Window owner = Window.GetWindow(this);
            if (owner == null) throw new Exception();
            MessageBoxResult result = MessageBox.Show(owner, "Do you want to delete \"" + path + "\"",
                "Delete Application", MessageBoxButton.YesNo);
            if (result == MessageBoxResult.Yes)
            {
                if (Directory.Exists(path))
                {
                    Directory.Delete(path, true);
                    App.TextBoxConsole.Text += "\n" + "Deleted directory \"" + path + "\"";
                }

                RefreshAppsList();
            }
        }

        private void ButtonCopyProject_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new FolderBrowserDialog();
            if (DialogResult.OK == dialog.ShowDialog())
            {
                var directory = (DirectoryInfo) ListBoxApps.SelectedItem;
                directory.Copy(dialog.SelectedPath + directory.Name);

                App.TextBoxConsole.Text += "\n" + "Copied application \"" + directory.Name + "\" to \"" +
                                           dialog.SelectedPath + "\"";
                RefreshAppsList();
            }
        }

        private void ButtonRenameProject_Click(object sender, RoutedEventArgs e)
        {
            var window = new RenameApp { Owner = Window.GetWindow(this) };
            window.ShowDialog();
        }
    }
}<file_sep>/Cordova Manager/Controls/CreateApp.xaml.cs
﻿using Cordova_Manager.Helpers;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using Helpers.Windows;

namespace Cordova_Manager.Controls
{
    public partial class CreateApp
    {
        public CreateApp()
        {
            InitializeComponent();

            TextBoxBasePath.Text = Properties.Settings.Default.RootFolderPath + "\\";
        }

        private void TextBoxName_TextChanged(object sender, TextChangedEventArgs e)
        {
            string name = TextBoxName.Text;
            TextBoxExtensionPath.Text = name;

            string id = TextBoxId.Text;
            if (string.IsNullOrWhiteSpace(id))
            {
                TextBoxId.Text = "io.cordova." + name;
            }
            else if (id.Contains("."))
            {
                int index = id.LastIndexOf(".", StringComparison.Ordinal);
                TextBoxId.Text = id.Substring(0, index + 1) + name;
            }
            else
            {
                TextBoxId.Text = TextBoxId.Text + "." + name;
            }
        }

        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            string path = TextBoxBasePath.Text + TextBoxExtensionPath.Text;
            if (Directory.Exists(path))
            {
                MessageBoxResult result = MessageBox.Show(this, "Directory already exists, do you want to delete it?",
                    "Directory collision", MessageBoxButton.YesNo);

                if (result == MessageBoxResult.Yes)
                {
                    Directory.Delete(path, true);
                    App.TextBoxConsole.Text += "\n" + "Deleted directory \"" + path + "\"";
                }
                else
                {
                    Close();
                    return;
                }
            }
            
            App.TextBoxConsole.Text += "\n" + CommandPrompt.Run(CordovaCommands.CreateApp(path, TextBoxName.Text, TextBoxId.Text));
            App.AppBrowser.RefreshAppsList();
            Close();
        }
    }
}<file_sep>/Helpers/IO/DirectoryInfoExtensions.cs
﻿using System.IO;

namespace Helpers.IO
{
    public static class DirectoryInfoExtensions
    {
        public static void Copy(this DirectoryInfo self, string destinationPath,
            bool copySubDirectories = true, bool overwriteFiles = false)
        {
            var directory = new DirectoryInfo(self.FullName);

            if (!directory.Exists) throw new DirectoryNotFoundException();
            if (!Directory.Exists(destinationPath)) Directory.CreateDirectory(destinationPath);

            FileInfo[] files = directory.GetFiles();
            foreach (FileInfo file in files)
            {
                string destinationFilePath = Path.Combine(destinationPath, file.Name);
                file.CopyTo(destinationFilePath, overwriteFiles);
            }

            if (copySubDirectories)
            {
                foreach (DirectoryInfo subdir in directory.GetDirectories())
                {
                    string temppath = Path.Combine(destinationPath, subdir.Name);
                    subdir.Copy(temppath, true, overwriteFiles);
                }
            }
        }
    }
}<file_sep>/Helpers/Converters/IndexToBoolConverter.cs
﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace Helpers.Converters
{
    public class IndexToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var index = (int) value;
            return index >= 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var enabled = (bool) value;
            return enabled ? 0 : -1;
        }
    }
}
<file_sep>/Cordova Manager/App.xaml.cs
﻿using System.Windows.Controls;
using Cordova_Manager.Controls;

namespace Cordova_Manager
{
    public partial class App
    {
        public static TextBox TextBoxConsole { get; set; }
        public static AppBrowser AppBrowser { get; set; }
        public static AppDetails AppDetails { get; set; }
    }
}<file_sep>/Cordova Manager/MainWindow.xaml.cs
﻿using System;
using Cordova_Manager.Controls;

namespace Cordova_Manager
{
    /// <summary>
    ///     Manager startup window.
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            if (string.IsNullOrEmpty(Properties.Settings.Default.RootFolderPath))
            {
                Properties.Settings.Default.RootFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            }

            InitializeComponent();

            App.TextBoxConsole = TextBoxConsole;
        }
    }
}<file_sep>/Cordova Manager/Controls/ManagerSettings.xaml.cs
﻿using Cordova_Manager.Properties;
using System.Windows;
using System.Windows.Forms;

namespace Cordova_Manager.Controls
{
    /// <summary>
    ///     Displays and updates the managers settings.
    /// </summary>
    public partial class ManagerSettings
    {
        // TODO Install menu

        public ManagerSettings()
        {
            InitializeComponent();
        }

        private void ButtonBrowseRootDirectory_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new FolderBrowserDialog();
            if (DialogResult.OK == dialog.ShowDialog())
            {
                Settings.Default.RootFolderPath = dialog.SelectedPath;
                Settings.Default.Save();
                App.AppBrowser.RefreshAppsList();
            }
        }
    }
}<file_sep>/Cordova Manager/Helpers/CordovaCommands.cs
namespace Cordova_Manager.Helpers
{
    internal static class CordovaCommands
    {
        public static string CreateApp(string path,
            string name = "",
            string id = "io.cordova.project")
        {
            string processedName = string.IsNullOrWhiteSpace(name) ? "" : " " + name;
            string processedUrl = string.IsNullOrWhiteSpace(id) ? "" : " " + id;
            return string.Format("cordova create {0}{1}{2}", path, processedUrl, processedName);
        }
    }
}<file_sep>/Cordova Manager/Helpers/Cordova.cs
﻿using System.IO;

namespace Cordova_Manager.Helpers
{
    internal static class Cordova
    {
        public static bool IsCordovaDirectory(string path)
        {
            return File.Exists(path + "\\config.xml") &&
                   Directory.Exists(path + "\\www") && 
                   Directory.Exists(path + "\\plugins") &&
                   Directory.Exists(path + "\\platforms") &&
                   Directory.Exists(path + "\\hooks");
        }
    }
}<file_sep>/Helpers/Windows/CommandPrompt.cs
using System.Diagnostics;

namespace Helpers.Windows
{
    public static class CommandPrompt
    {
        public static string Run(string command)
        {
            var startInfo = new ProcessStartInfo
            {
                FileName = "cmd.exe",
                Arguments = "/c \"" + command + "\"",
                RedirectStandardOutput = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };

            var process = new Process
            {
                StartInfo = startInfo
            };

            process.Start();
            return process.StandardOutput.ReadToEnd();
        }
    }
}