/// <summary>
        /// Loads recent template file from disk
        /// </summary>
        private void OnLoadRecentTemplate(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            if (!File.Exists(path))
            {
                DialogManager.ShowErrorDialog("Failed to find file " + path);
                RecentMenuManager.RemoveFileFromRecentList(this.RecentFiles, path);
                return;
            }

            this.LoadTemplateFromFile(path);
        }
        /// <summary>
        /// Run all needed logic on app shutdown
        /// </summary>
        /// <returns>True if all tabs closed and ready for shutdown, false is process was cancelled</returns>
        public bool CleanUpOnClosing()
        {
            RecentMenuManager.UpdateRecentFiles(this.RecentFiles.ToList());

            // Manually close all tabs on app shut down to provoce asking for save
            while (this.SelectedTab != null)
            {
                bool res = this.OnCloseTab();

                // in case closing was cancelled, break
                if (!res)
                {
                    return(false);
                }
            }

            return(true);
        }
        /// <summary>
        /// Saves As.. template
        /// </summary>
        private void OnSaveAsTemplate()
        {
            string suggestedName = string.Empty;

            if (this.SelectedTab is TemplateViewModel)
            {
                TemplateViewModel template = this.SelectedTab as TemplateViewModel;
                suggestedName = template.TemplateName;
            }

            string savePath = DialogManager.ShowSaveTemplateDialog(suggestedName);

            if (savePath == null)
            {
                return;
            }

            RecentMenuManager.AddFileNameToRecentList(this.RecentFiles, savePath);
            this.SaveTemplateByPath(savePath);
        }
        /// <summary>
        /// Loads template by specified path
        /// </summary>
        /// <param name="file">Path to template file</param>
        private void LoadTemplateFromFile(string file)
        {
            // return if there is active tabs and action was cancelled
            if (!this.CloseActiveTemplateTab())
            {
                return;
            }

            if (!File.Exists(file))
            {
                DialogManager.ShowErrorDialog("Failed to find file " + file + ".");
                return;
            }

            string directory = Path.GetDirectoryName(file);

            if (string.IsNullOrEmpty(directory))
            {
                DialogManager.ShowErrorDialog("Failed to load template image.");
                return;
            }

            string templateName = Path.GetFileNameWithoutExtension(file);

            // find files with image extension and template name in the same directory
            List <string> imageFiles = DialogManager.GetImageFilesFromDirectory(directory)
                                       .Where(x => Path.GetFileNameWithoutExtension(x).Equals(templateName)).ToList();

            if (imageFiles.Count < 1)
            {
                DialogManager.ShowErrorDialog("Failed to find template image.");
                return;
            }

            if (imageFiles.Count > 1)
            {
                DialogManager.ShowErrorDialog("Failed to load template image. Found several files with name: " +
                                              Path.GetFileNameWithoutExtension(file) + ".");
                return;
            }

            // load and deserialize template data
            try
            {
                string            jsonString        = File.ReadAllText(file);
                TemplateViewModel templateViewModel = TemplateSerializer.JsonToTemplate(jsonString);

                // if no name in template, use name of the file
                if (string.IsNullOrEmpty(templateViewModel.TemplateName))
                {
                    templateViewModel.TemplateName = Path.GetFileNameWithoutExtension(file);
                }

                // load image and check if it was loaded
                bool imageLoaded = templateViewModel.LoadTemplateImageFromFile(imageFiles[0]);
                if (!imageLoaded)
                {
                    return;
                }

                templateViewModel.LoadedPath = file;
                templateViewModel.IsDirty    = false;

                this.CloseActiveTemplateTab();

                this.AddTab(templateViewModel);

                RecentMenuManager.AddFileNameToRecentList(this.RecentFiles, file);
            }
            catch (SerializationException e)
            {
                DialogManager.ShowErrorDialog("Failed to read or deserialize the template.\nReason: " + e.Message);
                return;
            }
            catch (Exception e)
            {
                DialogManager.ShowErrorDialog("Unknown error while loading the template.\nError details: " + e.Message);
                return;
            }
        }